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

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