Plugin Directory

Changeset 2789870


Ignore:
Timestamp:
09/25/2022 01:01:25 PM (4 years ago)
Author:
greencp
Message:

[dynimg] Correct handling of special image sizes like "medium_large" (thanks @madmax4ever)
[dynimg] Improved rewrite rules (thanks @madmax4ever)
[dynimg] Better 404 handling (thanks @madmax4ever)
Fixed deprecated call when deactivating the plugin (thanks @madmax4ever)

Location:
wp-performance-pack/trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • wp-performance-pack/trunk/modules/dynamic_images/class.wppp_dynamic_images.php

    r2369424 r2789870  
    1010
    1111class WPPP_Dynamic_Images extends WPPP_Module {
    12     private $dynimg_image_sizes = NULL;
     12    public static $rewrite_regex = '(.+)-([0-9]+x[0-9]+)\.((?i)jpeg|jpg|png|gif)';
    1313
    1414    public $rw_folder = false;
     
    9797            }
    9898            $path = substr( plugins_url( $file, __FILE__ ), strlen( site_url() ) + 1 ); // cut wp-content including trailing slash
    99             add_rewrite_rule( '(.*)-([0-9]+)x([0-9]+)?\.((?i)jpeg|jpg|png|gif)' , $path, 'top' );
     99            add_rewrite_rule( WPPP_Dynamic_Images::$rewrite_regex, $path, 'top' );
    100100            add_filter ( 'mod_rewrite_rules', array ( $this, 'mod_rewrite_rules' ) );
    101101        }
     
    114114        // init is called prior to options update
    115115        // so add or remove rules before flushing
    116        
    117116        if ( $enabled ) {
    118117            $this->set_rewrite_rules();
     118            flush_rewrite_rules();
    119119        } else {
    120             global $wp_rewrite;
    121             if ( $wp_rewrite && isset( $wp_rewrite->non_wp_rules['(.*)-([0-9]+)x([0-9]+)?\.((?i)jpeg|jpg|png|gif)'] ) ) {
    122                 unset( $wp_rewrite->non_wp_rules['(.*)-([0-9]+)x([0-9]+)?\.((?i)jpeg|jpg|png|gif)'] );
    123             }
     120            WPPP_Dynamic_Images::static_disable_rewrite_rules();
     121        }
     122    }
     123
     124    public static function static_disable_rewrite_rules() {
     125        // init is called prior to options update
     126        // so add or remove rules before flushing
     127        global $wp_rewrite;
     128        if ( $wp_rewrite && isset( $wp_rewrite->non_wp_rules[ WPPP_Dynamic_Images::$rewrite_regex ] ) ) {
     129            unset( $wp_rewrite->non_wp_rules[ WPPP_Dynamic_Images::$rewrite_regex ] );
    124130        }
    125131        flush_rewrite_rules();
     
    268274        $intersizes = get_intermediate_image_sizes();
    269275        foreach ( $intersizes as $s ) {
    270             $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => false );
    271             if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) {
    272                 $sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes
    273             } else {
    274                 $sizes[$s]['width'] = intval ( get_option( "{$s}_size_w" ) ); // For default sizes set in options
    275                 if ( $sizes[$s]['width'] == 0 ) {
    276                     unset( $sizes[$s] );
    277                     continue;
    278                 }
    279             }
    280             if ( isset( $_wp_additional_image_sizes[$s]['height'] ) ) {
    281                 $sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes
    282             } else {
    283                 $sizes[$s]['height'] = intval ( get_option( "{$s}_size_h" ) ); // For default sizes set in options
    284                 if ( $sizes[$s]['height'] == 0 ) {
    285                     unset( $sizes[$s] );
    286                     continue;
    287                 }
    288             }
    289             if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) ) {
    290                 $sizes[$s]['crop'] = $_wp_additional_image_sizes[$s]['crop'] ? true : false; // For theme-added sizes
    291             } else {
    292                 $sizes[$s]['crop'] = get_option( "{$s}_crop" ) ? true : false; // For default sizes set in options
     276            $sizes[ $s ] = array( 'width' => 0, 'height' => 0, 'crop' => false );
     277            if ( in_array( $s, array( 'thumbnail', 'medium', 'medium_large', 'large' ) ) ) {
     278                $sizes[ $s ][ 'width' ] = intval( get_option( $s . '_size_w' ) );
     279                $sizes[ $s ][ 'height' ] = intval( get_option( $s . '_size_h' ) );
     280                $sizes[ $s ][ 'crop' ] = get_option( $s . '_crop' ) ? true : false;
     281            } elseif ( isset( $_wp_additional_image_sizes[ $s ] ) ) {
     282                $sizes[ $s ][ 'width' ] = intval( $_wp_additional_image_sizes[ $s ][ 'width' ] );
     283                $sizes[ $s ][ 'height' ] = intval( $_wp_additional_image_sizes[ $s ][ 'height' ] );
     284                $sizes[ $s ][ 'crop' ] = $_wp_additional_image_sizes[$s]['crop'] ? true : false;
     285            }
     286            if ( ( $sizes[ $s ][ 'width' ] == 0 ) && ( $sizes[ $s ][ 'height' ] == 0 ) ) {
     287                // unset size if both width and height are 0. This is due to special sizes like "medium_large"
     288                // "medium_large" can have a fixed width but has no height limit.
     289                unset( $sizes[ $s ] );
    293290            }
    294291        }
  • wp-performance-pack/trunk/modules/dynamic_images/class.wppp_rewrite.php

    r2240498 r2789870  
    6666                $upbase = parse_url( wp_upload_dir()[ 'baseurl' ], PHP_URL_PATH );
    6767                $wppp .= "RewriteCond %{REQUEST_FILENAME} !-f
    68 RewriteCond %{REQUEST_URI} {$upbase}(.*)$
    69 RewriteCond %{DOCUMENT_ROOT}{$content}/wppp/images/%1 -f
    70 RewriteRule .* {$content}/wppp/images/%1 [L]
     68RewriteCond %{REQUEST_URI} {$upbase}(.*)\.((?i)jpeg|jpg|png|gif)$
     69RewriteCond %{DOCUMENT_ROOT}{$content}/wppp/images/%1.%2 -f
     70RewriteRule .* {$content}/wppp/images/%1.%2 [L]
    7171";
    7272            }
    73 
    7473            $wppp .= "RewriteCond %{REQUEST_FILENAME} !-f
    75 RewriteRule ^(.*)-([0-9]+)x([0-9]+)?\.((?i)jpeg|jpg|png|gif) {$rewrite_base}wp-content/plugins/wp-performance-pack/modules/dynamic_images/{$wppp_file} [QSA,L]
     74RewriteRule ^(.+)-([0-9]+x[0-9]+)\.((?i)jpeg|jpg|png|gif)$ {$rewrite_base}wp-content/plugins/wp-performance-pack/modules/dynamic_images/{$wppp_file} [QSA,L]
    7675#WPPP End";
    7776        }
  • wp-performance-pack/trunk/modules/dynamic_images/class.wppp_serve_image.php

    r2789717 r2789870  
    5151     */
    5252    function exit404( $message ) {
     53        global $wp_query;
     54        if ( isset( $wp_query ) ) {
     55            $wp_query->set_404();
     56            status_header( 404 );
     57        } else {
     58            header( $_SERVER[ 'SERVER_PROTOCOL' ] . ' 404 Not Found' );
     59            header( 'Cache-Control: no-cache, must-revalidate' );   // HTTP/1.1
     60            header( 'Expires: Sat, 26 Jul 1997 05:00:00 GMT' );     // past date
     61            echo $message;
     62            exit();
     63        }
    5364    }
    5465
     
    196207                $new_size = image_resize_dimensions( $imgsize[ 'width' ], $imgsize[ 'height' ], $size_data[ 'width' ], $size_data[ 'height' ], $size_data[ 'crop' ] );
    197208                if ( ( abs( $new_size[ 4 ] - $this->width ) <= 1 ) && ( abs( $new_size[ 5 ] - $this->height ) <= 1 ) ) {
    198                     // allow size to vary by one pixel to catch rounding differences in size calculation 
     209                    // allow size to vary by one pixel to catch rounding differences in size calculation
    199210                    $the_size = $size;
    200211                    $crop = $size_data[ 'crop' ];
     
    208219            }
    209220            unset( $sizes );
    210 
    211             /*
    212             if ( $crop )
    213                 header('Location: http://192.168.3.5:8085/insecure/rs:fill:' . $this->width . ':' . $this->height . ':1/g:sm/plain/local://' . $this->filename );
    214             else
    215                 header('Location: http://192.168.3.5:8085/insecure/rs:fit:' . $this->width . ':' . $this->height . ':1/g:sm/plain/local://' . $this->filename );
    216             exit;
    217             */
    218221
    219222            // create intermediate file name before resizing in order to serve intermediate images from file if they are mirrored into wppp folder
  • wp-performance-pack/trunk/modules/dynamic_images/serve-dynamic-images-ut.php

    r2258139 r2789870  
    3131    }
    3232
    33     function exit404( $message ) {
    34         header( $_SERVER[ 'SERVER_PROTOCOL' ] . ' 404 Not Found' );
    35         header( 'Cache-Control: no-cache, must-revalidate' );   // HTTP/1.1
    36         header( 'Expires: Sat, 26 Jul 1997 05:00:00 GMT' );     // past date
    37         echo $message;
    38         exit();
    39     }
    40 
    4133    function load_wppp() {
    4234        global $wp_performance_pack;
  • wp-performance-pack/trunk/modules/dynamic_images/serve-dynamic-images.php

    r2325721 r2789870  
    22/**
    33 * Serve intermediate images on demand. Is called via mod_rewrite rule.
     4 * This file is used, when ShortInit is selected as server method.
     5 *
    46 *
    57 * @author Björn Ahrens
     
    1113
    1214class WPPP_Serve_Image_SI extends WPPP_Serve_Image {
    13 
    14     function exit404( $message ) {
    15         header( $_SERVER[ 'SERVER_PROTOCOL' ] . ' 404 Not Found' );
    16         header( 'Cache-Control: no-cache, must-revalidate' );   // HTTP/1.1
    17         header( 'Expires: Sat, 26 Jul 1997 05:00:00 GMT' );     // past date
    18         echo $message;
    19         exit();
    20     }
    2115
    2216    function init( $request ) {
  • wp-performance-pack/trunk/readme.txt

    r2789819 r2789870  
    33Tags: performance, image resizing, gettext, disable, cdn
    44Requires at least: 4.7
    5 Tested up to: 6.2
     5Tested up to: 6.0.2
    66Requires PHP: 5.3
    7 Stable tag: 2.5.1
     7Stable tag: 2.5.2
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    124124== Changelog ==
    125125
     126= 2.5.2 =
     127* [dynimg] Correct handling of special image sizes like "medium_large" (thanks @madmax4ever)
     128* [dynimg] Improved rewrite rules (thanks @madmax4ever)
     129* [dynimg] Better 404 handling (thanks @madmax4ever)
     130* Removed deprecated call when deactivating the plugin (thanks @madmax4ever)
     131
    126132= 2.5.1 =
    127133* [wpfeatures] Fixed comment entries in admin menu not being disabled correctly
  • wp-performance-pack/trunk/wp-performance-pack.php

    r2789819 r2789870  
    44    Plugin URI: http://wordpress.org/plugins/wp-performance-pack
    55    Description: Performance optimizations for WordPress. Improve localization performance and image handling, serve images through CDN. 
    6     Version: 2.5.1
     6    Version: 2.5.2
    77    Text Domain: wp-performance-pack
    88    Author: Bj&ouml;rn Ahrens
     
    130130     * @const string
    131131     */
    132     const wppp_version = '2.5.1';
     132    const wppp_version = '2.5.2';
    133133
    134134    /**
     
    360360        if ( $this->options['dynamic_images'] ) {
    361361            // Delete rewrite rules from htaccess
    362             WPPP_Dynamic_Images::flush_rewrite_rules( false );
     362            WPPP_Dynamic_Images::static_disable_rewrite_rules();
    363363        }
    364364
Note: See TracChangeset for help on using the changeset viewer.