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

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