Completed
Pull Request — 2.x (#4569)
by Scott Kingsley
04:42
created

PodsField::format()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 5
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Pods Field class for common type-specific methods.
5
 *
6
 * @package Pods
7
 */
8
class PodsField {
9
10
	/**
11
	 * Whether this field is running under 1.x deprecated forms
12
	 *
13
	 * @var bool
14
	 * @since 2.0
15
	 */
16
	public static $deprecated = false;
17
18
	/**
19
	 * Field Type Identifier
20
	 *
21
	 * @var string
22
	 * @since 2.0
23
	 */
24
	public static $type = 'text';
25
26
	/**
27
	 * Field Type Label
28
	 *
29
	 * @var string
30
	 * @since 2.0
31
	 */
32
	public static $label = 'Unknown';
33
34
	/**
35
	 * Field Type Preparation
36
	 *
37
	 * @var string
38
	 * @since 2.0
39
	 */
40
	public static $prepare = '%s';
41
42
	/**
43
	 * Pod Types supported on (true for all, false for none, or give array of specific types supported)
44
	 *
45
	 * @var array|bool
46
	 * @since 2.1
47
	 */
48
	public static $pod_types = true;
49
50
	/**
51
	 * API caching for fields that need it during validate/save
52
	 *
53
	 * @var \PodsAPI
54
	 * @since 2.3
55
	 */
56
	private static $api = false;
0 ignored issues
show
Unused Code introduced by
The property $api is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
57
58
	/**
59
	 * Do things like register/enqueue scripts and stylesheets
60
	 *
61
	 * @since 2.0
62
	 */
63
	public function __construct() {
64
65
		// Subclasses utilize this method if needed.
66
	}
67
68
	/**
69
	 * Add admin_init actions.
70
	 *
71
	 * @since 2.3
72
	 */
73
	public function admin_init() {
74
75
		// Add admin actions here.
76
	}
77
78
	/**
79
	 * Add options and set defaults for field type, shows in admin area
80
	 *
81
	 * @return array $options
82
	 *
83
	 * @since 2.0
84
	 * @see   PodsField::ui_options
85
	 */
86
	public function options() {
87
88
		$options = array(/*
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
89
            'option_name' => array(
90
                'label' => 'Option Label',
91
                'depends-on' => array( 'another_option' => 'specific-value' ),
92
                'default' => 'default-value',
93
                'type' => 'field_type',
94
                'data' => array(
95
                    'value1' => 'Label 1',
96
97
                    // Group your options together
98
                    'Option Group' => array(
99
                        'gvalue1' => 'Option Label 1',
100
                        'gvalue2' => 'Option Label 2'
101
                    ),
102
103
                    // below is only if the option_name above is the "{$fieldtype}_format_type"
104
                    'value2' => array(
105
                        'label' => 'Label 2',
106
                        'regex' => '[a-zA-Z]' // Uses JS regex validation for the value saved if this option selected
107
                    )
108
                ),
109
110
                // below is only for a boolean group
111
                'group' => array(
112
                    'option_boolean1' => array(
113
                        'label' => 'Option boolean 1?',
114
                        'default' => 1,
115
                        'type' => 'boolean'
116
                    ),
117
                    'option_boolean2' => array(
118
                        'label' => 'Option boolean 2?',
119
                        'default' => 0,
120
                        'type' => 'boolean'
121
                    )
122
                )
123
            )
124
            */
125
		);
126
127
		return $options;
128
129
	}
130
131
	/**
132
	 * Options for the Admin area, defaults to $this->options()
133
	 *
134
	 * @return array $options
135
	 *
136
	 * @since 2.0
137
	 * @see   PodsField::options
138
	 */
139
	public function ui_options() {
140
141
		return $this->options();
142
143
	}
144
145
	/**
146
	 * Define the current field's schema for DB table storage
147
	 *
148
	 * @param array|null $options
149
	 *
150
	 * @return string
151
	 *
152
	 * @since 2.0
153
	 */
154
	public function schema( $options = null ) {
155
156
		$schema = 'VARCHAR(255)';
157
158
		return $schema;
159
160
	}
161
162
	/**
163
	 * Define the current field's preparation for sprintf
164
	 *
165
	 * @param array|null $options
166
	 *
167
	 * @return string
168
	 *
169
	 * @since 2.0
170
	 */
171
	public function prepare( $options = null ) {
172
173
		$format = self::$prepare;
174
175
		return $format;
176
177
	}
178
179
	/**
180
	 * Check if the field is empty.
181
	 *
182
	 * @param mixed $value
183
	 *
184
	 * @return bool
185
	 *
186
	 * @since 2.7
187
	 */
188
	public function is_empty( $value ) {
189
190
		$is_empty = false;
191
192
		if ( is_string( $value ) ) {
193
			$value = trim( $value );
194
		}
195
196
		if ( empty( $value ) ) {
197
			$is_empty = true;
198
		}
199
200
		return $is_empty;
201
202
	}
203
204
	/**
205
	 * Change the value of the field
206
	 *
207
	 * @param mixed|null  $value
208
	 * @param string|null $name
209
	 * @param array|null  $options
210
	 * @param array|null  $pod
211
	 * @param int|null    $id
212
	 *
213
	 * @return mixed|null|string
214
	 *
215
	 * @since 2.3
216
	 */
217
	public function value( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
218
219
		return $value;
220
221
	}
222
223
	/**
224
	 * Change the way the value of the field is displayed with Pods::get
225
	 *
226
	 * @param mixed|null  $value
227
	 * @param string|null $name
228
	 * @param array|null  $options
229
	 * @param array|null  $pod
230
	 * @param int|null    $id
231
	 *
232
	 * @return mixed|null|string
233
	 *
234
	 * @since 2.0
235
	 */
236
	public function display( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
237
238
		return $value;
239
240
	}
241
242
	/**
243
	 * Reformat a number to the way the value of the field is displayed.
244
	 *
245
	 * @param mixed  $value
246
	 * @param string $name
247
	 * @param array  $options
248
	 * @param array  $pod
249
	 * @param int    $id
250
	 *
251
	 * @return string|null
252
	 * @since 2.0
253
	 */
254
	public function format( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
255
256
		return $value;
257
258
	}
259
260
	/**
261
	 * Customize output of the form field
262
	 *
263
	 * @param string     $name
264
	 * @param mixed|null $value
265
	 * @param array|null $options
266
	 * @param array|null $pod
267
	 * @param int|null   $id
268
	 *
269
	 * @since 2.0
270
	 */
271
	public function input( $name, $value = null, $options = null, $pod = null, $id = null ) {
272
273
		$options = (array) $options;
274
275
		$form_field_type = PodsForm::$field_type;
276
277
		if ( is_array( $value ) ) {
278
			$value = implode( ' ', $value );
279
		}
280
281
		pods_view( PODS_DIR . 'ui/fields/text.php', compact( array_keys( get_defined_vars() ) ) );
282
283
		return;
284
285
		// @todo Eventually use this code
286
		$options = (array) $options;
0 ignored issues
show
Unused Code introduced by
$options = (array) $options; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
287
288
		$type = pods_v( 'type', $options, static::$type );
289
290
		$args = compact( array_keys( get_defined_vars() ) );
291
		$args = (object) $args;
292
293
		$this->render_input_script( $args );
294
295
	}
296
297
	/**
298
	 * Render input script for Pods DFV
299
	 *
300
	 * @param array|object $args    {
301
	 *                              Field information arguments.
302
	 *
303
	 * @type string        $name    Field name
304
	 * @type string        $type    Field type
305
	 * @type array         $options Field options
306
	 * @type mixed         $value   Current value
307
	 * @type array         $pod     Pod information
308
	 * @type int|string    $id      Current item ID
309
	 * }
310
	 */
311
	public function render_input_script( $args ) {
312
313
		if ( is_array( $args ) ) {
314
			$args = (object) $args;
315
		}
316
317
		$script_content = json_encode( $this->build_dfv_field_data( $args ), JSON_HEX_TAG );
0 ignored issues
show
Unused Code introduced by
The call to json_encode() has too many arguments starting with JSON_HEX_TAG.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
318
		?>
319
		<div class="pods-form-ui-field pods-dfv-field">
320
			<script type="application/json" class="pods-dfv-field-data"><?php echo $script_content; ?></script>
321
		</div>
322
		<?php
323
324
	}
325
326
	/**
327
	 * Build field data for Pods DFV
328
	 *
329
	 * @param object $args            {
330
	 *                                Field information arguments.
331
	 *
332
	 * @type string     $name            Field name
333
	 * @type string     $type            Pod field type
334
	 * @type string     $form_field_type HTML field type
335
	 * @type array      $options         Field options
336
	 * @type mixed      $value           Current value
337
	 * @type array      $pod             Pod information
338
	 * @type int|string $id              Current item ID
339
	 * }
340
	 *
341
	 * @return array
342
	 */
343
	public function build_dfv_field_data( $args ) {
344
345
		// Handle DFV options.
346
		$args->options = $this->build_dfv_field_options( $args->options, $args );
347
348
		// Handle DFV attributes.
349
		$attributes = PodsForm::merge_attributes( array(), $args->name, $args->type, $args->options );
350
		$attributes = $this->build_dfv_field_attributes( $attributes, $args );
351
		$attributes = array_map( 'esc_attr', $attributes );
352
353
		// Build DFV field data.
354
		$data = array(
355
			'htmlAttr'      => array(
356
				'id'         => $attributes['id'],
357
				'class'      => $attributes['class'],
358
				'name'       => $attributes['name'],
359
				'name_clean' => $attributes['data-name-clean'],
360
			),
361
			'fieldType'     => $args->type,
362
			'fieldItemData' => $this->build_dfv_field_item_data( $args ),
363
			'fieldConfig'   => $this->build_dfv_field_config( $args ),
364
		);
365
366
		/**
367
		 * Filter Pods DFV field data to further customize functionality.
368
		 *
369
		 * @since 2.7
370
		 *
371
		 * @param array     $data            DFV field data
372
		 * @param object    $args            {
373
		 *                                   Field information arguments.
374
		 *
375
		 * @type string     $name            Field name
376
		 * @type string     $type            Pod field type
377
		 * @type string     $form_field_type HTML field type
378
		 * @type array      $options         Field options
379
		 * @type mixed      $value           Current value
380
		 * @type array      $pod             Pod information
381
		 * @type int|string $id              Current item ID
382
		 * }
383
		 *
384
		 * @param array     $attributes      HTML attributes
385
		 */
386
		$data = apply_filters( 'pods_field_dfv_data', $data, $args, $attributes );
387
388
		return $data;
389
390
	}
391
392
	/**
393
	 * Build field options and handle any validation/customization for Pods DFV
394
	 *
395
	 * @param array  $options
396
	 * @param object $args    {
397
	 *                        Field information arguments.
398
	 *
399
	 * @type string     $name    Field name
400
	 * @type string     $type    Field type
401
	 * @type array      $options Field options
402
	 * @type mixed      $value   Current value
403
	 * @type array      $pod     Pod information
404
	 * @type int|string $id      Current item ID
405
	 * }
406
	 *
407
	 * @return array
408
	 */
409
	public function build_dfv_field_options( $options, $args ) {
410
411
		return $options;
412
413
	}
414
415
	/**
416
	 * Build field HTML attributes for Pods DFV.
417
	 *
418
	 * @param array  $attributes Default HTML attributes from field and PodsForm::merge_attributes.
419
	 * @param object $args       {
420
	 *                           Field information arguments.
421
	 *
422
	 * @type string     $name       Field name
423
	 * @type string     $type       Field type
424
	 * @type array      $options    Field options
425
	 * @type mixed      $value      Current value
426
	 * @type array      $pod        Pod information
427
	 * @type int|string $id         Current item ID
428
	 * }
429
	 *
430
	 * @return array
431
	 */
432
	public function build_dfv_field_attributes( $attributes, $args ) {
433
434
		return $attributes;
435
436
	}
437
438
	/**
439
	 * Build field config for Pods DFV using field options.
440
	 *
441
	 * This is for customizing the options and adding output-specific config values.
442
	 *
443
	 * @param object $args    {
444
	 *                        Field information arguments.
445
	 *
446
	 * @type string     $name    Field name
447
	 * @type string     $type    Field type
448
	 * @type array      $options Field options
449
	 * @type mixed      $value   Current value
450
	 * @type array      $pod     Pod information
451
	 * @type int|string $id      Current item ID
452
	 * }
453
	 *
454
	 * @return array
455
	 */
456
	public function build_dfv_field_config( $args ) {
457
458
		$config = $args->options;
459
460
		unset( $config['data'] );
461
462
		$config['item_id'] = (int) $args->id;
463
464
		return $config;
465
466
	}
467
468
	/**
469
	 * Build array of item data for Pods DFV
470
	 *
471
	 * @param object $args    {
472
	 *                        Field information arguments.
473
	 *
474
	 * @type string     $name    Field name
475
	 * @type string     $type    Field type
476
	 * @type array      $options Field options
477
	 * @type mixed      $value   Current value
478
	 * @type array      $pod     Pod information
479
	 * @type int|string $id      Current item ID
480
	 * }
481
	 *
482
	 * @return array
483
	 */
484
	public function build_dfv_field_item_data( $args ) {
485
486
		$data = array();
487
488
		if ( ! empty( $args->options['data'] ) && is_array( $args->options['data'] ) ) {
489
			$data = $args->options['data'];
490
		}
491
492
		return $data;
493
494
	}
495
496
	/**
497
	 * Get the data from the field
498
	 *
499
	 * @param string            $name  The name of the field
500
	 * @param string|array|null $value The value of the field
501
	 * @param array|null        $options
502
	 * @param array|null        $pod
503
	 * @param int|null          $id
504
	 * @param boolean           $in_form
505
	 *
506
	 * @return array Array of possible field data
507
	 *
508
	 * @since 2.0
509
	 */
510
	public function data( $name, $value = null, $options = null, $pod = null, $id = null, $in_form = true ) {
511
512
		return (array) $value;
513
514
	}
515
516
	/**
517
	 * Build regex necessary for JS validation
518
	 *
519
	 * @param mixed|null  $value
520
	 * @param string|null $name
521
	 * @param array|null  $options
522
	 * @param string|null $pod
523
	 * @param int|null    $id
524
	 *
525
	 * @return bool
526
	 *
527
	 * @since 2.0
528
	 */
529
	public function regex( $value = null, $name = null, $options = null, $pod = null, $id = null ) {
530
531
		return false;
532
533
	}
534
535
	/**
536
	 * Validate a value before it's saved
537
	 *
538
	 * @param mixed       $value
539
	 * @param string|null $name
540
	 * @param array|null  $options
541
	 * @param array|null  $fields
542
	 * @param array|null  $pod
543
	 * @param int|null    $id
544
	 * @param array|null  $params
545
	 *
546
	 * @return bool
547
	 *
548
	 * @since 2.0
549
	 */
550
	public function validate( $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) {
551
552
		return true;
553
554
	}
555
556
	/**
557
	 * Change the value or perform actions after validation but before saving to the DB
558
	 *
559
	 * @param mixed       $value
560
	 * @param int|null    $id
561
	 * @param string|null $name
562
	 * @param array|null  $options
563
	 * @param array|null  $fields
564
	 * @param array|null  $pod
565
	 * @param object|null $params
566
	 *
567
	 * @return mixed
568
	 *
569
	 * @since 2.0
570
	 */
571
	public function pre_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
572
573
		return $value;
574
575
	}
576
577
	/**
578
	 * Save the value to the DB
579
	 *
580
	 * @param mixed       $value
581
	 * @param int|null    $id
582
	 * @param string|null $name
583
	 * @param array|null  $options
584
	 * @param array|null  $fields
585
	 * @param array|null  $pod
586
	 * @param object|null $params
587
	 *
588
	 * @return bool|null Whether the value was saved, returning null means no save needed to occur
589
	 *
590
	 * @since 2.3
591
	 */
592
	public function save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
593
594
		return null;
595
596
	}
