WordPress.org

Make WordPress Core

Ticket #32075: 32075-improved-patch-v6.2.patch

File 32075-improved-patch-v6.2.patch, 13.0 KB (added by jrf, 6 years ago)

Added default case & updated docblocks

  • src/wp-admin/admin.php

    From ddda10fe0bbe7ab45668e2f4e41895a6722fbd14 Mon Sep 17 00:00:00 2001
    Date: Sat, 13 Feb 2016 17:56:32 +0100
    Subject: [PATCH] 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                            | 16 +----
     src/wp-admin/includes/file.php                    |  3 +-
     src/wp-admin/includes/image-edit.php              |  3 +-
     src/wp-includes/class-wp-image-editor-gd.php      | 10 +--
     src/wp-includes/class-wp-image-editor-imagick.php |  3 +-
     src/wp-includes/default-constants.php             | 31 ++++----
     src/wp-includes/deprecated.php                    |  3 +-
     src/wp-includes/functions.php                     | 88 +++++++++++++++++++++++
     src/wp-includes/load.php                          | 28 ++++++++
     tests/phpunit/tests/functions.php                 | 16 +++++
     10 files changed, 154 insertions(+), 47 deletions(-)
    
    diff --git a/src/wp-admin/admin.php b/src/wp-admin/admin.php
    index d67160d..dd30e20 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        wp_raise_memory_limit( 'admin' );
    156142}
    157143
    158144/**
  • src/wp-admin/includes/file.php

    diff --git a/src/wp-admin/includes/file.php b/src/wp-admin/includes/file.php
    index cb85c05..68a38cf 100644
    a b function unzip_file($file, $to) { 
    558558                return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    559559
    560560        // Unzip can use a lot of memory, but not this much hopefully
    561         /** This filter is documented in wp-admin/admin.php */
    562         @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
     561        wp_raise_memory_limit( 'admin' );
    563562
    564563        $needed_dirs = array();
    565564        $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 8947f53..a954d24 100644
    a b function image_edit_apply_changes( $image, $changes ) { 
    586586function stream_preview_image( $post_id ) {
    587587        $post = get_post( $post_id );
    588588
    589         /** This filter is documented in wp-admin/admin.php */
    590         @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
     589        wp_raise_memory_limit( 'admin' );
    591590
    592591        $img = wp_get_image_editor( _load_image_to_edit_path( $post_id ) );
    593592
  • 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..9cb756d 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’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                wp_raise_memory_limit( 'image' );
    109101
    110102                $this->image = @imagecreatefromstring( file_get_contents( $this->file ) );
    111103
  • 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..1888316 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’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                wp_raise_memory_limit( 'image' );
    135134
    136135                try {
    137136                        $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 c9092bd..9b45fb7 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 202ad84..e2d8692 100644
    a b function wp_load_image( $file ) { 
    31623162                return __('The GD image library is not installed.');
    31633163
    31643164        // Set artificially high because GD uses uncompressed images in memory
    3165         @ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) );
     3165        wp_raise_memory_limit( 'image' );
     3166
    31663167        $image = imagecreatefromstring( file_get_contents( $file ) );
    31673168
    31683169        if ( !is_resource( $image ) )
  • src/wp-includes/functions.php

    diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php
    index 3e9b792..d63e9e5 100644
    a b function mysql_to_rfc3339( $date_string ) { 
    52065206        // Strip timezone information
    52075207        return preg_replace( '/(?:Z|[+-]\d{2}(?::\d{2})?)$/', '', $formatted );
    52085208}
     5209
     5210/**
     5211 * Raises the PHP memory limit for memory intensive processes.
     5212 *
     5213 * Allows only to raise the exciting limit and prevents lowering it.
     5214 *
     5215 * @since 4.5.0
     5216 *
     5217 * @param string $context Context in which the function is called.
     5218 *                        Either 'admin' or 'image'. Defaults to 'admin'.
     5219 */
     5220function wp_raise_memory_limit( $context = 'admin' ) {
     5221        $current_limit     = @ini_get( 'memory_limit' );
     5222        $current_limit_int = wp_php_ini_bytes_to_int( $current_limit );
     5223
     5224        if ( -1 === $current_limit_int ) {
     5225                return;
     5226        }
     5227
     5228        $wp_max_limit     = WP_MAX_MEMORY_LIMIT;
     5229        $wp_max_limit_int = wp_php_ini_bytes_to_int( $wp_max_limit );
     5230        $filtered_limit   = $wp_max_limit;
     5231
     5232        switch ( $context ) {
     5233                case 'admin':
     5234                        /**
     5235                         * Filter the memory limit available for administration screens.
     5236                         *
     5237                         * This only applies to administrators, who may require more memory for tasks like updates.
     5238                         * Memory limits when processing images (uploaded or edited by users of any role) are
     5239                         * handled separately.
     5240                         *
     5241                         * The WP_MAX_MEMORY_LIMIT constant specifically defines the maximum memory limit available
     5242                         * when in the administration back-end. The default is 256M (256 megabytes
     5243                         * of memory) or the original `memory_limit` php.ini value if this is higher.
     5244                         *
     5245                         * @since 3.0.0
     5246                         *
     5247                         * @param int|string $filtered_limit The maximum WordPress memory limit.
     5248                         *                                   Accepts an integer (bytes), or a shorthand string
     5249                         *                                   notation, such as '256M'.
     5250                         */
     5251                        $filtered_limit = apply_filters( 'admin_memory_limit', $filtered_limit );
     5252                        break;
     5253
     5254                case 'image':
     5255                        /**
     5256                         * Filter the memory limit allocated for image manipulation.
     5257                         *
     5258                         * @since 3.5.0
     5259                         *
     5260                         * @param int|string $filtered_limit Maximum memory limit to allocate for images.
     5261                         *                                   Default 256M or the original php.ini memory_limit,
     5262                         *                                   whichever is higher.
     5263                         *                                   Accepts an integer (bytes), or a shorthand string
     5264                         *                                   notation, such as '256M'.
     5265                         */
     5266                        $filtered_limit = apply_filters( 'image_memory_limit', $filtered_limit );
     5267                        break;
     5268
     5269                default:
     5270                        /**
     5271                         * Filter the memory limit allocated for arbitrary contexts.
     5272                         *
     5273                         * The dynamic portion of the hook name, `$context`, refers to an arbitrary
     5274                         * context passed on calling the function. This allows for plugins to define
     5275                         * their own contexts for raising the memory limit.
     5276                         *
     5277                         * @since 4.5.0
     5278                         *
     5279                         * @param int|string $filtered_limit Maximum memory limit to allocate for images.
     5280                         *                                   Default 256M or the original php.ini memory_limit,
     5281                         *                                   whichever is higher.
     5282                         *                                   Accepts an integer (bytes), or a shorthand string
     5283                         *                                   notation, such as '256M'.
     5284                         */
     5285                        $filtered_limit = apply_filters( "{$context}_memory_limit", $filtered_limit );
     5286                        break;
     5287        }
     5288
     5289        $filtered_limit_int = wp_php_ini_bytes_to_int( $filtered_limit );
     5290
     5291        if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) {
     5292                @ini_set( 'memory_limit', $filtered_limit );
     5293        } elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) {
     5294                @ini_set( 'memory_limit', $wp_max_limit );
     5295        }
     5296}
  • src/wp-includes/load.php

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

    diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php
    index 631b303..f0643fb 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         * {@internal Unfortunately as the default for 'WP_MAX_MEMORY_LIMIT' in the
     725         * test suite is -1, we can not 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}