PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta1
Elementor Website Builder – more than just a page builder v4.3.0-beta1
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 / components / documents / component.php

component.php in Elementor Website Builder – more than just a page builder 4.3.0-beta1, at modules/components/documents/component.php

260 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\Modules\Components\Documents;
3
4 use Elementor\Core\Base\Document;
5 use Elementor\Core\Utils\Api\Parse_Result;
6 use Elementor\Modules\Components\OverridableProps\Component_Overridable_Props_Parser;
7 use Elementor\Modules\Components\PropTypes\Override_Prop_Type;
8 use Elementor\Modules\Components\PropTypes\Overridable_Prop_Type;
9 use Elementor\Modules\Components\Variants\Component_Variants;
10 use Elementor\Modules\Components\Variants\Component_Variants_Parser;
11 use Elementor\Modules\Components\Widgets\Component_Instance;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly
15 }
16
17 class Component extends Document {
18 const TYPE = 'elementor_component';
19 const COMPONENT_UID_META_KEY = '_elementor_component_uid';
20 const OVERRIDABLE_PROPS_META_KEY = '_elementor_component_overridable_props';
21 const VARIANTS_META_KEY = '_elementor_component_variants';
22 const ARCHIVED_META_KEY = '_elementor_component_is_archived';
23 const ARCHIVED_AT_META_KEY = '_elementor_component_archived_at';
24 const COMPONENT_CUSTOM_META_KEYS = [
25 self::COMPONENT_UID_META_KEY,
26 self::OVERRIDABLE_PROPS_META_KEY,
27 self::VARIANTS_META_KEY,
28 self::ARCHIVED_META_KEY,
29 self::ARCHIVED_AT_META_KEY,
30 ];
31
32 public static function get_properties() {
33 $properties = parent::get_properties();
34
35 $properties['cpt'] = [ self::TYPE ];
36
37 return $properties;
38 }
39
40 public static function get_type() {
41 return self::TYPE;
42 }
43
44 public static function get_title() {
45 return esc_html__( 'Component', 'elementor' );
46 }
47
48 public static function get_plural_title() {
49 return esc_html__( 'Components', 'elementor' );
50 }
51
52 public static function get_labels(): array {
53 $plural_label = static::get_plural_title();
54 $singular_label = static::get_title();
55
56 $labels = [
57 'name' => $plural_label,
58 'singular_name' => $singular_label,
59 ];
60
61 return $labels;
62 }
63
64 public static function get_supported_features(): array {
65 return [
66 'title',
67 'author',
68 'thumbnail',
69 'custom-fields',
70 'revisions',
71 'elementor',
72 ];
73 }
74
75 public function get_component_uid() {
76 return $this->get_meta( self::COMPONENT_UID_META_KEY );
77 }
78
79 public function get_overridable_props(): Component_Overridable_Props {
80 $meta = $this->get_json_meta( self::OVERRIDABLE_PROPS_META_KEY );
81
82 return Component_Overridable_Props::make( $meta ?? [] );
83 }
84
85 public function archive() {
86 try {
87 $this->update_json_meta( self::ARCHIVED_META_KEY, [
88 'is_archived' => true,
89 'archived_at' => time(),
90 ] );
91 } catch ( \Exception $e ) {
92 throw new \Exception( 'Failed to archive component: ' . esc_html( $e->getMessage() ) );
93 }
94 }
95
96 public function get_is_archived(): bool {
97 $archived_meta = $this->get_json_meta( self::ARCHIVED_META_KEY );
98
99 return $archived_meta['is_archived'] ?? false;
100 }
101
102 public function update_overridable_props( $data ): Parse_Result {
103 $parser = Component_Overridable_Props_Parser::make();
104
105 $result = $parser->parse( $data );
106
107 if ( ! $result->is_valid() ) {
108 return $result;
109 }
110
111 $sanitized_data = $result->unwrap();
112
113 $this->update_json_meta( self::OVERRIDABLE_PROPS_META_KEY, $sanitized_data );
114
115 return $result;
116 }
117
118 public function get_variants(): Component_Variants {
119 $meta = $this->get_json_meta( self::VARIANTS_META_KEY );
120
121 return Component_Variants::make( $meta ?? [] );
122 }
123
124 public function update_variants( $data ): Parse_Result {
125 $parser = Component_Variants_Parser::make();
126
127 $result = $parser->parse( $data );
128
129 if ( ! $result->is_valid() ) {
130 return $result;
131 }
132
133 $sanitized_data = $result->unwrap();
134
135 $this->update_json_meta( self::VARIANTS_META_KEY, $sanitized_data );
136
137 return $result;
138 }
139
140 public function update_title( string $title ): bool {
141 $sanitized_title = sanitize_text_field( $title );
142
143 if ( empty( $sanitized_title ) ) {
144 return false;
145 }
146
147 return $this->update_post_field( 'post_title', $sanitized_title );
148 }
149
150 public function update_status( string $status ): bool {
151 if ( ! in_array( $status, [ Document::STATUS_PUBLISH, Document::STATUS_DRAFT, Document::STATUS_AUTOSAVE ], true ) ) {
152 return false;
153 }
154
155 return $this->update_post_field( 'post_status', $status );
156 }
157
158 private function update_post_field( string $field, $value ): bool {
159 if ( is_string( $value ) ) {
160 // NOTE: escape the json to support non-UTF-8 characters
161 $value = wp_slash( $value );
162 }
163 $result = wp_update_post( [
164 'ID' => $this->post->ID,
165 $field => $value,
166 ] );
167
168 $success = ! is_wp_error( $result ) && $result > 0;
169
170 if ( $success ) {
171 $this->refresh_post();
172 }
173
174 return $success;
175 }
176
177 public function print_elements_without_cache( array $elements_data ) {
178 $this->do_print_elements( $elements_data );
179 }
180
181 public function align_overridable_props_with_elements() {
182 $elements_data = $this->get_elements_data();
183 // format elements data to flat map of overridable prop key -> new origin value
184 $overridable_props_map = $this->get_elements_origin_values_map( $elements_data, [] );
185
186 if ( empty( $overridable_props_map ) ) {
187 return;
188 }
189
190 $updated_overridable_props = $this->get_overridable_props();
191
192 foreach ( $updated_overridable_props->props as $prop ) {
193
194 $new_origin_value = $overridable_props_map[ $prop->override_key ];
195
196 if ( isset( $new_origin_value ) ) {
197 $prop->origin_value = $new_origin_value;
198 }
199 }
200
201 $this->update_overridable_props( $updated_overridable_props->to_associative_array() );
202 }
203
204 private function get_elements_origin_values_map( array $elements_data, array $overridable_props_map ) {
205 foreach ( $elements_data as $element ) {
206 if ( $this->is_component_instance( $element ) ) {
207 $component_instance = $element['settings']['component_instance']['value'];
208 $overrides = $component_instance['overrides']['value'] ?? [];
209
210 if ( empty( $overrides ) ) {
211 continue;
212 }
213
214 foreach ( $overrides as $item ) {
215 if ( $this->is_overridable_prop( $item ) ) {
216 $override_key = $item['value']['override_key'];
217 $override = $item['value']['origin_value'];
218
219 if ( ! $this->is_override_prop( $override ) ) {
220 throw new \Exception( 'Invalid override value' );
221 }
222 $overridable_props_map[ $override_key ] = $override['value']['override_value'];
223 }
224 }
225
226 continue;
227 }
228
229 if ( ! empty( $element['settings'] ) ) {
230 foreach ( $element['settings'] as $prop_key => $prop_value ) {
231 if ( isset( $prop_value['$$type'] ) && Overridable_Prop_Type::get_key() === $prop_value['$$type'] ) {
232 $override_key = $prop_value['value']['override_key'];
233 $origin_value = $prop_value['value']['origin_value'];
234
235 $overridable_props_map[ $override_key ] = $origin_value;
236 }
237 }
238 }
239
240 if ( is_array( $element['elements'] ) ) {
241 $overridable_props_map = $this->get_elements_origin_values_map( $element['elements'], $overridable_props_map );
242 }
243 }
244
245 return $overridable_props_map;
246 }
247
248 private function is_overridable_prop( array $prop ): bool {
249 return isset( $prop['$$type'] ) && Overridable_Prop_Type::get_key() === $prop['$$type'];
250 }
251
252 private function is_override_prop( array $prop ): bool {
253 return isset( $prop['$$type'] ) && Override_Prop_Type::get_key() === $prop['$$type'];
254 }
255
256 private function is_component_instance( array $element ): bool {
257 return 'widget' === $element['elType'] && Component_Instance::get_element_type() === $element['widgetType'];
258 }
259 }
260