<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[38015] trunk: Bootstrap: Enhance core's memory limit handling.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/38015">38015</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/38015","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>ocean90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-07-08 14:36:37 +0000 (Fri, 08 Jul 2016)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/32075">#32075</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminadminphp">trunk/src/wp-admin/admin.php</a></li>
<li><a href="#trunksrcwpadminincludesfilephp">trunk/src/wp-admin/includes/file.php</a></li>
<li><a href="#trunksrcwpadminincludesimageeditphp">trunk/src/wp-admin/includes/image-edit.php</a></li>
<li><a href="#trunksrcwpincludesclasswpimageeditorgdphp">trunk/src/wp-includes/class-wp-image-editor-gd.php</a></li>
<li><a href="#trunksrcwpincludesclasswpimageeditorimagickphp">trunk/src/wp-includes/class-wp-image-editor-imagick.php</a></li>
<li><a href="#trunksrcwpincludesdefaultconstantsphp">trunk/src/wp-includes/default-constants.php</a></li>
<li><a href="#trunksrcwpincludesdeprecatedphp">trunk/src/wp-includes/deprecated.php</a></li>
<li><a href="#trunksrcwpincludesfunctionsphp">trunk/src/wp-includes/functions.php</a></li>
<li><a href="#trunksrcwpincludesloadphp">trunk/src/wp-includes/load.php</a></li>
<li><a href="#trunktestsphpunittestsfunctionsphp">trunk/tests/phpunit/tests/functions.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsloadisIniValueChangeablephp">trunk/tests/phpunit/tests/load/isIniValueChangeable.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminadminphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/admin.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/admin.php      2016-07-08 13:18:31 UTC (rev 38014)
+++ trunk/src/wp-admin/admin.php        2016-07-08 14:36:37 UTC (rev 38015)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -138,21 +138,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        require(ABSPATH . 'wp-admin/menu.php');
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> if ( current_user_can( 'manage_options' ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        /**
-        * Filters the maximum memory limit available for administration screens.
-        *
-        * This only applies to administrators, who may require more memory for tasks like updates.
-        * Memory limits when processing images (uploaded or edited by users of any role) are
-        * handled separately.
-        *
-        * The WP_MAX_MEMORY_LIMIT constant specifically defines the maximum memory limit available
-        * when in the administration back end. The default is 256M, or 256 megabytes of memory.
-        *
-        * @since 3.0.0
-        *
-        * @param string 'WP_MAX_MEMORY_LIMIT' The maximum WordPress memory limit. Default 256M.
-        */
-       @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_raise_memory_limit( 'admin' );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunksrcwpadminincludesfilephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/file.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/file.php      2016-07-08 13:18:31 UTC (rev 38014)
+++ trunk/src/wp-admin/includes/file.php        2016-07-08 14:36:37 UTC (rev 38015)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -569,9 +569,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! $wp_filesystem || !is_object($wp_filesystem) )
</span><span class="cx" style="display: block; padding: 0 10px">                return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        // Unzip can use a lot of memory, but not this much hopefully
-       /** This filter is documented in wp-admin/admin.php */
-       @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ // Unzip can use a lot of memory, but not this much hopefully.
+       wp_raise_memory_limit( 'admin' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $needed_dirs = array();
</span><span class="cx" style="display: block; padding: 0 10px">        $to = trailingslashit($to);
</span></span></pre></div>
<a id="trunksrcwpadminincludesimageeditphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/image-edit.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/image-edit.php        2016-07-08 13:18:31 UTC (rev 38014)
+++ trunk/src/wp-admin/includes/image-edit.php  2016-07-08 14:36:37 UTC (rev 38015)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -585,8 +585,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> function stream_preview_image( $post_id ) {
</span><span class="cx" style="display: block; padding: 0 10px">        $post = get_post( $post_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        /** This filter is documented in wp-admin/admin.php */
-       @ini_set( 'memory_limit', apply_filters( 'admin_memory_limit', WP_MAX_MEMORY_LIMIT ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_raise_memory_limit( 'admin' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $img = wp_get_image_editor( _load_image_to_edit_path( $post_id ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpimageeditorgdphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-image-editor-gd.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-image-editor-gd.php        2016-07-08 13:18:31 UTC (rev 38014)
+++ trunk/src/wp-includes/class-wp-image-editor-gd.php  2016-07-08 14:36:37 UTC (rev 38015)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -96,18 +96,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) )
</span><span class="cx" style="display: block; padding: 0 10px">                        return new WP_Error( 'error_loading_image', __('File doesn&#8217;t exist?'), $this->file );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                /**
-                * Filters the memory limit allocated for image manipulation.
-                *
-                * @since 3.5.0
-                *
-                * @param int|string $limit Maximum memory limit to allocate for images. Default WP_MAX_MEMORY_LIMIT.
-                *                          Accepts an integer (bytes), or a shorthand string notation, such as '256M'.
-                */
-               $image_memory_limit = apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT );
-
</del><span class="cx" style="display: block; padding: 0 10px">                 // Set artificially high because GD uses uncompressed images in memory.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                @ini_set( 'memory_limit', $image_memory_limit );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         wp_raise_memory_limit( 'image' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->image = @imagecreatefromstring( file_get_contents( $this->file ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpimageeditorimagickphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-image-editor-imagick.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-image-editor-imagick.php   2016-07-08 13:18:31 UTC (rev 38014)
+++ trunk/src/wp-includes/class-wp-image-editor-imagick.php     2016-07-08 14:36:37 UTC (rev 38015)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -137,14 +137,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! is_file( $this->file ) && ! preg_match( '|^https?://|', $this->file ) )
</span><span class="cx" style="display: block; padding: 0 10px">                        return new WP_Error( 'error_loading_image', __('File doesn&#8217;t exist?'), $this->file );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                /** This filter is documented in wp-includes/class-wp-image-editor-gd.php */
-               $image_memory_limit = apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT );
-
</del><span class="cx" style="display: block; padding: 0 10px">                 /*
</span><span class="cx" style="display: block; padding: 0 10px">                 * Even though Imagick uses less PHP memory than GD, set higher limit
</span><span class="cx" style="display: block; padding: 0 10px">                 * for users that have low PHP.ini limits.
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                @ini_set( 'memory_limit', $image_memory_limit );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         wp_raise_memory_limit( 'image' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                try {
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->image = new Imagick( $this->file );
</span></span></pre></div>
<a id="trunksrcwpincludesdefaultconstantsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/default-constants.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/default-constants.php       2016-07-08 13:18:31 UTC (rev 38014)
+++ trunk/src/wp-includes/default-constants.php 2016-07-08 14:36:37 UTC (rev 38015)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -28,36 +28,39 @@
</span><span class="cx" style="display: block; padding: 0 10px">        define( 'TB_IN_BYTES', 1024 * GB_IN_BYTES );
</span><span class="cx" style="display: block; padding: 0 10px">        /**#@-*/
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        // set memory limits
-       if ( !defined('WP_MEMORY_LIMIT') ) {
-               if ( is_multisite() ) {
-                       define('WP_MEMORY_LIMIT', '64M');
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $current_limit     = @ini_get( 'memory_limit' );
+       $current_limit_int = wp_convert_hr_to_bytes( $current_limit );
+
+       // Define memory limits.
+       if ( ! defined( 'WP_MEMORY_LIMIT' ) ) {
+               if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) {
+                       define( 'WP_MEMORY_LIMIT', $current_limit );
+               } elseif ( is_multisite() ) {
+                       define( 'WP_MEMORY_LIMIT', '64M' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        define('WP_MEMORY_LIMIT', '40M');
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 define( 'WP_MEMORY_LIMIT', '40M' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! defined( 'WP_MAX_MEMORY_LIMIT' ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                define( 'WP_MAX_MEMORY_LIMIT', '256M' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) {
+                       define( 'WP_MAX_MEMORY_LIMIT', $current_limit );
+               } elseif ( -1 === $current_limit_int || $current_limit_int > 268435456 /* = 256M */ ) {
+                       define( 'WP_MAX_MEMORY_LIMIT', $current_limit );
+               } else {
+                       define( 'WP_MAX_MEMORY_LIMIT', '256M' );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        // Set memory limits.
+       $wp_limit_int = wp_convert_hr_to_bytes( WP_MEMORY_LIMIT );
+       if ( -1 !== $current_limit_int && ( -1 === $wp_limit_int || $wp_limit_int > $current_limit_int ) ) {
+               @ini_set( 'memory_limit', WP_MEMORY_LIMIT );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( ! isset($blog_id) )
</span><span class="cx" style="display: block; padding: 0 10px">                $blog_id = 1;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        // set memory limits.
-       if ( function_exists( 'memory_get_usage' ) ) {
-               $current_limit = @ini_get( 'memory_limit' );
-               $current_limit_int = intval( $current_limit );
-               if ( false !== strpos( $current_limit, 'G' ) )
-                       $current_limit_int *= 1024;
-               $wp_limit_int = intval( WP_MEMORY_LIMIT );
-               if ( false !== strpos( WP_MEMORY_LIMIT, 'G' ) )
-                       $wp_limit_int *= 1024;
-
-               if ( -1 != $current_limit && ( -1 == WP_MEMORY_LIMIT || $current_limit_int < $wp_limit_int ) )
-                       @ini_set( 'memory_limit', WP_MEMORY_LIMIT );
-       }
-
</del><span class="cx" style="display: block; padding: 0 10px">         if ( !defined('WP_CONTENT_DIR') )
</span><span class="cx" style="display: block; padding: 0 10px">                define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' ); // no trailing slash, full paths only - WP_CONTENT_URL is defined further down
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpincludesdeprecatedphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/deprecated.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/deprecated.php      2016-07-08 13:18:31 UTC (rev 38014)
+++ trunk/src/wp-includes/deprecated.php        2016-07-08 14:36:37 UTC (rev 38015)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -3175,11 +3175,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! function_exists('imagecreatefromstring') )
</span><span class="cx" style="display: block; padding: 0 10px">                return __('The GD image library is not installed.');
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        /** This filter is documented in wp-includes/class-wp-image-editor-gd.php */
-       $image_memory_limit = apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT );
-
</del><span class="cx" style="display: block; padding: 0 10px">         // Set artificially high because GD uses uncompressed images in memory.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        @ini_set( 'memory_limit', $image_memory_limit );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ wp_raise_memory_limit( 'image' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $image = imagecreatefromstring( file_get_contents( $file ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpincludesfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/functions.php       2016-07-08 13:18:31 UTC (rev 38014)
+++ trunk/src/wp-includes/functions.php 2016-07-08 14:36:37 UTC (rev 38015)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -5379,3 +5379,112 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Attempts to raise the PHP memory limit for memory intensive processes.
+ *
+ * Only allows raising the existing limit and prevents lowering it.
+ *
+ * @since 4.6.0
+ *
+ * @param string $context  Context in which the function is called.
+ *                         Either 'admin', 'image' or an arbitrary other context.
+ *                         Defaults to 'admin'.
+ *                         If an arbitrary context is passed, the similarly arbitrary
+ *                         "{$context}_memory_limit" filter will be invoked.
+ * @return bool|int|string The limit that was set or false on failure.
+ */
+function wp_raise_memory_limit( $context = 'admin' ) {
+       // Exit early if the limit cannot be changed.
+       if ( false === wp_is_ini_value_changeable( 'memory_limit' ) ) {
+               return false;
+       }
+
+       $current_limit     = @ini_get( 'memory_limit' );
+       $current_limit_int = wp_convert_hr_to_bytes( $current_limit );
+
+       if ( -1 === $current_limit_int ) {
+               return false;
+       }
+
+       $wp_max_limit     = WP_MAX_MEMORY_LIMIT;
+       $wp_max_limit_int = wp_convert_hr_to_bytes( $wp_max_limit );
+       $filtered_limit   = $wp_max_limit;
+
+       switch ( $context ) {
+               case 'admin':
+                       /**
+                        * Filters the maximum memory limit available for administration screens.
+                        *
+                        * This only applies to administrators, who may require more memory for tasks like updates.
+                        * Memory limits when processing images (uploaded or edited by users of any role) are
+                        * handled separately.
+                        *
+                        * The WP_MAX_MEMORY_LIMIT constant specifically defines the maximum memory limit available
+                        * when in the administration back end. The default is 256M (256 megabytes
+                        * of memory) or the original `memory_limit` php.ini value if this is higher.
+                        *
+                        * @since 3.0.0
+                        * @since 4.6.0 The default takes the original `memory_limit` into account.
+                        *
+                        * @param int|string $filtered_limit The maximum WordPress memory limit.
+                        *                                   Accepts an integer (bytes), or a shorthand string
+                        *                                   notation, such as '256M'.
+                        */
+                       $filtered_limit = apply_filters( 'admin_memory_limit', $filtered_limit );
+                       break;
+
+               case 'image':
+                       /**
+                        * Filters the memory limit allocated for image manipulation.
+                        *
+                        * @since 3.5.0
+                        * @since 4.6.0 The default takes the original `memory_limit` into account.
+                        *
+                        * @param int|string $filtered_limit Maximum memory limit to allocate for images.
+                        *                                   Default WP_MAX_MEMORY_LIMIT or the original
+                        *                                   php.ini memory_limit, whichever is higher.
+                        *                                   Accepts an integer (bytes), or a shorthand string
+                        *                                   notation, such as '256M'.
+                        */
+                       $filtered_limit = apply_filters( 'image_memory_limit', $filtered_limit );
+                       break;
+
+               default:
+                       /**
+                        * Filters the memory limit allocated for arbitrary contexts.
+                        *
+                        * The dynamic portion of the hook name, `$context`, refers to an arbitrary
+                        * context passed on calling the function. This allows for plugins to define
+                        * their own contexts for raising the memory limit.
+                        *
+                        * @since 4.6.0
+                        *
+                        * @param int|string $filtered_limit Maximum memory limit to allocate for images.
+                        *                                   Default 256M or the original php.ini memory_limit,
+                        *                                   whichever is higher.
+                        *                                   Accepts an integer (bytes), or a shorthand string
+                        *                                   notation, such as '256M'.
+                        */
+                       $filtered_limit = apply_filters( "{$context}_memory_limit", $filtered_limit );
+                       break;
+       }
+
+       $filtered_limit_int = wp_convert_hr_to_bytes( $filtered_limit );
+
+       if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) {
+               if ( false !== @ini_set( 'memory_limit', $filtered_limit ) ) {
+                       return $filtered_limit;
+               } else {
+                       return false;
+               }
+       } elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) {
+               if ( false !== @ini_set( 'memory_limit', $wp_max_limit ) ) {
+                       return $wp_max_limit;
+               } else {
+                       return false;
+               }
+       }
+
+       return false;
+}
</ins></span></pre></div>
<a id="trunksrcwpincludesloadphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/load.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/load.php    2016-07-08 13:18:31 UTC (rev 38014)
+++ trunk/src/wp-includes/load.php      2016-07-08 14:36:37 UTC (rev 38015)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1002,3 +1002,27 @@
</span><span class="cx" style="display: block; padding: 0 10px">        // Deal with large (float) values which run into the maximum integer size.
</span><span class="cx" style="display: block; padding: 0 10px">        return min( $bytes, PHP_INT_MAX );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Determines whether a PHP ini value is changeable at runtime.
+ *
+ * @since 4.6.0
+ *
+ * @link http://php.net/manual/en/function.ini-get-all.php
+ *
+ * @param string $setting The name of the ini setting to check.
+ * @return bool True if the value is changeable at runtime. False otherwise.
+ */
+function wp_is_ini_value_changeable( $setting ) {
+       static $ini_all;
+
+       if ( ! isset( $ini_all ) ) {
+               $ini_all = ini_get_all();
+       }
+
+       if ( isset( $ini_all[ $setting ]['access'] ) && ( INI_ALL === $ini_all[ $setting ]['access'] || INI_USER === $ini_all[ $setting ]['access'] ) ) {
+               return true;
+       }
+
+       return false;
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestsfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/functions.php   2016-07-08 13:18:31 UTC (rev 38014)
+++ trunk/tests/phpunit/tests/functions.php     2016-07-08 14:36:37 UTC (rev 38015)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -478,12 +478,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25',
</span><span class="cx" style="display: block; padding: 0 10px">                                true,
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Android 2.2, Android Webkit Browser 
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Android 2.2, Android Webkit Browser
</ins><span class="cx" style="display: block; padding: 0 10px">                         array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'Mozilla/5.0 (Android 2.2; Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.19.4 (KHTML, like Gecko) Version/5.0.3 Safari/533.19.4',
</span><span class="cx" style="display: block; padding: 0 10px">                                true,
</span><span class="cx" style="display: block; padding: 0 10px">                        ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // BlackBerry 9900, BlackBerry browser 
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // BlackBerry 9900, BlackBerry browser
</ins><span class="cx" style="display: block; padding: 0 10px">                         array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+',
</span><span class="cx" style="display: block; padding: 0 10px">                                true,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -865,4 +865,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertNull( wp_ext2type( 'unknown_format' ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * Tests raising the memory limit.
+        *
+        * Unfortunately as the default for 'WP_MAX_MEMORY_LIMIT' in the
+        * test suite is -1, we can not test the memory limit negotiations.
+        *
+        * @ticket 32075
+        */
+       function test_wp_raise_memory_limit() {
+               ini_set( 'memory_limit', '40M' );
+               $this->assertSame( -1, wp_raise_memory_limit() );
+               $this->assertEquals( '-1', ini_get( 'memory_limit' ) );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsloadisIniValueChangeablephp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/load/isIniValueChangeable.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/load/isIniValueChangeable.php                           (rev 0)
+++ trunk/tests/phpunit/tests/load/isIniValueChangeable.php     2016-07-08 14:36:37 UTC (rev 38015)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,48 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * Tests for wp_is_ini_value_changeable().
+ *
+ * @group load.php
+ */
+class Tests_Functions_Is_Ini_Value_Changeable extends WP_UnitTestCase {
+
+       /**
+        * Tests the determining of the changeability of a PHP ini value.
+        *
+        * @ticket 32075
+        *
+        * @dataProvider data_wp_is_ini_value_changeable
+        *
+        * @param string $setting  The setting passed to wp_is_ini_value_changeable().
+        * @param bool   $expected The expected output of wp_convert_hr_to_bytes().
+        */
+       function test_wp_is_ini_value_changeable( $setting, $expected ) {
+               $this->assertSame( $expected, wp_is_ini_value_changeable( $setting ) );
+       }
+
+       /**
+        * Data provider for test_wp_is_ini_value_changeable().
+        *
+        * @return array {
+        *     @type array {
+        *         @type string $setting  The setting passed to wp_is_ini_value_changeable().
+        *         @type bool   $expected The expected output of wp_convert_hr_to_bytes().
+        *     }
+        * }
+        */
+       function data_wp_is_ini_value_changeable() {
+               $array = array(
+                       array( 'memory_limit', true ), // PHP_INI_ALL.
+                       array( 'log_errors', true ), // PHP_INI_ALL.
+                       array( 'upload_max_filesize', false ), // PHP_INI_PERDIR.
+                       array( 'upload_tmp_dir', false ), // PHP_INI_SYSTEM.
+               );
+
+               if ( extension_loaded( 'Tidy' ) && version_compare( PHP_VERSION, '7.0.0', '>' ) ) {
+                       $array[] = array( 'tidy.clean_output', true ); // PHP_INI_USER.
+               }
+
+               return $array;
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>