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

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

1,333 lines 31.1 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\Document;
6 use Elementor\Core\Frontend\Render_Mode_Manager;
7 use Elementor\Core\Responsive\Files\Frontend as FrontendFile;
8 use Elementor\Core\Files\CSS\Global_CSS;
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
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 if ( is_singular() && Plugin::$instance->db->is_built_with_elementor( $this->post_id ) ) {
224 add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_styles' ] );
225 }
226
227 // Priority 7 to allow google fonts in header template to load in <head> tag
228 add_action( 'wp_head', [ $this, 'print_fonts_links' ], 7 );
229 add_action( 'wp_head', [ $this, 'add_theme_color_meta_tag' ] );
230 add_action( 'wp_footer', [ $this, 'wp_footer' ] );
231 }
232
233 /**
234 * @since 2.0.12
235 * @access public
236 * @param string|array $class
237 */
238 public function add_body_class( $class ) {
239 if ( is_array( $class ) ) {
240 $this->body_classes = array_merge( $this->body_classes, $class );
241 } else {
242 $this->body_classes[] = $class;
243 }
244 }
245
246 /**
247 * Add Theme Color Meta Tag
248 *
249 * @since 3.0.0
250 * @access public
251 */
252 public function add_theme_color_meta_tag() {
253 $kit = Plugin::$instance->kits_manager->get_active_kit_for_frontend();
254 $mobile_theme_color = $kit->get_settings( 'mobile_theme_color' );
255
256 if ( ! empty( $mobile_theme_color ) ) {
257 ?>
258 <meta name="theme-color" content="<?php echo $mobile_theme_color; ?>">
259 <?php
260 }
261 }
262
263 /**
264 * Body tag classes.
265 *
266 * Add new elementor classes to the body tag.
267 *
268 * Fired by `body_class` filter.
269 *
270 * @since 1.0.0
271 * @access public
272 *
273 * @param array $classes Optional. One or more classes to add to the body tag class list.
274 * Default is an empty array.
275 *
276 * @return array Body tag classes.
277 */
278 public function body_class( $classes = [] ) {
279 $classes = array_merge( $classes, $this->body_classes );
280
281 $id = get_the_ID();
282
283 if ( is_singular() && Plugin::$instance->db->is_built_with_elementor( $id ) ) {
284 $classes[] = 'elementor-page elementor-page-' . $id;
285 }
286
287 return $classes;
288 }
289
290 /**
291 * Add content filter.
292 *
293 * Remove plain content and render the content generated by Elementor.
294 *
295 * @since 1.8.0
296 * @access public
297 */
298 public function add_content_filter() {
299 add_filter( 'the_content', [ $this, 'apply_builder_in_content' ], self::THE_CONTENT_FILTER_PRIORITY );
300 }
301
302 /**
303 * Remove content filter.
304 *
305 * When the Elementor generated content rendered, we remove the filter to prevent multiple
306 * accuracies. This way we make sure Elementor renders the content only once.
307 *
308 * @since 1.8.0
309 * @access public
310 */
311 public function remove_content_filter() {
312 remove_filter( 'the_content', [ $this, 'apply_builder_in_content' ], self::THE_CONTENT_FILTER_PRIORITY );
313 }
314
315 /**
316 * Registers scripts.
317 *
318 * Registers all the frontend scripts.
319 *
320 * Fired by `wp_enqueue_scripts` action.
321 *
322 * @since 1.2.1
323 * @access public
324 */
325 public function register_scripts() {
326 /**
327 * Before frontend register scripts.
328 *
329 * Fires before Elementor frontend scripts are registered.
330 *
331 * @since 1.2.1
332 */
333 do_action( 'elementor/frontend/before_register_scripts' );
334
335 wp_register_script(
336 'elementor-webpack-runtime',
337 $this->get_js_assets_url( 'webpack.runtime', 'assets/js/' ),
338 [],
339 ELEMENTOR_VERSION,
340 true
341 );
342
343 wp_register_script(
344 'elementor-frontend-modules',
345 $this->get_js_assets_url( 'frontend-modules' ),
346 [
347 'elementor-webpack-runtime',
348 'jquery',
349 ],
350 ELEMENTOR_VERSION,
351 true
352 );
353
354 wp_register_script(
355 'elementor-waypoints',
356 $this->get_js_assets_url( 'waypoints', 'assets/lib/waypoints/' ),
357 [
358 'jquery',
359 ],
360 '4.0.2',
361 true
362 );
363
364 wp_register_script(
365 'flatpickr',
366 $this->get_js_assets_url( 'flatpickr', 'assets/lib/flatpickr/' ),
367 [
368 'jquery',
369 ],
370 '4.1.4',
371 true
372 );
373
374 wp_register_script(
375 'imagesloaded',
376 $this->get_js_assets_url( 'imagesloaded', 'assets/lib/imagesloaded/' ),
377 [
378 'jquery',
379 ],
380 '4.1.0',
381 true
382 );
383
384 wp_register_script(
385 'jquery-numerator',
386 $this->get_js_assets_url( 'jquery-numerator', 'assets/lib/jquery-numerator/' ),
387 [
388 'jquery',
389 ],
390 '0.2.1',
391 true
392 );
393
394 /**
395 * @deprecated since 2.7.0 Use Swiper instead
396 */
397 wp_register_script(
398 'jquery-slick',
399 $this->get_js_assets_url( 'slick', 'assets/lib/slick/' ),
400 [
401 'jquery',
402 ],
403 '1.8.1',
404 true
405 );
406
407 wp_register_script(
408 'elementor-dialog',
409 $this->get_js_assets_url( 'dialog', 'assets/lib/dialog/' ),
410 [
411 'jquery-ui-position',
412 ],
413 '4.8.1',
414 true
415 );
416
417 wp_register_script(
418 'elementor-gallery',
419 $this->get_js_assets_url( 'e-gallery', 'assets/lib/e-gallery/js/' ),
420 [
421 'jquery',
422 ],
423 '1.2.0',
424 true
425 );
426
427 wp_register_script(
428 'share-link',
429 $this->get_js_assets_url( 'share-link', 'assets/lib/share-link/' ),
430 [
431 'jquery',
432 ],
433 ELEMENTOR_VERSION,
434 true
435 );
436
437 wp_register_script(
438 'elementor-frontend',
439 $this->get_js_assets_url( 'frontend' ),
440 $this->get_elementor_frontend_dependencies(),
441 ELEMENTOR_VERSION,
442 true
443 );
444
445 /**
446 * After frontend register scripts.
447 *
448 * Fires after Elementor frontend scripts are registered.
449 *
450 * @since 1.2.1
451 */
452 do_action( 'elementor/frontend/after_register_scripts' );
453 }
454
455 /**
456 * Registers styles.
457 *
458 * Registers all the frontend styles.
459 *
460 * Fired by `wp_enqueue_scripts` action.
461 *
462 * @since 1.2.0
463 * @access public
464 */
465 public function register_styles() {
466 /**
467 * Before frontend register styles.
468 *
469 * Fires before Elementor frontend styles are registered.
470 *
471 * @since 1.2.0
472 */
473 do_action( 'elementor/frontend/before_register_styles' );
474
475 wp_register_style(
476 'font-awesome',
477 $this->get_css_assets_url( 'font-awesome', 'assets/lib/font-awesome/css/' ),
478 [],
479 '4.7.0'
480 );
481
482 wp_register_style(
483 'elementor-icons',
484 $this->get_css_assets_url( 'elementor-icons', 'assets/lib/eicons/css/' ),
485 [],
486 '5.10.0'
487 );
488
489 wp_register_style(
490 'elementor-animations',
491 $this->get_css_assets_url( 'animations', 'assets/lib/animations/', true ),
492 [],
493 ELEMENTOR_VERSION
494 );
495
496 wp_register_style(
497 'flatpickr',
498 $this->get_css_assets_url( 'flatpickr', 'assets/lib/flatpickr/' ),
499 [],
500 '4.1.4'
501 );
502
503 wp_register_style(
504 'elementor-gallery',
505 $this->get_css_assets_url( 'e-gallery', 'assets/lib/e-gallery/css/' ),
506 [],
507 '1.2.0'
508 );
509
510 $min_suffix = Utils::is_script_debug() ? '' : '.min';
511
512 $direction_suffix = is_rtl() ? '-rtl' : '';
513
514 $frontend_file_name = 'frontend' . $direction_suffix . $min_suffix . '.css';
515
516 $has_custom_file = Responsive::has_custom_breakpoints();
517
518 if ( $has_custom_file ) {
519 $frontend_file = new FrontendFile( 'custom-' . $frontend_file_name, Responsive::get_stylesheet_templates_path() . $frontend_file_name );
520
521 $time = $frontend_file->get_meta( 'time' );
522
523 if ( ! $time ) {
524 $frontend_file->update();
525 }
526
527 $frontend_file_url = $frontend_file->get_url();
528 } else {
529 $frontend_file_url = ELEMENTOR_ASSETS_URL . 'css/' . $frontend_file_name;
530 }
531
532 $frontend_dependencies = [];
533
534 if ( ! Plugin::$instance->experiments->is_feature_active( 'e_dom_optimization' ) ) {
535 // If The Dom Optimization feature is disabled, register the legacy CSS
536 wp_register_style(
537 'elementor-frontend-legacy',
538 ELEMENTOR_ASSETS_URL . 'css/frontend-legacy' . $direction_suffix . $min_suffix . '.css',
539 [],
540 ELEMENTOR_VERSION
541 );
542
543 $frontend_dependencies[] = 'elementor-frontend-legacy';
544 }
545
546 wp_register_style(
547 'elementor-frontend',
548 $frontend_file_url,
549 $frontend_dependencies,
550 $has_custom_file ? null : ELEMENTOR_VERSION
551 );
552
553 /**
554 * After frontend register styles.
555 *
556 * Fires after Elementor frontend styles are registered.
557 *
558 * @since 1.2.0
559 */
560 do_action( 'elementor/frontend/after_register_styles' );
561 }
562
563 /**
564 * Enqueue scripts.
565 *
566 * Enqueue all the frontend scripts.
567 *
568 * @since 1.0.0
569 * @access public
570 */
571 public function enqueue_scripts() {
572 /**
573 * Before frontend enqueue scripts.
574 *
575 * Fires before Elementor frontend scripts are enqueued.
576 *
577 * @since 1.0.0
578 */
579 do_action( 'elementor/frontend/before_enqueue_scripts' );
580
581 wp_enqueue_script( 'elementor-frontend' );
582
583 wp_set_script_translations( 'elementor-frontend', 'elementor' );
584
585 if ( ! $this->is_improved_assets_loading() ) {
586 wp_enqueue_script(
587 'preloaded-elements-handlers',
588 $this->get_js_assets_url( 'preloaded-elements-handlers', 'assets/js/' ),
589 [
590 'elementor-frontend',
591 ],
592 ELEMENTOR_VERSION,
593 true
594 );
595 }
596
597 $this->print_config();
598
599 /**
600 * After frontend enqueue scripts.
601 *
602 * Fires after Elementor frontend scripts are enqueued.
603 *
604 * @since 1.0.0
605 */
606 do_action( 'elementor/frontend/after_enqueue_scripts' );
607 }
608
609 /**
610 * Enqueue styles.
611 *
612 * Enqueue all the frontend styles.
613 *
614 * Fired by `wp_enqueue_scripts` action.
615 *
616 * @since 1.0.0
617 * @access public
618 */
619 public function enqueue_styles() {
620 /**
621 * Before frontend styles enqueued.
622 *
623 * Fires before Elementor frontend styles are enqueued.
624 *
625 * @since 1.0.0
626 */
627 do_action( 'elementor/frontend/before_enqueue_styles' );
628
629 wp_enqueue_style( 'elementor-icons' );
630 wp_enqueue_style( 'elementor-animations' );
631 wp_enqueue_style( 'elementor-frontend' );
632
633 /**
634 * After frontend styles enqueued.
635 *
636 * Fires after Elementor frontend styles are enqueued.
637 *
638 * @since 1.0.0
639 */
640 do_action( 'elementor/frontend/after_enqueue_styles' );
641
642 if ( ! Plugin::$instance->preview->is_preview_mode() ) {
643 $this->parse_global_css_code();
644
645 $post_id = get_the_ID();
646 // Check $post_id for virtual pages. check is singular because the $post_id is set to the first post on archive pages.
647 if ( $post_id && is_singular() ) {
648 $css_file = Post_CSS::create( get_the_ID() );
649 $css_file->enqueue();
650 }
651 }
652 }
653
654 /**
655 * Elementor footer scripts and styles.
656 *
657 * Handle styles and scripts that are not printed in the header.
658 *
659 * Fired by `wp_footer` action.
660 *
661 * @since 1.0.11
662 * @access public
663 */
664 public function wp_footer() {
665 if ( ! $this->_has_elementor_in_page ) {
666 return;
667 }
668
669 $this->enqueue_styles();
670 $this->enqueue_scripts();
671
672 $this->print_fonts_links();
673 }
674
675 /**
676 * Print fonts links.
677 *
678 * Enqueue all the frontend fonts by url.
679 *
680 * Fired by `wp_head` action.
681 *
682 * @since 1.9.4
683 * @access public
684 */
685 public function print_fonts_links() {
686 $google_fonts = [
687 'google' => [],
688 'early' => [],
689 ];
690
691 foreach ( $this->fonts_to_enqueue as $key => $font ) {
692 $font_type = Fonts::get_font_type( $font );
693
694 switch ( $font_type ) {
695 case Fonts::GOOGLE:
696 $google_fonts['google'][] = $font;
697 break;
698
699 case Fonts::EARLYACCESS:
700 $google_fonts['early'][] = $font;
701 break;
702
703 case false:
704 $this->maybe_enqueue_icon_font( $font );
705 break;
706 default:
707 /**
708 * Print font links.
709 *
710 * Fires when Elementor frontend fonts are printed on the HEAD tag.
711 *
712 * The dynamic portion of the hook name, `$font_type`, refers to the font type.
713 *
714 * @since 2.0.0
715 *
716 * @param string $font Font name.
717 */
718 do_action( "elementor/fonts/print_font_links/{$font_type}", $font );
719 }
720 }
721 $this->fonts_to_enqueue = [];
722
723 $this->enqueue_google_fonts( $google_fonts );
724 $this->enqueue_icon_fonts();
725 }
726
727 private function maybe_enqueue_icon_font( $icon_font_type ) {
728 if ( ! Icons_Manager::is_migration_allowed() ) {
729 return;
730 }
731
732 $icons_types = Icons_Manager::get_icon_manager_tabs();
733 if ( ! isset( $icons_types[ $icon_font_type ] ) ) {
734 return;
735 }
736
737 $icon_type = $icons_types[ $icon_font_type ];
738 if ( isset( $icon_type['url'] ) ) {
739 $this->icon_fonts_to_enqueue[ $icon_font_type ] = [ $icon_type['url'] ];
740 }
741 }
742
743 private function enqueue_icon_fonts() {
744 if ( empty( $this->icon_fonts_to_enqueue ) || ! Icons_Manager::is_migration_allowed() ) {
745 return;
746 }
747
748 foreach ( $this->icon_fonts_to_enqueue as $icon_type => $css_url ) {
749 wp_enqueue_style( 'elementor-icons-' . $icon_type );
750 $this->enqueued_icon_fonts[] = $css_url;
751 }
752
753 //clear enqueued icons
754 $this->icon_fonts_to_enqueue = [];
755 }
756
757 /**
758 * Print Google fonts.
759 *
760 * Enqueue all the frontend Google fonts.
761 *
762 * Fired by `wp_head` action.
763 *
764 * @since 1.0.0
765 * @access private
766 *
767 * @param array $google_fonts Optional. Google fonts to print in the frontend.
768 * Default is an empty array.
769 */
770 private function enqueue_google_fonts( $google_fonts = [] ) {
771 static $google_fonts_index = 0;
772
773 $print_google_fonts = true;
774
775 /**
776 * Print frontend google fonts.
777 *
778 * Filters whether to enqueue Google fonts in the frontend.
779 *
780 * @since 1.0.0
781 *
782 * @param bool $print_google_fonts Whether to enqueue Google fonts. Default is true.
783 */
784 $print_google_fonts = apply_filters( 'elementor/frontend/print_google_fonts', $print_google_fonts );
785
786 if ( ! $print_google_fonts ) {
787 return;
788 }
789
790 // Print used fonts
791 if ( ! empty( $google_fonts['google'] ) ) {
792 $google_fonts_index++;
793
794 foreach ( $google_fonts['google'] as &$font ) {
795 $font = str_replace( ' ', '+', $font ) . ':100,100italic,200,200italic,300,300italic,400,400italic,500,500italic,600,600italic,700,700italic,800,800italic,900,900italic';
796 }
797
798 $fonts_url = sprintf( 'https://fonts.googleapis.com/css?family=%s', implode( rawurlencode( '|' ), $google_fonts['google'] ) );
799
800 $subsets = [
801 'ru_RU' => 'cyrillic',
802 'bg_BG' => 'cyrillic',
803 'he_IL' => 'hebrew',
804 'el' => 'greek',
805 'vi' => 'vietnamese',
806 'uk' => 'cyrillic',
807 'cs_CZ' => 'latin-ext',
808 'ro_RO' => 'latin-ext',
809 'pl_PL' => 'latin-ext',
810 'hr_HR' => 'latin-ext',
811 'hu_HU' => 'latin-ext',
812 'sk_SK' => 'latin-ext',
813 'tr_TR' => 'latin-ext',
814 'lt_LT' => 'latin-ext',
815 ];
816
817 $subsets = apply_filters( 'elementor/frontend/google_font_subsets', $subsets );
818
819 $locale = get_locale();
820
821 if ( isset( $subsets[ $locale ] ) ) {
822 $fonts_url .= '&subset=' . $subsets[ $locale ];
823 }
824
825 wp_enqueue_style( 'google-fonts-' . $google_fonts_index, $fonts_url ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
826 }
827
828 if ( ! empty( $google_fonts['early'] ) ) {
829 foreach ( $google_fonts['early'] as $current_font ) {
830 $google_fonts_index++;
831
832 //printf( '<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/earlyaccess/%s.css">', strtolower( str_replace( ' ', '', $current_font ) ) );
833
834 $font_url = sprintf( 'https://fonts.googleapis.com/earlyaccess/%s.css', strtolower( str_replace( ' ', '', $current_font ) ) );
835
836 wp_enqueue_style( 'google-earlyaccess-' . $google_fonts_index, $font_url ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
837 }
838 }
839
840 }
841
842 /**
843 * Enqueue fonts.
844 *
845 * Enqueue all the frontend fonts.
846 *
847 * @since 1.2.0
848 * @access public
849 *
850 * @param array $font Fonts to enqueue in the frontend.
851 */
852 public function enqueue_font( $font ) {
853 if ( in_array( $font, $this->registered_fonts ) ) {
854 return;
855 }
856
857 $this->fonts_to_enqueue[] = $font;
858 $this->registered_fonts[] = $font;
859 }
860
861 /**
862 * Parse global CSS.
863 *
864 * Enqueue the global CSS file.
865 *
866 * @since 1.2.0
867 * @access protected
868 */
869 protected function parse_global_css_code() {
870 $scheme_css_file = Global_CSS::create( 'global.css' );
871
872 $scheme_css_file->enqueue();
873 }
874
875 /**
876 * Apply builder in content.
877 *
878 * Used to apply the Elementor page editor on the post content.
879 *
880 * @since 1.0.0
881 * @access public
882 *
883 * @param string $content The post content.
884 *
885 * @return string The post content.
886 */
887 public function apply_builder_in_content( $content ) {
888 $this->restore_content_filters();
889
890 if ( Plugin::$instance->preview->is_preview_mode() || $this->_is_excerpt ) {
891 return $content;
892 }
893
894 // Remove the filter itself in order to allow other `the_content` in the elements
895 $this->remove_content_filter();
896
897 $post_id = get_the_ID();
898 $builder_content = $this->get_builder_content( $post_id );
899
900 if ( ! empty( $builder_content ) ) {
901 $content = $builder_content;
902 $this->remove_content_filters();
903 }
904
905 // Add the filter again for other `the_content` calls
906 $this->add_content_filter();
907
908 return $content;
909 }
910
911 /**
912 * Retrieve builder content.
913 *
914 * Used to render and return the post content with all the Elementor elements.
915 *
916 * Note that this method is an internal method, please use `get_builder_content_for_display()`.
917 *
918 * @since 1.0.0
919 * @access public
920 *
921 * @param int $post_id The post ID.
922 * @param bool $with_css Optional. Whether to retrieve the content with CSS
923 * or not. Default is false.
924 *
925 * @return string The post content.
926 */
927 public function get_builder_content( $post_id, $with_css = false ) {
928 if ( post_password_required( $post_id ) ) {
929 return '';
930 }
931
932 if ( ! Plugin::$instance->db->is_built_with_elementor( $post_id ) ) {
933 return '';
934 }
935
936 $document = Plugin::$instance->documents->get_doc_for_frontend( $post_id );
937
938 // Change the current post, so widgets can use `documents->get_current`.
939 Plugin::$instance->documents->switch_to_document( $document );
940
941 $data = $document->get_elements_data();
942
943 /**
944 * Frontend builder content data.
945 *
946 * Filters the builder content in the frontend.
947 *
948 * @since 1.0.0
949 *
950 * @param array $data The builder content.
951 * @param int $post_id The post ID.
952 */
953 $data = apply_filters( 'elementor/frontend/builder_content_data', $data, $post_id );
954
955 do_action( 'elementor/frontend/before_get_builder_content', $document, $this->_is_excerpt );
956
957 if ( empty( $data ) ) {
958 Plugin::$instance->documents->restore_document();
959
960 return '';
961 }
962
963 if ( ! $this->_is_excerpt ) {
964 if ( $document->is_autosave() ) {
965 $css_file = Post_Preview::create( $document->get_post()->ID );
966 } else {
967 $css_file = Post_CSS::create( $post_id );
968 }
969
970 $css_file->enqueue();
971 }
972
973 ob_start();
974
975 // Handle JS and Customizer requests, with CSS inline.
976 if ( is_customize_preview() || wp_doing_ajax() ) {
977 $with_css = true;
978 }
979
980 if ( ! empty( $css_file ) && $with_css ) {
981 $css_file->print_css();
982 }
983
984 $document->print_elements_with_wrapper( $data );
985
986 $content = ob_get_clean();
987
988 $content = $this->process_more_tag( $content );
989
990 /**
991 * Frontend content.
992 *
993 * Filters the content in the frontend.
994 *
995 * @since 1.0.0
996 *
997 * @param string $content The content.
998 */
999 $content = apply_filters( 'elementor/frontend/the_content', $content );
1000
1001 if ( ! empty( $content ) ) {
1002 $this->_has_elementor_in_page = true;
1003 }
1004
1005 Plugin::$instance->documents->restore_document();
1006
1007 // BC
1008 // TODO: use Deprecation::do_deprecated_action() in 3.1.0
1009 do_action( 'elementor/frontend/get_builder_content', $document, $this->_is_excerpt, $with_css );
1010
1011 return $content;
1012 }
1013
1014 /**
1015 * Retrieve builder content for display.
1016 *
1017 * Used to render and return the post content with all the Elementor elements.
1018 *
1019 * @since 1.0.0
1020 * @access public
1021 *
1022 * @param int $post_id The post ID.
1023 *
1024 * @param bool $with_css Optional. Whether to retrieve the content with CSS
1025 * or not. Default is false.
1026 *
1027 * @return string The post content.
1028 */
1029 public function get_builder_content_for_display( $post_id, $with_css = false ) {
1030 if ( ! get_post( $post_id ) ) {
1031 return '';
1032 }
1033
1034 $editor = Plugin::$instance->editor;
1035
1036 // Avoid recursion
1037 if ( get_the_ID() === (int) $post_id ) {
1038 $content = '';
1039 if ( $editor->is_edit_mode() ) {
1040 $content = '<div class="elementor-alert elementor-alert-danger">' . __( 'Invalid Data: The Template ID cannot be the same as the currently edited template. Please choose a different one.', 'elementor' ) . '</div>';
1041 }
1042
1043 return $content;
1044 }
1045
1046 // 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
1047 $is_edit_mode = $editor->is_edit_mode();
1048 $editor->set_edit_mode( false );
1049
1050 $with_css = $with_css ? true : $is_edit_mode;
1051
1052 $content = $this->get_builder_content( $post_id, $with_css );
1053
1054 // Restore edit mode state
1055 Plugin::$instance->editor->set_edit_mode( $is_edit_mode );
1056
1057 return $content;
1058 }
1059
1060 /**
1061 * Start excerpt flag.
1062 *
1063 * Flags when `the_excerpt` is called. Used to avoid enqueueing CSS in the excerpt.
1064 *
1065 * @since 1.4.3
1066 * @access public
1067 *
1068 * @param string $excerpt The post excerpt.
1069 *
1070 * @return string The post excerpt.
1071 */
1072 public function start_excerpt_flag( $excerpt ) {
1073 $this->_is_excerpt = true;
1074 return $excerpt;
1075 }
1076
1077 /**
1078 * End excerpt flag.
1079 *
1080 * Flags when `the_excerpt` call ended.
1081 *
1082 * @since 1.4.3
1083 * @access public
1084 *
1085 * @param string $excerpt The post excerpt.
1086 *
1087 * @return string The post excerpt.
1088 */
1089 public function end_excerpt_flag( $excerpt ) {
1090 $this->_is_excerpt = false;
1091 return $excerpt;
1092 }
1093
1094 /**
1095 * Remove content filters.
1096 *
1097 * Remove WordPress default filters that conflicted with Elementor.
1098 *
1099 * @since 1.5.0
1100 * @access public
1101 */
1102 public function remove_content_filters() {
1103 $filters = [
1104 'wpautop',
1105 'shortcode_unautop',
1106 'wptexturize',
1107 ];
1108
1109 foreach ( $filters as $filter ) {
1110 // Check if another plugin/theme do not already removed the filter.
1111 if ( has_filter( 'the_content', $filter ) ) {
1112 remove_filter( 'the_content', $filter );
1113 $this->content_removed_filters[] = $filter;
1114 }
1115 }
1116 }
1117
1118 /**
1119 * Has Elementor In Page
1120 *
1121 * Determine whether the current page is using Elementor.
1122 *
1123 * @since 2.0.9
1124 *
1125 * @access public
1126 * @return bool
1127 */
1128 public function has_elementor_in_page() {
1129 return $this->_has_elementor_in_page;
1130 }
1131
1132 public function create_action_hash( $action, array $settings = [] ) {
1133 return '#' . rawurlencode( sprintf( 'elementor-action:action=%1$s&settings=%2$s', $action, base64_encode( wp_json_encode( $settings ) ) ) );
1134 }
1135
1136 /**
1137 * Is the current render mode is static.
1138 *
1139 * @return bool
1140 */
1141 public function is_static_render_mode() {
1142 // The render mode manager is exists only in frontend,
1143 // so by default if it is not exist the method will return false.
1144 if ( ! $this->render_mode_manager ) {
1145 return false;
1146 }
1147
1148 return $this->render_mode_manager->get_current()->is_static();
1149 }
1150
1151 /**
1152 * Get Init Settings
1153 *
1154 * Used to define the default/initial settings of the object. Inheriting classes may implement this method to define
1155 * their own default/initial settings.
1156 *
1157 * @since 2.3.0
1158 *
1159 * @access protected
1160 * @return array
1161 */
1162 protected function get_init_settings() {
1163 $is_preview_mode = Plugin::$instance->preview->is_preview_mode( Plugin::$instance->preview->get_post_id() );
1164
1165 $active_experimental_features = Plugin::$instance->experiments->get_active_features();
1166
1167 $active_experimental_features = array_fill_keys( array_keys( $active_experimental_features ), true );
1168
1169 $settings = [
1170 'environmentMode' => [
1171 'edit' => $is_preview_mode,
1172 'wpPreview' => is_preview(),
1173 'isScriptDebug' => Utils::is_script_debug(),
1174 'isImprovedAssetsLoading' => $this->is_improved_assets_loading(),
1175 ],
1176 // Empty array for BC to avoid errors.
1177 'i18n' => [],
1178 'is_rtl' => is_rtl(),
1179 'breakpoints' => Responsive::get_breakpoints(),
1180 'version' => ELEMENTOR_VERSION,
1181 'is_static' => $this->is_static_render_mode(),
1182 'experimentalFeatures' => $active_experimental_features,
1183 'urls' => [
1184 'assets' => ELEMENTOR_ASSETS_URL,
1185 ],
1186 ];
1187
1188 $settings['settings'] = SettingsManager::get_settings_frontend_config();
1189
1190 $kit = Plugin::$instance->kits_manager->get_active_kit_for_frontend();
1191 $settings['kit'] = $kit->get_frontend_settings();
1192
1193 if ( is_singular() ) {
1194 $post = get_post();
1195
1196 $title = Utils::urlencode_html_entities( wp_get_document_title() );
1197
1198 // Try to use the 'large' WP image size because the Pinterest share API
1199 // has problems accepting shares with large images sometimes, and the WP 'large' thumbnail is
1200 // the largest default WP image size that will probably not be changed in most sites
1201 $featured_image_url = get_the_post_thumbnail_url( null, 'large' );
1202
1203 // If the large size was nullified, use the full size which cannot be nullified/deleted
1204 if ( ! $featured_image_url ) {
1205 $featured_image_url = get_the_post_thumbnail_url( null, 'full' );
1206 }
1207
1208 $settings['post'] = [
1209 'id' => $post->ID,
1210 'title' => $title,
1211 'excerpt' => $post->post_excerpt,
1212 'featuredImage' => $featured_image_url,
1213 ];
1214 } else {
1215 $settings['post'] = [
1216 'id' => 0,
1217 'title' => wp_get_document_title(),
1218 'excerpt' => get_the_archive_description(),
1219 ];
1220 }
1221
1222 $empty_object = (object) [];
1223
1224 if ( $is_preview_mode ) {
1225 $settings['elements'] = [
1226 'data' => $empty_object,
1227 'editSettings' => $empty_object,
1228 'keys' => $empty_object,
1229 ];
1230 }
1231
1232 if ( is_user_logged_in() ) {
1233 $user = wp_get_current_user();
1234
1235 if ( ! empty( $user->roles ) ) {
1236 $settings['user'] = [
1237 'roles' => $user->roles,
1238 ];
1239 }
1240 }
1241
1242 return $settings;
1243 }
1244
1245 /**
1246 * Restore content filters.
1247 *
1248 * Restore removed WordPress filters that conflicted with Elementor.
1249 *
1250 * @since 1.5.0
1251 * @access private
1252 */
1253 private function restore_content_filters() {
1254 foreach ( $this->content_removed_filters as $filter ) {
1255 add_filter( 'the_content', $filter );
1256 }
1257
1258 $this->content_removed_filters = [];
1259 }
1260
1261 /**
1262 * Process More Tag
1263 *
1264 * Respect the native WP (<!--more-->) tag
1265 *
1266 * @access private
1267 * @since 2.0.4
1268 *
1269 * @param $content
1270 *
1271 * @return string
1272 */
1273 private function process_more_tag( $content ) {
1274 $post = get_post();
1275 $content = str_replace( '&lt;!--more--&gt;', '<!--more-->', $content );
1276 $parts = get_extended( $content );
1277 if ( empty( $parts['extended'] ) ) {
1278 return $content;
1279 }
1280
1281 if ( is_singular() ) {
1282 return $parts['main'] . '<div id="more-' . $post->ID . '"></div>' . $parts['extended'];
1283 }
1284
1285 if ( empty( $parts['more_text'] ) ) {
1286 $parts['more_text'] = __( '(more&hellip;)', 'elementor' );
1287 }
1288
1289 $more_link_text = sprintf(
1290 '<span aria-label="%1$s">%2$s</span>',
1291 sprintf(
1292 /* translators: %s: Name of current post */
1293 __( 'Continue reading %s', 'elementor' ),
1294 the_title_attribute( [
1295 'echo' => false,
1296 ] )
1297 ),
1298 $parts['more_text']
1299 );
1300
1301 $more_link = apply_filters( 'the_content_more_link', sprintf( ' <a href="%s#more-%s" class="more-link elementor-more-link">%s</a>', get_permalink(), $post->ID, $more_link_text ), $more_link_text );
1302
1303 return force_balance_tags( $parts['main'] ) . $more_link;
1304 }
1305
1306 private function is_improved_assets_loading() {
1307 return Plugin::$instance->experiments->is_feature_active( 'e_optimized_assets_loading' );
1308 }
1309
1310 private function get_elementor_frontend_dependencies() {
1311 $dependencies = [
1312 'elementor-frontend-modules',
1313 'elementor-dialog',
1314 'elementor-waypoints',
1315 'share-link',
1316 ];
1317
1318 if ( ! $this->is_improved_assets_loading() ) {
1319 wp_register_script(
1320 'swiper',
1321 $this->get_js_assets_url( 'swiper', 'assets/lib/swiper/' ),
1322 [],
1323 '5.3.6',
1324 true
1325 );
1326
1327 $dependencies[] = 'swiper';
1328 }
1329
1330 return $dependencies;
1331 }
1332 }
1333