WordPress.org

Make WordPress Core

Ticket #29211: 29211.5.diff

File 29211.5.diff, 6.6 KB (added by celloexpressions, 5 years ago)

Make the filter for fallback ajax image cropping attachment handling usable, handle attachments for cropped images for the Customizer, fix script dependencies, and clarify Customizer control class inheritance for the cropped-image control.

  • src/wp-admin/includes/ajax-actions.php

     
    30763076                wp_send_json_error( array( 'message' => __( 'Image could not be processed.' ) ) );
    30773077        }
    30783078
    3079         switch ( $context ) {
    3080                 case 'site-icon':
    3081                         require_once ABSPATH . '/wp-admin/includes/class-wp-site-icon.php';
    3082                         global $wp_site_icon;
     3079        if ( 'site-icon' === $context ) {
     3080                require_once ABSPATH . '/wp-admin/includes/class-wp-site-icon.php';
     3081                global $wp_site_icon;
    30833082
    3084                         /** This filter is documented in wp-admin/custom-header.php */
    3085                         $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
    3086                         $object  = $wp_site_icon->create_attachment_object( $cropped, $attachment_id );
    3087                         unset( $object['ID'] );
     3083                /** This filter is documented in wp-admin/custom-header.php */
     3084                $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
     3085                $object  = $wp_site_icon->create_attachment_object( $cropped, $attachment_id );
     3086                unset( $object['ID'] );
    30883087
    3089                         // Update the attachment.
    3090                         add_filter( 'intermediate_image_sizes_advanced', array( $wp_site_icon, 'additional_sizes' ) );
    3091                         $attachment_id = $wp_site_icon->insert_attachment( $object, $cropped );
    3092                         remove_filter( 'intermediate_image_sizes_advanced', array( $wp_site_icon, 'additional_sizes' ) );
     3088                // Update the attachment.
     3089                add_filter( 'intermediate_image_sizes_advanced', array( $wp_site_icon, 'additional_sizes' ) );
     3090                $attachment_id = $wp_site_icon->insert_attachment( $object, $cropped );
     3091                remove_filter( 'intermediate_image_sizes_advanced', array( $wp_site_icon, 'additional_sizes' ) );
    30933092
    3094                         // Additional sizes in wp_prepare_attachment_for_js().
    3095                         add_filter( 'image_size_names_choose', array( $wp_site_icon, 'additional_sizes' ) );
    3096                         break;
     3093                // Additional sizes in wp_prepare_attachment_for_js().
     3094                add_filter( 'image_size_names_choose', array( $wp_site_icon, 'additional_sizes' ) );
    30973095
    3098                 default:
     3096        } elseif ( 'on' === $_POST[ 'wp_customize' ] ) {
     3097                // Customizer context. Handle attachment creation and allow modification via filters.
     3098                $parent     = get_post( $attachment_id );
     3099                $parent_url = $parent->guid;
     3100                $url        = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url );
    30993101
    3100                         /**
    3101                          * Filter the attachment ID for a cropped image.
    3102                          *
    3103                          * @since 4.3.0
    3104                          *
    3105                          * @param int    $attachment_id The ID of the cropped image.
    3106                          * @param string $context       The feature requesting the cropped image.
    3107                          */
    3108                         $attachment_id = apply_filters( 'wp_ajax_cropped_attachment_id', 0, $context );
     3102                $size       = @getimagesize( $cropped );
     3103                $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
    31093104
    3110                         if ( ! $attachment_id ) {
    3111                                 wp_send_json_error();
    3112                         }
     3105                $object = array(
     3106                        'post_title'     => basename( $cropped ),
     3107                        'post_content'   => $url,
     3108                        'post_mime_type' => $image_type,
     3109                        'guid'           => $url,
     3110                        'context'        => $context,
     3111                );
     3112
     3113                $cropped_attachment_id = wp_insert_attachment( $object, $cropped );
     3114                $metadata = wp_generate_attachment_metadata( $cropped_attachment_id, $cropped );
     3115
     3116                /**
     3117                 * Filter the Customizer-croppped image attachment metadata.
     3118                 *
     3119                 * @since 4.3.0
     3120                 *
     3121                 * @see wp_generate_attachment_metadata()
     3122                 *
     3123                 * @param array $metadata Attachment metadata.
     3124                 */
     3125                $metadata = apply_filters( 'customize_crop_image_attachment_metadata', $metadata );
     3126                wp_update_attachment_metadata( $cropped_attachment_id, $metadata );
     3127
     3128                /**
     3129                 * Filter the attachment ID for a cropped image.
     3130                 *
     3131                 * @since 4.3.0
     3132                 *
     3133                 * @param int    $attachment_id The attachment ID of the cropped image.
     3134                 * @param string $context       The Customizer control requesting the cropped image.
     3135                 */
     3136                $attachment_id = apply_filters( 'customize_ajax_cropped_attachment_id', $cropped_attachment_id, $context );
     3137
     3138        } else {
     3139                // Custom, non-Customizer context. Plugins must implement attachment handling.
     3140
     3141                /**
     3142                 * Filter the attachment ID for a cropped image.
     3143                 *
     3144                 * An attachment ID for the cropped image must be returned, but the original attachment id
     3145                 * is passed. The cropped image will not be returned from the ajax call if a new attachment
     3146                 * is not created with this filter.
     3147                 *
     3148                 * @since 4.3.0
     3149                 *
     3150                 * @param int    $attachment_id The ID of the original image attachment.
     3151                 * @param string $context       The feature requesting the cropped image.
     3152                 * @param string $cropped       The filepath of the cropped image.
     3153                 */
     3154                $attachment_id = apply_filters( 'wp_ajax_cropped_attachment_id', $attachment_id, $context, $cropped );
    31133155        }
    31143156
     3157        if ( ! $attachment_id ) {
     3158                wp_send_json_error();
     3159        }               
     3160
    31153161        wp_send_json_success( wp_prepare_attachment_for_js( $attachment_id ) );
    31163162}
  • src/wp-includes/class-wp-customize-control.php

     
    10051005 *
    10061006 * @since 4.3.0
    10071007 *
    1008  * @see WP_Customize_Image_Control
     1008 * @see WP_Customize_Media_Control
    10091009 */
    1010 class WP_Customize_Cropped_Image_Control extends WP_Customize_Image_Control {
     1010class WP_Customize_Cropped_Image_Control extends WP_Customize_Media_Control {
    10111011
    10121012        /**
    10131013         * Control type.
  • src/wp-includes/script-loader.php

     
    410410        $scripts->add( 'customize-loader',   "/wp-includes/js/customize-loader$suffix.js",   array( 'customize-base' ), false, 1 );
    411411        $scripts->add( 'customize-preview',  "/wp-includes/js/customize-preview$suffix.js",  array( 'customize-base' ), false, 1 );
    412412        $scripts->add( 'customize-models',   "/wp-includes/js/customize-models.js", array( 'underscore', 'backbone' ), false, 1 );
    413         $scripts->add( 'customize-views',    "/wp-includes/js/customize-views.js",  array( 'jquery', 'underscore', 'imgareaselect', 'customize-models' ), false, 1 );
     413        $scripts->add( 'customize-views',    "/wp-includes/js/customize-views.js",  array( 'jquery', 'underscore', 'imgareaselect', 'customize-models', 'media-editor', 'media-views' ), false, 1 );
    414414        $scripts->add( 'customize-controls', "/wp-admin/js/customize-controls$suffix.js", array( 'customize-base' ), false, 1 );
    415415        did_action( 'init' ) && $scripts->localize( 'customize-controls', '_wpCustomizeControlsL10n', array(
    416416                'activate'           => __( 'Save & Activate' ),