PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta1
Elementor Website Builder – more than just a page builder v4.3.0-beta1
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 4.3.0-beta1, at includes/utils.php

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