Make WordPress Core

Changeset 55935


Ignore:
Timestamp:
06/18/2023 02:22:40 PM (21 months ago)
Author:
azaozz
Message:

Media: Deprecate the 'edit_custom_thumbnail_sizes' filter and disable the "Apply changes to [Thumbnail|All|All except thumbnail]" UI in the image editor. Add a (boolean) filter to reenable that UI.

Props peterwilsoncc, costdev, azaozz.
See: #57685.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/js/_enqueues/lib/image-edit.js

    r55919 r55935  
    359359     *
    360360     * @return {string} The value from the imagedit-save-target input field when available,
    361      *                  or 'full' when not available.
    362      */
    363     getTarget : function(postid) {
    364         return $('input[name="imgedit-target-' + postid + '"]:checked', '#imgedit-save-target-' + postid).val() || 'full';
     361     *                  'full' when not selected, or 'all' if it doesn't exist.
     362     */
     363    getTarget : function( postid ) {
     364        var element = $( '#imgedit-save-target-' + postid );
     365
     366        if ( element.length ) {
     367            return element.find( 'input[name="imgedit-target-' + postid + '"]:checked' ).val() || 'full';
     368        }
     369
     370        return 'all';
    365371    },
    366372
  • trunk/src/wp-admin/includes/image-edit.php

    r55919 r55935  
    4444        }
    4545    }
    46     $edit_custom_sizes = false;
     46
    4747    /**
    48      * Filters whether custom sizes are available options for image editing.
     48     * Shows the settings in the Image Editor that allow selecting to edit only the thumbnail of an image.
    4949     *
    50      * @since 6.0.0
     50     * @since 6.3.0
    5151     *
    52      * @param bool|string[] $edit_custom_sizes True if custom sizes can be edited or array of custom size names.
     52     * @param bool Whether to show the settings in the Image Editor. Default false.
    5353     */
    54     $edit_custom_sizes = apply_filters( 'edit_custom_thumbnail_sizes', $edit_custom_sizes );
     54    $edit_thumbnails_separately = (bool) apply_filters( 'image_edit_thumbnails_separately', false );
     55
    5556    ?>
    5657    <div class="imgedit-wrap wp-clearfix">
     
    273274
    274275    <?php
    275     if ( $thumb && $sub_sizes ) {
     276    if ( $edit_thumbnails_separately && $thumb && $sub_sizes ) {
    276277        $thumb_img = wp_constrain_dimensions( $thumb['width'], $thumb['height'], 160, 120 );
    277278        ?>
     
    308309                    <label for="imgedit-target-thumbnail"><?php _e( 'Thumbnail' ); ?></label>
    309310                </span>
    310  
     311
    311312                <span class="imgedit-label">
    312313                    <input type="radio" id="imgedit-target-nothumb" name="imgedit-target-<?php echo $post_id; ?>" value="nothumb" />
     
    314315                </span>
    315316
    316                 <?php
    317                 if ( $edit_custom_sizes ) {
    318                     if ( ! is_array( $edit_custom_sizes ) ) {
    319                         $edit_custom_sizes = get_intermediate_image_sizes();
    320                     }
    321                     foreach ( array_unique( $edit_custom_sizes ) as $key => $size ) {
    322                         if ( array_key_exists( $size, $meta['sizes'] ) ) {
    323                             if ( 'thumbnail' === $size ) {
    324                                 continue;
    325                             }
    326                             ?>
    327                             <span class="imgedit-label">
    328                             <input type="radio" id="imgedit-target-custom<?php echo esc_attr( $key ); ?>" name="imgedit-target-<?php echo $post_id; ?>" value="<?php echo esc_attr( $size ); ?>" />
    329                                 <label for="imgedit-target-custom<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $size ); ?></label>
    330                             </span>
    331                             <?php
    332                         }
    333                     }
    334                 }
    335                 ?>
    336317                </fieldset>
    337318            </div>
     
    919900    $scale   = ! empty( $_REQUEST['do'] ) && 'scale' === $_REQUEST['do'];
    920901
     902    /** This filter is documented in wp-admin/includes/image-edit.php */
     903    $edit_thumbnails_separately = (bool) apply_filters( 'image_edit_thumbnails_separately', false );
     904
    921905    if ( $scale ) {
    922906        $size = $img->get_size();
     
    978962        isset( $backup_sizes['full-orig'] ) && $backup_sizes['full-orig']['file'] != $basename ) {
    979963
    980         if ( 'thumbnail' === $target ) {
     964        if ( $edit_thumbnails_separately && 'thumbnail' === $target ) {
    981965            $new_path = "{$dirname}/{$filename}-temp.{$ext}";
    982966        } else {
     
    10281012        $meta['height'] = $size['height'];
    10291013
    1030         if ( $success ) {
     1014        if ( $success && ( 'nothumb' === $target || 'all' === $target ) ) {
    10311015            $sizes = get_intermediate_image_sizes();
    1032             if ( 'nothumb' === $target || 'all' === $target ) {
    1033                 if ( 'nothumb' === $target ) {
    1034                     $sizes = array_diff( $sizes, array( 'thumbnail' ) );
    1035                 }
    1036             } elseif ( 'thumbnail' !== $target ) {
    1037                 $sizes = array_diff( $sizes, array( $target ) );
     1016
     1017            if ( $edit_thumbnails_separately && 'nothumb' === $target ) {
     1018                $sizes = array_diff( $sizes, array( 'thumbnail' ) );
    10381019            }
    10391020        }
     
    10411022        $return->fw = $meta['width'];
    10421023        $return->fh = $meta['height'];
    1043     } elseif ( 'thumbnail' === $target ) {
     1024    } elseif ( $edit_thumbnails_separately && 'thumbnail' === $target ) {
    10441025        $sizes   = array( 'thumbnail' );
    10451026        $success = true;
    10461027        $delete  = true;
    10471028        $nocrop  = true;
    1048     } else {
    1049         $sizes   = array( $target );
    1050         $success = true;
    1051         $delete  = true;
    1052         $nocrop  = $_wp_additional_image_sizes[ $size ]['crop'];
    10531029    }
    10541030
  • trunk/tests/phpunit/tests/ajax/wpAjaxImageEditor.php

    r55859 r55935  
    2727        $this->remove_added_uploads();
    2828        parent::tear_down();
    29     }
    30 
    31     /**
    32      * @ticket 22985
    33      * @requires function imagejpeg
    34      *
    35      * @covers ::wp_insert_attachment
    36      * @covers ::wp_save_image
    37      */
    38     public function testCropImageThumbnail() {
    39         require_once ABSPATH . 'wp-admin/includes/image-edit.php';
    40 
    41         $filename = DIR_TESTDATA . '/images/canola.jpg';
    42         $contents = file_get_contents( $filename );
    43 
    44         $upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
    45         $id     = $this->_make_attachment( $upload );
    46 
    47         $_REQUEST['action']  = 'image-editor';
    48         $_REQUEST['context'] = 'edit-attachment';
    49         $_REQUEST['postid']  = $id;
    50         $_REQUEST['target']  = 'thumbnail';
    51         $_REQUEST['do']      = 'save';
    52         $_REQUEST['history'] = '[{"c":{"x":5,"y":8,"w":289,"h":322}}]';
    53 
    54         $media_meta = wp_get_attachment_metadata( $id );
    55         $this->assertArrayHasKey( 'sizes', $media_meta, 'attachment should have size data' );
    56         $this->assertArrayHasKey( 'medium', $media_meta['sizes'], 'attachment should have data for medium size' );
    57         $ret = wp_save_image( $id );
    58 
    59         $media_meta = wp_get_attachment_metadata( $id );
    60         $this->assertArrayHasKey( 'sizes', $media_meta, 'cropped attachment should have size data' );
    61         $this->assertArrayHasKey( 'medium', $media_meta['sizes'], 'cropped attachment should have data for medium size' );
    6229    }
    6330
  • trunk/tests/phpunit/tests/image/functions.php

    r55070 r55935  
    349349
    350350        $this->assertTrue( $ret, 'Image failed to save.' );
     351    }
     352
     353    /**
     354     * Tests that `wp_image_editor()` applies 'image_edit_thumbnails_separately' filters.
     355     *
     356     * @ticket 53161
     357     *
     358     * @covers ::wp_image_editor
     359     */
     360    public function test_wp_image_editor_should_apply_image_edit_thumbnails_separately_filters() {
     361        require_once ABSPATH . 'wp-admin/includes/image-edit.php';
     362
     363        $filename = DIR_TESTDATA . '/images/canola.jpg';
     364        $contents = file_get_contents( $filename );
     365        $upload   = wp_upload_bits( wp_basename( $filename ), null, $contents );
     366        $id       = $this->_make_attachment( $upload );
     367
     368        $filter = new MockAction();
     369        add_filter( 'image_edit_thumbnails_separately', array( &$filter, 'filter' ) );
     370
     371        ob_start();
     372        wp_image_editor( $id );
     373        ob_end_clean();
     374
     375        $this->assertSame( 1, $filter->get_call_count() );
     376    }
     377
     378    /**
     379     * Tests that `wp_image_editor()` conditionally outputs markup for editing thumbnails separately
     380     * based on the result of applying 'image_edit_thumbnails_separately' filters.
     381     *
     382     * @ticket 53161
     383     *
     384     * @covers ::wp_image_editor
     385     *
     386     * @dataProvider data_wp_image_editor_should_respect_image_edit_thumbnails_separately_filters
     387     *
     388     * @param string $callback The name of the callback for the 'image_edit_thumbnails_separately' hook.
     389     * @param bool   $expected Whether the markup should be output.
     390     */
     391    public function test_wp_image_editor_should_respect_image_edit_thumbnails_separately_filters( $callback, $expected ) {
     392        require_once ABSPATH . 'wp-admin/includes/image-edit.php';
     393
     394        $filename = DIR_TESTDATA . '/images/canola.jpg';
     395        $contents = file_get_contents( $filename );
     396        $upload   = wp_upload_bits( wp_basename( $filename ), null, $contents );
     397        $id       = $this->_make_attachment( $upload );
     398
     399        add_filter( 'image_edit_thumbnails_separately', $callback );
     400
     401        ob_start();
     402        wp_image_editor( $id );
     403        $actual = ob_get_clean();
     404
     405        if ( $expected ) {
     406            $this->assertStringContainsString(
     407                'imgedit-applyto',
     408                $actual,
     409                'The markup should have been output.'
     410            );
     411        } else {
     412            $this->assertStringNotContainsString(
     413                'imgedit-applyto',
     414                $actual,
     415                'The markup should not have been output.'
     416            );
     417        }
     418    }
     419
     420    /**
     421     * Data provider.
     422     *
     423     * @return array[]
     424     */
     425    public function data_wp_image_editor_should_respect_image_edit_thumbnails_separately_filters() {
     426        return array(
     427            'true'  => array(
     428                'callback' => '__return_true',
     429                'expected' => true,
     430            ),
     431            'false' => array(
     432                'callback' => '__return_false',
     433                'expected' => false,
     434            ),
     435        );
    351436    }
    352437
Note: See TracChangeset for help on using the changeset viewer.