Make WordPress Core

Ticket #40370: 40370-refresh-at-6-4.patch

File 40370-refresh-at-6-4.patch, 7.2 KB (added by misfist, 18 months ago)

Upload patch previously submitted https://github.com/WordPress/wordpress-develop/pull/5086

  • src/wp-includes/class-wp-image-editor-gd.php

    From c117743fb7842628429ad13b333365c7a9dbbbeb Mon Sep 17 00:00:00 2001
    From: Pea <pea@misfist.com>
    Date: Thu, 24 Aug 2023 17:20:59 -0400
    Subject: [PATCH] 40370-patch
    MIME-Version: 1.0
    Content-Type: text/plain; charset=UTF-8
    Content-Transfer-Encoding: 8bit
    
    Patch applied
    40370.diff​ (4.7 KB) - added by themattroyal 5 years ago
    patching file src/wp-includes/class-wp-image-editor-gd.php
    Hunk #1 succeeded at 153 with fuzz 1 (offset 17 lines).
    Hunk #2 FAILED at 225.
    Hunk #3 FAILED at 256.
    Hunk #4 succeeded at 557 with fuzz 2 (offset 105 lines).
    2 out of 4 hunks FAILED
    ---
     src/wp-includes/class-wp-image-editor-gd.php  | 23 ++++++++
     .../class-wp-image-editor-imagick.php         | 23 ++++++++
     src/wp-includes/class-wp-image-editor.php     | 34 ++++++++++-
     tests/phpunit/tests/media.php                 | 58 +++++++++++++++++++
     4 files changed, 137 insertions(+), 1 deletion(-)
    
    diff --git a/src/wp-includes/class-wp-image-editor-gd.php b/src/wp-includes/class-wp-image-editor-gd.php
    index de079357fb..b79082c2c7 100644
    a b class WP_Image_Editor_GD extends WP_Image_Editor { 
    153153                return parent::update_size( $width, $height );
    154154        }
    155155
     156        /**
     157         * Sets or updates current image size.
     158         *
     159         * @since 6.4.0
     160         *
     161         * @param bool|array $crop
     162         * @param int $dst_w
     163         * @param int $dst_h
     164         * @return true
     165         */
     166        protected function create_crop_hash( $crop, $dst_w, $dst_h ) {
     167                if ( ! is_array( $crop ) ) {
     168                        return false;
     169                }
     170
     171                $str = $crop[0] . $crop[1] . $dst_w . $dst_h;
     172                $hash = substr( md5( $str ), 0, 8 );
     173               
     174                return parent::update_crop_hash( $hash );
     175        }
     176
    156177        /**
    157178         * Resizes current image.
    158179         *
    class WP_Image_Editor_GD extends WP_Image_Editor { 
    220241                imagecopyresampled( $resized, $this->image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
    221242
    222243                if ( is_gd_image( $resized ) ) {
     244                        $this->create_crop_hash( $crop, $dst_w, $dst_h );
    223245                        $this->update_size( $dst_w, $dst_h );
    224246                        return $resized;
    225247                }
    class WP_Image_Editor_GD extends WP_Image_Editor { 
    534556                        'file'      => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
    535557                        'width'     => $this->size['width'],
    536558                        'height'    => $this->size['height'],
     559                        'hash'      => $this->hash,
    537560                        'mime-type' => $mime_type,
    538561                        'filesize'  => wp_filesize( $filename ),
    539562                );
  • src/wp-includes/class-wp-image-editor-imagick.php

    diff --git a/src/wp-includes/class-wp-image-editor-imagick.php b/src/wp-includes/class-wp-image-editor-imagick.php
    index 7229c903b4..76ecdd6d26 100644
    a b class WP_Image_Editor_Imagick extends WP_Image_Editor { 
    309309                }
    310310        }
    311311
     312        /**
     313         * Sets or updates current image size.
     314         *
     315         * @since 6.4.0
     316         *
     317         * @param bool|array $crop
     318         * @param int $dst_w
     319         * @param int $dst_h
     320         * @return true
     321         */
     322        protected function create_crop_hash( $crop, $dst_w, $dst_h ) {
     323                if ( ! is_array( $crop ) ) {
     324                        return false;
     325                }
     326
     327                $str = $crop[0] . $crop[1] . $dst_w . $dst_h;
     328                $hash = substr( md5( $str ), 0, 8 );
     329               
     330                return parent::update_crop_hash( $hash );
     331        }
     332
    312333        /**
    313334         * Resizes current image.
    314335         *
    class WP_Image_Editor_Imagick extends WP_Image_Editor { 
    343364                list( $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ) = $dims;
    344365
    345366                if ( $crop ) {
     367                        $this->create_crop_hash( $crop, $dst_w, $dst_h );
    346368                        return $this->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h );
    347369                }
    348370
    class WP_Image_Editor_Imagick extends WP_Image_Editor { 
    837859                        'file'      => wp_basename( apply_filters( 'image_make_intermediate_size', $filename ) ),
    838860                        'width'     => $this->size['width'],
    839861                        'height'    => $this->size['height'],
     862                        'hash'      => $this->hash,
    840863                        'mime-type' => $mime_type,
    841864                        'filesize'  => wp_filesize( $filename ),
    842865                );
  • src/wp-includes/class-wp-image-editor.php

    diff --git a/src/wp-includes/class-wp-image-editor.php b/src/wp-includes/class-wp-image-editor.php
    index bb15a03a07..9ad97bbb2f 100644
    a b  
    1515abstract class WP_Image_Editor {
    1616        protected $file              = null;
    1717        protected $size              = null;
     18        protected $hash              = null;
    1819        protected $mime_type         = null;
    1920        protected $output_mime_type  = null;
    2021        protected $default_mime_type = 'image/jpeg';
    abstract class WP_Image_Editor { 
    221222                return true;
    222223        }
    223224
     225
     226        /**
     227         * Gets current image hash (when crop position has been set).
     228         *
     229         * @since 5.1.0
     230         *
     231         * @return string $hash 8 character hash
     232         */
     233        public function get_crop_hash() {
     234                return $this->hash;
     235        }
     236
     237        /**
     238         * Sets current image hash (when crop position has been set).
     239         *
     240         * @since 5.1.0
     241         *
     242         * @param string $hash
     243         * @return true
     244         */
     245        protected function update_crop_hash( $hash = null ) {
     246                $this->hash = $hash;
     247                return true;
     248        }
     249
    224250        /**
    225251         * Gets the Image Compression quality on a 1-100% scale.
    226252         *
    abstract class WP_Image_Editor { 
    486512                        return false;
    487513                }
    488514
    489                 return "{$this->size['width']}x{$this->size['height']}";
     515                $suffix = "{$this->size['width']}x{$this->size['height']}";
     516
     517                if( $this->get_crop_hash() ){
     518                        $suffix = $suffix . "-{$this->hash}";
     519                }
     520               
     521                return $suffix;
    490522        }
    491523
    492524        /**
  • tests/phpunit/tests/media.php

    diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php
    index 54ac0b71b5..5ceec7f98a 100644
    a b VIDEO; 
    13631363                $this->assertSame( 'This is a test', $post->post_title );
    13641364        }
    13651365
     1366        /**
     1367         * @ticket 40370
     1368         */
     1369        public function test_media_handle_upload_add_image_size() {
     1370                global $_wp_additional_image_sizes;
     1371
     1372                $iptc_file = DIR_TESTDATA . '/images/test-image-iptc.jpg';
     1373
     1374                // Make a copy of this file as it gets moved during the file upload
     1375                $tmp_name = wp_tempnam( $iptc_file );
     1376
     1377                copy( $iptc_file, $tmp_name );
     1378
     1379                // FIXME : change to correct upload method that creates thumbnails
     1380                // and resuts in the new filenames with hashes
     1381                $_FILES['async-upload'] = array(
     1382                        'tmp_name' => $tmp_name,
     1383                        'name'     => 'test-image-iptc.jpg',
     1384                        'type'     => 'image/jpeg',
     1385                        'error'    => 0,
     1386                        'size'     => filesize( $iptc_file ),
     1387                );
     1388
     1389                // add multiple image sizes
     1390                add_image_size( 'newscentered', 400, 400, array( 'center', 'center') );
     1391                add_image_size( 'newstop', 400, 400, array( 'center', 'top' ) );
     1392                add_image_size( 'newsbottom', 400, 400, array( 'center', 'bottom' ) );
     1393
     1394                $info = wp_upload_dir();
     1395
     1396                $orig = array_map('basename', glob( ABSPATH . 'wp-content/uploads'
     1397                                        . $info['subdir'] .'/*.png'));
     1398
     1399                // upload file
     1400                $post_id = media_handle_upload(
     1401                        'async-upload',
     1402                        0,
     1403                        array(),
     1404                        array(
     1405                                'action'    => 'test_iptc_upload',
     1406                                'test_form' => false,
     1407                        )
     1408                );
     1409
     1410                unset( $_FILES['upload'] );
     1411
     1412                // Clean up.
     1413                wp_delete_attachment( $post_id );
     1414
     1415                // 3 new files should be created
     1416                $new = array_map('basename', glob( ABSPATH . 'wp-content/uploads'
     1417                                        . $info['subdir'] .'/*.{jpg, png}', GLOB_BRACE));
     1418                $new_files = array_diff($new, $orig);
     1419
     1420                $this->assertCount(3, $new_files );
     1421               
     1422        }
     1423
    13661424        /**
    13671425         * @ticket 33016
    13681426         */