Completed
Push — master ( 7f2dcf...7d18c1 )
by Scott Kingsley
02:41
created
includes/class-wp-fields-api-field.php 1 patch
Spacing   +116 added lines, -116 removed lines patch added patch discarded remove patch
@@ -111,7 +111,7 @@  discard block
 block discarded – undo
111 111
 
112 112
 		$args = func_get_args();
113 113
 
114
-		call_user_func_array( array( $this, 'init' ), $args );
114
+		call_user_func_array(array($this, 'init'), $args);
115 115
 
116 116
 	}
117 117
 
@@ -125,11 +125,11 @@  discard block
 block discarded – undo
125 125
 	 *
126 126
 	 * @return WP_Fields_API_Field $field
127 127
 	 */
128
-	public function init( $object_type, $id, $args = array() ) {
128
+	public function init($object_type, $id, $args = array()) {
129 129
 
130 130
 		$this->object_type = $object_type;
131 131
 
132
-		if ( is_array( $id ) ) {
132
+		if (is_array($id)) {
133 133
 			$args = $id;
134 134
 
135 135
 			$id = '';
@@ -137,31 +137,31 @@  discard block
 block discarded – undo
137 137
 			$this->id = $id;
138 138
 		}
139 139
 
140
-		$keys = array_keys( get_object_vars( $this ) );
140
+		$keys = array_keys(get_object_vars($this));
141 141
 
142
-		foreach ( $keys as $key ) {
143
-			if ( isset( $args[ $key ] ) ) {
144
-				$this->$key = $args[ $key ];
142
+		foreach ($keys as $key) {
143
+			if (isset($args[$key])) {
144
+				$this->$key = $args[$key];
145 145
 			}
146 146
 		}
147 147
 
148 148
 		// Parse the ID for array keys.
149
-		$this->id_data['keys'] = preg_split( '/\[/', str_replace( ']', '', $this->id ) );
150
-		$this->id_data['base'] = array_shift( $this->id_data['keys'] );
149
+		$this->id_data['keys'] = preg_split('/\[/', str_replace(']', '', $this->id));
150
+		$this->id_data['base'] = array_shift($this->id_data['keys']);
151 151
 
152 152
 		// Rebuild the ID.
153 153
 		$this->id = $this->id_data['base'];
154 154
 
155
-		if ( ! empty( $this->id_data['keys'] ) ) {
156
-			$this->id .= '[' . implode( '][', $this->id_data['keys'] ) . ']';
155
+		if ( ! empty($this->id_data['keys'])) {
156
+			$this->id .= '['.implode('][', $this->id_data['keys']).']';
157 157
 		}
158 158
 
159
-		if ( $this->sanitize_callback ) {
160
-			add_filter( "fields_sanitize_{$this->object_type}_{$this->object_name}_{$this->id}", $this->sanitize_callback, 10, 2 );
159
+		if ($this->sanitize_callback) {
160
+			add_filter("fields_sanitize_{$this->object_type}_{$this->object_name}_{$this->id}", $this->sanitize_callback, 10, 2);
161 161
 		}
162 162
 
163
-		if ( $this->sanitize_js_callback ) {
164
-			add_filter( "fields_sanitize_js_{$this->object_type}_{$this->object_name}_{$this->id}", $this->sanitize_js_callback, 10, 2 );
163
+		if ($this->sanitize_js_callback) {
164
+			add_filter("fields_sanitize_js_{$this->object_type}_{$this->object_name}_{$this->id}", $this->sanitize_js_callback, 10, 2);
165 165
 		}
166 166
 
167 167
 	}
@@ -180,11 +180,11 @@  discard block
 block discarded – undo
180 180
 		$value   = func_get_arg(0);
181 181
 		$item_id = func_get_arg(1);
182 182
 
183
-		if ( ! $this->check_capabilities() || false === $value ) {
183
+		if ( ! $this->check_capabilities() || false === $value) {
184 184
 			return false;
185 185
 		}
186 186
 
187
-		$value = $this->sanitize( $value );
187
+		$value = $this->sanitize($value);
188 188
 
189 189
 		/**
190 190
 		 * Fires when the WP_Fields_API_Field::save() method is called.
@@ -198,9 +198,9 @@  discard block
 block discarded – undo
198 198
 		 *
199 199
 		 * @return string The value to save
200 200
 		 */
201
-		$value = apply_filters( 'field_save_' . $this->object_type . '_' . $this->id_data[ 'base' ], $value, $item_id, $this );
201
+		$value = apply_filters('field_save_'.$this->object_type.'_'.$this->id_data['base'], $value, $item_id, $this);
202 202
 
203
-		return $this->update( $value, $item_id );
203
+		return $this->update($value, $item_id);
204 204
 
205 205
 	}
206 206
 
@@ -211,9 +211,9 @@  discard block
 block discarded – undo
211 211
 	 *
212 212
 	 * @return mixed Null if an input isn't valid, otherwise the sanitized value.
213 213
 	 */
214
-	public function sanitize( $value ) {
214
+	public function sanitize($value) {
215 215
 
216
-		$value = wp_unslash( $value );
216
+		$value = wp_unslash($value);
217 217
 
218 218
 		/**
219 219
 		 * Filter a Customize field value in un-slashed form.
@@ -221,7 +221,7 @@  discard block
 block discarded – undo
221 221
 		 * @param mixed               $value Value of the field.
222 222
 		 * @param WP_Fields_API_Field $this  WP_Fields_API_Field instance.
223 223
 		 */
224
-		return apply_filters( "fields_sanitize_{$this->object_type}_{$this->object_name}_{$this->id}", $value, $this );
224
+		return apply_filters("fields_sanitize_{$this->object_type}_{$this->object_name}_{$this->id}", $value, $this);
225 225
 
226 226
 	}
227 227
 
@@ -233,27 +233,27 @@  discard block
 block discarded – undo
233 233
 	 *
234 234
 	 * @return mixed The result of saving the value.
235 235
 	 */
236
-	protected function update( $value ) {
236
+	protected function update($value) {
237 237
 
238 238
 		// @todo Support post / term / user / comment object field updates
239 239
 
240 240
 		$item_id = func_get_arg(1);
241 241
 
242
-		switch ( $this->object_type ) {
243
-			case is_callable( $this->update_value_callback ) :
244
-				return call_user_func( $this->update_value_callback, $value, $item_id, $this );
242
+		switch ($this->object_type) {
243
+			case is_callable($this->update_value_callback) :
244
+				return call_user_func($this->update_value_callback, $value, $item_id, $this);
245 245
 
246 246
 			case 'customizer' :
247
-				return $this->_update_theme_mod( $value );
247
+				return $this->_update_theme_mod($value);
248 248
 
249 249
 			case 'settings' :
250
-				return $this->_update_option( $value );
250
+				return $this->_update_option($value);
251 251
 
252 252
 			case 'post' :
253 253
 			case 'term' :
254 254
 			case 'user' :
255 255
 			case 'comment' :
256
-				return $this->_update_meta( $this->object_type, $value, $item_id );
256
+				return $this->_update_meta($this->object_type, $value, $item_id);
257 257
 
258 258
 			default :
259 259
 
@@ -267,7 +267,7 @@  discard block
 block discarded – undo
267 267
 				 * @param int                 $item_id Item ID.
268 268
 				 * @param WP_Fields_API_Field $this    WP_Fields_API_Field instance.
269 269
 				 */
270
-				do_action( "fields_update_{$this->object_type}", $value, $item_id, $this );
270
+				do_action("fields_update_{$this->object_type}", $value, $item_id, $this);
271 271
 		}
272 272
 
273 273
 		return null;
@@ -281,22 +281,22 @@  discard block
 block discarded – undo
281 281
 	 *
282 282
 	 * @return null
283 283
 	 */
284
-	protected function _update_theme_mod( $value ) {
284
+	protected function _update_theme_mod($value) {
285 285
 
286
-		if ( is_null( $value ) ) {
287
-			remove_theme_mod( $this->id_data['base'] );
286
+		if (is_null($value)) {
287
+			remove_theme_mod($this->id_data['base']);
288 288
 		}
289 289
 
290 290
 		// Handle non-array theme mod.
291
-		if ( empty( $this->id_data['keys'] ) ) {
292
-			set_theme_mod( $this->id_data['base'], $value );
291
+		if (empty($this->id_data['keys'])) {
292
+			set_theme_mod($this->id_data['base'], $value);
293 293
 		} else {
294 294
 			// Handle array-based theme mod.
295
-			$mods = get_theme_mod( $this->id_data['base'] );
296
-			$mods = $this->multidimensional_replace( $mods, $this->id_data['keys'], $value );
295
+			$mods = get_theme_mod($this->id_data['base']);
296
+			$mods = $this->multidimensional_replace($mods, $this->id_data['keys'], $value);
297 297
 
298
-			if ( isset( $mods ) ) {
299
-				set_theme_mod( $this->id_data['base'], $mods );
298
+			if (isset($mods)) {
299
+				set_theme_mod($this->id_data['base'], $mods);
300 300
 			}
301 301
 		}
302 302
 
@@ -311,23 +311,23 @@  discard block
 block discarded – undo
311 311
 	 *
312 312
 	 * @return bool|null The result of saving the value.
313 313
 	 */
314
-	protected function _update_option( $value ) {
314
+	protected function _update_option($value) {
315 315
 
316
-		if ( is_null( $value ) ) {
317
-			delete_option( $this->id_data['base'] );
316
+		if (is_null($value)) {
317
+			delete_option($this->id_data['base']);
318 318
 		}
319 319
 
320 320
 		// Handle non-array option.
321
-		if ( empty( $this->id_data['keys'] ) ) {
322
-			return update_option( $this->id_data['base'], $value );
321
+		if (empty($this->id_data['keys'])) {
322
+			return update_option($this->id_data['base'], $value);
323 323
 		}
324 324
 
325 325
 		// Handle array-based options.
326
-		$options = get_option( $this->id_data['base'] );
327
-		$options = $this->multidimensional_replace( $options, $this->id_data['keys'], $value );
326
+		$options = get_option($this->id_data['base']);
327
+		$options = $this->multidimensional_replace($options, $this->id_data['keys'], $value);
328 328
 
329
-		if ( isset( $options ) ) {
330
-			return update_option( $this->id_data['base'], $options );
329
+		if (isset($options)) {
330
+			return update_option($this->id_data['base'], $options);
331 331
 		}
332 332
 
333 333
 		return null;
@@ -343,23 +343,23 @@  discard block
 block discarded – undo
343 343
 	 *
344 344
 	 * @return bool|null The result of saving the value.
345 345
 	 */
346
-	protected function _update_meta( $meta_type, $value, $item_id = 0 ) {
346
+	protected function _update_meta($meta_type, $value, $item_id = 0) {
347 347
 
348
-		if ( is_null( $value ) ) {
349
-			delete_metadata( $meta_type, $item_id, $this->id_data['base'] );
348
+		if (is_null($value)) {
349
+			delete_metadata($meta_type, $item_id, $this->id_data['base']);
350 350
 		}
351 351
 
352 352
 		// Handle non-array option.
353
-		if ( empty( $this->id_data['keys'] ) ) {
354
-			return update_metadata( $meta_type, $item_id, $this->id_data['base'], $value );
353
+		if (empty($this->id_data['keys'])) {
354
+			return update_metadata($meta_type, $item_id, $this->id_data['base'], $value);
355 355
 		}
356 356
 
357 357
 		// Handle array-based keys.
358
-		$keys = get_metadata( $meta_type, 0, $this->id_data['base'] );
359
-		$keys = $this->multidimensional_replace( $keys, $this->id_data['keys'], $value );
358
+		$keys = get_metadata($meta_type, 0, $this->id_data['base']);
359
+		$keys = $this->multidimensional_replace($keys, $this->id_data['keys'], $value);
360 360
 
361
-		if ( isset( $keys ) ) {
362
-			return update_metadata( $meta_type, $item_id, $this->id_data['base'], $keys );
361
+		if (isset($keys)) {
362
+			return update_metadata($meta_type, $item_id, $this->id_data['base'], $keys);
363 363
 		}
364 364
 
365 365
 		return null;
@@ -377,24 +377,24 @@  discard block
 block discarded – undo
377 377
 
378 378
 		$item_id = func_get_arg(0);
379 379
 
380
-		switch ( $this->object_type ) {
381
-			case is_callable( $this->value_callback ) :
382
-				$value = call_user_func( $this->value_callback, $item_id, $this );
383
-				$value = $this->multidimensional_get( $value, $this->id_data['keys'], $this->default );
380
+		switch ($this->object_type) {
381
+			case is_callable($this->value_callback) :
382
+				$value = call_user_func($this->value_callback, $item_id, $this);
383
+				$value = $this->multidimensional_get($value, $this->id_data['keys'], $this->default);
384 384
 
385 385
 				break;
386 386
 			case 'post' :
387 387
 			case 'term' :
388 388
 			case 'user' :
389 389
 			case 'comment' :
390
-				$value = $this->get_object_value( $item_id );
391
-				$value = $this->multidimensional_get( $value, $this->id_data['keys'], $this->default );
390
+				$value = $this->get_object_value($item_id);
391
+				$value = $this->multidimensional_get($value, $this->id_data['keys'], $this->default);
392 392
 				break;
393 393
 
394 394
 			case 'customizer' :
395 395
 			case 'settings' :
396 396
 				$value = $this->get_option_value();
397
-				$value = $this->multidimensional_get( $value, $this->id_data['keys'], $this->default );
397
+				$value = $this->multidimensional_get($value, $this->id_data['keys'], $this->default);
398 398
 				break;
399 399
 
400 400
 			default :
@@ -410,7 +410,7 @@  discard block
 block discarded – undo
410 410
 				 * @param mixed $default The field default value. Default empty.
411 411
 				 * @param int   $item_id (optional) The Item ID.
412 412
 				 */
413
-				$value = apply_filters( 'fields_value_' . $this->object_type . '_' . $this->object_name . '_' . $this->id_data['base'], $this->default, $item_id );
413
+				$value = apply_filters('fields_value_'.$this->object_type.'_'.$this->object_name.'_'.$this->id_data['base'], $this->default, $item_id);
414 414
 				break;
415 415
 		}
416 416
 
@@ -425,39 +425,39 @@  discard block
 block discarded – undo
425 425
 	 *
426 426
 	 * @return mixed|null
427 427
 	 */
428
-	public function get_object_value( $item_id ) {
428
+	public function get_object_value($item_id) {
429 429
 
430 430
 		$value = null;
431 431
 		$object = null;
432 432
 
433 433
 		$field_key = $this->id_data['base'];
434 434
 
435
-		switch ( $this->object_type ) {
435
+		switch ($this->object_type) {
436 436
 			case 'post' :
437
-				$object = get_post( $item_id );
437
+				$object = get_post($item_id);
438 438
 				break;
439 439
 
440 440
 			case 'term' :
441
-				$object = get_term( $item_id );
441
+				$object = get_term($item_id);
442 442
 				break;
443 443
 
444 444
 			case 'user' :
445
-				$object = get_userdata( $item_id );
445
+				$object = get_userdata($item_id);
446 446
 				break;
447 447
 
448 448
 			case 'comment' :
449
-				$object = get_comment( $item_id );
449
+				$object = get_comment($item_id);
450 450
 				break;
451 451
 		}
452 452
 
453
-		if ( $object && ! is_wp_error( $object ) && isset( $object->{$field_key} ) ) {
453
+		if ($object && ! is_wp_error($object) && isset($object->{$field_key} )) {
454 454
 			// Get value from object
455 455
 			$value = $object->{$field_key};
456 456
 		} else {
457 457
 			// Get value from meta
458
-			$value = get_metadata( $this->object_type, $item_id, $field_key );
458
+			$value = get_metadata($this->object_type, $item_id, $field_key);
459 459
 
460
-			if ( array() === $value ) {
460
+			if (array() === $value) {
461 461
 				$value = $this->default;
462 462
 			}
463 463
 		}
@@ -476,7 +476,7 @@  discard block
 block discarded – undo
476 476
 		$function = '';
477 477
 		$value = null;
478 478
 
479
-		switch ( $this->object_type ) {
479
+		switch ($this->object_type) {
480 480
 			case 'customizer' :
481 481
 				$function = 'get_theme_mod';
482 482
 				break;
@@ -486,14 +486,14 @@  discard block
 block discarded – undo
486 486
 				break;
487 487
 		}
488 488
 
489
-		if ( is_callable( $function ) ) {
489
+		if (is_callable($function)) {
490 490
 			// Handle non-array value
491
-			if ( empty( $this->id_data['keys'] ) ) {
492
-				return $function( $this->id_data['base'], $this->default );
491
+			if (empty($this->id_data['keys'])) {
492
+				return $function($this->id_data['base'], $this->default);
493 493
 			}
494 494
 
495 495
 			// Handle array-based value
496
-			$value = $function( $this->id_data['base'] );
496
+			$value = $function($this->id_data['base']);
497 497
 		}
498 498
 
499 499
 		return $value;
@@ -517,10 +517,10 @@  discard block
 block discarded – undo
517 517
 		 * @param mixed                $value The field value.
518 518
 		 * @param WP_Fields_API_Field $this  {@see WP_Fields_API_Field} instance.
519 519
 		 */
520
-		$value = apply_filters( "fields_sanitize_js_{$this->object_type}_{$this->object_name}_{$this->id}", $value, $this );
520
+		$value = apply_filters("fields_sanitize_js_{$this->object_type}_{$this->object_name}_{$this->id}", $value, $this);
521 521
 
522
-		if ( is_string( $value ) ) {
523
-			return html_entity_decode( $value, ENT_QUOTES, 'UTF-8' );
522
+		if (is_string($value)) {
523
+			return html_entity_decode($value, ENT_QUOTES, 'UTF-8');
524 524
 		}
525 525
 
526 526
 		return $value;
@@ -534,18 +534,18 @@  discard block
 block discarded – undo
534 534
 	 */
535 535
 	public function check_capabilities() {
536 536
 
537
-		if ( $this->capability && ! call_user_func_array( 'current_user_can', (array) $this->capability ) ) {
537
+		if ($this->capability && ! call_user_func_array('current_user_can', (array) $this->capability)) {
538 538
 			return false;
539 539
 		}
540 540
 
541
-		if ( $this->theme_supports && ! call_user_func_array( 'current_theme_supports', (array) $this->theme_supports ) ) {
541
+		if ($this->theme_supports && ! call_user_func_array('current_theme_supports', (array) $this->theme_supports)) {
542 542
 			return false;
543 543
 		}
544 544
 
545 545
 		$access = true;
546 546
 
547
-		if ( is_callable( $this->capabilities_callback ) ) {
548
-			$access = call_user_func( $this->capabilities_callback, $this );
547
+		if (is_callable($this->capabilities_callback)) {
548
+			$access = call_user_func($this->capabilities_callback, $this);
549 549
 		}
550 550
 
551 551
 		return $access;
@@ -561,42 +561,42 @@  discard block
 block discarded – undo
561 561
 	 *
562 562
 	 * @return null|array Keys are 'root', 'node', and 'key'.
563 563
 	 */
564
-	final protected function multidimensional( &$root, $keys, $create = false ) {
564
+	final protected function multidimensional(&$root, $keys, $create = false) {
565 565
 
566
-		if ( $create && empty( $root ) ) {
566
+		if ($create && empty($root)) {
567 567
 			$root = array();
568 568
 		}
569 569
 
570
-		if ( ! isset( $root ) || empty( $keys ) ) {
570
+		if ( ! isset($root) || empty($keys)) {
571 571
 			return null;
572 572
 		}
573 573
 
574
-		$last = array_pop( $keys );
574
+		$last = array_pop($keys);
575 575
 		$node = &$root;
576 576
 
577
-		foreach ( $keys as $key ) {
578
-			if ( $create && ! isset( $node[ $key ] ) ) {
579
-				$node[ $key ] = array();
577
+		foreach ($keys as $key) {
578
+			if ($create && ! isset($node[$key])) {
579
+				$node[$key] = array();
580 580
 			}
581 581
 
582
-			if ( ! is_array( $node ) || ! isset( $node[ $key ] ) ) {
582
+			if ( ! is_array($node) || ! isset($node[$key])) {
583 583
 				return null;
584 584
 			}
585 585
 
586
-			$node = &$node[ $key ];
586
+			$node = &$node[$key];
587 587
 		}
588 588
 
589
-		if ( $create ) {
590
-			if ( ! is_array( $node ) ) {
589
+		if ($create) {
590
+			if ( ! is_array($node)) {
591 591
 				// account for an array overriding a string or object value
592 592
 				$node = array();
593 593
 			}
594
-			if ( ! isset( $node[ $last ] ) ) {
595
-				$node[ $last ] = array();
594
+			if ( ! isset($node[$last])) {
595
+				$node[$last] = array();
596 596
 			}
597 597
 		}
598 598
 
599
-		if ( ! isset( $node[ $last ] ) ) {
599
+		if ( ! isset($node[$last])) {
600 600
 			return null;
601 601
 		}
602 602
 
@@ -617,19 +617,19 @@  discard block
 block discarded – undo
617 617
 	 *
618 618
 	 * @return
619 619
 	 */
620
-	final protected function multidimensional_replace( $root, $keys, $value ) {
620
+	final protected function multidimensional_replace($root, $keys, $value) {
621 621
 
622
-		if ( ! isset( $value ) ) {
622
+		if ( ! isset($value)) {
623 623
 			return $root;
624
-		} elseif ( empty( $keys ) ) {
624
+		} elseif (empty($keys)) {
625 625
 			// If there are no keys, we're replacing the root.
626 626
 			return $value;
627 627
 		}
628 628
 
629
-		$result = $this->multidimensional( $root, $keys, true );
629
+		$result = $this->multidimensional($root, $keys, true);
630 630
 
631
-		if ( isset( $result ) ) {
632
-			$result['node'][ $result['key'] ] = $value;
631
+		if (isset($result)) {
632
+			$result['node'][$result['key']] = $value;
633 633
 		}
634 634
 
635 635
 		return $root;
@@ -645,18 +645,18 @@  discard block
 block discarded – undo
645 645
 	 *
646 646
 	 * @return mixed The requested value or the default value.
647 647
 	 */
648
-	final protected function multidimensional_get( $root, $keys, $default = null ) {
648
+	final protected function multidimensional_get($root, $keys, $default = null) {
649 649
 
650 650
 		// If there are no keys, test the root.
651
-		if ( empty( $keys ) ) {
652
-			if ( isset( $root ) ) {
651
+		if (empty($keys)) {
652
+			if (isset($root)) {
653 653
 				return $root;
654 654
 			}
655 655
 		} else {
656
-			$result = $this->multidimensional( $root, $keys );
656
+			$result = $this->multidimensional($root, $keys);
657 657
 
658
-			if ( isset( $result ) ) {
659
-				return $result['node'][ $result['key'] ];
658
+			if (isset($result)) {
659
+				return $result['node'][$result['key']];
660 660
 			}
661 661
 		}
662 662
 
@@ -672,11 +672,11 @@  discard block
 block discarded – undo
672 672
 	 *
673 673
 	 * @return bool True if value is set, false if not.
674 674
 	 */
675
-	final protected function multidimensional_isset( $root, $keys ) {
675
+	final protected function multidimensional_isset($root, $keys) {
676 676
 
677
-		$result = $this->multidimensional_get( $root, $keys );
677
+		$result = $this->multidimensional_get($root, $keys);
678 678
 
679
-		return isset( $result );
679
+		return isset($result);
680 680
 
681 681
 	}
682 682
 }
@@ -698,7 +698,7 @@  discard block
 block discarded – undo
698 698
 	 *
699 699
 	 * @return mixed The result of saving the value.
700 700
 	 */
701
-	public function update( $value ) {
701
+	public function update($value) {
702 702
 
703 703
 		return null;
704 704
 
Please login to merge, or discard this patch.