WordPress.org

Make WordPress Core

Ticket #32075: 32075-improved-patch-v2.patch

File 32075-improved-patch-v2.patch, 12.0 KB (added by jrf, 6 years ago)

Grr.. Tabs, not spaces...

  • src/wp-admin/admin.php

    From 11759999b2e013e574cc5eb7c3743e22793eacbf Mon Sep 17 00:00:00 2001
    From: jrfnl <github_nospam@adviesenzo.nl>
    Date: Thu, 10 Dec 2015 10:58:06 +0100
    Subject: [PATCH] Improved patch for issue #32075.
    
    Prevent WP setting the memory limit to a value lower than it currently is.
    Also fixes a bug in how the memory limits were tested in the first place.
    ---
     src/wp-admin/admin.php                            | 17 +--------
     src/wp-admin/includes/file.php                    |  4 +-
     src/wp-admin/includes/image-edit.php              |  4 +-
     src/wp-includes/class-wp-image-editor-gd.php      | 11 +-----
     src/wp-includes/class-wp-image-editor-imagick.php |  4 +-
     src/wp-includes/default-constants.php             | 31 ++++++++--------
     src/wp-includes/deprecated.php                    |  4 +-
     src/wp-includes/functions.php                     | 45 +++++++++++++++++++++++
     src/wp-includes/load.php                          | 26 +++++++++++++
     tests/phpunit/tests/functions.php                 | 16 ++++++++
     10 files changed, 115 insertions(+), 47 deletions(-)
    
    diff --git a/src/wp-admin/admin.php b/src/wp-admin/admin.php
    index b1fa18f..6bfaf1c 100644
    a b else 
    138138        require(ABSPATH . 'wp-admin/menu.php');
    139139
    140140if ( current_user_can( 'manage_options' ) ) {
    141         /**
    142          * Filter the maximum memory limit available for administration screens.
    143          *
    144          * This only applies to administrators, who may require more memory for tasks like updates.
    145          * Memory limits when processing images (uploaded or edited by users of any role) are
    146          * handled separately.
    147          *
    148          * The WP_MAX_MEMORY_LIMIT constant specifically defines the maximum memory limit available
    149          * when in the administration back-end. The default is 256M, or 256 megabytes of memory.
    150          *
    151          * @since 3.0.0
    152          *
    153          * @param string 'WP_MAX_MEMORY_LIMIT' The maximum WordPress memory limit. Default 256M.
    154          */
    155         @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
     141        /** The 'admin_memory_limit' filter is documented in wp-includes/functions.php */
     142        wp_raise_memory_limit( 'admin_memory_limit' );
    156143}
    157144
    158145/**
  • src/wp-admin/includes/file.php

    diff --git a/src/wp-admin/includes/file.php b/src/wp-admin/includes/file.php
    index b8052eb..e108b71 100644
    a b function unzip_file($file, $to) { 
    546546                return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    547547
    548548        // Unzip can use a lot of memory, but not this much hopefully
    549         /** This filter is documented in wp-admin/admin.php */
    550         @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
     549        /** The 'admin_memory_limit' filter is documented in wp-includes/functions.php */
     550        wp_raise_memory_limit( 'admin_memory_limit' );
    551551
    552552        $needed_dirs = array();
    553553        $to = trailingslashit($to);
  • src/wp-admin/includes/image-edit.php

    diff --git a/src/wp-admin/includes/image-edit.php b/src/wp-admin/includes/image-edit.php
    index 4015f30..031c73f 100644
    a b function image_edit_apply_changes( $image, $changes ) { 
    557557function stream_preview_image( $post_id ) {
    558558        $post = get_post( $post_id );
    559559
    560         /** This filter is documented in wp-admin/admin.php */
    561         @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
     560        /** The 'admin_memory_limit' filter is documented in wp-includes/functions.php */
     561        wp_raise_memory_limit( 'admin_memory_limit' );
    562562
    563563        $img = wp_get_image_editor( _load_image_to_edit_path( $post_id ) );
    564564
  • src/wp-includes/class-wp-image-editor-gd.php

    diff --git a/src/wp-includes/class-wp-image-editor-gd.php b/src/wp-includes/class-wp-image-editor-gd.php
    index 2093c6b..f60d645 100644
    a b class WP_Image_Editor_GD extends WP_Image_Editor { 
    9696                if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) )
    9797                        return new WP_Error( 'error_loading_image', __('File doesn&#8217;t exist?'), $this->file );
    9898
    99                 /**
    100                  * Filter the memory limit allocated for image manipulation.
    101                  *
    102                  * @since 3.5.0
    103                  *
    104                  * @param int|string $limit Maximum memory limit to allocate for images. Default WP_MAX_MEMORY_LIMIT.
    105                  *                          Accepts an integer (bytes), or a shorthand string notation, such as '256M'.
    106                  */
    10799                // Set artificially high because GD uses uncompressed images in memory
    108                 @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) );
     100                /** The 'image_memory_limit' filter is documented in wp-includes/functions.php */
     101                wp_raise_memory_limit( 'image_memory_limit' );
    109102
    110103                $this->image = @imagecreatefromstring( file_get_contents( $this->file ) );
    111104
  • 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 a14fa40..992187b 100644
    a b class WP_Image_Editor_Imagick extends WP_Image_Editor { 
    129129                if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) )
    130130                        return new WP_Error( 'error_loading_image', __('File doesn&#8217;t exist?'), $this->file );
    131131
    132                 /** This filter is documented in wp-includes/class-wp-image-editor-imagick.php */
    133132                // Even though Imagick uses less PHP memory than GD, set higher limit for users that have low PHP.ini limits
    134                 @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) );
     133                /** The 'image_memory_limit' filter is documented in wp-includes/functions.php */
     134                wp_raise_memory_limit( 'image_memory_limit' );
    135135
    136136                try {
    137137                        $this->image = new Imagick( $this->file );
  • src/wp-includes/default-constants.php

    diff --git a/src/wp-includes/default-constants.php b/src/wp-includes/default-constants.php
    index d7a0e4e..8ceb45a 100644
    a b  
    1717function wp_initial_constants() {
    1818        global $blog_id;
    1919
    20         // set memory limits
     20        // Define memory limits.
    2121        if ( !defined('WP_MEMORY_LIMIT') ) {
    2222                if ( is_multisite() ) {
    2323                        define('WP_MEMORY_LIMIT', '64M');
    function wp_initial_constants() { 
    2626                }
    2727        }
    2828
     29        $current_limit     = @ini_get( 'memory_limit' );
     30        $current_limit_int = wp_php_ini_bytes_to_int( $current_limit );
     31
    2932        if ( ! defined( 'WP_MAX_MEMORY_LIMIT' ) ) {
    30                 define( 'WP_MAX_MEMORY_LIMIT', '256M' );
     33                if ( -1 === $current_limit_int || $current_limit_int > 268435456 ) {
     34                        define( 'WP_MAX_MEMORY_LIMIT', $current_limit );
     35                } else {
     36                        define( 'WP_MAX_MEMORY_LIMIT', '256M' );
     37                }
     38        }
     39
     40        // Set memory limits.
     41        $wp_limit_int = wp_php_ini_bytes_to_int( WP_MEMORY_LIMIT );
     42        if ( -1 !== $current_limit_int && ( -1 === $wp_limit_int || $wp_limit_int > $current_limit_int ) ) {
     43                @ini_set( 'memory_limit', WP_MEMORY_LIMIT );
    3144        }
    3245
    3346        if ( ! isset($blog_id) )
    3447                $blog_id = 1;
    3548
    36         // set memory limits.
    37         if ( function_exists( 'memory_get_usage' ) ) {
    38                 $current_limit = @ini_get( 'memory_limit' );
    39                 $current_limit_int = intval( $current_limit );
    40                 if ( false !== strpos( $current_limit, 'G' ) )
    41                         $current_limit_int *= 1024;
    42                 $wp_limit_int = intval( WP_MEMORY_LIMIT );
    43                 if ( false !== strpos( WP_MEMORY_LIMIT, 'G' ) )
    44                         $wp_limit_int *= 1024;
    45 
    46                 if ( -1 != $current_limit && ( -1 == WP_MEMORY_LIMIT || $current_limit_int < $wp_limit_int ) )
    47                         @ini_set( 'memory_limit', WP_MEMORY_LIMIT );
    48         }
    49 
    5049        if ( !defined('WP_CONTENT_DIR') )
    5150                define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' ); // no trailing slash, full paths only - WP_CONTENT_URL is defined further down
    5251
  • src/wp-includes/deprecated.php

    diff --git a/src/wp-includes/deprecated.php b/src/wp-includes/deprecated.php
    index 571de24..c1b41bd 100644
    a b function wp_load_image( $file ) { 
    31343134                return __('The GD image library is not installed.');
    31353135
    31363136        // Set artificially high because GD uses uncompressed images in memory
    3137         @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) );
     3137        /** The 'image_memory_limit' filter is documented in wp-includes/functions.php */
     3138        wp_raise_memory_limit( 'image_memory_limit' );
     3139
    31383140        $image = imagecreatefromstring( file_get_contents( $file ) );
    31393141
    31403142        if ( !is_resource( $image ) )
  • src/wp-includes/functions.php

    diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php
    index e3fb353..948fe8b 100644
    a b function mysql_to_rfc3339( $date_string ) { 
    51665166        // Strip timezone information
    51675167        return preg_replace( '/(?:Z|[+-]\d{2}(?::\d{2})?)$/', '', $formatted );
    51685168}
     5169
     5170/**
     5171 * WP raise memory limit.
     5172 *
     5173 * Raises the PHP memory limit for memory intensive processes.
     5174 * Only allows filter to raise and not lower the exciting limit.
     5175 *
     5176 * @param string $filter Name of the filter to apply. Defaults to 'admin_memory_limit'.
     5177 */
     5178function wp_raise_memory_limit( $filter = 'admin_memory_limit' ) {
     5179        $current_limit     = @ini_get( 'memory_limit' );
     5180        $current_limit_int = wp_php_ini_bytes_to_int( $current_limit );
     5181
     5182        if ( -1 === $current_limit_int ) {
     5183                return;
     5184        }
     5185
     5186        $wp_max_limit     = WP_MAX_MEMORY_LIMIT;
     5187        $wp_max_limit_int = wp_php_ini_bytes_to_int( $wp_max_limit );
     5188
     5189        /**
     5190         * Filter the desired PHP memory limit before raising it.
     5191         *
     5192         * The WP_MAX_MEMORY_LIMIT constant specifically defines the maximum memory limit
     5193         * available when in the administration back-end. The default is 256M (256 megabytes
     5194         * of memory) or the original `memory_limit` php.ini value if this is higher.
     5195         *
     5196         * @since 3.0.0
     5197         *
     5198         * Filters which get passed in are:
     5199         * 'admin_memory_limit' - Filter the memory limit available for administration screens.
     5200         * 'image_memory_limit' - Filter the memory limit allocated for image manipulation.
     5201         *
     5202         * @param string $filter       The name of the filter to apply. Passed in.
     5203         * @param string $wp_max_limit The maximum WordPress memory limit. Default 256M.
     5204         */
     5205        $filtered_limit     = apply_filters( $filter, $wp_max_limit );
     5206        $filtered_limit_int = wp_php_ini_bytes_to_int( $filtered_limit );
     5207
     5208        if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) {
     5209                @ini_set( 'memory_limit', $filtered_limit );
     5210        } elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) {
     5211                @ini_set( 'memory_limit', $wp_max_limit );
     5212        }
     5213}
  • src/wp-includes/load.php

    diff --git a/src/wp-includes/load.php b/src/wp-includes/load.php
    index 9d247ba..017558b 100644
    a b function wp_installing( $is_installing = null ) { 
    892892
    893893        return (bool) $installing;
    894894}
     895
     896/**
     897 * Convert a PHP ini shorthand byte value to an integer byte value.
     898 *
     899 * @see http://php.net/manual/en/function.ini-get.php
     900 * @see http://php.net/manual/en/faq.using.php#faq.using.shorthandbytes
     901 *
     902 * @param string $value An PHP ini byte value, either shorthand or ordinary.
     903 * @return int Value in bytes.
     904 */
     905function wp_php_ini_bytes_to_int( $value ) {
     906        $value = trim( $value );
     907        $last  = strtolower( $value[ strlen( $value ) - 1 ] );
     908       
     909        switch( $last ) {
     910                // Note: the `break` statement is left out on purpose!
     911                case 'g':
     912                        $value *= 1024;
     913                case 'm':
     914                        $value *= 1024;
     915                case 'k':
     916                        $value *= 1024;
     917        }
     918
     919        return (int) $value;
     920}
  • tests/phpunit/tests/functions.php

    diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php
    index 631b303..2c4ab82 100644
    a b class Tests_Functions extends WP_UnitTestCase { 
    717717                the_date( 'Y', 'before ', ' after', false );
    718718                $this->assertEquals( '', ob_get_clean() );
    719719        }
     720
     721        /**
     722         * Test raising the memory limit.
     723         *
     724         * Unfortunately as the default for 'WP_MAX_MEMORY_LIMIT' in the test suite is -1, we can not
     725         * test the memory limit negotiations.
     726         *
     727         * @ticket 32075
     728         */
     729        function test_wp_raise_memory_limit() {
     730                $original = ini_get( 'memory_limit' );
     731
     732                ini_set( 'memory_limit', '40M' );
     733                wp_raise_memory_limit();
     734                $this->assertEquals( '-1', ini_get( 'memory_limit' ) );
     735        }
    720736}