WordPress.org

Make WordPress Core


Ignore:
Timestamp:
07/08/2016 02:36:37 PM (4 years ago)
Author:
ocean90
Message:

Bootstrap: Enhance core's memory limit handling.

  • Don't lower memory limit if the current limit is greater than WP_MAX_MEMORY_LIMIT.
  • Set WP_MEMORY_LIMIT and WP_MAX_MEMORY_LIMIT to current limit if the memory_limit setting can't be changed at runtime.
  • Use wp_convert_hr_to_bytes() when parsing the value of the memory_limit setting because it can be a shorthand or an integer value.
  • Introduce wp_raise_memory_limit( $context ) to raise the PHP memory limit for memory intensive processes. This DRYs up some logic and includes the existing admin_memory_limit and image_memory_limit filters. The function can also be used for custom contexts, the {$context}_memory_limit filter allows to customize the limit.
  • Introduce wp_is_ini_value_changeable( $setting ) to determine whether a PHP ini value is changeable at runtime.
  • Remove a function_exists( 'memory_get_usage' ) check. Since PHP 5.2.1 support for memory limit is always enabled.

Related commits: [38011-38013]

Props jrf, A5hleyRich, swissspidy, ocean90.
Fixes #32075.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r37991 r38015  
    53805380    return false;
    53815381}
     5382
     5383/**
     5384 * Attempts to raise the PHP memory limit for memory intensive processes.
     5385 *
     5386 * Only allows raising the existing limit and prevents lowering it.
     5387 *
     5388 * @since 4.6.0
     5389 *
     5390 * @param string $context  Context in which the function is called.
     5391 *                         Either 'admin', 'image' or an arbitrary other context.
     5392 *                         Defaults to 'admin'.
     5393 *                         If an arbitrary context is passed, the similarly arbitrary
     5394 *                         "{$context}_memory_limit" filter will be invoked.
     5395 * @return bool|int|string The limit that was set or false on failure.
     5396 */
     5397function wp_raise_memory_limit( $context = 'admin' ) {
     5398    // Exit early if the limit cannot be changed.
     5399    if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) {
     5400        return false;
     5401    }
     5402
     5403    $current_limit     = @ini_get( 'memory_limit' );
     5404    $current_limit_int = wp_convert_hr_to_bytes( $current_limit );
     5405
     5406    if ( -1 === $current_limit_int ) {
     5407        return false;
     5408    }
     5409
     5410    $wp_max_limit     = WP_MAX_MEMORY_LIMIT;
     5411    $wp_max_limit_int = wp_convert_hr_to_bytes( $wp_max_limit );
     5412    $filtered_limit   = $wp_max_limit;
     5413
     5414    switch ( $context ) {
     5415        case 'admin':
     5416            /**
     5417             * Filters the maximum memory limit available for administration screens.
     5418             *
     5419             * This only applies to administrators, who may require more memory for tasks like updates.
     5420             * Memory limits when processing images (uploaded or edited by users of any role) are
     5421             * handled separately.
     5422             *
     5423             * The WP_MAX_MEMORY_LIMIT constant specifically defines the maximum memory limit available
     5424             * when in the administration back end. The default is 256M (256 megabytes
     5425             * of memory) or the original `memory_limit` php.ini value if this is higher.
     5426             *
     5427             * @since 3.0.0
     5428             * @since 4.6.0 The default takes the original `memory_limit` into account.
     5429             *
     5430             * @param int|string $filtered_limit The maximum WordPress memory limit.
     5431             *                                   Accepts an integer (bytes), or a shorthand string
     5432             *                                   notation, such as '256M'.
     5433             */
     5434            $filtered_limit = apply_filters( 'admin_memory_limit', $filtered_limit );
     5435            break;
     5436
     5437        case 'image':
     5438            /**
     5439             * Filters the memory limit allocated for image manipulation.
     5440             *
     5441             * @since 3.5.0
     5442             * @since 4.6.0 The default takes the original `memory_limit` into account.
     5443             *
     5444             * @param int|string $filtered_limit Maximum memory limit to allocate for images.
     5445             *                                   Default WP_MAX_MEMORY_LIMIT or the original
     5446             *                                   php.ini memory_limit, whichever is higher.
     5447             *                                   Accepts an integer (bytes), or a shorthand string
     5448             *                                   notation, such as '256M'.
     5449             */
     5450            $filtered_limit = apply_filters( 'image_memory_limit', $filtered_limit );
     5451            break;
     5452
     5453        default:
     5454            /**
     5455             * Filters the memory limit allocated for arbitrary contexts.
     5456             *
     5457             * The dynamic portion of the hook name, `$context`, refers to an arbitrary
     5458             * context passed on calling the function. This allows for plugins to define
     5459             * their own contexts for raising the memory limit.
     5460             *
     5461             * @since 4.6.0
     5462             *
     5463             * @param int|string $filtered_limit Maximum memory limit to allocate for images.
     5464             *                                   Default 256M or the original php.ini memory_limit,
     5465             *                                   whichever is higher.
     5466             *                                   Accepts an integer (bytes), or a shorthand string
     5467             *                                   notation, such as '256M'.
     5468             */
     5469            $filtered_limit = apply_filters( "{$context}_memory_limit", $filtered_limit );
     5470            break;
     5471    }
     5472
     5473    $filtered_limit_int = wp_convert_hr_to_bytes( $filtered_limit );
     5474
     5475    if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) {
     5476        if ( false !== @ini_set( 'memory_limit', $filtered_limit ) ) {
     5477            return $filtered_limit;
     5478        } else {
     5479            return false;
     5480        }
     5481    } elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) {
     5482        if ( false !== @ini_set( 'memory_limit', $wp_max_limit ) ) {
     5483            return $wp_max_limit;
     5484        } else {
     5485            return false;
     5486        }
     5487    }
     5488
     5489    return false;
     5490}
Note: See TracChangeset for help on using the changeset viewer.