Completed
Push — master ( 4647f7...7bc7ee )
by Scott Kingsley
04:50
created
includes/class-wp-fields-api-field.php 3 patches
Indentation   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -1,12 +1,12 @@
 block discarded – undo
1 1
 <?php
2 2
 /**
3
- * Fields API Field Class
4
- *
5
- * Handles saving and sanitizing of fields.
6
- *
7
- * @package    WordPress
8
- * @subpackage Fields_API
9
- */
3
+	 * Fields API Field Class
4
+	 *
5
+	 * Handles saving and sanitizing of fields.
6
+	 *
7
+	 * @package    WordPress
8
+	 * @subpackage Fields_API
9
+	 */
10 10
 class WP_Fields_API_Field {
11 11
 
12 12
 	/**
Please login to merge, or discard this patch.
Doc Comments   +3 added lines, -7 removed lines patch added patch discarded remove patch
@@ -133,10 +133,8 @@  discard block
 block discarded – undo
133 133
 	 * Check user capabilities and theme supports, and then save
134 134
 	 * the value of the field.
135 135
 	 *
136
-	 * @param mixed $value   The value to save.
137
-	 * @param int   $item_id The Item ID.
138 136
 	 *
139
-	 * @return false|mixed False if cap check fails or value isn't set.
137
+	 * @return null|boolean False if cap check fails or value isn't set.
140 138
 	 */
141 139
 	public function save() {
142 140
 
@@ -188,9 +186,8 @@  discard block
 block discarded – undo
188 186
 	 * Save the value of the field, using the related API.
189 187
 	 *
190 188
 	 * @param mixed $value The value to update.
191
-	 * @param int $item_id Item ID.
192 189
 	 *
193
-	 * @return mixed The result of saving the value.
190
+	 * @return null|boolean The result of saving the value.
194 191
 	 */
195 192
 	protected function update( $value ) {
196 193
 
@@ -325,7 +322,6 @@  discard block
 block discarded – undo
325 322
 	/**
326 323
 	 * Fetch the value of the field.
327 324
 	 *
328
-	 * @param int $item_id (optional) The Item ID.
329 325
 	 *
330 326
 	 * @return mixed The value.
331 327
 	 */
@@ -582,7 +578,7 @@  discard block
 block discarded – undo
582 578
 	 *
583 579
 	 * @param       $root
584 580
 	 * @param       $keys
585
-	 * @param mixed $default A default value which is used as a fallback. Default is null.
581
+	 * @param string $default A default value which is used as a fallback. Default is null.
586 582
 	 *
587 583
 	 * @return mixed The requested value or the default value.
588 584
 	 */
Please login to merge, or discard this patch.
Spacing   +107 added lines, -107 removed lines patch added patch discarded remove patch
@@ -74,7 +74,7 @@  discard block
 block discarded – undo
74 74
 
75 75
 		$args = func_get_args();
76 76
 
77
-		call_user_func_array( array( $this, 'init' ), $args );
77
+		call_user_func_array(array($this, 'init'), $args);
78 78
 
79 79
 	}
80 80
 
@@ -88,11 +88,11 @@  discard block
 block discarded – undo
88 88
 	 *
89 89
 	 * @return WP_Fields_API_Field $field
90 90
 	 */
91
-	public function init( $object_type, $id, $args = array() ) {
91
+	public function init($object_type, $id, $args = array()) {
92 92
 
93 93
 		$this->object_type = $object_type;
94 94
 
95
-		if ( is_array( $id ) ) {
95
+		if (is_array($id)) {
96 96
 			$args = $id;
97 97
 
98 98
 			$id = '';
@@ -100,31 +100,31 @@  discard block
 block discarded – undo
100 100
 			$this->id = $id;
101 101
 		}
102 102
 
103
-		$keys = array_keys( get_object_vars( $this ) );
103
+		$keys = array_keys(get_object_vars($this));
104 104
 
105
-		foreach ( $keys as $key ) {
106
-			if ( isset( $args[ $key ] ) ) {
107
-				$this->$key = $args[ $key ];
105
+		foreach ($keys as $key) {
106
+			if (isset($args[$key])) {
107
+				$this->$key = $args[$key];
108 108
 			}
109 109
 		}
110 110
 
111 111
 		// Parse the ID for array keys.
112
-		$this->id_data['keys'] = preg_split( '/\[/', str_replace( ']', '', $this->id ) );
113
-		$this->id_data['base'] = array_shift( $this->id_data['keys'] );
112
+		$this->id_data['keys'] = preg_split('/\[/', str_replace(']', '', $this->id));
113
+		$this->id_data['base'] = array_shift($this->id_data['keys']);
114 114
 
115 115
 		// Rebuild the ID.
116 116
 		$this->id = $this->id_data['base'];
117 117
 
118
-		if ( ! empty( $this->id_data['keys'] ) ) {
119
-			$this->id .= '[' . implode( '][', $this->id_data['keys'] ) . ']';
118
+		if ( ! empty($this->id_data['keys'])) {
119
+			$this->id .= '['.implode('][', $this->id_data['keys']).']';
120 120
 		}
121 121
 
122
-		if ( $this->sanitize_callback ) {
123
-			add_filter( "fields_sanitize_{$this->object_type}_{$this->object_name}_{$this->id}", $this->sanitize_callback, 10, 2 );
122
+		if ($this->sanitize_callback) {
123
+			add_filter("fields_sanitize_{$this->object_type}_{$this->object_name}_{$this->id}", $this->sanitize_callback, 10, 2);
124 124
 		}
125 125
 
126
-		if ( $this->sanitize_js_callback ) {
127
-			add_filter( "fields_sanitize_js_{$this->object_type}_{$this->object_name}_{$this->id}", $this->sanitize_js_callback, 10, 2 );
126
+		if ($this->sanitize_js_callback) {
127
+			add_filter("fields_sanitize_js_{$this->object_type}_{$this->object_name}_{$this->id}", $this->sanitize_js_callback, 10, 2);
128 128
 		}
129 129
 
130 130
 	}
@@ -143,7 +143,7 @@  discard block
 block discarded – undo
143 143
 		$value   = func_get_arg(0);
144 144
 		$item_id = func_get_arg(1);
145 145
 
146
-		if ( ! $this->check_capabilities() || false === $value ) {
146
+		if ( ! $this->check_capabilities() || false === $value) {
147 147
 			return false;
148 148
 		}
149 149
 
@@ -157,9 +157,9 @@  discard block
 block discarded – undo
157 157
 		 *
158 158
 		 * @param WP_Fields_API_Field $this {@see WP_Fields_API_Field} instance.
159 159
 		 */
160
-		do_action( 'field_save_' . $this->object_type . ' _' . $this->id_data[ 'base' ], $this, $value, $item_id );
160
+		do_action('field_save_'.$this->object_type.' _'.$this->id_data['base'], $this, $value, $item_id);
161 161
 
162
-		return $this->update( $value, $item_id );
162
+		return $this->update($value, $item_id);
163 163
 
164 164
 	}
165 165
 
@@ -170,9 +170,9 @@  discard block
 block discarded – undo
170 170
 	 *
171 171
 	 * @return mixed Null if an input isn't valid, otherwise the sanitized value.
172 172
 	 */
173
-	public function sanitize( $value ) {
173
+	public function sanitize($value) {
174 174
 
175
-		$value = wp_unslash( $value );
175
+		$value = wp_unslash($value);
176 176
 
177 177
 		/**
178 178
 		 * Filter a Customize field value in un-slashed form.
@@ -180,7 +180,7 @@  discard block
 block discarded – undo
180 180
 		 * @param mixed                $value Value of the field.
181 181
 		 * @param WP_Fields_API_Field $this  WP_Fields_API_Field instance.
182 182
 		 */
183
-		return apply_filters( "fields_sanitize_{$this->object_type}_{$this->object_name}_{$this->id}", $value, $this );
183
+		return apply_filters("fields_sanitize_{$this->object_type}_{$this->object_name}_{$this->id}", $value, $this);
184 184
 
185 185
 	}
186 186
 
@@ -192,24 +192,24 @@  discard block
 block discarded – undo
192 192
 	 *
193 193
 	 * @return mixed The result of saving the value.
194 194
 	 */
195
-	protected function update( $value ) {
195
+	protected function update($value) {
196 196
 
197 197
 		// @todo Support post / term / user / comment object field updates
198 198
 
199 199
 		$item_id = func_get_arg(1);
200 200
 
201
-		switch ( $this->object_type ) {
201
+		switch ($this->object_type) {
202 202
 			case 'customizer' :
203
-				return $this->_update_theme_mod( $value );
203
+				return $this->_update_theme_mod($value);
204 204
 
205 205
 			case 'settings' :
206
-				return $this->_update_option( $value );
206
+				return $this->_update_option($value);
207 207
 
208 208
 			case 'post' :
209 209
 			case 'term' :
210 210
 			case 'user' :
211 211
 			case 'comment' :
212
-				return $this->_update_meta( $this->object_type, $value, $item_id );
212
+				return $this->_update_meta($this->object_type, $value, $item_id);
213 213
 
214 214
 			default :
215 215
 
@@ -223,7 +223,7 @@  discard block
 block discarded – undo
223 223
 				 * @param int                 $item_id Item ID.
224 224
 				 * @param WP_Fields_API_Field $this    WP_Fields_API_Field instance.
225 225
 				 */
226
-				do_action( "fields_update_{$this->object_type}", $value, $item_id, $this );
226
+				do_action("fields_update_{$this->object_type}", $value, $item_id, $this);
227 227
 		}
228 228
 
229 229
 		return null;
@@ -237,22 +237,22 @@  discard block
 block discarded – undo
237 237
 	 *
238 238
 	 * @return null
239 239
 	 */
240
-	protected function _update_theme_mod( $value ) {
240
+	protected function _update_theme_mod($value) {
241 241
 
242
-		if ( is_null( $value ) ) {
243
-			remove_theme_mod( $this->id_data['base'] );
242
+		if (is_null($value)) {
243
+			remove_theme_mod($this->id_data['base']);
244 244
 		}
245 245
 
246 246
 		// Handle non-array theme mod.
247
-		if ( empty( $this->id_data['keys'] ) ) {
248
-			set_theme_mod( $this->id_data['base'], $value );
247
+		if (empty($this->id_data['keys'])) {
248
+			set_theme_mod($this->id_data['base'], $value);
249 249
 		} else {
250 250
 			// Handle array-based theme mod.
251
-			$mods = get_theme_mod( $this->id_data['base'] );
252
-			$mods = $this->multidimensional_replace( $mods, $this->id_data['keys'], $value );
251
+			$mods = get_theme_mod($this->id_data['base']);
252
+			$mods = $this->multidimensional_replace($mods, $this->id_data['keys'], $value);
253 253
 
254
-			if ( isset( $mods ) ) {
255
-				set_theme_mod( $this->id_data['base'], $mods );
254
+			if (isset($mods)) {
255
+				set_theme_mod($this->id_data['base'], $mods);
256 256
 			}
257 257
 		}
258 258
 
@@ -267,23 +267,23 @@  discard block
 block discarded – undo
267 267
 	 *
268 268
 	 * @return bool|null The result of saving the value.
269 269
 	 */
270
-	protected function _update_option( $value ) {
270
+	protected function _update_option($value) {
271 271
 
272
-		if ( is_null( $value ) ) {
273
-			delete_option( $this->id_data['base'] );
272
+		if (is_null($value)) {
273
+			delete_option($this->id_data['base']);
274 274
 		}
275 275
 
276 276
 		// Handle non-array option.
277
-		if ( empty( $this->id_data['keys'] ) ) {
278
-			return update_option( $this->id_data['base'], $value );
277
+		if (empty($this->id_data['keys'])) {
278
+			return update_option($this->id_data['base'], $value);
279 279
 		}
280 280
 
281 281
 		// Handle array-based options.
282
-		$options = get_option( $this->id_data['base'] );
283
-		$options = $this->multidimensional_replace( $options, $this->id_data['keys'], $value );
282
+		$options = get_option($this->id_data['base']);
283
+		$options = $this->multidimensional_replace($options, $this->id_data['keys'], $value);
284 284
 
285
-		if ( isset( $options ) ) {
286
-			return update_option( $this->id_data['base'], $options );
285
+		if (isset($options)) {
286
+			return update_option($this->id_data['base'], $options);
287 287
 		}
288 288
 
289 289
 		return null;
@@ -299,23 +299,23 @@  discard block
 block discarded – undo
299 299
 	 *
300 300
 	 * @return bool|null The result of saving the value.
301 301
 	 */
302
-	protected function _update_meta( $meta_type, $value, $item_id = 0 ) {
302
+	protected function _update_meta($meta_type, $value, $item_id = 0) {
303 303
 
304
-		if ( is_null( $value ) ) {
305
-			delete_metadata( $meta_type, $item_id, $this->id_data['base'] );
304
+		if (is_null($value)) {
305
+			delete_metadata($meta_type, $item_id, $this->id_data['base']);
306 306
 		}
307 307
 
308 308
 		// Handle non-array option.
309
-		if ( empty( $this->id_data['keys'] ) ) {
310
-			return update_metadata( $meta_type, $item_id, $this->id_data['base'], $value );
309
+		if (empty($this->id_data['keys'])) {
310
+			return update_metadata($meta_type, $item_id, $this->id_data['base'], $value);
311 311
 		}
312 312
 
313 313
 		// Handle array-based keys.
314
-		$keys = get_metadata( $meta_type, 0, $this->id_data['base'] );
315
-		$keys = $this->multidimensional_replace( $keys, $this->id_data['keys'], $value );
314
+		$keys = get_metadata($meta_type, 0, $this->id_data['base']);
315
+		$keys = $this->multidimensional_replace($keys, $this->id_data['keys'], $value);
316 316
 
317
-		if ( isset( $keys ) ) {
318
-			return update_metadata( $meta_type, $item_id, $this->id_data['base'], $keys );
317
+		if (isset($keys)) {
318
+			return update_metadata($meta_type, $item_id, $this->id_data['base'], $keys);
319 319
 		}
320 320
 
321 321
 		return null;
@@ -333,19 +333,19 @@  discard block
 block discarded – undo
333 333
 
334 334
 		$item_id = func_get_arg(0);
335 335
 
336
-		switch ( $this->object_type ) {
336
+		switch ($this->object_type) {
337 337
 			case 'post' :
338 338
 			case 'term' :
339 339
 			case 'user' :
340 340
 			case 'comment' :
341
-				$value = $this->get_object_value( $item_id );
342
-				$value = $this->multidimensional_get( $value, $this->id_data['keys'], $this->default );
341
+				$value = $this->get_object_value($item_id);
342
+				$value = $this->multidimensional_get($value, $this->id_data['keys'], $this->default);
343 343
 				break;
344 344
 
345 345
 			case 'customizer' :
346 346
 			case 'settings' :
347 347
 				$value = $this->get_option_value();
348
-				$value = $this->multidimensional_get( $value, $this->id_data['keys'], $this->default );
348
+				$value = $this->multidimensional_get($value, $this->id_data['keys'], $this->default);
349 349
 				break;
350 350
 
351 351
 			default :
@@ -361,7 +361,7 @@  discard block
 block discarded – undo
361 361
 				 * @param mixed $default The field default value. Default empty.
362 362
 				 * @param int   $item_id (optional) The Item ID.
363 363
 				 */
364
-				$value = apply_filters( 'fields_value_' . $this->object_type . '_' . $this->object_name . '_' . $this->id_data['base'], $this->default, $item_id );
364
+				$value = apply_filters('fields_value_'.$this->object_type.'_'.$this->object_name.'_'.$this->id_data['base'], $this->default, $item_id);
365 365
 				break;
366 366
 		}
367 367
 
@@ -376,37 +376,37 @@  discard block
 block discarded – undo
376 376
 	 *
377 377
 	 * @return mixed|null
378 378
 	 */
379
-	public function get_object_value( $item_id ) {
379
+	public function get_object_value($item_id) {
380 380
 
381 381
 		$value = null;
382 382
 		$object = null;
383 383
 
384 384
 		$field_key = $this->id_data['base'];
385 385
 
386
-		switch ( $this->object_type ) {
386
+		switch ($this->object_type) {
387 387
 			case 'post' :
388
-				$object = get_post( $item_id );
388
+				$object = get_post($item_id);
389 389
 				break;
390 390
 
391 391
 			case 'term' :
392
-				$object = get_term( $item_id );
392
+				$object = get_term($item_id);
393 393
 				break;
394 394
 
395 395
 			case 'user' :
396
-				$object = get_userdata( $item_id );
396
+				$object = get_userdata($item_id);
397 397
 				break;
398 398
 
399 399
 			case 'comment' :
400
-				$object = get_comment( $item_id );
400
+				$object = get_comment($item_id);
401 401
 				break;
402 402
 		}
403 403
 
404
-		if ( $object && ! is_wp_error( $object ) && isset( $object->{$field_key} ) ) {
404
+		if ($object && ! is_wp_error($object) && isset($object->{$field_key} )) {
405 405
 			// Get value from object
406 406
 			$value = $object->{$field_key};
407 407
 		} else {
408 408
 			// Get value from meta
409
-			$value = get_metadata( $this->object_type, $item_id, $field_key );
409
+			$value = get_metadata($this->object_type, $item_id, $field_key);
410 410
 		}
411 411
 
412 412
 		return $value;
@@ -423,7 +423,7 @@  discard block
 block discarded – undo
423 423
 		$function = '';
424 424
 		$value = null;
425 425
 
426
-		switch ( $this->object_type ) {
426
+		switch ($this->object_type) {
427 427
 			case 'customizer' :
428 428
 				$function = 'get_theme_mod';
429 429
 				break;
@@ -433,14 +433,14 @@  discard block
 block discarded – undo
433 433
 				break;
434 434
 		}
435 435
 
436
-		if ( is_callable( $function ) ) {
436
+		if (is_callable($function)) {
437 437
 			// Handle non-array value
438
-			if ( empty( $this->id_data['keys'] ) ) {
439
-				return $function( $this->id_data['base'], $this->default );
438
+			if (empty($this->id_data['keys'])) {
439
+				return $function($this->id_data['base'], $this->default);
440 440
 			}
441 441
 
442 442
 			// Handle array-based value
443
-			$value = $function( $this->id_data['base'] );
443
+			$value = $function($this->id_data['base']);
444 444
 		}
445 445
 
446 446
 		return $value;
@@ -464,10 +464,10 @@  discard block
 block discarded – undo
464 464
 		 * @param mixed                $value The field value.
465 465
 		 * @param WP_Fields_API_Field $this  {@see WP_Fields_API_Field} instance.
466 466
 		 */
467
-		$value = apply_filters( "fields_sanitize_js_{$this->object_type}_{$this->object_name}_{$this->id}", $value, $this );
467
+		$value = apply_filters("fields_sanitize_js_{$this->object_type}_{$this->object_name}_{$this->id}", $value, $this);
468 468
 
469
-		if ( is_string( $value ) ) {
470
-			return html_entity_decode( $value, ENT_QUOTES, 'UTF-8' );
469
+		if (is_string($value)) {
470
+			return html_entity_decode($value, ENT_QUOTES, 'UTF-8');
471 471
 		}
472 472
 
473 473
 		return $value;
@@ -481,11 +481,11 @@  discard block
 block discarded – undo
481 481
 	 */
482 482
 	public function check_capabilities() {
483 483
 
484
-		if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) {
484
+		if ($this->capability && ! call_user_func_array('current_user_can', (array) $this->capability)) {
485 485
 			return false;
486 486
 		}
487 487
 
488
-		if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) ) {
488
+		if ($this->theme_supports && ! call_user_func_array('current_theme_supports', (array) $this->theme_supports)) {
489 489
 			return false;
490 490
 		}
491 491
 
@@ -502,42 +502,42 @@  discard block
 block discarded – undo
502 502
 	 *
503 503
 	 * @return null|array Keys are 'root', 'node', and 'key'.
504 504
 	 */
505
-	final protected function multidimensional( &$root, $keys, $create = false ) {
505
+	final protected function multidimensional(&$root, $keys, $create = false) {
506 506
 
507
-		if ( $create && empty( $root ) ) {
507
+		if ($create && empty($root)) {
508 508
 			$root = array();
509 509
 		}
510 510
 
511
-		if ( ! isset( $root ) || empty( $keys ) ) {
511
+		if ( ! isset($root) || empty($keys)) {
512 512
 			return null;
513 513
 		}
514 514
 
515
-		$last = array_pop( $keys );
515
+		$last = array_pop($keys);
516 516
 		$node = &$root;
517 517
 
518
-		foreach ( $keys as $key ) {
519
-			if ( $create && ! isset( $node[ $key ] ) ) {
520
-				$node[ $key ] = array();
518
+		foreach ($keys as $key) {
519
+			if ($create && ! isset($node[$key])) {
520
+				$node[$key] = array();
521 521
 			}
522 522
 
523
-			if ( ! is_array( $node ) || ! isset( $node[ $key ] ) ) {
523
+			if ( ! is_array($node) || ! isset($node[$key])) {
524 524
 				return null;
525 525
 			}
526 526
 
527
-			$node = &$node[ $key ];
527
+			$node = &$node[$key];
528 528
 		}
529 529
 
530
-		if ( $create ) {
531
-			if ( ! is_array( $node ) ) {
530
+		if ($create) {
531
+			if ( ! is_array($node)) {
532 532
 				// account for an array overriding a string or object value
533 533
 				$node = array();
534 534
 			}
535
-			if ( ! isset( $node[ $last ] ) ) {
536
-				$node[ $last ] = array();
535
+			if ( ! isset($node[$last])) {
536
+				$node[$last] = array();
537 537
 			}
538 538
 		}
539 539
 
540
-		if ( ! isset( $node[ $last ] ) ) {
540
+		if ( ! isset($node[$last])) {
541 541
 			return null;
542 542
 		}
543 543
 
@@ -558,19 +558,19 @@  discard block
 block discarded – undo
558 558
 	 *
559 559
 	 * @return
560 560
 	 */
561
-	final protected function multidimensional_replace( $root, $keys, $value ) {
561
+	final protected function multidimensional_replace($root, $keys, $value) {
562 562
 
563
-		if ( ! isset( $value ) ) {
563
+		if ( ! isset($value)) {
564 564
 			return $root;
565
-		} elseif ( empty( $keys ) ) {
565
+		} elseif (empty($keys)) {
566 566
 			// If there are no keys, we're replacing the root.
567 567
 			return $value;
568 568
 		}
569 569
 
570
-		$result = $this->multidimensional( $root, $keys, true );
570
+		$result = $this->multidimensional($root, $keys, true);
571 571
 
572
-		if ( isset( $result ) ) {
573
-			$result['node'][ $result['key'] ] = $value;
572
+		if (isset($result)) {
573
+			$result['node'][$result['key']] = $value;
574 574
 		}
575 575
 
576 576
 		return $root;
@@ -586,18 +586,18 @@  discard block
 block discarded – undo
586 586
 	 *
587 587
 	 * @return mixed The requested value or the default value.
588 588
 	 */
589
-	final protected function multidimensional_get( $root, $keys, $default = null ) {
589
+	final protected function multidimensional_get($root, $keys, $default = null) {
590 590
 
591 591
 		// If there are no keys, test the root.
592
-		if ( empty( $keys ) ) {
593
-			if ( isset( $root ) ) {
592
+		if (empty($keys)) {
593
+			if (isset($root)) {
594 594
 				return $root;
595 595
 			}
596 596
 		} else {
597
-			$result = $this->multidimensional( $root, $keys );
597
+			$result = $this->multidimensional($root, $keys);
598 598
 
599
-			if ( isset( $result ) ) {
600
-				return $result['node'][ $result['key'] ];
599
+			if (isset($result)) {
600
+				return $result['node'][$result['key']];
601 601
 			}
602 602
 		}
603 603
 
@@ -613,11 +613,11 @@  discard block
 block discarded – undo
613 613
 	 *
614 614
 	 * @return bool True if value is set, false if not.
615 615
 	 */
616
-	final protected function multidimensional_isset( $root, $keys ) {
616
+	final protected function multidimensional_isset($root, $keys) {
617 617
 
618
-		$result = $this->multidimensional_get( $root, $keys );
618
+		$result = $this->multidimensional_get($root, $keys);
619 619
 
620
-		return isset( $result );
620
+		return isset($result);
621 621
 
622 622
 	}
623 623
 }
@@ -639,7 +639,7 @@  discard block
 block discarded – undo
639 639
 	 *
640 640
 	 * @return mixed The result of saving the value.
641 641
 	 */
642
-	public function update( $value ) {
642
+	public function update($value) {
643 643
 
644 644
 		return null;
645 645
 
Please login to merge, or discard this patch.