Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 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
30 changes: 28 additions & 2 deletions src/wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2184,7 +2184,23 @@ function path_join( $base, $path ) {
* @return string Normalized path.
*/
function wp_normalize_path( $path ) {
Comment thread
josephscott marked this conversation as resolved.
Outdated
$wrapper = '';
$path = (string) $path;

static $hot = array();
Comment thread
josephscott marked this conversation as resolved.
Outdated
static $warm = array();
static $max = 100;

if ( isset( $hot[ $path ] ) ) {
return $hot[ $path ];
}

if ( isset( $warm[ $path ] ) ) {
$hot[ $path ] = $warm[ $path ];
Comment thread
josephscott marked this conversation as resolved.
Outdated
return $hot[ $path ];
}

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

if ( wp_is_stream( $path ) ) {
list( $wrapper, $path ) = explode( '://', $path, 2 );
Expand All @@ -2203,7 +2219,17 @@ function wp_normalize_path( $path ) {
$path = ucfirst( $path );
}

return $wrapper . $path;
$value = $wrapper . $path;

$hot[ $original_path ] = $value;

// Rotate segments when hot is full.
if ( count( $hot ) > $max ) {
$warm = $hot;
$hot = array();
}

return $value;
}

/**
Expand Down
41 changes: 41 additions & 0 deletions tests/phpunit/tests/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,50 @@ 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 33265
Comment thread
josephscott marked this conversation as resolved.
Outdated
*/
public function test_wp_normalize_path_with_stringable_object() {
$stringable = new class() {
public function __toString() {
return '/var/www/html\\test';
}
};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a more representative test might be an SplFileInfo instance, since that is actually used in Core. as discovered in #10781 we can fix that currently-improper call in another patch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no particular preference there. I will update the test_wp_normalize_path_with_stringable_object() to try that out.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.


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

/**
* Tests that wp_normalize_path() returns consistent results on repeated calls.
*
* The function uses a static cache, so this verifies cache behavior.
*
* @ticket 33265
Comment thread
josephscott marked this conversation as resolved.
Outdated
*/
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.

public function test_wp_unique_filename() {

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