Changeset 45538
- Timestamp:
- 06/15/2019 01:01:48 AM (5 years ago)
- Location:
- trunk/src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/image.php
r44785 r45538 68 68 69 69 /** 70 * Generate post thumbnail attachment meta data. 70 * Compare the existing image sub-sizes (as saved in the attachment meta) 71 * to the currently registered image sub-sizes, and return the difference. 72 * 73 * Registered sub-sizes that are larger than the image are skipped. 74 * 75 * @since 5.3.0 76 * 77 * @param int $attachment_id The image attachment post ID. 78 * @return array An array of the image sub-sizes that are currently defined but don't exist for this image. 79 */ 80 function wp_get_missing_image_subsizes( $attachment_id ) { 81 if ( ! wp_attachment_is_image( $attachment_id ) ) { 82 return array(); 83 } 84 85 $registered_sizes = wp_get_registered_image_subsizes(); 86 $image_meta = wp_get_attachment_metadata( $attachment_id ); 87 88 // Meta error? 89 if ( empty( $image_meta ) ) { 90 return $defined_sizes; 91 } 92 93 $full_width = (int) $image_meta['width']; 94 $full_height = (int) $image_meta['height']; 95 $possible_sizes = array(); 96 97 // Skip registered sizes that are too large for the uploaded image. 98 foreach ( $registered_sizes as $size_name => $size_data ) { 99 if ( image_resize_dimensions( $full_width, $full_height, $size_data['width'], $size_data['height'], $size_data['crop'] ) ) { 100 $possible_sizes[ $size_name ] = $size_data; 101 } 102 } 103 104 if ( empty( $image_meta['sizes'] ) ) { 105 $image_meta['sizes'] = array(); 106 } 107 108 // Remove sizes that already exist. Only checks for matching "size names". 109 // It is possible that the dimensions for a particular size name have changed. 110 // For example the user has changed the values on the Settings -> Media screen. 111 // However we keep the old sub-sizes with the previous dimensions 112 // as the image may have been used in an older post. 113 $missing_sizes = array_diff_key( $possible_sizes, $image_meta['sizes'] ); 114 115 /** 116 * Filters the array of missing image sub-sizes for an uploaded image. 117 * 118 * @since 5.3.0 119 * 120 * @param array $missing_sizes Array with the missing image sub-sizes. 121 * @param array $image_meta The image meta data. 122 * @param int $attachment_id The image attachment post ID. 123 */ 124 return apply_filters( 'wp_get_missing_image_subsizes', $missing_sizes, $image_meta, $attachment_id ); 125 } 126 127 /** 128 * If any of the currently registered image sub-sizes are missing, 129 * create them and update the image meta data. 130 * 131 * @since 5.3.0 132 * 133 * @param int $attachment_id The image attachment post ID. 134 * @return array The updated image meta data array. 135 */ 136 function wp_update_image_subsizes( $attachment_id ) { 137 $missing_sizes = wp_get_missing_image_subsizes( $attachment_id ); 138 $image_meta = wp_get_attachment_metadata( $attachment_id ); 139 140 if ( empty( $missing_sizes ) ) { 141 return $image_meta; 142 } 143 144 $image_file = get_attached_file( $attachment_id ); 145 146 // This also updates the image meta. 147 return _wp_make_subsizes( $missing_sizes, $image_file, $image_meta, $attachment_id ); 148 } 149 150 /** 151 * Creates image sub-sizes, adds the new data to the image meta `sizes` array, and updates the image metadata. 152 * 153 * Intended for use after an image is uploaded. Saves/updates the image metadata after each 154 * sub-size is created. If there was an error, it is added to the returned image metadata array. 155 * 156 * @since 5.3.0 157 * 158 * @param string $file Full path to the image file. 159 * @param array $image_meta The attachment meta data array. 160 * @param int $attachment_id Attachment Id to process. 161 * @return array The attachment metadata with updated `sizes` array. Includes an array of errors encountered while resizing. 162 */ 163 function wp_create_image_subsizes( $file, $image_meta, $attachment_id ) { 164 if ( empty( $image_meta ) || ! isset( $image_meta['width'], $image_meta['height'] ) ) { 165 // New uploaded image. 166 $imagesize = getimagesize( $file ); 167 $image_meta['width'] = $imagesize[0]; 168 $image_meta['height'] = $imagesize[1]; 169 170 // Make the file path relative to the upload dir. 171 $image_meta['file'] = _wp_relative_upload_path( $file ); 172 173 // Fetch additional metadata from EXIF/IPTC. 174 $exif_meta = wp_read_image_metadata( $file ); 175 176 if ( $exif_meta ) { 177 $image_meta['image_meta'] = $exif_meta; 178 } 179 } 180 181 $new_sizes = wp_get_registered_image_subsizes(); 182 183 /** 184 * Filters the image sizes automatically generated when uploading an image. 185 * 186 * @since 2.9.0 187 * @since 4.4.0 Added the `$image_meta` argument. 188 * @since 5.3.0 Added the `$attachment_id` argument. 189 * 190 * @param array $new_sizes Associative array of image sizes to be created. 191 * @param array $image_meta The image meta data: width, height, file, sizes, etc. 192 * @param int $attachment_id The attachment post ID for the image. 193 */ 194 $new_sizes = apply_filters( 'intermediate_image_sizes_advanced', $new_sizes, $image_meta, $attachment_id ); 195 196 return _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ); 197 } 198 199 /** 200 * Low-level function to create image sub-sizes. 201 * 202 * Updates the image meta after each sub-size is created. 203 * Errors are stored in the returned image metadata array. 204 * 205 * @since 5.3.0 206 * @access private 207 * 208 * $padam array $new_sizes Array defining what sizes to create. 209 * @param string $file Full path to the image file. 210 * @param array $image_meta The attachment meta data array. 211 * @param int $attachment_id Attachment Id to process. 212 * @return array The attachment meta data with updated `sizes` array. Includes an array of errors encountered while resizing. 213 */ 214 function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id ) { 215 // Check if any of the new sizes already exist. 216 if ( isset( $image_meta['sizes'] ) && is_array( $image_meta['sizes'] ) ) { 217 foreach ( $image_meta['sizes'] as $size_name => $size_meta ) { 218 // Only checks "size name" so we don't override existing images even if the dimensions 219 // don't match the currently defined size with the same name. 220 // To change the behavior, unset changed/mismatched sizes in the `sizes` array in image meta. 221 if ( array_key_exists( $size_name, $new_sizes ) ) { 222 unset( $new_sizes[ $size_name ] ); 223 } 224 } 225 } else { 226 $image_meta['sizes'] = array(); 227 } 228 229 if ( ! empty( $new_sizes ) ) { 230 $editor = wp_get_image_editor( $file ); 231 232 if ( ! is_wp_error( $editor ) ) { 233 if ( method_exists( $editor, 'make_subsize' ) ) { 234 foreach ( $new_sizes as $new_size_name => $new_size_data ) { 235 $new_size_meta = $editor->make_subsize( $new_size_data ); 236 237 if ( is_wp_error( $new_size_meta ) ) { 238 if ( empty( $image_meta['subsize_errors'] ) ) { 239 $image_meta['subsize_errors'] = array(); 240 } 241 242 $error = array( 243 'error_code' => $new_size_meta->get_error_code(), 244 'error_message' => $new_size_meta->get_error_message(), 245 ); 246 247 // Store the error code and error message for displaying in the UI. 248 $image_meta['subsize_errors'][ $new_size_name ] = $error; 249 } else { 250 // The sub-size was created successfully. 251 // Clear out previous errors in creating this subsize. 252 if ( ! empty( $image_meta['subsize_errors'][ $new_size_name ] ) ) { 253 unset( $image_meta['subsize_errors'][ $new_size_name ] ); 254 } 255 256 if ( empty( $image_meta['subsize_errors'] ) ) { 257 unset( $image_meta['subsize_errors'] ); 258 } 259 260 // Save the size meta value. 261 $image_meta['sizes'][ $new_size_name ] = $new_size_meta; 262 } 263 264 wp_update_attachment_metadata( $attachment_id, $image_meta ); 265 } 266 } else { 267 // Fall back to `$editor->multi_resize()`. 268 $created_sizes = $editor->multi_resize( $new_sizes ); 269 270 if ( ! empty( $created_sizes ) ) { 271 $image_meta['sizes'] = array_merge( $image_meta['sizes'], $created_sizes ); 272 unset( $image_meta['subsize_errors'] ); 273 wp_update_attachment_metadata( $attachment_id, $image_meta ); 274 } 275 } 276 } 277 } 278 279 return $image_meta; 280 } 281 282 /** 283 * Generate attachment meta data and create image sub-sizes for images. 71 284 * 72 285 * @since 2.1.0 … … 84 297 85 298 if ( preg_match( '!^image/!', $mime_type ) && file_is_displayable_image( $file ) ) { 86 $imagesize = getimagesize( $file );87 $metadata['width'] = $imagesize[0];88 $metadata['height'] = $imagesize[1];89 90 // Make the file path relative to the upload dir.91 $metadata['file'] = _wp_relative_upload_path( $file );92 93 299 // Make thumbnails and other intermediate sizes. 94 $_wp_additional_image_sizes = wp_get_additional_image_sizes(); 95 96 $sizes = array(); 97 foreach ( get_intermediate_image_sizes() as $s ) { 98 $sizes[ $s ] = array( 99 'width' => '', 100 'height' => '', 101 'crop' => false, 102 ); 103 if ( isset( $_wp_additional_image_sizes[ $s ]['width'] ) ) { 104 // For theme-added sizes 105 $sizes[ $s ]['width'] = intval( $_wp_additional_image_sizes[ $s ]['width'] ); 106 } else { 107 // For default sizes set in options 108 $sizes[ $s ]['width'] = get_option( "{$s}_size_w" ); 109 } 110 111 if ( isset( $_wp_additional_image_sizes[ $s ]['height'] ) ) { 112 // For theme-added sizes 113 $sizes[ $s ]['height'] = intval( $_wp_additional_image_sizes[ $s ]['height'] ); 114 } else { 115 // For default sizes set in options 116 $sizes[ $s ]['height'] = get_option( "{$s}_size_h" ); 117 } 118 119 if ( isset( $_wp_additional_image_sizes[ $s ]['crop'] ) ) { 120 // For theme-added sizes 121 $sizes[ $s ]['crop'] = $_wp_additional_image_sizes[ $s ]['crop']; 122 } else { 123 // For default sizes set in options 124 $sizes[ $s ]['crop'] = get_option( "{$s}_crop" ); 125 } 126 } 127 128 /** 129 * Filters the image sizes automatically generated when uploading an image. 130 * 131 * @since 2.9.0 132 * @since 4.4.0 Added the `$metadata` argument. 133 * @since 5.1.0 Added the `$attachment_id` argument. 134 * 135 * @param array $sizes An associative array of image sizes. 136 * @param array $metadata An associative array of image metadata: width, height, file. 137 * @param int $attachment_id Current attachment ID. 138 */ 139 $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes, $metadata, $attachment_id ); 140 141 if ( $sizes ) { 142 $editor = wp_get_image_editor( $file ); 143 144 if ( ! is_wp_error( $editor ) ) { 145 $metadata['sizes'] = $editor->multi_resize( $sizes ); 146 } 147 } else { 148 $metadata['sizes'] = array(); 149 } 150 151 // Fetch additional metadata from EXIF/IPTC. 152 $image_meta = wp_read_image_metadata( $file ); 153 if ( $image_meta ) { 154 $metadata['image_meta'] = $image_meta; 155 } 300 $metadata = wp_create_image_subsizes( $file, $metadata, $attachment_id ); 156 301 } elseif ( wp_attachment_is( 'video', $attachment ) ) { 157 302 $metadata = wp_read_video_metadata( $file ); … … 235 380 $fallback_sizes = apply_filters( 'fallback_intermediate_image_sizes', $fallback_sizes, $metadata ); 236 381 237 $sizes = array(); 238 $_wp_additional_image_sizes = wp_get_additional_image_sizes(); 239 240 foreach ( $fallback_sizes as $s ) { 241 if ( isset( $_wp_additional_image_sizes[ $s ]['width'] ) ) { 242 $sizes[ $s ]['width'] = intval( $_wp_additional_image_sizes[ $s ]['width'] ); 243 } else { 244 $sizes[ $s ]['width'] = get_option( "{$s}_size_w" ); 245 } 246 247 if ( isset( $_wp_additional_image_sizes[ $s ]['height'] ) ) { 248 $sizes[ $s ]['height'] = intval( $_wp_additional_image_sizes[ $s ]['height'] ); 249 } else { 250 $sizes[ $s ]['height'] = get_option( "{$s}_size_h" ); 251 } 252 253 if ( isset( $_wp_additional_image_sizes[ $s ]['crop'] ) ) { 254 $sizes[ $s ]['crop'] = $_wp_additional_image_sizes[ $s ]['crop']; 255 } else { 256 // Force thumbnails to be soft crops. 257 if ( 'thumbnail' !== $s ) { 258 $sizes[ $s ]['crop'] = get_option( "{$s}_crop" ); 259 } 260 } 382 $defined_sizes = wp_get_registered_image_subsizes(); 383 $merged_sizes = array_intersect_key( $defined_sizes, array_flip( $fallback_sizes ) ); 384 385 // Force thumbnails to be soft crops. 386 if ( isset( $merged_sizes['thumbnail'] ) && is_array( $merged_sizes['thumbnail'] ) ) { 387 $merged_sizes['thumbnail']['crop'] = false; 261 388 } 262 389 263 390 // Only load PDFs in an image editor if we're processing sizes. 264 if ( ! empty( $ sizes ) ) {391 if ( ! empty( $merged_sizes ) ) { 265 392 $editor = wp_get_image_editor( $file ); 266 393 … … 283 410 284 411 if ( ! is_wp_error( $editor ) ) { 285 $metadata['sizes'] = $editor->multi_resize( $ sizes );412 $metadata['sizes'] = $editor->multi_resize( $merged_sizes ); 286 413 $metadata['sizes']['full'] = $uploaded; 287 414 } … … 450 577 $exif_image_types = apply_filters( 'wp_read_image_metadata_types', array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM ) ); 451 578 452 if ( is_callable( 'exif_read_data' ) && in_array( $image_type, $exif_image_types ) ) {579 if ( is_callable( 'exif_read_data' ) && in_array( $image_type, $exif_image_types, true ) ) { 453 580 $exif = @exif_read_data( $file ); 454 581 … … 572 699 if ( empty( $info ) ) { 573 700 $result = false; 574 } elseif ( ! in_array( $info[2], $displayable_image_types ) ) {701 } elseif ( ! in_array( $info[2], $displayable_image_types, true ) ) { 575 702 $result = false; 576 703 } else { … … 655 782 656 783 if ( $filepath && file_exists( $filepath ) ) { 657 if ( 'full' != $size && ( $data = image_get_intermediate_size( $attachment_id, $size ) ) ) { 784 $data = image_get_intermediate_size( $attachment_id, $size ); 785 786 if ( 'full' != $size && $data ) { 787 $filepath = path_join( dirname( $filepath ), $data['file'] ); 788 658 789 /** 659 790 * Filters the path to the current image. … … 667 798 * @param string $size Size of the image. 668 799 */ 669 $filepath = apply_filters( 'load_image_to_edit_filesystempath', path_join( dirname( $filepath ), $data['file'] ), $attachment_id, $size );670 } 671 } elseif ( function_exists( 'fopen' ) && true == ini_get( 'allow_url_fopen' ) ) {800 $filepath = apply_filters( 'load_image_to_edit_filesystempath', $filepath, $attachment_id, $size ); 801 } 802 } elseif ( function_exists( 'fopen' ) && true === ini_get( 'allow_url_fopen' ) ) { 672 803 /** 673 804 * Filters the image URL if not in the local filesystem. … … 706 837 */ 707 838 function _copy_image_file( $attachment_id ) { 708 $dst_file = $src_file = get_attached_file( $attachment_id ); 839 $dst_file = get_attached_file( $attachment_id ); 840 $src_file = $dst_file; 841 709 842 if ( ! file_exists( $src_file ) ) { 710 843 $src_file = _load_image_to_edit_path( $attachment_id ); -
trunk/src/wp-includes/class-wp-image-editor-gd.php
r44550 r45538 44 44 // On some setups GD library does not provide imagerotate() - Ticket #11536 45 45 if ( isset( $args['methods'] ) && 46 in_array( 'rotate', $args['methods'] ) &&46 in_array( 'rotate', $args['methods'], true ) && 47 47 ! function_exists( 'imagerotate' ) ) { 48 48 … … 179 179 protected function _resize( $max_w, $max_h, $crop = false ) { 180 180 $dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $max_w, $max_h, $crop ); 181 181 182 if ( ! $dims ) { 182 183 return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ), $this->file ); 183 184 } 185 184 186 list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; 185 187 … … 218 220 */ 219 221 public function multi_resize( $sizes ) { 220 $metadata = array(); 222 $metadata = array(); 223 224 foreach ( $sizes as $size => $size_data ) { 225 $meta = $this->make_subsize( $size_data ); 226 227 if ( ! is_wp_error( $meta ) ) { 228 $metadata[ $size ] = $meta; 229 } 230 } 231 232 return $metadata; 233 } 234 235 /** 236 * Create an image sub-size and return the image meta data value for it. 237 * 238 * @since 5.3.0 239 * 240 * @param array $size_data Array of width, height, and whether to crop. 241 * @return WP_Error|array WP_Error on error, or the image data array for inclusion in the `sizes` array in the image meta. 242 */ 243 public function make_subsize( $size_data ) { 244 if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) { 245 return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) ); 246 } 247 221 248 $orig_size = $this->size; 222 249 223 foreach ( $sizes as $size => $size_data ) { 224 if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) { 225 continue; 226 } 227 228 if ( ! isset( $size_data['width'] ) ) { 229 $size_data['width'] = null; 230 } 231 if ( ! isset( $size_data['height'] ) ) { 232 $size_data['height'] = null; 233 } 234 235 if ( ! isset( $size_data['crop'] ) ) { 236 $size_data['crop'] = false; 237 } 238 239 $image = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); 240 $duplicate = ( ( $orig_size['width'] == $size_data['width'] ) && ( $orig_size['height'] == $size_data['height'] ) ); 241 242 if ( ! is_wp_error( $image ) && ! $duplicate ) { 243 $resized = $this->_save( $image ); 244 245 imagedestroy( $image ); 246 247 if ( ! is_wp_error( $resized ) && $resized ) { 248 unset( $resized['path'] ); 249 $metadata[ $size ] = $resized; 250 } 251 } 252 253 $this->size = $orig_size; 254 } 255 256 return $metadata; 250 if ( ! isset( $size_data['width'] ) ) { 251 $size_data['width'] = null; 252 } 253 254 if ( ! isset( $size_data['height'] ) ) { 255 $size_data['height'] = null; 256 } 257 258 if ( ! isset( $size_data['crop'] ) ) { 259 $size_data['crop'] = false; 260 } 261 262 $resized = $this->_resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); 263 264 if ( is_wp_error( $resized ) ) { 265 $saved = $resized; 266 } else { 267 $saved = $this->_save( $resized ); 268 imagedestroy( $resized ); 269 } 270 271 $this->size = $orig_size; 272 273 if ( ! is_wp_error( $saved ) ) { 274 unset( $saved['path'] ); 275 } 276 277 return $saved; 257 278 } 258 279 … … 392 413 } 393 414 394 if ( 'image/gif' == $mime_type ) {415 if ( 'image/gif' === $mime_type ) { 395 416 if ( ! $this->make_image( $filename, 'imagegif', array( $image, $filename ) ) ) { 396 417 return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); 397 418 } 398 } elseif ( 'image/png' == $mime_type ) {419 } elseif ( 'image/png' === $mime_type ) { 399 420 // convert from full colors to index colors, like original PNG. 400 421 if ( function_exists( 'imageistruecolor' ) && ! imageistruecolor( $image ) ) { … … 405 426 return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); 406 427 } 407 } elseif ( 'image/jpeg' == $mime_type ) {428 } elseif ( 'image/jpeg' === $mime_type ) { 408 429 if ( ! $this->make_image( $filename, 'imagejpeg', array( $image, $filename, $this->get_quality() ) ) ) { 409 430 return new WP_Error( 'image_save_error', __( 'Image Editor Save Failed' ) ); -
trunk/src/wp-includes/class-wp-image-editor-imagick.php
r42746 r45538 109 109 // setIteratorIndex is optional unless mime is an animated format. 110 110 // Here, we just say no if you are missing it and aren't loading a jpeg. 111 if ( ! method_exists( 'Imagick', 'setIteratorIndex' ) && $mime_type != 'image/jpeg' ) {111 if ( ! method_exists( 'Imagick', 'setIteratorIndex' ) && $mime_type !== 'image/jpeg' ) { 112 112 return false; 113 113 } … … 147 147 $filename = $this->file; 148 148 149 if ( 'pdf' == $file_extension ) {149 if ( 'pdf' === $file_extension ) { 150 150 $filename = $this->pdf_setup(); 151 151 } … … 194 194 195 195 try { 196 if ( 'image/jpeg' == $this->mime_type ) {196 if ( 'image/jpeg' === $this->mime_type ) { 197 197 $this->image->setImageCompressionQuality( $quality ); 198 198 $this->image->setImageCompression( imagick::COMPRESSION_JPEG ); … … 261 261 return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ) ); 262 262 } 263 263 264 list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims; 264 265 … … 313 314 * Imagick constant is defined or fall back to our default filter. 314 315 */ 315 if ( in_array( $filter_name, $allowed_filters ) && defined( 'Imagick::' . $filter_name ) ) {316 if ( in_array( $filter_name, $allowed_filters, true ) && defined( 'Imagick::' . $filter_name ) ) { 316 317 $filter = constant( 'Imagick::' . $filter_name ); 317 318 } else { … … 362 363 363 364 // Set appropriate quality settings after resizing. 364 if ( 'image/jpeg' == $this->mime_type ) {365 if ( 'image/jpeg' === $this->mime_type ) { 365 366 if ( is_callable( array( $this->image, 'unsharpMaskImage' ) ) ) { 366 367 $this->image->unsharpMaskImage( 0.25, 0.25, 8, 0.065 ); … … 414 415 * 415 416 * @param array $sizes { 416 * An array of image size arrays. Default sizes are ' small', 'medium', 'medium_large', 'large'.417 * An array of image size arrays. Default sizes are 'thumbnail', 'medium', 'medium_large', 'large'. 417 418 * 418 419 * Either a height or width must be provided. … … 431 432 */ 432 433 public function multi_resize( $sizes ) { 433 $metadata = array(); 434 $metadata = array(); 435 436 foreach ( $sizes as $size => $size_data ) { 437 $meta = $this->make_subsize( $size_data ); 438 439 if ( ! is_wp_error( $meta ) ) { 440 $metadata[ $size ] = $meta; 441 } 442 } 443 444 return $metadata; 445 } 446 447 /** 448 * Create an image sub-size and return the image meta data value for it. 449 * 450 * @since 5.3.0 451 * 452 * @param array $size_data Array of width, height, and whether to crop. 453 * @return WP_Error|array WP_Error on error, or the image data array for inclusion in the `sizes` array in the image meta. 454 */ 455 public function make_subsize( $size_data ) { 456 if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) { 457 return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) ); 458 } 459 434 460 $orig_size = $this->size; 435 461 $orig_image = $this->image->getImage(); 436 462 437 foreach ( $sizes as $size => $size_data ) { 438 if ( ! $this->image ) { 439 $this->image = $orig_image->getImage(); 440 } 441 442 if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) { 443 continue; 444 } 445 446 if ( ! isset( $size_data['width'] ) ) { 447 $size_data['width'] = null; 448 } 449 if ( ! isset( $size_data['height'] ) ) { 450 $size_data['height'] = null; 451 } 452 453 if ( ! isset( $size_data['crop'] ) ) { 454 $size_data['crop'] = false; 455 } 456 457 $resize_result = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); 458 $duplicate = ( ( $orig_size['width'] == $size_data['width'] ) && ( $orig_size['height'] == $size_data['height'] ) ); 459 460 if ( ! is_wp_error( $resize_result ) && ! $duplicate ) { 461 $resized = $this->_save( $this->image ); 462 463 $this->image->clear(); 464 $this->image->destroy(); 465 $this->image = null; 466 467 if ( ! is_wp_error( $resized ) && $resized ) { 468 unset( $resized['path'] ); 469 $metadata[ $size ] = $resized; 470 } 471 } 472 473 $this->size = $orig_size; 474 } 475 463 if ( ! isset( $size_data['width'] ) ) { 464 $size_data['width'] = null; 465 } 466 467 if ( ! isset( $size_data['height'] ) ) { 468 $size_data['height'] = null; 469 } 470 471 if ( ! isset( $size_data['crop'] ) ) { 472 $size_data['crop'] = false; 473 } 474 475 $resized = $this->resize( $size_data['width'], $size_data['height'], $size_data['crop'] ); 476 477 if ( is_wp_error( $resized ) ) { 478 $saved = $resized; 479 } else { 480 $saved = $this->_save( $this->image ); 481 482 $this->image->clear(); 483 $this->image->destroy(); 484 $this->image = null; 485 } 486 487 $this->size = $orig_size; 476 488 $this->image = $orig_image; 477 489 478 return $metadata; 490 if ( ! is_wp_error( $saved ) ) { 491 unset( $saved['path'] ); 492 } 493 494 return $saved; 479 495 } 480 496 … … 718 734 // Strip profiles. 719 735 foreach ( $this->image->getImageProfiles( '*', true ) as $key => $value ) { 720 if ( ! in_array( $key, $protected_profiles ) ) {736 if ( ! in_array( $key, $protected_profiles, true ) ) { 721 737 $this->image->removeImageProfile( $key ); 722 738 } -
trunk/src/wp-includes/media.php
r45270 r45538 18 18 function wp_get_additional_image_sizes() { 19 19 global $_wp_additional_image_sizes; 20 20 21 if ( ! $_wp_additional_image_sizes ) { 21 22 $_wp_additional_image_sizes = array(); 22 23 } 24 23 25 return $_wp_additional_image_sizes; 24 26 } … … 67 69 $max_width = $size[0]; 68 70 $max_height = $size[1]; 69 } elseif ( $size == 'thumb' || $size== 'thumbnail' ) {71 } elseif ( $size === 'thumb' || $size === 'thumbnail' ) { 70 72 $max_width = intval( get_option( 'thumbnail_size_w' ) ); 71 73 $max_height = intval( get_option( 'thumbnail_size_h' ) ); … … 75 77 $max_height = 96; 76 78 } 77 } elseif ( $size == 'medium' ) {79 } elseif ( $size === 'medium' ) { 78 80 $max_width = intval( get_option( 'medium_size_w' ) ); 79 81 $max_height = intval( get_option( 'medium_size_h' ) ); 80 82 81 } elseif ( $size == 'medium_large' ) {83 } elseif ( $size === 'medium_large' ) { 82 84 $max_width = intval( get_option( 'medium_large_size_w' ) ); 83 85 $max_height = intval( get_option( 'medium_large_size_h' ) ); … … 86 88 $max_width = min( intval( $content_width ), $max_width ); 87 89 } 88 } elseif ( $size == 'large' ) {90 } elseif ( $size === 'large' ) { 89 91 /* 90 92 * We're inserting a large size image into the editor. If it's a really … … 95 97 $max_width = intval( get_option( 'large_size_w' ) ); 96 98 $max_height = intval( get_option( 'large_size_h' ) ); 99 97 100 if ( intval( $content_width ) > 0 ) { 98 101 $max_width = min( intval( $content_width ), $max_width ); 99 102 } 100 } elseif ( ! empty( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) {103 } elseif ( ! empty( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ), true ) ) { 101 104 $max_width = intval( $_wp_additional_image_sizes[ $size ]['width'] ); 102 105 $max_height = intval( $_wp_additional_image_sizes[ $size ]['height'] ); … … 196 199 * Default 'medium'. 197 200 */ 198 if ( $out = apply_filters( 'image_downsize', false, $id, $size ) ) { 201 $out = apply_filters( 'image_downsize', false, $id, $size ); 202 203 if ( $out ) { 199 204 return $out; 200 205 } … … 202 207 $img_url = wp_get_attachment_url( $id ); 203 208 $meta = wp_get_attachment_metadata( $id ); 204 $width = $height = 0; 209 $width = 0; 210 $height = 0; 205 211 $is_intermediate = false; 206 212 $img_url_basename = wp_basename( $img_url ); … … 220 226 221 227 // try for a new style intermediate size 222 if ( $intermediate = image_get_intermediate_size( $id, $size ) ) { 228 $intermediate = image_get_intermediate_size( $id, $size ); 229 230 if ( $intermediate ) { 223 231 $img_url = str_replace( $img_url_basename, $intermediate['file'], $img_url ); 224 232 $width = $intermediate['width']; 225 233 $height = $intermediate['height']; 226 234 $is_intermediate = true; 227 } elseif ( $size == 'thumbnail' ) {235 } elseif ( $size === 'thumbnail' ) { 228 236 // fall back to the old thumbnail 229 if ( ( $thumb_file = wp_get_attachment_thumb_file( $id ) ) && $info = getimagesize( $thumb_file ) ) { 237 $thumb_file = wp_get_attachment_thumb_file( $id ); 238 $info = getimagesize( $thumb_file ); 239 240 if ( $thumb_file && $info ) { 230 241 $img_url = str_replace( $img_url_basename, wp_basename( $thumb_file ), $img_url ); 231 242 $width = $info[0]; … … 234 245 } 235 246 } 247 236 248 if ( ! $width && ! $height && isset( $meta['width'], $meta['height'] ) ) { 237 249 // any other type: use the real image … … 246 258 return array( $img_url, $width, $height, $is_intermediate ); 247 259 } 260 248 261 return false; 249 250 262 } 251 263 … … 413 425 } 414 426 415 $width_ratio = $height_ratio = 1.0; 416 $did_width = $did_height = false; 427 $width_ratio = 1.0; 428 $height_ratio = 1.0; 429 $did_width = false; 430 $did_height = false; 417 431 418 432 if ( $max_width > 0 && $current_width > 0 && $current_width > $max_width ) { … … 447 461 448 462 // Note: $did_width means it is possible $smaller_ratio == $width_ratio. 449 if ( $did_width && $w == $max_width - 1 ) {463 if ( $did_width && $w === $max_width - 1 ) { 450 464 $w = $max_width; // Round it up 451 465 } 452 466 453 467 // Note: $did_height means it is possible $smaller_ratio == $height_ratio. 454 if ( $did_height && $h == $max_height - 1 ) {468 if ( $did_height && $h === $max_height - 1 ) { 455 469 $h = $max_height; // Round it up 456 470 } … … 521 535 */ 522 536 $output = apply_filters( 'image_resize_dimensions', null, $orig_w, $orig_h, $dest_w, $dest_h, $crop ); 537 523 538 if ( null !== $output ) { 524 539 return $output; … … 577 592 578 593 // if the resulting image would be the same size or larger we don't want to resize it 579 if ( $new_w >= $orig_w && $new_h >= $orig_h && $dest_w != $orig_w && $dest_h != $orig_h) {594 if ( $new_w >= $orig_w && $new_h >= $orig_h && intval( $dest_w ) !== intval( $orig_w ) && intval( $dest_h ) !== intval( $orig_h ) ) { 580 595 return false; 581 596 } … … 688 703 */ 689 704 function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) { 690 if ( ! $size || ! is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) || empty( $imagedata['sizes'] ) ) { 705 $imagedata = wp_get_attachment_metadata( $post_id ); 706 707 if ( ! $size || ! is_array( $imagedata ) || empty( $imagedata['sizes'] ) ) { 691 708 return false; 692 709 } … … 705 722 foreach ( $imagedata['sizes'] as $_size => $data ) { 706 723 // If there's an exact match to an existing image size, short circuit. 707 if ( $data['width'] == $size[0] && $data['height'] == $size[1]) {724 if ( intval( $data['width'] ) === intval( $size[0] ) && intval( $data['height'] ) === intval( $size[1] ) ) { 708 725 $candidates[ $data['width'] * $data['height'] ] = $data; 709 726 break; … … 786 803 */ 787 804 function get_intermediate_image_sizes() { 788 $_wp_additional_image_sizes = wp_get_additional_image_sizes(); 789 $image_sizes = array( 'thumbnail', 'medium', 'medium_large', 'large' ); // Standard sizes 790 if ( ! empty( $_wp_additional_image_sizes ) ) { 791 $image_sizes = array_merge( $image_sizes, array_keys( $_wp_additional_image_sizes ) ); 805 $default_sizes = array( 'thumbnail', 'medium', 'medium_large', 'large' ); 806 $additional_sizes = wp_get_additional_image_sizes(); 807 808 if ( ! empty( $additional_sizes ) ) { 809 $default_sizes = array_merge( $default_sizes, array_keys( $additional_sizes ) ); 792 810 } 793 811 … … 797 815 * @since 2.5.0 798 816 * 799 * @param array $image_sizes An array of intermediate image sizes. Defaults 800 * are 'thumbnail', 'medium', 'medium_large', 'large'. 801 */ 802 return apply_filters( 'intermediate_image_sizes', $image_sizes ); 817 * @param array $default_sizes An array of intermediate image sizes. Defaults 818 * are 'thumbnail', 'medium', 'medium_large', 'large'. 819 */ 820 return apply_filters( 'intermediate_image_sizes', $default_sizes ); 821 } 822 823 /** 824 * Returns a normalized list of all currently registered image sub-sizes. 825 * 826 * @since 5.3.0 827 * @uses wp_get_additional_image_sizes() 828 * @uses get_intermediate_image_sizes() 829 * 830 * @return array Associative array of the registered image sub-sizes. 831 */ 832 function wp_get_registered_image_subsizes() { 833 $additional_sizes = wp_get_additional_image_sizes(); 834 $all_sizes = array(); 835 836 foreach ( get_intermediate_image_sizes() as $size_name ) { 837 $size_data = array( 838 'width' => 0, 839 'height' => 0, 840 'crop' => false, 841 ); 842 843 if ( isset( $additional_sizes[ $size_name ]['width'] ) ) { 844 // For sizes added by plugins and themes. 845 $size_data['width'] = intval( $additional_sizes[ $size_name ]['width'] ); 846 } else { 847 // For default sizes set in options. 848 $size_data['width'] = intval( get_option( "{$size_name}_size_w" ) ); 849 } 850 851 if ( isset( $additional_sizes[ $size_name ]['height'] ) ) { 852 $size_data['height'] = intval( $additional_sizes[ $size_name ]['height'] ); 853 } else { 854 $size_data['height'] = intval( get_option( "{$size_name}_size_h" ) ); 855 } 856 857 if ( empty( $size_data['width'] ) && empty( $size_data['height'] ) ) { 858 // This size isn't set. 859 continue; 860 } 861 862 if ( isset( $additional_sizes[ $size_name ]['crop'] ) ) { 863 $size_data['crop'] = (bool) $additional_sizes[ $size_name ]['crop']; 864 } else { 865 $size_data['crop'] = (bool) get_option( "{$size_name}_crop" ); 866 } 867 868 $all_sizes[ $size_name ] = $size_data; 869 } 870 871 return $all_sizes; 803 872 } 804 873 … … 827 896 $src = false; 828 897 829 if ( $icon && $src = wp_mime_type_icon( $attachment_id ) ) { 830 /** This filter is documented in wp-includes/post.php */ 831 $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); 832 833 $src_file = $icon_dir . '/' . wp_basename( $src ); 834 @list( $width, $height ) = getimagesize( $src_file ); 898 if ( $icon ) { 899 $src = wp_mime_type_icon( $attachment_id ); 900 901 if ( $src ) { 902 /** This filter is documented in wp-includes/post.php */ 903 $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); 904 905 $src_file = $icon_dir . '/' . wp_basename( $src ); 906 @list( $width, $height ) = getimagesize( $src_file ); 907 } 835 908 } 836 909 … … 1015 1088 */ 1016 1089 function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) { 1017 if ( ! $image = wp_get_attachment_image_src( $attachment_id, $size ) ) { 1090 $image = wp_get_attachment_image_src( $attachment_id, $size ); 1091 1092 if ( ! $image ) { 1018 1093 return false; 1019 1094 } … … 1147 1222 // If the file name is part of the `src`, we've confirmed a match. 1148 1223 if ( ! $src_matched && false !== strpos( $image_src, $dirname . $image['file'] ) ) { 1149 $src_matched = $is_src = true; 1224 $src_matched = true; 1225 $is_src = true; 1150 1226 } 1151 1227 … … 1233 1309 */ 1234 1310 function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) { 1235 if ( ! $image = wp_get_attachment_image_src( $attachment_id, $size ) ) { 1311 $image = wp_get_attachment_image_src( $attachment_id, $size ); 1312 1313 if ( ! $image ) { 1236 1314 return false; 1237 1315 } … … 1319 1397 } 1320 1398 1321 $selected_images = $attachment_ids = array(); 1399 $selected_images = array(); 1400 $attachment_ids = array(); 1322 1401 1323 1402 foreach ( $matches[0] as $image ) { 1324 if ( false === strpos( $image, ' srcset=' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) && 1325 ( $attachment_id = absint( $class_id[1] ) ) ) { 1326 1327 /* 1328 * If exactly the same image tag is used more than once, overwrite it. 1329 * All identical tags will be replaced later with 'str_replace()'. 1330 */ 1331 $selected_images[ $image ] = $attachment_id; 1332 // Overwrite the ID when the same image is included more than once. 1333 $attachment_ids[ $attachment_id ] = true; 1403 if ( false === strpos( $image, ' srcset=' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) ) { 1404 $attachment_id = absint( $class_id[1] ); 1405 1406 if ( $attachment_id ) { 1407 /* 1408 * If exactly the same image tag is used more than once, overwrite it. 1409 * All identical tags will be replaced later with 'str_replace()'. 1410 */ 1411 $selected_images[ $image ] = $attachment_id; 1412 // Overwrite the ID when the same image is included more than once. 1413 $attachment_ids[ $attachment_id ] = true; 1414 } 1334 1415 } 1335 1416 } … … 1540 1621 */ 1541 1622 $output = apply_filters( 'img_caption_shortcode', '', $attr, $content ); 1542 if ( $output != '' ) { 1623 1624 if ( ! empty( $output ) ) { 1543 1625 return $output; 1544 1626 } … … 1558 1640 1559 1641 $atts['width'] = (int) $atts['width']; 1642 1560 1643 if ( $atts['width'] < 1 || empty( $atts['caption'] ) ) { 1561 1644 return $content; 1562 1645 } 1563 1646 1564 $id = $caption_id = $describedby = ''; 1647 $id = ''; 1648 $caption_id = ''; 1649 $describedby = ''; 1565 1650 1566 1651 if ( $atts['id'] ) { … … 1604 1689 1605 1690 $style = ''; 1691 1606 1692 if ( $caption_width ) { 1607 1693 $style = 'style="width: ' . (int) $caption_width . 'px" '; … … 1706 1792 */ 1707 1793 $output = apply_filters( 'post_gallery', '', $attr, $instance ); 1708 if ( $output != '' ) { 1794 1795 if ( ! empty( $output ) ) { 1709 1796 return $output; 1710 1797 } … … 1851 1938 1852 1939 $i = 0; 1940 1853 1941 foreach ( $attachments as $id => $attachment ) { 1854 1942 1855 1943 $attr = ( trim( $attachment->post_excerpt ) ) ? array( 'aria-describedby' => "$selector-$id" ) : ''; 1944 1856 1945 if ( ! empty( $atts['link'] ) && 'file' === $atts['link'] ) { 1857 1946 $image_output = wp_get_attachment_link( $id, $atts['size'], false, false, false, $attr ); … … 1861 1950 $image_output = wp_get_attachment_link( $id, $atts['size'], true, false, false, $attr ); 1862 1951 } 1952 1863 1953 $image_meta = wp_get_attachment_metadata( $id ); 1864 1954 1865 1955 $orientation = ''; 1956 1866 1957 if ( isset( $image_meta['height'], $image_meta['width'] ) ) { 1867 1958 $orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape'; 1868 1959 } 1960 1869 1961 $output .= "<{$itemtag} class='gallery-item'>"; 1870 1962 $output .= " … … 1872 1964 $image_output 1873 1965 </{$icontag}>"; 1966 1874 1967 if ( $captiontag && trim( $attachment->post_excerpt ) ) { 1875 1968 $output .= " … … 1878 1971 </{$captiontag}>"; 1879 1972 } 1973 1880 1974 $output .= "</{$itemtag}>"; 1881 if ( ! $html5 && $columns > 0 && ++$i % $columns == 0 ) { 1975 1976 if ( ! $html5 && $columns > 0 && ++$i % $columns === 0 ) { 1882 1977 $output .= '<br style="clear: both" />'; 1883 1978 } … … 2026 2121 */ 2027 2122 $output = apply_filters( 'post_playlist', '', $attr, $instance ); 2028 if ( $output != '' ) { 2123 2124 if ( ! empty( $output ) ) { 2029 2125 return $output; 2030 2126 } … … 2336 2432 */ 2337 2433 $override = apply_filters( 'wp_audio_shortcode_override', '', $attr, $content, $instance ); 2434 2338 2435 if ( '' !== $override ) { 2339 2436 return $override; … … 2360 2457 if ( ! empty( $atts['src'] ) ) { 2361 2458 $type = wp_check_filetype( $atts['src'], wp_get_mime_types() ); 2362 if ( ! in_array( strtolower( $type['ext'] ), $default_types ) ) { 2459 2460 if ( ! in_array( strtolower( $type['ext'] ), $default_types, true ) ) { 2363 2461 return sprintf( '<a class="wp-embedded-audio" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) ); 2364 2462 } 2463 2365 2464 $primary = true; 2366 2465 array_unshift( $default_types, 'src' ); … … 2369 2468 if ( ! empty( $atts[ $ext ] ) ) { 2370 2469 $type = wp_check_filetype( $atts[ $ext ], wp_get_mime_types() ); 2470 2371 2471 if ( strtolower( $type['ext'] ) === $ext ) { 2372 2472 $primary = true; … … 2378 2478 if ( ! $primary ) { 2379 2479 $audios = get_attached_media( 'audio', $post_id ); 2480 2380 2481 if ( empty( $audios ) ) { 2381 2482 return; … … 2384 2485 $audio = reset( $audios ); 2385 2486 $atts['src'] = wp_get_attachment_url( $audio->ID ); 2487 2386 2488 if ( empty( $atts['src'] ) ) { 2387 2489 return; … … 2399 2501 */ 2400 2502 $library = apply_filters( 'wp_audio_shortcode_library', 'mediaelement' ); 2503 2401 2504 if ( 'mediaelement' === $library && did_action( 'init' ) ) { 2402 2505 wp_enqueue_style( 'wp-mediaelement' ); … … 2432 2535 2433 2536 $attr_strings = array(); 2537 2434 2538 foreach ( $html_atts as $k => $v ) { 2435 2539 $attr_strings[] = $k . '="' . esc_attr( $v ) . '"'; … … 2437 2541 2438 2542 $html = ''; 2543 2439 2544 if ( 'mediaelement' === $library && 1 === $instance ) { 2440 2545 $html .= "<!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->\n"; 2441 2546 } 2547 2442 2548 $html .= sprintf( '<audio %s controls="controls">', join( ' ', $attr_strings ) ); 2443 2549 2444 2550 $fileurl = ''; 2445 2551 $source = '<source type="%s" src="%s" />'; 2552 2446 2553 foreach ( $default_types as $fallback ) { 2447 2554 if ( ! empty( $atts[ $fallback ] ) ) { … … 2449 2556 $fileurl = $atts[ $fallback ]; 2450 2557 } 2558 2451 2559 $type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() ); 2452 2560 $url = add_query_arg( '_', $instance, $atts[ $fallback ] ); … … 2458 2566 $html .= wp_mediaelement_fallback( $fileurl ); 2459 2567 } 2568 2460 2569 $html .= '</audio>'; 2461 2570 … … 2545 2654 */ 2546 2655 $override = apply_filters( 'wp_video_shortcode_override', '', $attr, $content, $instance ); 2656 2547 2657 if ( '' !== $override ) { 2548 2658 return $override; … … 2583 2693 } 2584 2694 2585 $is_vimeo = $is_youtube = false; 2695 $is_vimeo = false; 2696 $is_youtube = false; 2586 2697 $yt_pattern = '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#'; 2587 2698 $vimeo_pattern = '#^https?://(.+\.)?vimeo\.com/.*#'; … … 2591 2702 $is_vimeo = ( preg_match( $vimeo_pattern, $atts['src'] ) ); 2592 2703 $is_youtube = ( preg_match( $yt_pattern, $atts['src'] ) ); 2704 2593 2705 if ( ! $is_youtube && ! $is_vimeo ) { 2594 2706 $type = wp_check_filetype( $atts['src'], wp_get_mime_types() ); 2595 if ( ! in_array( strtolower( $type['ext'] ), $default_types ) ) { 2707 2708 if ( ! in_array( strtolower( $type['ext'] ), $default_types, true ) ) { 2596 2709 return sprintf( '<a class="wp-embedded-video" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) ); 2597 2710 } … … 2814 2927 2815 2928 foreach ( $attachments as $k => $attachment ) { 2816 if ( $attachment->ID == $post->ID) {2929 if ( intval( $attachment->ID ) === intval( $post->ID ) ) { 2817 2930 break; 2818 2931 } … … 2867 2980 $attachment = (object) $attachment; 2868 2981 } 2982 2869 2983 if ( ! is_object( $attachment ) ) { 2870 2984 return array(); … … 2879 2993 $objects[] = 'attachment:' . substr( $filename, strrpos( $filename, '.' ) + 1 ); 2880 2994 } 2995 2881 2996 if ( ! empty( $attachment->post_mime_type ) ) { 2882 2997 $objects[] = 'attachment:' . $attachment->post_mime_type; 2998 2883 2999 if ( false !== strpos( $attachment->post_mime_type, '/' ) ) { 2884 3000 foreach ( explode( '/', $attachment->post_mime_type ) as $token ) { … … 2891 3007 2892 3008 $taxonomies = array(); 3009 2893 3010 foreach ( $objects as $object ) { 2894 if ( $taxes = get_object_taxonomies( $object, $output ) ) { 3011 $taxes = get_object_taxonomies( $object, $output ); 3012 3013 if ( $taxes ) { 2895 3014 $taxonomies = array_merge( $taxonomies, $taxes ); 2896 3015 } … … 2918 3037 function get_taxonomies_for_attachments( $output = 'names' ) { 2919 3038 $taxonomies = array(); 3039 2920 3040 foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy ) { 2921 3041 foreach ( $taxonomy->object_type as $object_type ) { 2922 if ( 'attachment' == $object_type || 0 === strpos( $object_type, 'attachment:' ) ) {2923 if ( 'names' == $output ) {3042 if ( 'attachment' === $object_type || 0 === strpos( $object_type, 'attachment:' ) ) { 3043 if ( 'names' === $output ) { 2924 3044 $taxonomies[] = $taxonomy->name; 2925 3045 } else { … … 3194 3314 */ 3195 3315 function wp_prepare_attachment_for_js( $attachment ) { 3196 if ( ! $attachment = get_post( $attachment ) ) { 3316 $attachment = get_post( $attachment ); 3317 3318 if ( ! $attachment ) { 3197 3319 return; 3198 3320 } 3199 3321 3200 if ( 'attachment' != $attachment->post_type ) {3322 if ( 'attachment' !== $attachment->post_type ) { 3201 3323 return; 3202 3324 } … … 3317 3439 3318 3440 /** This filter is documented in wp-includes/media.php */ 3319 if ( $downsize = apply_filters( 'image_downsize', false, $attachment->ID, $size ) ) { 3441 $downsize = apply_filters( 'image_downsize', false, $attachment->ID, $size ); 3442 3443 if ( $downsize ) { 3320 3444 if ( empty( $downsize[3] ) ) { 3321 3445 continue; … … 3817 3941 */ 3818 3942 function get_attached_media( $type, $post = 0 ) { 3819 if ( ! $post = get_post( $post ) ) { 3943 $post = get_post( $post ); 3944 3945 if ( ! $post ) { 3820 3946 return array(); 3821 3947 } … … 3907 4033 */ 3908 4034 function get_post_galleries( $post, $html = true ) { 3909 if ( ! $post = get_post( $post ) ) { 4035 $post = get_post( $post ); 4036 4037 if ( ! $post ) { 3910 4038 return array(); 3911 4039 } … … 4028 4156 */ 4029 4157 function wp_maybe_generate_attachment_metadata( $attachment ) { 4030 if ( empty( $attachment ) || ( empty( $attachment->ID ) || ! $attachment_id = (int)$attachment->ID ) ) {4158 if ( empty( $attachment ) || empty( $attachment->ID ) ) { 4031 4159 return; 4032 4160 } 4033 4161 4034 $file = get_attached_file( $attachment_id ); 4035 $meta = wp_get_attachment_metadata( $attachment_id ); 4162 $attachment_id = (int) $attachment->ID; 4163 $file = get_attached_file( $attachment_id ); 4164 $meta = wp_get_attachment_metadata( $attachment_id ); 4165 4036 4166 if ( empty( $meta ) && file_exists( $file ) ) { 4037 $_meta = get_post_meta( $attachment_id ); 4038 $regeneration_lock = 'wp_generating_att_' . $attachment_id; 4039 if ( ! array_key_exists( '_wp_attachment_metadata', $_meta ) && ! get_transient( $regeneration_lock ) ) { 4040 set_transient( $regeneration_lock, $file ); 4167 $_meta = get_post_meta( $attachment_id ); 4168 $_lock = 'wp_generating_att_' . $attachment_id; 4169 4170 if ( ! array_key_exists( '_wp_attachment_metadata', $_meta ) && ! get_transient( $_lock ) ) { 4171 set_transient( $_lock, $file ); 4041 4172 wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) ); 4042 delete_transient( $ regeneration_lock );4173 delete_transient( $_lock ); 4043 4174 } 4044 4175 } … … 4073 4204 } 4074 4205 4075 $sql 4206 $sql = $wpdb->prepare( 4076 4207 "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s", 4077 4208 $path 4078 4209 ); 4210 4079 4211 $post_id = $wpdb->get_var( $sql ); 4080 4212
Note: See TracChangeset
for help on using the changeset viewer.