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

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