Skip to content

Commit

Permalink
Merge pull request #28 from stellarwp/feat/add-to-group-path
Browse files Browse the repository at this point in the history
Handle later calls to the add_to_group_path method
  • Loading branch information
lucatume authored Dec 17, 2024
2 parents 6c26d08 + 7bef433 commit b0432d1
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
49 changes: 44 additions & 5 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ class Asset {
* 2. If a path group is set, that will be used.
* 3. Otherwise, the root path will be used.
*
* In the case where the `$group_path_over_root_path` property is true, the order of priority will change to this:
*
* 1. If a path group is set, that will be used.
* 2. If a specific root path is set, that will be used.
* 3. Otherwise, the root path will be used.
*
* @var string
*/
protected string $group_path_name = '';
Expand Down Expand Up @@ -276,6 +282,17 @@ class Asset {
*/
protected ?string $version = null;

/**
* Whether to use the group path over the root path.
* This flag will be raised when the asset is added to a group path
* and lowered when it's removed from it.
*
* @since TBD
*
* @var bool
*/
private $group_path_over_root_path = false;

/**
* Constructor.
*
Expand Down Expand Up @@ -313,6 +330,8 @@ public function add_to_group_path( string $group_path_name ) {

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

$this->group_path_over_root_path = true;

return $this;
}

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

/**
* Removes the asset from a group.
*
* This method is the inverse of the `add_to_group_path` method.
*
* @param string $group_path_name The name of the group path to remove the asset from.
*
* @return void The asset is removed from the specified group path.
*/
public function remove_from_group_path( string $group_path_name ): void {
$this->group_path_over_root_path = false;
$this->group_path_name = '';
}

/**
* Builds the base asset URL.
*
Expand Down Expand Up @@ -589,13 +622,13 @@ protected function build_min_asset_url( $original_url ): string {

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

if ( $script_debug && file_exists( wp_normalize_path( $root_path . $resource_path . $resource ) ) ) {
if ( $script_debug && is_file( wp_normalize_path( $root_path . $resource_path . $resource ) ) ) {
return $original_url;
}

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

if ( ! file_exists( $minified_abs_file_path ) ) {
if ( ! is_file( $minified_abs_file_path ) ) {
return $original_url;
}

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

if ( $this->group_path_over_root_path ) {
$group_path = Config::get_path_of_group_path( $this->group_path_name );

return $group_path ?: $this->root_path;
}

if ( $this->root_path !== Config::get_path() ) {
return $this->root_path;
}

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

return $group_path ? $group_path : $this->root_path;
return $group_path ?: $this->root_path;
}

/**
Expand Down Expand Up @@ -1101,7 +1140,7 @@ public function has_asset_file(): bool {
return false;
}

return file_exists( $asset_file_path );
return is_file( $asset_file_path );
}

/**
Expand Down Expand Up @@ -1342,7 +1381,7 @@ public function maybe_get_min_file( $url ) {
$file_path = wp_normalize_path( "{$base_dir}/{$partial_path}" );
$file_url = "{$base_url}/{$partial_path}";

if ( file_exists( $file_path ) ) {
if ( is_file( $file_path ) ) {
return $file_url;
}
}
Expand Down
32 changes: 32 additions & 0 deletions tests/wpunit/AssetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace wpunit;

use StellarWP\Assets\Asset;
use StellarWP\Assets\Config;
use StellarWP\Assets\Tests\AssetTestCase;

class AssetTest extends AssetTestCase {
public function test_add_to_group_path_changes_resolution_path_to_group(): void {
Config::reset();
Config::set_hook_prefix( 'bork' );
Config::set_version( '1.1.0' );
Config::set_path( constant( 'WP_PLUGIN_DIR' ) . '/assets' );
Config::set_relative_asset_path( 'tests/_data/' );
Config::add_group_path( 'fake-group', constant( 'WP_PLUGIN_DIR' ) . '/some-plugin/build', '/' );

$asset = new Asset( 'test-script', 'fake.js', '1.0.0', codecept_data_dir() );

$this->assertEquals( WP_PLUGIN_DIR . '/assets/tests/_data/', $asset->get_root_path() );

// Now add the asset to a group path.
$asset->add_to_group_path( 'fake-group' );

// The asset root path will change to the group path.
$this->assertEquals( WP_PLUGIN_DIR . '/some-plugin/build/', $asset->get_root_path() );

$asset->remove_from_group_path( 'fake-group' );

$this->assertEquals( WP_PLUGIN_DIR . '/assets/tests/_data/', $asset->get_root_path() );
}
}
2 changes: 1 addition & 1 deletion tests/wpunit/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min
$plugins_path = str_replace( constant( 'WP_CONTENT_DIR' ), '', constant( 'WP_PLUGIN_DIR' ) );

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

0 comments on commit b0432d1

Please sign in to comment.