diff --git a/src/wp-includes/plugin.php b/src/wp-includes/plugin.php index 9bef5f03e88e7..bd5b92c7934bc 100644 --- a/src/wp-includes/plugin.php +++ b/src/wp-includes/plugin.php @@ -820,6 +820,7 @@ function plugin_basename( $file ) { * This is used in plugin_basename() to resolve symlinked paths. * * @since 3.9.0 + * @since 7.3.0 Returns false if the real path of the file cannot be determined. * * @see wp_normalize_path() * @@ -839,13 +840,21 @@ function wp_register_plugin_realpath( $file ) { $wpmu_plugin_path = wp_normalize_path( WPMU_PLUGIN_DIR ); } - $plugin_path = wp_normalize_path( dirname( $file ) ); - $plugin_realpath = wp_normalize_path( dirname( realpath( $file ) ) ); + $plugin_path = wp_normalize_path( dirname( $file ) ); if ( $plugin_path === $wp_plugin_path || $plugin_path === $wpmu_plugin_path ) { return false; } + $file_realpath = realpath( $file ); + + // An empty registered path would make plugin_basename() mangle every plugin path. + if ( false === $file_realpath ) { + return false; + } + + $plugin_realpath = wp_normalize_path( dirname( $file_realpath ) ); + if ( $plugin_path !== $plugin_realpath ) { $wp_plugin_paths[ $plugin_path ] = $plugin_realpath; } diff --git a/tests/phpunit/tests/functions/wpRegisterPluginRealpath.php b/tests/phpunit/tests/functions/wpRegisterPluginRealpath.php new file mode 100644 index 0000000000000..88e59a9fef6e9 --- /dev/null +++ b/tests/phpunit/tests/functions/wpRegisterPluginRealpath.php @@ -0,0 +1,63 @@ +wp_plugin_paths_backup = $GLOBALS['wp_plugin_paths']; + } + + public function tear_down() { + $GLOBALS['wp_plugin_paths'] = $this->wp_plugin_paths_backup; + + parent::tear_down(); + } + + /** + * @ticket 43489 + */ + public function test_should_return_false_for_nonexistent_file() { + $this->assertFalse( wp_register_plugin_realpath( WP_PLUGIN_DIR . '/missing-plugin-43489/missing-plugin-43489.php' ) ); + } + + /** + * @ticket 43489 + */ + public function test_should_not_register_path_for_nonexistent_file() { + global $wp_plugin_paths; + + wp_register_plugin_realpath( WP_PLUGIN_DIR . '/missing-plugin-43489/missing-plugin-43489.php' ); + + $this->assertArrayNotHasKey( wp_normalize_path( WP_PLUGIN_DIR ) . '/missing-plugin-43489', $wp_plugin_paths ); + } + + /** + * @ticket 43489 + */ + public function test_should_not_affect_plugin_basename_for_nonexistent_file() { + wp_register_plugin_realpath( WP_PLUGIN_DIR . '/missing-plugin-43489/missing-plugin-43489.php' ); + + $this->assertSame( 'hello.php', plugin_basename( WP_PLUGIN_DIR . '/hello.php' ) ); + } + + /** + * @ticket 43489 + */ + public function test_should_return_true_for_existing_file_in_plugin_subdirectory() { + $this->assertTrue( wp_register_plugin_realpath( DIR_TESTDATA . '/plugins/hello.php' ) ); + } +}