Skip to content

Commit

Permalink
Allow for setting a path without dynamic directories by file type
Browse files Browse the repository at this point in the history
  • Loading branch information
borkweb committed Sep 15, 2023
1 parent 9478691 commit 5c8f06c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 23 deletions.
71 changes: 48 additions & 23 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@ class Asset {
*/
protected bool $should_print = false;

/**
* Whether to use the asset directory prefix based on asset type.
*
* @var bool
*/
protected bool $should_use_asset_directory_prefix = true;

/**
* The asset slug.
*
Expand Down Expand Up @@ -323,28 +330,44 @@ protected function build_asset_url(): string {
$extension = $type;
}

$should_prefix = $this->should_use_asset_directory_prefix;

if ( is_null( $resource_path ) ) {
$resources_path = $this->get_path();
switch ( $extension ) {
case 'css':
$resources_path = preg_replace( '#/css/$#', '/', $resources_path );
$resource_path = "{$resources_path}css/";
break;
case 'js':
$resources_path = preg_replace( '#/js/$#', '/', $resources_path );
$resource_path = "{$resources_path}js/";
break;
case 'scss':
$resources_path = preg_replace( '#/scss/$#', '/', $resources_path );
$resource_path = "{$resources_path}scss/";
break;
case 'pcss':
$resources_path = preg_replace( '#/postcss/$#', '/', $resources_path );
$resource_path = "{$resources_path}postcss/";
break;
default:
$resource_path = $resources_path;
break;
$resource_path = $resources_path;

if ( $should_prefix ) {
$prefix_dir = '';

switch ( $extension ) {
case 'css':
$prefix_dir = 'css';
$resources_path = preg_replace( '#/css/$#', '/', $resources_path );
$resource_path = "{$resources_path}css/";
break;
case 'js':
$prefix_dir = 'js';
$resources_path = preg_replace( '#/js/$#', '/', $resources_path );
$resource_path = "{$resources_path}js/";
break;
case 'scss':
$prefix_dir = 'scss';
$resources_path = preg_replace( '#/scss/$#', '/', $resources_path );
$resource_path = "{$resources_path}scss/";
break;
case 'pcss':
$prefix_dir = 'postcss';
$resources_path = preg_replace( '#/postcss/$#', '/', $resources_path );
$resource_path = "{$resources_path}postcss/";
break;
default:
$resource_path = $resources_path;
break;
}

if ( $prefix_dir && strpos( $resource, $prefix_dir . '/' ) === 0 ) {
$resource = substr( $resource, strlen( $prefix_dir . '/' ) );
}
}
}

Expand Down Expand Up @@ -1027,12 +1050,14 @@ public function set_as_enqueued() {
*
* @since 1.0.0
*
* @param string|null $path The path to the minified file.
* @param string|null $path The path to the minified file.
* @param bool $should_automatically_use_asset_type_directory_prefix Whether to prefix files automatically by type (e.g. js/ for JS). Defaults to true.
*
* @return $this
*/
public function set_path( ?string $path = null ) {
$this->path = trailingslashit( $path );
public function set_path( ?string $path = null, bool $should_automatically_use_asset_type_directory_prefix = true ) {
$this->path = trailingslashit( $path );
$this->should_use_asset_directory_prefix = $should_automatically_use_asset_type_directory_prefix;
return $this;
}

Expand Down
Empty file added tests/_data/unnested.css
Empty file.
36 changes: 36 additions & 0 deletions tests/acceptance/EnqueueCSSCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,42 @@ public function it_should_enqueue_from_alternate_path_with_css_in_path( Acceptan
$I->seeElement( 'link', [ 'href' => 'http://wordpress.test/wp-content/plugins/assets/tests/_data/other-asset-root/css/fake-alt.css?ver=1.0.0' ] );
}

public function it_should_enqueue_from_alternate_path_with_css_in_file_ref( AcceptanceTester $I ) {
$code = file_get_contents( codecept_data_dir( 'enqueue-template.php' ) );
$code .= <<<PHP
add_action( 'wp_enqueue_scripts', function() {
Asset::add( 'fake-css', 'css/fake-alt.css' )
->enqueue_on( 'wp_enqueue_scripts' )
->set_path( 'tests/_data/other-asset-root' )
->register();
}, 100 );
PHP;

$I->haveMuPlugin( 'enqueue.php', $code );


$I->amOnPage( '/' );
$I->seeElement( 'link', [ 'href' => 'http://wordpress.test/wp-content/plugins/assets/tests/_data/other-asset-root/css/fake-alt.css?ver=1.0.0' ] );
}

public function it_should_enqueue_from_alternate_path_with_without_prefix_dir( AcceptanceTester $I ) {
$code = file_get_contents( codecept_data_dir( 'enqueue-template.php' ) );
$code .= <<<PHP
add_action( 'wp_enqueue_scripts', function() {
Asset::add( 'fake-css', 'unnested.css' )
->enqueue_on( 'wp_enqueue_scripts' )
->set_path( 'tests/_data', false )
->register();
}, 100 );
PHP;

$I->haveMuPlugin( 'enqueue.php', $code );


$I->amOnPage( '/' );
$I->seeElement( 'link', [ 'href' => 'http://wordpress.test/wp-content/plugins/assets/tests/_data/unnested.css?ver=1.0.0' ] );
}

public function it_should_enqueue_min( AcceptanceTester $I ) {
$code = file_get_contents( codecept_data_dir( 'enqueue-template.php' ) );
$code .= <<<PHP
Expand Down

0 comments on commit 5c8f06c

Please sign in to comment.