PluginProbe
Booking Calendar / 11.1
Booking Calendar v11.1
11.8.3 11.8.2 11.8.1 11.8 11.7 11.6.1 11.6 11.5 11.4.3 11.4.2 11.4.1 11.4 11.3 11.2.1 11.2 11.1 11.0 10.15.7 10.15.6 10.1.3 10.10 10.10.1 10.10.2 10.11 10.11.2 All 203 releases
booking / includes / _functions / class-wpbc-action-scheduler-compatibility.php

class-wpbc-action-scheduler-compatibility.php in Booking Calendar 11.1, at includes/_functions/class-wpbc-action-scheduler-compatibility.php

115 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class to Increase Memory and Time
4 *
5 * @package Support functions.
6 */
7
8 /**
9 * Class WPBC_Action_Scheduler_Compatibility
10 */
11 class WPBC_Action_Scheduler_Compatibility {
12
13 /**
14 * Converts a shorthand byte value to an integer byte value.
15 *
16 * Wrapper for wp_convert_hr_to_bytes(), moved to load.php in WordPress 4.6 from media.php
17 *
18 * @link https://secure.php.net/manual/en/function.ini-get.php
19 * @link https://secure.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
20 *
21 * @param string $value A (PHP ini) byte value, either shorthand or ordinary.
22 * @return int An integer byte value.
23 */
24 public static function convert_hr_to_bytes( $value ) {
25 if ( function_exists( 'wp_convert_hr_to_bytes' ) ) {
26 return wp_convert_hr_to_bytes( $value );
27 }
28
29 $value = strtolower( trim( $value ) );
30 $bytes = (int) $value;
31
32 if ( false !== strpos( $value, 'g' ) ) {
33 $bytes *= GB_IN_BYTES;
34 } elseif ( false !== strpos( $value, 'm' ) ) {
35 $bytes *= MB_IN_BYTES;
36 } elseif ( false !== strpos( $value, 'k' ) ) {
37 $bytes *= KB_IN_BYTES;
38 }
39
40 // Deal with large (float) values which run into the maximum integer size.
41 return min( $bytes, PHP_INT_MAX );
42 }
43
44 /**
45 * Attempts to raise the PHP memory limit for memory intensive processes.
46 *
47 * Only allows raising the existing limit and prevents lowering it.
48 *
49 * Wrapper for wp_raise_memory_limit(), added in WordPress v4.6.0
50 *
51 * @return bool|int|string The limit that was set or false on failure.
52 */
53 public static function raise_memory_limit() {
54 if ( function_exists( 'wp_raise_memory_limit' ) ) {
55 return wp_raise_memory_limit( 'admin' );
56 }
57
58 $current_limit = @ini_get( 'memory_limit' ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
59 $current_limit_int = self::convert_hr_to_bytes( $current_limit );
60
61 if ( -1 === $current_limit_int ) {
62 return false;
63 }
64
65 $wp_max_limit = WP_MAX_MEMORY_LIMIT;
66 $wp_max_limit_int = self::convert_hr_to_bytes( $wp_max_limit );
67 $filtered_limit = apply_filters( 'admin_memory_limit', $wp_max_limit ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
68 $filtered_limit_int = self::convert_hr_to_bytes( $filtered_limit );
69
70 if ( -1 === $filtered_limit_int || ( $filtered_limit_int > $wp_max_limit_int && $filtered_limit_int > $current_limit_int ) ) {
71 if ( false !== @ini_set( 'memory_limit', $filtered_limit ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, Squiz.PHP.DiscouragedFunctions.Discouraged
72 return $filtered_limit;
73 } else {
74 return false;
75 }
76 } elseif ( -1 === $wp_max_limit_int || $wp_max_limit_int > $current_limit_int ) {
77 if ( false !== @ini_set( 'memory_limit', $wp_max_limit ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, Squiz.PHP.DiscouragedFunctions.Discouraged
78 return $wp_max_limit;
79 } else {
80 return false;
81 }
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 /*
98 * If the max execution time is already unlimited (zero), or if it exceeds or is equal to the proposed
99 * limit, there is no reason for us to make further changes (we never want to lower it).
100 */
101 if (
102 0 === $max_execution_time
103 || ( $max_execution_time >= $limit && 0 !== $limit )
104 ) {
105 return;
106 }
107
108 if ( function_exists( 'wc_set_time_limit' ) ) {
109 wc_set_time_limit( $limit );
110 } 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
111 @set_time_limit( $limit ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged, Squiz.PHP.DiscouragedFunctions.Discouraged
112 }
113 }
114 }
115