Changeset 61857
- Timestamp:
- 03/06/2026 08:04:31 PM (26 hours ago)
- Location:
- trunk
- Files:
-
- 2 edited
-
src/wp-includes/functions.php (modified) (3 diffs)
-
tests/phpunit/tests/functions.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/functions.php
r61710 r61857 2180 2180 * @since 4.5.0 Allows for Windows network shares. 2181 2181 * @since 4.9.7 Allows for PHP file wrappers. 2182 * @since 7.0.0 Uses a static cache to store normalized paths. 2182 2183 * 2183 2184 * @param string $path Path to normalize. 2184 2185 * @return string Normalized path. 2185 2186 */ 2186 function wp_normalize_path( $path ) { 2187 $wrapper = ''; 2187 function wp_normalize_path( $path ): string { 2188 $path = (string) $path; 2189 2190 static $cache = array(); 2191 if ( isset( $cache[ $path ] ) ) { 2192 return $cache[ $path ]; 2193 } 2194 2195 $original_path = $path; 2196 $wrapper = ''; 2188 2197 2189 2198 if ( wp_is_stream( $path ) ) { … … 2197 2206 2198 2207 // Replace multiple slashes down to a singular, allowing for network shares having two slashes. 2199 $path = preg_replace( '|(?<=.)/+|', '/', $path );2208 $path = (string) preg_replace( '|(?<=.)/+|', '/', $path ); 2200 2209 2201 2210 // Windows paths should uppercase the drive letter. … … 2204 2213 } 2205 2214 2206 return $wrapper . $path; 2215 $cache[ $original_path ] = $wrapper . $path; 2216 return $cache[ $original_path ]; 2207 2217 } 2208 2218 -
trunk/tests/phpunit/tests/functions.php
r61328 r61857 223 223 array( 'http://example.com//path.ext', 'http://example.com/path.ext' ), 224 224 array( 'file://c:\\www\\path\\', 'file://C:/www/path/' ), 225 ); 225 226 // Edge cases. 227 array( '', '' ), // Empty string should return empty string. 228 array( 123, '123' ), // Integer should be cast to string. 229 ); 230 } 231 232 /** 233 * Tests that wp_normalize_path() works with objects that have __toString(). 234 * 235 * This is important because the function uses a static cache, and the input 236 * must be cast to string before being used as an array key. 237 * 238 * @ticket 64538 239 */ 240 public function test_wp_normalize_path_with_stringable_object() { 241 $file_info = new SplFileInfo( '/var/www/html\\test' ); 242 243 $this->assertSame( '/var/www/html/test', wp_normalize_path( $file_info ) ); 244 } 245 246 /** 247 * Tests that wp_normalize_path() returns consistent results on repeated calls. 248 * 249 * The function uses a static cache, so this verifies cache behavior. 250 * 251 * @ticket 64538 252 */ 253 public function test_wp_normalize_path_returns_consistent_results() { 254 $path = 'C:\\www\\path\\'; 255 256 $first_call = wp_normalize_path( $path ); 257 $second_call = wp_normalize_path( $path ); 258 $third_call = wp_normalize_path( $path ); 259 260 $this->assertSame( $first_call, $second_call, 'Second call should return same result as first.' ); 261 $this->assertSame( $second_call, $third_call, 'Third call should return same result as second.' ); 262 $this->assertSame( 'C:/www/path/', $first_call, 'Normalized path should match expected value.' ); 263 } 264 265 /** 266 * Tests that wp_normalize_path() static cache stores results. 267 * 268 * @ticket 64538 269 */ 270 public function test_wp_normalize_path_static_cache() { 271 $path = '/var/www/cache-test\\subdir\\'; 272 $expected = '/var/www/cache-test/subdir/'; 273 274 $result = wp_normalize_path( $path ); 275 $this->assertSame( $expected, $result ); 276 277 $reflection = new ReflectionFunction( 'wp_normalize_path' ); 278 $static_vars = $reflection->getStaticVariables(); 279 280 $this->assertArrayHasKey( 'cache', $static_vars, 'Static cache array should exist.' ); 281 $this->assertArrayHasKey( $path, $static_vars['cache'], 'Cache should contain the normalized path.' ); 282 $this->assertSame( $expected, $static_vars['cache'][ $path ], 'Cached value should match the expected normalized path.' ); 226 283 } 227 284
Note: See TracChangeset
for help on using the changeset viewer.