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

705 lines 17.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 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 Plugin::$instance->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_function( __FUNCTION__, '3.3.0', 'Plugin::$instance->documents->get_create_new_post_url()' );
364
365 return Plugin::$instance->documents->get_create_new_post_url( $post_type, $template_type );
366 }
367
368 /**
369 * Get post autosave.
370 *
371 * Retrieve an autosave for any given post.
372 *
373 * @since 1.9.2
374 * @access public
375 * @static
376 *
377 * @param int $post_id Post ID.
378 * @param int $user_id Optional. User ID. Default is `0`.
379 *
380 * @return \WP_Post|false Post autosave or false.
381 */
382 public static function get_post_autosave( $post_id, $user_id = 0 ) {
383 global $wpdb;
384
385 $post = get_post( $post_id );
386
387 $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 ] );
388
389 if ( $user_id ) {
390 $where .= $wpdb->prepare( ' AND post_author = %d', $user_id );
391 }
392
393 $revision = $wpdb->get_row( "SELECT * FROM $wpdb->posts WHERE $where AND post_type = 'revision'" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
394
395 if ( $revision ) {
396 $revision = new \WP_Post( $revision );
397 } else {
398 $revision = false;
399 }
400
401 return $revision;
402 }
403
404 /**
405 * Is CPT supports custom templates.
406 *
407 * Whether the Custom Post Type supports templates.
408 *
409 * @since 2.0.0
410 * @access public
411 * @static
412 *
413 * @return bool True is templates are supported, False otherwise.
414 */
415 public static function is_cpt_custom_templates_supported() {
416 require_once ABSPATH . '/wp-admin/includes/theme.php';
417
418 return method_exists( wp_get_theme(), 'get_post_templates' );
419 }
420
421 /**
422 * @since 2.1.2
423 * @access public
424 * @static
425 */
426 public static function array_inject( $array, $key, $insert ) {
427 $length = array_search( $key, array_keys( $array ), true ) + 1;
428
429 return array_slice( $array, 0, $length, true ) +
430 $insert +
431 array_slice( $array, $length, null, true );
432 }
433
434 /**
435 * Render html attributes
436 *
437 * @access public
438 * @static
439 * @param array $attributes
440 *
441 * @return string
442 */
443 public static function render_html_attributes( array $attributes ) {
444 $rendered_attributes = [];
445
446 foreach ( $attributes as $attribute_key => $attribute_values ) {
447 if ( is_array( $attribute_values ) ) {
448 $attribute_values = implode( ' ', $attribute_values );
449 }
450
451 $rendered_attributes[] = sprintf( '%1$s="%2$s"', $attribute_key, esc_attr( $attribute_values ) );
452 }
453
454 return implode( ' ', $rendered_attributes );
455 }
456
457 /**
458 * Safe print html attributes
459 *
460 * @access public
461 * @static
462 * @param array $attributes
463 */
464 public static function print_html_attributes( array $attributes ) {
465 // PHPCS - the method render_html_attributes is safe.
466 echo self::render_html_attributes( $attributes ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
467 }
468
469 public static function get_meta_viewport( $context = '' ) {
470 $meta_tag = '<meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover" />';
471 /**
472 * Viewport meta tag.
473 *
474 * Filters the Elementor preview URL.
475 *
476 * @since 2.5.0
477 *
478 * @param string $meta_tag Viewport meta tag.
479 */
480 return apply_filters( 'elementor/template/viewport_tag', $meta_tag, $context );
481 }
482
483 /**
484 * Add Elementor Config js vars to the relevant script handle,
485 * WP will wrap it with <script> tag.
486 * To make sure this script runs thru the `script_loader_tag` hook, use a known handle value.
487 * @param string $handle
488 * @param string $js_var
489 * @param mixed $config
490 */
491 public static function print_js_config( $handle, $js_var, $config ) {
492 $config = wp_json_encode( $config );
493
494 if ( get_option( 'elementor_editor_break_lines' ) ) {
495 // Add new lines to avoid memory limits in some hosting servers that handles the buffer output according to new line characters
496 $config = str_replace( '}},"', '}},' . PHP_EOL . '"', $config );
497 }
498
499 $script_data = 'var ' . $js_var . ' = ' . $config . ';';
500
501 wp_add_inline_script( $handle, $script_data, 'before' );
502 }
503
504 public static function handle_deprecation( $item, $version, $replacement = null ) {
505 preg_match( '/^[0-9]+\.[0-9]+/', ELEMENTOR_VERSION, $current_version );
506
507 $current_version_as_float = (float) $current_version[0];
508
509 preg_match( '/^[0-9]+\.[0-9]+/', $version, $alias_version );
510
511 $alias_version_as_float = (float) $alias_version[0];
512
513 if ( round( $current_version_as_float - $alias_version_as_float, 1 ) >= self::DEPRECATION_RANGE ) {
514 _deprecated_file( $item, $version, $replacement );
515 }
516 }
517
518 /**
519 * Checks a control value for being empty, including a string of '0' not covered by PHP's empty().
520 *
521 * @param mixed $source
522 * @param bool|string $key
523 *
524 * @return bool
525 */
526 public static function is_empty( $source, $key = false ) {
527 if ( is_array( $source ) ) {
528 if ( ! isset( $source[ $key ] ) ) {
529 return true;
530 }
531
532 $source = $source[ $key ];
533 }
534
535 return '0' !== $source && empty( $source );
536 }
537
538 public static function has_pro() {
539 return defined( 'ELEMENTOR_PRO_VERSION' );
540 }
541
542 /**
543 * Convert HTMLEntities to UTF-8 characters
544 *
545 * @param $string
546 * @return string
547 */
548 public static function urlencode_html_entities( $string ) {
549 $entities_dictionary = [
550 '&#145;' => "'", // Opening single quote
551 '&#146;' => "'", // Closing single quote
552 '&#147;' => '"', // Closing double quote
553 '&#148;' => '"', // Opening double quote
554 '&#8216;' => "'", // Closing single quote
555 '&#8217;' => "'", // Opening single quote
556 '&#8218;' => "'", // Single low quote
557 '&#8220;' => '"', // Closing double quote
558 '&#8221;' => '"', // Opening double quote
559 '&#8222;' => '"', // Double low quote
560 ];
561
562 // Decode decimal entities
563 $string = str_replace( array_keys( $entities_dictionary ), array_values( $entities_dictionary ), $string );
564
565 return rawurlencode( html_entity_decode( $string, ENT_QUOTES | ENT_HTML5, 'UTF-8' ) );
566 }
567
568 /**
569 * Parse attributes that come as a string of comma-delimited key|value pairs.
570 * Removes Javascript events and unescaped `href` attributes.
571 *
572 * @param string $attributes_string
573 *
574 * @param string $delimiter Default comma `,`.
575 *
576 * @return array
577 */
578 public static function parse_custom_attributes( $attributes_string, $delimiter = ',' ) {
579 $attributes = explode( $delimiter, $attributes_string );
580 $result = [];
581
582 foreach ( $attributes as $attribute ) {
583 $attr_key_value = explode( '|', $attribute );
584
585 $attr_key = mb_strtolower( $attr_key_value[0] );
586
587 // Remove any not allowed characters.
588 preg_match( '/[-_a-z0-9]+/', $attr_key, $attr_key_matches );
589
590 if ( empty( $attr_key_matches[0] ) ) {
591 continue;
592 }
593
594 $attr_key = $attr_key_matches[0];
595
596 // Avoid Javascript events and unescaped href.
597 if ( 'href' === $attr_key || 'on' === substr( $attr_key, 0, 2 ) ) {
598 continue;
599 }
600
601 if ( isset( $attr_key_value[1] ) ) {
602 $attr_value = trim( $attr_key_value[1] );
603 } else {
604 $attr_value = '';
605 }
606
607 $result[ $attr_key ] = $attr_value;
608 }
609
610 return $result;
611 }
612
613 public static function find_element_recursive( $elements, $id ) {
614 foreach ( $elements as $element ) {
615 if ( $id === $element['id'] ) {
616 return $element;
617 }
618
619 if ( ! empty( $element['elements'] ) ) {
620 $element = self::find_element_recursive( $element['elements'], $id );
621
622 if ( $element ) {
623 return $element;
624 }
625 }
626 }
627
628 return false;
629 }
630
631 /**
632 * Change Submenu First Item Label
633 *
634 * Overwrite the label of the first submenu item of an admin menu item.
635 *
636 * Fired by `admin_menu` action.
637 *
638 * @since 3.1.0
639 *
640 * @param $menu_slug
641 * @param $new_label
642 * @access public
643 */
644 public static function change_submenu_first_item_label( $menu_slug, $new_label ) {
645 global $submenu;
646
647 if ( isset( $submenu[ $menu_slug ] ) ) {
648 // @codingStandardsIgnoreStart
649 $submenu[ $menu_slug ][0][0] = $new_label;
650 // @codingStandardsIgnoreEnd
651 }
652 }
653
654 /**
655 * Validate an HTML tag against a safe allowed list.
656 *
657 * @param string $tag
658 *
659 * @return string
660 */
661 public static function validate_html_tag( $tag ) {
662 return in_array( strtolower( $tag ), self::ALLOWED_HTML_WRAPPER_TAGS ) ? $tag : 'div';
663 }
664
665 /**
666 * Safe print a validated HTML tag.
667 *
668 * @param string $tag
669 */
670 public static function print_validated_html_tag( $tag ) {
671 // PHPCS - the method validate_html_tag is safe.
672 echo self::validate_html_tag( $tag ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
673 }
674
675 /**
676 * Print internal content (not user input) without escaping.
677 */
678 public static function print_unescaped_internal_string( $string ) {
679 echo $string; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
680 }
681
682 /**
683 * Get recently edited posts query.
684 *
685 * Returns `WP_Query` of the recent edited posts.
686 * By default max posts ( $args['posts_per_page'] ) is 3.
687 *
688 * @param array $args
689 *
690 * @return \WP_Query
691 */
692 public static function get_recently_edited_posts_query( $args = [] ) {
693 $args = wp_parse_args( $args, [
694 'post_type' => 'any',
695 'post_status' => [ 'publish', 'draft' ],
696 'posts_per_page' => '3',
697 'meta_key' => '_elementor_edit_mode',
698 'meta_value' => 'builder',
699 'orderby' => 'modified',
700 ] );
701
702 return new \WP_Query( $args );
703 }
704 }
705