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

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