PluginProbe
Elementor Website Builder – more than just a page builder / 3.25.4
Elementor Website Builder – more than just a page builder v3.25.4
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 / modules / atomic-widgets / base / atomic-widget-base.php

atomic-widget-base.php in Elementor Website Builder – more than just a page builder 3.25.4, at modules/atomic-widgets/base/atomic-widget-base.php

160 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\AtomicWidgets\Base;
3
4 use Elementor\Modules\AtomicWidgets\Controls\Section;
5 use Elementor\Modules\AtomicWidgets\PropsResolver\Props_Resolver;
6 use Elementor\Modules\AtomicWidgets\PropTypes\Prop_Type;
7 use Elementor\Modules\AtomicWidgets\Settings_Validator;
8 use Elementor\Utils;
9 use Elementor\Widget_Base;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit; // Exit if accessed directly.
13 }
14
15 abstract class Atomic_Widget_Base extends Widget_Base {
16 protected $version = '0.0';
17 protected $styles = [];
18
19 public function __construct( $data = [], $args = null ) {
20 parent::__construct( $data, $args );
21
22 $this->version = $data['version'] ?? '0.0';
23 $this->styles = $data['styles'] ?? [];
24 }
25
26 public function get_atomic_controls() {
27 $controls = $this->define_atomic_controls();
28 $schema = static::get_props_schema();
29
30 // Validate the schema only in the Editor.
31 static::validate_schema( $schema );
32
33 return $this->get_valid_controls( $schema, $controls );
34 }
35
36 private function get_valid_controls( array $schema, array $controls ): array {
37 $valid_controls = [];
38
39 foreach ( $controls as $control ) {
40 if ( $control instanceof Section ) {
41 $cloned_section = clone $control;
42
43 $cloned_section->set_items(
44 $this->get_valid_controls( $schema, $control->get_items() )
45 );
46
47 $valid_controls[] = $cloned_section;
48 continue;
49 }
50
51 if ( ! ( $control instanceof Atomic_Control_Base ) ) {
52 Utils::safe_throw( 'Control must be an instance of `Atomic_Control_Base`.' );
53 continue;
54 }
55
56 $prop_name = $control->get_bind();
57
58 if ( ! $prop_name ) {
59 Utils::safe_throw( 'Control is missing a bound prop from the schema.' );
60 continue;
61 }
62
63 if ( ! array_key_exists( $prop_name, $schema ) ) {
64 Utils::safe_throw( "Prop `{$prop_name}` is not defined in the schema of `{$this->get_name()}`." );
65 continue;
66 }
67
68 $valid_controls[] = $control;
69 }
70
71 return $valid_controls;
72 }
73
74 abstract protected function define_atomic_controls(): array;
75
76 final public function get_controls( $control_id = null ) {
77 if ( ! empty( $control_id ) ) {
78 return null;
79 }
80
81 return [];
82 }
83
84 final public function get_initial_config() {
85 $config = parent::get_initial_config();
86
87 $config['atomic_controls'] = $this->get_atomic_controls();
88 $config['atomic_props_schema'] = static::get_props_schema();
89 $config['version'] = $this->version;
90
91 return $config;
92 }
93
94 final public function get_data_for_save() {
95 $data = parent::get_data_for_save();
96
97 $data['version'] = $this->version;
98 $data['settings'] = $this->sanitize_atomic_settings( $data['settings'] );
99
100 return $data;
101 }
102
103 final public function get_raw_data( $with_html_content = false ) {
104 $raw_data = parent::get_raw_data( $with_html_content );
105
106 $raw_data['styles'] = $this->styles;
107
108 return $raw_data;
109 }
110
111 final public function get_stack( $with_common_controls = true ) {
112 return [
113 'controls' => [],
114 'tabs' => [],
115 ];
116 }
117
118 final public function get_atomic_settings(): array {
119 $schema = static::get_props_schema();
120 $props = $this->get_settings();
121
122 return Props_Resolver::for_settings()->resolve( $schema, $props );
123 }
124
125 public static function get_props_schema(): array {
126 return apply_filters(
127 'elementor/atomic-widgets/props-schema',
128 static::define_props_schema()
129 );
130 }
131
132 // TODO: Move to a `Schema_Validator` class?
133 private static function validate_schema( array $schema ) {
134 $widget_name = static::class;
135
136 foreach ( $schema as $key => $prop ) {
137 if ( ! ( $prop instanceof Prop_Type ) ) {
138 Utils::safe_throw( "Prop `$key` must be an instance of `Prop_Type` in `{$widget_name}`." );
139 }
140 }
141 }
142
143 private function sanitize_atomic_settings( array $settings ): array {
144 $schema = static::get_props_schema();
145
146 [ , $validated, $errors ] = Settings_Validator::make( $schema )->validate( $settings );
147
148 if ( ! empty( $errors ) ) {
149 throw new \Exception( 'Settings validation failed. Invalid keys: ' . join( ', ', $errors ) );
150 }
151
152 return $validated;
153 }
154
155 /**
156 * @return array<string, Prop_Type>
157 */
158 abstract protected static function define_props_schema(): array;
159 }
160