arguments
3 years ago
arrayable
3 years ago
filters
3 years ago
http
3 years ago
macro-constants
3 years ago
post
3 years ago
repository
3 years ago
resources
3 years ago
security
3 years ago
theme
3 years ago
attributes-trait.php
3 years ago
base-attributes-trait.php
3 years ago
builder-helper.php
3 years ago
compatibility.php
3 years ago
date-tools.php
3 years ago
gallery.php
3 years ago
get-icon-trait.php
3 years ago
get-template-trait.php
3 years ago
html-attributes-trait.php
3 years ago
instance-trait.php
3 years ago
macros-parser.php
3 years ago
regexp-tools.php
3 years ago
tools.php
3 years ago
tools.php
596 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Jet_Form_Builder\Classes; |
| 4 | |
| 5 | // If this file is called directly, abort. |
| 6 | use Jet_Form_Builder\Plugin; |
| 7 | |
| 8 | if ( ! defined( 'WPINC' ) ) { |
| 9 | die; |
| 10 | } |
| 11 | |
| 12 | |
| 13 | class Tools { |
| 14 | |
| 15 | const EMPTY_DEEP_VALUE = self::class; |
| 16 | |
| 17 | public static function is_editor() { |
| 18 | return self::is_block_editor() || self::is_elementor_editor(); |
| 19 | } |
| 20 | |
| 21 | public static function is_block_editor() { |
| 22 | $allowed_actions = array( 'add', 'edit' ); |
| 23 | |
| 24 | return ( |
| 25 | in_array( self::sanitize_get_param( 'context' ), $allowed_actions, true ) |
| 26 | || in_array( self::sanitize_get_param( 'action' ), $allowed_actions, true ) |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | public static function sanitize_get_param( $param_name ) { |
| 31 | // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 32 | return ! empty( $_GET[ $param_name ] ) ? sanitize_key( $_GET[ $param_name ] ) : ''; |
| 33 | } |
| 34 | |
| 35 | public static function is_elementor_editor() { |
| 36 | if ( ! defined( 'ELEMENTOR_VERSION' ) ) { |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | return ( \Elementor\Plugin::instance()->editor->is_edit_mode() ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Returns all post types list to use in JS components |
| 45 | * |
| 46 | * @param bool $placeholder |
| 47 | * |
| 48 | * @param array $args |
| 49 | * @param string $operator |
| 50 | * |
| 51 | * @return array [type] [description] |
| 52 | */ |
| 53 | public static function get_post_types_for_js( $placeholder = false, $args = array(), $operator = 'and' ) { |
| 54 | |
| 55 | $post_types = get_post_types( $args, 'objects', $operator ); |
| 56 | |
| 57 | $post_types_list = array(); |
| 58 | |
| 59 | if ( $placeholder && is_array( $placeholder ) ) { |
| 60 | $placeholder['value'] = isset( $placeholder['value'] ) ? $placeholder['value'] : ''; |
| 61 | $post_types_list[] = $placeholder; |
| 62 | } |
| 63 | |
| 64 | foreach ( $post_types as $post_type ) { |
| 65 | if ( $post_type->name !== Plugin::instance()->post_type->slug() ) { |
| 66 | $post_types_list[] = array( |
| 67 | 'value' => $post_type->name, |
| 68 | 'label' => $post_type->label, |
| 69 | ); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return self::with_placeholder( $post_types_list ); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get post types list for options. |
| 78 | * |
| 79 | * @return array |
| 80 | */ |
| 81 | public static function get_post_types_for_options() { |
| 82 | return self::get_post_types_for_js( false, array( 'public' => true ) ); |
| 83 | } |
| 84 | |
| 85 | private static function get_escape_func( $type ) { |
| 86 | switch ( $type ) { |
| 87 | case 'template': |
| 88 | default: |
| 89 | return array( self::class, 'esc_template' ); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Sanitize WYSIWYG field |
| 95 | * |
| 96 | * @param $input |
| 97 | * |
| 98 | * @return string |
| 99 | */ |
| 100 | public static function sanitize_wysiwyg( $input ) { |
| 101 | $input = wp_kses_post( $input ); |
| 102 | $input = wp_specialchars_decode( stripslashes( $input ), ENT_COMPAT ); |
| 103 | |
| 104 | return $input; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Return all taxonomies list to use in JS components |
| 109 | * |
| 110 | * @param array $args |
| 111 | * |
| 112 | * @return array |
| 113 | */ |
| 114 | public static function get_taxonomies_for_js( $args = array() ) { |
| 115 | $taxonomies = get_taxonomies( $args, 'objects' ); |
| 116 | |
| 117 | return self::with_placeholder( self::prepare_list_for_js( $taxonomies, 'name', 'label' ) ); |
| 118 | } |
| 119 | |
| 120 | public static function get_taxonomies_for_modify( array $args = array() ): array { |
| 121 | $taxonomies = get_taxonomies( $args, 'objects' ); |
| 122 | $response = array(); |
| 123 | |
| 124 | /** @var \WP_Taxonomy $taxonomy */ |
| 125 | foreach ( $taxonomies as $taxonomy ) { |
| 126 | $response[] = array( |
| 127 | 'label' => $taxonomy->label, |
| 128 | 'value' => "jet_tax__{$taxonomy->name}", |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | return self::with_placeholder( $response ); |
| 133 | } |
| 134 | |
| 135 | public static function get_generators_list_for_js() { |
| 136 | $generators = Plugin::instance()->form->get_generators_list(); |
| 137 | |
| 138 | return self::prepare_list_for_js( $generators ); |
| 139 | } |
| 140 | |
| 141 | public static function get_allowed_mimes_list_for_js() { |
| 142 | return array_values( get_allowed_mime_types() ); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Returns all registeredroles for JS |
| 147 | */ |
| 148 | public static function get_user_roles_for_js( $exclude = array( 'administrator' ) ) { |
| 149 | |
| 150 | $roles = self::get_user_roles( $exclude ); |
| 151 | $result = array(); |
| 152 | |
| 153 | foreach ( $roles as $role => $label ) { |
| 154 | $result[] = array( |
| 155 | 'value' => $role, |
| 156 | 'label' => $label, |
| 157 | ); |
| 158 | } |
| 159 | |
| 160 | return self::with_placeholder( $result ); |
| 161 | } |
| 162 | |
| 163 | public static function get_options_pages_for_js() { |
| 164 | $pages = array(); |
| 165 | |
| 166 | if ( function_exists( 'jet_engine' ) ) { |
| 167 | $pages = jet_engine()->options_pages->get_options_pages_for_select(); |
| 168 | } |
| 169 | |
| 170 | return self::prepare_list_for_js( $pages ); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Returns pages list |
| 175 | * |
| 176 | * @return [type] [description] |
| 177 | */ |
| 178 | public static function get_pages_list_for_js() { |
| 179 | $pages = get_pages(); |
| 180 | |
| 181 | return self::prepare_list_for_js( $pages, 'ID', 'post_title' ); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Returns pages list |
| 186 | * |
| 187 | * @param bool $for_elementor |
| 188 | * |
| 189 | * @param array $args |
| 190 | * |
| 191 | * @return array [description] |
| 192 | */ |
| 193 | public static function get_forms_list_for_js( $for_elementor = false, $args = array() ) { |
| 194 | $posts = get_posts( |
| 195 | array_merge( |
| 196 | array( |
| 197 | 'post_status' => 'publish', |
| 198 | 'posts_per_page' => - 1, |
| 199 | 'post_type' => jet_form_builder()->post_type->slug(), |
| 200 | ), |
| 201 | $args |
| 202 | ) |
| 203 | ); |
| 204 | |
| 205 | return self::prepare_list_for_js( $posts, 'ID', 'post_title', $for_elementor ); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Returns all registered user roles |
| 210 | * |
| 211 | * @param string[] $exclude |
| 212 | * |
| 213 | * @return array [type] [description] |
| 214 | */ |
| 215 | public static function get_user_roles( $exclude = array( 'administrator' ) ) { |
| 216 | |
| 217 | if ( ! function_exists( 'get_editable_roles' ) ) { |
| 218 | return array(); |
| 219 | } else { |
| 220 | $roles = get_editable_roles(); |
| 221 | $result = array(); |
| 222 | |
| 223 | foreach ( $roles as $role => $data ) { |
| 224 | if ( ! in_array( $role, $exclude ) ) { |
| 225 | $result[ $role ] = $data['name']; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | return $result; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Prepare passed array for using in JS options |
| 235 | * |
| 236 | * @param array $array |
| 237 | * @param null $value_key |
| 238 | * @param null $label_key |
| 239 | * @param bool $for_elementor |
| 240 | * |
| 241 | * Only if $for_elementor === false |
| 242 | * @param array $additional_attrs |
| 243 | * |
| 244 | * @return array [type] [description] |
| 245 | */ |
| 246 | public static function prepare_list_for_js( |
| 247 | $array = array(), |
| 248 | $value_key = null, |
| 249 | $label_key = null, |
| 250 | $for_elementor = false, |
| 251 | $additional_attrs = array() |
| 252 | ) { |
| 253 | |
| 254 | $result = array(); |
| 255 | |
| 256 | if ( ! is_array( $array ) || empty( $array ) ) { |
| 257 | return $result; |
| 258 | } |
| 259 | |
| 260 | foreach ( $array as $key => $item ) { |
| 261 | |
| 262 | $value = null; |
| 263 | $label = null; |
| 264 | |
| 265 | if ( is_scalar( $item ) ) { |
| 266 | $value = $key; |
| 267 | $label = $item; |
| 268 | } else { |
| 269 | $value = self::get_property( $item, $value_key ); |
| 270 | $label = self::get_property( $item, $label_key ); |
| 271 | } |
| 272 | |
| 273 | if ( $for_elementor ) { |
| 274 | $result[ $value ] = $label; |
| 275 | } else { |
| 276 | $prepared = array( |
| 277 | 'value' => $value, |
| 278 | 'label' => $label, |
| 279 | ); |
| 280 | foreach ( $additional_attrs as $attr ) { |
| 281 | $prepared[ $attr ] = self::get_property( $item, $attr ); |
| 282 | } |
| 283 | |
| 284 | $result[] = $prepared; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | return $result; |
| 289 | |
| 290 | } |
| 291 | |
| 292 | public static function with_placeholder( $array, $label = '--' ) { |
| 293 | return array_merge( |
| 294 | array( |
| 295 | array( 'label' => $label, 'value' => '' ), |
| 296 | ), |
| 297 | $array |
| 298 | ); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Check if is valid timestamp |
| 303 | * |
| 304 | * @param mixed $timestamp |
| 305 | * |
| 306 | * @return boolean |
| 307 | */ |
| 308 | public static function is_valid_timestamp( $timestamp ): bool { |
| 309 | return ( (string) (int) $timestamp === $timestamp || (int) $timestamp === $timestamp ) |
| 310 | && ( $timestamp <= PHP_INT_MAX ) |
| 311 | && ( $timestamp >= ~PHP_INT_MAX ); |
| 312 | } |
| 313 | |
| 314 | public static function array_merge_intersect_key( $source, $arrays ) { |
| 315 | foreach ( $source as $index => $path ) { |
| 316 | $name = isset( $path['path'] ) ? $path['path'] : $index; |
| 317 | |
| 318 | $deep_value = self::getDeepValue( $name, $arrays ); |
| 319 | |
| 320 | if ( self::EMPTY_DEEP_VALUE === $deep_value ) { |
| 321 | unset( $source[ $index ] ); |
| 322 | } else { |
| 323 | $source[ $index ] = $deep_value; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | return $source; |
| 328 | } |
| 329 | |
| 330 | public static function getDeepValue( $key, $source ) { |
| 331 | $keys = explode( '/', $key ); |
| 332 | $last = end( $keys ); |
| 333 | reset( $keys ); |
| 334 | |
| 335 | return self::deep( $keys, current( $keys ), $last, $source ); |
| 336 | } |
| 337 | |
| 338 | private static function deep( $array, $key, $last, $source ) { |
| 339 | |
| 340 | if ( isset( $source[ $key ] ) ) { |
| 341 | if ( $last !== $key ) { |
| 342 | return self::deep( $array, next( $array ), $last, $source[ $key ] ); |
| 343 | } |
| 344 | |
| 345 | return $source[ $key ]; |
| 346 | } |
| 347 | |
| 348 | return self::EMPTY_DEEP_VALUE; |
| 349 | } |
| 350 | |
| 351 | public static function call( $callback, ...$params ) { |
| 352 | if ( ! is_callable( $callback ) ) { |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | call_user_func( $callback, ...$params ); |
| 357 | } |
| 358 | |
| 359 | public static function decode_unserializable( $value ) { |
| 360 | $data = self::decode_json( $value ); |
| 361 | |
| 362 | return $data ?: maybe_unserialize( $value ); |
| 363 | } |
| 364 | |
| 365 | public static function decode_json( $json ) { |
| 366 | if ( is_array( $json ) ) { |
| 367 | foreach ( $json as $key => $row ) { |
| 368 | $json[ $key ] = static::decode_json( $row ); |
| 369 | } |
| 370 | |
| 371 | return $json; |
| 372 | } |
| 373 | if ( defined( 'JSON_INVALID_UTF8_IGNORE' ) ) { |
| 374 | return json_decode( $json, true, 512, JSON_INVALID_UTF8_IGNORE ); |
| 375 | } |
| 376 | |
| 377 | return json_decode( $json, true ); |
| 378 | } |
| 379 | |
| 380 | public static function encode_json( $json ) { |
| 381 | return wp_json_encode( $json, JSON_UNESCAPED_UNICODE ); |
| 382 | } |
| 383 | |
| 384 | public static function sanitize_recursive( $source = null ) { |
| 385 | if ( ! is_array( $source ) ) { |
| 386 | return self::sanitize( $source ); |
| 387 | } |
| 388 | |
| 389 | $result = array(); |
| 390 | |
| 391 | foreach ( $source as $key => $value ) { |
| 392 | $result[ $key ] = self::sanitize_recursive( $value ); |
| 393 | } |
| 394 | |
| 395 | return $result; |
| 396 | } |
| 397 | |
| 398 | public static function maybe_recursive_sanitize( $source = null ) { |
| 399 | return self::sanitize_recursive( $source ); |
| 400 | } |
| 401 | |
| 402 | public static function sanitize( $source ) { |
| 403 | if ( self::is_url( $source ) ) { |
| 404 | return esc_url_raw( $source ); |
| 405 | } |
| 406 | |
| 407 | return self::sanitize_text_field( $source ); |
| 408 | } |
| 409 | |
| 410 | public static function is_url( $url ) { |
| 411 | return wp_http_validate_url( $url ); |
| 412 | } |
| 413 | |
| 414 | public static function sanitize_text_field( $source, $replace_enqueue = true ) { |
| 415 | $str = (string) $source; |
| 416 | |
| 417 | $filtered = wp_check_invalid_utf8( $str ); |
| 418 | $sanitize_callback = apply_filters( 'jet-form-builder/sanitize-string/callback', false ); |
| 419 | |
| 420 | if ( $sanitize_callback && is_callable( $sanitize_callback ) ) { |
| 421 | $filtered = call_user_func( $sanitize_callback, $filtered ); |
| 422 | } else if ( $replace_enqueue && false !== strpos( $filtered, '<' ) ) { |
| 423 | $filtered = wp_kses_post( $filtered ); |
| 424 | } |
| 425 | |
| 426 | return trim( $filtered ); |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * @param $type |
| 431 | * @param mixed ...$values |
| 432 | * |
| 433 | * @return mixed |
| 434 | */ |
| 435 | private static function call_escape_func( $type, ...$values ) { |
| 436 | return call_user_func( self::get_escape_func( $type ), ...$values ); |
| 437 | } |
| 438 | |
| 439 | public static function recursive_wp_kses( $source, $allowed_html = 'strip' ) { |
| 440 | if ( ! is_array( $source ) ) { |
| 441 | return wp_kses( $source, $allowed_html ); |
| 442 | } |
| 443 | |
| 444 | $result = array(); |
| 445 | foreach ( $source as $key => $value ) { |
| 446 | $result[ $key ] = self::sanitize_recursive( $value ); |
| 447 | } |
| 448 | |
| 449 | return $result; |
| 450 | } |
| 451 | |
| 452 | public static function sanitize_files( $source ) { |
| 453 | if ( ! is_array( $source ) ) { |
| 454 | return false; |
| 455 | } |
| 456 | |
| 457 | $response = array(); |
| 458 | |
| 459 | foreach ( $source as $index => $item ) { |
| 460 | foreach ( $item as $key => $value ) { |
| 461 | switch ( $key ) { |
| 462 | case 'error': |
| 463 | case 'size': |
| 464 | $response[ $index ][ $key ] = absint( $value ); |
| 465 | break; |
| 466 | case 'name': |
| 467 | $response[ $index ][ $key ] = sanitize_file_name( $value ); |
| 468 | break; |
| 469 | case 'type': |
| 470 | $response[ $index ][ $key ] = sanitize_mime_type( $value ); |
| 471 | break; |
| 472 | case 'tmp_name': |
| 473 | $response[ $index ][ $key ] = sanitize_text_field( $value ); |
| 474 | break; |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | return $response; |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * @param $source |
| 484 | * |
| 485 | * @return string |
| 486 | */ |
| 487 | private static function esc_template( $source, $replace_enqueue = true ): string { |
| 488 | return self::sanitize_text_field( $source, $replace_enqueue ); |
| 489 | } |
| 490 | |
| 491 | public static function get_jet_engine_version() { |
| 492 | return function_exists( 'jet_engine' ) |
| 493 | ? jet_engine()->get_version() |
| 494 | : false; |
| 495 | } |
| 496 | |
| 497 | public static function is_readable( string $filename ) { |
| 498 | // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged |
| 499 | return strlen( $filename ) <= PHP_MAXPATHLEN && @is_readable( $filename ); |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Returns template path |
| 504 | * |
| 505 | * @param [type] $path [description] |
| 506 | * |
| 507 | * @return [type] [description] |
| 508 | */ |
| 509 | public static function get_global_template( $path = '' ) { |
| 510 | return JET_FORM_BUILDER_PATH . 'templates/' . $path; |
| 511 | } |
| 512 | |
| 513 | public static function get_property( $source, $name, $if_not_exist = '' ) { |
| 514 | if ( is_object( $source ) ) { |
| 515 | return $source->{$name} ?? $if_not_exist; |
| 516 | } |
| 517 | |
| 518 | return $source[ $name ] ?? $if_not_exist; |
| 519 | } |
| 520 | |
| 521 | public static function esc_template_string( $source, $replace_enqueue = true ) { |
| 522 | return self::call_escape_func( 'template', $source, $replace_enqueue ); |
| 523 | } |
| 524 | |
| 525 | public static function is_repeater_val( $value ): bool { |
| 526 | if ( is_array( $value ) && ! empty( $value ) ) { |
| 527 | foreach ( $value as $item ) { |
| 528 | return is_array( $item ); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | return false; |
| 533 | } |
| 534 | |
| 535 | public static function set_current_post( $post_id ) { |
| 536 | global $post; |
| 537 | |
| 538 | $post = get_post( absint( $post_id ) ); |
| 539 | } |
| 540 | |
| 541 | public static function prepare_repeater_value( $value, $fields_map ): array { |
| 542 | $prepared_value = array(); |
| 543 | |
| 544 | foreach ( $value as $index => $row ) { |
| 545 | $prepared_row = array(); |
| 546 | |
| 547 | foreach ( $row as $item_key => $item_value ) { |
| 548 | $item_key = ! empty( $fields_map[ $item_key ] ) ? self::sanitize_text_field( $fields_map[ $item_key ] ) : $item_key; |
| 549 | $prepared_row[ $item_key ] = $item_value; |
| 550 | } |
| 551 | |
| 552 | $prepared_value[ 'item-' . $index ] = $prepared_row; |
| 553 | } |
| 554 | |
| 555 | return $prepared_value; |
| 556 | } |
| 557 | |
| 558 | public static function is_webhook(): bool { |
| 559 | return ( |
| 560 | defined( 'JET_FB_REST_WEBHOOK' ) && |
| 561 | JET_FB_REST_WEBHOOK |
| 562 | ); |
| 563 | } |
| 564 | |
| 565 | public static function esc_attr( $value ) { |
| 566 | if ( ! is_scalar( $value ) && $value ) { |
| 567 | return esc_attr( Tools::encode_json( $value ) ); |
| 568 | } |
| 569 | |
| 570 | return esc_attr( $value ); |
| 571 | } |
| 572 | |
| 573 | public static function get_suffix(): string { |
| 574 | return defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * Get from function below |
| 579 | * @return string |
| 580 | * @see \retrieve_password |
| 581 | */ |
| 582 | public static function get_site_name(): string { |
| 583 | if ( is_multisite() ) { |
| 584 | return get_network()->site_name; |
| 585 | } |
| 586 | |
| 587 | /* |
| 588 | * The blogname option is escaped with esc_html on the way into the database |
| 589 | * in sanitize_option. We want to reverse this for the plain text arena of emails. |
| 590 | */ |
| 591 | |
| 592 | return wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); |
| 593 | } |
| 594 | |
| 595 | } |
| 596 |