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

716 lines 17.4 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 if ( ! defined( 'ABSPATH' ) ) {
5 exit; // Exit if accessed directly.
6 }
7
8 /**
9 * Elementor utils.
10 *
11 * Elementor utils handler class is responsible for different utility methods
12 * used by Elementor.
13 *
14 * @since 1.0.0
15 */
16 class Utils {
17
18 const DEPRECATION_RANGE = 0.4;
19
20 /**
21 * A list of safe tage for `validate_html_tag` method.
22 */
23 const ALLOWED_HTML_WRAPPER_TAGS = [
24 'a',
25 'article',
26 'aside',
27 'div',
28 'footer',
29 'h1',
30 'h2',
31 'h3',
32 'h4',
33 'h5',
34 'h6',
35 'header',
36 'main',
37 'nav',
38 'p',
39 'section',
40 'span',
41 ];
42
43 /**
44 * Is ajax.
45 *
46 * Whether the current request is a WordPress ajax request.
47 *
48 * @since 1.0.0
49 * @deprecated 2.6.0 Use `wp_doing_ajax()` instead.
50 * @access public
51 * @static
52 *
53 * @return bool True if it's a WordPress ajax request, false otherwise.
54 */
55 public static function is_ajax() {
56 _deprecated_function( __METHOD__, '2.6.0', 'wp_doing_ajax()' );
57
58 return wp_doing_ajax();
59 }
60
61 /**
62 * Is WP CLI.
63 *
64 * @return bool
65 */
66 public static function is_wp_cli() {
67 return defined( 'WP_CLI' ) && WP_CLI;
68 }
69
70 /**
71 * Is script debug.
72 *
73 * Whether script debug is enabled or not.
74 *
75 * @since 1.0.0
76 * @access public
77 * @static
78 *
79 * @return bool True if it's a script debug is active, false otherwise.
80 */
81 public static function is_script_debug() {
82 return defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG;
83 }
84
85 /**
86 * Get pro link.
87 *
88 * Retrieve the link to Elementor Pro.
89 *
90 * @since 1.7.0
91 * @access public
92 * @static
93 *
94 * @param string $link URL to Elementor pro.
95 *
96 * @return string Elementor pro link.
97 */
98 public static function get_pro_link( $link ) {
99 static $theme_name = false;
100
101 if ( ! $theme_name ) {
102 $theme_obj = wp_get_theme();
103 if ( $theme_obj->parent() ) {
104 $theme_name = $theme_obj->parent()->get( 'Name' );
105 } else {
106 $theme_name = $theme_obj->get( 'Name' );
107 }
108
109 $theme_name = sanitize_key( $theme_name );
110 }
111
112 $link = add_query_arg( 'utm_term', $theme_name, $link );
113
114 return $link;
115 }
116
117 /**
118 * Replace URLs.
119 *
120 * Replace old URLs to new URLs. This method also updates all the Elementor data.
121 *
122 * @since 2.1.0
123 * @static
124 * @access public
125 *
126 * @param $from
127 * @param $to
128 *
129 * @return string
130 * @throws \Exception
131 */
132 public static function replace_urls( $from, $to ) {
133 $from = trim( $from );
134 $to = trim( $to );
135
136 if ( $from === $to ) {
137 throw new \Exception( __( 'The `from` and `to` URL\'s must be different', 'elementor' ) );
138 }
139
140 $is_valid_urls = ( filter_var( $from, FILTER_VALIDATE_URL ) && filter_var( $to, FILTER_VALIDATE_URL ) );
141 if ( ! $is_valid_urls ) {
142 throw new \Exception( __( 'The `from` and `to` URL\'s must be valid URL\'s', 'elementor' ) );
143 }
144
145 global $wpdb;
146
147 // @codingStandardsIgnoreStart cannot use `$wpdb->prepare` because it remove's the backslashes
148 $rows_affected = $wpdb->query(
149 "UPDATE {$wpdb->postmeta} " .
150 "SET `meta_value` = REPLACE(`meta_value`, '" . str_replace( '/', '\\\/', $from ) . "', '" . str_replace( '/', '\\\/', $to ) . "') " .
151 "WHERE `meta_key` = '_elementor_data' AND `meta_value` LIKE '[%' ;" ); // meta_value LIKE '[%' are json formatted
152 // @codingStandardsIgnoreEnd
153
154 if ( false === $rows_affected ) {
155 throw new \Exception( __( 'An error occurred', 'elementor' ) );
156 }
157
158 // Allow externals to replace-urls, when they have to.
159 $rows_affected += (int) apply_filters( 'elementor/tools/replace-urls', 0, $from, $to );
160
161 Plugin::$instance->files_manager->clear_cache();
162
163 return sprintf(
164 /* translators: %d: Number of rows */
165 _n( '%d row affected.', '%d rows affected.', $rows_affected, 'elementor' ),
166 $rows_affected
167 );
168 }
169
170 /**
171 * Is post supports Elementor.
172 *
173 * Whether the post supports editing with Elementor.
174 *
175 * @since 1.0.0
176 * @access public
177 * @static
178 *
179 * @param int $post_id Optional. Post ID. Default is `0`.
180 *
181 * @return string True if post supports editing with Elementor, false otherwise.
182 */
183 public static function is_post_support( $post_id = 0 ) {
184 $post_type = get_post_type( $post_id );
185
186 $is_supported = self::is_post_type_support( $post_type );
187
188 /**
189 * Is post type support.
190 *
191 * Filters whether the post type supports editing with Elementor.
192 *
193 * @since 1.0.0
194 * @deprecated 2.2.0 Use `elementor/utils/is_post_support` Instead
195 *
196 * @param bool $is_supported Whether the post type supports editing with Elementor.
197 * @param int $post_id Post ID.
198 * @param string $post_type Post type.
199 */
200 $is_supported = apply_filters( 'elementor/utils/is_post_type_support', $is_supported, $post_id, $post_type );
201
202 /**
203 * Is post support.
204 *
205 * Filters whether the post supports editing with Elementor.
206 *
207 * @since 2.2.0
208 *
209 * @param bool $is_supported Whether the post type supports editing with Elementor.
210 * @param int $post_id Post ID.
211 * @param string $post_type Post type.
212 */
213 $is_supported = apply_filters( 'elementor/utils/is_post_support', $is_supported, $post_id, $post_type );
214
215 return $is_supported;
216 }
217
218
219 /**
220 * Is post type supports Elementor.
221 *
222 * Whether the post type supports editing with Elementor.
223 *
224 * @since 2.2.0
225 * @access public
226 * @static
227 *
228 * @param string $post_type Post Type.
229 *
230 * @return string True if post type supports editing with Elementor, false otherwise.
231 */
232 public static function is_post_type_support( $post_type ) {
233 if ( ! post_type_exists( $post_type ) ) {
234 return false;
235 }
236
237 if ( ! post_type_supports( $post_type, 'elementor' ) ) {
238 return false;
239 }
240
241 return true;
242 }
243
244 /**
245 * Get placeholder image source.
246 *
247 * Retrieve the source of the placeholder image.
248 *
249 * @since 1.0.0
250 * @access public
251 * @static
252 *
253 * @return string The source of the default placeholder image used by Elementor.
254 */
255 public static function get_placeholder_image_src() {
256 $placeholder_image = ELEMENTOR_ASSETS_URL . 'images/placeholder.png';
257
258 /**
259 * Get placeholder image source.
260 *
261 * Filters the source of the default placeholder image used by Elementor.
262 *
263 * @since 1.0.0
264 *
265 * @param string $placeholder_image The source of the default placeholder image.
266 */
267 $placeholder_image = apply_filters( 'elementor/utils/get_placeholder_image_src', $placeholder_image );
268
269 return $placeholder_image;
270 }
271
272 /**
273 * Generate random string.
274 *
275 * Returns a string containing a hexadecimal representation of random number.
276 *
277 * @since 1.0.0
278 * @access public
279 * @static
280 *
281 * @return string Random string.
282 */
283 public static function generate_random_string() {
284 return dechex( rand() );
285 }
286
287 /**
288 * Do not cache.
289 *
290 * Tell WordPress cache plugins not to cache this request.
291 *
292 * @since 1.0.0
293 * @access public
294 * @static
295 */
296 public static function do_not_cache() {
297 if ( ! defined( 'DONOTCACHEPAGE' ) ) {
298 define( 'DONOTCACHEPAGE', true );
299 }
300
301 if ( ! defined( 'DONOTCACHEDB' ) ) {
302 define( 'DONOTCACHEDB', true );
303 }
304
305 if ( ! defined( 'DONOTMINIFY' ) ) {
306 define( 'DONOTMINIFY', true );
307 }
308
309 if ( ! defined( 'DONOTCDN' ) ) {
310 define( 'DONOTCDN', true );
311 }
312
313 if ( ! defined( 'DONOTCACHCEOBJECT' ) ) {
314 define( 'DONOTCACHCEOBJECT', true );
315 }
316
317 // Set the headers to prevent caching for the different browsers.
318 nocache_headers();
319 }
320
321 /**
322 * Get timezone string.
323 *
324 * Retrieve timezone string from the WordPress database.
325 *
326 * @since 1.0.0
327 * @access public
328 * @static
329 *
330 * @return string Timezone string.
331 */
332 public static function get_timezone_string() {
333 $current_offset = (float) get_option( 'gmt_offset' );
334 $timezone_string = get_option( 'timezone_string' );
335
336 // Create a UTC+- zone if no timezone string exists.
337 if ( empty( $timezone_string ) ) {
338 if ( $current_offset < 0 ) {
339 $timezone_string = 'UTC' . $current_offset;
340 } else {
341 $timezone_string = 'UTC+' . $current_offset;
342 }
343 }
344
345 return $timezone_string;
346 }
347
348 /**
349 * Get create new post URL.
350 *
351 * Retrieve a custom URL for creating a new post/page using Elementor.
352 *
353 * @since 1.9.0
354 * @access public
355 * @static
356 *
357 * @param string $post_type Optional. Post type slug. Default is 'page'.
358 * @param string|null $template_type Optional. Query arg 'template_type'. Default is null.
359 *
360 * @return string A URL for creating new post using Elementor.
361 */
362 public static function get_create_new_post_url( $post_type = 'page', $template_type = null ) {
363 $query_args = [
364 'action' => 'elementor_new_post',
365 'post_type' => $post_type,
366 ];
367
368 if ( $template_type ) {
369 $query_args['template_type'] = $template_type;
370 }
371
372 $new_post_url = add_query_arg( $query_args, admin_url( 'edit.php' ) );
373
374 $new_post_url = add_query_arg( '_wpnonce', wp_create_nonce( 'elementor_action_new_post' ), $new_post_url );
375
376 return $new_post_url;
377 }
378
379 /**
380 * Get post autosave.
381 *
382 * Retrieve an autosave for any given post.
383 *
384 * @since 1.9.2
385 * @access public
386 * @static
387 *
388 * @param int $post_id Post ID.
389 * @param int $user_id Optional. User ID. Default is `0`.
390 *
391 * @return \WP_Post|false Post autosave or false.
392 */
393 public static function get_post_autosave( $post_id, $user_id = 0 ) {
394 global $wpdb;
395
396 $post = get_post( $post_id );
397
398 $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 ] );
399
400 if ( $user_id ) {
401 $where .= $wpdb->prepare( ' AND post_author = %d', $user_id );
402 }
403
404 $revision = $wpdb->get_row( "SELECT * FROM $wpdb->posts WHERE $where AND post_type = 'revision'" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
405
406 if ( $revision ) {
407 $revision = new \WP_Post( $revision );
408 } else {
409 $revision = false;
410 }
411
412 return $revision;
413 }
414
415 /**
416 * Is CPT supports custom templates.
417 *
418 * Whether the Custom Post Type supports templates.
419 *
420 * @since 2.0.0
421 * @access public
422 * @static
423 *
424 * @return bool True is templates are supported, False otherwise.
425 */
426 public static function is_cpt_custom_templates_supported() {
427 require_once ABSPATH . '/wp-admin/includes/theme.php';
428
429 return method_exists( wp_get_theme(), 'get_post_templates' );
430 }
431
432 /**
433 * @since 2.1.2
434 * @access public
435 * @static
436 */
437 public static function array_inject( $array, $key, $insert ) {
438 $length = array_search( $key, array_keys( $array ), true ) + 1;
439
440 return array_slice( $array, 0, $length, true ) +
441 $insert +
442 array_slice( $array, $length, null, true );
443 }
444
445 /**
446 * Render html attributes
447 *
448 * @access public
449 * @static
450 * @param array $attributes
451 *
452 * @return string
453 */
454 public static function render_html_attributes( array $attributes ) {
455 $rendered_attributes = [];
456
457 foreach ( $attributes as $attribute_key => $attribute_values ) {
458 if ( is_array( $attribute_values ) ) {
459 $attribute_values = implode( ' ', $attribute_values );
460 }
461
462 $rendered_attributes[] = sprintf( '%1$s="%2$s"', $attribute_key, esc_attr( $attribute_values ) );
463 }
464
465 return implode( ' ', $rendered_attributes );
466 }
467
468 /**
469 * Safe print html attributes
470 *
471 * @access public
472 * @static
473 * @param array $attributes
474 */
475 public static function print_html_attributes( array $attributes ) {
476 // PHPCS - the method render_html_attributes is safe.
477 echo self::render_html_attributes( $attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
478 }
479
480 public static function get_meta_viewport( $context = '' ) {
481 $meta_tag = '<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" />';
482 /**
483 * Viewport meta tag.
484 *
485 * Filters the Elementor preview URL.
486 *
487 * @since 2.5.0
488 *
489 * @param string $meta_tag Viewport meta tag.
490 */
491 return apply_filters( 'elementor/template/viewport_tag', $meta_tag, $context );
492 }
493
494 /**
495 * Add Elementor Config js vars to the relevant script handle,
496 * WP will wrap it with <script> tag.
497 * To make sure this script runs thru the `script_loader_tag` hook, use a known handle value.
498 * @param string $handle
499 * @param string $js_var
500 * @param mixed $config
501 */
502 public static function print_js_config( $handle, $js_var, $config ) {
503 $config = wp_json_encode( $config );
504
505 if ( get_option( 'elementor_editor_break_lines' ) ) {
506 // Add new lines to avoid memory limits in some hosting servers that handles the buffer output according to new line characters
507 $config = str_replace( '}},"', '}},' . PHP_EOL . '"', $config );
508 }
509
510 $script_data = 'var ' . $js_var . ' = ' . $config . ';';
511
512 wp_add_inline_script( $handle, $script_data, 'before' );
513 }
514
515 public static function handle_deprecation( $item, $version, $replacement = null ) {
516 preg_match( '/^[0-9]+\.[0-9]+/', ELEMENTOR_VERSION, $current_version );
517
518 $current_version_as_float = (float) $current_version[0];
519
520 preg_match( '/^[0-9]+\.[0-9]+/', $version, $alias_version );
521
522 $alias_version_as_float = (float) $alias_version[0];
523
524 if ( round( $current_version_as_float - $alias_version_as_float, 1 ) >= self::DEPRECATION_RANGE ) {
525 _deprecated_file( $item, $version, $replacement );
526 }
527 }
528
529 /**
530 * Checks a control value for being empty, including a string of '0' not covered by PHP's empty().
531 *
532 * @param mixed $source
533 * @param bool|string $key
534 *
535 * @return bool
536 */
537 public static function is_empty( $source, $key = false ) {
538 if ( is_array( $source ) ) {
539 if ( ! isset( $source[ $key ] ) ) {
540 return true;
541 }
542
543 $source = $source[ $key ];
544 }
545
546 return '0' !== $source && empty( $source );
547 }
548
549 public static function has_pro() {
550 return defined( 'ELEMENTOR_PRO_VERSION' );
551 }
552
553 /**
554 * Convert HTMLEntities to UTF-8 characters
555 *
556 * @param $string
557 * @return string
558 */
559 public static function urlencode_html_entities( $string ) {
560 $entities_dictionary = [
561 '&#145;' => "'", // Opening single quote
562 '&#146;' => "'", // Closing single quote
563 '&#147;' => '"', // Closing double quote
564 '&#148;' => '"', // Opening double quote
565 '&#8216;' => "'", // Closing single quote
566 '&#8217;' => "'", // Opening single quote
567 '&#8218;' => "'", // Single low quote
568 '&#8220;' => '"', // Closing double quote
569 '&#8221;' => '"', // Opening double quote
570 '&#8222;' => '"', // Double low quote
571 ];
572
573 // Decode decimal entities
574 $string = str_replace( array_keys( $entities_dictionary ), array_values( $entities_dictionary ), $string );
575
576 return rawurlencode( html_entity_decode( $string, ENT_QUOTES | ENT_HTML5, 'UTF-8' ) );
577 }
578
579 /**
580 * Parse attributes that come as a string of comma-delimited key|value pairs.
581 * Removes Javascript events and unescaped `href` attributes.
582 *
583 * @param string $attributes_string
584 *
585 * @param string $delimiter Default comma `,`.
586 *
587 * @return array
588 */
589 public static function parse_custom_attributes( $attributes_string, $delimiter = ',' ) {
590 $attributes = explode( $delimiter, $attributes_string );
591 $result = [];
592
593 foreach ( $attributes as $attribute ) {
594 $attr_key_value = explode( '|', $attribute );
595
596 $attr_key = mb_strtolower( $attr_key_value[0] );
597
598 // Remove any not allowed characters.
599 preg_match( '/[-_a-z0-9]+/', $attr_key, $attr_key_matches );
600
601 if ( empty( $attr_key_matches[0] ) ) {
602 continue;
603 }
604
605 $attr_key = $attr_key_matches[0];
606
607 // Avoid Javascript events and unescaped href.
608 if ( 'href' === $attr_key || 'on' === substr( $attr_key, 0, 2 ) ) {
609 continue;
610 }
611
612 if ( isset( $attr_key_value[1] ) ) {
613 $attr_value = trim( $attr_key_value[1] );
614 } else {
615 $attr_value = '';
616 }
617
618 $result[ $attr_key ] = $attr_value;
619 }
620
621 return $result;
622 }
623
624 public static function find_element_recursive( $elements, $id ) {
625 foreach ( $elements as $element ) {
626 if ( $id === $element['id'] ) {
627 return $element;
628 }
629
630 if ( ! empty( $element['elements'] ) ) {
631 $element = self::find_element_recursive( $element['elements'], $id );
632
633 if ( $element ) {
634 return $element;
635 }
636 }
637 }
638
639 return false;
640 }
641
642 /**
643 * Change Submenu First Item Label
644 *
645 * Overwrite the label of the first submenu item of an admin menu item.
646 *
647 * Fired by `admin_menu` action.
648 *
649 * @since 3.1.0
650 *
651 * @param $menu_slug
652 * @param $new_label
653 * @access public
654 */
655 public static function change_submenu_first_item_label( $menu_slug, $new_label ) {
656 global $submenu;
657
658 if ( isset( $submenu[ $menu_slug ] ) ) {
659 // @codingStandardsIgnoreStart
660 $submenu[ $menu_slug ][0][0] = $new_label;
661 // @codingStandardsIgnoreEnd
662 }
663 }
664
665 /**
666 * Validate an HTML tag against a safe allowed list.
667 *
668 * @param string $tag
669 *
670 * @return string
671 */
672 public static function validate_html_tag( $tag ) {
673 return in_array( strtolower( $tag ), self::ALLOWED_HTML_WRAPPER_TAGS ) ? $tag : 'div';
674 }
675
676 /**
677 * Safe print a validated HTML tag.
678 *
679 * @param string $tag
680 */
681 public static function print_validated_html_tag( $tag ) {
682 // PHPCS - the method validate_html_tag is safe.
683 echo self::validate_html_tag( $tag ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
684 }
685
686 /**
687 * Print internal content (not user input) without escaping.
688 */
689 public static function print_unescaped_internal_string( $string ) {
690 echo $string; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
691 }
692
693 /**
694 * Get recently edited posts query.
695 *
696 * Returns `WP_Query` of the recent edited posts.
697 * By default max posts ( $args['posts_per_page'] ) is 3.
698 *
699 * @param array $args
700 *
701 * @return \WP_Query
702 */
703 public static function get_recently_edited_posts_query( $args = [] ) {
704 $args = wp_parse_args( $args, [
705 'post_type' => 'any',
706 'post_status' => [ 'publish', 'draft' ],
707 'posts_per_page' => '3',
708 'meta_key' => '_elementor_edit_mode',
709 'meta_value' => 'builder',
710 'orderby' => 'modified',
711 ] );
712
713 return new \WP_Query( $args );
714 }
715 }
716