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

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