PluginProbe
Elementor Website Builder – more than just a page builder / 3.21.3
Elementor Website Builder – more than just a page builder v3.21.3
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 / frontend.php

frontend.php in Elementor Website Builder – more than just a page builder 3.21.3, at includes/frontend.php

1,536 lines 38.0 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\Base\App;
5 use Elementor\Core\Frontend\Render_Mode_Manager;
6 use Elementor\Core\Responsive\Files\Frontend as FrontendFile;
7 use Elementor\Core\Files\CSS\Global_CSS;
8 use Elementor\Core\Files\CSS\Post as Post_CSS;
9 use Elementor\Core\Files\CSS\Post_Preview;
10 use Elementor\Core\Responsive\Responsive;
11 use Elementor\Core\Settings\Manager as SettingsManager;
12 use Elementor\Core\Breakpoints\Manager as Breakpoints_Manager;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit; // Exit if accessed directly.
16 }
17
18 /**
19 * Elementor frontend.
20 *
21 * Elementor frontend handler class is responsible for initializing Elementor in
22 * the frontend.
23 *
24 * @since 1.0.0
25 */
26 class Frontend extends App {
27
28 /**
29 * The priority of the content filter.
30 */
31 const THE_CONTENT_FILTER_PRIORITY = 9;
32
33 /**
34 * Post ID.
35 *
36 * Holds the ID of the current post.
37 *
38 * @access private
39 *
40 * @var int Post ID.
41 */
42 private $post_id;
43
44 /**
45 * Fonts to enqueue
46 *
47 * Holds the list of fonts that are being used in the current page.
48 *
49 * @since 1.9.4
50 * @access public
51 *
52 * @var array Used fonts. Default is an empty array.
53 */
54 public $fonts_to_enqueue = [];
55
56 /**
57 * Holds the class that respond to manage the render mode.
58 *
59 * @var Render_Mode_Manager
60 */
61 public $render_mode_manager;
62
63 /**
64 * Registered fonts.
65 *
66 * Holds the list of enqueued fonts in the current page.
67 *
68 * @since 1.0.0
69 * @access private
70 *
71 * @var array Registered fonts. Default is an empty array.
72 */
73 private $registered_fonts = [];
74
75 /**
76 * Icon Fonts to enqueue
77 *
78 * Holds the list of Icon fonts that are being used in the current page.
79 *
80 * @since 2.4.0
81 * @access private
82 *
83 * @var array Used icon fonts. Default is an empty array.
84 */
85 private $icon_fonts_to_enqueue = [];
86
87 /**
88 * Enqueue Icon Fonts
89 *
90 * Holds the list of Icon fonts already enqueued in the current page.
91 *
92 * @since 2.4.0
93 * @access private
94 *
95 * @var array enqueued icon fonts. Default is an empty array.
96 */
97 private $enqueued_icon_fonts = [];
98
99 /**
100 * Whether the page is using Elementor.
101 *
102 * Used to determine whether the current page is using Elementor.
103 *
104 * @since 1.0.0
105 * @access private
106 *
107 * @var bool Whether Elementor is being used. Default is false.
108 */
109 private $_has_elementor_in_page = false;
110
111 /**
112 * Whether the excerpt is being called.
113 *
114 * Used to determine whether the call to `the_content()` came from `get_the_excerpt()`.
115 *
116 * @since 1.0.0
117 * @access private
118 *
119 * @var bool Whether the excerpt is being used. Default is false.
120 */
121 private $_is_excerpt = false;
122
123 /**
124 * Filters removed from the content.
125 *
126 * Hold the list of filters removed from `the_content()`. Used to hold the filters that
127 * conflicted with Elementor while Elementor process the content.
128 *
129 * @since 1.0.0
130 * @access private
131 *
132 * @var array Filters removed from the content. Default is an empty array.
133 */
134 private $content_removed_filters = [];
135
136 /**
137 * @var string[]
138 */
139 private $body_classes = [
140 'elementor-default',
141 ];
142
143 private $google_fonts_index = 0;
144
145 /**
146 * @var string
147 */
148 private $e_swiper_asset_path;
149
150 /**
151 * @var string
152 */
153 private $e_swiper_version;
154
155 /**
156 * Front End constructor.
157 *
158 * Initializing Elementor front end. Make sure we are not in admin, not and
159 * redirect from old URL structure of Elementor editor.
160 *
161 * @since 1.0.0
162 * @access public
163 */
164 public function __construct() {
165 // We don't need this class in admin side, but in AJAX requests.
166 if ( is_admin() && ! wp_doing_ajax() ) {
167 return;
168 }
169
170 add_action( 'template_redirect', [ $this, 'init_render_mode' ], -1 /* Before admin bar. */ );
171 add_action( 'template_redirect', [ $this, 'init' ] );
172 add_action( 'wp_enqueue_scripts', [ $this, 'register_scripts' ], 5 );
173 add_action( 'wp_enqueue_scripts', [ $this, 'register_styles' ], 5 );
174
175 $this->add_content_filter();
176 $this->init_swiper_settings();
177
178 // Hack to avoid enqueue post CSS while it's a `the_excerpt` call.
179 add_filter( 'get_the_excerpt', [ $this, 'start_excerpt_flag' ], 1 );
180 add_filter( 'get_the_excerpt', [ $this, 'end_excerpt_flag' ], 20 );
181 }
182
183 /**
184 * Get module name.
185 *
186 * Retrieve the module name.
187 *
188 * @since 2.3.0
189 * @access public
190 *
191 * @return string Module name.
192 */
193 public function get_name() {
194 return 'frontend';
195 }
196
197 /**
198 * Init render mode manager.
199 */
200 public function init_render_mode() {
201 if ( Plugin::$instance->editor->is_edit_mode() ) {
202 return;
203 }
204
205 $this->render_mode_manager = new Render_Mode_Manager();
206 }
207
208 /**
209 * Init.
210 *
211 * Initialize Elementor front end. Hooks the needed actions to run Elementor
212 * in the front end, including script and style registration.
213 *
214 * Fired by `template_redirect` action.
215 *
216 * @since 1.0.0
217 * @access public
218 */
219 public function init() {
220 if ( Plugin::$instance->editor->is_edit_mode() ) {
221 return;
222 }
223
224 add_filter( 'body_class', [ $this, 'body_class' ] );
225
226 if ( Plugin::$instance->preview->is_preview_mode() ) {
227 return;
228 }
229
230 if ( current_user_can( 'manage_options' ) ) {
231 Plugin::$instance->init_common();
232 }
233
234 $this->post_id = get_the_ID();
235
236 $document = Plugin::$instance->documents->get( $this->post_id );
237
238 if ( is_singular() && $document && $document->is_built_with_elementor() ) {
239 add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_styles' ] );
240 }
241
242 // Priority 7 to allow google fonts in header template to load in <head> tag
243 add_action( 'wp_head', [ $this, 'print_fonts_links' ], 7 );
244 add_action( 'wp_head', [ $this, 'print_google_fonts_preconnect_tag' ], 8 );
245 add_action( 'wp_head', [ $this, 'add_theme_color_meta_tag' ] );
246 add_action( 'wp_footer', [ $this, 'wp_footer' ] );
247 }
248
249 public function print_google_fonts_preconnect_tag() {
250 if ( 0 >= $this->google_fonts_index ) {
251 return;
252 }
253
254 echo '<link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin>';
255 }
256
257 /**
258 * @since 2.0.12
259 * @access public
260 * @param string|array $class
261 */
262 public function add_body_class( $class ) {
263 if ( is_array( $class ) ) {
264 $this->body_classes = array_merge( $this->body_classes, $class );
265 } else {
266 $this->body_classes[] = $class;
267 }
268 }
269
270 /**
271 * Add Theme Color Meta Tag
272 *
273 * @since 3.0.0
274 * @access public
275 */
276 public function add_theme_color_meta_tag() {
277 $kit = Plugin::$instance->kits_manager->get_active_kit_for_frontend();
278 $mobile_theme_color = $kit->get_settings( 'mobile_browser_background' );
279
280 if ( ! empty( $mobile_theme_color ) ) {
281 ?>
282 <meta name="theme-color" content="<?php echo esc_attr( $mobile_theme_color ); ?>">
283 <?php
284 }
285 }
286
287 /**
288 * Body tag classes.
289 *
290 * Add new elementor classes to the body tag.
291 *
292 * Fired by `body_class` filter.
293 *
294 * @since 1.0.0
295 * @access public
296 *
297 * @param array $classes Optional. One or more classes to add to the body tag class list.
298 * Default is an empty array.
299 *
300 * @return array Body tag classes.
301 */
302 public function body_class( $classes = [] ) {
303 $classes = array_merge( $classes, $this->body_classes );
304
305 $id = get_the_ID();
306
307 $document = Plugin::$instance->documents->get( $id );
308
309 if ( is_singular() && $document && $document->is_built_with_elementor() ) {
310 $classes[] = 'elementor-page elementor-page-' . $id;
311 }
312
313 if ( Plugin::$instance->preview->is_preview_mode() ) {
314 $editor_preferences = SettingsManager::get_settings_managers( 'editorPreferences' );
315
316 $show_hidden_elements = $editor_preferences->get_model()->get_settings( 'show_hidden_elements' );
317
318 if ( 'yes' === $show_hidden_elements ) {
319 $classes[] = 'e-preview--show-hidden-elements';
320 }
321 }
322
323 return $classes;
324 }
325
326 /**
327 * Add content filter.
328 *
329 * Remove plain content and render the content generated by Elementor.
330 *
331 * @since 1.8.0
332 * @access public
333 */
334 public function add_content_filter() {
335 add_filter( 'the_content', [ $this, 'apply_builder_in_content' ], self::THE_CONTENT_FILTER_PRIORITY );
336 }
337
338 public function init_swiper_settings() {
339 $e_swiper_latest = Plugin::$instance->experiments->is_feature_active( 'e_swiper_latest' );
340 $this->e_swiper_asset_path = $e_swiper_latest ? 'assets/lib/swiper/v8/' : 'assets/lib/swiper/';
341 $this->e_swiper_version = $e_swiper_latest ? '8.4.5' : '5.3.6';
342 }
343
344 /**
345 * Remove content filter.
346 *
347 * When the Elementor generated content rendered, we remove the filter to prevent multiple
348 * accuracies. This way we make sure Elementor renders the content only once.
349 *
350 * @since 1.8.0
351 * @access public
352 */
353 public function remove_content_filter() {
354 remove_filter( 'the_content', [ $this, 'apply_builder_in_content' ], self::THE_CONTENT_FILTER_PRIORITY );
355 }
356
357 /**
358 * Registers scripts.
359 *
360 * Registers all the frontend scripts.
361 *
362 * Fired by `wp_enqueue_scripts` action.
363 *
364 * @since 1.2.1
365 * @access public
366 */
367 public function register_scripts() {
368 /**
369 * Before frontend register scripts.
370 *
371 * Fires before Elementor frontend scripts are registered.
372 *
373 * @since 1.2.1
374 */
375 do_action( 'elementor/frontend/before_register_scripts' );
376
377 wp_register_script(
378 'elementor-webpack-runtime',
379 $this->get_js_assets_url( 'webpack.runtime', 'assets/js/' ),
380 [],
381 ELEMENTOR_VERSION,
382 true
383 );
384
385 wp_register_script(
386 'elementor-frontend-modules',
387 $this->get_js_assets_url( 'frontend-modules' ),
388 [
389 'elementor-webpack-runtime',
390 'jquery',
391 ],
392 ELEMENTOR_VERSION,
393 true
394 );
395
396 wp_register_script(
397 'elementor-waypoints',
398 $this->get_js_assets_url( 'waypoints', 'assets/lib/waypoints/' ),
399 [
400 'jquery',
401 ],
402 '4.0.2',
403 true
404 );
405
406 wp_register_script(
407 'flatpickr',
408 $this->get_js_assets_url( 'flatpickr', 'assets/lib/flatpickr/' ),
409 [
410 'jquery',
411 ],
412 '4.1.4',
413 true
414 );
415
416 wp_register_script(
417 'imagesloaded',
418 $this->get_js_assets_url( 'imagesloaded', 'assets/lib/imagesloaded/' ),
419 [
420 'jquery',
421 ],
422 '4.1.0',
423 true
424 );
425
426 wp_register_script(
427 'jquery-numerator',
428 $this->get_js_assets_url( 'jquery-numerator', 'assets/lib/jquery-numerator/' ),
429 [
430 'jquery',
431 ],
432 '0.2.1',
433 true
434 );
435
436 wp_register_script(
437 'elementor-dialog',
438 $this->get_js_assets_url( 'dialog', 'assets/lib/dialog/' ),
439 [
440 'jquery-ui-position',
441 ],
442 '4.9.0',
443 true
444 );
445
446 wp_register_script(
447 'elementor-gallery',
448 $this->get_js_assets_url( 'e-gallery', 'assets/lib/e-gallery/js/' ),
449 [
450 'jquery',
451 ],
452 '1.2.0',
453 true
454 );
455
456 wp_register_script(
457 'share-link',
458 $this->get_js_assets_url( 'share-link', 'assets/lib/share-link/' ),
459 [
460 'jquery',
461 ],
462 ELEMENTOR_VERSION,
463 true
464 );
465
466 wp_register_script(
467 'elementor-frontend',
468 $this->get_js_assets_url( 'frontend' ),
469 [
470 'elementor-frontend-modules',
471 'elementor-waypoints',
472 'jquery-ui-position',
473 ],
474 ELEMENTOR_VERSION,
475 true
476 );
477
478 /**
479 * After frontend register scripts.
480 *
481 * Fires after Elementor frontend scripts are registered.
482 *
483 * @since 1.2.1
484 */
485 do_action( 'elementor/frontend/after_register_scripts' );
486 }
487
488 /**
489 * Registers styles.
490 *
491 * Registers all the frontend styles.
492 *
493 * Fired by `wp_enqueue_scripts` action.
494 *
495 * @since 1.2.0
496 * @access public
497 */
498 public function register_styles() {
499 /**
500 * Before frontend register styles.
501 *
502 * Fires before Elementor frontend styles are registered.
503 *
504 * @since 1.2.0
505 */
506 do_action( 'elementor/frontend/before_register_styles' );
507
508 wp_register_style(
509 'font-awesome',
510 $this->get_css_assets_url( 'font-awesome', 'assets/lib/font-awesome/css/' ),
511 [],
512 '4.7.0'
513 );
514
515 wp_register_style(
516 'elementor-icons',
517 $this->get_css_assets_url( 'elementor-icons', 'assets/lib/eicons/css/' ),
518 [],
519 Icons_Manager::ELEMENTOR_ICONS_VERSION
520 );
521
522 wp_register_style(
523 'flatpickr',
524 $this->get_css_assets_url( 'flatpickr', 'assets/lib/flatpickr/' ),
525 [],
526 '4.1.4'
527 );
528
529 wp_register_style(
530 'elementor-gallery',
531 $this->get_css_assets_url( 'e-gallery', 'assets/lib/e-gallery/css/' ),
532 [],
533 '1.2.0'
534 );
535
536 $min_suffix = Utils::is_script_debug() ? '' : '.min';
537
538 $direction_suffix = is_rtl() ? '-rtl' : '';
539
540 $frontend_base_file_name = $this->is_optimized_css_mode() ? 'frontend-lite' : 'frontend';
541
542 $frontend_file_name = $frontend_base_file_name . $direction_suffix . $min_suffix . '.css';
543
544 $has_custom_breakpoints = Plugin::$instance->breakpoints->has_custom_breakpoints();
545
546 wp_register_style(
547 'elementor-frontend',
548 $this->get_frontend_file_url( $frontend_file_name, $has_custom_breakpoints ),
549 [],
550 $has_custom_breakpoints ? null : ELEMENTOR_VERSION
551 );
552
553 wp_register_style(
554 'swiper',
555 $this->get_css_assets_url( 'swiper', $this->e_swiper_asset_path . 'css/' ),
556 [],
557 $this->e_swiper_version
558 );
559
560 /**
561 * After frontend register styles.
562 *
563 * Fires after Elementor frontend styles are registered.
564 *
565 * @since 1.2.0
566 */
567 do_action( 'elementor/frontend/after_register_styles' );
568 }
569
570 /**
571 * Enqueue scripts.
572 *
573 * Enqueue all the frontend scripts.
574 *
575 * @since 1.0.0
576 * @access public
577 */
578 public function enqueue_scripts() {
579 /**
580 * Before frontend enqueue scripts.
581 *
582 * Fires before Elementor frontend scripts are enqueued.
583 *
584 * @since 1.0.0
585 */
586 do_action( 'elementor/frontend/before_enqueue_scripts' );
587
588 wp_enqueue_script( 'elementor-frontend' );
589
590 $this->print_config();
591
592 $this->enqueue_conditional_assets();
593
594 /**
595 * After frontend enqueue scripts.
596 *
597 * Fires after Elementor frontend scripts are enqueued.
598 *
599 * @since 1.0.0
600 */
601 do_action( 'elementor/frontend/after_enqueue_scripts' );
602 }
603
604 /**
605 * Enqueue styles.
606 *
607 * Enqueue all the frontend styles.
608 *
609 * Fired by `wp_enqueue_scripts` action.
610 *
611 * @since 1.0.0
612 * @access public
613 */
614 public function enqueue_styles() {
615 static $is_enqueue_styles_already_triggered;
616
617 if ( ! $is_enqueue_styles_already_triggered ) {
618 $is_enqueue_styles_already_triggered = true;
619
620 /**
621 * Before frontend styles enqueued.
622 *
623 * Fires before Elementor frontend styles are enqueued.
624 *
625 * @since 1.0.0
626 */
627 do_action( 'elementor/frontend/before_enqueue_styles' );
628
629 // The e-icons are needed in preview mode for the editor icons (plus-icon for new section, folder-icon for the templates library etc.).
630 if ( ! Plugin::$instance->experiments->is_feature_active( 'e_font_icon_svg' ) || Plugin::$instance->preview->is_preview_mode() ) {
631 wp_enqueue_style( 'elementor-icons' );
632 }
633
634 wp_enqueue_style( 'elementor-frontend' );
635
636 wp_enqueue_style( 'swiper' );
637
638 /**
639 * After frontend styles enqueued.
640 *
641 * Fires after Elementor frontend styles are enqueued.
642 *
643 * @since 1.0.0
644 */
645 do_action( 'elementor/frontend/after_enqueue_styles' );
646
647 if ( ! Plugin::$instance->preview->is_preview_mode() ) {
648 $this->parse_global_css_code();
649
650 $post_id = get_the_ID();
651 // Check $post_id for virtual pages. check is singular because the $post_id is set to the first post on archive pages.
652 if ( $post_id && is_singular() ) {
653 $css_file = Post_CSS::create( get_the_ID() );
654 $css_file->enqueue();
655 }
656 }
657 }
658 }
659
660 /**
661 * Get Frontend File URL
662 *
663 * Returns the URL for the CSS file to be loaded in the front end. If requested via the second parameter, a custom
664 * file is generated based on a passed template file name. Otherwise, the URL for the default CSS file is returned.
665 *
666 * @since 3.4.5
667 *
668 * @access public
669 *
670 * @param string $frontend_file_name
671 * @param boolean $custom_file
672 *
673 * @return string frontend file URL
674 */
675 public function get_frontend_file_url( $frontend_file_name, $custom_file ) {
676 if ( $custom_file ) {
677 $frontend_file = $this->get_frontend_file( $frontend_file_name );
678
679 $frontend_file_url = $frontend_file->get_url();
680 } else {
681 $frontend_file_url = ELEMENTOR_ASSETS_URL . 'css/' . $frontend_file_name;
682 }
683
684 return $frontend_file_url;
685 }
686
687 /**
688 * Get Frontend File Path
689 *
690 * Returns the path for the CSS file to be loaded in the front end. If requested via the second parameter, a custom
691 * file is generated based on a passed template file name. Otherwise, the path for the default CSS file is returned.
692 *
693 * @since 3.5.0
694 * @access public
695 *
696 * @param string $frontend_file_name
697 * @param boolean $custom_file
698 *
699 * @return string frontend file path
700 */
701 public function get_frontend_file_path( $frontend_file_name, $custom_file ) {
702 if ( $custom_file ) {
703 $frontend_file = $this->get_frontend_file( $frontend_file_name );
704
705 $frontend_file_path = $frontend_file->get_path();
706 } else {
707 $frontend_file_path = ELEMENTOR_ASSETS_PATH . 'css/' . $frontend_file_name;
708 }
709
710 return $frontend_file_path;
711 }
712
713 /**
714 * Get Frontend File
715 *
716 * Returns a frontend file instance.
717 *
718 * @since 3.5.0
719 * @access public
720 *
721 * @param string $frontend_file_name
722 * @param string $file_prefix
723 * @param string $template_file_path
724 *
725 * @return FrontendFile
726 */
727 public function get_frontend_file( $frontend_file_name, $file_prefix = 'custom-', $template_file_path = '' ) {
728 static $cached_frontend_files = [];
729
730 $file_name = $file_prefix . $frontend_file_name;
731
732 if ( isset( $cached_frontend_files[ $file_name ] ) ) {
733 return $cached_frontend_files[ $file_name ];
734 }
735
736 if ( ! $template_file_path ) {
737 $template_file_path = Breakpoints_Manager::get_stylesheet_templates_path() . $frontend_file_name;
738 }
739
740 $frontend_file = new FrontendFile( $file_name, $template_file_path );
741
742 $time = $frontend_file->get_meta( 'time' );
743
744 if ( ! $time ) {
745 $frontend_file->update();
746 }
747
748 $cached_frontend_files[ $file_name ] = $frontend_file;
749
750 return $frontend_file;
751 }
752
753 /**
754 * Enqueue assets conditionally.
755 *
756 * Enqueue all assets that were pre-enabled.
757 *
758 * @since 3.3.0
759 * @access private
760 */
761 private function enqueue_conditional_assets() {
762 Plugin::$instance->assets_loader->enqueue_assets();
763 }
764
765 /**
766 * Elementor footer scripts and styles.
767 *
768 * Handle styles and scripts that are not printed in the header.
769 *
770 * Fired by `wp_footer` action.
771 *
772 * @since 1.0.11
773 * @access public
774 */
775 public function wp_footer() {
776 if ( ! $this->_has_elementor_in_page ) {
777 return;
778 }
779
780 $this->enqueue_styles();
781 $this->enqueue_scripts();
782
783 $this->print_fonts_links();
784 }
785
786 /**
787 * @return array|array[]
788 */
789 public function get_list_of_google_fonts_by_type(): array {
790 $google_fonts = [
791 'google' => [],
792 'early' => [],
793 ];
794
795 foreach ( $this->fonts_to_enqueue as $key => $font ) {
796 $font_type = Fonts::get_font_type( $font );
797
798 switch ( $font_type ) {
799 case Fonts::GOOGLE:
800 $google_fonts['google'][] = $font;
801 break;
802
803 case Fonts::EARLYACCESS:
804 $google_fonts['early'][] = $font;
805 break;
806
807 case false:
808 $this->maybe_enqueue_icon_font( $font );
809 break;
810 default:
811 /**
812 * Print font links.
813 *
814 * Fires when Elementor frontend fonts are printed on the HEAD tag.
815 *
816 * The dynamic portion of the hook name, `$font_type`, refers to the font type.
817 *
818 * @since 2.0.0
819 *
820 * @param string $font Font name.
821 */
822 do_action( "elementor/fonts/print_font_links/{$font_type}", $font );
823 }
824 }
825 $this->fonts_to_enqueue = [];
826
827 return $google_fonts;
828 }
829
830 /**
831 * Print fonts links.
832 *
833 * Enqueue all the frontend fonts by url.
834 *
835 * Fired by `wp_head` action.
836 *
837 * @since 1.9.4
838 * @access public
839 */
840 public function print_fonts_links() {
841 $google_fonts = $this->get_list_of_google_fonts_by_type();
842
843 $this->enqueue_google_fonts( $google_fonts );
844 $this->enqueue_icon_fonts();
845 }
846
847 private function maybe_enqueue_icon_font( $icon_font_type ) {
848 if ( ! Icons_Manager::is_migration_allowed() ) {
849 return;
850 }
851
852 $icons_types = Icons_Manager::get_icon_manager_tabs();
853 if ( ! isset( $icons_types[ $icon_font_type ] ) ) {
854 return;
855 }
856
857 $icon_type = $icons_types[ $icon_font_type ];
858 if ( isset( $icon_type['url'] ) ) {
859 $this->icon_fonts_to_enqueue[ $icon_font_type ] = [ $icon_type['url'] ];
860 }
861 }
862
863 private function enqueue_icon_fonts() {
864 if ( empty( $this->icon_fonts_to_enqueue ) || ! Icons_Manager::is_migration_allowed() ) {
865 return;
866 }
867
868 foreach ( $this->icon_fonts_to_enqueue as $icon_type => $css_url ) {
869 wp_enqueue_style( 'elementor-icons-' . $icon_type );
870 $this->enqueued_icon_fonts[] = $css_url;
871 }
872
873 //clear enqueued icons
874 $this->icon_fonts_to_enqueue = [];
875 }
876
877 /**
878 * @param array $fonts Stable google fonts ($google_fonts['google']).
879 * @return string
880 */
881 public function get_stable_google_fonts_url( array $fonts ): string {
882 foreach ( $fonts as &$font ) {
883 $font = str_replace( ' ', '+', $font ) . ':100,100italic,200,200italic,300,300italic,400,400italic,500,500italic,600,600italic,700,700italic,800,800italic,900,900italic';
884 }
885
886 // Defining a font-display type to google fonts.
887 $font_display_url_str = '&display=' . Fonts::get_font_display_setting();
888
889 $fonts_url = sprintf( 'https://fonts.googleapis.com/css?family=%1$s%2$s', implode( rawurlencode( '|' ), $fonts ), $font_display_url_str );
890
891 $subsets = [
892 'ru_RU' => 'cyrillic',
893 'bg_BG' => 'cyrillic',
894 'he_IL' => 'hebrew',
895 'el' => 'greek',
896 'vi' => 'vietnamese',
897 'uk' => 'cyrillic',
898 'cs_CZ' => 'latin-ext',
899 'ro_RO' => 'latin-ext',
900 'pl_PL' => 'latin-ext',
901 'hr_HR' => 'latin-ext',
902 'hu_HU' => 'latin-ext',
903 'sk_SK' => 'latin-ext',
904 'tr_TR' => 'latin-ext',
905 'lt_LT' => 'latin-ext',
906 ];
907
908 /**
909 * Google font subsets.
910 *
911 * Filters the list of Google font subsets from which locale will be enqueued in frontend.
912 *
913 * @since 1.0.0
914 *
915 * @param array $subsets A list of font subsets.
916 */
917 $subsets = apply_filters( 'elementor/frontend/google_font_subsets', $subsets );
918
919 $locale = get_locale();
920
921 if ( isset( $subsets[ $locale ] ) ) {
922 $fonts_url .= '&subset=' . $subsets[ $locale ];
923 }
924
925 return $fonts_url;
926 }
927
928 /**
929 * @param array $fonts Early Access google fonts ($google_fonts['early']).
930 * @return array
931 */
932 public function get_early_access_google_font_urls( array $fonts ): array {
933 $font_urls = [];
934
935 foreach ( $fonts as $font ) {
936 $font_urls[] = sprintf( 'https://fonts.googleapis.com/earlyaccess/%s.css', strtolower( str_replace( ' ', '', $font ) ) );
937 }
938
939 return $font_urls;
940 }
941
942 /**
943 * Print Google fonts.
944 *
945 * Enqueue all the frontend Google fonts.
946 *
947 * Fired by `wp_head` action.
948 *
949 * @since 1.0.0
950 * @access private
951 *
952 * @param array $google_fonts Optional. Google fonts to print in the frontend.
953 * Default is an empty array.
954 */
955 private function enqueue_google_fonts( $google_fonts = [] ) {
956 $print_google_fonts = Fonts::is_google_fonts_enabled();
957
958 /**
959 * Print frontend google fonts.
960 *
961 * Filters whether to enqueue Google fonts in the frontend.
962 *
963 * @since 1.0.0
964 *
965 * @param bool $print_google_fonts Whether to enqueue Google fonts. Default is true.
966 */
967 $print_google_fonts = apply_filters( 'elementor/frontend/print_google_fonts', $print_google_fonts );
968
969 if ( ! $print_google_fonts ) {
970 return;
971 }
972
973 // Print used fonts
974 if ( ! empty( $google_fonts['google'] ) ) {
975 $this->google_fonts_index++;
976
977 $fonts_url = $this->get_stable_google_fonts_url( $google_fonts['google'] );
978
979 wp_enqueue_style( 'google-fonts-' . $this->google_fonts_index, $fonts_url ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
980 }
981
982 if ( ! empty( $google_fonts['early'] ) ) {
983 $early_access_font_urls = $this->get_early_access_google_font_urls( $google_fonts['early'] );
984
985 foreach ( $early_access_font_urls as $ea_font_url ) {
986 $this->google_fonts_index++;
987
988 //printf( '<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/earlyaccess/%s.css">', strtolower( str_replace( ' ', '', $current_font ) ) );
989
990 wp_enqueue_style( 'google-earlyaccess-' . $this->google_fonts_index, $ea_font_url ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
991 }
992 }
993
994 }
995
996 /**
997 * Enqueue fonts.
998 *
999 * Enqueue all the frontend fonts.
1000 *
1001 * @since 1.2.0
1002 * @access public
1003 *
1004 * @param array $font Fonts to enqueue in the frontend.
1005 */
1006 public function enqueue_font( $font ) {
1007 if ( in_array( $font, $this->registered_fonts ) ) {
1008 return;
1009 }
1010
1011 $this->fonts_to_enqueue[] = $font;
1012 $this->registered_fonts[] = $font;
1013 }
1014
1015 /**
1016 * Parse global CSS.
1017 *
1018 * Enqueue the global CSS file.
1019 *
1020 * @since 1.2.0
1021 * @access protected
1022 */
1023 protected function parse_global_css_code() {
1024 $scheme_css_file = Global_CSS::create( 'global.css' );
1025
1026 $scheme_css_file->enqueue();
1027 }
1028
1029 /**
1030 * Apply builder in content.
1031 *
1032 * Used to apply the Elementor page editor on the post content.
1033 *
1034 * @since 1.0.0
1035 * @access public
1036 *
1037 * @param string $content The post content.
1038 *
1039 * @return string The post content.
1040 */
1041 public function apply_builder_in_content( $content ) {
1042 $this->restore_content_filters();
1043
1044 if ( Plugin::$instance->preview->is_preview_mode() || $this->_is_excerpt ) {
1045 return $content;
1046 }
1047
1048 // Remove the filter itself in order to allow other `the_content` in the elements
1049 $this->remove_content_filter();
1050
1051 $post_id = get_the_ID();
1052 $builder_content = $this->get_builder_content( $post_id );
1053
1054 if ( ! empty( $builder_content ) ) {
1055 $content = $builder_content;
1056 $this->remove_content_filters();
1057 }
1058
1059 // Add the filter again for other `the_content` calls
1060 $this->add_content_filter();
1061
1062 return $content;
1063 }
1064
1065 /**
1066 * Retrieve builder content.
1067 *
1068 * Used to render and return the post content with all the Elementor elements.
1069 *
1070 * Note that this method is an internal method, please use `get_builder_content_for_display()`.
1071 *
1072 * @since 1.0.0
1073 * @access public
1074 *
1075 * @param int $post_id The post ID.
1076 * @param bool $with_css Optional. Whether to retrieve the content with CSS
1077 * or not. Default is false.
1078 *
1079 * @return string The post content.
1080 */
1081 public function get_builder_content( $post_id, $with_css = false ) {
1082 if ( post_password_required( $post_id ) ) {
1083 return '';
1084 }
1085
1086 $document = Plugin::$instance->documents->get_doc_for_frontend( $post_id );
1087
1088 if ( ! $document || ! $document->is_built_with_elementor() ) {
1089 return '';
1090 }
1091
1092 // Change the current post, so widgets can use `documents->get_current`.
1093 Plugin::$instance->documents->switch_to_document( $document );
1094
1095 $data = $document->get_elements_data();
1096
1097 /**
1098 * Frontend builder content data.
1099 *
1100 * Filters the builder content in the frontend.
1101 *
1102 * @since 1.0.0
1103 *
1104 * @param array $data The builder content.
1105 * @param int $post_id The post ID.
1106 */
1107 $data = apply_filters( 'elementor/frontend/builder_content_data', $data, $post_id );
1108
1109 do_action( 'elementor/frontend/before_get_builder_content', $document, $this->_is_excerpt );
1110
1111 if ( empty( $data ) ) {
1112 Plugin::$instance->documents->restore_document();
1113
1114 return '';
1115 }
1116
1117 if ( ! $this->_is_excerpt ) {
1118 if ( $document->is_autosave() ) {
1119 $css_file = Post_Preview::create( $document->get_post()->ID );
1120 } else {
1121 $css_file = Post_CSS::create( $post_id );
1122 }
1123
1124 /**
1125 * Builder Content - Before Enqueue CSS File
1126 *
1127 * Allows intervening with a document's CSS file before it is enqueued.
1128 *
1129 * @param $css_file Post_CSS|Post_Preview
1130 */
1131 $css_file = apply_filters( 'elementor/frontend/builder_content/before_enqueue_css_file', $css_file );
1132
1133 $css_file->enqueue();
1134 }
1135
1136 ob_start();
1137
1138 // Handle JS and Customizer requests, with CSS inline.
1139 if ( is_customize_preview() || wp_doing_ajax() ) {
1140 $with_css = true;
1141 }
1142
1143 /**
1144 * Builder Content - With CSS
1145 *
1146 * Allows overriding the `$with_css` parameter which is a factor in determining whether to print the document's
1147 * CSS and font links inline in a `style` tag above the document's markup.
1148 *
1149 * @param $with_css boolean
1150 */
1151 $with_css = apply_filters( 'elementor/frontend/builder_content/before_print_css', $with_css );
1152
1153 if ( ! empty( $css_file ) && $with_css ) {
1154 $css_file->print_css();
1155 }
1156
1157 $document->print_elements_with_wrapper( $data );
1158
1159 $content = ob_get_clean();
1160
1161 $content = $this->process_more_tag( $content );
1162
1163 /**
1164 * Frontend content.
1165 *
1166 * Filters the content in the frontend.
1167 *
1168 * @since 1.0.0
1169 *
1170 * @param string $content The content.
1171 */
1172 $content = apply_filters( 'elementor/frontend/the_content', $content );
1173
1174 if ( ! empty( $content ) ) {
1175 $this->_has_elementor_in_page = true;
1176 }
1177
1178 Plugin::$instance->documents->restore_document();
1179
1180 // BC
1181 // TODO: use Deprecation::do_deprecated_action() in 3.1.0
1182 do_action( 'elementor/frontend/get_builder_content', $document, $this->_is_excerpt, $with_css );
1183
1184 return $content;
1185 }
1186
1187 /**
1188 * Retrieve builder content for display.
1189 *
1190 * Used to render and return the post content with all the Elementor elements.
1191 *
1192 * @since 1.0.0
1193 * @access public
1194 *
1195 * @param int $post_id The post ID.
1196 *
1197 * @param bool $with_css Optional. Whether to retrieve the content with CSS
1198 * or not. Default is false.
1199 *
1200 * @return string The post content.
1201 */
1202 public function get_builder_content_for_display( $post_id, $with_css = false ) {
1203 if ( ! get_post( $post_id ) ) {
1204 return '';
1205 }
1206
1207 $editor = Plugin::$instance->editor;
1208
1209 // Avoid recursion
1210 if ( get_the_ID() === (int) $post_id ) {
1211 $content = '';
1212 if ( $editor->is_edit_mode() ) {
1213 $content = '<div class="elementor-alert elementor-alert-danger">' . esc_html__( 'Invalid Data: The Template ID cannot be the same as the currently edited template. Please choose a different one.', 'elementor' ) . '</div>';
1214 }
1215
1216 return $content;
1217 }
1218
1219 // Set edit mode as false, so don't render settings and etc. use the $is_edit_mode to indicate if we need the CSS inline
1220 $is_edit_mode = $editor->is_edit_mode();
1221 $editor->set_edit_mode( false );
1222
1223 $with_css = $with_css ? true : $is_edit_mode;
1224
1225 $content = $this->get_builder_content( $post_id, $with_css );
1226
1227 // Restore edit mode state
1228 Plugin::$instance->editor->set_edit_mode( $is_edit_mode );
1229
1230 return $content;
1231 }
1232
1233 /**
1234 * Start excerpt flag.
1235 *
1236 * Flags when `the_excerpt` is called. Used to avoid enqueueing CSS in the excerpt.
1237 *
1238 * @since 1.4.3
1239 * @access public
1240 *
1241 * @param string $excerpt The post excerpt.
1242 *
1243 * @return string The post excerpt.
1244 */
1245 public function start_excerpt_flag( $excerpt ) {
1246 $this->_is_excerpt = true;
1247 return $excerpt;
1248 }
1249
1250 /**
1251 * End excerpt flag.
1252 *
1253 * Flags when `the_excerpt` call ended.
1254 *
1255 * @since 1.4.3
1256 * @access public
1257 *
1258 * @param string $excerpt The post excerpt.
1259 *
1260 * @return string The post excerpt.
1261 */
1262 public function end_excerpt_flag( $excerpt ) {
1263 $this->_is_excerpt = false;
1264 return $excerpt;
1265 }
1266
1267 /**
1268 * Remove content filters.
1269 *
1270 * Remove WordPress default filters that conflicted with Elementor.
1271 *
1272 * @since 1.5.0
1273 * @access public
1274 */
1275 public function remove_content_filters() {
1276 $filters = [
1277 'wpautop',
1278 'shortcode_unautop',
1279 'wptexturize',
1280 ];
1281
1282 foreach ( $filters as $filter ) {
1283 // Check if another plugin/theme do not already removed the filter.
1284 if ( has_filter( 'the_content', $filter ) ) {
1285 remove_filter( 'the_content', $filter );
1286 $this->content_removed_filters[] = $filter;
1287 }
1288 }
1289 }
1290
1291 /**
1292 * Has Elementor In Page
1293 *
1294 * Determine whether the current page is using Elementor.
1295 *
1296 * @since 2.0.9
1297 *
1298 * @access public
1299 * @return bool
1300 */
1301 public function has_elementor_in_page() {
1302 return $this->_has_elementor_in_page;
1303 }
1304
1305 public function create_action_hash( $action, array $settings = [] ) {
1306 return '#' . rawurlencode( sprintf( 'elementor-action:action=%1$s&settings=%2$s', $action, base64_encode( wp_json_encode( $settings ) ) ) );
1307 }
1308
1309 /**
1310 * Is the current render mode is static.
1311 *
1312 * @return bool
1313 */
1314 public function is_static_render_mode() {
1315 // The render mode manager is exists only in frontend,
1316 // so by default if it is not exist the method will return false.
1317 if ( ! $this->render_mode_manager ) {
1318 return false;
1319 }
1320
1321 return $this->render_mode_manager->get_current()->is_static();
1322 }
1323
1324 /**
1325 * Get Init Settings
1326 *
1327 * Used to define the default/initial settings of the object. Inheriting classes may implement this method to define
1328 * their own default/initial settings.
1329 *
1330 * @since 2.3.0
1331 *
1332 * @access protected
1333 * @return array
1334 */
1335 protected function get_init_settings() {
1336 $is_preview_mode = Plugin::$instance->preview->is_preview_mode( Plugin::$instance->preview->get_post_id() );
1337
1338 $active_experimental_features = Plugin::$instance->experiments->get_active_features();
1339
1340 $active_experimental_features = array_fill_keys( array_keys( $active_experimental_features ), true );
1341
1342 $assets_url = ELEMENTOR_ASSETS_URL;
1343
1344 /**
1345 * Frontend assets URL
1346 *
1347 * Filters Elementor frontend assets URL.
1348 *
1349 * @since 2.3.0
1350 *
1351 * @param string $assets_url The frontend assets URL. Default is ELEMENTOR_ASSETS_URL.
1352 */
1353 $assets_url = apply_filters( 'elementor/frontend/assets_url', $assets_url );
1354
1355 $settings = [
1356 'environmentMode' => [
1357 'edit' => $is_preview_mode,
1358 'wpPreview' => is_preview(),
1359 'isScriptDebug' => Utils::is_script_debug(),
1360 ],
1361 'i18n' => [
1362 'shareOnFacebook' => esc_html__( 'Share on Facebook', 'elementor' ),
1363 'shareOnTwitter' => esc_html__( 'Share on Twitter', 'elementor' ),
1364 'pinIt' => esc_html__( 'Pin it', 'elementor' ),
1365 'download' => esc_html__( 'Download', 'elementor' ),
1366 'downloadImage' => esc_html__( 'Download image', 'elementor' ),
1367 'fullscreen' => esc_html__( 'Fullscreen', 'elementor' ),
1368 'zoom' => esc_html__( 'Zoom', 'elementor' ),
1369 'share' => esc_html__( 'Share', 'elementor' ),
1370 'playVideo' => esc_html__( 'Play Video', 'elementor' ),
1371 'previous' => esc_html__( 'Previous', 'elementor' ),
1372 'next' => esc_html__( 'Next', 'elementor' ),
1373 'close' => esc_html__( 'Close', 'elementor' ),
1374 'a11yCarouselWrapperAriaLabel' => __( 'Carousel | Horizontal scrolling: Arrow Left & Right', 'elementor' ),
1375 'a11yCarouselPrevSlideMessage' => __( 'Previous slide', 'elementor' ),
1376 'a11yCarouselNextSlideMessage' => __( 'Next slide', 'elementor' ),
1377 'a11yCarouselFirstSlideMessage' => __( 'This is the first slide', 'elementor' ),
1378 'a11yCarouselLastSlideMessage' => __( 'This is the last slide', 'elementor' ),
1379 'a11yCarouselPaginationBulletMessage' => __( 'Go to slide', 'elementor' ),
1380 ],
1381 'is_rtl' => is_rtl(),
1382 // 'breakpoints' object is kept for BC.
1383 'breakpoints' => Responsive::get_breakpoints(),
1384 // 'responsive' contains the custom breakpoints config introduced in Elementor v3.2.0
1385 'responsive' => [
1386 'breakpoints' => Plugin::$instance->breakpoints->get_breakpoints_config(),
1387 ],
1388 'version' => ELEMENTOR_VERSION,
1389 'is_static' => $this->is_static_render_mode(),
1390 'experimentalFeatures' => $active_experimental_features,
1391 'urls' => [
1392 'assets' => $assets_url,
1393 ],
1394 'swiperClass' => Plugin::$instance->experiments->is_feature_active( 'e_swiper_latest' ) ? 'swiper' : 'swiper-container',
1395 ];
1396
1397 $settings['settings'] = SettingsManager::get_settings_frontend_config();
1398
1399 $kit = Plugin::$instance->kits_manager->get_active_kit_for_frontend();
1400 $settings['kit'] = $kit->get_frontend_settings();
1401
1402 if ( is_singular() ) {
1403 $post = get_post();
1404
1405 $title = Utils::urlencode_html_entities( wp_get_document_title() );
1406
1407 // Try to use the 'large' WP image size because the Pinterest share API
1408 // has problems accepting shares with large images sometimes, and the WP 'large' thumbnail is
1409 // the largest default WP image size that will probably not be changed in most sites
1410 $featured_image_url = get_the_post_thumbnail_url( null, 'large' );
1411
1412 // If the large size was nullified, use the full size which cannot be nullified/deleted
1413 if ( ! $featured_image_url ) {
1414 $featured_image_url = get_the_post_thumbnail_url( null, 'full' );
1415 }
1416
1417 $settings['post'] = [
1418 'id' => $post->ID,
1419 'title' => $title,
1420 'excerpt' => $post->post_excerpt,
1421 'featuredImage' => $featured_image_url,
1422 ];
1423 } else {
1424 $settings['post'] = [
1425 'id' => 0,
1426 'title' => wp_get_document_title(),
1427 'excerpt' => get_the_archive_description(),
1428 ];
1429 }
1430
1431 $empty_object = (object) [];
1432
1433 if ( $is_preview_mode ) {
1434 $settings['elements'] = [
1435 'data' => $empty_object,
1436 'editSettings' => $empty_object,
1437 'keys' => $empty_object,
1438 ];
1439 }
1440
1441 if ( is_user_logged_in() ) {
1442 $user = wp_get_current_user();
1443
1444 if ( ! empty( $user->roles ) ) {
1445 $settings['user'] = [
1446 'roles' => $user->roles,
1447 ];
1448 }
1449 }
1450
1451 return $settings;
1452 }
1453
1454 /**
1455 * Restore content filters.
1456 *
1457 * Restore removed WordPress filters that conflicted with Elementor.
1458 *
1459 * @since 1.5.0
1460 * @access public
1461 */
1462 public function restore_content_filters() {
1463 foreach ( $this->content_removed_filters as $filter ) {
1464 add_filter( 'the_content', $filter );
1465 }
1466
1467 $this->content_removed_filters = [];
1468 }
1469
1470 /**
1471 * Process More Tag
1472 *
1473 * Respect the native WP (<!--more-->) tag
1474 *
1475 * @access private
1476 * @since 2.0.4
1477 *
1478 * @param $content
1479 *
1480 * @return string
1481 */
1482 private function process_more_tag( $content ) {
1483 $post = get_post();
1484 $content = str_replace( '&lt;!--more--&gt;', '<!--more-->', $content );
1485 $parts = get_extended( $content );
1486 if ( empty( $parts['extended'] ) ) {
1487 return $content;
1488 }
1489
1490 if ( is_singular() ) {
1491 return $parts['main'] . '<div id="more-' . $post->ID . '"></div>' . $parts['extended'];
1492 }
1493
1494 if ( empty( $parts['more_text'] ) ) {
1495 $parts['more_text'] = esc_html__( '(more&hellip;)', 'elementor' );
1496 }
1497
1498 $more_link_text = sprintf(
1499 '<span aria-label="%1$s">%2$s</span>',
1500 sprintf(
1501 /* translators: %s: Current post name. */
1502 __( 'Continue reading %s', 'elementor' ),
1503 the_title_attribute( [
1504 'echo' => false,
1505 ] )
1506 ),
1507 $parts['more_text']
1508 );
1509
1510 $more_link = sprintf( ' <a href="%s#more-%s" class="more-link elementor-more-link">%s</a>', get_permalink(), $post->ID, $more_link_text );
1511
1512 /**
1513 * The content "more" link.
1514 *
1515 * Filters the "more" link displayed after the content.
1516 *
1517 * This hook can be used either to change the link syntax or to change the
1518 * text inside the link.
1519 *
1520 * @since 2.0.4
1521 *
1522 * @param string $more_link The more link.
1523 * @param string $more_link_text The text inside the more link.
1524 */
1525 $more_link = apply_filters( 'the_content_more_link', $more_link, $more_link_text );
1526
1527 return force_balance_tags( $parts['main'] ) . $more_link;
1528 }
1529
1530 private function is_optimized_css_mode() {
1531 $is_optimized_css_loading = Plugin::$instance->experiments->is_feature_active( 'e_optimized_css_loading' );
1532
1533 return ! Utils::is_script_debug() && $is_optimized_css_loading && ! Plugin::$instance->preview->is_preview_mode();
1534 }
1535 }
1536