Skip to content

Commit b6efe53

Browse files
authored
Merge pull request #25 from stellarwp/feat/allow-paths-outside-of-themes-and-plugins
Allow paths outside of theme and plugins
2 parents c35ada8 + 1c0b43a commit b6efe53

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

assets.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/**
33
* Plugin Name: Assets
44
* Description: Asset library with a plugin bootstrap file for automated testing.
5-
* Version: 1.4.0
5+
* Version: 1.4.1
66
* Author: StellarWP
77
* Author URI: https://stellarwp.com
88
*/

src/Assets/Config.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public static function reset() {
146146
static::$root_path = '';
147147
static::$path_urls = [];
148148
static::$version = '';
149+
Utils::clear_runtime_cache();
149150
}
150151

151152
/**
@@ -196,6 +197,7 @@ public static function set_version( string $version ) {
196197
* Normalizes a path.
197198
*
198199
* @since 1.4.0
200+
* @since 1.4.1 Allow for paths that are not in the plugin or theme directory.
199201
*
200202
* @param string $path The path to normalize.
201203
*
@@ -211,9 +213,10 @@ protected static function normalize_path( string $path ): string {
211213
if (
212214
$plugins_content_dir_position === false
213215
&& $themes_content_dir_position === false
216+
&& strpos( $path, '/' ) !== 0
214217
) {
215-
// Default to plugins.
216-
$path = $plugin_dir . $path;
218+
// Default to plugins if a relative path is provided.
219+
$path = trailingslashit( $plugin_dir ) . $path;
217220
} elseif ( $plugins_content_dir_position !== false ) {
218221
$path = substr( $path, $plugins_content_dir_position );
219222
} elseif ( $themes_content_dir_position !== false ) {

src/Assets/Utils.php

+11
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ public static function get_bases(): array {
101101
return static::$bases[ $key ];
102102
}
103103

104+
/**
105+
* Clears the runtime cache.
106+
*
107+
* @since 1.4.1
108+
*
109+
* @return void
110+
*/
111+
public static function clear_runtime_cache() {
112+
static::$bases = [];
113+
}
114+
104115
/**
105116
* Get the runtime cache key.
106117
*

tests/wpunit/ConfigTest.php

+27
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ public function should_set_hook_prefix() {
2323
$this->assertEquals( 'bork', Config::get_hook_prefix() );
2424
}
2525

26+
public function paths_provider() {
27+
yield 'plugin' => [ WP_PLUGIN_DIR . '/my-plugin/', '/var/www/html/wp-content/plugins/my-plugin/' ];
28+
yield 'theme' => [ get_theme_file_path() . '/', get_theme_file_path() . '/' ];
29+
yield 'mu-plugin' => [ WPMU_PLUGIN_DIR . '/my-plugin/', '/var/www/html/wp-content/mu-plugins/my-plugin/' ];
30+
yield 'content' => [ WP_CONTENT_DIR . '/stuff/', '/var/www/html/wp-content/stuff/' ];
31+
yield 'root' => [ ABSPATH . 'stuff/', '/var/www/html/stuff/' ];
32+
yield 'relative' => [ 'my-plugin/', '/var/www/html/wp-content/plugins/my-plugin/' ];
33+
}
34+
35+
/**
36+
* @test
37+
* @dataProvider paths_provider
38+
*/
39+
public function should_set_root_path_correctly( $path, $expected ) {
40+
Config::set_path( $path );
41+
$this->assertEquals( $expected, Config::get_path( $path ), Config::get_path( $path ) );
42+
}
43+
2644
/**
2745
* @test
2846
*/
@@ -32,6 +50,15 @@ public function should_set_path() {
3250
$this->assertEquals( WP_PLUGIN_DIR . '/assets/', Config::get_path() );
3351
}
3452

53+
/**
54+
* @test
55+
*/
56+
public function should_set_path_outside_of_themes_and_plugins() {
57+
Config::set_path( ABSPATH . 'foo/' );
58+
59+
$this->assertEquals( '/var/www/html/foo/', Config::get_path() );
60+
}
61+
3562
/**
3663
* @test
3764
*/

0 commit comments

Comments
 (0)