[wp-trac] [WordPress Trac] #34981: Usage of `image_size_names_choose` breaks JS attachment model attributes

WordPress Trac noreply at wordpress.org
Thu Dec 10 16:31:33 UTC 2015


#34981: Usage of `image_size_names_choose` breaks JS attachment model attributes
--------------------------+-----------------------------
 Reporter:  dnaber-de     |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Media         |    Version:  4.3.1
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 Here's a small use case that shows how the issue arise.

 I want to limit the options of image sizes in the default media modal to
 so I use the filter `image_size_names_choose` provided in `wp-
 admin/includes/media.php` and remove the sizes `thumbnail` and as well as
 `medium`. This brings me the expected result:

 [[Image(https://naber.pegasus.uberspace.de/fs/public/images/2015/limit-
 attachment-sizes.png)]]

 With this I have now the problem, that for every thumbnail image in the
 media library (mode: grid) the large image source is used which is a
 performance issue if there are more than a couple of images in the
 library.

 The reason is, that the same filter `image_size_names_choose` is also
 applied to each image in `wp_prepare_attachment_for_js()` and therefore
 the attachment model does not reflect all available image sizes
 (`console.log( attachment.sizes )`:

 [[Image(https://naber.pegasus.uberspace.de/fs/public/images/2015
 /attachment-model-sizes.png)]]

 Here's a plugin that reproduces the issue in a clean WordPress (4.4)
 install:

 `wp-content/plugins/image_sizes_issue/image_sizes_issue.php`:

 {{{#!php
 <?php
 /**
  * Plugin Name: Image sizes issue
  */

 add_filter(
         'image_size_names_choose',
         function( $sizes ) {
                 unset( $sizes[ 'medium' ] );
                 unset( $sizes[ 'thumbnail' ] );

                 return $sizes;
         }
 );
 add_action( 'admin_enqueue_scripts', function() {

         wp_enqueue_media();
         wp_enqueue_script(
                 'image_size_test',
                 plugins_url( '/image_sizes_issue.js', __FILE__ ),
                 [ 'jquery' ]
         );
 } );

 }}}


 `wp-content/plugins/image_sizes_issue/image_sizes_issue.js`:

 {{{
 ( function( $, wp ) {
         $( function()  {
                 var image   = wp.media.model.Attachment.get( '4' );
                 var promise = image.sync( 'read', image );
                 promise.then( function( results ) {
                         console.log( results.sizes );
                 } );
          } );
 } )(
         jQuery,
         window.wp = window.wp || {}
 );

 }}}

 For what I see, the filter `image_size_names_choose` is meant to affect
 the default UI for customization purpose. At least the name of the filter
 and the name of the function `image_size_input_fields()` (introduced in
 2.7) suggests that.

 In contrast to that, `wp_prepare_attachment_for_js()` prepares the data
 structure for the Javascript API. I'm sure there's some reason to apply
 the filter there, but I have no idea what this reason could be.

 A possible, backward compatible solution could be, to pass a second
 argument to the filter to specify the context, the filter is applied in.

 I detected this behaviour in 4.3.1 but I think it go back to 3.5 when
 `wp_prepare_attachments_for_js()` was introduced.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/34981>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list