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

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