From 7ac88c02fa0b66256797db374123dc1d9f9cc736 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Sat, 20 Aug 2016 23:36:28 +0000 Subject: [PATCH] Media: when calling `pathinfo()`, also pass a `PATHINFO_*` constant to avoid array notices for unset keys. Props JaworskiMatt. Fixes #37608. Built from https://develop.svn.wordpress.org/trunk@38294 git-svn-id: http://core.svn.wordpress.org/trunk@38235 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/file.php | 5 +-- wp-admin/includes/image-edit.php | 44 +++++++++++++++------------ wp-admin/includes/media.php | 5 ++- wp-includes/class-wp-image-editor.php | 13 +++----- wp-includes/functions.php | 11 ++++--- wp-includes/version.php | 2 +- 6 files changed, 42 insertions(+), 38 deletions(-) diff --git a/wp-admin/includes/file.php b/wp-admin/includes/file.php index face9164b4..245acf3f16 100644 --- a/wp-admin/includes/file.php +++ b/wp-admin/includes/file.php @@ -77,9 +77,10 @@ $wp_file_descriptions = array( function get_file_description( $file ) { global $wp_file_descriptions, $allowed_files; - $relative_pathinfo = pathinfo( $file ); + $dirname = pathinfo( $file, PATHINFO_DIRNAME ); + $file_path = $allowed_files[ $file ]; - if ( isset( $wp_file_descriptions[ basename( $file ) ] ) && '.' === $relative_pathinfo['dirname'] ) { + if ( isset( $wp_file_descriptions[ basename( $file ) ] ) && '.' === $dirname ) { return $wp_file_descriptions[ basename( $file ) ]; } elseif ( file_exists( $file_path ) && is_file( $file_path ) ) { $template_data = implode( '', file( $file_path ) ); diff --git a/wp-admin/includes/image-edit.php b/wp-admin/includes/image-edit.php index 928cd1357f..2d9e053525 100644 --- a/wp-admin/includes/image-edit.php +++ b/wp-admin/includes/image-edit.php @@ -761,28 +761,33 @@ function wp_save_image( $post_id ) { $backup_sizes = array(); // Generate new filename. - $path = get_attached_file($post_id); - $path_parts = pathinfo( $path ); - $filename = $path_parts['filename']; + $path = get_attached_file( $post_id ); + + $basename = pathinfo( $path, PATHINFO_BASENAME ); + $dirname = pathinfo( $path, PATHINFO_DIRNAME ); + $ext = pathinfo( $path, PATHINFO_EXTENSION ); + $filename = pathinfo( $path, PATHINFO_FILENAME ); $suffix = time() . rand(100, 999); if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE && - isset($backup_sizes['full-orig']) && $backup_sizes['full-orig']['file'] != $path_parts['basename'] ) { + isset($backup_sizes['full-orig']) && $backup_sizes['full-orig']['file'] != $basename ) { - if ( 'thumbnail' == $target ) - $new_path = "{$path_parts['dirname']}/{$filename}-temp.{$path_parts['extension']}"; - else + if ( 'thumbnail' == $target ) { + $new_path = "{$dirname}/{$filename}-temp.{$ext}"; + } else { $new_path = $path; + } } else { - while( true ) { + while ( true ) { $filename = preg_replace( '/-e([0-9]+)$/', '', $filename ); $filename .= "-e{$suffix}"; - $new_filename = "{$filename}.{$path_parts['extension']}"; - $new_path = "{$path_parts['dirname']}/$new_filename"; - if ( file_exists($new_path) ) + $new_filename = "{$filename}.{$ext}"; + $new_path = "{$dirname}/$new_filename"; + if ( file_exists($new_path) ) { $suffix++; - else + } else { break; + } } } @@ -792,18 +797,19 @@ function wp_save_image( $post_id ) { return $return; } - if ( 'nothumb' == $target || 'all' == $target || 'full' == $target || $scaled ) { + if ( 'nothumb' === $target || 'all' === $target || 'full' === $target || $scaled ) { $tag = false; - if ( isset($backup_sizes['full-orig']) ) { - if ( ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE ) && $backup_sizes['full-orig']['file'] != $path_parts['basename'] ) + if ( isset( $backup_sizes['full-orig'] ) ) { + if ( ( ! defined( 'IMAGE_EDIT_OVERWRITE' ) || ! IMAGE_EDIT_OVERWRITE ) && $backup_sizes['full-orig']['file'] !== $basename ) { $tag = "full-$suffix"; + } } else { $tag = 'full-orig'; } - if ( $tag ) - $backup_sizes[$tag] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $path_parts['basename']); - + if ( $tag ) { + $backup_sizes[$tag] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $basename ); + } $success = ( $path === $new_path ) || update_attached_file( $post_id, $new_path ); $meta['file'] = _wp_relative_upload_path( $new_path ); @@ -834,7 +840,7 @@ function wp_save_image( $post_id ) { if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE && ! empty( $meta['sizes'] ) ) { foreach ( $meta['sizes'] as $size ) { if ( ! empty( $size['file'] ) && preg_match( '/-e[0-9]{13}-/', $size['file'] ) ) { - $delete_file = path_join( $path_parts['dirname'], $size['file'] ); + $delete_file = path_join( $dirname, $size['file'] ); wp_delete_file( $delete_file ); } } diff --git a/wp-admin/includes/media.php b/wp-admin/includes/media.php index 8b95181c54..53987bbe4f 100644 --- a/wp-admin/includes/media.php +++ b/wp-admin/includes/media.php @@ -284,13 +284,12 @@ function media_handle_upload($file_id, $post_id, $post_data = array(), $override if ( isset($file['error']) ) return new WP_Error( 'upload_error', $file['error'] ); - $name_parts = pathinfo($name); - $name = trim( substr( $name, 0, -(1 + strlen($name_parts['extension'])) ) ); + $basename = pathinfo( $name, PATHINFO_BASENAME ); $url = $file['url']; $type = $file['type']; $file = $file['file']; - $title = $name; + $title = $basename; $content = ''; $excerpt = ''; diff --git a/wp-includes/class-wp-image-editor.php b/wp-includes/class-wp-image-editor.php index c046823e9f..cc991eb8a9 100644 --- a/wp-includes/class-wp-image-editor.php +++ b/wp-includes/class-wp-image-editor.php @@ -346,12 +346,8 @@ abstract class WP_Image_Editor { } if ( $filename ) { - $ext = ''; - $info = pathinfo( $filename ); - $dir = $info['dirname']; - - if ( isset( $info['extension'] ) ) - $ext = $info['extension']; + $dir = pathinfo( $filename, PATHINFO_DIRNAME ); + $ext = pathinfo( $filename, PATHINFO_EXTENSION ); $filename = trailingslashit( $dir ) . wp_basename( $filename, ".$ext" ) . ".{$new_ext}"; } @@ -375,9 +371,8 @@ abstract class WP_Image_Editor { if ( ! $suffix ) $suffix = $this->get_suffix(); - $info = pathinfo( $this->file ); - $dir = $info['dirname']; - $ext = $info['extension']; + $dir = pathinfo( $this->file, PATHINFO_DIRNAME ); + $ext = pathinfo( $this->file, PATHINFO_EXTENSION ); $name = wp_basename( $this->file, ".$ext" ); $new_ext = strtolower( $extension ? $extension : $ext ); diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 204211bade..de12f2bb46 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -2034,13 +2034,16 @@ function wp_unique_filename( $dir, $filename, $unique_filename_callback = null ) $filename = sanitize_file_name($filename); // Separate the filename into a name and extension. - $info = pathinfo($filename); - $ext = !empty($info['extension']) ? '.' . $info['extension'] : ''; - $name = basename($filename, $ext); + $ext = pathinfo( $filename, PATHINFO_EXTENSION ); + $name = pathinfo( $filename, PATHINFO_BASENAME ); + if ( $ext ) { + $ext = '.' . $ext; + } // Edge case: if file is named '.ext', treat as an empty name. - if ( $name === $ext ) + if ( $name === $ext ) { $name = ''; + } /* * Increment the file number until we have a unique file to save in $dir. diff --git a/wp-includes/version.php b/wp-includes/version.php index aa702ec7da..24df26c38f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.7-alpha-38293'; +$wp_version = '4.7-alpha-38294'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.