PluginProbe
ManageWP Worker / 4.9.25
ManageWP Worker v4.9.25
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / src / MWP / System / Utils.php

Utils.php in ManageWP Worker 4.9.25, at src/MWP/System/Utils.php

90 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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