| 1 |
<?php |
| 2 |
/** |
| 3 |
* Utils class |
| 4 |
* |
| 5 |
* @since next.version |
| 6 |
* |
| 7 |
* @package QuillForms |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace QuillForms; |
| 11 |
|
| 12 |
/** |
| 13 |
* Utils Class |
| 14 |
*/ |
| 15 |
final class Utils { |
| 16 |
|
| 17 |
/** |
| 18 |
* Get max execution time |
| 19 |
* |
| 20 |
* @return int |
| 21 |
*/ |
| 22 |
public static function get_max_execution_time() { |
| 23 |
$max_execution_time = 30; |
| 24 |
|
| 25 |
if ( function_exists( 'ini_get' ) ) { |
| 26 |
$max_execution_time = ini_get( 'max_execution_time' ); |
| 27 |
|
| 28 |
if ( ! $max_execution_time ) { |
| 29 |
$max_execution_time = 30; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
// Decrease a little bit to avoid reaching the limit. |
| 34 |
$max_execution_time = $max_execution_time * 0.75; |
| 35 |
return apply_filters( 'quillforms_max_execution_time', $max_execution_time ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Is memory limit reached |
| 40 |
* |
| 41 |
* @return bool |
| 42 |
*/ |
| 43 |
public static function is_memory_limit_reached() { |
| 44 |
$memory_limit = self::get_memory_limit(); |
| 45 |
$memory_usage = memory_get_usage( true ); |
| 46 |
$memory_limit = self::convert_to_bytes( $memory_limit ); |
| 47 |
$memory_limit = $memory_limit * 0.75; |
| 48 |
|
| 49 |
return $memory_usage >= $memory_limit; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Get memory limit |
| 54 |
* |
| 55 |
* @return string |
| 56 |
*/ |
| 57 |
public static function get_memory_limit() { |
| 58 |
$memory_limit = '128M'; |
| 59 |
|
| 60 |
if ( function_exists( 'ini_get' ) ) { |
| 61 |
$memory_limit = ini_get( 'memory_limit' ); |
| 62 |
|
| 63 |
if ( ! $memory_limit ) { |
| 64 |
$memory_limit = '128M'; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return apply_filters( 'quillforms_memory_limit', $memory_limit ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Convert to bytes |
| 73 |
* |
| 74 |
* @param string $value |
| 75 |
* |
| 76 |
* @return int |
| 77 |
*/ |
| 78 |
public static function convert_to_bytes( $value ) { |
| 79 |
$value = trim( $value ); |
| 80 |
$last = strtolower( $value[ strlen( $value ) - 1 ] ); |
| 81 |
$new_value = intval( $value ); |
| 82 |
|
| 83 |
switch ( $last ) { |
| 84 |
case 'g': |
| 85 |
$new_value *= GB_IN_BYTES; |
| 86 |
break; |
| 87 |
case 'm': |
| 88 |
$new_value *= MB_IN_BYTES; |
| 89 |
break; |
| 90 |
case 'k': |
| 91 |
$new_value *= KB_IN_BYTES; |
| 92 |
break; |
| 93 |
} |
| 94 |
|
| 95 |
return $new_value; |
| 96 |
} |
| 97 |
} |
| 98 |
|