597
598
	/**
599
	 * Perform actions after saving to the DB
600
	 *
601
	 * @param mixed       $value
602
	 * @param int|null    $id
603
	 * @param string|null $name
604
	 * @param array|null  $options
605
	 * @param array|null  $fields
606
	 * @param array|null  $pod
607
	 * @param object|null $params
608
	 *
609
	 * @since 2.0
610
	 */
611
	public function post_save( $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) {
612
613
		// Subclasses utilize this method if needed.
614
	}
615
616
	/**
617
	 * Perform actions before deleting from the DB
618
	 *
619
	 * @param int|null    $id
620
	 * @param string|null $name
621
	 * @param array|null  $options
622
	 * @param string|null $pod
623
	 *
624
	 * @since 2.0
625
	 */
626
	public function pre_delete( $id = null, $name = null, $options = null, $pod = null ) {
627
628
		// Subclasses utilize this method if needed.
629
	}
630
631
	/**
632
	 * Delete the value from the DB
633
	 *
634
	 * @param int|null    $id
635
	 * @param string|null $name
636
	 * @param array|null  $options
637
	 * @param array|null  $pod
638
	 *
639
	 * @since 2.3
640
	 */
641
	public function delete( $id = null, $name = null, $options = null, $pod = null ) {
642
643
		// Subclasses utilize this method if needed.
644
	}
645
646
	/**
647
	 * Perform actions after deleting from the DB
648
	 *
649
	 * @param int|null    $id
650
	 * @param string|null $name
651
	 * @param array|null  $options
652
	 * @param array|null  $pod
653
	 *
654
	 * @since 2.0
655
	 */
656
	public function post_delete( $id = null, $name = null, $options = null, $pod = null ) {
657
658
		// Subclasses utilize this method if needed.
659
	}
660
661
	/**
662
	 * Customize the Pods UI manage table column output
663
	 *
664
	 * @param int         $id
665
	 * @param mixed       $value
666
	 * @param string|null $name
667
	 * @param array|null  $options
668
	 * @param array|null  $fields
669
	 * @param array|null  $pod
670
	 *
671
	 * @return string Value to be shown in the UI
672
	 *
673
	 * @since 2.0
674
	 */
675
	public function ui( $id, $value, $name = null, $options = null, $fields = null, $pod = null ) {
676
677
		return $value;
678
679
	}
680
681
	/**
682
	 * Placeholder function to allow var_export() use with classes
683
	 *
684
	 * @param array $properties
685
	 *
686
	 * @return object|void
687
	 */
688
	public static function __set_state( $properties ) {
689
690
		return;
691
692
	}
693
694
}
695