Plugin Directory

Changeset 3686771


Ignore:
Timestamp:
09/08/2026 01:58:24 PM (3 months ago)
Author:
RistoNiinemets
Message:

Release 2.2.0

Location:
ajax-thumbnail-rebuild
Files:
16 edited
1 copied

Legend:

Unmodified
Added
Removed
  • ajax-thumbnail-rebuild/tags/2.2.0/ajax-thumbnail-rebuild.php

    r3681218 r3686771  
    44 * Plugin URI: https://wordpress.org/plugins/ajax-thumbnail-rebuild/
    55 * Author: ristoniinemets, junkcoder
    6  * Version: 2.1.0
     6 * Version: 2.2.0
    77 * Description: Rebuild the thumbnails of your media library one image at a time, without running into script timeouts.
    88 * Requires at least: 5.6
    … …  
    1919}
    2020
    21 define( 'ATR_VERSION', '2.1.0' );
     21define( 'ATR_VERSION', '2.2.0' );
    2222define( 'ATR_FILE', __FILE__ );
    2323define( 'ATR_PATH', plugin_dir_path( __FILE__ ) );
  • ajax-thumbnail-rebuild/tags/2.2.0/assets/js/admin.js

    r3663080 r3686771  
    2929                var skippedList = document.getElementById( 'atr-skipped' );
    3030                var selectAll = document.getElementById( 'atr-size-select-all' );
    31                 // Sizes marked as on demand are shown but disabled: they are never rebuilt here.
    32                 var sizeBoxes = Array.prototype.slice.call( document.querySelectorAll( '.atr-size:not([disabled])' ) );
     31                var sizeBoxes = Array.prototype.slice.call( document.querySelectorAll( '.atr-size' ) );
    3332
    3433                var stopped = false;
  • ajax-thumbnail-rebuild/tags/2.2.0/includes/class-atr-admin-page.php

    r3663080 r3686771  
    158158                                                                                                                ?>
    159159                                                                                                        </label>
    160                                                                                                         <input type="checkbox" class="atr-size" id="<?php echo esc_attr( $id ); ?>" value="<?php echo esc_attr( $size['name'] ); ?>" <?php checked( ! $waiting ); ?> <?php disabled( $waiting ); ?> />
     160                                                                                                        <input type="checkbox" class="atr-size" id="<?php echo esc_attr( $id ); ?>" value="<?php echo esc_attr( $size['name'] ); ?>" checked="checked" />
    161161                                                                                                </th>
    162162                                                                                                <td class="column-primary">
    … …  
    164164                                                                                                        <?php if ( $waiting ) : ?>
    165165                                                                                                                <span class="atr-badge"><?php esc_html_e( 'on demand', 'ajax-thumbnail-rebuild' ); ?></span>
     166                                                                                                                <p class="description"><?php esc_html_e( 'Cut the first time a page asks for it. Rebuilding redoes it for the images that already have it, and cuts it for no others.', 'ajax-thumbnail-rebuild' ); ?></p>
    166167                                                                                                        <?php endif; ?>
    167168                                                                                                </td>
  • ajax-thumbnail-rebuild/tags/2.2.0/includes/class-atr-regenerator.php

    r3663080 r3686771  
    8989                }
    9090
     91                /* Before the sizes are cut, so they are cut from a source that is already
     92                   within the limit - and before the dimensions below are read, so what is
     93                   stored describes the file as it is left. */
     94                $metadata = ATR_Uploads::enforce_limit( $attachment_id, $metadata );
     95
    9196                $imagesize = wp_getimagesize( $file );
    9297
    … …  
    112117                $source = self::source_file( $attachment_id, $file );
    113118
    114                 $on_demand = ATR_On_Demand::sizes();
     119                /* Read before the loop writes to it, so "did this image have the size" is
     120                   answered about the state the rebuild started from. */
     121                $had = $metadata['sizes'];
    115122
    116123                foreach ( ATR_Image_Sizes::all( $metadata, $attachment_id ) as $name => $size ) {
    … …  
    120127                        }
    121128
    122                         if ( in_array( $name, $on_demand, true ) ) {
    123                                 // Marked as on demand: it is cut when a page asks for it, not here.
     129                        /* A size that waits is rebuilt where the image already has it and nowhere
     130                           else. The file is as stale as any other after a size changes, so it has
     131                           to be redoable - but cutting it for an image no page has asked about
     132                           would fill the library with exactly the files this feature exists to
     133                           avoid writing. Whether it waits is asked of covers(), so the one switch
     134                           for the whole site and a single size marked on its own behave alike. */
     135                        if ( ATR_On_Demand::covers( $name ) && empty( $had[ $name ] ) ) {
    124136                                continue;
    125137                        }
  • ajax-thumbnail-rebuild/tags/2.2.0/includes/class-atr-settings.php

    r3681218 r3686771  
    2626                        'upload_quality'       => ATR_Uploads::CORE_QUALITY,
    2727                        'max_upload_dimension' => ATR_Uploads::CORE_THRESHOLD,
     28                        'shrink_original'      => false,
    2829                        'optimize_enabled'     => false,
    2930                        'optimize_quality'     => ATR_Uploads::CORE_QUALITY,
    … …  
    100101                        'uploads'   => array(
    101102                                'label' => __( 'Uploads', 'ajax-thumbnail-rebuild' ),
    102                                 'keys'  => array( 'upload_quality', 'max_upload_dimension' ),
     103                                'keys'  => array( 'upload_quality', 'max_upload_dimension', 'shrink_original' ),
    103104                        ),
    104105                        'optimise'  => array(
    … …  
    159160                );
    160161
     162                add_settings_field(
     163                        'shrink_original',
     164                        __( 'The untouched original', 'ajax-thumbnail-rebuild' ),
     165                        array( __CLASS__, 'render_shrink_original' ),
     166                        self::page_slug( 'uploads' ),
     167                        'atr_uploads'
     168                );
     169
    161170                add_settings_section(
    162171                        'atr_optimize',
    … …  
    360369                switch ( $key ) {
    361370                        // Checkboxes: absent means unticked, which is why $managed matters.
     371                        case 'shrink_original':
    362372                        case 'optimize_enabled':
    363373                        case 'background_enabled':
    … …  
    473483                        );
    474484                        ?>
     485                </p>
     486                <?php
     487        }
     488
     489        public static function render_shrink_original(): void {
     490                $settings = self::all();
     491                ?>
     492                <label>
     493                        <input type="checkbox" name="<?php echo esc_attr( self::OPTION ); ?>[shrink_original]" value="1" <?php checked( $settings['shrink_original'] ); ?> />
     494                        <?php esc_html_e( 'Bring the untouched original down to this limit as well', 'ajax-thumbnail-rebuild' ); ?>
     495                </label>
     496                <p class="description">
     497                        <?php esc_html_e( 'The original is what the sub sizes are cut from; once they are cut it only sits there, and on a photograph from a modern camera it is most of what the library weighs. With this on it is scaled to the limit like everything else - on upload, and for images already here the next time they are rebuilt.', 'ajax-thumbnail-rebuild' ); ?>
     498                </p>
     499                <p class="description">
     500                        <strong><?php esc_html_e( 'This cannot be undone: the full resolution the original was uploaded at is gone, and no size larger than the limit can be cut from it afterwards.', 'ajax-thumbnail-rebuild' ); ?></strong>
    475501                </p>
    476502                <?php
    … …  
    748774        public static function render_on_demand_sizes(): void {
    749775                $settings = self::all();
    750                 $marked   = (array) $settings['on_demand_sizes'];
     776                $stored   = (array) $settings['on_demand_sizes'];
    751777                $sizes    = ATR_Image_Sizes::all();
     778
     779                /* What the site actually treats as on demand, which is the stored list
     780                   plus whatever ajax_thumbnail_rebuild_on_demand_sizes adds. A theme
     781                   declaring its own sizes in code is the case this is for: the size
     782                   and the template that asks for it then live in the same commit. */
     783                $effective = ATR_On_Demand::sizes();
     784
     785                /* Declared in code rather than here. Ticking one of these off would do
     786                   nothing - the filter puts it straight back on the next read - so the
     787                   box says what is true and does not pretend to be editable. */
     788                $declared = array_diff( $effective, $stored );
    752789                ?>
    753790                <fieldset>
    … …  
    756793                                <p class="description"><?php esc_html_e( 'This site has no image sizes registered.', 'ajax-thumbnail-rebuild' ); ?></p>
    757794                        <?php endif; ?>
    758                         <?php foreach ( $sizes as $size ) : ?>
     795                        <?php
     796                        foreach ( $sizes as $size ) :
     797                                $is_declared = in_array( $size['name'], $declared, true );
     798                                ?>
    759799                                <label style="display:block; margin-bottom:4px;">
    760                                         <input type="checkbox" name="<?php echo esc_attr( self::OPTION ); ?>[on_demand_sizes][]" value="<?php echo esc_attr( $size['name'] ); ?>" <?php checked( in_array( $size['name'], $marked, true ) ); ?> />
     800                                        <?php if ( $is_declared ) : ?>
     801                                                <?php /* No name attribute: a disabled box posts nothing anyway, and one
     802                                                         that did would write a filter's decision into the option. */ ?>
     803                                                <input type="checkbox" value="<?php echo esc_attr( $size['name'] ); ?>" checked disabled />
     804                                        <?php else : ?>
     805                                                <input type="checkbox" name="<?php echo esc_attr( self::OPTION ); ?>[on_demand_sizes][]" value="<?php echo esc_attr( $size['name'] ); ?>" <?php checked( in_array( $size['name'], $stored, true ) ); ?> />
     806                                        <?php endif; ?>
    761807                                        <code><?php echo esc_html( $size['name'] ); ?></code>
    762808                                        <span class="description"><?php echo esc_html( ATR_Image_Sizes::dimensions_label( $size ) ); ?></span>
     809                                        <?php if ( $is_declared ) : ?>
     810                                                <span class="description"><?php esc_html_e( '- declared in code', 'ajax-thumbnail-rebuild' ); ?></span>
     811                                        <?php endif; ?>
    763812                                </label>
    764813                        <?php endforeach; ?>
    … …  
    767816                        <?php esc_html_e( 'A size ticked here is never written on upload and is skipped by a rebuild, whatever the Rebuild tab has selected. It is cut the first time a page asks for it. Useful for the big sizes only a handful of pages ever use.', 'ajax-thumbnail-rebuild' ); ?>
    768817                </p>
     818                <?php if ( $declared ) : ?>
     819                        <p class="description">
     820                                <?php esc_html_e( 'The greyed-out ones are set by a theme or plugin through the ajax_thumbnail_rebuild_on_demand_sizes filter and cannot be changed here.', 'ajax-thumbnail-rebuild' ); ?>
     821                        </p>
     822                <?php endif; ?>
    769823                <?php
    770824        }
  • ajax-thumbnail-rebuild/tags/2.2.0/includes/class-atr-uploads.php

    r3663080 r3686771  
    2525                add_filter( 'wp_editor_set_quality', array( $this, 'filter_quality' ), 10, 2 );
    2626                add_filter( 'big_image_size_threshold', array( $this, 'filter_threshold' ) );
     27
     28                /* After core has cut the sub sizes from the original, and before the
     29                   optimiser at 15, so what that optimises is the file we leave behind
     30                   rather than one we are about to write over. */
     31                add_filter( 'wp_generate_attachment_metadata', array( $this, 'enforce_limit_after_generate' ), 12, 2 );
    2732        }
    2833
    … …  
    6166                return $setting > 0 ? $setting : (int) $threshold;
    6267        }
     68
     69        /**
     70         * Bring every file of an upload down to the limit, once the sizes are cut.
     71         *
     72         * @param array $metadata      Attachment metadata.
     73         * @param int   $attachment_id Attachment id.
     74         * @return array
     75         */
     76        public function enforce_limit_after_generate( $metadata, $attachment_id ) {
     77                if ( ! is_array( $metadata ) ) {
     78                        return $metadata;
     79                }
     80
     81                return self::enforce_limit( (int) $attachment_id, $metadata );
     82        }
     83
     84        /**
     85         * No file of this attachment may be longer than the limit on its long edge.
     86         *
     87         * The sub sizes are cut from the original, so this runs after they are made
     88         * and never before. What is left afterwards is an original at the limit -
     89         * the full resolution it arrived at is gone, which is what the setting says
     90         * it does.
     91         *
     92         * On an image already in the library the "-scaled" copy was written to the
     93         * old limit and is now larger than the original beside it, so it comes down
     94         * too. It keeps its name: the URL is in the content of pages already.
     95         *
     96         * @param int   $attachment_id Attachment to work on.
     97         * @param array $metadata      Metadata to update.
     98         * @return array Metadata, with the served file's dimensions kept honest.
     99         */
     100        public static function enforce_limit( int $attachment_id, array $metadata ): array {
     101                if ( ! ATR_Settings::get( 'shrink_original' ) ) {
     102                        return $metadata;
     103                }
     104
     105                $limit = (int) ATR_Settings::get( 'max_upload_dimension', self::CORE_THRESHOLD );
     106
     107                if ( $limit <= 0 || ! wp_attachment_is_image( $attachment_id ) ) {
     108                        return $metadata;
     109                }
     110
     111                $file = get_attached_file( $attachment_id );
     112
     113                if ( ! $file || ! file_exists( $file ) ) {
     114                        return $metadata;
     115                }
     116
     117                $directory = trailingslashit( dirname( $file ) );
     118                $original  = empty( $metadata['original_image'] )
     119                        ? $file
     120                        : $directory . wp_basename( (string) $metadata['original_image'] );
     121
     122                $written = self::shrink_to( $original, $limit );
     123
     124                /* The served copy, when it is a file of its own. On a fresh upload core
     125                   wrote it to this same limit already and there is nothing to do. */
     126                if ( $original !== $file ) {
     127                        $written = self::shrink_to( $file, $limit ) || $written;
     128                }
     129
     130                if ( ! $written ) {
     131                        return $metadata;
     132                }
     133
     134                $imagesize = wp_getimagesize( $file );
     135
     136                if ( $imagesize ) {
     137                        $metadata['width']  = (int) $imagesize[0];
     138                        $metadata['height'] = (int) $imagesize[1];
     139                }
     140
     141                if ( function_exists( 'wp_filesize' ) ) {
     142                        $metadata['filesize'] = wp_filesize( $file );
     143                }
     144
     145                // Freshly written files: whatever optimising was done to the old ones is gone.
     146                unset( $metadata[ ATR_Optimizer::DONE_KEY ] );
     147
     148                return $metadata;
     149        }
     150
     151        /**
     152         * Scale one file down to the limit, in place and under its own name.
     153         *
     154         * Written beside the file and moved over it only once it is there, so a
     155         * failed resize cannot damage what the site is already serving.
     156         *
     157         * @param string $path  Absolute path.
     158         * @param int    $limit Longest edge allowed.
     159         * @return bool Whether the file was replaced.
     160         */
     161        private static function shrink_to( string $path, int $limit ): bool {
     162                if ( ! file_exists( $path ) || ! is_writable( $path ) ) {
     163                        return false;
     164                }
     165
     166                $imagesize = wp_getimagesize( $path );
     167
     168                if ( ! $imagesize || ( (int) $imagesize[0] <= $limit && (int) $imagesize[1] <= $limit ) ) {
     169                        return false;
     170                }
     171
     172                $editor = wp_get_image_editor( $path );
     173
     174                if ( is_wp_error( $editor ) ) {
     175                        return false;
     176                }
     177
     178                if ( is_wp_error( $editor->resize( $limit, $limit ) ) ) {
     179                        return false;
     180                }
     181
     182                $mime  = (string) wp_get_image_mime( $path );
     183                $saved = $editor->save( self::temporary_path( $path ), $mime ? $mime : null );
     184
     185                if ( is_wp_error( $saved ) || empty( $saved['path'] ) || ! file_exists( $saved['path'] ) ) {
     186                        return false;
     187                }
     188
     189                if ( ! rename( $saved['path'], $path ) ) {
     190                        wp_delete_file( $saved['path'] );
     191
     192                        return false;
     193                }
     194
     195                clearstatcache( true, $path );
     196
     197                return true;
     198        }
     199
     200        /**
     201         * Somewhere to write the smaller file while it is being made.
     202         *
     203         * @param string $path File it is made from.
     204         * @return string
     205         */
     206        private static function temporary_path( string $path ): string {
     207                $extension = pathinfo( $path, PATHINFO_EXTENSION );
     208
     209                return preg_replace( '/\.[^.]+$/', '', $path ) . '-atr-shrinking' . ( $extension ? '.' . $extension : '' );
     210        }
    63211}
  • ajax-thumbnail-rebuild/tags/2.2.0/languages/ajax-thumbnail-rebuild.pot

    r3681218 r3686771  
    446446msgstr ""
    447447
     448#: includes/class-atr-settings.php:164
    448449#: includes/class-atr-settings.php:203
    449450msgid "The untouched original"
    … …  
    515516#, php-format
    516517msgid "An upload longer than this on its longest edge is scaled down to it, and the untouched original is kept beside it. WordPress uses %s. Set 0 to keep every upload at the size it arrived."
     518msgstr ""
     519
     520#: includes/class-atr-settings.php:494
     521msgid "Bring the untouched original down to this limit as well"
     522msgstr ""
     523
     524#: includes/class-atr-settings.php:497
     525msgid "The original is what the sub sizes are cut from; once they are cut it only sits there, and on a photograph from a modern camera it is most of what the library weighs. With this on it is scaled to the limit like everything else - on upload, and for images already here the next time they are rebuilt."
     526msgstr ""
     527
     528#: includes/class-atr-settings.php:500
     529msgid "This cannot be undone: the full resolution the original was uploaded at is gone, and no size larger than the limit can be cut from it afterwards."
    517530msgstr ""
    518531
  • ajax-thumbnail-rebuild/tags/2.2.0/readme.txt

    r3681218 r3686771  
    66Requires PHP: 7.4
    77Tested up to: 7.1
    8 Stable tag: 2.1.0
     8Stable tag: 2.2.0
    99License: GPLv2 or later
    1010License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    9090
    9191== Changelog ==
     92
     93= 2.2.0 =
     94
     95* New setting **The untouched original**, under Uploads beside the limit: the full size original kept beside a scaled upload is brought down to the limit as well. WordPress cuts the sub sizes from that file and then leaves it alone at whatever it arrived as, which on a photograph from a modern camera is most of what the library weighs. Off by default, and it cannot be undone - the resolution the original was uploaded at is gone, and no size larger than the limit can be cut from it afterwards. New uploads are covered as they arrive; an image already in the library is covered the next time it is rebuilt, and its `-scaled` copy comes down with it where that was written to an older limit.
     96* A size marked as made on demand can be rebuilt again. It was shown ticked off and disabled on the rebuild screen, so a file cut for it stayed as it was however the size changed. It is now rebuilt for the images that already have it and cut for no others, which refreshes what is there without writing the files the setting exists to avoid.
     97
     98= 2.1.1 =
     99
     100* Sizes a theme or plugin adds through `ajax_thumbnail_rebuild_on_demand_sizes` now show up ticked under **Made on demand** instead of looking switched off, and are greyed out there: unticking one never had any effect, because the filter put it back on the next read.
    92101
    93102= 2.1.0 =
    … …  
    271280== Upgrade Notice ==
    272281
     282= 2.2.0 =
     283
     284The untouched original beside a scaled upload can now be brought down to the size limit as well, which is where most of a library's weight usually sits. Off by default; it cannot be undone. Sizes made on demand can also be rebuilt now, for the images that already have them.
     285
     286= 2.1.1 =
     287
     288The Made on demand list now shows the sizes a theme sets in code, ticked and greyed out, rather than leaving them looking switched off. Display only; nothing about which sizes wait has changed.
     289
    273290= 2.1.0 =
    274291
  • ajax-thumbnail-rebuild/trunk/ajax-thumbnail-rebuild.php

    r3681218 r3686771  
    44 * Plugin URI: https://wordpress.org/plugins/ajax-thumbnail-rebuild/
    55 * Author: ristoniinemets, junkcoder
    6  * Version: 2.1.0
     6 * Version: 2.2.0
    77 * Description: Rebuild the thumbnails of your media library one image at a time, without running into script timeouts.
    88 * Requires at least: 5.6
    … …  
    1919}
    2020
    21 define( 'ATR_VERSION', '2.1.0' );
     21define( 'ATR_VERSION', '2.2.0' );
    2222define( 'ATR_FILE', __FILE__ );
    2323define( 'ATR_PATH', plugin_dir_path( __FILE__ ) );
  • ajax-thumbnail-rebuild/trunk/assets/js/admin.js

    r3663080 r3686771  
    2929                var skippedList = document.getElementById( 'atr-skipped' );
    3030                var selectAll = document.getElementById( 'atr-size-select-all' );
    31                 // Sizes marked as on demand are shown but disabled: they are never rebuilt here.
    32                 var sizeBoxes = Array.prototype.slice.call( document.querySelectorAll( '.atr-size:not([disabled])' ) );
     31                var sizeBoxes = Array.prototype.slice.call( document.querySelectorAll( '.atr-size' ) );
    3332
    3433                var stopped = false;
  • ajax-thumbnail-rebuild/trunk/includes/class-atr-admin-page.php

    r3663080 r3686771  
    158158                                                                                                                ?>
    159159                                                                                                        </label>
    160                                                                                                         <input type="checkbox" class="atr-size" id="<?php echo esc_attr( $id ); ?>" value="<?php echo esc_attr( $size['name'] ); ?>" <?php checked( ! $waiting ); ?> <?php disabled( $waiting ); ?> />
     160                                                                                                        <input type="checkbox" class="atr-size" id="<?php echo esc_attr( $id ); ?>" value="<?php echo esc_attr( $size['name'] ); ?>" checked="checked" />
    161161                                                                                                </th>
    162162                                                                                                <td class="column-primary">
    … …  
    164164                                                                                                        <?php if ( $waiting ) : ?>
    165165                                                                                                                <span class="atr-badge"><?php esc_html_e( 'on demand', 'ajax-thumbnail-rebuild' ); ?></span>
     166                                                                                                                <p class="description"><?php esc_html_e( 'Cut the first time a page asks for it. Rebuilding redoes it for the images that already have it, and cuts it for no others.', 'ajax-thumbnail-rebuild' ); ?></p>
    166167                                                                                                        <?php endif; ?>
    167168                                                                                                </td>
  • ajax-thumbnail-rebuild/trunk/includes/class-atr-regenerator.php

    r3663080 r3686771  
    8989                }
    9090
     91                /* Before the sizes are cut, so they are cut from a source that is already
     92                   within the limit - and before the dimensions below are read, so what is
     93                   stored describes the file as it is left. */
     94                $metadata = ATR_Uploads::enforce_limit( $attachment_id, $metadata );
     95
    9196                $imagesize = wp_getimagesize( $file );
    9297
    … …  
    112117                $source = self::source_file( $attachment_id, $file );
    113118
    114                 $on_demand = ATR_On_Demand::sizes();
     119                /* Read before the loop writes to it, so "did this image have the size" is
     120                   answered about the state the rebuild started from. */
     121                $had = $metadata['sizes'];
    115122
    116123                foreach ( ATR_Image_Sizes::all( $metadata, $attachment_id ) as $name => $size ) {
    … …  
    120127                        }
    121128
    122                         if ( in_array( $name, $on_demand, true ) ) {
    123                                 // Marked as on demand: it is cut when a page asks for it, not here.
     129                        /* A size that waits is rebuilt where the image already has it and nowhere
     130                           else. The file is as stale as any other after a size changes, so it has
     131                           to be redoable - but cutting it for an image no page has asked about
     132                           would fill the library with exactly the files this feature exists to
     133                           avoid writing. Whether it waits is asked of covers(), so the one switch
     134                           for the whole site and a single size marked on its own behave alike. */
     135                        if ( ATR_On_Demand::covers( $name ) && empty( $had[ $name ] ) ) {
    124136                                continue;
    125137                        }
  • ajax-thumbnail-rebuild/trunk/includes/class-atr-settings.php

    r3681218 r3686771  
    2626                        'upload_quality'       => ATR_Uploads::CORE_QUALITY,
    2727                        'max_upload_dimension' => ATR_Uploads::CORE_THRESHOLD,
     28                        'shrink_original'      => false,
    2829                        'optimize_enabled'     => false,
    2930                        'optimize_quality'     => ATR_Uploads::CORE_QUALITY,
    … …  
    100101                        'uploads'   => array(
    101102                                'label' => __( 'Uploads', 'ajax-thumbnail-rebuild' ),
    102                                 'keys'  => array( 'upload_quality', 'max_upload_dimension' ),
     103                                'keys'  => array( 'upload_quality', 'max_upload_dimension', 'shrink_original' ),
    103104                        ),
    104105                        'optimise'  => array(
    … …  
    159160                );
    160161
     162                add_settings_field(
     163                        'shrink_original',
     164                        __( 'The untouched original', 'ajax-thumbnail-rebuild' ),
     165                        array( __CLASS__, 'render_shrink_original' ),
     166                        self::page_slug( 'uploads' ),
     167                        'atr_uploads'
     168                );
     169
    161170                add_settings_section(
    162171                        'atr_optimize',
    … …  
    360369                switch ( $key ) {
    361370                        // Checkboxes: absent means unticked, which is why $managed matters.
     371                        case 'shrink_original':
    362372                        case 'optimize_enabled':
    363373                        case 'background_enabled':
    … …  
    473483                        );
    474484                        ?>
     485                </p>
     486                <?php
     487        }
     488
     489        public static function render_shrink_original(): void {
     490                $settings = self::all();
     491                ?>
     492                <label>
     493                        <input type="checkbox" name="<?php echo esc_attr( self::OPTION ); ?>[shrink_original]" value="1" <?php checked( $settings['shrink_original'] ); ?> />
     494                        <?php esc_html_e( 'Bring the untouched original down to this limit as well', 'ajax-thumbnail-rebuild' ); ?>
     495                </label>
     496                <p class="description">
     497                        <?php esc_html_e( 'The original is what the sub sizes are cut from; once they are cut it only sits there, and on a photograph from a modern camera it is most of what the library weighs. With this on it is scaled to the limit like everything else - on upload, and for images already here the next time they are rebuilt.', 'ajax-thumbnail-rebuild' ); ?>
     498                </p>
     499                <p class="description">
     500                        <strong><?php esc_html_e( 'This cannot be undone: the full resolution the original was uploaded at is gone, and no size larger than the limit can be cut from it afterwards.', 'ajax-thumbnail-rebuild' ); ?></strong>
    475501                </p>
    476502                <?php
    … …  
    748774        public static function render_on_demand_sizes(): void {
    749775                $settings = self::all();
    750                 $marked   = (array) $settings['on_demand_sizes'];
     776                $stored   = (array) $settings['on_demand_sizes'];
    751777                $sizes    = ATR_Image_Sizes::all();
     778
     779                /* What the site actually treats as on demand, which is the stored list
     780                   plus whatever ajax_thumbnail_rebuild_on_demand_sizes adds. A theme
     781                   declaring its own sizes in code is the case this is for: the size
     782                   and the template that asks for it then live in the same commit. */
     783                $effective = ATR_On_Demand::sizes();
     784
     785                /* Declared in code rather than here. Ticking one of these off would do
     786                   nothing - the filter puts it straight back on the next read - so the
     787                   box says what is true and does not pretend to be editable. */
     788                $declared = array_diff( $effective, $stored );
    752789                ?>
    753790                <fieldset>
    … …  
    756793                                <p class="description"><?php esc_html_e( 'This site has no image sizes registered.', 'ajax-thumbnail-rebuild' ); ?></p>
    757794                        <?php endif; ?>
    758                         <?php foreach ( $sizes as $size ) : ?>
     795                        <?php
     796                        foreach ( $sizes as $size ) :
     797                                $is_declared = in_array( $size['name'], $declared, true );
     798                                ?>
    759799                                <label style="display:block; margin-bottom:4px;">
    760                                         <input type="checkbox" name="<?php echo esc_attr( self::OPTION ); ?>[on_demand_sizes][]" value="<?php echo esc_attr( $size['name'] ); ?>" <?php checked( in_array( $size['name'], $marked, true ) ); ?> />
     800                                        <?php if ( $is_declared ) : ?>
     801                                                <?php /* No name attribute: a disabled box posts nothing anyway, and one
     802                                                         that did would write a filter's decision into the option. */ ?>
     803                                                <input type="checkbox" value="<?php echo esc_attr( $size['name'] ); ?>" checked disabled />
     804                                        <?php else : ?>
     805                                                <input type="checkbox" name="<?php echo esc_attr( self::OPTION ); ?>[on_demand_sizes][]" value="<?php echo esc_attr( $size['name'] ); ?>" <?php checked( in_array( $size['name'], $stored, true ) ); ?> />
     806                                        <?php endif; ?>
    761807                                        <code><?php echo esc_html( $size['name'] ); ?></code>
    762808                                        <span class="description"><?php echo esc_html( ATR_Image_Sizes::dimensions_label( $size ) ); ?></span>
     809                                        <?php if ( $is_declared ) : ?>
     810                                                <span class="description"><?php esc_html_e( '- declared in code', 'ajax-thumbnail-rebuild' ); ?></span>
     811                                        <?php endif; ?>
    763812                                </label>
    764813                        <?php endforeach; ?>
    … …  
    767816                        <?php esc_html_e( 'A size ticked here is never written on upload and is skipped by a rebuild, whatever the Rebuild tab has selected. It is cut the first time a page asks for it. Useful for the big sizes only a handful of pages ever use.', 'ajax-thumbnail-rebuild' ); ?>
    768817                </p>
     818                <?php if ( $declared ) : ?>
     819                        <p class="description">
     820                                <?php esc_html_e( 'The greyed-out ones are set by a theme or plugin through the ajax_thumbnail_rebuild_on_demand_sizes filter and cannot be changed here.', 'ajax-thumbnail-rebuild' ); ?>
     821                        </p>
     822                <?php endif; ?>
    769823                <?php
    770824        }
  • ajax-thumbnail-rebuild/trunk/includes/class-atr-uploads.php

    r3663080 r3686771  
    2525                add_filter( 'wp_editor_set_quality', array( $this, 'filter_quality' ), 10, 2 );
    2626                add_filter( 'big_image_size_threshold', array( $this, 'filter_threshold' ) );
     27
     28                /* After core has cut the sub sizes from the original, and before the
     29                   optimiser at 15, so what that optimises is the file we leave behind
     30                   rather than one we are about to write over. */
     31                add_filter( 'wp_generate_attachment_metadata', array( $this, 'enforce_limit_after_generate' ), 12, 2 );
    2732        }
    2833
    … …  
    6166                return $setting > 0 ? $setting : (int) $threshold;
    6267        }
     68
     69        /**
     70         * Bring every file of an upload down to the limit, once the sizes are cut.
     71         *
     72         * @param array $metadata      Attachment metadata.
     73         * @param int   $attachment_id Attachment id.
     74         * @return array
     75         */
     76        public function enforce_limit_after_generate( $metadata, $attachment_id ) {
     77                if ( ! is_array( $metadata ) ) {
     78                        return $metadata;
     79                }
     80
     81                return self::enforce_limit( (int) $attachment_id, $metadata );
     82        }
     83
     84        /**
     85         * No file of this attachment may be longer than the limit on its long edge.
     86         *
     87         * The sub sizes are cut from the original, so this runs after they are made
     88         * and never before. What is left afterwards is an original at the limit -
     89         * the full resolution it arrived at is gone, which is what the setting says
     90         * it does.
     91         *
     92         * On an image already in the library the "-scaled" copy was written to the
     93         * old limit and is now larger than the original beside it, so it comes down
     94         * too. It keeps its name: the URL is in the content of pages already.
     95         *
     96         * @param int   $attachment_id Attachment to work on.
     97         * @param array $metadata      Metadata to update.
     98         * @return array Metadata, with the served file's dimensions kept honest.
     99         */
     100        public static function enforce_limit( int $attachment_id, array $metadata ): array {
     101                if ( ! ATR_Settings::get( 'shrink_original' ) ) {
     102                        return $metadata;
     103                }
     104
     105                $limit = (int) ATR_Settings::get( 'max_upload_dimension', self::CORE_THRESHOLD );
     106
     107                if ( $limit <= 0 || ! wp_attachment_is_image( $attachment_id ) ) {
     108                        return $metadata;
     109                }
     110
     111                $file = get_attached_file( $attachment_id );
     112
     113                if ( ! $file || ! file_exists( $file ) ) {
     114                        return $metadata;
     115                }
     116
     117                $directory = trailingslashit( dirname( $file ) );
     118                $original  = empty( $metadata['original_image'] )
     119                        ? $file
     120                        : $directory . wp_basename( (string) $metadata['original_image'] );
     121
     122                $written = self::shrink_to( $original, $limit );
     123
     124                /* The served copy, when it is a file of its own. On a fresh upload core
     125                   wrote it to this same limit already and there is nothing to do. */
     126                if ( $original !== $file ) {
     127                        $written = self::shrink_to( $file, $limit ) || $written;
     128                }
     129
     130                if ( ! $written ) {
     131                        return $metadata;
     132                }
     133
     134                $imagesize = wp_getimagesize( $file );
     135
     136                if ( $imagesize ) {
     137                        $metadata['width']  = (int) $imagesize[0];
     138                        $metadata['height'] = (int) $imagesize[1];
     139                }
     140
     141                if ( function_exists( 'wp_filesize' ) ) {
     142                        $metadata['filesize'] = wp_filesize( $file );
     143                }
     144
     145                // Freshly written files: whatever optimising was done to the old ones is gone.
     146                unset( $metadata[ ATR_Optimizer::DONE_KEY ] );
     147
     148                return $metadata;
     149        }
     150
     151        /**
     152         * Scale one file down to the limit, in place and under its own name.
     153         *
     154         * Written beside the file and moved over it only once it is there, so a
     155         * failed resize cannot damage what the site is already serving.
     156         *
     157         * @param string $path  Absolute path.
     158         * @param int    $limit Longest edge allowed.
     159         * @return bool Whether the file was replaced.
     160         */
     161        private static function shrink_to( string $path, int $limit ): bool {
     162                if ( ! file_exists( $path ) || ! is_writable( $path ) ) {
     163                        return false;
     164                }
     165
     166                $imagesize = wp_getimagesize( $path );
     167
     168                if ( ! $imagesize || ( (int) $imagesize[0] <= $limit && (int) $imagesize[1] <= $limit ) ) {
     169                        return false;
     170                }
     171
     172                $editor = wp_get_image_editor( $path );
     173
     174                if ( is_wp_error( $editor ) ) {
     175                        return false;
     176                }
     177
     178                if ( is_wp_error( $editor->resize( $limit, $limit ) ) ) {
     179                        return false;
     180                }
     181
     182                $mime  = (string) wp_get_image_mime( $path );
     183                $saved = $editor->save( self::temporary_path( $path ), $mime ? $mime : null );
     184
     185                if ( is_wp_error( $saved ) || empty( $saved['path'] ) || ! file_exists( $saved['path'] ) ) {
     186                        return false;
     187                }
     188
     189                if ( ! rename( $saved['path'], $path ) ) {
     190                        wp_delete_file( $saved['path'] );
     191
     192                        return false;
     193                }
     194
     195                clearstatcache( true, $path );
     196
     197                return true;
     198        }
     199
     200        /**
     201         * Somewhere to write the smaller file while it is being made.
     202         *
     203         * @param string $path File it is made from.
     204         * @return string
     205         */
     206        private static function temporary_path( string $path ): string {
     207                $extension = pathinfo( $path, PATHINFO_EXTENSION );
     208
     209                return preg_replace( '/\.[^.]+$/', '', $path ) . '-atr-shrinking' . ( $extension ? '.' . $extension : '' );
     210        }
    63211}
  • ajax-thumbnail-rebuild/trunk/languages/ajax-thumbnail-rebuild.pot

    r3681218 r3686771  
    446446msgstr ""
    447447
     448#: includes/class-atr-settings.php:164
    448449#: includes/class-atr-settings.php:203
    449450msgid "The untouched original"
    … …  
    515516#, php-format
    516517msgid "An upload longer than this on its longest edge is scaled down to it, and the untouched original is kept beside it. WordPress uses %s. Set 0 to keep every upload at the size it arrived."
     518msgstr ""
     519
     520#: includes/class-atr-settings.php:494
     521msgid "Bring the untouched original down to this limit as well"
     522msgstr ""
     523
     524#: includes/class-atr-settings.php:497
     525msgid "The original is what the sub sizes are cut from; once they are cut it only sits there, and on a photograph from a modern camera it is most of what the library weighs. With this on it is scaled to the limit like everything else - on upload, and for images already here the next time they are rebuilt."
     526msgstr ""
     527
     528#: includes/class-atr-settings.php:500
     529msgid "This cannot be undone: the full resolution the original was uploaded at is gone, and no size larger than the limit can be cut from it afterwards."
    517530msgstr ""
    518531
  • ajax-thumbnail-rebuild/trunk/readme.txt

    r3681218 r3686771  
    66Requires PHP: 7.4
    77Tested up to: 7.1
    8 Stable tag: 2.1.0
     8Stable tag: 2.2.0
    99License: GPLv2 or later
    1010License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    9090
    9191== Changelog ==
     92
     93= 2.2.0 =
     94
     95* New setting **The untouched original**, under Uploads beside the limit: the full size original kept beside a scaled upload is brought down to the limit as well. WordPress cuts the sub sizes from that file and then leaves it alone at whatever it arrived as, which on a photograph from a modern camera is most of what the library weighs. Off by default, and it cannot be undone - the resolution the original was uploaded at is gone, and no size larger than the limit can be cut from it afterwards. New uploads are covered as they arrive; an image already in the library is covered the next time it is rebuilt, and its `-scaled` copy comes down with it where that was written to an older limit.
     96* A size marked as made on demand can be rebuilt again. It was shown ticked off and disabled on the rebuild screen, so a file cut for it stayed as it was however the size changed. It is now rebuilt for the images that already have it and cut for no others, which refreshes what is there without writing the files the setting exists to avoid.
     97
     98= 2.1.1 =
     99
     100* Sizes a theme or plugin adds through `ajax_thumbnail_rebuild_on_demand_sizes` now show up ticked under **Made on demand** instead of looking switched off, and are greyed out there: unticking one never had any effect, because the filter put it back on the next read.
    92101
    93102= 2.1.0 =
    … …  
    271280== Upgrade Notice ==
    272281
     282= 2.2.0 =
     283
     284The untouched original beside a scaled upload can now be brought down to the size limit as well, which is where most of a library's weight usually sits. Off by default; it cannot be undone. Sizes made on demand can also be rebuilt now, for the images that already have them.
     285
     286= 2.1.1 =
     287
     288The Made on demand list now shows the sizes a theme sets in code, ticked and greyed out, rather than leaving them looking switched off. Display only; nothing about which sizes wait has changed.
     289
    273290= 2.1.0 =
    274291
Note: See TracChangeset for help on using the changeset viewer.