Skip to content

Commit 63fab6b

Browse files
committed
Add wp_join_paths and wp_canonicalize_path functions
1 parent b35b3e4 commit 63fab6b

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
},
3030
"files": [
3131
"src/WordPress/Blueprints/functions.php",
32+
"src/WordPress/Filesystem/functions.php",
3233
"src/WordPress/Streams/stream_str_replace.php"
3334
]
3435
},
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
function wp_join_paths() {
4+
$paths = array();
5+
foreach ( func_get_args() as $arg ) {
6+
if ( $arg !== '' ) {
7+
$paths[] = $arg;
8+
}
9+
}
10+
$path = implode( '/', $paths );
11+
12+
return preg_replace( '#/+#', '/', $path );
13+
}
14+
15+
function wp_canonicalize_path( $path ) {
16+
// Convert to absolute path
17+
if ( ! str_starts_with( $path, '/' ) ) {
18+
$path = '/' . $path;
19+
}
20+
21+
// Resolve . and ..
22+
$parts = explode( '/', $path );
23+
$normalized = array();
24+
foreach ( $parts as $part ) {
25+
if ( $part === '.' || $part === '' ) {
26+
continue;
27+
}
28+
if ( $part === '..' ) {
29+
array_pop( $normalized );
30+
continue;
31+
}
32+
$normalized[] = $part;
33+
}
34+
35+
// Reconstruct path
36+
$result = '/' . implode( '/', $normalized );
37+
if ( $result === '/.' ) {
38+
$result = '/';
39+
}
40+
return $result === '' ? '/' : $result;
41+
}

0 commit comments

Comments
 (0)