| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class ActionScheduler_Compatibility |
| 5 |
*/ |
| 6 |
class ActionScheduler_Compatibility { |
| 7 |
/** |
| 8 |
* Converts a shorthand byte value to an integer byte value. |
| 9 |
* |
| 10 |
* Wrapper for wp_convert_hr_to_bytes(), moved to load.php in WordPress 4.6 from media.php |
| 11 |
* |
| 12 |
* @link https://secure.php.net/manual/en/function.ini-get.php |
| 13 |
* @link https://secure.php.net/manual/en/faq.using.php#faq.using.shorthandbytes |
| 14 |
* |
| 15 |
* @param string $value A (PHP ini) byte value, either shorthand or ordinary. |
| 16 |
* @return int An integer byte value. |
| 17 |
*/ |
| 18 |
public static function convert_hr_to_bytes( $value ) { |
| 19 |
if ( function_exists( 'wp_convert_hr_to_bytes' ) ) { |
| 20 |
return wp_convert_hr_to_bytes( $value ); |
| 21 |
} |
| 22 |
|
| 23 |
$value = strtolower( trim( $value ) ); |
| 24 |
$bytes = (int) $value; |
| 25 |
|
| 26 |
if ( false !== strpos( $value, 'g' ) ) { |
| 27 |
$bytes *= GB_IN_BYTES; |
| 28 |
} elseif ( false !== strpos( $value, 'm' ) ) { |
| 29 |
$bytes *= MB_IN_BYTES; |
| 30 |
} elseif ( false !== strpos( $value, 'k' ) ) { |
| 31 |
$bytes *= KB_IN_BYTES; |
| 32 |
} |
| 33 |
|
| 34 |
// Deal with large (float) values which run into the maximum integer size. |
| 35 |
return min( $bytes, PHP_INT_MAX ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Attempts to raise the PHP memory limit for memory intensive processes. |
| 40 |
* |
| 41 |
* Only allows raising the existing limit and prevents lowering it. |
| 42 |
* |
| 43 |
* Wrapper for wp_raise_memory_limit(), added in WordPress v4.6.0 |
| 44 |
* |
| 45 |
* @return bool|int|string The limit that was set or false on failure. |
| 46 |
*/ |
| 47 |
public static function raise_memory_limit() { |
| 48 |
if ( function_exists( 'wp_raise_memory_limit' ) ) { |
| 49 |
return wp_raise_memory_limit( 'admin' ); |
| 50 |
} |
| 51 |
|
| 52 |
$current_limit = @ini_get( 'memory_limit' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 53 |
$current_limit_int = self::convert_hr_to_bytes( $current_limit ); |
| 54 |
|
| 55 |
if ( -1 === $current_limit_int ) { |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
$wp_max_limit = WP_MAX_MEMORY_LIMIT; |
| 60 |
$wp_max_limit_int = self::convert_hr_to_bytes( $wp_max_limit ); |
| 61 |
$filtered_limit = apply_filters( 'admin_memory_limit', $wp_max_limit ); |
| 62 |
$filtered_limit_int = self::convert_hr_to_bytes( $filtered_limit ); |
| 63 |
|
| 64 |
// phpcs:disable WordPress.PHP.IniSet.memory_limit_Blacklisted |
| 65 |
// phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged |
| 66 |
|
| 67 |
if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) { |
| 68 |
if ( false !== @ini_set( 'memory_limit', $filtered_limit ) ) { |
| 69 |
return $filtered_limit; |
| 70 |
} else { |
| 71 |
return false; |
| 72 |
} |
| 73 |
} elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) { |
| 74 |
if ( false !== @ini_set( 'memory_limit', $wp_max_limit ) ) { |
| 75 |
return $wp_max_limit; |
| 76 |
} else { |
| 77 |
return false; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
// phpcs:enable |
| 82 |
|
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Attempts to raise the PHP timeout for time intensive processes. |
| 88 |
* |
| 89 |
* Only allows raising the existing limit and prevents lowering it. Wrapper for wc_set_time_limit(), when available. |
| 90 |
* |
| 91 |
* @param int $limit The time limit in seconds. |
| 92 |
*/ |
| 93 |
public static function raise_time_limit( $limit = 0 ) { |
| 94 |
$limit = (int) $limit; |
| 95 |
$max_execution_time = (int) ini_get( 'max_execution_time' ); |
| 96 |
|
| 97 |
// If the max execution time is already set to zero (unlimited), there is no reason to make a further change. |
| 98 |
if ( 0 === $max_execution_time ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
// Whichever of $max_execution_time or $limit is higher is the amount by which we raise the time limit. |
| 103 |
$raise_by = 0 === $limit || $limit > $max_execution_time ? $limit : $max_execution_time; |
| 104 |
|
| 105 |
if ( function_exists( 'wc_set_time_limit' ) ) { |
| 106 |
wc_set_time_limit( $raise_by ); |
| 107 |
} elseif ( function_exists( 'set_time_limit' ) && false === strpos( ini_get( 'disable_functions' ), 'set_time_limit' ) && ! ini_get( 'safe_mode' ) ) { // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.safe_modeDeprecatedRemoved |
| 108 |
@set_time_limit( $raise_by ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|