From 2883bbbb68ddd5ae80e460aa8e22b07acaf56af1 Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Wed, 15 Jul 2015 19:54:25 +0000 Subject: [PATCH] Customize: Provide a default way to save cropped images. Allows plugins and themes to use `WP_Customize_Cropped_Image_Control` without having to define their own way of saving the cropped image. Props celloexpressions for initial patch. Fixes #29211. Built from https://develop.svn.wordpress.org/trunk@33280 git-svn-id: http://core.svn.wordpress.org/trunk@33252 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/ajax-actions.php | 55 +++++++++++++++++++--- wp-includes/class-wp-customize-control.php | 4 +- wp-includes/script-loader.php | 2 +- wp-includes/version.php | 2 +- 4 files changed, 52 insertions(+), 11 deletions(-) diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index 99b7d1576a..df90b97a45 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -3094,19 +3094,60 @@ function wp_ajax_crop_image() { default: + /** + * Fires before a cropped image is saved. + * + * Allows to add filters to modify the way a cropped image is saved. + * + * @since 4.3.0 + * + * @param string $context The Customizer control requesting the cropped image. + * @param int $attachment_id The attachment ID of the original image. + * @param string $cropped Path to the cropped image file. + */ + do_action( 'wp_ajax_crop_image_pre_save', $context, $attachment_id, $cropped ); + + /** This filter is documented in wp-admin/custom-header.php */ + $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication. + + $parent_url = get_post( $attachment_id )->guid; + $url = str_replace( basename( $parent_url ), basename( $cropped ), $parent_url ); + + $size = @getimagesize( $cropped ); + $image_type = ( $size ) ? $size['mime'] : 'image/jpeg'; + + $object = array( + 'post_title' => basename( $cropped ), + 'post_content' => $url, + 'post_mime_type' => $image_type, + 'guid' => $url, + 'context' => $context, + ); + + $attachment_id = wp_insert_attachment( $object, $cropped ); + $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped ); + + /** + * Filter the cropped image attachment metadata. + * + * @since 4.3.0 + * + * @see wp_generate_attachment_metadata() + * + * @param array $metadata Attachment metadata. + */ + $metadata = apply_filters( 'wp_ajax_cropped_attachment_metadata', $metadata ); + wp_update_attachment_metadata( $attachment_id, $metadata ); + /** * Filter the attachment ID for a cropped image. * * @since 4.3.0 * - * @param int $attachment_id The ID of the cropped image. - * @param string $context The feature requesting the cropped image. + * @param int $attachment_id The attachment ID of the cropped image. + * @param string $context The Customizer control requesting the cropped image. */ - $attachment_id = apply_filters( 'wp_ajax_cropped_attachment_id', 0, $context ); - - if ( ! $attachment_id ) { - wp_send_json_error(); - } + $attachment_id = apply_filters( 'wp_ajax_cropped_attachment_id', $attachment_id, $context ); } wp_send_json_success( wp_prepare_attachment_for_js( $attachment_id ) ); diff --git a/wp-includes/class-wp-customize-control.php b/wp-includes/class-wp-customize-control.php index 94dc49b40d..2e3f703f0d 100644 --- a/wp-includes/class-wp-customize-control.php +++ b/wp-includes/class-wp-customize-control.php @@ -1005,9 +1005,9 @@ class WP_Customize_Background_Image_Control extends WP_Customize_Image_Control { * * @since 4.3.0 * - * @see WP_Customize_Image_Control + * @see WP_Customize_Media_Control */ -class WP_Customize_Cropped_Image_Control extends WP_Customize_Image_Control { +class WP_Customize_Cropped_Image_Control extends WP_Customize_Media_Control { /** * Control type. diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php index 1188273552..2dee46cfab 100644 --- a/wp-includes/script-loader.php +++ b/wp-includes/script-loader.php @@ -410,7 +410,7 @@ function wp_default_scripts( &$scripts ) { $scripts->add( 'customize-loader', "/wp-includes/js/customize-loader$suffix.js", array( 'customize-base' ), false, 1 ); $scripts->add( 'customize-preview', "/wp-includes/js/customize-preview$suffix.js", array( 'customize-base' ), false, 1 ); $scripts->add( 'customize-models', "/wp-includes/js/customize-models.js", array( 'underscore', 'backbone' ), false, 1 ); - $scripts->add( 'customize-views', "/wp-includes/js/customize-views.js", array( 'jquery', 'underscore', 'imgareaselect', 'customize-models' ), false, 1 ); + $scripts->add( 'customize-views', "/wp-includes/js/customize-views.js", array( 'jquery', 'underscore', 'imgareaselect', 'customize-models', 'media-editor', 'media-views' ), false, 1 ); $scripts->add( 'customize-controls', "/wp-admin/js/customize-controls$suffix.js", array( 'customize-base' ), false, 1 ); did_action( 'init' ) && $scripts->localize( 'customize-controls', '_wpCustomizeControlsL10n', array( 'activate' => __( 'Save & Activate' ), diff --git a/wp-includes/version.php b/wp-includes/version.php index 0cd74198c7..0c28aba66a 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.3-beta2-33279'; +$wp_version = '4.3-beta2-33280'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.