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
This commit is contained in:
Konstantin Obenland 2015-07-15 19:54:25 +00:00
parent 4cb73cf347
commit 2883bbbb68
4 changed files with 52 additions and 11 deletions

View File

@ -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 ) );

View File

@ -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.

View File

@ -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' ),

View File

@ -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.