Skip to content

Commit

Permalink
Add wp_join_paths and wp_canonicalize_path functions
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Jan 10, 2025
1 parent b35b3e4 commit 63fab6b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"files": [
"src/WordPress/Blueprints/functions.php",
"src/WordPress/Filesystem/functions.php",
"src/WordPress/Streams/stream_str_replace.php"
]
},
Expand Down
41 changes: 41 additions & 0 deletions src/WordPress/Filesystem/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

function wp_join_paths() {
$paths = array();
foreach ( func_get_args() as $arg ) {
if ( $arg !== '' ) {
$paths[] = $arg;
}
}
$path = implode( '/', $paths );

return preg_replace( '#/+#', '/', $path );
}

function wp_canonicalize_path( $path ) {
// Convert to absolute path
if ( ! str_starts_with( $path, '/' ) ) {
$path = '/' . $path;
}

// Resolve . and ..
$parts = explode( '/', $path );
$normalized = array();
foreach ( $parts as $part ) {
if ( $part === '.' || $part === '' ) {
continue;
}
if ( $part === '..' ) {
array_pop( $normalized );
continue;
}
$normalized[] = $part;
}

// Reconstruct path
$result = '/' . implode( '/', $normalized );
if ( $result === '/.' ) {
$result = '/';
}
return $result === '' ? '/' : $result;
}

0 comments on commit 63fab6b

Please sign in to comment.