Skip to content
Closed
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
18 changes: 14 additions & 4 deletions src/wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2179,12 +2179,21 @@ function path_join( $base, $path ) {
* @since 4.4.0 Ensures upper-case drive letters on Windows systems.
* @since 4.5.0 Allows for Windows network shares.
* @since 4.9.7 Allows for PHP file wrappers.
* @since 7.0.0 Uses a static cache to store normalized paths.
*
* @param string $path Path to normalize.
* @return string Normalized path.
*/
function wp_normalize_path( $path ) {
$wrapper = '';
function wp_normalize_path( $path ): string {
$path = (string) $path;

static $cache = array();
if ( isset( $cache[ $path ] ) ) {
return $cache[ $path ];
}

$original_path = $path;
$wrapper = '';

if ( wp_is_stream( $path ) ) {
list( $wrapper, $path ) = explode( '://', $path, 2 );
Expand All @@ -2196,14 +2205,15 @@ function wp_normalize_path( $path ) {
$path = str_replace( '\\', '/', $path );

// Replace multiple slashes down to a singular, allowing for network shares having two slashes.
$path = preg_replace( '|(?<=.)/+|', '/', $path );
$path = (string) preg_replace( '|(?<=.)/+|', '/', $path );

// Windows paths should uppercase the drive letter.
if ( ':' === substr( $path, 1, 1 ) ) {
$path = ucfirst( $path );
}

return $wrapper . $path;
$cache[ $original_path ] = $wrapper . $path;
return $cache[ $original_path ];
}

/**
Expand Down
57 changes: 57 additions & 0 deletions tests/phpunit/tests/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,66 @@ public function data_wp_normalize_path() {
array( 'php://input', 'php://input' ),
array( 'http://example.com//path.ext', 'http://example.com/path.ext' ),
array( 'file://c:\\www\\path\\', 'file://C:/www/path/' ),

// Edge cases.
array( '', '' ), // Empty string should return empty string.
array( 123, '123' ), // Integer should be cast to string.
);
}

/**
* Tests that wp_normalize_path() works with objects that have __toString().
*
* This is important because the function uses a static cache, and the input
* must be cast to string before being used as an array key.
*
* @ticket 64538
*/
public function test_wp_normalize_path_with_stringable_object() {
$file_info = new SplFileInfo( '/var/www/html\\test' );

$this->assertSame( '/var/www/html/test', wp_normalize_path( $file_info ) );
}

/**
* Tests that wp_normalize_path() returns consistent results on repeated calls.
*
* The function uses a static cache, so this verifies cache behavior.
*
* @ticket 64538
*/
public function test_wp_normalize_path_returns_consistent_results() {
$path = 'C:\\www\\path\\';

$first_call = wp_normalize_path( $path );
$second_call = wp_normalize_path( $path );
$third_call = wp_normalize_path( $path );

$this->assertSame( $first_call, $second_call, 'Second call should return same result as first.' );
$this->assertSame( $second_call, $third_call, 'Third call should return same result as second.' );
$this->assertSame( 'C:/www/path/', $first_call, 'Normalized path should match expected value.' );
}
Comment thread
josephscott marked this conversation as resolved.

/**
* Tests that wp_normalize_path() static cache stores results.
*
* @ticket 64538
*/
public function test_wp_normalize_path_static_cache() {
$path = '/var/www/cache-test\\subdir\\';
$expected = '/var/www/cache-test/subdir/';

$result = wp_normalize_path( $path );
$this->assertSame( $expected, $result );

$reflection = new ReflectionFunction( 'wp_normalize_path' );
$static_vars = $reflection->getStaticVariables();

$this->assertArrayHasKey( 'cache', $static_vars, 'Static cache array should exist.' );
$this->assertArrayHasKey( $path, $static_vars['cache'], 'Cache should contain the normalized path.' );
$this->assertSame( $expected, $static_vars['cache'][ $path ], 'Cached value should match the expected normalized path.' );
}

public function test_wp_unique_filename() {

$testdir = DIR_TESTDATA . '/images/';
Expand Down
Loading