Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/tectria 83 complex setups resources paths #13

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 1 addition & 48 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@
use InvalidArgumentException;

class Asset {
/**
* Stores all the Bases for the request.
*
* @since 1.2.3
*
* @var array
*/
protected static array $bases = [];

/**
* @var array The asset action.
*/
Expand Down Expand Up @@ -847,44 +838,6 @@ public function is_vendor(): bool {
return $this->is_vendor;
}

/**
* Gets the asset bases for the request, both directory and URL.
*
* @since 1.2.3
*
* @return array
*/
protected function get_bases(): array {
$key = md5( serialize( [ WP_CONTENT_DIR, WP_CONTENT_URL ] ) );

if ( empty( static::$bases[ $key ] ) ) {
static::$bases[ $key ] = [
'wpmu_plugin' => [
'base_dir' => wp_normalize_path( WPMU_PLUGIN_DIR ),
'base_url' => set_url_scheme( WPMU_PLUGIN_URL ),
],
'wp_plugin' => [
'base_dir' => wp_normalize_path( WP_PLUGIN_DIR ),
'base_url' => set_url_scheme( WP_PLUGIN_URL ),
],
'wp_content' => [
'base_dir' => wp_normalize_path( WP_CONTENT_DIR ),
'base_url' => set_url_scheme( WP_CONTENT_URL ),
],
'plugins' => [
'base_dir' => wp_normalize_path( WP_PLUGIN_DIR ),
'base_url' => plugins_url(),
],
'stylesheet' => [
'base_dir' => wp_normalize_path( get_stylesheet_directory() ),
'base_url' => get_stylesheet_directory_uri(),
],
];
}

return static::$bases[ $key ];
}

/**
* Returns the path to a minified version of a js or css file, if it exists.
* If the file does not exist, returns false.
Expand All @@ -896,7 +849,7 @@ protected function get_bases(): array {
* @return string|false The url to the minified version or false, if file not found.
*/
public function maybe_get_min_file( $url ) {
$bases = $this->get_bases();
$bases = Utils::get_bases();

$urls = [];

Expand Down
8 changes: 4 additions & 4 deletions src/Assets/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ public static function get_relative_asset_path(): string {
* @return string
*/
public static function get_url( $path ): string {
$key = md5( serialize( [ WP_CONTENT_DIR, WP_CONTENT_URL ] ) );
$key = Utils::get_runtime_cache_key( [ $path ] );

if ( empty( static::$path_urls[ $key ] ) ) {
static::$path_urls[ $key ] = trailingslashit( str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $path ) );
$bases = Utils::get_bases();
static::$path_urls[ $key ] = trailingslashit( str_replace( wp_list_pluck( $bases, 'base_dir' ), wp_list_pluck( $bases, 'base_url' ), $path ) );
}

codecept_debug( static::$path_urls[ $key ] );

return static::$path_urls[ $key ];
}

Expand Down
77 changes: 77 additions & 0 deletions src/Assets/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
namespace StellarWP\Assets;

class Utils {
/**
* Stores all the Bases for the request.
*
* @since 1.2.3
*
* @var array
*/
protected static array $bases = [];

/**
* Determines if the provided value should be regarded as 'true'.
*
Expand Down Expand Up @@ -53,4 +62,72 @@ public static function is_truthy( $var ) : bool {
// For other types (ints, floats etc) cast to bool
return (bool) $var;
}

/**
* Gets the asset bases for the request, both directory and URL.
*
* @since 1.2.3
*
* @return array
*/
public static function get_bases(): array {
$key = self::get_runtime_cache_key();

if ( empty( static::$bases[ $key ] ) ) {
static::$bases[ $key ] = [
'wpmu_plugin' => [
'base_dir' => wp_normalize_path( WPMU_PLUGIN_DIR ),
'base_url' => set_url_scheme( WPMU_PLUGIN_URL ),
],
'wp_plugin' => [
'base_dir' => wp_normalize_path( WP_PLUGIN_DIR ),
'base_url' => set_url_scheme( WP_PLUGIN_URL ),
],
'wp_content' => [
'base_dir' => wp_normalize_path( WP_CONTENT_DIR ),
'base_url' => set_url_scheme( WP_CONTENT_URL ),
],
'plugins' => [
'base_dir' => wp_normalize_path( WP_PLUGIN_DIR ),
'base_url' => plugins_url(),
],
'stylesheet' => [
'base_dir' => wp_normalize_path( get_stylesheet_directory() ),
'base_url' => get_stylesheet_directory_uri(),
],
];
}

return static::$bases[ $key ];
}

/**
* Get the runtime cache key.
*
* @since 1.2.3
*
* @param array $extra Extra data to include in the cache key.
*
* @return string
*/
public static function get_runtime_cache_key( array $extra = [] ): string {
return md5(
serialize(
array_merge(
[
WPMU_PLUGIN_DIR,
WPMU_PLUGIN_URL,
WP_PLUGIN_DIR,
WP_PLUGIN_URL,
WP_CONTENT_DIR,
WP_CONTENT_URL,
plugins_url(),
get_stylesheet_directory(),
get_stylesheet_directory_uri(),
],
$extra
)
)
);
}
}
116 changes: 86 additions & 30 deletions tests/wpunit/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,41 +79,81 @@ public function it_should_locate_minified_versions_of_external_assets() {

/**
* @test
*
* @dataProvider constantProvider
*/
public function it_should_get_the_correct_url_when_wp_content_dir_and_wp_content_url_are_diff() {
Assets::init()->remove( 'fake1-script' );
Assets::init()->remove( 'fake2-script' );
Assets::init()->remove( 'fake3-script' );

$this->set_const_value( 'WP_CONTENT_DIR', '/var/www/html/wp-content' );
$this->set_const_value( 'WP_CONTENT_URL', 'http://wordpress.test/foo' );
$this->set_const_value( 'WP_PLUGIN_DIR', '/var/www/html/wp-content/plugins' );
$this->set_const_value( 'WP_PLUGIN_URL', 'http://wordpress.test/foo/plugins' );
$this->set_const_value( 'SCRIPT_DEBUG', false );
public function it_should_get_the_correct_url_when_wp_content_dir_and_wp_content_url_are_diff( $id, $constants ) {
$slugs = [
'fake1' => [ true, false ],
'fake2' => [ false, false ],
'fake3' => [ true, true ]
];

foreach ( array_keys( $slugs ) as $slug ) {
Assets::init()->remove( $slug . '-script' );
Assets::init()->remove( $slug . '-style' );
}

foreach ( $constants as $constant => $value ) {
$this->set_const_value( $constant, $value );
$this->assertEquals( $value, constant( $constant ) );
}

Config::reset();

Config::set_hook_prefix( 'bork' );
Config::set_version( '1.1.0' );
Config::set_path( dirname( dirname( __DIR__ ) ) );
Config::set_path( constant( 'WP_PLUGIN_DIR' ) . '/assets' );
Config::set_relative_asset_path( 'tests/_data/' );

Asset::add( 'fake1-script', 'fake1.js' )->register();
Asset::add( 'fake2-script', 'fake2.js' )->register();
Asset::add( 'fake3-script', 'fake3.js' )->register();
foreach ( array_keys( $slugs ) as $slug ) {
Asset::add( $slug . '-script', $slug . '.js' );
Asset::add( $slug . '-style', $slug . '.css' );
}

$this->assertEquals(
'http://wordpress.test/foo/plugins/assets/tests/_data/js/fake1.min.js',
Assets::init()->get( 'fake1-script' )->get_url()
);
$this->assertEquals(
'http://wordpress.test/foo/plugins/assets/tests/_data/js/fake2.js',
Assets::init()->get( 'fake2-script' )->get_url()
);
$this->assertEquals(
'http://wordpress.test/foo/plugins/assets/tests/_data/js/fake3.min.js',
Assets::init()->get( 'fake3-script' )->get_url()
);
foreach ( $slugs as $slug => $data ) {
$this->assert_minified_found( $slug, true, $data['0'], $data['1'], $id );
$this->assert_minified_found( $slug, false, $data['0'], $data['1'], $id );
}
}

public function constantProvider() {
$data = [
[
// Normal.
'**normal**',
[
'WP_CONTENT_DIR' => '/var/www/html/wp-content',
'WP_CONTENT_URL' => 'http://wordpress.test/wp-content',
'WP_PLUGIN_DIR' => '/var/www/html/wp-content/plugins',
'WP_PLUGIN_URL' => 'http://wordpress.test/wp-content/plugins',
],
],
[
// Small complexity.
'**small-complex**',
[
'WP_CONTENT_DIR' => '/var/www/html/wp-content',
'WP_CONTENT_URL' => 'http://wordpress.test/foo',
'WP_PLUGIN_DIR' => '/var/www/html/wp-content/plugins',
'WP_PLUGIN_URL' => 'http://wordpress.test/foo/plugins',
],
],
[
// Complex.
'**complex**',
[
'WP_CONTENT_DIR' => '/var/www/html/content',
'WP_CONTENT_URL' => 'http://wordpress.test/content',
'WP_PLUGIN_DIR' => '/var/www/html/plugins',
'WP_PLUGIN_URL' => 'http://wordpress.test/plugins',
],
],
];

foreach ( $data as $d ) {
yield $d;
}
}

/**
Expand Down Expand Up @@ -435,10 +475,10 @@ protected function existence_assertions( $test_slug_prefix ) {
* @param bool $has_min
* @param bool $has_only_min
*/
protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min = true, $has_only_min = false ) {
protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min = true, $has_only_min = false, $id = '' ) {
$asset = Assets::init()->get( $slug_prefix . '-' . ( $is_js ? 'script' : 'style' ) );

$url = get_site_url() . '/wp-content/plugins/assets/tests/_data/' . ( $is_js ? 'js' : 'css' ) . '/' . $slug_prefix;
$url = plugins_url( '/assets/tests/_data/' . ( $is_js ? 'js' : 'css' ) . '/' . $slug_prefix );

$urls = [];

Expand All @@ -457,9 +497,24 @@ protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min
$urls[] = $url . ( $is_js ? '.js' : '.css' );
}

$plugins_path = str_replace( constant( 'WP_CONTENT_DIR' ), '', constant( 'WP_PLUGIN_DIR' ) );

if ( constant( 'WP_PLUGIN_DIR' ) !== constant( 'WP_CONTENT_DIR' ) . $plugins_path ) {
// If we are testing outside of the actual plugin directory, the file_exists 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(
static function ( $url ) {
return str_replace( '.min', '', $url );
},
$urls
);
}

$this->assertEquals(
$urls['0'],
$asset->get_url()
$asset->get_url(),
$id
);

$this->set_const_value( 'SCRIPT_DEBUG', true );
Expand All @@ -474,7 +529,8 @@ protected function assert_minified_found( $slug_prefix, $is_js = true, $has_min

$this->assertEquals(
$urls['1'],
$asset->get_url()
$asset->get_url(),
$id
);
}

Expand Down
Loading