Skip to content

Commit 8ead342

Browse files
authored
Merge pull request #3858 from wp-cli/3818-basename
Introduce `WP_CLI\Utils\basename()` for locale-independent basename()
2 parents a830543 + 016cc57 commit 8ead342

File tree

7 files changed

+25
-11
lines changed

7 files changed

+25
-11
lines changed

php/WP_CLI/CommandWithUpgrade.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace WP_CLI;
44

55
use WP_CLI;
6+
use WP_CLI\Utils;
67

78
abstract class CommandWithUpgrade extends \WP_CLI_Command {
89

@@ -152,7 +153,7 @@ function install( $args, $assoc_args ) {
152153
if ( $branch_length ) {
153154
$new_path = substr( rtrim( $source, '/' ), 0, - ( $branch_length + 1 ) ) . '/';
154155
if ( $GLOBALS['wp_filesystem']->move( $source, $new_path ) ) {
155-
WP_CLI::log( "Renamed Github-based project from '" . basename( $source ) . "' to '" . basename( $new_path ) . "'." );
156+
WP_CLI::log( "Renamed Github-based project from '" . Utils\basename( $source ) . "' to '" . Utils\basename( $new_path ) . "'." );
156157
return $new_path;
157158
} else {
158159
return new \WP_Error( 'wpcli_install_gitub', "Couldn't move Github-based project to appropriate directory." );

php/WP_CLI/Extractor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ private static function extract_zip( $zipfile, $dest ) {
4646
if ( true === $res ) {
4747
$tempdir = implode( DIRECTORY_SEPARATOR, Array (
4848
dirname( $zipfile ),
49-
basename( $zipfile, '.zip' ),
49+
Utils\basename( $zipfile, '.zip' ),
5050
$zip->getNameIndex( 0 )
5151
) );
5252

@@ -75,7 +75,7 @@ private static function extract_tarball( $tarball, $dest ) {
7575
$phar = new PharData( $tarball );
7676
$tempdir = implode( DIRECTORY_SEPARATOR, Array (
7777
dirname( $tarball ),
78-
basename( $tarball, '.tar.gz' ),
78+
Utils\basename( $tarball, '.tar.gz' ),
7979
$phar->getFileName()
8080
) );
8181

php/WP_CLI/Runner.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -833,10 +833,10 @@ public function start() {
833833
if ( in_array( $path, $this->_required_files[ $scope ] ) ) {
834834
switch ( $scope ) {
835835
case 'global':
836-
$context = ' (from global ' . basename( $this->global_config_path ) . ')';
836+
$context = ' (from global ' . Utils\basename( $this->global_config_path ) . ')';
837837
break;
838838
case 'project':
839-
$context = ' (from project\'s ' . basename( $this->project_config_path ) . ')';
839+
$context = ' (from project\'s ' . Utils\basename( $this->project_config_path ) . ')';
840840
break;
841841
case 'runtime':
842842
$context = ' (from runtime argument)';
@@ -845,7 +845,7 @@ public function start() {
845845
break;
846846
}
847847
}
848-
WP_CLI::error( sprintf( "Required file '%s' doesn't exist%s.", basename( $path ), $context ) );
848+
WP_CLI::error( sprintf( "Required file '%s' doesn't exist%s.", Utils\basename( $path ), $context ) );
849849
}
850850
Utils\load_file( $path );
851851
WP_CLI::debug( 'Required file from config: ' . $path, 'bootstrap' );

php/commands/media.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ function import( $args, $assoc_args = array() ) {
216216

217217
$file_array = array(
218218
'tmp_name' => $tempfile,
219-
'name' => basename( $file )
219+
'name' => Utils\basename( $file )
220220
);
221221

222222
$post_array= array(
@@ -243,7 +243,7 @@ function import( $args, $assoc_args = array() ) {
243243
}
244244

245245
if ( empty( $post_array['post_title'] ) ) {
246-
$post_array['post_title'] = preg_replace( '/\.[^.]+$/', '', basename( $file ) );
246+
$post_array['post_title'] = preg_replace( '/\.[^.]+$/', '', Utils\basename( $file ) );
247247
}
248248

249249
// Deletes the temporary file.
@@ -294,7 +294,7 @@ function import( $args, $assoc_args = array() ) {
294294
// wp_tempnam() inexplicably forces a .tmp extension, which spoils MIME type detection
295295
private function make_copy( $path ) {
296296
$dir = get_temp_dir();
297-
$filename = basename( $path );
297+
$filename = Utils\basename( $path );
298298
if ( empty( $filename ) )
299299
$filename = time();
300300

php/commands/plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,7 @@ protected function get_status( $file ) {
961961
*/
962962
private function get_details( $file ) {
963963
$plugin_folder = get_plugins( '/' . plugin_basename( dirname( $file ) ) );
964-
$plugin_file = basename( $file );
964+
$plugin_file = Utils\basename( $file );
965965

966966
return $plugin_folder[$plugin_file];
967967
}

php/commands/scaffold.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ private function scaffold_plugin_theme_tests( $args, $assoc_args, $type ) {
705705
WP_CLI::error( "Invalid {$type} directory specified." );
706706
}
707707
if ( empty( $slug ) ) {
708-
$slug = basename( $target_dir );
708+
$slug = Utils\basename( $target_dir );
709709
}
710710
}
711711

php/utils.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,3 +823,16 @@ function parse_str_to_argv( $arguments ) {
823823
}, $argv );
824824
return $argv;
825825
}
826+
827+
/**
828+
* Locale-independent version of basename()
829+
*
830+
* @access public
831+
*
832+
* @param string $path
833+
* @param string $suffix
834+
* @return string
835+
*/
836+
function basename( $path, $suffix = '' ) {
837+
return urldecode( \basename( str_replace( array( '%2F', '%5C' ), '/', urlencode( $path ) ), $suffix ) );
838+
}

0 commit comments

Comments
 (0)