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

1,289 lines 29.8 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 if ( empty( $data ) ) {
947 return '';
948 }
949
950 if ( ! $this->_is_excerpt ) {
951 if ( $document->is_autosave() ) {
952 $css_file = Post_Preview::create( $document->get_post()->ID );
953 } else {
954 $css_file = Post_CSS::create( $post_id );
955 }
956
957 $css_file->enqueue();
958 }
959
960 ob_start();
961
962 // Handle JS and Customizer requests, with CSS inline.
963 if ( is_customize_preview() || wp_doing_ajax() ) {
964 $with_css = true;
965 }
966
967 if ( ! empty( $css_file ) && $with_css ) {
968 $css_file->print_css();
969 }
970
971 $document->print_elements_with_wrapper( $data );
972
973 $content = ob_get_clean();
974
975 $content = $this->process_more_tag( $content );
976
977 /**
978 * Frontend content.
979 *
980 * Filters the content in the frontend.
981 *
982 * @since 1.0.0
983 *
984 * @param string $content The content.
985 */
986 $content = apply_filters( 'elementor/frontend/the_content', $content );
987
988 if ( ! empty( $content ) ) {
989 $this->_has_elementor_in_page = true;
990 }
991
992 Plugin::$instance->documents->restore_document();
993
994 do_action( 'elementor/frontend/get_builder_content', $document, $this->_is_excerpt, $with_css );
995
996 return $content;
997 }
998
999 /**
1000 * Retrieve builder content for display.
1001 *
1002 * Used to render and return the post content with all the Elementor elements.
1003 *
1004 * @since 1.0.0
1005 * @access public
1006 *
1007 * @param int $post_id The post ID.
1008 *
1009 * @param bool $with_css Optional. Whether to retrieve the content with CSS
1010 * or not. Default is false.
1011 *
1012 * @return string The post content.
1013 */
1014 public function get_builder_content_for_display( $post_id, $with_css = false ) {
1015 if ( ! get_post( $post_id ) ) {
1016 return '';
1017 }
1018
1019 $editor = Plugin::$instance->editor;
1020
1021 // Avoid recursion
1022 if ( get_the_ID() === (int) $post_id ) {
1023 $content = '';
1024 if ( $editor->is_edit_mode() ) {
1025 $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>';
1026 }
1027
1028 return $content;
1029 }
1030
1031 // 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
1032 $is_edit_mode = $editor->is_edit_mode();
1033 $editor->set_edit_mode( false );
1034
1035 $with_css = $with_css ? true : $is_edit_mode;
1036
1037 $content = $this->get_builder_content( $post_id, $with_css );
1038
1039 // Restore edit mode state
1040 Plugin::$instance->editor->set_edit_mode( $is_edit_mode );
1041
1042 return $content;
1043 }
1044
1045 /**
1046 * Start excerpt flag.
1047 *
1048 * Flags when `the_excerpt` is called. Used to avoid enqueueing CSS in the excerpt.
1049 *
1050 * @since 1.4.3
1051 * @access public
1052 *
1053 * @param string $excerpt The post excerpt.
1054 *
1055 * @return string The post excerpt.
1056 */
1057 public function start_excerpt_flag( $excerpt ) {
1058 $this->_is_excerpt = true;
1059 return $excerpt;
1060 }
1061
1062 /**
1063 * End excerpt flag.
1064 *
1065 * Flags when `the_excerpt` call ended.
1066 *
1067 * @since 1.4.3
1068 * @access public
1069 *
1070 * @param string $excerpt The post excerpt.
1071 *
1072 * @return string The post excerpt.
1073 */
1074 public function end_excerpt_flag( $excerpt ) {
1075 $this->_is_excerpt = false;
1076 return $excerpt;
1077 }
1078
1079 /**
1080 * Remove content filters.
1081 *
1082 * Remove WordPress default filters that conflicted with Elementor.
1083 *
1084 * @since 1.5.0
1085 * @access public
1086 */
1087 public function remove_content_filters() {
1088 $filters = [
1089 'wpautop',
1090 'shortcode_unautop',
1091 'wptexturize',
1092 ];
1093
1094 foreach ( $filters as $filter ) {
1095 // Check if another plugin/theme do not already removed the filter.
1096 if ( has_filter( 'the_content', $filter ) ) {
1097 remove_filter( 'the_content', $filter );
1098 $this->content_removed_filters[] = $filter;
1099 }
1100 }
1101 }
1102
1103 /**
1104 * Has Elementor In Page
1105 *
1106 * Determine whether the current page is using Elementor.
1107 *
1108 * @since 2.0.9
1109 *
1110 * @access public
1111 * @return bool
1112 */
1113 public function has_elementor_in_page() {
1114 return $this->_has_elementor_in_page;
1115 }
1116
1117 public function create_action_hash( $action, array $settings = [] ) {
1118 return '#' . rawurlencode( sprintf( 'elementor-action:action=%1$s&settings=%2$s', $action, base64_encode( wp_json_encode( $settings ) ) ) );
1119 }
1120
1121 /**
1122 * Is the current render mode is static.
1123 *
1124 * @return bool
1125 */
1126 public function is_static_render_mode() {
1127 // The render mode manager is exists only in frontend,
1128 // so by default if it is not exist the method will return false.
1129 if ( ! $this->render_mode_manager ) {
1130 return false;
1131 }
1132
1133 return $this->render_mode_manager->get_current()->is_static();
1134 }
1135
1136 /**
1137 * Get Init Settings
1138 *
1139 * Used to define the default/initial settings of the object. Inheriting classes may implement this method to define
1140 * their own default/initial settings.
1141 *
1142 * @since 2.3.0
1143 *
1144 * @access protected
1145 * @return array
1146 */
1147 protected function get_init_settings() {
1148 $is_preview_mode = Plugin::$instance->preview->is_preview_mode( Plugin::$instance->preview->get_post_id() );
1149
1150 $settings = [
1151 'environmentMode' => [
1152 'edit' => $is_preview_mode,
1153 'wpPreview' => is_preview(),
1154 ],
1155 'i18n' => [
1156 'shareOnFacebook' => __( 'Share on Facebook', 'elementor' ),
1157 'shareOnTwitter' => __( 'Share on Twitter', 'elementor' ),
1158 'pinIt' => __( 'Pin it', 'elementor' ),
1159 'download' => __( 'Download', 'elementor' ),
1160 'downloadImage' => __( 'Download image', 'elementor' ),
1161 'fullscreen' => __( 'Fullscreen', 'elementor' ),
1162 'zoom' => __( 'Zoom', 'elementor' ),
1163 'share' => __( 'Share', 'elementor' ),
1164 'playVideo' => __( 'Play Video', 'elementor' ),
1165 'previous' => __( 'Previous', 'elementor' ),
1166 'next' => __( 'Next', 'elementor' ),
1167 'close' => __( 'Close', 'elementor' ),
1168 ],
1169 'is_rtl' => is_rtl(),
1170 'breakpoints' => Responsive::get_breakpoints(),
1171 'version' => ELEMENTOR_VERSION,
1172 'is_static' => $this->is_static_render_mode(),
1173 'legacyMode' => [
1174 'elementWrappers' => Plugin::instance()->get_legacy_mode( 'elementWrappers' ),
1175 ],
1176 'urls' => [
1177 'assets' => ELEMENTOR_ASSETS_URL,
1178 ],
1179 ];
1180
1181 $settings['settings'] = SettingsManager::get_settings_frontend_config();
1182
1183 $kit = Plugin::$instance->kits_manager->get_active_kit_for_frontend();
1184 $settings['kit'] = $kit->get_frontend_settings();
1185
1186 if ( is_singular() ) {
1187 $post = get_post();
1188
1189 $title = Utils::urlencode_html_entities( wp_get_document_title() );
1190
1191 $settings['post'] = [
1192 'id' => $post->ID,
1193 'title' => $title,
1194 'excerpt' => $post->post_excerpt,
1195 'featuredImage' => get_the_post_thumbnail_url(),
1196 ];
1197 } else {
1198 $settings['post'] = [
1199 'id' => 0,
1200 'title' => wp_get_document_title(),
1201 'excerpt' => get_the_archive_description(),
1202 ];
1203 }
1204
1205 $empty_object = (object) [];
1206
1207 if ( $is_preview_mode ) {
1208 $settings['elements'] = [
1209 'data' => $empty_object,
1210 'editSettings' => $empty_object,
1211 'keys' => $empty_object,
1212 ];
1213 }
1214
1215 if ( is_user_logged_in() ) {
1216 $user = wp_get_current_user();
1217
1218 if ( ! empty( $user->roles ) ) {
1219 $settings['user'] = [
1220 'roles' => $user->roles,
1221 ];
1222 }
1223 }
1224
1225 return $settings;
1226 }
1227
1228 /**
1229 * Restore content filters.
1230 *
1231 * Restore removed WordPress filters that conflicted with Elementor.
1232 *
1233 * @since 1.5.0
1234 * @access private
1235 */
1236 private function restore_content_filters() {
1237 foreach ( $this->content_removed_filters as $filter ) {
1238 add_filter( 'the_content', $filter );
1239 }
1240
1241 $this->content_removed_filters = [];
1242 }
1243
1244 /**
1245 * Process More Tag
1246 *
1247 * Respect the native WP (<!--more-->) tag
1248 *
1249 * @access private
1250 * @since 2.0.4
1251 *
1252 * @param $content
1253 *
1254 * @return string
1255 */
1256 private function process_more_tag( $content ) {
1257 $post = get_post();
1258 $content = str_replace( '&lt;!--more--&gt;', '<!--more-->', $content );
1259 $parts = get_extended( $content );
1260 if ( empty( $parts['extended'] ) ) {
1261 return $content;
1262 }
1263
1264 if ( is_singular() ) {
1265 return $parts['main'] . '<div id="more-' . $post->ID . '"></div>' . $parts['extended'];
1266 }
1267
1268 if ( empty( $parts['more_text'] ) ) {
1269 $parts['more_text'] = __( '(more&hellip;)', 'elementor' );
1270 }
1271
1272 $more_link_text = sprintf(
1273 '<span aria-label="%1$s">%2$s</span>',
1274 sprintf(
1275 /* translators: %s: Name of current post */
1276 __( 'Continue reading %s', 'elementor' ),
1277 the_title_attribute( [
1278 'echo' => false,
1279 ] )
1280 ),
1281 $parts['more_text']
1282 );
1283
1284 $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 );
1285
1286 return force_balance_tags( $parts['main'] ) . $more_link;
1287 }
1288 }
1289