PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta3
Elementor Website Builder – more than just a page builder v4.3.0-beta3
4.3.0-beta3 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 All 452 releases
elementor / modules / kit-elements-defaults / utils / settings-sanitizer.php

settings-sanitizer.php in Elementor Website Builder – more than just a page builder 4.3.0-beta3, 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 use Elementor\Utils;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly.
12 }
13
14 class Settings_Sanitizer {
15
16 const SPECIAL_SETTINGS = [
17 '__dynamic__',
18 '__globals__',
19 ];
20
21 /**
22 * @var Elements_Manager
23 */
24 private $elements_manager;
25
26 /**
27 * @var array
28 */
29 private $widget_types;
30
31 /**
32 * @var Element_Base | null
33 */
34 private $pending_element = null;
35
36 /**
37 * @var array | null
38 */
39 private $pending_settings = null;
40
41 /**
42 * @param Elements_Manager $elements_manager
43 * @param array $widget_types
44 */
45 public function __construct( Elements_Manager $elements_manager, array $widget_types = [] ) {
46 $this->elements_manager = $elements_manager;
47 $this->widget_types = $widget_types;
48 }
49
50 /**
51 * @param $type
52 *
53 * @return $this
54 */
55 public function for( $type ) {
56 $this->pending_element = $this->create_element( $type );
57
58 return $this;
59 }
60
61 /**
62 * @param $settings
63 *
64 * @return $this
65 */
66 public function using( $settings ) {
67 $this->pending_settings = $settings;
68
69 return $this;
70 }
71
72 /**
73 * @return $this
74 */
75 public function reset() {
76 $this->pending_element = null;
77 $this->pending_settings = null;
78
79 return $this;
80 }
81
82 /**
83 * @return bool
84 */
85 public function is_prepared() {
86 return $this->pending_element && is_array( $this->pending_settings );
87 }
88
89 /**
90 * @return $this
91 */
92 public function remove_invalid_settings() {
93 if ( ! $this->is_prepared() ) {
94 return $this;
95 }
96
97 $valid_settings_keys = $this->get_valid_settings_keys(
98 $this->pending_element->get_controls()
99 );
100
101 $this->pending_settings = $this->filter_invalid_settings(
102 $this->pending_settings,
103 array_merge( $valid_settings_keys, self::SPECIAL_SETTINGS )
104 );
105
106 foreach ( self::SPECIAL_SETTINGS as $special_setting ) {
107 if ( ! isset( $this->pending_settings[ $special_setting ] ) ) {
108 continue;
109 }
110
111 $this->pending_settings[ $special_setting ] = $this->filter_invalid_settings(
112 $this->pending_settings[ $special_setting ],
113 $valid_settings_keys
114 );
115 }
116
117 return $this;
118 }
119
120 public function kses_deep() {
121 if ( ! $this->is_prepared() ) {
122 return $this;
123 }
124
125 $this->pending_settings = Utils::kses_post_deep( $this->pending_settings );
126
127 return $this;
128 }
129
130 /**
131 * @param Document $document
132 *
133 * @return $this
134 */
135 public function prepare_for_export( Document $document ) {
136 return $this->run_import_export_sanitize_process( $document, 'on_export' );
137 }
138
139 /**
140 * @param Document $document
141 *
142 * @return $this
143 */
144 public function prepare_for_import( Document $document ) {
145 return $this->run_import_export_sanitize_process( $document, 'on_import' );
146 }
147
148 /**
149 * @return array
150 */
151 public function get() {
152 if ( ! $this->is_prepared() ) {
153 return [];
154 }
155
156 $settings = $this->pending_settings;
157
158 $this->reset();
159
160 return $settings;
161 }
162
163 /**
164 * @param string $type
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