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

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

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