Skip to content

Commit

Permalink
Assets handling.
Browse files Browse the repository at this point in the history
Auto registration and enqueuing of assets.
  • Loading branch information
amostajo committed Oct 27, 2016
1 parent 49fe3b1 commit 1df97d0
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/lib/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @copyright 10Quality <http://www.10quality.com>
* @license MIT
* @package WPMVC
* @version 2.0.4
* @version 2.0.7
*/

if ( ! function_exists( 'resize_image' ) ) {
Expand Down Expand Up @@ -77,6 +77,8 @@ function assets_url( $path, $file )
$route = preg_replace( '/.+?(?=wp-content)/', '', $route );
// Clean project relative path
$route = preg_replace( '/\/app[\/\\A-Za-z0-9\.\\-]+/', '', $route );
$route = preg_replace( '/\/assets[\/\\A-Za-z0-9\.\\-]+/', '', $route );
$route = preg_replace( '/\/vendor[\/\\A-Za-z0-9\.\\-]+/', '', $route );
return $url.'/'.apply_filters( 'app_route', $route ).'/assets/'.$path;
}
}
Expand Down Expand Up @@ -119,4 +121,30 @@ function get_wp_home_path()
? get_home_path()
: preg_replace( '/wp-content[A-Za-z0-9\.\-\\\_]+/', '', __DIR__ );
}
}

if ( ! function_exists( 'assets_path' ) ) {
/**
* Returns path of asset located in a theme or plugin.
* @since 1.0.1
* @since 2.0.4 Refactored to work with new structure.
*
* @param string $relative Asset relative path.
* @param string $file File location path.
*
* @return string URL
*/
function assets_path( $relative, $file )
{
// Preparation
$route = preg_replace( '/\\\\/', '/', $file );
$path = rtrim( preg_replace( '/\\\\/', '/', get_home_path() ), '/' );
// Clean base path
$route = preg_replace( '/.+?(?=wp-content)/', '', $route );
// Clean project relative path
$route = preg_replace( '/\/app[\/\\A-Za-z0-9\.\\-]+/', '', $route );
$route = preg_replace( '/\/assets[\/\\A-Za-z0-9\.\\-]+/', '', $route );
$route = preg_replace( '/\/vendor[\/\\A-Za-z0-9\.\\-]+/', '', $route );
return $path.'/'.apply_filters( 'app_route_path', $route ).'/assets/'.$relative;
}
}
101 changes: 101 additions & 0 deletions src/psr4/Bridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use WPMVC\Log;
use WPMVC\Contracts\Plugable;
use WPMVC\MVC\Engine;
use TenQuality\WP\File;
use Exception;

/**
Expand Down Expand Up @@ -78,6 +79,13 @@ abstract class Bridge implements Plugable
*/
protected $models;

/**
* List of assets to register or enqueue.
* @var array
* @since 2.0.7
*/
protected $assets;

/**
* List of Models that requires bridge processing to function.
* @var array
Expand All @@ -90,6 +98,7 @@ abstract class Bridge implements Plugable
* @since 1.0.0
* @since 2.0.3 Cache and log are obligatory configuration settings.
* @since 2.0.4 Added models.
* @since 2.0.7 Added assets.
*
* @param array $config Configuration options.
*/
Expand All @@ -100,6 +109,7 @@ public function __construct( Config $config )
$this->shortcodes = [];
$this->widgets = [];
$this->models = [];
$this->assets = [];
$this->_automatedModels = [];
$this->config = $config;
$this->mvc = new Engine(
Expand All @@ -111,6 +121,7 @@ public function __construct( Config $config )
$this->set_addons();
Cache::init( $this->config );
Log::init( $this->config );
$this->checkAssets();
}

/**
Expand Down Expand Up @@ -354,10 +365,32 @@ public function add_model( $class )
$this->models[] = $class;
}

/**
* Adds an asset for registration.
* @since 2.0.7
*
* @param string $asset Asset relative path (within assets forlder).
* @param bool $enqueue Flag that indicates if asset should be enqueued upon registration.
* @param array $dep Dependencies.
* @param bool $footer Flag that indicates if asset should enqueue at page footer.
*/
public function add_asset( $asset, $enqueue = true, $dep = [], $footer = null )
{
if ( $footer === null )
$footer = preg_match( '/\.js/', $asset );
$this->assets[] = [
'path' => $asset,
'enqueue' => $enqueue,
'dep' => $dep,
'footer' => $footer,
];
}

/**
* Adds hooks and filters into Wordpress core.
* @since 1.0.3
* @since 2.0.4 Added models.
* @since 2.0.7 Added assets.
*/
public function add_hooks()
{
Expand Down Expand Up @@ -398,6 +431,10 @@ public function add_hooks()
if ( count( $this->models ) > 0 ) {
add_action( 'init', [ &$this, '_models' ], 1 );
}
// Assets
if ( count( $this->assets ) > 0 ) {
add_action( 'wp_enqueue_scripts', [ &$this, '_assets' ], 10 );
}
}
}

Expand Down Expand Up @@ -489,6 +526,53 @@ public function _models()
add_action( 'add_meta_boxes', [ &$this, '_metabox' ] );
}

/**
* Enqueues assets registered in class.
* @since 2.0.7
*/
public function _assets()
{
$version = $this->config->get('version') ? $this->config->get('version') : '1.0.0';
foreach ( $this->assets as $asset ) {
$name = strtolower( preg_replace( '/css|js|\/|\.min|\./', '', $asset['path'] ) )
.'-'.strtolower( $this->config->get('namespace') );
// Styles
if ( preg_match( '/\.css/', $asset['path'] ) ) {
wp_register_style(
$name,
assets_url( $asset['path'], __DIR__ ),
$asset['dep'],
$version
);
if ($asset['enqueue'])
wp_enqueue_style(
$name,
assets_url( $asset['path'], __DIR__ ),
$asset['dep'],
$version,
$asset['footer']
);
}
// Scripts
if ( preg_match( '/\.js/', $asset['path'] ) ) {
wp_register_script(
$name,
assets_url( $asset['path'], __DIR__ ),
$asset['dep'],
$version
);
if ($asset['enqueue'])
wp_enqueue_scripts(
$name,
assets_url( $asset['path'], __DIR__ ),
$asset['dep'],
$version,
$asset['footer']
);
}
}
}

/**
* Returns class method call mapped to a mvc engine method.
* @since 1.0.3
Expand Down Expand Up @@ -608,4 +692,21 @@ private function _save( $type, $args )
);
}
}

/**
* Checks if generated assets exist or not.
* @since 2.0.7
*/
private function checkAssets()
{
if ( $this->config->get( 'autoenqueue' )
&& $this->config->get( 'autoenqueue' ) === true
) {
$file = File::auth();
if ( $file->exists( assets_path( 'js/app.js', __DIR__ ) ) )
$this->add_asset( 'js/app.js' );
if ( $file->exists( assets_path( 'css/app.css', __DIR__ ) ) )
$this->add_asset( 'css/app.css' );
}
}
}

0 comments on commit 1df97d0

Please sign in to comment.