PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.6.6
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.6.6
1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 1.7.0.9 1.8.0 All 210 releases
charitable / includes / utilities / class-charitable-data-processor.php

class-charitable-data-processor.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.6.6, at includes/utilities/class-charitable-data-processor.php

363 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Responsible for accepting a set of raw data, such as form submission data,
4 * and sanitizing and normalizing that data, depending on its data type and
5 * the type of data (text, checkbox, etc.).
6 *
7 * @package Charitable/Classes/Charitable_Data_Processor
8 * @author Eric Daams
9 * @copyright Copyright (c) 2018, Studio 164a
10 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
11 * @since 1.5.9
12 * @version 1.5.9
13 */
14
15 // Exit if accessed directly.
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit;
18 }
19
20
21 if ( ! class_exists( 'Charitable_Data_Processor' ) ) :
22
23 class Charitable_Data_Processor {
24 /**
25 * The raw input data.
26 *
27 * @since 1.2.0
28 *
29 * @var array
30 */
31 protected $data;
32
33 /**
34 * The map of fields.
35 *
36 * @since 1.2.0
37 *
38 * @var array
39 */
40 protected $fields;
41
42 /**
43 * This is the output data. It has the same structure
44 * as the map of fields, but has the sanitized &
45 * normalized values from the input data.
46 *
47 * @since 1.2.0
48 *
49 * @var array
50 */
51 protected $output;
52
53 /**
54 * Instantiate the formatter with a dataset and array of fields.
55 *
56 * @since 1.5.9
57 *
58 * @param array $data The raw input data.
59 * @param array $fields The map of fields.
60 */
61 public function __construct( $data, $fields ) {
62 $this->data = $data;
63 $this->fields = $fields;
64 $this->output = array();
65 $this->invalid = false;
66
67 $this->process_data( $this->fields );
68 }
69
70 /**
71 * Returns whether the data is valid.
72 *
73 * @since 1.5.9
74 *
75 * @return boolean
76 */
77 public function is_valid() {
78 return false == $this->invalid;
79 }
80
81 /**
82 * Return a single field's output data.
83 *
84 * @since 1.5.9
85 *
86 * @param string $key The field key.
87 * @param string|false $data_type Optional. The data type.
88 * @return mixed|null
89 */
90 public function get( $key, $data_type = false ) {
91 if ( $data_type ) {
92 return $this->get_from_data_type( $key, $data_type );
93 }
94
95 return isset( $this->output[ $key ] ) ? $this->output[ $key ] : null;
96 }
97
98 /**
99 * Return a single field's output data, from a data type.
100 *
101 * @since 1.5.9
102 *
103 * @param string $key The field key.
104 * @param string $data_type The data type.
105 * @return mixed|null
106 */
107 public function get_from_data_type( $key, $data_type ) {
108 return isset( $this->output[ $data_type ][ $key ] ) ? $this->output[ $data_type ][ $key ] : null;
109 }
110
111 /**
112 * Returns the full output array.
113 *
114 * @since 1.5.9
115 *
116 * @return array
117 */
118 public function output() {
119 return $this->output;
120 }
121
122 /**
123 * Process an array of fields.
124 *
125 * @since 1.5.9
126 *
127 * @param array $fields Map of fields.
128 * @param false|string $data_type Optional. Data type.
129 * @return void
130 */
131 protected function process_data( $fields, $data_type = false ) {
132 $data = array();
133
134 foreach ( $fields as $key => $type ) {
135 if ( is_array( $type ) ) {
136 $this->process_data( $type, $key );
137 continue;
138 }
139
140 $data[ $key ] = $this->process_field( $key, $type, $data_type );
141 }
142
143 if ( ! empty( $data ) ) {
144 $this->set_output( $data, $data_type );
145 }
146 }
147
148 /**
149 * Add a set of data to the output array.
150 *
151 * @since 1.5.9
152 *
153 * @param array $data The processed data.
154 * @param false|string $data_type Optional. Data type.
155 * @return void
156 */
157 protected function set_output( $data, $data_type = false ) {
158 if ( $data_type ) {
159 $this->output[ $data_type ] = $data;
160 } else {
161 $this->output = $data;
162 }
163 }
164
165 /**
166 * Apply the correct function to a field, based on the type of function
167 * and the key, type and data type of the field.
168 *
169 * @since 1.5.9
170 *
171 * @param array $functions Stack of functions, in order of priority, with $args
172 * passed as the value of the function.
173 * @return mixed|null
174 */
175 protected function apply_function_to_field( $functions, $default = '' ) {
176 foreach ( $functions as $function => $args ) {
177 if ( method_exists( $this, $function ) ) {
178 return call_user_func_array( array( $this, $function ), $args );
179 }
180 }
181
182 return $default;
183 }
184
185 /**
186 * Process a single field, returning the set value or null.
187 *
188 * @since 1.5.9
189 *
190 * @param string $key The field key.
191 * @param string $type The type of field.
192 * @param string|false $data_type Optional. The data type.
193 * @return mixed|null The set value of the field, or NULL if the
194 * field was not contained in the data.
195 */
196 protected function process_field( $key, $type, $data_type = false ) {
197 /* Retrieve the value. */
198 $value = $this->apply_function_to_field( array(
199 'process_' . $key => array( $type, $data_type ),
200 'process_' . $data_type => array( $key, $type ),
201 'process_' . $type => array( $key, $data_type ),
202 'process_generic_field' => array( $key ),
203 ) );
204
205 /* Return the value after it is sanitized. */
206 return $this->apply_function_to_field( array(
207 'sanitize_' . $key => array( $value, $type, $data_type ),
208 'sanitize_' . $data_type => array( $value, $key, $type ),
209 'sanitize_' . $type => array( $value, $key, $data_type ),
210 ), $value );
211 }
212
213 /**
214 * Process a generic field. i.e. One that hasn't been processed
215 * by any of the other processors.
216 *
217 * @since 1.5.9
218 *
219 * @param string $key The field key.
220 * @return mixed|null The set value of the field, or NULL if the
221 * field was not contained in the data.
222 */
223 public function process_generic_field( $key ) {
224 return array_key_exists( $key, $this->data ) ? $this->data[ $key ] : null;
225 }
226
227 /**
228 * Process a checkbox field.
229 *
230 * @since 1.5.9
231 *
232 * @param string $key The field key.
233 * @return int|string Returns 0 if the checkbox was not checked, or the
234 * value of the checkbox if checked.
235 */
236 protected function process_checkbox( $key ) {
237 return array_key_exists( $key, $this->data ) ? $this->data[ $key ] : 0;
238 }
239
240 /**
241 * Process a picture field.
242 *
243 * @since 1.5.9
244 *
245 * @param string $key
246 * @return int|false
247 */
248 protected function process_picture( $key ) {
249 $value = array_key_exists( $key, $this->data ) ? $this->data[ $key ] : '';
250
251 /**
252 * If Javascript is enabled, we do not expect to have a $_FILES array with the
253 * picture, as the upload was already handled client-side.
254 */
255 if ( ! $this->picture_file_exists( $key ) ) {
256 return $value;
257 }
258
259 $value = $this->upload_attachment( $key );
260
261 if ( is_wp_error( $value ) ) {
262 charitable_get_notices()->add_errors_from_wp_error( $value );
263 $value = '';
264 $this->invalid = true;
265 }
266
267 return $value;
268 }
269
270 /**
271 * Sanitize a number.
272 *
273 * @since 1.5.9
274 *
275 * @param string|int The number to be sanitized.
276 * @return int
277 */
278 protected function sanitize_number( $value ) {
279 return intval( $value );
280 }
281
282 /**
283 * Sanitize a value received from a datepicker.
284 *
285 * @since 1.5.9
286 *
287 * @param string $value The datepicker value.
288 * @return string|int If a date was chosen, returns the date in YYYY-MM-DD format.
289 * Otherwise, returns 0.
290 */
291 protected function sanitize_datepicker( $value ) {
292 if ( empty( $value ) ) {
293 return 0;
294 }
295
296 return charitable_sanitize_date( $value, 'Y-m-d' );
297 }
298
299 /**
300 * Returns true if a file was found for the picture in the $_FILES array.
301 *
302 * @since 1.5.9
303 *
304 * @param string $key The picture file key.
305 * @return boolean
306 */
307 protected function picture_file_exists( $key ) {
308 return isset( $_FILES ) && isset( $_FILES[ $key ] );
309 }
310
311 /**
312 * Uploads a file and attaches it to the given post.
313 *
314 * @since 1.5.9
315 *
316 * @param string $file_key Key of the file input.
317 * @param int $post_id Post ID.
318 * @return int|WP_Error ID of the attachment or a WP_Error object on failure.
319 */
320 public function upload_attachment( $file_key, $post_id = 0 ) {
321 require_once( ABSPATH . 'wp-admin/includes/image.php' );
322 require_once( ABSPATH . 'wp-admin/includes/file.php' );
323 require_once( ABSPATH . 'wp-admin/includes/media.php' );
324
325 $overrides = $this->get_file_overrides( $file_key, $overrides );
326
327 return media_handle_upload( $file_key, $post_id, array(), $overrides );
328 }
329
330 /**
331 * Return overrides array for use with upload_attachment() methods.
332 *
333 * @since 1.0.0
334 *
335 * @param string $file_key Reference to a single element of `$_FILES`. Call the
336 * function once for each uploaded file.
337 * @param array $overrides Optional. An associative array of names=>values to
338 * override default variables. Default false.
339 * @return array
340 */
341 protected function get_file_overrides( $file_key, $overrides = array() ) {
342 $allowed_mimes = array(
343 'jpg|jpeg|jpe' => 'image/jpeg',
344 'gif' => 'image/gif',
345 'png' => 'image/png',
346 'bmp' => 'image/bmp',
347 'tif|tiff' => 'image/tiff',
348 'ico' => 'image/x-icon',
349 );
350
351 $defaults = array(
352 'test_form' => false,
353 'mimes' => apply_filters( 'charitable_file_' . $file_key . '_allowed_mimes', $allowed_mimes ),
354 );
355
356 $overrides = wp_parse_args( $overrides, $defaults );
357
358 return $overrides;
359 }
360 }
361
362 endif;
363