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

1,550 lines 38.4 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 do_action( 'elementor/post/render', $post_id );
670 $this->handle_page_assets( $post_id );
671
672 $css_file = Post_CSS::create( get_the_ID() );
673 $css_file->enqueue();
674 }
675 }
676
677 do_action( 'elementor/frontend/after_enqueue_post_styles' );
678 }
679 }
680
681 private function handle_page_assets( $post_id ): void {
682 $page_assets = get_post_meta( $post_id, Assets::ASSETS_META_KEY, true );
683 if ( ! empty( $page_assets ) ) {
684 Plugin::$instance->assets_loader->enable_assets( $page_assets );
685 return;
686 }
687
688 $document = Plugin::$instance->documents->get( $post_id );
689
690 if ( ! $document ) {
691 return;
692 }
693
694 $document->update_runtime_elements();
695 }
696
697 /**
698 * Get Frontend File URL
699 *
700 * Returns the URL for the CSS file to be loaded in the front end. If requested via the second parameter, a custom
701 * file is generated based on a passed template file name. Otherwise, the URL for the default CSS file is returned.
702 *
703 * @since 3.4.5
704 *
705 * @access public
706 *
707 * @param string $frontend_file_name
708 * @param boolean $custom_file
709 *
710 * @return string frontend file URL
711 */
712 public function get_frontend_file_url( $frontend_file_name, $custom_file, $css_subfolder = '' ) {
713 if ( $custom_file ) {
714 $frontend_file = $this->get_frontend_file( $frontend_file_name );
715
716 $frontend_file_url = $frontend_file->get_url();
717 } else {
718 $frontend_file_url = ELEMENTOR_ASSETS_URL . 'css/' . $css_subfolder . $frontend_file_name;
719 }
720
721 return $frontend_file_url;
722 }
723
724 /**
725 * Get Frontend File Path
726 *
727 * Returns the path for the CSS file to be loaded in the front end. If requested via the second parameter, a custom
728 * file is generated based on a passed template file name. Otherwise, the path for the default CSS file is returned.
729 *
730 * @since 3.5.0
731 * @access public
732 *
733 * @param string $frontend_file_name
734 * @param boolean $custom_file
735 *
736 * @return string frontend file path
737 */
738 public function get_frontend_file_path( $frontend_file_name, $custom_file ) {
739 if ( $custom_file ) {
740 $frontend_file = $this->get_frontend_file( $frontend_file_name );
741
742 $frontend_file_path = $frontend_file->get_path();
743 } else {
744 $frontend_file_path = ELEMENTOR_ASSETS_PATH . 'css/' . $frontend_file_name;
745 }
746
747 return $frontend_file_path;
748 }
749
750 /**
751 * Get Frontend File
752 *
753 * Returns a frontend file instance.
754 *
755 * @since 3.5.0
756 * @access public
757 *
758 * @param string $frontend_file_name
759 * @param string $file_prefix
760 * @param string $template_file_path
761 *
762 * @return FrontendFile
763 */
764 public function get_frontend_file( $frontend_file_name, $file_prefix = 'custom-', $template_file_path = '' ) {
765 static $cached_frontend_files = [];
766
767 $file_name = $file_prefix . $frontend_file_name;
768
769 if ( isset( $cached_frontend_files[ $file_name ] ) ) {
770 return $cached_frontend_files[ $file_name ];
771 }
772
773 if ( ! $template_file_path ) {
774 $template_file_path = Breakpoints_Manager::get_stylesheet_templates_path() . $frontend_file_name;
775 }
776
777 $frontend_file = new FrontendFile( $file_name, $template_file_path );
778
779 $time = $frontend_file->get_meta( 'time' );
780
781 if ( ! $time ) {
782 $frontend_file->update();
783 }
784
785 $cached_frontend_files[ $file_name ] = $frontend_file;
786
787 return $frontend_file;
788 }
789
790 /**
791 * Enqueue assets conditionally.
792 *
793 * Enqueue all assets that were pre-enabled.
794 *
795 * @since 3.3.0
796 * @access private
797 */
798 private function enqueue_conditional_assets() {
799 Plugin::$instance->assets_loader->enqueue_assets();
800 }
801
802 /**
803 * Elementor footer scripts and styles.
804 *
805 * Handle styles and scripts that are not printed in the header.
806 *
807 * Fired by `wp_footer` action.
808 *
809 * @since 1.0.11
810 * @access public
811 */
812 public function wp_footer() {
813 if ( ! $this->_has_elementor_in_page ) {
814 return;
815 }
816
817 $this->enqueue_styles();
818 $this->enqueue_scripts();
819
820 $this->print_fonts_links();
821 }
822
823 /**
824 * @return array|array[]
825 */
826 public function get_list_of_google_fonts_by_type(): array {
827 $google_fonts = [
828 'google' => [],
829 'early' => [],
830 ];
831
832 foreach ( $this->fonts_to_enqueue as $key => $font ) {
833 $font_type = Fonts::get_font_type( $font );
834
835 switch ( $font_type ) {
836 case Fonts::GOOGLE:
837 $google_fonts['google'][] = $font;
838 break;
839
840 case Fonts::EARLYACCESS:
841 $google_fonts['early'][] = $font;
842 break;
843
844 case false:
845 $this->maybe_enqueue_icon_font( $font );
846 break;
847 default:
848 /**
849 * Print font links.
850 *
851 * Fires when Elementor frontend fonts are printed on the HEAD tag.
852 *
853 * The dynamic portion of the hook name, `$font_type`, refers to the font type.
854 *
855 * @since 2.0.0
856 *
857 * @param string $font Font name.
858 */
859 do_action( "elementor/fonts/print_font_links/{$font_type}", $font );
860 }
861 }
862 $this->fonts_to_enqueue = [];
863
864 return $google_fonts;
865 }
866
867 /**
868 * Print fonts links.
869 *
870 * Enqueue all the frontend fonts by url.
871 *
872 * Fired by `wp_head` action.
873 *
874 * @since 1.9.4
875 * @access public
876 */
877 public function print_fonts_links() {
878 $google_fonts = $this->get_list_of_google_fonts_by_type();
879
880 $this->enqueue_google_fonts( $google_fonts );
881 $this->enqueue_icon_fonts();
882 }
883
884 private function maybe_enqueue_icon_font( $icon_font_type ) {
885 if ( ! Icons_Manager::is_migration_allowed() ) {
886 return;
887 }
888
889 $icons_types = Icons_Manager::get_icon_manager_tabs();
890 if ( ! isset( $icons_types[ $icon_font_type ] ) ) {
891 return;
892 }
893
894 $icon_type = $icons_types[ $icon_font_type ];
895 if ( isset( $icon_type['url'] ) ) {
896 $this->icon_fonts_to_enqueue[ $icon_font_type ] = [ $icon_type['url'] ];
897 }
898 }
899
900 private function enqueue_icon_fonts() {
901 if ( empty( $this->icon_fonts_to_enqueue ) || ! Icons_Manager::is_migration_allowed() ) {
902 return;
903 }
904
905 foreach ( $this->icon_fonts_to_enqueue as $icon_type => $css_url ) {
906 wp_enqueue_style( 'elementor-icons-' . $icon_type );
907 $this->enqueued_icon_fonts[] = $css_url;
908 }
909
910 // Clear enqueued icons.
911 $this->icon_fonts_to_enqueue = [];
912 }
913
914 /**
915 * @param array $fonts Stable google fonts ($google_fonts['google']).
916 * @return string
917 */
918 public function get_stable_google_fonts_url( array $fonts ): string {
919 foreach ( $fonts as &$font ) {
920 $font = str_replace( ' ', '+', $font ) . ':100,100italic,200,200italic,300,300italic,400,400italic,500,500italic,600,600italic,700,700italic,800,800italic,900,900italic';
921 }
922
923 // Defining a font-display type to google fonts.
924 $font_display_url_str = '&display=' . Fonts::get_font_display_setting();
925
926 $fonts_url = sprintf( 'https://fonts.googleapis.com/css?family=%1$s%2$s', implode( rawurlencode( '|' ), $fonts ), $font_display_url_str );
927
928 $subsets = [
929 'ru_RU' => 'cyrillic',
930 'bg_BG' => 'cyrillic',
931 'he_IL' => 'hebrew',
932 'el' => 'greek',
933 'vi' => 'vietnamese',
934 'uk' => 'cyrillic',
935 'cs_CZ' => 'latin-ext',
936 'ro_RO' => 'latin-ext',
937 'pl_PL' => 'latin-ext',
938 'hr_HR' => 'latin-ext',
939 'hu_HU' => 'latin-ext',
940 'sk_SK' => 'latin-ext',
941 'tr_TR' => 'latin-ext',
942 'lt_LT' => 'latin-ext',
943 ];
944
945 /**
946 * Google font subsets.
947 *
948 * Filters the list of Google font subsets from which locale will be enqueued in frontend.
949 *
950 * @since 1.0.0
951 *
952 * @param array $subsets A list of font subsets.
953 */
954 $subsets = apply_filters( 'elementor/frontend/google_font_subsets', $subsets );
955
956 $locale = get_locale();
957
958 if ( isset( $subsets[ $locale ] ) ) {
959 $fonts_url .= '&subset=' . $subsets[ $locale ];
960 }
961
962 return $fonts_url;
963 }
964
965 /**
966 * @param array $fonts Early Access google fonts ($google_fonts['early']).
967 * @return array
968 */
969 public function get_early_access_google_font_urls( array $fonts ): array {
970 $font_urls = [];
971
972 foreach ( $fonts as $font ) {
973 $font_urls[] = sprintf( 'https://fonts.googleapis.com/earlyaccess/%s.css', strtolower( str_replace( ' ', '', $font ) ) );
974 }
975
976 return $font_urls;
977 }
978
979 /**
980 * Print Google fonts.
981 *
982 * Enqueue all the frontend Google fonts.
983 *
984 * Fired by `wp_head` action.
985 *
986 * @since 1.0.0
987 * @access private
988 *
989 * @param array $google_fonts Optional. Google fonts to print in the frontend.
990 * Default is an empty array.
991 */
992 private function enqueue_google_fonts( $google_fonts = [] ) {
993 $print_google_fonts = Fonts::is_google_fonts_enabled();
994
995 /**
996 * Print frontend google fonts.
997 *
998 * Filters whether to enqueue Google fonts in the frontend.
999 *
1000 * @since 1.0.0
1001 *
1002 * @param bool $print_google_fonts Whether to enqueue Google fonts. Default is true.
1003 */
1004 $print_google_fonts = apply_filters( 'elementor/frontend/print_google_fonts', $print_google_fonts );
1005
1006 if ( ! $print_google_fonts ) {
1007 return;
1008 }
1009
1010 $force_enqueue_from_cdn = Plugin::$instance->preview->is_preview_mode();
1011
1012 if ( ! empty( $google_fonts['google'] ) ) {
1013 foreach ( $google_fonts['google'] as $current_font ) {
1014 Google_Font::enqueue( $current_font, Google_Font::TYPE_DEFAULT, $force_enqueue_from_cdn );
1015 }
1016 }
1017
1018 if ( ! empty( $google_fonts['early'] ) ) {
1019 foreach ( $google_fonts['early'] as $current_font ) {
1020 Google_Font::enqueue( $current_font, Google_Font::TYPE_EARLYACCESS, $force_enqueue_from_cdn );
1021 }
1022 }
1023 }
1024
1025 /**
1026 * Enqueue fonts.
1027 *
1028 * Enqueue all the frontend fonts.
1029 *
1030 * @since 1.2.0
1031 * @access public
1032 *
1033 * @param array $font Fonts to enqueue in the frontend.
1034 */
1035 public function enqueue_font( $font ) {
1036 if ( in_array( $font, $this->registered_fonts ) ) {
1037 return;
1038 }
1039
1040 $this->fonts_to_enqueue[] = $font;
1041 $this->registered_fonts[] = $font;
1042 }
1043
1044 /**
1045 * Apply builder in content.
1046 *
1047 * Used to apply the Elementor page editor on the post content.
1048 *
1049 * @since 1.0.0
1050 * @access public
1051 *
1052 * @param string $content The post content.
1053 *
1054 * @return string The post content.
1055 */
1056 public function apply_builder_in_content( $content ) {
1057 $this->restore_content_filters();
1058
1059 if ( Plugin::$instance->preview->is_preview_mode() || $this->_is_excerpt ) {
1060 return $content;
1061 }
1062
1063 // Remove the filter itself in order to allow other `the_content` in the elements
1064 $this->remove_content_filter();
1065
1066 $post_id = get_the_ID();
1067 $builder_content = $this->get_builder_content( $post_id );
1068
1069 if ( ! empty( $builder_content ) ) {
1070 $content = $builder_content;
1071 $this->remove_content_filters();
1072 }
1073
1074 // Add the filter again for other `the_content` calls
1075 $this->add_content_filter();
1076
1077 return $content;
1078 }
1079
1080 /**
1081 * Retrieve builder content.
1082 *
1083 * Used to render and return the post content with all the Elementor elements.
1084 *
1085 * Note that this method is an internal method, please use `get_builder_content_for_display()`.
1086 *
1087 * @since 1.0.0
1088 * @access public
1089 *
1090 * @param int $post_id The post ID.
1091 * @param bool $with_css Optional. Whether to retrieve the content with CSS
1092 * or not. Default is false.
1093 *
1094 * @return string The post content.
1095 */
1096 public function get_builder_content( $post_id, $with_css = false ) {
1097 if ( post_password_required( $post_id ) ) {
1098 return '';
1099 }
1100
1101 $document = Plugin::$instance->documents->get_doc_for_frontend( $post_id );
1102
1103 if ( ! $document || ! $document->is_built_with_elementor() ) {
1104 return '';
1105 }
1106
1107 // Change the current post, so widgets can use `documents->get_current`.
1108 Plugin::$instance->documents->switch_to_document( $document );
1109
1110 $data = $document->get_elements_data();
1111
1112 /**
1113 * Frontend builder content data.
1114 *
1115 * Filters the builder content in the frontend.
1116 *
1117 * @since 1.0.0
1118 *
1119 * @param array $data The builder content.
1120 * @param int $post_id The post ID.
1121 */
1122 $data = apply_filters( 'elementor/frontend/builder_content_data', $data, $post_id );
1123
1124 do_action( 'elementor/frontend/before_get_builder_content', $document, $this->_is_excerpt );
1125
1126 if ( empty( $data ) ) {
1127 Plugin::$instance->documents->restore_document();
1128
1129 return '';
1130 }
1131
1132 if ( ! $this->_is_excerpt ) {
1133 if ( $document->is_autosave() ) {
1134 $css_file = Post_Preview::create( $document->get_post()->ID );
1135 } else {
1136 $css_file = Post_CSS::create( $post_id );
1137 }
1138
1139 /**
1140 * Builder Content - Before Enqueue CSS File
1141 *
1142 * Allows intervening with a document's CSS file before it is enqueued.
1143 *
1144 * @param $css_file Post_CSS|Post_Preview
1145 */
1146 $css_file = apply_filters( 'elementor/frontend/builder_content/before_enqueue_css_file', $css_file );
1147
1148 $css_file->enqueue();
1149 }
1150
1151 ob_start();
1152
1153 // Handle JS and Customizer requests, with CSS inline.
1154 if ( is_customize_preview() || wp_doing_ajax() ) {
1155 $with_css = true;
1156 }
1157
1158 /**
1159 * Builder Content - With CSS
1160 *
1161 * Allows overriding the `$with_css` parameter which is a factor in determining whether to print the document's
1162 * CSS and font links inline in a `style` tag above the document's markup.
1163 *
1164 * @param $with_css boolean
1165 */
1166 $with_css = apply_filters( 'elementor/frontend/builder_content/before_print_css', $with_css );
1167
1168 if ( ! empty( $css_file ) && $with_css ) {
1169 $css_file->print_css();
1170 }
1171
1172 $document->print_elements_with_wrapper( $data );
1173
1174 $content = ob_get_clean();
1175
1176 $content = $this->process_more_tag( $content );
1177
1178 /**
1179 * Frontend content.
1180 *
1181 * Filters the content in the frontend.
1182 *
1183 * @since 1.0.0
1184 *
1185 * @param string $content The content.
1186 */
1187 $content = apply_filters( 'elementor/frontend/the_content', $content );
1188
1189 if ( ! empty( $content ) ) {
1190 $this->_has_elementor_in_page = true;
1191 }
1192
1193 Plugin::$instance->documents->restore_document();
1194
1195 // BC
1196 // TODO: use Deprecation::do_deprecated_action() in 3.1.0
1197 do_action( 'elementor/frontend/get_builder_content', $document, $this->_is_excerpt, $with_css );
1198
1199 return $content;
1200 }
1201
1202 /**
1203 * Retrieve builder content for display.
1204 *
1205 * Used to render and return the post content with all the Elementor elements.
1206 *
1207 * @since 1.0.0
1208 * @access public
1209 *
1210 * @param int $post_id The post ID.
1211 *
1212 * @param bool $with_css Optional. Whether to retrieve the content with CSS
1213 * or not. Default is false.
1214 *
1215 * @return string The post content.
1216 */
1217 public function get_builder_content_for_display( $post_id, $with_css = false ) {
1218 if ( ! get_post( $post_id ) ) {
1219 return '';
1220 }
1221
1222 $editor = Plugin::$instance->editor;
1223
1224 // Avoid recursion
1225 if ( get_the_ID() === (int) $post_id ) {
1226 $content = '';
1227 if ( $editor->is_edit_mode() ) {
1228 $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>';
1229 }
1230
1231 return $content;
1232 }
1233
1234 // 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
1235 $is_edit_mode = $editor->is_edit_mode();
1236 $editor->set_edit_mode( false );
1237
1238 $with_css = $with_css ? true : $is_edit_mode;
1239
1240 $content = $this->get_builder_content( $post_id, $with_css );
1241
1242 // Restore edit mode state
1243 Plugin::$instance->editor->set_edit_mode( $is_edit_mode );
1244
1245 return $content;
1246 }
1247
1248 /**
1249 * Start excerpt flag.
1250 *
1251 * Flags when `the_excerpt` is called. Used to avoid enqueueing CSS in the excerpt.
1252 *
1253 * @since 1.4.3
1254 * @access public
1255 *
1256 * @param string $excerpt The post excerpt.
1257 *
1258 * @return string The post excerpt.
1259 */
1260 public function start_excerpt_flag( $excerpt ) {
1261 $this->_is_excerpt = true;
1262 return $excerpt;
1263 }
1264
1265 /**
1266 * End excerpt flag.
1267 *
1268 * Flags when `the_excerpt` call ended.
1269 *
1270 * @since 1.4.3
1271 * @access public
1272 *
1273 * @param string $excerpt The post excerpt.
1274 *
1275 * @return string The post excerpt.
1276 */
1277 public function end_excerpt_flag( $excerpt ) {
1278 $this->_is_excerpt = false;
1279 return $excerpt;
1280 }
1281
1282 /**
1283 * Remove content filters.
1284 *
1285 * Remove WordPress default filters that conflicted with Elementor.
1286 *
1287 * @since 1.5.0
1288 * @access public
1289 */
1290 public function remove_content_filters() {
1291 $filters = [
1292 'wpautop',
1293 'shortcode_unautop',
1294 'wptexturize',
1295 ];
1296
1297 foreach ( $filters as $filter ) {
1298 // Check if another plugin/theme do not already removed the filter.
1299 if ( has_filter( 'the_content', $filter ) ) {
1300 remove_filter( 'the_content', $filter );
1301 $this->content_removed_filters[] = $filter;
1302 }
1303 }
1304 }
1305
1306 /**
1307 * Has Elementor In Page
1308 *
1309 * Determine whether the current page is using Elementor.
1310 *
1311 * @since 2.0.9
1312 *
1313 * @access public
1314 * @return bool
1315 */
1316 public function has_elementor_in_page() {
1317 return $this->_has_elementor_in_page;
1318 }
1319
1320 public function create_action_hash( $action, array $settings = [] ) {
1321 return '#' . rawurlencode( sprintf( 'elementor-action:action=%1$s&settings=%2$s', $action, base64_encode( wp_json_encode( $settings ) ) ) );
1322 }
1323
1324 /**
1325 * Is the current render mode is static.
1326 *
1327 * @return bool
1328 */
1329 public function is_static_render_mode() {
1330 // The render mode manager is exists only in frontend,
1331 // so by default if it is not exist the method will return false.
1332 if ( ! $this->render_mode_manager ) {
1333 return false;
1334 }
1335
1336 return $this->render_mode_manager->get_current()->is_static();
1337 }
1338
1339 /**
1340 * Get Init Settings
1341 *
1342 * Used to define the default/initial settings of the object. Inheriting classes may implement this method to define
1343 * their own default/initial settings.
1344 *
1345 * @since 2.3.0
1346 *
1347 * @access protected
1348 * @return array
1349 */
1350 protected function get_init_settings() {
1351 $is_preview_mode = Plugin::$instance->preview->is_preview_mode( Plugin::$instance->preview->get_post_id() );
1352
1353 $active_experimental_features = Plugin::$instance->experiments->get_active_features();
1354
1355 $active_experimental_features = array_fill_keys( array_keys( $active_experimental_features ), true );
1356
1357 $assets_url = ELEMENTOR_ASSETS_URL;
1358
1359 /**
1360 * Frontend assets URL
1361 *
1362 * Filters Elementor frontend assets URL.
1363 *
1364 * @since 2.3.0
1365 *
1366 * @param string $assets_url The frontend assets URL. Default is ELEMENTOR_ASSETS_URL.
1367 */
1368 $assets_url = apply_filters( 'elementor/frontend/assets_url', $assets_url );
1369
1370 $settings = [
1371 'environmentMode' => [
1372 'edit' => $is_preview_mode,
1373 'wpPreview' => is_preview(),
1374 'isScriptDebug' => Utils::is_script_debug(),
1375 ],
1376 'i18n' => [
1377 'shareOnFacebook' => esc_html__( 'Share on Facebook', 'elementor' ),
1378 'shareOnTwitter' => esc_html__( 'Share on Twitter', 'elementor' ),
1379 'pinIt' => esc_html__( 'Pin it', 'elementor' ),
1380 'download' => esc_html__( 'Download', 'elementor' ),
1381 'downloadImage' => esc_html__( 'Download image', 'elementor' ),
1382 'fullscreen' => esc_html__( 'Fullscreen', 'elementor' ),
1383 'zoom' => esc_html__( 'Zoom', 'elementor' ),
1384 'share' => esc_html__( 'Share', 'elementor' ),
1385 'playVideo' => esc_html__( 'Play Video', 'elementor' ),
1386 'previous' => esc_html__( 'Previous', 'elementor' ),
1387 'next' => esc_html__( 'Next', 'elementor' ),
1388 'close' => esc_html__( 'Close', 'elementor' ),
1389 'a11yCarouselPrevSlideMessage' => __( 'Previous slide', 'elementor' ),
1390 'a11yCarouselNextSlideMessage' => __( 'Next slide', 'elementor' ),
1391 'a11yCarouselFirstSlideMessage' => __( 'This is the first slide', 'elementor' ),
1392 'a11yCarouselLastSlideMessage' => __( 'This is the last slide', 'elementor' ),
1393 'a11yCarouselPaginationBulletMessage' => __( 'Go to slide', 'elementor' ),
1394 ],
1395 'is_rtl' => is_rtl(),
1396 // 'breakpoints' object is kept for BC.
1397 'breakpoints' => Responsive::get_breakpoints(),
1398 // 'responsive' contains the custom breakpoints config introduced in Elementor v3.2.0
1399 'responsive' => [
1400 'breakpoints' => Plugin::$instance->breakpoints->get_breakpoints_config(),
1401 'hasCustomBreakpoints' => Plugin::$instance->breakpoints->has_custom_breakpoints(),
1402 ],
1403 'version' => ELEMENTOR_VERSION,
1404 'is_static' => $this->is_static_render_mode(),
1405 'experimentalFeatures' => $active_experimental_features,
1406 'urls' => [
1407 'assets' => $assets_url,
1408 'ajaxurl' => admin_url( 'admin-ajax.php' ),
1409 'uploadUrl' => wp_upload_dir()['baseurl'],
1410 ],
1411 'nonces' => [
1412 'floatingButtonsClickTracking' => wp_create_nonce( Module::CLICK_TRACKING_NONCE ),
1413 ],
1414 'swiperClass' => 'swiper',
1415 ];
1416
1417 $settings['settings'] = SettingsManager::get_settings_frontend_config();
1418
1419 $kit = Plugin::$instance->kits_manager->get_active_kit_for_frontend();
1420 $settings['kit'] = $kit->get_frontend_settings();
1421
1422 if ( is_singular() ) {
1423 $post = get_post();
1424
1425 $title = Utils::urlencode_html_entities( wp_get_document_title() );
1426
1427 // Try to use the 'large' WP image size because the Pinterest share API
1428 // has problems accepting shares with large images sometimes, and the WP 'large' thumbnail is
1429 // the largest default WP image size that will probably not be changed in most sites
1430 $featured_image_url = get_the_post_thumbnail_url( null, 'large' );
1431
1432 // If the large size was nullified, use the full size which cannot be nullified/deleted
1433 if ( ! $featured_image_url ) {
1434 $featured_image_url = get_the_post_thumbnail_url( null, 'full' );
1435 }
1436
1437 $settings['post'] = [
1438 'id' => $post->ID,
1439 'title' => $title,
1440 'excerpt' => $post->post_excerpt,
1441 'featuredImage' => $featured_image_url,
1442 ];
1443 } else {
1444 $settings['post'] = [
1445 'id' => 0,
1446 'title' => wp_get_document_title(),
1447 'excerpt' => get_the_archive_description(),
1448 ];
1449 }
1450
1451 $empty_object = (object) [];
1452
1453 if ( $is_preview_mode ) {
1454 $settings['elements'] = [
1455 'data' => $empty_object,
1456 'editSettings' => $empty_object,
1457 'keys' => $empty_object,
1458 ];
1459 }
1460
1461 if ( is_user_logged_in() ) {
1462 $user = wp_get_current_user();
1463
1464 if ( ! empty( $user->roles ) ) {
1465 $settings['user'] = [
1466 'roles' => $user->roles,
1467 ];
1468 }
1469 }
1470
1471 return $settings;
1472 }
1473
1474 /**
1475 * Restore content filters.
1476 *
1477 * Restore removed WordPress filters that conflicted with Elementor.
1478 *
1479 * @since 1.5.0
1480 * @access public
1481 */
1482 public function restore_content_filters() {
1483 foreach ( $this->content_removed_filters as $filter ) {
1484 add_filter( 'the_content', $filter );
1485 }
1486
1487 $this->content_removed_filters = [];
1488 }
1489
1490 /**
1491 * Process More Tag
1492 *
1493 * Respect the native WP (<!--more-->) tag
1494 *
1495 * @access private
1496 * @since 2.0.4
1497 *
1498 * @param string $content
1499 *
1500 * @return string
1501 */
1502 private function process_more_tag( $content ) {
1503 $post = get_post();
1504 $content = str_replace( '&lt;!--more--&gt;', '<!--more-->', $content );
1505 $parts = get_extended( $content );
1506 if ( empty( $parts['extended'] ) ) {
1507 return $content;
1508 }
1509
1510 if ( is_singular() ) {
1511 return $parts['main'] . '<div id="more-' . $post->ID . '"></div>' . $parts['extended'];
1512 }
1513
1514 if ( empty( $parts['more_text'] ) ) {
1515 $parts['more_text'] = esc_html__( '(more&hellip;)', 'elementor' );
1516 }
1517
1518 $more_link_text = sprintf(
1519 '<span aria-label="%1$s">%2$s</span>',
1520 sprintf(
1521 /* translators: %s: Current post name. */
1522 __( 'Continue reading %s', 'elementor' ),
1523 the_title_attribute( [
1524 'echo' => false,
1525 ] )
1526 ),
1527 $parts['more_text']
1528 );
1529
1530 $more_link = sprintf( ' <a href="%s#more-%s" class="more-link elementor-more-link">%s</a>', get_permalink(), $post->ID, $more_link_text );
1531
1532 /**
1533 * The content "more" link.
1534 *
1535 * Filters the "more" link displayed after the content.
1536 *
1537 * This hook can be used either to change the link syntax or to change the
1538 * text inside the link.
1539 *
1540 * @since 2.0.4
1541 *
1542 * @param string $more_link The more link.
1543 * @param string $more_link_text The text inside the more link.
1544 */
1545 $more_link = apply_filters( 'the_content_more_link', $more_link, $more_link_text );
1546
1547 return force_balance_tags( $parts['main'] ) . $more_link;
1548 }
1549 }
1550