PluginProbe
Elementor Website Builder – more than just a page builder / 3.30.3
Elementor Website Builder – more than just a page builder v3.30.3
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / includes / frontend.php

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

1,547 lines 38.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor;
3
4 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 $force_enqueue_from_cdn = Plugin::$instance->preview->is_preview_mode();
1008
1009 if ( ! empty( $google_fonts['google'] ) ) {
1010 foreach ( $google_fonts['google'] as $current_font ) {
1011 Google_Font::enqueue( $current_font, Google_Font::TYPE_DEFAULT, $force_enqueue_from_cdn );
1012 }
1013 }
1014
1015 if ( ! empty( $google_fonts['early'] ) ) {
1016 foreach ( $google_fonts['early'] as $current_font ) {
1017 Google_Font::enqueue( $current_font, Google_Font::TYPE_EARLYACCESS, $force_enqueue_from_cdn );
1018 }
1019 }
1020 }
1021
1022 /**
1023 * Enqueue fonts.
1024 *
1025 * Enqueue all the frontend fonts.
1026 *
1027 * @since 1.2.0
1028 * @access public
1029 *
1030 * @param array $font Fonts to enqueue in the frontend.
1031 */
1032 public function enqueue_font( $font ) {
1033 if ( in_array( $font, $this->registered_fonts ) ) {
1034 return;
1035 }
1036
1037 $this->fonts_to_enqueue[] = $font;
1038 $this->registered_fonts[] = $font;
1039 }
1040
1041 /**
1042 * Apply builder in content.
1043 *
1044 * Used to apply the Elementor page editor on the post content.
1045 *
1046 * @since 1.0.0
1047 * @access public
1048 *
1049 * @param string $content The post content.
1050 *
1051 * @return string The post content.
1052 */
1053 public function apply_builder_in_content( $content ) {
1054 $this->restore_content_filters();
1055
1056 if ( Plugin::$instance->preview->is_preview_mode() || $this->_is_excerpt ) {
1057 return $content;
1058 }
1059
1060 // Remove the filter itself in order to allow other `the_content` in the elements
1061 $this->remove_content_filter();
1062
1063 $post_id = get_the_ID();
1064 $builder_content = $this->get_builder_content( $post_id );
1065
1066 if ( ! empty( $builder_content ) ) {
1067 $content = $builder_content;
1068 $this->remove_content_filters();
1069 }
1070
1071 // Add the filter again for other `the_content` calls
1072 $this->add_content_filter();
1073
1074 return $content;
1075 }
1076
1077 /**
1078 * Retrieve builder content.
1079 *
1080 * Used to render and return the post content with all the Elementor elements.
1081 *
1082 * Note that this method is an internal method, please use `get_builder_content_for_display()`.
1083 *
1084 * @since 1.0.0
1085 * @access public
1086 *
1087 * @param int $post_id The post ID.
1088 * @param bool $with_css Optional. Whether to retrieve the content with CSS
1089 * or not. Default is false.
1090 *
1091 * @return string The post content.
1092 */
1093 public function get_builder_content( $post_id, $with_css = false ) {
1094 if ( post_password_required( $post_id ) ) {
1095 return '';
1096 }
1097
1098 $document = Plugin::$instance->documents->get_doc_for_frontend( $post_id );
1099
1100 if ( ! $document || ! $document->is_built_with_elementor() ) {
1101 return '';
1102 }
1103
1104 // Change the current post, so widgets can use `documents->get_current`.
1105 Plugin::$instance->documents->switch_to_document( $document );
1106
1107 $data = $document->get_elements_data();
1108
1109 /**
1110 * Frontend builder content data.
1111 *
1112 * Filters the builder content in the frontend.
1113 *
1114 * @since 1.0.0
1115 *
1116 * @param array $data The builder content.
1117 * @param int $post_id The post ID.
1118 */
1119 $data = apply_filters( 'elementor/frontend/builder_content_data', $data, $post_id );
1120
1121 do_action( 'elementor/frontend/before_get_builder_content', $document, $this->_is_excerpt );
1122
1123 if ( empty( $data ) ) {
1124 Plugin::$instance->documents->restore_document();
1125
1126 return '';
1127 }
1128
1129 if ( ! $this->_is_excerpt ) {
1130 if ( $document->is_autosave() ) {
1131 $css_file = Post_Preview::create( $document->get_post()->ID );
1132 } else {
1133 $css_file = Post_CSS::create( $post_id );
1134 }
1135
1136 /**
1137 * Builder Content - Before Enqueue CSS File
1138 *
1139 * Allows intervening with a document's CSS file before it is enqueued.
1140 *
1141 * @param $css_file Post_CSS|Post_Preview
1142 */
1143 $css_file = apply_filters( 'elementor/frontend/builder_content/before_enqueue_css_file', $css_file );
1144
1145 $css_file->enqueue();
1146 }
1147
1148 ob_start();
1149
1150 // Handle JS and Customizer requests, with CSS inline.
1151 if ( is_customize_preview() || wp_doing_ajax() ) {
1152 $with_css = true;
1153 }
1154
1155 /**
1156 * Builder Content - With CSS
1157 *
1158 * Allows overriding the `$with_css` parameter which is a factor in determining whether to print the document's
1159 * CSS and font links inline in a `style` tag above the document's markup.
1160 *
1161 * @param $with_css boolean
1162 */
1163 $with_css = apply_filters( 'elementor/frontend/builder_content/before_print_css', $with_css );
1164
1165 if ( ! empty( $css_file ) && $with_css ) {
1166 $css_file->print_css();
1167 }
1168
1169 $document->print_elements_with_wrapper( $data );
1170
1171 $content = ob_get_clean();
1172
1173 $content = $this->process_more_tag( $content );
1174
1175 /**
1176 * Frontend content.
1177 *
1178 * Filters the content in the frontend.
1179 *
1180 * @since 1.0.0
1181 *
1182 * @param string $content The content.
1183 */
1184 $content = apply_filters( 'elementor/frontend/the_content', $content );
1185
1186 if ( ! empty( $content ) ) {
1187 $this->_has_elementor_in_page = true;
1188 }
1189
1190 Plugin::$instance->documents->restore_document();
1191
1192 // BC
1193 // TODO: use Deprecation::do_deprecated_action() in 3.1.0
1194 do_action( 'elementor/frontend/get_builder_content', $document, $this->_is_excerpt, $with_css );
1195
1196 return $content;
1197 }
1198
1199 /**
1200 * Retrieve builder content for display.
1201 *
1202 * Used to render and return the post content with all the Elementor elements.
1203 *
1204 * @since 1.0.0
1205 * @access public
1206 *
1207 * @param int $post_id The post ID.
1208 *
1209 * @param bool $with_css Optional. Whether to retrieve the content with CSS
1210 * or not. Default is false.
1211 *
1212 * @return string The post content.
1213 */
1214 public function get_builder_content_for_display( $post_id, $with_css = false ) {
1215 if ( ! get_post( $post_id ) ) {
1216 return '';
1217 }
1218
1219 $editor = Plugin::$instance->editor;
1220
1221 // Avoid recursion
1222 if ( get_the_ID() === (int) $post_id ) {
1223 $content = '';
1224 if ( $editor->is_edit_mode() ) {
1225 $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>';
1226 }
1227
1228 return $content;
1229 }
1230
1231 // 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
1232 $is_edit_mode = $editor->is_edit_mode();
1233 $editor->set_edit_mode( false );
1234
1235 $with_css = $with_css ? true : $is_edit_mode;
1236
1237 $content = $this->get_builder_content( $post_id, $with_css );
1238
1239 // Restore edit mode state
1240 Plugin::$instance->editor->set_edit_mode( $is_edit_mode );
1241
1242 return $content;
1243 }
1244
1245 /**
1246 * Start excerpt flag.
1247 *
1248 * Flags when `the_excerpt` is called. Used to avoid enqueueing CSS in the excerpt.
1249 *
1250 * @since 1.4.3
1251 * @access public
1252 *
1253 * @param string $excerpt The post excerpt.
1254 *
1255 * @return string The post excerpt.
1256 */
1257 public function start_excerpt_flag( $excerpt ) {
1258 $this->_is_excerpt = true;
1259 return $excerpt;
1260 }
1261
1262 /**
1263 * End excerpt flag.
1264 *
1265 * Flags when `the_excerpt` call ended.
1266 *
1267 * @since 1.4.3
1268 * @access public
1269 *
1270 * @param string $excerpt The post excerpt.
1271 *
1272 * @return string The post excerpt.
1273 */
1274 public function end_excerpt_flag( $excerpt ) {
1275 $this->_is_excerpt = false;
1276 return $excerpt;
1277 }
1278
1279 /**
1280 * Remove content filters.
1281 *
1282 * Remove WordPress default filters that conflicted with Elementor.
1283 *
1284 * @since 1.5.0
1285 * @access public
1286 */
1287 public function remove_content_filters() {
1288 $filters = [
1289 'wpautop',
1290 'shortcode_unautop',
1291 'wptexturize',
1292 ];
1293
1294 foreach ( $filters as $filter ) {
1295 // Check if another plugin/theme do not already removed the filter.
1296 if ( has_filter( 'the_content', $filter ) ) {
1297 remove_filter( 'the_content', $filter );
1298 $this->content_removed_filters[] = $filter;
1299 }
1300 }
1301 }
1302
1303 /**
1304 * Has Elementor In Page
1305 *
1306 * Determine whether the current page is using Elementor.
1307 *
1308 * @since 2.0.9
1309 *
1310 * @access public
1311 * @return bool
1312 */
1313 public function has_elementor_in_page() {
1314 return $this->_has_elementor_in_page;
1315 }
1316
1317 public function create_action_hash( $action, array $settings = [] ) {
1318 return '#' . rawurlencode( sprintf( 'elementor-action:action=%1$s&settings=%2$s', $action, base64_encode( wp_json_encode( $settings ) ) ) );
1319 }
1320
1321 /**
1322 * Is the current render mode is static.
1323 *
1324 * @return bool
1325 */
1326 public function is_static_render_mode() {
1327 // The render mode manager is exists only in frontend,
1328 // so by default if it is not exist the method will return false.
1329 if ( ! $this->render_mode_manager ) {
1330 return false;
1331 }
1332
1333 return $this->render_mode_manager->get_current()->is_static();
1334 }
1335
1336 /**
1337 * Get Init Settings
1338 *
1339 * Used to define the default/initial settings of the object. Inheriting classes may implement this method to define
1340 * their own default/initial settings.
1341 *
1342 * @since 2.3.0
1343 *
1344 * @access protected
1345 * @return array
1346 */
1347 protected function get_init_settings() {
1348 $is_preview_mode = Plugin::$instance->preview->is_preview_mode( Plugin::$instance->preview->get_post_id() );
1349
1350 $active_experimental_features = Plugin::$instance->experiments->get_active_features();
1351
1352 $active_experimental_features = array_fill_keys( array_keys( $active_experimental_features ), true );
1353
1354 $assets_url = ELEMENTOR_ASSETS_URL;
1355
1356 /**
1357 * Frontend assets URL
1358 *
1359 * Filters Elementor frontend assets URL.
1360 *
1361 * @since 2.3.0
1362 *
1363 * @param string $assets_url The frontend assets URL. Default is ELEMENTOR_ASSETS_URL.
1364 */
1365 $assets_url = apply_filters( 'elementor/frontend/assets_url', $assets_url );
1366
1367 $settings = [
1368 'environmentMode' => [
1369 'edit' => $is_preview_mode,
1370 'wpPreview' => is_preview(),
1371 'isScriptDebug' => Utils::is_script_debug(),
1372 ],
1373 'i18n' => [
1374 'shareOnFacebook' => esc_html__( 'Share on Facebook', 'elementor' ),
1375 'shareOnTwitter' => esc_html__( 'Share on Twitter', 'elementor' ),
1376 'pinIt' => esc_html__( 'Pin it', 'elementor' ),
1377 'download' => esc_html__( 'Download', 'elementor' ),
1378 'downloadImage' => esc_html__( 'Download image', 'elementor' ),
1379 'fullscreen' => esc_html__( 'Fullscreen', 'elementor' ),
1380 'zoom' => esc_html__( 'Zoom', 'elementor' ),
1381 'share' => esc_html__( 'Share', 'elementor' ),
1382 'playVideo' => esc_html__( 'Play Video', 'elementor' ),
1383 'previous' => esc_html__( 'Previous', 'elementor' ),
1384 'next' => esc_html__( 'Next', 'elementor' ),
1385 'close' => esc_html__( 'Close', 'elementor' ),
1386 'a11yCarouselPrevSlideMessage' => __( 'Previous slide', 'elementor' ),
1387 'a11yCarouselNextSlideMessage' => __( 'Next slide', 'elementor' ),
1388 'a11yCarouselFirstSlideMessage' => __( 'This is the first slide', 'elementor' ),
1389 'a11yCarouselLastSlideMessage' => __( 'This is the last slide', 'elementor' ),
1390 'a11yCarouselPaginationBulletMessage' => __( 'Go to slide', 'elementor' ),
1391 ],
1392 'is_rtl' => is_rtl(),
1393 // 'breakpoints' object is kept for BC.
1394 'breakpoints' => Responsive::get_breakpoints(),
1395 // 'responsive' contains the custom breakpoints config introduced in Elementor v3.2.0
1396 'responsive' => [
1397 'breakpoints' => Plugin::$instance->breakpoints->get_breakpoints_config(),
1398 'hasCustomBreakpoints' => Plugin::$instance->breakpoints->has_custom_breakpoints(),
1399 ],
1400 'version' => ELEMENTOR_VERSION,
1401 'is_static' => $this->is_static_render_mode(),
1402 'experimentalFeatures' => $active_experimental_features,
1403 'urls' => [
1404 'assets' => $assets_url,
1405 'ajaxurl' => admin_url( 'admin-ajax.php' ),
1406 'uploadUrl' => wp_upload_dir()['baseurl'],
1407 ],
1408 'nonces' => [
1409 'floatingButtonsClickTracking' => wp_create_nonce( Module::CLICK_TRACKING_NONCE ),
1410 ],
1411 'swiperClass' => 'swiper',
1412 ];
1413
1414 $settings['settings'] = SettingsManager::get_settings_frontend_config();
1415
1416 $kit = Plugin::$instance->kits_manager->get_active_kit_for_frontend();
1417 $settings['kit'] = $kit->get_frontend_settings();
1418
1419 if ( is_singular() ) {
1420 $post = get_post();
1421
1422 $title = Utils::urlencode_html_entities( wp_get_document_title() );
1423
1424 // Try to use the 'large' WP image size because the Pinterest share API
1425 // has problems accepting shares with large images sometimes, and the WP 'large' thumbnail is
1426 // the largest default WP image size that will probably not be changed in most sites
1427 $featured_image_url = get_the_post_thumbnail_url( null, 'large' );
1428
1429 // If the large size was nullified, use the full size which cannot be nullified/deleted
1430 if ( ! $featured_image_url ) {
1431 $featured_image_url = get_the_post_thumbnail_url( null, 'full' );
1432 }
1433
1434 $settings['post'] = [
1435 'id' => $post->ID,
1436 'title' => $title,
1437 'excerpt' => $post->post_excerpt,
1438 'featuredImage' => $featured_image_url,
1439 ];
1440 } else {
1441 $settings['post'] = [
1442 'id' => 0,
1443 'title' => wp_get_document_title(),
1444 'excerpt' => get_the_archive_description(),
1445 ];
1446 }
1447
1448 $empty_object = (object) [];
1449
1450 if ( $is_preview_mode ) {
1451 $settings['elements'] = [
1452 'data' => $empty_object,
1453 'editSettings' => $empty_object,
1454 'keys' => $empty_object,
1455 ];
1456 }
1457
1458 if ( is_user_logged_in() ) {
1459 $user = wp_get_current_user();
1460
1461 if ( ! empty( $user->roles ) ) {
1462 $settings['user'] = [
1463 'roles' => $user->roles,
1464 ];
1465 }
1466 }
1467
1468 return $settings;
1469 }
1470
1471 /**
1472 * Restore content filters.
1473 *
1474 * Restore removed WordPress filters that conflicted with Elementor.
1475 *
1476 * @since 1.5.0
1477 * @access public
1478 */
1479 public function restore_content_filters() {
1480 foreach ( $this->content_removed_filters as $filter ) {
1481 add_filter( 'the_content', $filter );
1482 }
1483
1484 $this->content_removed_filters = [];
1485 }
1486
1487 /**
1488 * Process More Tag
1489 *
1490 * Respect the native WP (<!--more-->) tag
1491 *
1492 * @access private
1493 * @since 2.0.4
1494 *
1495 * @param string $content
1496 *
1497 * @return string
1498 */
1499 private function process_more_tag( $content ) {
1500 $post = get_post();
1501 $content = str_replace( '&lt;!--more--&gt;', '<!--more-->', $content );
1502 $parts = get_extended( $content );
1503 if ( empty( $parts['extended'] ) ) {
1504 return $content;
1505 }
1506
1507 if ( is_singular() ) {
1508 return $parts['main'] . '<div id="more-' . $post->ID . '"></div>' . $parts['extended'];
1509 }
1510
1511 if ( empty( $parts['more_text'] ) ) {
1512 $parts['more_text'] = esc_html__( '(more&hellip;)', 'elementor' );
1513 }
1514
1515 $more_link_text = sprintf(
1516 '<span aria-label="%1$s">%2$s</span>',
1517 sprintf(
1518 /* translators: %s: Current post name. */
1519 __( 'Continue reading %s', 'elementor' ),
1520 the_title_attribute( [
1521 'echo' => false,
1522 ] )
1523 ),
1524 $parts['more_text']
1525 );
1526
1527 $more_link = sprintf( ' <a href="%s#more-%s" class="more-link elementor-more-link">%s</a>', get_permalink(), $post->ID, $more_link_text );
1528
1529 /**
1530 * The content "more" link.
1531 *
1532 * Filters the "more" link displayed after the content.
1533 *
1534 * This hook can be used either to change the link syntax or to change the
1535 * text inside the link.
1536 *
1537 * @since 2.0.4
1538 *
1539 * @param string $more_link The more link.
1540 * @param string $more_link_text The text inside the more link.
1541 */
1542 $more_link = apply_filters( 'the_content_more_link', $more_link, $more_link_text );
1543
1544 return force_balance_tags( $parts['main'] ) . $more_link;
1545 }
1546 }
1547