PluginProbe
Elementor Website Builder – more than just a page builder / 3.25.8
Elementor Website Builder – more than just a page builder v3.25.8
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.25.8, at includes/utils.php

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