Ticket #3141: 3141.r6668.diff

File 3141.r6668.diff, 4.1 KB (added by darkdragon, 5 years ago)

updated patch which moves a function to pluggable, so that plugins can replace the functionality in the function. Based on r6668

Line 
1Index: wp-includes/functions.php
2===================================================================
3--- wp-includes/functions.php   (revision 6668)
4+++ wp-includes/functions.php   (working copy)
5@@ -1676,4 +1676,67 @@
6                        trigger_error( printf( __("%s is <strong>deprecated</strong> since version %s with no alternative available."), $file, $version ) );
7        }
8 }
9+
10+/**
11+ * _set_memory_limit() - Try to set PHP memory limit to amount
12+ *
13+ * If the amount is not given, then the default is to set the amount
14+ * to '32MB', however the parameter available will allow setting the
15+ * amount to something other than '32MB'.
16+ *
17+ * Failure might mean that the memory limit is already higher than
18+ * either the default or the amount given. It could mean that ini_set
19+ * and/or ini_get has been disabled and either getting and/or setting
20+ * could not be accomplished.
21+ *
22+ * @since 2.5
23+ * @access private
24+ *
25+ * @param string $mib MiB amount as a string with MiB amount followed by 'M'. Default is 32M.
26+ * @return bool True if the amount was set by this function or already 32MB, false on failure.
27+ */
28+function _set_memory_limit($mib='32M') {
29+       if( !function_exists('memory_get_usage') )
30+               return null;
31+
32+       if( (int) @ini_get('memory_limit') < absint($mib) )
33+               @ini_set('memory_limit', $mib);
34+
35+       if( @ini_get('memory_limit') == $mib )
36+               return true;
37+
38+       return false;
39+}
40+
41+/**
42+ * wp_get_memory_limit() - Return the PHP memory limit in MiB
43+ *
44+ * If null is returned, then --enable-memory-limit directive was
45+ * not enabled during PHP install (< 5.2.1). Prior to PHP 5.2.1
46+ * if --enable-memory-limit was not used during the install of
47+ * PHP, then memory_get_usage() would not exist in PHP.
48+ *
49+ * So Failure (false return value) might also mean that
50+ * memory_limit is unavailable on PHP version greater than PHP
51+ * 5.2.1.
52+ *
53+ * @todo Complete edge cases which have the memory limit set to something
54+ *             other than 'M'. The Memory limit can be an integer
55+ *
56+ * @since 2.5
57+ *
58+ * @return bool|int False if memory limit is not already in MiB or the MiB amount
59+ */
60+function wp_get_memory_limit() {
61+       if( !function_exists('memory_get_usage') )
62+               return null;
63+
64+       $iMemoryLimit = @ini_get('memory_limit');
65+
66+       if( false !== stripos($iMemoryLimit, 'm') )
67+               return absint($iMemoryLimit);
68+       
69+       return false;
70+}
71+
72 ?>
73Index: wp-includes/pluggable.php
74===================================================================
75--- wp-includes/pluggable.php   (revision 6668)
76+++ wp-includes/pluggable.php   (working copy)
77@@ -1260,4 +1260,39 @@
78 }
79 endif;
80 
81+if ( !function_exists('wp_increase_memory_limit') ) :
82+/**
83+ * wp_increase_memory_limit() - Increase PHP memory limit when needed
84+ *
85+ * This function can be applied to memory intensive loops to slowly increase
86+ * the memory when needed instead of just failing.
87+ *
88+ * Null return value means that the function to get the current memory
89+ * usage is unavailable. False return value can mean that the increase is
90+ * not needed at the moment or that the memory limit could not be increased.
91+ *
92+ * @todo Return WP_Error instead of false or instead apply this todo to
93+ *             the wp_set_memory_limit() function instead to return a WP_Error.
94+ * @since 2.5
95+ *
96+ * @return null|bool True on success. Null if can't increase memory
97+ */
98+function wp_increase_memory_limit() {
99+       if( !function_exists('memory_get_usage') )
100+               return null;
101+
102+       $iMemoryLimit = (int) @ini_get('memory_limit');
103+       $iWhenIncrease = (int) $iMemoryLimit * 0.75;
104+
105+       $iCurrentUsage = memory_get_usage() / 1048576; // Divide by Megabytes
106+
107+       if( $iCurrentUsage >= $iWhenIncrease ) {
108+               $iIncreaseToAmount = ($iMemoryLimit+$iWhenIncrease).'MB';
109+               return _set_memory_limit( $iIncreaseToAmount );
110+       }
111+
112+       return false;
113+}
114+endif;
115+
116 ?>
117Index: wp-settings.php
118===================================================================
119--- wp-settings.php     (revision 6668)
120+++ wp-settings.php     (working copy)
121@@ -194,6 +194,11 @@
122 require (ABSPATH . WPINC . '/functions.php');
123 require (ABSPATH . WPINC . '/classes.php');
124 
125+if( defined('WP_MEMORY_LIMIT') )
126+       _set_memory_limit( WP_MEMORY_LIMIT );
127+else
128+       _set_memory_limit();
129+
130 require_wp_db();
131 
132 if ( !empty($wpdb->error) )