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

Handle Diff URL and Path for wp-content #12

Merged
merged 11 commits into from
Jun 13, 2024
2 changes: 1 addition & 1 deletion assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: Assets
* Description: Asset library with a plugin bootstrap file for automated testing.
* Version: 1.0.0
* Version: 1.2.3
* Author: StellarWP
* Author URI: https://stellarwp.com
*/
106 changes: 63 additions & 43 deletions src/Assets/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
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 @@ -838,6 +847,44 @@ 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 ] ) );
bordoni marked this conversation as resolved.
Show resolved Hide resolved

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 @@ -849,61 +896,34 @@ public function is_vendor(): bool {
* @return string|false The url to the minified version or false, if file not found.
*/
public function maybe_get_min_file( $url ) {
static $wpmu_plugin_url;
static $wp_plugin_url;
static $wp_content_url;
static $plugins_url;
static $base_dirs;
static $stylesheet_url;
static $stylesheet_dir;
$bases = $this->get_bases();

$urls = [];
if ( ! isset( $wpmu_plugin_url ) ) {
$wpmu_plugin_url = set_url_scheme( WPMU_PLUGIN_URL );
}

if ( ! isset( $wp_plugin_url ) ) {
$wp_plugin_url = set_url_scheme( WP_PLUGIN_URL );
}

if ( ! isset( $wp_content_url ) ) {
$wp_content_url = set_url_scheme( WP_CONTENT_URL );
}

if ( ! isset( $plugins_url ) ) {
$plugins_url = plugins_url();
}

if ( ! isset( $stylesheet_url ) ) {
$stylesheet_url = get_stylesheet_directory_uri();
$stylesheet_dir = get_stylesheet_directory();
}

if ( ! isset( $base_dirs ) ) {
$base_dirs[ WPMU_PLUGIN_DIR ] = wp_normalize_path( WPMU_PLUGIN_DIR );
$base_dirs[ WP_PLUGIN_DIR ] = wp_normalize_path( WP_PLUGIN_DIR );
$base_dirs[ WP_CONTENT_DIR ] = wp_normalize_path( WP_CONTENT_DIR );
$base_dirs[ $stylesheet_dir ] = wp_normalize_path( $stylesheet_dir );
}
$wpmu_plugin_url = $bases['wpmu_plugin']['base_url'];
$wp_plugin_url = $bases['wp_plugin']['base_url'];
$wp_content_url = $bases['wp_content']['base_url'];
$plugins_url = $bases['plugins']['base_url'];
$stylesheet_url = $bases['stylesheet']['base_url'];

if ( 0 === strpos( $url, $wpmu_plugin_url ) ) {
// URL inside WPMU plugin dir.
$base_dir = $base_dirs[ WPMU_PLUGIN_DIR ];
$base_url = $wpmu_plugin_url;
$base_dir = $bases['wpmu_plugin']['base_dir'];
$base_url = $bases['wpmu_plugin']['base_url'];
} elseif ( 0 === strpos( $url, $wp_plugin_url ) ) {
// URL inside WP plugin dir.
$base_dir = $base_dirs[ WP_PLUGIN_DIR ];
$base_url = $wp_plugin_url;
$base_dir = $bases['wp_plugin']['base_dir'];
$base_url = $bases['wp_plugin']['base_url'];
} elseif ( 0 === strpos( $url, $wp_content_url ) ) {
// URL inside WP content dir.
$base_dir = $base_dirs[ WP_CONTENT_DIR ];
$base_url = $wp_content_url;
$base_dir = $bases['wp_content']['base_dir'];
$base_url = $bases['wp_content']['base_url'];
} elseif ( 0 === strpos( $url, $plugins_url ) ) {
$base_dir = $base_dirs[ WP_PLUGIN_DIR ];
$base_url = $plugins_url;
$base_dir = $bases['plugins']['base_dir'];
$base_url = $bases['plugins']['base_url'];
} elseif ( 0 === strpos( $url, $stylesheet_url ) ) {
$base_dir = $base_dirs[ $stylesheet_dir ];
$base_url = $stylesheet_url;
$base_dir = $bases['stylesheet']['base_dir'];
$base_url = $bases['stylesheet']['base_url'];
} else {
// Resource needs to be inside wp-content or a plugins dir.
return false;
Expand Down
12 changes: 6 additions & 6 deletions src/Assets/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -674,11 +674,11 @@ protected function do_enqueue( Asset $asset, bool $force_enqueue = false ): void
/**
* Register the Assets on the correct hooks.
*
* @since 1.0.0
*
* @param array|Asset|null $assets Array of asset objects, single asset object, or null.
*
* @return void
* @since 1.0.0
*
*/
public function register_in_wp( $assets = null ) {
if ( ! (
Expand Down Expand Up @@ -775,11 +775,11 @@ public function register_in_wp( $assets = null ) {
/**
* Removes an Asset from been registered and enqueue.
*
* @since 1.0.0
*
* @param string $slug Slug of the Asset.
*
* @return bool
* @since 1.0.0
*
*/
public function remove( $slug ) {
if ( ! $this->exists( $slug ) ) {
Expand All @@ -806,13 +806,13 @@ public function remove( $slug ) {
*
* The method will force the scripts and styles to print overriding their registration and conditional.
*
* @since 1.0.0
*
* @param string|array $group Which group(s) should be enqueued.
* @param bool $echo Whether to print the group(s) tag(s) to the page or not; default to `true` to
* print the HTML `script` (JS) and `link` (CSS) tags to the page.
*
* @return string The `script` and `link` HTML tags produced for the group(s).
* @since 1.0.0
*
*/
public function print_group( $group, $echo = true ) {
$all_assets = $this->get();
Expand Down
17 changes: 9 additions & 8 deletions src/Assets/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,14 @@ public static function get_relative_asset_path(): string {
* @return string
*/
public static function get_url( $path ): string {
if ( empty( static::$path_urls[ $path ] ) ) {
static::$path_urls[ $path ] = trailingslashit( get_site_url() . $path );
$key = md5( serialize( [ WP_CONTENT_DIR, WP_CONTENT_URL ] ) );
bordoni marked this conversation as resolved.
Show resolved Hide resolved
if ( empty( static::$path_urls[ $key ] ) ) {
static::$path_urls[ $key ] = trailingslashit( str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $path ) );
}

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

bordoni marked this conversation as resolved.
Show resolved Hide resolved
return static::$path_urls[ $key ];
}

/**
Expand Down Expand Up @@ -127,17 +130,15 @@ public static function set_relative_asset_path( string $path ) {
* @return void
*/
public static function set_path( string $path ) {
$content_dir = str_replace( get_site_url(), '', WP_CONTENT_URL );

$plugins_content_dir_position = strpos( $path, $content_dir . '/plugins' );
$themes_content_dir_position = strpos( $path, $content_dir . '/themes' );
$plugins_content_dir_position = strpos( $path, WP_PLUGIN_DIR );
$themes_content_dir_position = strpos( $path, get_theme_root() );

if (
$plugins_content_dir_position === false
&& $themes_content_dir_position === false
) {
// Default to plugins.
$path = $content_dir . '/plugins/' . $path;
$path = WP_PLUGIN_DIR . $path;
} elseif ( $plugins_content_dir_position !== false ) {
$path = substr( $path, $plugins_content_dir_position );
} elseif ( $themes_content_dir_position !== false ) {
Expand Down
39 changes: 39 additions & 0 deletions tests/wpunit/AssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,45 @@ public function it_should_locate_minified_versions_of_external_assets() {
}
}

/**
* @test
*/
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 );

Config::reset();

Config::set_hook_prefix( 'bork' );
Config::set_version( '1.1.0' );
Config::set_path( dirname( dirname( __DIR__ ) ) );
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();

$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()
);
}

/**
* @test
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/wpunit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function should_set_hook_prefix() {
public function should_set_path() {
Config::set_path( dirname( dirname( __DIR__ ) ) );

$this->assertEquals( '/wp-content/plugins/assets/', Config::get_path() );
$this->assertEquals( WP_PLUGIN_DIR . '/assets/', Config::get_path() );
}

/**
Expand Down
Loading