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

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