PluginProbe
WP Ultimate Post Grid / 4.1.1
WP Ultimate Post Grid v4.1.1
4.1.1 trunk 1.5 1.6 1.7 1.7.1 1.7.2 1.8 1.9 2.0 2.1 2.2 2.3 2.4.0 2.5.0 2.6.0 2.7.0 2.8.0 2.8.2 3.0.0 3.3.0 3.4.0 3.5.0 3.6.0 3.7.0 All 32 releases
wp-ultimate-post-grid / vendor / bv-settings / includes / class-bvs-saver.php

class-bvs-saver.php in WP Ultimate Post Grid 4.1.1, at vendor/bv-settings/includes/class-bvs-saver.php

300 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handle the settings saving.
4 *
5 * @link https://bootstrapped.ventures
6 * @since 1.1.0
7 *
8 * @package BV_Settings
9 */
10
11 if ( ! class_exists( 'BV_Settings_V1_1_0_Saver' ) ) {
12 /**
13 * Handle the settings saving.
14 *
15 * @since 1.1.0
16 */
17 class BV_Settings_V1_1_0_Saver {
18 private $bvs;
19
20 /**
21 * Store main instance and initialize.
22 *
23 * @since 1.1.0
24 * @param object $bvs Versioned settings implementation.
25 */
26 public function __construct( $bvs ) {
27 $this->bvs = $bvs;
28 }
29
30 /**
31 * Update the settings.
32 *
33 * @since 1.1.0
34 * @param array $settings_to_update Settings to update.
35 * @return array
36 */
37 public function update_settings( $settings_to_update ) {
38 $old_settings = $this->bvs->get_settings();
39
40 if ( is_array( $settings_to_update ) ) {
41 $settings_to_update = $this->sanitize_settings( $settings_to_update );
42 $new_settings = array_merge( $old_settings, $settings_to_update );
43
44 $new_settings = apply_filters( $this->bvs->atts['uid'] . '_settings_update', $new_settings, $old_settings );
45
46 update_option( $this->bvs->atts['uid'] . '_settings', $new_settings );
47 $this->bvs->helpers['structure']->set_settings( $new_settings );
48 }
49
50 return $this->bvs->get_settings();
51 }
52
53 /**
54 * Flatten setting options to a list of valid option values.
55 *
56 * @since 1.1.0
57 * @param array $options Options to flatten.
58 * @return array
59 */
60 private function get_option_values( $options ) {
61 $values = array();
62
63 if ( ! is_array( $options ) ) {
64 return $values;
65 }
66
67 foreach ( $options as $key => $option ) {
68 if ( is_array( $option ) && isset( $option['options'] ) ) {
69 $values = array_merge( $values, $this->get_option_values( $option['options'] ) );
70 } elseif ( is_array( $option ) && isset( $option['value'] ) ) {
71 $values[] = $option['value'];
72 } elseif ( is_array( $option ) ) {
73 $values = array_merge( $values, $this->get_option_values( $option ) );
74 } else {
75 $values[] = $key;
76 }
77 }
78
79 return $values;
80 }
81
82 /**
83 * Sanitize an async dropdown value.
84 *
85 * @since 1.1.0
86 * @param mixed $value Value to sanitize.
87 * @param array $details Setting details.
88 * @return mixed
89 */
90 private function sanitize_dropdown_async( $value, $details ) {
91 $value_key = isset( $details['valueKey'] ) ? $details['valueKey'] : 'value';
92 $label_key = isset( $details['labelKey'] ) ? $details['labelKey'] : 'label';
93 $value_storage = isset( $details['valueStorage'] ) ? $details['valueStorage'] : '';
94
95 if ( 'object' === $value_storage || is_array( $value ) ) {
96 if ( is_array( $value ) ) {
97 $value_id = isset( $value[ $value_key ] ) ? $value[ $value_key ] : '';
98 $value_text = isset( $value[ $label_key ] ) ? $value[ $label_key ] : $value_id;
99 } else {
100 $value_id = $value;
101 $value_text = $value;
102 }
103
104 $value_id = is_scalar( $value_id ) ? $value_id : '';
105 $value_text = is_scalar( $value_text ) ? $value_text : '';
106
107 return array(
108 $value_key => sanitize_text_field( $value_id ),
109 $label_key => sanitize_text_field( $value_text ),
110 );
111 }
112
113 return sanitize_text_field( $value );
114 }
115
116 /**
117 * Sanitize an object table value.
118 *
119 * @since 1.1.0
120 * @param mixed $value Value to sanitize.
121 * @param array $details Setting details.
122 * @return array
123 */
124 private function sanitize_object_table( $value, $details ) {
125 $sanitized_value = array();
126
127 if ( ! is_array( $value ) ) {
128 return $sanitized_value;
129 }
130
131 $columns = isset( $details['columns'] ) && is_array( $details['columns'] ) ? $details['columns'] : array();
132 $default_rows = isset( $details['default'] ) && is_array( $details['default'] ) ? $details['default'] : array();
133 $row_keys = ! empty( $default_rows ) ? array_keys( $default_rows ) : array_keys( $value );
134 $row_label_key = isset( $details['rowLabelKey'] ) ? $details['rowLabelKey'] : 'label';
135
136 foreach ( $row_keys as $row_key ) {
137 $row = isset( $value[ $row_key ] ) && is_array( $value[ $row_key ] ) ? $value[ $row_key ] : array();
138 $sanitized_row = array();
139
140 if ( isset( $default_rows[ $row_key ][ $row_label_key ] ) ) {
141 $sanitized_row[ $row_label_key ] = sanitize_text_field( $default_rows[ $row_key ][ $row_label_key ] );
142 } elseif ( isset( $row[ $row_label_key ] ) ) {
143 $sanitized_row[ $row_label_key ] = sanitize_text_field( $row[ $row_label_key ] );
144 }
145
146 foreach ( $columns as $column ) {
147 if ( ! isset( $column['key'] ) ) {
148 continue;
149 }
150
151 $column_key = $column['key'];
152 $column_value = isset( $row[ $column_key ] ) ? $row[ $column_key ] : '';
153
154 if ( isset( $column['format'] ) && 'semicolonList' === $column['format'] ) {
155 if ( is_string( $column_value ) ) {
156 $column_value = explode( ';', $column_value );
157 }
158
159 $sanitized_items = array();
160 if ( is_array( $column_value ) ) {
161 foreach ( $column_value as $item ) {
162 $item = trim( sanitize_text_field( $item ) );
163
164 if ( strlen( $item ) ) {
165 $sanitized_items[] = $item;
166 }
167 }
168 }
169
170 $sanitized_row[ $column_key ] = $sanitized_items;
171 } else {
172 $sanitized_row[ $column_key ] = sanitize_text_field( $column_value );
173 }
174 }
175
176 $sanitized_row_key = ! empty( $default_rows ) ? $row_key : sanitize_key( $row_key );
177 $sanitized_value[ $sanitized_row_key ] = $sanitized_row;
178 }
179
180 return $sanitized_value;
181 }
182
183 /**
184 * Sanitize the settings.
185 *
186 * @since 1.1.0
187 * @param array $settings Settings to sanitize.
188 * @return array
189 */
190 public function sanitize_settings( $settings ) {
191 $sanitized_settings = array();
192 $settings_details = $this->bvs->helpers['structure']->get_details();
193
194 foreach ( $settings as $id => $value ) {
195 if ( array_key_exists( $id, $settings_details ) ) {
196 $details = $settings_details[ $id ];
197
198 $sanitized_value = null;
199
200 // Check for custom sanitization function.
201 if ( isset( $details['sanitize'] ) && is_callable( $details['sanitize'] ) ) {
202 $sanitized_value = call_user_func( $details['sanitize'], $value );
203 }
204
205 // Options callback.
206 if ( isset( $details['optionsCallback'] ) ) {
207 $details['options'] = call_user_func( $details['optionsCallback'], $details );
208 }
209
210 // Value format sanitization.
211 if ( is_null( $sanitized_value ) && isset( $details['valueFormat'] ) && 'newlineList' === $details['valueFormat'] ) {
212 if ( is_string( $value ) ) {
213 $value = preg_split( '/\r\n|\r|\n/', $value );
214 }
215
216 $sanitized_value = array();
217
218 if ( is_array( $value ) ) {
219 foreach ( $value as $item ) {
220 $sanitized_value[] = sanitize_text_field( $item );
221 }
222 }
223 }
224
225 // Default sanitization based on type.
226 if ( is_null( $sanitized_value ) && isset( $details['type'] ) ) {
227 switch ( $details['type'] ) {
228 case 'code':
229 $sanitized_value = wp_kses_post( $value );
230
231 // Fix for CSS code.
232 $sanitized_value = str_replace( '&gt;', '>', $sanitized_value );
233 break;
234 case 'color':
235 $sanitized_value = sanitize_text_field( $value );
236 break;
237 case 'dropdown':
238 if ( isset( $details['options'] ) && array_key_exists( $value, $details['options'] ) ) {
239 $sanitized_value = $value;
240 }
241 break;
242 case 'dropdownGrouped':
243 $option_values = isset( $details['options'] ) ? $this->get_option_values( $details['options'] ) : array();
244 $option_values = array_map( 'strval', $option_values );
245
246 if ( empty( $option_values ) || in_array( (string) $value, $option_values, true ) ) {
247 $sanitized_value = sanitize_text_field( $value );
248 }
249 break;
250 case 'dropdownAsync':
251 $sanitized_value = $this->sanitize_dropdown_async( $value, $details );
252 break;
253 case 'dropdownMultiselect':
254 $sanitized_value = array();
255
256 if ( isset( $details['options'] ) && is_array( $value ) ) {
257 foreach ( $value as $option ) {
258 if ( array_key_exists( $option, $details['options'] ) ) {
259 $sanitized_value[] = $option;
260 }
261 }
262 }
263 break;
264 case 'email':
265 $sanitized_value = sanitize_email( $value );
266 break;
267 case 'number':
268 $sanitized_value = sanitize_text_field( $value );
269 break;
270 case 'objectTable':
271 $sanitized_value = $this->sanitize_object_table( $value, $details );
272 break;
273 case 'richTextarea':
274 $sanitized_value = wp_kses_post( $value );
275 break;
276 case 'text':
277 $sanitized_value = sanitize_text_field( $value );
278 break;
279 case 'textarea':
280 $sanitized_value = wp_kses_post( $value );
281 break;
282 case 'toggle':
283 $sanitized_value = $value ? true : false;
284 break;
285 }
286 }
287
288 $sanitized_value = apply_filters( $this->bvs->atts['uid'] . '_sanitized', $sanitized_value, $value, $id, $details );
289
290 if ( ! is_null( $sanitized_value ) ) {
291 $sanitized_settings[ $id ] = $sanitized_value;
292 }
293 }
294 }
295
296 return $sanitized_settings;
297 }
298 }
299 }
300