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

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