| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the ManageWP Worker plugin. |
| 4 |
* |
| 5 |
* (c) ManageWP LLC <contact@managewp.com> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
class MWP_System_Utils |
| 12 |
{ |
| 13 |
/** |
| 14 |
* Converts value of 'memory_limit' php.ini directive to bytes. |
| 15 |
* |
| 16 |
* @param int|string $memoryLimit |
| 17 |
* |
| 18 |
* @return int Limit in bytes or -1 if it's unlimited. |
| 19 |
*/ |
| 20 |
public static function convertToBytes($memoryLimit) |
| 21 |
{ |
| 22 |
$memoryLimit = (string)$memoryLimit; |
| 23 |
|
| 24 |
if ('-1' === $memoryLimit) { |
| 25 |
return -1; |
| 26 |
} |
| 27 |
|
| 28 |
$memoryLimit = strtolower($memoryLimit); |
| 29 |
$max = strtolower(ltrim($memoryLimit, '+')); |
| 30 |
if (0 === strpos($max, '0x')) { |
| 31 |
$max = intval($max, 16); |
| 32 |
} elseif (0 === strpos($max, '0')) { |
| 33 |
$max = intval($max, 8); |
| 34 |
} else { |
| 35 |
$max = intval($max); |
| 36 |
} |
| 37 |
|
| 38 |
switch (substr($memoryLimit, -1)) { |
| 39 |
/** @noinspection PhpMissingBreakStatementInspection */ |
| 40 |
case 't': |
| 41 |
$max *= 1024; |
| 42 |
/** @noinspection PhpMissingBreakStatementInspection */ |
| 43 |
case 'g': |
| 44 |
$max *= 1024; |
| 45 |
/** @noinspection PhpMissingBreakStatementInspection */ |
| 46 |
case 'm': |
| 47 |
$max *= 1024; |
| 48 |
case 'k': |
| 49 |
$max *= 1024; |
| 50 |
} |
| 51 |
|
| 52 |
return $max; |
| 53 |
} |
| 54 |
|
| 55 |
public static function getCurrentMemoryLimit() |
| 56 |
{ |
| 57 |
return MWP_System_Utils::convertToBytes(ini_get('memory_limit')); |
| 58 |
} |
| 59 |
|
| 60 |
public static function getWPMemoryLimit() |
| 61 |
{ |
| 62 |
if (!defined('WP_MEMORY_LIMIT')) { |
| 63 |
return '64M'; |
| 64 |
} |
| 65 |
|
| 66 |
return WP_MEMORY_LIMIT; |
| 67 |
} |
| 68 |
|
| 69 |
public static function getWPMaxMemoryLimit() |
| 70 |
{ |
| 71 |
if (!defined('WP_MAX_MEMORY_LIMIT')) { |
| 72 |
return '256M'; |
| 73 |
} |
| 74 |
|
| 75 |
return WP_MAX_MEMORY_LIMIT; |
| 76 |
} |
| 77 |
|
| 78 |
public static function setMemoryLimit($tryLimit) |
| 79 |
{ |
| 80 |
$limitValue = self::convertToBytes($tryLimit); |
| 81 |
$currentValue = self::getCurrentMemoryLimit(); |
| 82 |
|
| 83 |
if ($currentValue === -1 || $currentValue >= $limitValue) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
@ini_set('memory_limit', $tryLimit); |
| 88 |
} |
| 89 |
} |
| 90 |
|