Skip to content

Commit b0432d1

Browse files
authored
Merge pull request #28 from stellarwp/feat/add-to-group-path
Handle later calls to the add_to_group_path method
2 parents 6c26d08 + 7bef433 commit b0432d1

File tree

3 files changed

+77
-6
lines changed

3 files changed

+77
-6
lines changed

src/Assets/Asset.php

+44-5
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ class Asset {
186186
* 2. If a path group is set, that will be used.
187187
* 3. Otherwise, the root path will be used.
188188
*
189+
* In the case where the `$group_path_over_root_path` property is true, the order of priority will change to this:
190+
*
191+
* 1. If a path group is set, that will be used.
192+
* 2. If a specific root path is set, that will be used.
193+
* 3. Otherwise, the root path will be used.
194+
*
189195
* @var string
190196
*/
191197
protected string $group_path_name = '';
@@ -276,6 +282,17 @@ class Asset {
276282
*/
277283
protected ?string $version = null;
278284

285+
/**
286+
* Whether to use the group path over the root path.
287+
* This flag will be raised when the asset is added to a group path
288+
* and lowered when it's removed from it.
289+
*
290+
* @since TBD
291+
*
292+
* @var bool
293+
*/
294+
private $group_path_over_root_path = false;
295+
279296
/**
280297
* Constructor.
281298
*
@@ -313,6 +330,8 @@ public function add_to_group_path( string $group_path_name ) {
313330

314331
$this->prefix_asset_directory( Config::is_group_path_using_asset_directory_prefix( $this->group_path_name ) );
315332

333+
$this->group_path_over_root_path = true;
334+
316335
return $this;
317336
}
318337

@@ -480,6 +499,20 @@ protected function build_resource_path_data(): array {
480499
return (array) apply_filters( "stellarwp/assets/{$hook_prefix}/resource_path_data", $data, $this->get_slug(), $this );
481500
}
482501

502+
/**
503+
* Removes the asset from a group.
504+
*
505+
* This method is the inverse of the `add_to_group_path` method.
506+
*
507+
* @param string $group_path_name The name of the group path to remove the asset from.
508+
*
509+
* @return void The asset is removed from the specified group path.
510+
*/
511+
public function remove_from_group_path( string $group_path_name ): void {
512+
$this->group_path_over_root_path = false;
513+
$this->group_path_name = '';
514+
}
515+
483516
/**
484517
* Builds the base asset URL.
485518
*
@@ -589,13 +622,13 @@ protected function build_min_asset_url( $original_url ): string {
589622

590623
$script_debug = defined( 'SCRIPT_DEBUG' ) && Utils::is_truthy( SCRIPT_DEBUG );
591624

592-
if ( $script_debug && file_exists( wp_normalize_path( $root_path . $resource_path . $resource ) ) ) {
625+
if ( $script_debug && is_file( wp_normalize_path( $root_path . $resource_path . $resource ) ) ) {
593626
return $original_url;
594627
}
595628

596629
$minified_abs_file_path = wp_normalize_path( $root_path . $minified_file_path );
597630

598-
if ( ! file_exists( $minified_abs_file_path ) ) {
631+
if ( ! is_file( $minified_abs_file_path ) ) {
599632
return $original_url;
600633
}
601634

@@ -958,13 +991,19 @@ public function get_root_path(): ?string {
958991
return $this->root_path;
959992
}
960993

994+
if ( $this->group_path_over_root_path ) {
995+
$group_path = Config::get_path_of_group_path( $this->group_path_name );
996+
997+
return $group_path ?: $this->root_path;
998+
}
999+
9611000
if ( $this->root_path !== Config::get_path() ) {
9621001
return $this->root_path;
9631002
}
9641003

9651004
$group_path = Config::get_path_of_group_path( $this->group_path_name );
9661005

967-
return $group_path ? $group_path : $this->root_path;
1006+
return $group_path ?: $this->root_path;
9681007
}
9691008

9701009
/**
@@ -1101,7 +1140,7 @@ public function has_asset_file(): bool {
11011140
return false;
11021141
}
11031142

1104-
return file_exists( $asset_file_path );
1143+
return is_file( $asset_file_path );
11051144
}
11061145

11071146
/**
@@ -1342,7 +1381,7 @@ public function maybe_get_min_file( $url ) {
13421381
$file_path = wp_normalize_path( "{$base_dir}/{$partial_path}" );
13431382
$file_url = "{$base_url}/{$partial_path}";
13441383

1345-
if ( file_exists( $file_path ) ) {
1384+
if ( is_file( $file_path ) ) {
13461385
return $file_url;
13471386
}
13481387
}

tests/wpunit/AssetTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace wpunit;
4+
5+
use StellarWP\Assets\Asset;
6+
use StellarWP\Assets\Config;
7+
use StellarWP\Assets\Tests\AssetTestCase;
8+
9+
class AssetTest extends AssetTestCase {
10+
public function test_add_to_group_path_changes_resolution_path_to_group(): void {
11+
Config::reset();
12+
Config::set_hook_prefix( 'bork' );
13+
Config::set_version( '1.1.0' );
14+
Config::set_path( constant( 'WP_PLUGIN_DIR' ) . '/assets' );
15+
Config::set_relative_asset_path( 'tests/_data/' );
16+
Config::add_group_path( 'fake-group', constant( 'WP_PLUGIN_DIR' ) . '/some-plugin/build', '/' );
17+
18+
$asset = new Asset( 'test-script', 'fake.js', '1.0.0', codecept_data_dir() );
19+
20+
$this->assertEquals( WP_PLUGIN_DIR . '/assets/tests/_data/', $asset->get_root_path() );
21+
22+
// Now add the asset to a group path.
23+
$asset->add_to_group_path( 'fake-group' );
24+
25+
// The asset root path will change to the group path.
26+
$this->assertEquals( WP_PLUGIN_DIR . '/some-plugin/build/', $asset->get_root_path() );
27+
28+
$asset->remove_from_group_path( 'fake-group' );
29+
30+
$this->assertEquals( WP_PLUGIN_DIR . '/assets/tests/_data/', $asset->get_root_path() );
31+
}
32+
}

tests/wpunit/AssetsTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min
971971
$plugins_path = str_replace( constant( 'WP_CONTENT_DIR' ), '', constant( 'WP_PLUGIN_DIR' ) );
972972

973973
if ( constant( 'WP_PLUGIN_DIR' ) !== constant( 'WP_CONTENT_DIR' ) . $plugins_path || strpos( constant( 'ABSPATH' ), 'C:') === 0 || $wont_figure_out_min_vs_unmin ) {
974-
// If we are testing outside of the actual plugin directory, the file_exists will always fail.
974+
// If we are testing outside of the actual plugin directory, the `is_file` check will always fail.
975975
// In installations where this set up is the actual, the file should exist.
976976
// In this case it will always fail to locate mins.
977977
$urls = array_map(

0 commit comments

Comments
 (0)