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

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