Skip to content

Commit

Permalink
#1 Add edge case for customized mu-plugins and plugins directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
fumikito committed Feb 28, 2020
1 parent 69e63c6 commit 880cce9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
15 changes: 11 additions & 4 deletions src/Hametuha/StringUtility/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,27 @@ trait Path {
/**
* Convert local file path to URL.
*
* This function is similar to `get_styelsheet_directory_uri()`
* or `plugin_dir_url()`, but works wherever library is.
* This function is similar to `get_stylesheet_directory_uri()`
* or `plugin_dir_url()`, but works wherever the library is.
*
* @param string $path File path in document root.
* @return string URL.
*/
public function path_to_url( $path ) {
if ( false !== strpos( $path, WP_CONTENT_DIR ) ) {
if ( false !== strpos( $path, WP_PLUGIN_DIR ) ) {
// This is in plugin dir.
return str_replace( WP_PLUGIN_DIR, WP_PLUGIN_URL, $path );
} elseif ( false !== strpos( $path, WPMU_PLUGIN_DIR ) ) {
// This is in mu-plugins dir.
return str_replace( WPMU_PLUGIN_DIR, WPMU_PLUGIN_URL, $path );
} elseif ( false !== strpos( $path, WP_CONTENT_DIR ) ) {
// This is in wp-content dir.
return str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $path );
} elseif ( false !== strpos( $path, ABSPATH ) ) {
// Convert from ABSPATH.
return str_replace( ABSPATH, home_url( '/' ), $path );
} else {
return $path;
}
}

}
18 changes: 15 additions & 3 deletions tests/test-path.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,26 @@ class PathTest extends \PHPUnit\Framework\TestCase {

protected function setUp() {
define( 'ABSPATH', '/var/www/wordpress/' );
define( 'WP_CONTENT_DIR', '/var/www/wordpress/contents' );
define( 'WP_CONTENT_URL', 'https://example.com/contents' );
define( 'WP_PLUGIN_DIR', '/var/www/wordpress/plugins' );
define( 'WP_PLUGIN_URL', 'https://example.com/plugins' );
define( 'WPMU_PLUGIN_DIR', '/var/www/wordpress/mu' );
define( 'WPMU_PLUGIN_URL', 'https://example.com/mu' );
$this->path = new HametuhaPathImplementor();
}


/**
* Test path convertor
* Test path converter.
*/
public function testPathConverter() {
$this->assertEquals( $this->path->path_to_url( '/var/www/wordpress/hoge' ), 'https://example.com/hoge' );
// ABSPATH
$this->assertEquals( $this->path->path_to_url( '/var/www/wordpress/hoge' ), 'https://example.com/hoge', 'Root directory.' );
// In plugins dir.
$this->assertEquals( $this->path->path_to_url( '/var/www/wordpress/plugins/my-plugin' ), 'https://example.com/plugins/my-plugin', 'Plugin changed.' );
// In mu-plugins dir.
$this->assertEquals( $this->path->path_to_url( '/var/www/wordpress/mu/mu-plugin.jpg' ), 'https://example.com/mu/mu-plugin.jpg', 'MU plugins.' );
// In themes dir.
$this->assertEquals( $this->path->path_to_url( '/var/www/wordpress/contents/themes/style.css' ), 'https://example.com/contents/themes/style.css', 'For theme directory.' );
}
}

0 comments on commit 880cce9

Please sign in to comment.