| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bookit Helpers |
| 4 |
*/ |
| 5 |
|
| 6 |
use Bookit\Vendor\StellarWP\Arrays\Arr; |
| 7 |
|
| 8 |
/** |
| 9 |
* Generate Price |
| 10 |
* @param $price |
| 11 |
* @return string |
| 12 |
*/ |
| 13 |
function bookit_price( $price ) { |
| 14 |
$settings = \Bookit\Classes\Admin\SettingsController::get_settings(); |
| 15 |
$formatted_price = number_format($price, $settings['decimals_number'], $settings['decimals_separator'], $settings['thousands_separator']); |
| 16 |
|
| 17 |
if ( $settings['currency_position'] == 'left' ) { |
| 18 |
$formatted_price = $settings['currency_symbol'] . $formatted_price; |
| 19 |
} else { |
| 20 |
$formatted_price .= $settings['currency_symbol']; |
| 21 |
} |
| 22 |
|
| 23 |
return $formatted_price; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Translated Date Time |
| 28 |
* @param $format |
| 29 |
* @param $timestamp |
| 30 |
* @return mixed |
| 31 |
*/ |
| 32 |
function bookit_datetime_i18n( $format, $timestamp ) { |
| 33 |
$timezone_str = get_option('timezone_string') ?: 'UTC'; |
| 34 |
$timezone = new \DateTimeZone($timezone_str); |
| 35 |
|
| 36 |
// The date in the local timezone. |
| 37 |
$date = new \DateTime(null, $timezone); |
| 38 |
$date->setTimestamp($timestamp); |
| 39 |
$date_string = $date->format('Y-m-d H:i:s'); |
| 40 |
|
| 41 |
// Pretend the local date is UTC to get the timestamp |
| 42 |
// to pass to date_i18n(). |
| 43 |
$utc_timezone = new \DateTimeZone('UTC'); |
| 44 |
$utc_date = new \DateTime($date_string, $utc_timezone); |
| 45 |
$timestamp = $utc_date->getTimestamp(); |
| 46 |
|
| 47 |
return date_i18n($format, $timestamp, true); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Generate List |
| 52 |
* @param $data |
| 53 |
* @param $key |
| 54 |
* @param $value |
| 55 |
* @param $elementor |
| 56 |
* @return array |
| 57 |
*/ |
| 58 |
function bookit_data_to_list( $data, $key, $value, $elementor = false ) { |
| 59 |
if ( $elementor ) { |
| 60 |
$list = [ '' => esc_html__('Keep empty', 'bookit') ]; |
| 61 |
} else { |
| 62 |
$list = [ esc_html__('Keep empty', 'bookit') => '' ]; |
| 63 |
} |
| 64 |
|
| 65 |
if ( count($data) > 0 ) { |
| 66 |
foreach ( $data as $item ) { |
| 67 |
$list[$item[$key]] = $item[$value]; |
| 68 |
} |
| 69 |
} else { |
| 70 |
if ( $elementor ) { |
| 71 |
$list = [ '' => esc_html__('Nothing found', 'bookit') ]; |
| 72 |
} else { |
| 73 |
$list = [ esc_html__('Nothing found', 'bookit') => '' ]; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
return $list; |
| 78 |
} |
| 79 |
/** |
| 80 |
* Convert PHP To Moment JS Date Format |
| 81 |
* @param $format |
| 82 |
* @return string |
| 83 |
*/ |
| 84 |
function bookit_php_to_moment( $format ) { |
| 85 |
$replacements = [ |
| 86 |
'd' => 'DD', |
| 87 |
'D' => 'ddd', |
| 88 |
'j' => 'D', |
| 89 |
'l' => 'dddd', |
| 90 |
'N' => 'E', |
| 91 |
'S' => 'o', |
| 92 |
'w' => 'e', |
| 93 |
'z' => 'DDD', |
| 94 |
'W' => 'W', |
| 95 |
'F' => 'MMMM', |
| 96 |
'm' => 'MM', |
| 97 |
'M' => 'MMM', |
| 98 |
'n' => 'M', |
| 99 |
't' => '', // no equivalent |
| 100 |
'L' => '', // no equivalent |
| 101 |
'o' => 'YYYY', |
| 102 |
'Y' => 'YYYY', |
| 103 |
'y' => 'YY', |
| 104 |
'a' => 'a', |
| 105 |
'A' => 'A', |
| 106 |
'B' => '', // no equivalent |
| 107 |
'g' => 'h', |
| 108 |
'G' => 'H', |
| 109 |
'h' => 'hh', |
| 110 |
'H' => 'HH', |
| 111 |
'i' => 'mm', |
| 112 |
's' => 'ss', |
| 113 |
'u' => 'SSS', |
| 114 |
'e' => 'zz', // deprecated since version 1.6.0 of moment.js |
| 115 |
'I' => '', // no equivalent |
| 116 |
'O' => '', // no equivalent |
| 117 |
'P' => '', // no equivalent |
| 118 |
'T' => '', // no equivalent |
| 119 |
'Z' => '', // no equivalent |
| 120 |
'c' => '', // no equivalent |
| 121 |
'r' => '', // no equivalent |
| 122 |
'U' => 'X', |
| 123 |
]; |
| 124 |
$momentFormat = strtr($format, $replacements); |
| 125 |
return $momentFormat; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Check if Pro plugin is active |
| 130 |
* @return bool |
| 131 |
*/ |
| 132 |
function bookit_pro_active() {//todo remove |
| 133 |
return defined("BOOKIT_PRO_VERSION"); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Disable Pro Features |
| 138 |
* @return bool |
| 139 |
*/ |
| 140 |
function bookit_pro_features_disabled() {//todo remove |
| 141 |
return bookit_pro_active() ? 'false' : 'true'; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Function to get inside array option value |
| 146 |
* key separated by . |
| 147 |
* ex-le: 'bookit_settings.first_key_of_option_bookit_settings.second_key_of_option_bookit_settings |
| 148 |
*/ |
| 149 |
|
| 150 |
function get_option_by_path( $path ) { |
| 151 |
|
| 152 |
$keys = explode( '.', $path ); |
| 153 |
$option = get_option( $keys[0] ); |
| 154 |
|
| 155 |
if ( ! $option || ! is_array( $option ) ) { |
| 156 |
return false; |
| 157 |
} |
| 158 |
array_shift($keys); |
| 159 |
|
| 160 |
foreach ( $keys as $key ) { |
| 161 |
if ( is_array( $option ) && array_key_exists( $key, $option ) ) { |
| 162 |
$option = $option[ $key ]; |
| 163 |
}else{ |
| 164 |
return false; |
| 165 |
} |
| 166 |
} |
| 167 |
return $option; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* @param string $path | dot-delimited nested key values 'data.settings.etc' |
| 172 |
* @param array $array |
| 173 |
* |
| 174 |
* @return false|mixed |
| 175 |
*/ |
| 176 |
function get_deep_array_value_by_path( $path, $array ) { |
| 177 |
$keys = explode( '.', $path ); |
| 178 |
|
| 179 |
foreach ( $keys as $key ) { |
| 180 |
if ( is_array( $array ) && array_key_exists( $key, $array ) ) { |
| 181 |
$array = $array[ $key ]; |
| 182 |
}else{ |
| 183 |
return false; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
return $array; |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* Encrypts a value using AES-256-CBC encryption and returns a hexadecimal-encoded string. |
| 192 |
* |
| 193 |
* @param string $value The value to be encrypted. |
| 194 |
* |
| 195 |
* @return string The encrypted and hexadecimal-encoded string. |
| 196 |
*/ |
| 197 |
function bookit_crypt( $value ) { |
| 198 |
$vector = "1234567890123412"; |
| 199 |
$data = openssl_encrypt( $value, 'aes-256-cbc', BOOKIT_FILE, OPENSSL_RAW_DATA, $vector ); |
| 200 |
$hex_data = bin2hex( $data ); |
| 201 |
|
| 202 |
return $hex_data; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Decrypts a hexadecimal-encoded string that was encrypted using AES-256-CBC. |
| 207 |
* |
| 208 |
* @param string $value The hexadecimal-encoded string to be decrypted. |
| 209 |
* |
| 210 |
* @return string The decrypted value. |
| 211 |
*/ |
| 212 |
function bookit_decrypt( $value ) { |
| 213 |
$vector = "1234567890123412"; |
| 214 |
$hex_data = $value; |
| 215 |
$data = hex2bin( $hex_data ); |
| 216 |
|
| 217 |
return openssl_decrypt( $data, 'aes-256-cbc', BOOKIT_FILE, OPENSSL_RAW_DATA, $vector ); |
| 218 |
} |
| 219 |
|
| 220 |
/** check is date string by format */ |
| 221 |
function isDateByFormat($date, $format = 'Y-m-d H:i:s') { |
| 222 |
$dateVar = DateTime::createFromFormat($format, $date); |
| 223 |
return $dateVar && $dateVar->format($format) == $date; |
| 224 |
} |
| 225 |
|
| 226 |
if ( ! function_exists( 'bookit_get_request_var' ) ) { |
| 227 |
/** |
| 228 |
* Tests to see if the requested variable is set either as a post field or as a URL |
| 229 |
* param and returns the value if so. |
| 230 |
* |
| 231 |
* Post data takes priority over fields passed in the URL query. If the field is not |
| 232 |
* set then $default (null unless a different value is specified) will be returned. |
| 233 |
* |
| 234 |
* The variable being tested for can be an array if you wish to find a nested value. |
| 235 |
* |
| 236 |
* @since 2.5.0 |
| 237 |
* |
| 238 |
* @see Arr::get() |
| 239 |
* |
| 240 |
* @param string|array $var |
| 241 |
* @param mixed $default |
| 242 |
* |
| 243 |
* @return mixed |
| 244 |
*/ |
| 245 |
function bookit_get_request_var( $var, $default = null ) { |
| 246 |
$requests = []; |
| 247 |
|
| 248 |
// Prevent a slew of warnings every time we call this. |
| 249 |
if ( isset( $_REQUEST ) ) { |
| 250 |
$requests[] = (array) $_REQUEST; |
| 251 |
} |
| 252 |
|
| 253 |
if ( isset( $_GET ) ) { |
| 254 |
$requests[] = (array) $_GET; |
| 255 |
} |
| 256 |
|
| 257 |
if ( isset( $_POST ) ) { |
| 258 |
$requests[] = (array) $_POST; |
| 259 |
} |
| 260 |
|
| 261 |
if ( empty( $requests ) ) { |
| 262 |
return $default; |
| 263 |
} |
| 264 |
|
| 265 |
$unsafe = Arr::get_in_any( $requests, $var, $default ); |
| 266 |
return bookit_sanitize_deep( $unsafe ); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
if ( ! function_exists( 'bookit_sanitize_deep' ) ) { |
| 271 |
|
| 272 |
/** |
| 273 |
* Sanitizes a value according to its type. |
| 274 |
* |
| 275 |
* The function will recursively sanitize array values. |
| 276 |
* |
| 277 |
* @since 4.9.20 |
| 278 |
* |
| 279 |
* @param mixed $value The value, or values, to sanitize. |
| 280 |
* |
| 281 |
* @return mixed|null Either the sanitized version of the value, or `null` if the value is not a string, number or |
| 282 |
* array. |
| 283 |
*/ |
| 284 |
function bookit_sanitize_deep( &$value ) { |
| 285 |
if ( is_bool( $value ) ) { |
| 286 |
return $value; |
| 287 |
} |
| 288 |
if ( is_string( $value ) ) { |
| 289 |
$value = bookit_sanitize_string( $value ); |
| 290 |
return $value; |
| 291 |
} |
| 292 |
if ( is_int( $value ) ) { |
| 293 |
$value = filter_var( $value, FILTER_VALIDATE_INT ); |
| 294 |
return $value; |
| 295 |
} |
| 296 |
if ( is_float( $value ) ) { |
| 297 |
$value = filter_var( $value, FILTER_VALIDATE_FLOAT ); |
| 298 |
return $value; |
| 299 |
} |
| 300 |
if ( is_array( $value ) ) { |
| 301 |
array_walk( $value, 'bookit_sanitize_deep' ); |
| 302 |
return $value; |
| 303 |
} |
| 304 |
|
| 305 |
return null; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
if ( ! function_exists( 'bookit_is_truthy' ) ) { |
| 310 |
/** |
| 311 |
* Determines if the provided value should be regarded as 'true'. |
| 312 |
* |
| 313 |
* @param mixed $var |
| 314 |
* |
| 315 |
* @return bool |
| 316 |
*/ |
| 317 |
function bookit_is_truthy( $var ) { |
| 318 |
if ( is_bool( $var ) ) { |
| 319 |
return $var; |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Provides an opportunity to modify strings that will be |
| 324 |
* deemed to evaluate to true. |
| 325 |
* |
| 326 |
* @param array $truthy_strings |
| 327 |
*/ |
| 328 |
$truthy_strings = (array) apply_filters( 'bookit_is_truthy_strings', [ |
| 329 |
'1', |
| 330 |
'enable', |
| 331 |
'enabled', |
| 332 |
'on', |
| 333 |
'y', |
| 334 |
'yes', |
| 335 |
'true', |
| 336 |
] ); |
| 337 |
|
| 338 |
// Makes sure we are dealing with lowercase for testing |
| 339 |
if ( is_string( $var ) ) { |
| 340 |
$var = strtolower( $var ); |
| 341 |
} |
| 342 |
|
| 343 |
// If $var is a string, it is only true if it is contained in the above array |
| 344 |
if ( in_array( $var, $truthy_strings, true ) ) { |
| 345 |
return true; |
| 346 |
} |
| 347 |
|
| 348 |
// All other strings will be treated as false |
| 349 |
if ( is_string( $var ) ) { |
| 350 |
return false; |
| 351 |
} |
| 352 |
|
| 353 |
// For other types (ints, floats etc) cast to bool |
| 354 |
return (bool) $var; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
/** |
| 359 |
* Determine whether Bookit Payment is in sandbox mode. |
| 360 |
* |
| 361 |
* @since 2.5.0 |
| 362 |
* |
| 363 |
* @param string $option_name The option name to check. |
| 364 |
* |
| 365 |
* @return bool Whether Bookit Payment is in test mode. |
| 366 |
*/ |
| 367 |
function bookit_is_sandbox_mode( $option_name ) { |
| 368 |
$sandbox_mode = bookit_is_truthy( get_option( $option_name ) ); |
| 369 |
|
| 370 |
/** |
| 371 |
* Filter whether we should use sandbox mode. |
| 372 |
* |
| 373 |
* @since 2.5.0 |
| 374 |
* |
| 375 |
* @param boolean $sandbox_mode should be available or not. |
| 376 |
*/ |
| 377 |
return apply_filters( 'bookit_is_sandbox_mode', $sandbox_mode ); |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* Sanitizes string values. |
| 382 |
* |
| 383 |
* @since 2.5.0 |
| 384 |
* |
| 385 |
* @param string $string The string being sanitized. |
| 386 |
* |
| 387 |
* @return string $string The sanitized version of the string. |
| 388 |
*/ |
| 389 |
function bookit_sanitize_string( $string ) { |
| 390 |
// Replace HTML tags and entities with their plain text equivalents |
| 391 |
$string = htmlspecialchars_decode( $string, ENT_QUOTES ); |
| 392 |
|
| 393 |
// Remove any remaining HTML tags |
| 394 |
$string = strip_tags( $string ); |
| 395 |
|
| 396 |
return $string; |
| 397 |
} |