Make WordPress Core

Ticket #32075: 32075.6.diff

File 32075.6.diff, 17.3 KB (added by ocean90, 9 years ago)

Use wp_convert_hr_to_bytes()

  • src/wp-admin/admin.php

     
    138138        require(ABSPATH . 'wp-admin/menu.php');
    139139
    140140if ( current_user_can( 'manage_options' ) ) {
    141         /**
    142          * Filters 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

     
    570570                return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
    571571
    572572        // Unzip can use a lot of memory, but not this much hopefully
    573         /** This filter is documented in wp-admin/admin.php */
    574         @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
     573        wp_raise_memory_limit( 'admin' );
    575574
    576575        $needed_dirs = array();
    577576        $to = trailingslashit($to);
  • src/wp-admin/includes/image-edit.php

     
    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

     
    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                  * Filters 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                  */
    107                 $image_memory_limit = apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT );
    108 
    10999                // Set artificially high because GD uses uncompressed images in memory.
    110                 @ini_set( 'memory_limit', $image_memory_limit );
     100                wp_raise_memory_limit( 'image' );
    111101
    112102                $this->image = @imagecreatefromstring( file_get_contents( $this->file ) );
    113103
  • src/wp-includes/class-wp-image-editor-imagick.php

     
    137137                if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) )
    138138                        return new WP_Error( 'error_loading_image', __('File doesn’t exist?'), $this->file );
    139139
    140                 /** This filter is documented in wp-includes/class-wp-image-editor-gd.php */
    141                 $image_memory_limit = apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT );
    142 
    143140                /*
    144141                 * Even though Imagick uses less PHP memory than GD, set higher limit
    145142                 * for users that have low PHP.ini limits.
    146143                 */
    147                 @ini_set( 'memory_limit', $image_memory_limit );
     144                wp_raise_memory_limit( 'image' );
    148145
    149146                try {
    150147                        $this->image = new Imagick( $this->file );
  • src/wp-includes/default-constants.php

     
    1717function wp_initial_constants() {
    1818        global $blog_id;
    1919
    20         // set memory limits
    21         if ( !defined('WP_MEMORY_LIMIT') ) {
    22                 if ( is_multisite() ) {
    23                         define('WP_MEMORY_LIMIT', '64M');
     20        /**#@+
     21         * Constants for expressing human-readable data sizes in their respective number of bytes.
     22         *
     23         * @since 4.4.0
     24         */
     25        define( 'KB_IN_BYTES', 1024 );
     26        define( 'MB_IN_BYTES', 1024 * KB_IN_BYTES );
     27        define( 'GB_IN_BYTES', 1024 * MB_IN_BYTES );
     28        define( 'TB_IN_BYTES', 1024 * GB_IN_BYTES );
     29        /**#@-*/
     30
     31        $current_limit     = @ini_get( 'memory_limit' );
     32        $current_limit_int = wp_convert_hr_to_bytes( $current_limit );
     33
     34        // Define memory limits.
     35        if ( ! defined( 'WP_MEMORY_LIMIT' ) ) {
     36                if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) {
     37                        define( 'WP_MEMORY_LIMIT', $current_limit );
     38                } elseif ( is_multisite() ) {
     39                        define( 'WP_MEMORY_LIMIT', '64M' );
    2440                } else {
    25                         define('WP_MEMORY_LIMIT', '40M');
     41                        define( 'WP_MEMORY_LIMIT', '40M' );
    2642                }
    2743        }
    2844
    2945        if ( ! defined( 'WP_MAX_MEMORY_LIMIT' ) ) {
    30                 define( 'WP_MAX_MEMORY_LIMIT', '256M' );
     46                if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) {
     47                        define( 'WP_MAX_MEMORY_LIMIT', $current_limit );
     48                } elseif ( -1 === $current_limit_int || $current_limit_int > 256 * MB_IN_BYTES ) {
     49                        define( 'WP_MAX_MEMORY_LIMIT', $current_limit );
     50                } else {
     51                        define( 'WP_MAX_MEMORY_LIMIT', '256M' );
     52                }
    3153        }
    3254
     55        // Set memory limits.
     56        $wp_limit_int = wp_convert_hr_to_bytes( WP_MEMORY_LIMIT );
     57        if ( -1 !== $current_limit_int && ( -1 === $wp_limit_int || $wp_limit_int > $current_limit_int ) ) {
     58                @ini_set( 'memory_limit', WP_MEMORY_LIMIT );
     59        }
     60
    3361        if ( ! isset($blog_id) )
    3462                $blog_id = 1;
    3563
    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 
    5064        if ( !defined('WP_CONTENT_DIR') )
    5165                define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' ); // no trailing slash, full paths only - WP_CONTENT_URL is defined further down
    5266
     
    110124        define( 'MONTH_IN_SECONDS',  30 * DAY_IN_SECONDS    );
    111125        define( 'YEAR_IN_SECONDS',  365 * DAY_IN_SECONDS    );
    112126        /**#@-*/
    113 
    114         /**#@+
    115          * Constants for expressing human-readable data sizes in their respective number of bytes.
    116          *
    117          * @since 4.4.0
    118          */
    119         define( 'KB_IN_BYTES', 1024 );
    120         define( 'MB_IN_BYTES', 1024 * KB_IN_BYTES );
    121         define( 'GB_IN_BYTES', 1024 * MB_IN_BYTES );
    122         define( 'TB_IN_BYTES', 1024 * GB_IN_BYTES );
    123         /**#@-*/
    124127}
    125128
    126129/**
  • src/wp-includes/deprecated.php

     
    31753175        if ( ! function_exists('imagecreatefromstring') )
    31763176                return __('The GD image library is not installed.');
    31773177
    3178         /** This filter is documented in wp-includes/class-wp-image-editor-gd.php */
    3179         $image_memory_limit = apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT );
    3180 
    31813178        // Set artificially high because GD uses uncompressed images in memory.
    3182         @ini_set( 'memory_limit', $image_memory_limit );
     3179        wp_raise_memory_limit( 'image' );
    31833180
    31843181        $image = imagecreatefromstring( file_get_contents( $file ) );
    31853182
  • src/wp-includes/functions.php

     
    53505350        // Strip timezone information
    53515351        return preg_replace( '/(?:Z|[+-]\d{2}(?::\d{2})?)$/', '', $formatted );
    53525352}
     5353
     5354/**
     5355 * Attempts to raise the PHP memory limit for memory intensive processes.
     5356 *
     5357 * Only allows raising the existing limit and prevents lowering it.
     5358 *
     5359 * @since 4.6.0
     5360 *
     5361 * @param string $context Context in which the function is called.
     5362 *                        Either 'admin' or 'image'. Defaults to 'admin'.
     5363 * @return bool|int|string The limit that was attempted to set or false on failure.
     5364 */
     5365function wp_raise_memory_limit( $context = 'admin' ) {
     5366        // Exit early if the limit cannot be changed.
     5367        if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) {
     5368                return false;
     5369        }
     5370
     5371        $current_limit     = @ini_get( 'memory_limit' );
     5372        $current_limit_int = wp_convert_hr_to_bytes( $current_limit );
     5373
     5374        if ( -1 === $current_limit_int ) {
     5375                return false;
     5376        }
     5377
     5378        $wp_max_limit     = WP_MAX_MEMORY_LIMIT;
     5379        $wp_max_limit_int = wp_convert_hr_to_bytes( $wp_max_limit );
     5380        $filtered_limit   = $wp_max_limit;
     5381
     5382        switch ( $context ) {
     5383                case 'admin':
     5384                        /**
     5385                         * Filters the memory limit available for administration screens.
     5386                         *
     5387                         * This only applies to administrators, who may require more memory for tasks like updates.
     5388                         * Memory limits when processing images (uploaded or edited by users of any role) are
     5389                         * handled separately.
     5390                         *
     5391                         * The WP_MAX_MEMORY_LIMIT constant specifically defines the maximum memory limit available
     5392                         * when in the administration back end. The default is 256M (256 megabytes
     5393                         * of memory) or the original `memory_limit` php.ini value if this is higher.
     5394                         *
     5395                         * @since 3.0.0
     5396                         * @since 4.6.0 The default takes the original `memory_limit` into account.
     5397                         *
     5398                         * @param int|string $filtered_limit The maximum WordPress memory limit.
     5399                         *                                   Accepts an integer (bytes), or a shorthand string
     5400                         *                                   notation, such as '256M'.
     5401                         */
     5402                        $filtered_limit = apply_filters( 'admin_memory_limit', $filtered_limit );
     5403                        break;
     5404
     5405                case 'image':
     5406                        /**
     5407                         * Filters the memory limit allocated for image manipulation.
     5408                         *
     5409                         * @since 3.5.0
     5410                         * @since 4.6.0 The default takes the original `memory_limit` into account.
     5411                         *
     5412                         * @param int|string $filtered_limit Maximum memory limit to allocate for images.
     5413                         *                                   Default 256M or the original php.ini memory_limit,
     5414                         *                                   whichever is higher.
     5415                         *                                   Accepts an integer (bytes), or a shorthand string
     5416                         *                                   notation, such as '256M'.
     5417                         */
     5418                        $filtered_limit = apply_filters( 'image_memory_limit', $filtered_limit );
     5419                        break;
     5420
     5421                default:
     5422                        /**
     5423                         * Filters the memory limit allocated for arbitrary contexts.
     5424                         *
     5425                         * The dynamic portion of the hook name, `$context`, refers to an arbitrary
     5426                         * context passed on calling the function. This allows for plugins to define
     5427                         * their own contexts for raising the memory limit.
     5428                         *
     5429                         * @since 4.6.0
     5430                         *
     5431                         * @param int|string $filtered_limit Maximum memory limit to allocate for images.
     5432                         *                                   Default 256M or the original php.ini memory_limit,
     5433                         *                                   whichever is higher.
     5434                         *                                   Accepts an integer (bytes), or a shorthand string
     5435                         *                                   notation, such as '256M'.
     5436                         */
     5437                        $filtered_limit = apply_filters( "{$context}_memory_limit", $filtered_limit );
     5438                        break;
     5439        }
     5440
     5441        $filtered_limit_int = wp_convert_hr_to_bytes( $filtered_limit );
     5442
     5443        if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) {
     5444                @ini_set( 'memory_limit', $filtered_limit );
     5445
     5446                return $filtered_limit;
     5447        } elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) {
     5448                @ini_set( 'memory_limit', $wp_max_limit );
     5449
     5450                return $wp_max_limit;
     5451        } else {
     5452                return false;
     5453        }
     5454}
  • src/wp-includes/load.php

     
    974974        }
    975975        return false;
    976976}
     977
     978/**
     979 * Converts a shorthand byte value to an integer byte value.
     980 *
     981 * @since 2.3.0
     982 *
     983 * @param string $value A shorthand byte value.
     984 * @return int An integer byte value.
     985 */
     986function wp_convert_hr_to_bytes( $value ) {
     987        $value = trim( $value );
     988        $last  = strtolower( substr( $value, -1 ) );
     989
     990        switch ( $last ) {
     991                // Note: the `break` statement is left out on purpose!
     992                case 'g':
     993                        $value *= 1024;
     994                case 'm':
     995                        $value *= 1024;
     996                case 'k':
     997                        $value *= 1024;
     998                default:
     999                        // Left empty on purpose.
     1000                        break;
     1001        }
     1002
     1003        // Deal with large (float) values which run into the maximum integer size.
     1004        return (int) min( PHP_INT_MAX, $value );
     1005}
     1006
     1007/**
     1008 * Determines whether a PHP ini value is changeable at runtime.
     1009 *
     1010 * @since 4.6.0
     1011 *
     1012 * @link http://php.net/manual/en/function.ini-get-all.php
     1013 *
     1014 * @param string $setting The name of the ini setting to check.
     1015 * @return bool True if the value is changeable at runtime. False otherwise.
     1016 */
     1017function wp_is_ini_value_changeable( $setting ) {
     1018        static $ini_all;
     1019
     1020        if ( ! isset( $ini_all ) ) {
     1021                $ini_all = ini_get_all();
     1022        }
     1023
     1024        return isset( $ini_all[ $setting ]['access'] ) &&
     1025               ( INI_ALL === $ini_all[ $setting ]['access'] || INI_USER === $ini_all[ $setting ]['access'] );
     1026}
  • src/wp-includes/media.php

     
    27772777}
    27782778
    27792779/**
    2780  * Converts a shorthand byte value to an integer byte value.
    2781  *
    2782  * @since 2.3.0
    2783  *
    2784  * @param string $size A shorthand byte value.
    2785  * @return int An integer byte value.
    2786  */
    2787 function wp_convert_hr_to_bytes( $size ) {
    2788         $size  = strtolower( $size );
    2789         $bytes = (int) $size;
    2790         if ( strpos( $size, 'k' ) !== false )
    2791                 $bytes = intval( $size ) * KB_IN_BYTES;
    2792         elseif ( strpos( $size, 'm' ) !== false )
    2793                 $bytes = intval($size) * MB_IN_BYTES;
    2794         elseif ( strpos( $size, 'g' ) !== false )
    2795                 $bytes = intval( $size ) * GB_IN_BYTES;
    2796         return $bytes;
    2797 }
    2798 
    2799 /**
    28002780 * Determines the maximum upload size allowed in php.ini.
    28012781 *
    28022782 * @since 2.5.0
  • tests/phpunit/tests/functions.php

     
    887887
    888888                $this->assertNull( wp_ext2type( 'unknown_format' ) );
    889889        }
     890
     891        /**
     892         * Test raising the memory limit.
     893         *
     894         * Unfortunately as the default for 'WP_MAX_MEMORY_LIMIT' in the
     895         * test suite is -1, we can not test the memory limit negotiations.
     896         *
     897         * @ticket 32075
     898         */
     899        function test_wp_raise_memory_limit() {
     900                ini_set( 'memory_limit', '40M' );
     901                $this->assertSame( -1, wp_raise_memory_limit() );
     902                $this->assertEquals( '-1', ini_get( 'memory_limit' ) );
     903        }
    890904}
  • tests/phpunit/tests/load.php

     
     1<?php
     2
     3/**
     4 * @group load.php
     5 */
     6class Tests_Load extends WP_UnitTestCase {
     7
     8        /**
     9         * Test converting PHP ini byte values to integer byte values.
     10         *
     11         * @dataProvider data_wp_convert_hr_to_bytes
     12         */
     13        function test_wp_convert_hr_to_bytes( $value, $expected ) {
     14                $this->assertSame( $expected, wp_convert_hr_to_bytes( $value ) );
     15        }
     16
     17        function data_wp_convert_hr_to_bytes() {
     18                $array = array(
     19                        // Integer input
     20                        array( -1, -1 ), // = no memory limit
     21                        array( 8388608, 8388608 ), // 8M
     22
     23                        // String input (memory limit shorthand values)
     24                        array( '32k', 32768 ),
     25                        array( '64K', 65536 ),
     26                        array( '128m', 134217728 ),
     27                        array( '256M', 268435456 ),
     28                        array( '1g', 1073741824 ),
     29                        array( '1024', 1024 ), // No letter will be interpreted as integer value.
     30
     31                        // Edge cases
     32                        array( 'g', 0 ),
     33                        array( 'null', 0 ),
     34                        array( 'off', 0 ),
     35                );
     36
     37                // Test while running into maximum integer size limit on 32bit systems.
     38                if ( 2147483647 === PHP_INT_MAX ) {
     39                        $array[] = array( '2G', 2147483647 );
     40                        $array[] = array( '4G', 2147483647 );
     41                } else {
     42                        $array[] = array( '2G', 2147483648 );
     43                        $array[] = array( '4G', 4294967296 );
     44                }
     45
     46                return $array;
     47        }
     48
     49        /**
     50         * Test the determining of the changability of a PHP ini value.
     51         *
     52         * @dataProvider data_wp_is_ini_value_changable
     53         */
     54        function test_wp_is_ini_value_changable( $setting, $expected ) {
     55                $this->assertSame( $expected, wp_is_ini_value_changeable( $setting ) );
     56        }
     57
     58        function data_wp_is_ini_value_changable() {
     59                $array = array(
     60                        array( 'memory_limit', true ), // PHP_INI_ALL
     61                        array( 'log_errors', true ), // PHP_INI_ALL
     62                        array( 'upload_max_filesize', false ), // PHP_INI_PERDIR
     63                        array( 'upload_tmp_dir', false ), // PHP_INI_SYSTEM
     64                );
     65
     66                if ( extension_loaded( 'Tidy' ) && version_compare( PHP_VERSION, '7.0.0', '>' ) ) {
     67                        $array[] = array( 'tidy.clean_output', true ); // PHP_INI_USER
     68                }
     69
     70                return $array;
     71        }
     72}