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

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