PluginProbe
Elementor Website Builder – more than just a page builder / 3.11.2
Elementor Website Builder – more than just a page builder v3.11.2
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 / kit-elements-defaults / utils / settings-sanitizer.php

settings-sanitizer.php in Elementor Website Builder – more than just a page builder 3.11.2, at modules/kit-elements-defaults/utils/settings-sanitizer.php

275 lines 5.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\KitElementsDefaults\Utils;
3
4 use Elementor\Core\Breakpoints\Manager as Breakpoints_Manager;
5 use Elementor\Element_Base;
6 use Elementor\Elements_Manager;
7 use Elementor\Core\Base\Document;
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly.
11 }
12
13 class Settings_Sanitizer {
14
15 const SPECIAL_SETTINGS = [
16 '__dynamic__',
17 '__globals__',
18 ];
19
20 /**
21 * @var Elements_Manager
22 */
23 private $elements_manager;
24
25 /**
26 * @var array
27 */
28 private $widget_types;
29
30 /**
31 * @var Element_Base | null
32 */
33 private $pending_element = null;
34
35 /**
36 * @var array | null
37 */
38 private $pending_settings = null;
39
40 /**
41 * @param Elements_Manager $elements_manager
42 * @param array $widget_types
43 */
44 public function __construct( Elements_Manager $elements_manager, array $widget_types = [] ) {
45 $this->elements_manager = $elements_manager;
46 $this->widget_types = $widget_types;
47 }
48
49 /**
50 * @param $type
51 *
52 * @return $this
53 */
54 public function for( $type ) {
55 $this->pending_element = $this->create_element( $type );
56
57 return $this;
58 }
59
60 /**
61 * @param $settings
62 *
63 * @return $this
64 */
65 public function using( $settings ) {
66 $this->pending_settings = $settings;
67
68 return $this;
69 }
70
71 /**
72 * @return $this
73 */
74 public function reset() {
75 $this->pending_element = null;
76 $this->pending_settings = null;
77
78 return $this;
79 }
80
81 /**
82 * @return bool
83 */
84 public function is_prepared() {
85 return $this->pending_element && is_array( $this->pending_settings );
86 }
87
88 /**
89 * @return $this
90 */
91 public function remove_invalid_settings() {
92 if ( ! $this->is_prepared() ) {
93 return $this;
94 }
95
96 $valid_settings_keys = $this->get_valid_settings_keys(
97 $this->pending_element->get_controls()
98 );
99
100 $this->pending_settings = $this->filter_invalid_settings(
101 $this->pending_settings,
102 array_merge( $valid_settings_keys, self::SPECIAL_SETTINGS )
103 );
104
105 foreach ( self::SPECIAL_SETTINGS as $special_setting ) {
106 if ( ! isset( $this->pending_settings[ $special_setting ] ) ) {
107 continue;
108 }
109
110 $this->pending_settings[ $special_setting ] = $this->filter_invalid_settings(
111 $this->pending_settings[ $special_setting ],
112 $valid_settings_keys
113 );
114 }
115
116 return $this;
117 }
118
119 public function kses_deep() {
120 if ( ! $this->is_prepared() ) {
121 return $this;
122 }
123
124 $this->pending_settings = wp_kses_post_deep( $this->pending_settings );
125
126 return $this;
127 }
128
129 /**
130 * @param Document $document
131 *
132 * @return $this
133 */
134 public function prepare_for_export( Document $document ) {
135 return $this->run_import_export_sanitize_process( $document, 'on_export' );
136 }
137
138 /**
139 * @param Document $document
140 *
141 * @return $this
142 */
143 public function prepare_for_import( Document $document ) {
144 return $this->run_import_export_sanitize_process( $document, 'on_import' );
145 }
146
147 /**
148 * @return array
149 */
150 public function get() {
151 if ( ! $this->is_prepared() ) {
152 return [];
153 }
154
155 $settings = $this->pending_settings;
156
157 $this->reset();
158
159 return $settings;
160 }
161
162 /**
163 * @param string $type
164 * @param array $settings
165 *
166 * @return Element_Base|null
167 */
168 private function create_element( $type ) {
169 $is_widget = in_array( $type, $this->widget_types, true );
170 $is_inner_section = 'inner-section' === $type;
171
172 if ( $is_inner_section ) {
173 return $this->elements_manager->create_element_instance( [
174 'elType' => 'section',
175 'isInner' => true,
176 'id' => '0',
177 ] );
178 }
179
180 if ( $is_widget ) {
181 return $this->elements_manager->create_element_instance( [
182 'elType' => 'widget',
183 'widgetType' => $type,
184 'id' => '0',
185 ] );
186 }
187
188 return $this->elements_manager->create_element_instance( [
189 'elType' => $type,
190 'id' => '0',
191 ] );
192 }
193
194 /**
195 * @param Document $document
196 * @param $process_type
197 *
198 * @return $this
199 */
200 private function run_import_export_sanitize_process( Document $document, $process_type ) {
201 if ( ! $this->is_prepared() ) {
202 return $this;
203 }
204
205 $result = $document->process_element_import_export(
206 $this->pending_element,
207 $process_type,
208 [ 'settings' => $this->pending_settings ]
209 );
210
211 if ( empty( $result['settings'] ) ) {
212 return $this;
213 }
214
215 $this->pending_settings = $result['settings'];
216
217 return $this;
218 }
219
220 /**
221 * Get all the available settings of a specific element, including responsive settings.
222 *
223 * @param array $controls
224 *
225 * @return array
226 */
227 private function get_valid_settings_keys( $controls ) {
228 if ( ! $controls ) {
229 return [];
230 }
231
232 $control_keys = array_keys( $controls );
233
234 $optional_responsive_keys = [
235 Breakpoints_Manager::BREAKPOINT_KEY_MOBILE,
236 Breakpoints_Manager::BREAKPOINT_KEY_MOBILE_EXTRA,
237 Breakpoints_Manager::BREAKPOINT_KEY_TABLET,
238 Breakpoints_Manager::BREAKPOINT_KEY_TABLET_EXTRA,
239 Breakpoints_Manager::BREAKPOINT_KEY_LAPTOP,
240 Breakpoints_Manager::BREAKPOINT_KEY_WIDESCREEN,
241 ];
242
243 $settings = [];
244
245 foreach ( $control_keys as $control_key ) {
246 // Add the responsive settings.
247 foreach ( $optional_responsive_keys as $responsive_key ) {
248 $settings[] = "{$control_key}_{$responsive_key}";
249 }
250 // Add the setting itself (not responsive).
251 $settings[] = $control_key;
252 }
253
254 return $settings;
255 }
256
257 /**
258 * Remove invalid settings.
259 *
260 * @param $settings
261 * @param $valid_settings_keys
262 *
263 * @return array
264 */
265 private function filter_invalid_settings( $settings, $valid_settings_keys ) {
266 return array_filter(
267 $settings,
268 function ( $setting_key ) use ( $valid_settings_keys ) {
269 return in_array( $setting_key, $valid_settings_keys, true );
270 },
271 ARRAY_FILTER_USE_KEY
272 );
273 }
274 }
275