Replace usages of basename() with wp_basename() in order to support multibyte filenames

This is focused on the pieces of code that touch media files and the tests that support them. `basename` isn't multibyte compatible out of the box. See http://php.net/basename and https://bugs.php.net/bug.php?id=62119.

See #43170.
Props Viper007Bond.

Built from https://develop.svn.wordpress.org/trunk@44785


git-svn-id: http://core.svn.wordpress.org/trunk@44617 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Aaron Jorbin
2019-03-01 20:58:52 +00:00
parent e0e99fe82e
commit 71cf332e65
18 changed files with 44 additions and 44 deletions

View File

@@ -494,7 +494,7 @@ class Custom_Background {
$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
$filename = basename( $file );
$filename = wp_basename( $file );
// Construct the object array
$object = array(

View File

@@ -820,7 +820,7 @@ endif;
return $this->finished();
} elseif ( $width > $max_width ) {
$oitar = $width / $max_width;
$image = wp_crop_image( $attachment_id, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace( basename( $file ), 'midsize-' . basename( $file ), $file ) );
$image = wp_crop_image( $attachment_id, 0, 0, $width, $height, $max_width, $height / $oitar, false, str_replace( wp_basename( $file ), 'midsize-' . wp_basename( $file ), $file ) );
if ( ! $image || is_wp_error( $image ) ) {
wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
}
@@ -828,7 +828,7 @@ endif;
/** This filter is documented in wp-admin/custom-header.php */
$image = apply_filters( 'wp_create_file_in_uploads', $image, $attachment_id ); // For replication
$url = str_replace( basename( $url ), basename( $image ), $url );
$url = str_replace( wp_basename( $url ), wp_basename( $image ), $url );
$width = $width / $oitar;
$height = $height / $oitar;
} else {
@@ -895,7 +895,7 @@ endif;
$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
$filename = basename( $file );
$filename = wp_basename( $file );
// Construct the object array
$object = array(
@@ -984,7 +984,7 @@ endif;
$this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
// Cleanup.
$medium = str_replace( basename( $original ), 'midsize-' . basename( $original ), $original );
$medium = str_replace( wp_basename( $original ), 'midsize-' . wp_basename( $original ), $original );
if ( file_exists( $medium ) ) {
wp_delete_file( $medium );
}
@@ -1208,14 +1208,14 @@ endif;
final public function create_attachment_object( $cropped, $parent_attachment_id ) {
$parent = get_post( $parent_attachment_id );
$parent_url = wp_get_attachment_url( $parent->ID );
$url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
$url = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
$size = @getimagesize( $cropped );
$image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
$object = array(
'ID' => $parent_attachment_id,
'post_title' => basename( $cropped ),
'post_title' => wp_basename( $cropped ),
'post_mime_type' => $image_type,
'guid' => $url,
'context' => 'custom-header',

View File

@@ -3651,13 +3651,13 @@ function wp_ajax_crop_image() {
$cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
$parent_url = wp_get_attachment_url( $attachment_id );
$url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
$url = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
$size = @getimagesize( $cropped );
$image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
$object = array(
'post_title' => basename( $cropped ),
'post_title' => wp_basename( $cropped ),
'post_content' => $url,
'post_mime_type' => $image_type,
'guid' => $url,

View File

@@ -85,14 +85,14 @@ class WP_Site_Icon {
public function create_attachment_object( $cropped, $parent_attachment_id ) {
$parent = get_post( $parent_attachment_id );
$parent_url = wp_get_attachment_url( $parent->ID );
$url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
$url = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
$size = @getimagesize( $cropped );
$image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
$object = array(
'ID' => $parent_attachment_id,
'post_title' => basename( $cropped ),
'post_title' => wp_basename( $cropped ),
'post_content' => $url,
'post_mime_type' => $image_type,
'guid' => $url,

View File

@@ -33,7 +33,7 @@ function wp_image_editor( $post_id, $msg = false ) {
$backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true );
$can_restore = false;
if ( ! empty( $backup_sizes ) && isset( $backup_sizes['full-orig'], $meta['file'] ) ) {
$can_restore = $backup_sizes['full-orig']['file'] != basename( $meta['file'] );
$can_restore = $backup_sizes['full-orig']['file'] != wp_basename( $meta['file'] );
}
if ( $msg ) {

View File

@@ -47,7 +47,7 @@ function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $s
}
if ( ! $dst_file ) {
$dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file );
$dst_file = str_replace( wp_basename( $src_file ), 'cropped-' . wp_basename( $src_file ), $src_file );
}
/*
@@ -56,7 +56,7 @@ function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $s
*/
wp_mkdir_p( dirname( $dst_file ) );
$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) );
$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), wp_basename( $dst_file ) );
$result = $editor->save( $dst_file );
if ( is_wp_error( $result ) ) {
@@ -189,7 +189,7 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) {
$ext = '.png';
break;
}
$basename = str_replace( '.', '-', basename( $file ) ) . '-image' . $ext;
$basename = str_replace( '.', '-', wp_basename( $file ) ) . '-image' . $ext;
$uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] );
if ( false === $uploaded['error'] ) {
$image_attachment = array(
@@ -711,8 +711,8 @@ function _copy_image_file( $attachment_id ) {
}
if ( $src_file ) {
$dst_file = str_replace( basename( $dst_file ), 'copy-' . basename( $dst_file ), $dst_file );
$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) );
$dst_file = str_replace( wp_basename( $dst_file ), 'copy-' . wp_basename( $dst_file ), $dst_file );
$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), wp_basename( $dst_file ) );
/*
* The directory containing the original file may no longer

View File

@@ -99,7 +99,7 @@ function wp_import_handle_upload() {
// Construct the object array
$object = array(
'post_title' => basename( $upload['file'] ),
'post_title' => wp_basename( $upload['file'] ),
'post_content' => $upload['url'],
'post_mime_type' => $upload['type'],
'guid' => $upload['url'],

View File

@@ -434,7 +434,7 @@ function media_handle_sideload( $file_array, $post_id, $desc = null, $post_data
$url = $file['url'];
$type = $file['type'];
$file = $file['file'];
$title = preg_replace( '/\.[^.]+$/', '', basename( $file ) );
$title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) );
$content = '';
// Use image exif/iptc data for title and caption defaults if possible.
@@ -826,7 +826,7 @@ function wp_media_upload_handler() {
if ( isset( $_POST['media_type'] ) && 'image' != $_POST['media_type'] ) {
$title = esc_html( wp_unslash( $_POST['title'] ) );
if ( empty( $title ) ) {
$title = esc_html( basename( $src ) );
$title = esc_html( wp_basename( $src ) );
}
if ( $title && $src ) {
@@ -930,7 +930,7 @@ function media_sideload_image( $file, $post_id, $desc = null, $return = 'html' )
}
$file_array = array();
$file_array['name'] = basename( $matches[0] );
$file_array['name'] = wp_basename( $matches[0] );
// Download file to temp location.
$file_array['tmp_name'] = download_url( $file );

View File

@@ -362,7 +362,7 @@ function wp_print_theme_file_tree( $tree, $level = 2, $size = 1, $index = 1 ) {
aria-posinset="<?php echo esc_attr( $index ); ?>">
<?php
$file_description = esc_html( get_file_description( $filename ) );
if ( $file_description !== $filename && basename( $filename ) !== $file_description ) {
if ( $file_description !== $filename && wp_basename( $filename ) !== $file_description ) {
$file_description .= '<br /><span class="nonessential">(' . esc_html( $filename ) . ')</span>';
}