PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta3
Elementor Website Builder – more than just a page builder v4.3.0-beta3
4.3.0-beta3 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 All 452 releases
elementor / modules / atomic-widgets / elements / base / has-template.php

has-template.php in Elementor Website Builder – more than just a page builder 4.3.0-beta3, at modules/atomic-widgets/elements/base/has-template.php

99 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\AtomicWidgets\Elements\Base;
4
5 use Elementor\Modules\AtomicWidgets\Elements\TemplateRenderer\Template_Renderer;
6 use Elementor\Utils;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 /**
13 * @mixin Has_Atomic_Base
14 */
15 trait Has_Template {
16
17 private static function get_macros_template_key(): string {
18 return 'elementor/macros';
19 }
20
21 public function get_initial_config() {
22 $config = parent::get_initial_config();
23
24 $config['twig_main_template'] = $this->get_main_template();
25 $config['twig_templates'] = $this->get_templates_contents();
26 return $config;
27 }
28
29 protected function transform_link_for_render( array $parsed ): array {
30 return $parsed;
31 }
32
33 protected function get_shared_templates(): array {
34 return [
35 self::get_macros_template_key() => __DIR__ . '/_macros.html.twig',
36 ];
37 }
38
39 protected function render() {
40 try {
41 $renderer = Template_Renderer::instance();
42
43 $all_templates = array_merge( $this->get_shared_templates(), $this->get_templates() );
44
45 foreach ( $all_templates as $name => $path ) {
46 if ( $renderer->is_registered( $name ) ) {
47 continue;
48 }
49
50 $renderer->register( $name, $path );
51 }
52
53 $settings = $this->get_atomic_settings();
54
55 $context = [
56 'id' => $this->get_id(),
57 'interaction_id' => $this->get_interaction_id(),
58 'type' => $this->get_name(),
59 'settings' => $settings,
60 'tag' => static::get_computed_html_tag( $settings ),
61 'base_styles' => $this->get_base_styles_dictionary(),
62 ];
63
64 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
65 echo $renderer->render( $this->get_main_template(), $context );
66 } catch ( \Exception $e ) {
67 if ( Utils::is_elementor_debug() ) {
68 throw $e;
69 }
70 }
71 }
72
73 protected function get_templates_contents() {
74 return array_map(
75 fn ( $path ) => Utils::file_get_contents( $path ),
76 array_merge( $this->get_shared_templates(), $this->get_templates() )
77 );
78 }
79
80 protected function get_main_template() {
81 $templates = $this->get_templates();
82
83 if ( count( $templates ) > 1 ) {
84 Utils::safe_throw( 'When having more than one template, you should override this method to return the main template.' );
85
86 return null;
87 }
88
89 foreach ( $templates as $key => $path ) {
90 // Returns first key in the array.
91 return $key;
92 }
93
94 return null;
95 }
96
97 abstract protected function get_templates(): array;
98 }
99