Skip to content

Commit

Permalink
Prefix Config::set_path() when path is relative
Browse files Browse the repository at this point in the history
This ensures that if an absolute path is set during `Config::set_path()`, the library respects that full path rather than attempting to prefix it.

This builds on top of the work by @dpanta94 in #25
  • Loading branch information
borkweb committed Nov 8, 2024
1 parent 579f8aa commit 56b6539
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/Assets/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,10 @@ protected static function normalize_path( string $path ): string {
if (
$plugins_content_dir_position === false
&& $themes_content_dir_position === false
&& strpos( $path, '/' ) !== 0
) {
// Default to path.
$path = $path;
// Default to plugins if a relative path is provided.
$path = trailingslashit( $plugin_dir ) . $path;
} elseif ( $plugins_content_dir_position !== false ) {
$path = substr( $path, $plugins_content_dir_position );
} elseif ( $themes_content_dir_position !== false ) {
Expand Down
12 changes: 6 additions & 6 deletions tests/wpunit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public function should_set_hook_prefix() {
}

public function paths_provider() {
yield 'plugin' => [ WP_PLUGIN_DIR . '/assets/', '/var/www/html/wp-content/plugins/assets/' ];
yield 'theme' => [ get_theme_file_path() . '/assets/', get_theme_file_path() . '/assets/' ];
yield 'mu-plugin' => [ WPMU_PLUGIN_DIR . '/assets/', '/var/www/html/wp-content/mu-plugins/assets/' ];
yield 'content' => [ WP_CONTENT_DIR . '/assets/', '/var/www/html/wp-content/assets/' ];
yield 'root' => [ ABSPATH . 'assets/', '/var/www/html/assets/' ];
yield 'relative' => [ 'src/resources/', 'src/resources/' ];
yield 'plugin' => [ WP_PLUGIN_DIR . '/my-plugin/', '/var/www/html/wp-content/plugins/my-plugin/' ];
yield 'theme' => [ get_theme_file_path() . '/', get_theme_file_path() . '/' ];
yield 'mu-plugin' => [ WPMU_PLUGIN_DIR . '/my-plugin/', '/var/www/html/wp-content/mu-plugins/my-plugin/' ];
yield 'content' => [ WP_CONTENT_DIR . '/stuff/', '/var/www/html/wp-content/stuff/' ];
yield 'root' => [ ABSPATH . 'stuff/', '/var/www/html/stuff/' ];
yield 'relative' => [ 'my-plugin/', '/var/www/html/wp-content/plugins/my-plugin/' ];
}

/**
Expand Down

0 comments on commit 56b6539

Please sign in to comment.