Casts
2 weeks ago
Constants
2 weeks ago
DTO
2 weeks ago
Http
2 weeks ago
Managers
2 weeks ago
Models
2 weeks ago
Providers
2 weeks ago
Resources
2 weeks ago
Services
2 weeks ago
Supports
2 weeks ago
Traits
2 weeks ago
Utils
2 weeks ago
Wordpress
2 weeks ago
helpers.php
2 weeks ago
helpers.php
169 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kirki\App; |
| 4 | |
| 5 | use Kirki\Framework\Sanitizer; |
| 6 | |
| 7 | defined('ABSPATH') || exit; |
| 8 | |
| 9 | if (!function_exists('Kirki\App\get_timezone')) { |
| 10 | function get_timezone($is_utc = false) { |
| 11 | if ($is_utc) { |
| 12 | return 'UTC'; |
| 13 | } |
| 14 | |
| 15 | return wp_timezone(); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | if (!function_exists('Kirki\App\get_upload_directory')) { |
| 20 | /** |
| 21 | * Get the base upload directory path |
| 22 | * |
| 23 | * @return string |
| 24 | */ |
| 25 | function get_upload_directory() { |
| 26 | return wp_upload_dir()['basedir']; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | if (!function_exists('Kirki\App\get_upload_directory_url')) { |
| 31 | /** |
| 32 | * Get the base upload directory url |
| 33 | * |
| 34 | * @return string |
| 35 | */ |
| 36 | function get_upload_directory_url() { |
| 37 | return wp_upload_dir()['baseurl']; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | if (!function_exists('Kirki\App\to_boolean')) { |
| 42 | /** |
| 43 | * Return boolean value |
| 44 | * @param mixed $value |
| 45 | * @return bool |
| 46 | */ |
| 47 | function to_boolean($value) { |
| 48 | if (is_bool($value)) { |
| 49 | return $value; |
| 50 | } |
| 51 | |
| 52 | if (is_null($value)) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | if (is_string($value)) { |
| 57 | if ($value === '' || $value === '0' || strtolower($value) === 'false') { |
| 58 | return false; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | if (is_numeric($value) && (int) $value === 0) { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | if (is_array($value) || is_object($value)) { |
| 67 | return !empty($value); |
| 68 | } |
| 69 | |
| 70 | return true; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | if (!function_exists('Kirki\App\is_truthy')) { |
| 75 | /** |
| 76 | * Return is empty array |
| 77 | * @param mixed $value |
| 78 | * @return bool |
| 79 | */ |
| 80 | function is_truthy($value) { |
| 81 | $value = to_boolean($value); |
| 82 | |
| 83 | return $value === true; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (!function_exists('Kirki\App\is_falsy')) { |
| 88 | /** |
| 89 | * Return is empty array |
| 90 | * @param mixed $value |
| 91 | * @return bool |
| 92 | */ |
| 93 | function is_falsy($value) { |
| 94 | $value = to_boolean($value); |
| 95 | |
| 96 | return $value === false; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if (!function_exists('Kirki\App\is_not_empty_array')) { |
| 101 | /** |
| 102 | * Return is not empty array |
| 103 | * @param mixed $value |
| 104 | * @return bool |
| 105 | */ |
| 106 | function is_not_empty_array($value) { |
| 107 | if (!is_array($value)) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | return count($value) > 0; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (!function_exists('Kirki\App\get_editor_mode')) { |
| 116 | /** |
| 117 | * Get editor mode value |
| 118 | * |
| 119 | * @return string |
| 120 | */ |
| 121 | function get_editor_mode() { |
| 122 | return 'kirki'; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if (!function_exists('Kirki\App\soft_flush_rewrite_rules')) { |
| 127 | /** |
| 128 | * Soft flush rewrite rules |
| 129 | * |
| 130 | * @return void |
| 131 | */ |
| 132 | function soft_flush_rewrite_rules() { |
| 133 | flush_rewrite_rules(false); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | |
| 138 | if (!function_exists('Kirki\App\get_request_header')) { |
| 139 | /** |
| 140 | * Get request header by header name with sanitization |
| 141 | * |
| 142 | * @param string $header_name |
| 143 | * |
| 144 | * @return string|null |
| 145 | */ |
| 146 | function get_request_header(string $header_name) { |
| 147 | if (function_exists('getallheaders')) { |
| 148 | $headers = getallheaders(); |
| 149 | |
| 150 | return Sanitizer::apply_rule($headers[$header_name] ?? null, Sanitizer::TEXT); |
| 151 | } |
| 152 | |
| 153 | // Fallback because `getallheaders()` is not guaranteed to exist in every PHP environment |
| 154 | $normalized_name = strtolower(trim($header_name)); |
| 155 | $content_headers = ['content-type', 'content-length', 'content-md5']; |
| 156 | |
| 157 | $header_key = in_array($normalized_name, $content_headers, true) |
| 158 | ? str_replace('-', '_', strtoupper($normalized_name)) |
| 159 | : 'HTTP_' . str_replace('-', '_', strtoupper($normalized_name)); |
| 160 | |
| 161 | $value = filter_input(INPUT_SERVER, $header_key, FILTER_UNSAFE_RAW); |
| 162 | |
| 163 | if (is_null($value) || $value === '' || $value === false) { |
| 164 | return null; |
| 165 | } |
| 166 | |
| 167 | return Sanitizer::apply_rule($value, Sanitizer::TEXT); |
| 168 | } |
| 169 | } |