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

281 lines 5.6 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 = map_deep( $this->pending_settings, function( $value ) {
125 if ( ! is_string( $value ) ) {
126 return $value;
127 }
128
129 return wp_kses_post( $value );
130 } );
131
132 return $this;
133 }
134
135 /**
136 * @param Document $document
137 *
138 * @return $this
139 */
140 public function prepare_for_export( Document $document ) {
141 return $this->run_import_export_sanitize_process( $document, 'on_export' );
142 }
143
144 /**
145 * @param Document $document
146 *
147 * @return $this
148 */
149 public function prepare_for_import( Document $document ) {
150 return $this->run_import_export_sanitize_process( $document, 'on_import' );
151 }
152
153 /**
154 * @return array
155 */
156 public function get() {
157 if ( ! $this->is_prepared() ) {
158 return [];
159 }
160
161 $settings = $this->pending_settings;
162
163 $this->reset();
164
165 return $settings;
166 }
167
168 /**
169 * @param string $type
170 * @param array $settings
171 *
172 * @return Element_Base|null
173 */
174 private function create_element( $type ) {
175 $is_widget = in_array( $type, $this->widget_types, true );
176 $is_inner_section = 'inner-section' === $type;
177
178 if ( $is_inner_section ) {
179 return $this->elements_manager->create_element_instance( [
180 'elType' => 'section',
181 'isInner' => true,
182 'id' => '0',
183 ] );
184 }
185
186 if ( $is_widget ) {
187 return $this->elements_manager->create_element_instance( [
188 'elType' => 'widget',
189 'widgetType' => $type,
190 'id' => '0',
191 ] );
192 }
193
194 return $this->elements_manager->create_element_instance( [
195 'elType' => $type,
196 'id' => '0',
197 ] );
198 }
199
200 /**
201 * @param Document $document
202 * @param $process_type
203 *
204 * @return $this
205 */
206 private function run_import_export_sanitize_process( Document $document, $process_type ) {
207 if ( ! $this->is_prepared() ) {
208 return $this;
209 }
210
211 $result = $document->process_element_import_export(
212 $this->pending_element,
213 $process_type,
214 [ 'settings' => $this->pending_settings ]
215 );
216
217 if ( empty( $result['settings'] ) ) {
218 return $this;
219 }
220
221 $this->pending_settings = $result['settings'];
222
223 return $this;
224 }
225
226 /**
227 * Get all the available settings of a specific element, including responsive settings.
228 *
229 * @param array $controls
230 *
231 * @return array
232 */
233 private function get_valid_settings_keys( $controls ) {
234 if ( ! $controls ) {
235 return [];
236 }
237
238 $control_keys = array_keys( $controls );
239
240 $optional_responsive_keys = [
241 Breakpoints_Manager::BREAKPOINT_KEY_MOBILE,
242 Breakpoints_Manager::BREAKPOINT_KEY_MOBILE_EXTRA,
243 Breakpoints_Manager::BREAKPOINT_KEY_TABLET,
244 Breakpoints_Manager::BREAKPOINT_KEY_TABLET_EXTRA,
245 Breakpoints_Manager::BREAKPOINT_KEY_LAPTOP,
246 Breakpoints_Manager::BREAKPOINT_KEY_WIDESCREEN,
247 ];
248
249 $settings = [];
250
251 foreach ( $control_keys as $control_key ) {
252 // Add the responsive settings.
253 foreach ( $optional_responsive_keys as $responsive_key ) {
254 $settings[] = "{$control_key}_{$responsive_key}";
255 }
256 // Add the setting itself (not responsive).
257 $settings[] = $control_key;
258 }
259
260 return $settings;
261 }
262
263 /**
264 * Remove invalid settings.
265 *
266 * @param $settings
267 * @param $valid_settings_keys
268 *
269 * @return array
270 */
271 private function filter_invalid_settings( $settings, $valid_settings_keys ) {
272 return array_filter(
273 $settings,
274 function ( $setting_key ) use ( $valid_settings_keys ) {
275 return in_array( $setting_key, $valid_settings_keys, true );
276 },
277 ARRAY_FILTER_USE_KEY
278 );
279 }
280 }
281