diff options
Diffstat (limited to 'plugins/jetpack/_inc/lib/class.media.php')
-rw-r--r-- | plugins/jetpack/_inc/lib/class.media.php | 71 |
1 files changed, 39 insertions, 32 deletions
diff --git a/plugins/jetpack/_inc/lib/class.media.php b/plugins/jetpack/_inc/lib/class.media.php index 9e419580..9db73961 100644 --- a/plugins/jetpack/_inc/lib/class.media.php +++ b/plugins/jetpack/_inc/lib/class.media.php @@ -17,42 +17,45 @@ class Jetpack_Media { * The hash is built according to the filename trying to avoid name collisions * with other media files. * - * @param number $media_id - media post ID - * @param string $new_filename - the new filename + * @param number $media_id - media post ID. + * @param string $new_filename - the new filename. * @return string A random filename. */ public static function generate_new_filename( $media_id, $new_filename ) { - // get the right filename extension + // Get the right filename extension. $new_filename_paths = pathinfo( $new_filename ); - $new_file_ext = $new_filename_paths['extension']; + $new_file_ext = $new_filename_paths['extension']; + + // Get the file parts from the current attachment. + $current_file = get_attached_file( $media_id ); + $current_file_parts = pathinfo( $current_file ); + $current_file_ext = $current_file_parts['extension']; + $current_file_dirname = $current_file_parts['dirname']; - // take out filename from the original file or from the current attachment + // Take out filename from the original file or from the current attachment. $original_media = (array) self::get_original_media( $media_id ); if ( ! empty( $original_media ) ) { $original_file_parts = pathinfo( $original_media['file'] ); - $filename_base = $original_file_parts['filename']; + $filename_base = $original_file_parts['filename']; } else { - $current_file = get_attached_file( $media_id ); - $current_file_parts = pathinfo( $current_file ); - $current_file_ext = $current_file_parts['filename']; $filename_base = $current_file_parts['filename']; } - // add unique seed based on the filename - $filename_base .= '-' . crc32( $filename_base ) . '-'; + // Add unique seed based on the filename. + $filename_base .= '-' . crc32( $filename_base ) . '-'; $number_suffix = time() . rand( 100, 999 ); do { - $filename = $filename_base; + $filename = $filename_base; $filename .= $number_suffix; - $file_ext = $new_file_ext ? $new_file_ext : $current_file_ext; + $file_ext = $new_file_ext ? $new_file_ext : $current_file_ext; $new_filename = "{$filename}.{$file_ext}"; - $new_path = "{$current_file_parts['dirname']}/$new_filename"; + $new_path = "{$current_file_dirname}/$new_filename"; $number_suffix++; - } while( file_exists( $new_path ) ); + } while ( file_exists( $new_path ) ); return $new_filename; } @@ -70,7 +73,7 @@ class Jetpack_Media { * @param number $media_id * @return string */ - private function get_time_string_from_guid( $media_id ) { + private static function get_time_string_from_guid( $media_id ) { $time = date( "Y/m", strtotime( current_time( 'mysql' ) ) ); if ( $media = get_post( $media_id ) ) { @@ -397,22 +400,24 @@ class Jetpack_Media { * - preserve original media file * - trace revision history * - * @param number $media_id - media post ID - * @param array $file_array - temporal file + * @param number $media_id - media post ID. + * @param array $file_array - temporal file. * @return {Post|WP_Error} Updated media item or a WP_Error is something went wrong. */ public static function edit_media_file( $media_id, $file_array ) { - $media_item = get_post( $media_id ); + $media_item = get_post( $media_id ); $has_original_media = self::get_original_media( $media_id ); if ( ! $has_original_media ) { + // The first time that the media is updated - // the original media is stored into the revision_history + // the original media is stored into the revision_history. $snapshot = self::get_snapshot( $media_item ); + //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase add_post_meta( $media_id, self::$WP_ORIGINAL_MEDIA, $snapshot, true ); } - // save temporary file in the correct location + // Save temporary file in the correct location. $uploaded_file = self::save_temporary_file( $file_array, $media_id ); if ( is_wp_error( $uploaded_file ) ) { @@ -420,27 +425,30 @@ class Jetpack_Media { return $uploaded_file; } - // revision_history control + // Revision_history control. self::register_revision( $media_item, $uploaded_file, $has_original_media ); - $uploaded_path = $uploaded_file['file']; + $uploaded_path = $uploaded_file['file']; $udpated_mime_type = $uploaded_file['type']; - $was_updated = update_attached_file( $media_id, $uploaded_path ); + $was_updated = update_attached_file( $media_id, $uploaded_path ); if ( ! $was_updated ) { return WP_Error( 'update_error', 'Media update error' ); } + // Check maximum amount of revision_history before updating the attachment metadata. + self::limit_revision_history( $media_id ); + $new_metadata = wp_generate_attachment_metadata( $media_id, $uploaded_path ); wp_update_attachment_metadata( $media_id, $new_metadata ); - // check maximum amount of revision_history - self::limit_revision_history( $media_id ); - - $edited_action = wp_update_post( (object) array( - 'ID' => $media_id, - 'post_mime_type' => $udpated_mime_type - ), true ); + $edited_action = wp_update_post( + (object) array( + 'ID' => $media_id, + 'post_mime_type' => $udpated_mime_type, + ), + true + ); if ( is_wp_error( $edited_action ) ) { return $edited_action; @@ -456,4 +464,3 @@ function clean_revision_history( $media_id ) { }; add_action( 'delete_attachment', 'clean_revision_history' ); - |