PluginProbe
Elementor Website Builder – more than just a page builder / 3.20.4
Elementor Website Builder – more than just a page builder v3.20.4
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / includes / utils.php

utils.php in Elementor Website Builder – more than just a page builder 3.20.4, at includes/utils.php

877 lines 21.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor;
3
4 use Elementor\Core\Utils\Collection;
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit; // Exit if accessed directly.
8 }
9
10 /**
11 * Elementor utils.
12 *
13 * Elementor utils handler class is responsible for different utility methods
14 * used by Elementor.
15 *
16 * @since 1.0.0
17 */
18 class Utils {
19
20 const DEPRECATION_RANGE = 0.4;
21
22 const EDITOR_BREAK_LINES_OPTION_KEY = 'elementor_editor_break_lines';
23
24 /**
25 * A list of safe tage for `validate_html_tag` method.
26 */
27 const ALLOWED_HTML_WRAPPER_TAGS = [
28 'a',
29 'article',
30 'aside',
31 'button',
32 'div',
33 'footer',
34 'h1',
35 'h2',
36 'h3',
37 'h4',
38 'h5',
39 'h6',
40 'header',
41 'main',
42 'nav',
43 'p',
44 'section',
45 'span',
46 ];
47
48 const EXTENDED_ALLOWED_HTML_TAGS = [
49 'iframe' => [
50 'iframe' => [
51 'allow' => true,
52 'allowfullscreen' => true,
53 'frameborder' => true,
54 'height' => true,
55 'loading' => true,
56 'name' => true,
57 'referrerpolicy' => true,
58 'sandbox' => true,
59 'src' => true,
60 'width' => true,
61 ],
62 ],
63 'svg' => [
64 'svg' => [
65 'aria-hidden' => true,
66 'aria-labelledby' => true,
67 'class' => true,
68 'height' => true,
69 'role' => true,
70 'viewbox' => true,
71 'width' => true,
72 'xmlns' => true,
73 ],
74 'g' => [
75 'fill' => true,
76 ],
77 'title' => [
78 'title' => true,
79 ],
80 'path' => [
81 'd' => true,
82 'fill' => true,
83 ],
84 ],
85 'image' => [
86 'img' => [
87 'srcset' => true,
88 'sizes' => true,
89 ],
90 ],
91 ];
92
93 /**
94 * Is WP CLI.
95 *
96 * @return bool
97 */
98 public static function is_wp_cli() {
99 return defined( 'WP_CLI' ) && WP_CLI;
100 }
101
102 /**
103 * Is script debug.
104 *
105 * Whether script debug is enabled or not.
106 *
107 * @since 1.0.0
108 * @access public
109 * @static
110 *
111 * @return bool True if it's a script debug is active, false otherwise.
112 */
113 public static function is_script_debug() {
114 return defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
115 }
116
117 /**
118 * Whether elementor test mode is enabled or not.
119 *
120 * @return bool
121 */
122 public static function is_elementor_tests() {
123 return defined( 'ELEMENTOR_TESTS' ) && ELEMENTOR_TESTS;
124 }
125
126 /**
127 * Get pro link.
128 *
129 * Retrieve the link to Elementor Pro.
130 *
131 * @since 1.7.0
132 * @access public
133 * @static
134 *
135 * @param string $link URL to Elementor pro.
136 *
137 * @return string Elementor pro link.
138 */
139 public static function get_pro_link( $link ) {
140 static $theme_name = false;
141
142 if ( ! $theme_name ) {
143 $theme_obj = wp_get_theme();
144 if ( $theme_obj->parent() ) {
145 $theme_name = $theme_obj->parent()->get( 'Name' );
146 } else {
147 $theme_name = $theme_obj->get( 'Name' );
148 }
149
150 $theme_name = sanitize_key( $theme_name );
151 }
152
153 $link = add_query_arg( 'utm_term', $theme_name, $link );
154
155 return $link;
156 }
157
158 /**
159 * Replace URLs.
160 *
161 * Replace old URLs to new URLs. This method also updates all the Elementor data.
162 *
163 * @since 2.1.0
164 * @static
165 * @access public
166 *
167 * @param $from
168 * @param $to
169 *
170 * @return string
171 * @throws \Exception
172 */
173 public static function replace_urls( $from, $to ) {
174 $from = trim( $from );
175 $to = trim( $to );
176
177 if ( empty( $from ) ) {
178 throw new \Exception( 'Couldn’t replace your address because the old URL was not provided. Try again by entering the old URL.' );
179 }
180
181 if ( empty( $to ) ) {
182 throw new \Exception( 'Couldn’t replace your address because the new URL was not provided. Try again by entering the new URL.' );
183 }
184
185 if ( $from === $to ) {
186 throw new \Exception( 'Couldn’t replace your address because both of the URLs provided are identical. Try again by entering different URLs.' );
187 }
188
189 $is_valid_urls = ( filter_var( $from, FILTER_VALIDATE_URL ) && filter_var( $to, FILTER_VALIDATE_URL ) );
190
191 if ( ! $is_valid_urls ) {
192 throw new \Exception( 'Couldn’t replace your address because at least one of the URLs provided are invalid. Try again by entering valid URLs.' );
193 }
194
195 global $wpdb;
196 $escaped_from = str_replace( '/', '\\/', $from );
197 $escaped_to = str_replace( '/', '\\/', $to );
198 $meta_value_like = '[%'; // meta_value LIKE '[%' are json formatted
199
200 $rows_affected = $wpdb->query(
201 $wpdb->prepare(
202 "UPDATE {$wpdb->postmeta} " .
203 'SET `meta_value` = REPLACE(`meta_value`, %s, %s) ' .
204 "WHERE `meta_key` = '_elementor_data' AND `meta_value` LIKE %s;",
205 $escaped_from,
206 $escaped_to,
207 $meta_value_like
208 )
209 );
210
211 if ( false === $rows_affected ) {
212 throw new \Exception( 'An error occurred while replacing URL\'s.' );
213 }
214
215 // Allow externals to replace-urls, when they have to.
216 $rows_affected += (int) apply_filters( 'elementor/tools/replace-urls', 0, $from, $to );
217
218 Plugin::$instance->files_manager->clear_cache();
219
220 return sprintf(
221 /* translators: %d: Number of rows. */
222 _n( '%d database row affected.', '%d database rows affected.', $rows_affected, 'elementor' ),
223 $rows_affected
224 );
225 }
226
227 /**
228 * Is post supports Elementor.
229 *
230 * Whether the post supports editing with Elementor.
231 *
232 * @since 1.0.0
233 * @access public
234 * @static
235 *
236 * @param int $post_id Optional. Post ID. Default is `0`.
237 *
238 * @return string True if post supports editing with Elementor, false otherwise.
239 */
240 public static function is_post_support( $post_id = 0 ) {
241 $post_type = get_post_type( $post_id );
242
243 $is_supported = self::is_post_type_support( $post_type );
244
245 /**
246 * Is post type support.
247 *
248 * Filters whether the post type supports editing with Elementor.
249 *
250 * @since 1.0.0
251 * @deprecated 2.2.0 Use `elementor/utils/is_post_support` hook Instead.
252 *
253 * @param bool $is_supported Whether the post type supports editing with Elementor.
254 * @param int $post_id Post ID.
255 * @param string $post_type Post type.
256 */
257 $is_supported = apply_filters( 'elementor/utils/is_post_type_support', $is_supported, $post_id, $post_type );
258
259 /**
260 * Is post support.
261 *
262 * Filters whether the post supports editing with Elementor.
263 *
264 * @since 2.2.0
265 *
266 * @param bool $is_supported Whether the post type supports editing with Elementor.
267 * @param int $post_id Post ID.
268 * @param string $post_type Post type.
269 */
270 $is_supported = apply_filters( 'elementor/utils/is_post_support', $is_supported, $post_id, $post_type );
271
272 return $is_supported;
273 }
274
275
276 /**
277 * Is post type supports Elementor.
278 *
279 * Whether the post type supports editing with Elementor.
280 *
281 * @since 2.2.0
282 * @access public
283 * @static
284 *
285 * @param string $post_type Post Type.
286 *
287 * @return string True if post type supports editing with Elementor, false otherwise.
288 */
289 public static function is_post_type_support( $post_type ) {
290 if ( ! post_type_exists( $post_type ) ) {
291 return false;
292 }
293
294 if ( ! post_type_supports( $post_type, 'elementor' ) ) {
295 return false;
296 }
297
298 return true;
299 }
300
301 /**
302 * Get placeholder image source.
303 *
304 * Retrieve the source of the placeholder image.
305 *
306 * @since 1.0.0
307 * @access public
308 * @static
309 *
310 * @return string The source of the default placeholder image used by Elementor.
311 */
312 public static function get_placeholder_image_src() {
313 $placeholder_image = ELEMENTOR_ASSETS_URL . 'images/placeholder.png';
314
315 /**
316 * Get placeholder image source.
317 *
318 * Filters the source of the default placeholder image used by Elementor.
319 *
320 * @since 1.0.0
321 *
322 * @param string $placeholder_image The source of the default placeholder image.
323 */
324 $placeholder_image = apply_filters( 'elementor/utils/get_placeholder_image_src', $placeholder_image );
325
326 return $placeholder_image;
327 }
328
329 /**
330 * Generate random string.
331 *
332 * Returns a string containing a hexadecimal representation of random number.
333 *
334 * @since 1.0.0
335 * @access public
336 * @static
337 *
338 * @return string Random string.
339 */
340 public static function generate_random_string() {
341 return dechex( rand() );
342 }
343
344 /**
345 * Do not cache.
346 *
347 * Tell WordPress cache plugins not to cache this request.
348 *
349 * @since 1.0.0
350 * @access public
351 * @static
352 */
353 public static function do_not_cache() {
354 if ( ! defined( 'DONOTCACHEPAGE' ) ) {
355 define( 'DONOTCACHEPAGE', true );
356 }
357
358 if ( ! defined( 'DONOTCACHEDB' ) ) {
359 define( 'DONOTCACHEDB', true );
360 }
361
362 if ( ! defined( 'DONOTMINIFY' ) ) {
363 define( 'DONOTMINIFY', true );
364 }
365
366 if ( ! defined( 'DONOTCDN' ) ) {
367 define( 'DONOTCDN', true );
368 }
369
370 if ( ! defined( 'DONOTCACHEOBJECT' ) ) {
371 define( 'DONOTCACHEOBJECT', true );
372 }
373
374 // Set the headers to prevent caching for the different browsers.
375 nocache_headers();
376 }
377
378 /**
379 * Get timezone string.
380 *
381 * Retrieve timezone string from the WordPress database.
382 *
383 * @since 1.0.0
384 * @access public
385 * @static
386 *
387 * @return string Timezone string.
388 */
389 public static function get_timezone_string() {
390 $current_offset = (float) get_option( 'gmt_offset' );
391 $timezone_string = get_option( 'timezone_string' );
392
393 // Create a UTC+- zone if no timezone string exists.
394 if ( empty( $timezone_string ) ) {
395 if ( $current_offset < 0 ) {
396 $timezone_string = 'UTC' . $current_offset;
397 } else {
398 $timezone_string = 'UTC+' . $current_offset;
399 }
400 }
401
402 return $timezone_string;
403 }
404
405 /**
406 * Get create new post URL.
407 *
408 * Retrieve a custom URL for creating a new post/page using Elementor.
409 *
410 * @since 1.9.0
411 * @access public
412 * @deprecated 3.3.0 Use `Plugin::$instance->documents->get_create_new_post_url()` instead.
413 * @static
414 *
415 * @param string $post_type Optional. Post type slug. Default is 'page'.
416 * @param string|null $template_type Optional. Query arg 'template_type'. Default is null.
417 *
418 * @return string A URL for creating new post using Elementor.
419 */
420 public static function get_create_new_post_url( $post_type = 'page', $template_type = null ) {
421 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __FUNCTION__, '3.3.0', 'Plugin::$instance->documents->get_create_new_post_url()' );
422
423 return Plugin::$instance->documents->get_create_new_post_url( $post_type, $template_type );
424 }
425
426 /**
427 * Get post autosave.
428 *
429 * Retrieve an autosave for any given post.
430 *
431 * @since 1.9.2
432 * @access public
433 * @static
434 *
435 * @param int $post_id Post ID.
436 * @param int $user_id Optional. User ID. Default is `0`.
437 *
438 * @return \WP_Post|false Post autosave or false.
439 */
440 public static function get_post_autosave( $post_id, $user_id = 0 ) {
441 global $wpdb;
442
443 $post = get_post( $post_id );
444
445 $where = $wpdb->prepare( 'post_parent = %d AND post_name LIKE %s AND post_modified_gmt > %s', [ $post_id, "{$post_id}-autosave%", $post->post_modified_gmt ] );
446
447 if ( $user_id ) {
448 $where .= $wpdb->prepare( ' AND post_author = %d', $user_id );
449 }
450
451 $revision = $wpdb->get_row( "SELECT * FROM $wpdb->posts WHERE $where AND post_type = 'revision'" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
452
453 if ( $revision ) {
454 $revision = new \WP_Post( $revision );
455 } else {
456 $revision = false;
457 }
458
459 return $revision;
460 }
461
462 /**
463 * Is CPT supports custom templates.
464 *
465 * Whether the Custom Post Type supports templates.
466 *
467 * @since 2.0.0
468 * @access public
469 * @static
470 *
471 * @return bool True is templates are supported, False otherwise.
472 */
473 public static function is_cpt_custom_templates_supported() {
474 require_once ABSPATH . '/wp-admin/includes/theme.php';
475
476 return method_exists( wp_get_theme(), 'get_post_templates' );
477 }
478
479 /**
480 * @since 2.1.2
481 * @access public
482 * @static
483 */
484 public static function array_inject( $array, $key, $insert ) {
485 $length = array_search( $key, array_keys( $array ), true ) + 1;
486
487 return array_slice( $array, 0, $length, true ) +
488 $insert +
489 array_slice( $array, $length, null, true );
490 }
491
492 /**
493 * Render html attributes
494 *
495 * @access public
496 * @static
497 * @param array $attributes
498 *
499 * @return string
500 */
501 public static function render_html_attributes( array $attributes ) {
502 $rendered_attributes = [];
503
504 foreach ( $attributes as $attribute_key => $attribute_values ) {
505 if ( is_array( $attribute_values ) ) {
506 $attribute_values = implode( ' ', $attribute_values );
507 }
508
509 $rendered_attributes[] = sprintf( '%1$s="%2$s"', $attribute_key, esc_attr( $attribute_values ) );
510 }
511
512 return implode( ' ', $rendered_attributes );
513 }
514
515 /**
516 * Safe print html attributes
517 *
518 * @access public
519 * @static
520 * @param array $attributes
521 */
522 public static function print_html_attributes( array $attributes ) {
523 // PHPCS - the method render_html_attributes is safe.
524 echo self::render_html_attributes( $attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
525 }
526
527 public static function get_meta_viewport( $context = '' ) {
528 $meta_tag = '<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" />';
529
530 /**
531 * Viewport meta tag.
532 *
533 * Filters the meta tag containing the viewport information.
534 *
535 * This hook can be used to change the intial viewport meta tag set by Elementor
536 * and replace it with a different viewport tag.
537 *
538 * @since 2.5.0
539 *
540 * @param string $meta_tag Viewport meta tag.
541 * @param string $context Page context.
542 */
543 $meta_tag = apply_filters( 'elementor/template/viewport_tag', $meta_tag, $context );
544
545 return $meta_tag;
546 }
547
548 /**
549 * Add Elementor Config js vars to the relevant script handle,
550 * WP will wrap it with <script> tag.
551 * To make sure this script runs thru the `script_loader_tag` hook, use a known handle value.
552 * @param string $handle
553 * @param string $js_var
554 * @param mixed $config
555 */
556 public static function print_js_config( $handle, $js_var, $config ) {
557 $config = wp_json_encode( $config );
558
559 if ( get_option( self::EDITOR_BREAK_LINES_OPTION_KEY ) ) {
560 // Add new lines to avoid memory limits in some hosting servers that handles the buffer output according to new line characters
561 $config = str_replace( '}},"', '}},' . PHP_EOL . '"', $config );
562 }
563
564 $script_data = 'var ' . $js_var . ' = ' . $config . ';';
565
566 wp_add_inline_script( $handle, $script_data, 'before' );
567 }
568
569 public static function handle_deprecation( $item, $version, $replacement = null ) {
570 preg_match( '/^[0-9]+\.[0-9]+/', ELEMENTOR_VERSION, $current_version );
571
572 $current_version_as_float = (float) $current_version[0];
573
574 preg_match( '/^[0-9]+\.[0-9]+/', $version, $alias_version );
575
576 $alias_version_as_float = (float) $alias_version[0];
577
578 if ( round( $current_version_as_float - $alias_version_as_float, 1 ) >= self::DEPRECATION_RANGE ) {
579 _deprecated_file( $item, $version, $replacement ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
580 }
581 }
582
583 /**
584 * Checks a control value for being empty, including a string of '0' not covered by PHP's empty().
585 *
586 * @param mixed $source
587 * @param bool|string $key
588 *
589 * @return bool
590 */
591 public static function is_empty( $source, $key = false ) {
592 if ( is_array( $source ) ) {
593 if ( ! isset( $source[ $key ] ) ) {
594 return true;
595 }
596
597 $source = $source[ $key ];
598 }
599
600 return '0' !== $source && empty( $source );
601 }
602
603 public static function has_pro() {
604 return defined( 'ELEMENTOR_PRO_VERSION' );
605 }
606
607 /**
608 * Convert HTMLEntities to UTF-8 characters
609 *
610 * @param $string
611 * @return string
612 */
613 public static function urlencode_html_entities( $string ) {
614 $entities_dictionary = [
615 '&#145;' => "'", // Opening single quote
616 '&#146;' => "'", // Closing single quote
617 '&#147;' => '"', // Closing double quote
618 '&#148;' => '"', // Opening double quote
619 '&#8216;' => "'", // Closing single quote
620 '&#8217;' => "'", // Opening single quote
621 '&#8218;' => "'", // Single low quote
622 '&#8220;' => '"', // Closing double quote
623 '&#8221;' => '"', // Opening double quote
624 '&#8222;' => '"', // Double low quote
625 ];
626
627 // Decode decimal entities
628 $string = str_replace( array_keys( $entities_dictionary ), array_values( $entities_dictionary ), $string );
629
630 return rawurlencode( html_entity_decode( $string, ENT_QUOTES | ENT_HTML5, 'UTF-8' ) );
631 }
632
633 /**
634 * Parse attributes that come as a string of comma-delimited key|value pairs.
635 * Removes Javascript events and unescaped `href` attributes.
636 *
637 * @param string $attributes_string
638 *
639 * @param string $delimiter Default comma `,`.
640 *
641 * @return array
642 */
643 public static function parse_custom_attributes( $attributes_string, $delimiter = ',' ) {
644 $attributes = explode( $delimiter, $attributes_string );
645 $result = [];
646
647 foreach ( $attributes as $attribute ) {
648 $attr_key_value = explode( '|', $attribute );
649
650 $attr_key = mb_strtolower( $attr_key_value[0] );
651
652 // Remove any not allowed characters.
653 preg_match( '/[-_a-z0-9]+/', $attr_key, $attr_key_matches );
654
655 if ( empty( $attr_key_matches[0] ) ) {
656 continue;
657 }
658
659 $attr_key = $attr_key_matches[0];
660
661 // Avoid Javascript events and unescaped href.
662 if ( 'href' === $attr_key || 'on' === substr( $attr_key, 0, 2 ) ) {
663 continue;
664 }
665
666 if ( isset( $attr_key_value[1] ) ) {
667 $attr_value = trim( $attr_key_value[1] );
668 } else {
669 $attr_value = '';
670 }
671
672 $result[ $attr_key ] = $attr_value;
673 }
674
675 return $result;
676 }
677
678 public static function find_element_recursive( $elements, $id ) {
679 foreach ( $elements as $element ) {
680 if ( $id === $element['id'] ) {
681 return $element;
682 }
683
684 if ( ! empty( $element['elements'] ) ) {
685 $element = self::find_element_recursive( $element['elements'], $id );
686
687 if ( $element ) {
688 return $element;
689 }
690 }
691 }
692
693 return false;
694 }
695
696 /**
697 * Change Submenu First Item Label
698 *
699 * Overwrite the label of the first submenu item of an admin menu item.
700 *
701 * Fired by `admin_menu` action.
702 *
703 * @since 3.1.0
704 *
705 * @param $menu_slug
706 * @param $new_label
707 * @access public
708 */
709 public static function change_submenu_first_item_label( $menu_slug, $new_label ) {
710 global $submenu;
711
712 if ( isset( $submenu[ $menu_slug ] ) ) {
713 // @codingStandardsIgnoreStart
714 $submenu[ $menu_slug ][0][0] = $new_label;
715 // @codingStandardsIgnoreEnd
716 }
717 }
718
719 /**
720 * Validate an HTML tag against a safe allowed list.
721 *
722 * @param string $tag
723 *
724 * @return string
725 */
726 public static function validate_html_tag( $tag ) {
727 return $tag && in_array( strtolower( $tag ), self::ALLOWED_HTML_WRAPPER_TAGS ) ? $tag : 'div';
728 }
729
730 /**
731 * Safe print a validated HTML tag.
732 *
733 * @param string $tag
734 */
735 public static function print_validated_html_tag( $tag ) {
736 // PHPCS - the method validate_html_tag is safe.
737 echo self::validate_html_tag( $tag ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
738 }
739
740 /**
741 * Print internal content (not user input) without escaping.
742 */
743 public static function print_unescaped_internal_string( $string ) {
744 echo $string; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
745 }
746
747 /**
748 * Get recently edited posts query.
749 *
750 * Returns `WP_Query` of the recent edited posts.
751 * By default max posts ( $args['posts_per_page'] ) is 3.
752 *
753 * @param array $args
754 *
755 * @return \WP_Query
756 */
757 public static function get_recently_edited_posts_query( $args = [] ) {
758 $args = wp_parse_args( $args, [
759 'no_found_rows' => true,
760 'post_type' => 'any',
761 'post_status' => [ 'publish', 'draft' ],
762 'posts_per_page' => '3',
763 'meta_key' => '_elementor_edit_mode',
764 'meta_value' => 'builder',
765 'orderby' => 'modified',
766 ] );
767
768 return new \WP_Query( $args );
769 }
770
771 public static function print_wp_kses_extended( $string, array $tags ) {
772 $allowed_html = wp_kses_allowed_html( 'post' );
773
774 foreach ( $tags as $tag ) {
775 if ( isset( self::EXTENDED_ALLOWED_HTML_TAGS[ $tag ] ) ) {
776 $extended_tags = apply_filters( "elementor/extended_allowed_html_tags/{$tag}", self::EXTENDED_ALLOWED_HTML_TAGS[ $tag ] );
777 $allowed_html = array_replace_recursive( $allowed_html, $extended_tags );
778 }
779 }
780
781 echo wp_kses( $string, $allowed_html );
782 }
783
784 public static function is_elementor_path( $path ) {
785 $path = wp_normalize_path( $path );
786
787 /**
788 * Elementor related paths.
789 *
790 * Filters Elementor related paths.
791 *
792 * @param string[] $available_paths
793 */
794 $available_paths = apply_filters( 'elementor/utils/elementor_related_paths', [ ELEMENTOR_PATH ] );
795
796 return (bool) ( new Collection( $available_paths ) )
797 ->map( function ( $p ) {
798 // `untrailingslashit` in order to include other plugins prefixed with elementor.
799 return untrailingslashit( wp_normalize_path( $p ) );
800 } )
801 ->find(function ( $p ) use ( $path ) {
802 return false !== strpos( $path, $p );
803 } );
804 }
805
806 /**
807 * @param $file
808 * @param mixed ...$args
809 * @return false|string
810 */
811 public static function file_get_contents( $file, ...$args ) {
812 if ( ! is_file( $file ) || ! is_readable( $file ) ) {
813 return false;
814 }
815
816 return file_get_contents( $file, ...$args );
817 }
818
819 public static function get_super_global_value( $super_global, $key ) {
820 if ( ! isset( $super_global[ $key ] ) ) {
821 return null;
822 }
823
824 if ( $_FILES === $super_global ) {
825 return isset( $super_global[ $key ]['name'] ) ?
826 self::sanitize_file_name( $super_global[ $key ] ) :
827 self::sanitize_multi_upload( $super_global[ $key ] );
828 }
829
830 return wp_kses_post_deep( wp_unslash( $super_global[ $key ] ) );
831 }
832
833 private static function sanitize_multi_upload( $fields ) {
834 return array_map( function( $field ) {
835 return array_map( 'self::sanitize_file_name', $field );
836 }, $fields );
837 }
838
839 private static function sanitize_file_name( $file ) {
840 $file['name'] = sanitize_file_name( $file['name'] );
841
842 return $file;
843 }
844
845 /**
846 * Return specific object property value if exist from array of keys.
847 *
848 * @param $array
849 * @param $keys
850 * @return key|false
851 */
852 public static function get_array_value_by_keys( $array, $keys ) {
853 $keys = (array) $keys;
854 foreach ( $keys as $key ) {
855 if ( ! isset( $array[ $key ] ) ) {
856 return null;
857 }
858 $array = $array[ $key ];
859 }
860 return $array;
861 }
862
863 public static function get_cached_callback( $callback, $cache_key, $cache_time = 24 * HOUR_IN_SECONDS ) {
864 $cache = get_site_transient( $cache_key );
865
866 if ( ! $cache ) {
867 $cache = call_user_func( $callback );
868
869 if ( ! is_wp_error( $cache ) ) {
870 set_site_transient( $cache_key, $cache, $cache_time );
871 }
872 }
873
874 return $cache;
875 }
876 }
877