diff --git a/assets.php b/assets.php index 445eab5..e8005e7 100644 --- a/assets.php +++ b/assets.php @@ -2,7 +2,7 @@ /** * Plugin Name: Assets * Description: Asset library with a plugin bootstrap file for automated testing. - * Version: 1.4.11 + * Version: 1.5.1 * Author: StellarWP * Author URI: https://stellarwp.com */ diff --git a/src/Assets/Config.php b/src/Assets/Config.php index d2a4361..fcc7caa 100644 --- a/src/Assets/Config.php +++ b/src/Assets/Config.php @@ -148,6 +148,8 @@ public static function get_relative_asset_path(): string { /** * Gets the root path of the project. * + * @since 1.5.1 + * * @return string */ public static function get_url( $path ): string { @@ -156,7 +158,25 @@ public static function get_url( $path ): string { if ( empty( static::$path_urls[ $key ] ) ) { $bases = Utils::get_bases(); - static::$path_urls[ $key ] = trailingslashit( str_replace( wp_list_pluck( $bases, 'base_dir' ), wp_list_pluck( $bases, 'base_url' ), $path ) ); + + $base_dirs = array_column( $bases, 'base_dir' ); + $base_urls = array_column( $bases, 'base_url' ); + + uasort( $base_dirs, function( $a, $b ) { + return strlen( $b ) <=> strlen( $a ); + } ); + + foreach ( $base_dirs as $dir_type => $dir_path ) { + if ( ! isset( $base_urls[ $dir_type ] ) ) { + continue; + } + + static::$path_urls[ $key ] = str_replace( $dir_path, $base_urls[ $dir_type ], $path ); + + if ( static::$path_urls[ $key ] !== $path ) { + break; + } + } } return static::$path_urls[ $key ]; diff --git a/tests/wpunit/AssetsTest.php b/tests/wpunit/AssetsTest.php index 83892e3..cc6b748 100644 --- a/tests/wpunit/AssetsTest.php +++ b/tests/wpunit/AssetsTest.php @@ -464,6 +464,16 @@ public function constantProvider() { 'WP_PLUGIN_URL' => 'http://wordpress.test/wp-content/addons', ], ], + [ + 'rare case', + [ + 'ABSPATH' => '/wordpress.test/', + 'WP_CONTENT_DIR' => '/wordpress.test/wp-content', + 'WP_CONTENT_URL' => 'http://wordpress.test/wp-content', + 'WP_PLUGIN_DIR' => '/wordpress.test/wp-content/plugins', + 'WP_PLUGIN_URL' => 'http://wordpress.test/wp-content/plugins', + ], + ], ]; foreach ( $data as $d ) { @@ -1129,7 +1139,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 ( constant( 'WP_PLUGIN_DIR' ) !== constant( 'WP_CONTENT_DIR' ) . $plugins_path || strpos( constant( 'ABSPATH' ), 'C:') === 0 || strpos( constant( 'ABSPATH' ), '/wordpress.test/') === 0 || $wont_figure_out_min_vs_unmin ) { // 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.