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

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