From 2cce6bb55a3abf30916602de24246893fd0dbf4b Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Thu, 19 Oct 2017 04:17:31 +0000 Subject: [PATCH] Customizer: Improve handling of crops in the media library. This is a follow up on r41732, implementing the following improvements: * Attachment parent info is now stored in attachment meta rather than a separate post meta key. * Attachments created from contextual crops (e.g. header, logos, etc.) are filtered out of the media library using a new `_filterContext` method in `wp.media.controller.Library`. Props joemcgill, westonruter. See #21819. git-svn-id: https://develop.svn.wordpress.org/trunk@41937 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/custom-header.php | 10 ++++++---- src/wp-includes/js/media-views.js | 16 ++++++++++++++++ src/wp-includes/js/media/controllers/library.js | 16 ++++++++++++++++ src/wp-includes/media.php | 3 +++ src/wp-includes/theme.php | 2 +- 5 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/wp-admin/custom-header.php b/src/wp-admin/custom-header.php index 8b2973fc11..71561332e0 100644 --- a/src/wp-admin/custom-header.php +++ b/src/wp-admin/custom-header.php @@ -1187,6 +1187,11 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?> $attachment_id = wp_insert_attachment( $object, $cropped ); $metadata = wp_generate_attachment_metadata( $attachment_id, $cropped ); + // If this is a crop, save the original attachment ID as metadata. + if ( $parent_id ) { + $metadata['attachment_parent'] = $parent_id; + } + /** * Filters the header image attachment metadata. * @@ -1197,11 +1202,8 @@ wp_nonce_field( 'custom-header-options', '_wpnonce-custom-header-options' ); ?> * @param array $metadata Attachment metadata. */ $metadata = apply_filters( 'wp_header_image_attachment_metadata', $metadata ); - wp_update_attachment_metadata( $attachment_id, $metadata ); - if ( $parent_id ) { - $meta = add_post_meta( $attachment_id, '_wp_attachment_parent', $parent_id, true ); - } + wp_update_attachment_metadata( $attachment_id, $metadata ); return $attachment_id; } diff --git a/src/wp-includes/js/media-views.js b/src/wp-includes/js/media-views.js index 2e462971e2..b8d30c9c78 100644 --- a/src/wp-includes/js/media-views.js +++ b/src/wp-includes/js/media-views.js @@ -953,6 +953,9 @@ Library = wp.media.controller.State.extend(/** @lends wp.media.controller.Librar }) ); } + this._filterContext(); + this.get('library').on( 'add', this._filterContext, this ); + this.resetDisplays(); }, @@ -1152,6 +1155,19 @@ Library = wp.media.controller.State.extend(/** @lends wp.media.controller.Librar if ( view && view.get( mode ) ) { setUserSetting( 'libraryContent', mode ); } + }, + + /** + * Filter out contextually created attachments (e.g. headers, logos, etc.) + * + * @since 4.9.0 + */ + _filterContext: function() { + var library = this.get('library'); + + library.set( library.filter( function( item ) { + return item.get('context') === ''; + } ) ); } }); diff --git a/src/wp-includes/js/media/controllers/library.js b/src/wp-includes/js/media/controllers/library.js index 89dc0f4daa..6f30cbb354 100644 --- a/src/wp-includes/js/media/controllers/library.js +++ b/src/wp-includes/js/media/controllers/library.js @@ -85,6 +85,9 @@ Library = wp.media.controller.State.extend(/** @lends wp.media.controller.Librar }) ); } + this._filterContext(); + this.get('library').on( 'add', this._filterContext, this ); + this.resetDisplays(); }, @@ -284,6 +287,19 @@ Library = wp.media.controller.State.extend(/** @lends wp.media.controller.Librar if ( view && view.get( mode ) ) { setUserSetting( 'libraryContent', mode ); } + }, + + /** + * Filter out contextually created attachments (e.g. headers, logos, etc.) + * + * @since 4.9.0 + */ + _filterContext: function() { + var library = this.get('library'); + + library.set( library.filter( function( item ) { + return item.get('context') === ''; + } ) ); } }); diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index a280e3e41e..1981f3ad9a 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -3159,6 +3159,9 @@ function wp_prepare_attachment_for_js( $attachment ) { $response['filesizeHumanReadable'] = size_format( $bytes ); } + $context = get_post_meta( $attachment->ID, '_wp_attachment_context', true ); + $response['context'] = ( $context ) ? $context : ''; + if ( current_user_can( 'edit_post', $attachment->ID ) ) { $response['nonces']['update'] = wp_create_nonce( 'update-post_' . $attachment->ID ); $response['nonces']['edit'] = wp_create_nonce( 'image_editor-' . $attachment->ID ); diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php index 66390ff2bf..a69f4830cc 100644 --- a/src/wp-includes/theme.php +++ b/src/wp-includes/theme.php @@ -1214,7 +1214,7 @@ function get_uploaded_header_images() { $header_images[$header_index]['url'] = $url; $header_images[$header_index]['thumbnail_url'] = $url; $header_images[$header_index]['alt_text'] = get_post_meta( $header->ID, '_wp_attachment_image_alt', true ); - $header_images[$header_index]['attachment_parent'] = (int) get_post_meta( $header->ID, '_wp_attachment_parent', true ); + $header_images[$header_index]['attachment_parent'] = isset( $header_data['attachment_parent'] ) ? $header_data['attachment_parent'] : ''; if ( isset( $header_data['width'] ) ) $header_images[$header_index]['width'] = $header_data['width'];