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

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