Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/wp-includes/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
*
Expand All @@ -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;
}
Expand Down
63 changes: 63 additions & 0 deletions tests/phpunit/tests/functions/wpRegisterPluginRealpath.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* Tests for wp_register_plugin_realpath()
*
* @group functions
* @group plugins
*
* @covers ::wp_register_plugin_realpath
*/
class Tests_Functions_WpRegisterPluginRealpath extends WP_UnitTestCase {

/**
* @var array
*/
protected $wp_plugin_paths_backup;

public function set_up() {
parent::set_up();

$this->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' ) );
}
}
Loading