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

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

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