Completed
Push — milestone/2_0/react-ui ( feeecc...e46ba4 )
by
unknown
07:22
created

Field::get_value_set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Carbon_Fields\Field;
4
5
use Carbon_Fields\App;
6
use Carbon_Fields\Datastore\Datastore_Interface;
7
use Carbon_Fields\Datastore\Datastore_Holder_Interface;
8
use Carbon_Fields\Value_Set\Value_Set;
9
use Carbon_Fields\Helper\Helper;
10
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
11
12
/**
13
 * Base field class.
14
 * Defines the key container methods and their default implementations.
15
 * Implements factory design pattern.
16
 **/
17
class Field implements Datastore_Holder_Interface {
18
	
19
	/**
20
	 * Globally unique field identificator. Generated randomly
21
	 *
22
	 * @var string
23
	 */
24
	protected $id;
25
26
	/**
27
	 * Stores the initial <kbd>$type</kbd> variable passed to the <code>factory()</code> method
28
	 *
29
	 * @see factory
30
	 * @var string
31
	 */
32
	public $type;
33
34
	/**
35
	 * Array of ancestor field names
36
	 *
37
	 * @var array
38
	 **/
39
	protected $hierarchy = array();
40
41
	/**
42
	 * Array of complex entry ids
43
	 *
44
	 * @var array
45
	 **/
46
	protected $hierarchy_index = array();
47
48
	/**
49
	 * Field value
50
	 *
51
	 * @var Value_Set
52
	 */
53
	protected $value_set;
54
55
	/**
56
	 * Default field value
57
	 *
58
	 * @var mixed
59
	 */
60
	protected $default_value = '';
61
62
	/**
63
	 * Sanitized field name used as input name attribute during field render
64
	 *
65
	 * @see factory()
66
	 * @see set_name()
67
	 * @var string
68
	 */
69
	protected $name;
70
71
	/**
72
	 * Field name prefix
73
	 *
74
	 * @see set_name()
75
	 * @var string
76
	 */
77
	protected $name_prefix = '_';
78
79
	/**
80
	 * The base field name which is used in the container.
81
	 *
82
	 * @see set_base_name()
83
	 * @var string
84
	 */
85
	protected $base_name;
86
87
	/**
88
	 * Field name used as label during field render
89
	 *
90
	 * @see factory()
91
	 * @see set_label()
92
	 * @var string
93
	 */
94
	protected $label;
95
96
	/**
97
	 * Additional text containing information and guidance for the user
98
	 *
99
	 * @see help_text()
100
	 * @var string
101
	 */
102
	protected $help_text;
103
104
	/**
105
	 * Field DataStore instance to which save, load and delete calls are delegated
106
	 *
107
	 * @see set_datastore()
108
	 * @see get_datastore()
109
	 * @var Datastore_Interface
110
	 */
111
	protected $datastore;
112
113
	/**
114
	 * Flag whether the datastore is the default one or replaced with a custom one
115
	 *
116
	 * @see set_datastore()
117
	 * @see get_datastore()
118
	 * @var boolean
119
	 */
120
	protected $has_default_datastore = true;
121
122
	/**
123
	 * The type of the container this field is in
124
	 *
125
	 * @see get_context()
126
	 * @var string
127
	 */
128
	protected $context;
129
130
	/**
131
	 * Whether or not this value should be auto loaded. Applicable to theme options only.
132
	 *
133
	 * @see set_autoload()
134
	 * @var bool
135
	 **/
136
	protected $autoload = false;
137
138
	/**
139
	 * Whether or not this field will be initialized when the field is in the viewport (visible).
140
	 *
141
	 * @see set_lazyload()
142
	 * @var bool
143
	 **/
144
	protected $lazyload = false;
145
146
	/**
147
	 * The width of the field.
148
	 *
149
	 * @see set_width()
150
	 * @var int
151
	 **/
152
	protected $width = 0;
153
154
	/**
155
	 * Custom CSS classes.
156
	 *
157
	 * @see add_class()
158
	 * @var array
159
	 **/
160
	protected $classes = array();
161
162
	/**
163
	 * Whether or not this field is required.
164
	 *
165
	 * @see set_required()
166
	 * @var bool
167
	 **/
168
	protected $required = false;
169
170
	/**
171
	 * Stores the field conditional logic rules.
172
	 *
173
	 * @var array
174
	 **/
175
	protected $conditional_logic = array();
176
177
	/**
178
	 * Whether the field should be included in the response of the requests to the REST API
179
	 *
180
	 * @see  set_visible_in_rest_api
181
	 * @see  get_visible_in_rest_api
182
	 * @var boolean
183
	 */
184
	protected $visible_in_rest_api = false;
185
186
	/**
187
	 * Clone the Value_Set object as well
188
	 *
189
	 * @var array
190
	 **/
191
	public function __clone() {
192
		$this->set_value_set( clone $this->get_value_set() );
193
	}
194
195
	/**
196
	 * Create a new field of type $type and name $name and label $label.
197
	 *
198
	 * @param string $type
199
	 * @param string $name lower case and underscore-delimited
200
	 * @param string $label (optional) Automatically generated from $name if not present
201
	 * @return Field
202
	 **/
203 14
	public static function factory( $type, $name, $label = null ) {
204 14
		$type = str_replace( ' ', '_', ucwords( str_replace( '_', ' ', $type ) ) );
205
206 14
		$class = __NAMESPACE__ . '\\' . $type . '_Field';
207
208 14 View Code Duplication
		if ( ! class_exists( $class ) ) {
209 4
			Incorrect_Syntax_Exception::raise( 'Unknown field "' . $type . '".' );
210 1
			$class = __NAMESPACE__ . '\\Broken_Field';
211 1
		}
212
213 11
		$field = new $class( $type, $name, $label );
214
215 7
		return $field;
216
	}
217
218
	/**
219
	 * An alias of factory().
220
	 *
221
	 * @see Field::factory()
222
	 * @return Field
223
	 **/
224 14
	public static function make( $type, $name, $label = null ) {
225 14
		return static::factory( $type, $name, $label );
226
	}
227
228
	/**
229
	 * Create a field from a certain type with the specified label.
230
	 * 
231
	 * @param string $type  Field type
232
	 * @param string $name  Field name
233
	 * @param string $label Field label
234
	 */
235 11
	protected function __construct( $type, $name, $label ) {
236 11
		App::verify_boot();
237
		
238 11
		$this->type = $type;
239 11
		$this->set_base_name( $name );
240 11
		$this->set_name( $name );
241 7
		$this->set_label( $label );
242
243
		// Pick random ID
244 7
		$random_string = md5( mt_rand() . $this->get_name() . $this->get_label() );
245 7
		$random_string = substr( $random_string, 0, 5 ); // 5 chars should be enough
246 7
		$this->id = 'carbon-' . $random_string;
247
248 7
		$this->init();
249 7
	}
250
251
	/**
252
	 * Cleans up an object class for usage as HTML class
253
	 *
254
	 * @param string $type
255
	 * @return string
256
	 */
257
	protected function clean_type( $type ) {
258
		$remove = array(
259
			'_',
260
			'\\',
261
			'CarbonFields',
262
			'Field',
263
		);
264
		$clean_class = str_replace( $remove, '', $type );
265
266
		return $clean_class;
267
	}
268
269
	/**
270
	 * Returns the type of the field based on the class.
271
	 * The class is stripped by the "CarbonFields" prefix.
272
	 * Also the "Field" suffix is removed.
273
	 * Then underscores and backslashes are removed.
274
	 *
275
	 * @return string
276
	 */
277
	public function get_type() {
278
		$class = get_class( $this );
279
280
		return $this->clean_type( $class );
281
	}
282
283
	/**
284
	 * Activate the field once the container is attached.
285
	 */
286
	public function activate() {
287
		$this->admin_init();
288
289
		add_action( 'admin_footer', array( get_class(), 'admin_hook_scripts' ), 5 );
290
		add_action( 'admin_footer', array( get_class(), 'admin_hook_styles' ), 5 );
291
		add_action( 'admin_footer', array( get_class( $this ), 'admin_enqueue_scripts' ), 5 );
292
293
		do_action( 'crb_field_activated', $this );
294
	}
295
296
	/**
297
	 * Get array of hierarchy field names
298
	 *
299
	 * @return array
300
	 **/
301
	public function get_hierarchy() {
302
		return $this->hierarchy;
303
	}
304
305
	/**
306
	 * Set array of hierarchy field names
307
	 **/
308
	public function set_hierarchy( $hierarchy ) {
309
		$this->hierarchy = $hierarchy;
310
	}
311
312
	/**
313
	 * Get array of hierarchy indexes
314
	 *
315
	 * @return array
316
	 **/
317
	public function get_hierarchy_index() {
318
		return $this->hierarchy_index;
319
	}
320
321
	/**
322
	 * Set array of hierarchy indexes
323
	 **/
324
	public function set_hierarchy_index( $hierarchy_index ) {
325
		$hierarchy_index = ( ! empty( $hierarchy_index ) ) ? $hierarchy_index : array( 0 );
326
		$this->hierarchy_index = $hierarchy_index;
327
	}
328
329
	/**
330
	 * Return whether the field is a root field and holds a single value
331
	 *
332
	 * @return bool
333
	 **/
334 3
	public function is_simple_root_field() {
335 3
		$hierarchy = $this->get_hierarchy();
336
		return (
337 3
			empty( $hierarchy )
338 3
			&&
339
			(
340 2
				$this->get_value_set()->get_type() === Value_Set::TYPE_SINGLE_VALUE
341 2
				||
342 2
				$this->get_value_set()->get_type() === Value_Set::TYPE_MULTIPLE_PROPERTIES
343 2
			)
344 3
		);
345
	}
346
347
	/**
348
	 * Perform instance initialization
349
	 **/
350
	public function init() {}
351
352
	/**
353
	 * Instance initialization when in the admin area.
354
	 * Called during field boot.
355
	 **/
356
	public function admin_init() {}
357
358
	/**
359
	 * Enqueue admin scripts.
360
	 * Called once per field type.
361
	 **/
362
	public static function admin_enqueue_scripts() {}
363
364
	/**
365
	 * Get value from datastore
366
	 *
367
	 * @param bool $fallback_to_default
368
	 * @return mixed
369
	 **/
370
	protected function get_value_from_datastore( $fallback_to_default = true ) {
371
		$value_tree = $this->get_datastore()->load( $this );
372
		
373
		$value = null;
374
		if ( isset( $value_tree['value_set'] ) ) {
375
			$value = $value_tree['value_set'];
376
		}
377
378
		if ( $value === null && $fallback_to_default ) {
379
			$value = $this->get_default_value();
380
		}
381
382
		return $value;
383
	}
384
385
	/**
386
	 * Load value from datastore
387
	 **/
388 2
	public function load() {
389 2
		$value = $this->get_value_from_datastore();
390 2
		$this->set_value( $value );
391 2
	}
392
393
	/**
394
	 * Save value to storage
395
	 **/
396 5
	public function save() {
397 5
		if ( ! in_array( $this->get_value_set()->get_type(), array( Value_Set::TYPE_SINGLE_VALUE, Value_Set::TYPE_MULTIPLE_PROPERTIES ) ) ) {
398 2
			$this->delete();
399 2
		}
400 5
		return $this->get_datastore()->save( $this );
401
	}
402
403
	/**
404
	 * Delete value from storage
405
	 */
406 1
	public function delete() {
407 1
		$this->get_datastore()->delete( $this );
408 1
	}
409
410
	/**
411
	 * Load the field value from an input array based on it's name
412
	 *
413
	 * @param array $input Array of field names and values.
414
	 **/
415 2
	public function set_value_from_input( $input ) {
416 2
		if ( isset( $input[ $this->get_name() ] ) ) {
417 1
			$this->set_value( $input[ $this->get_name() ] );
418 1
		} else {
419 1
			$this->set_value( array() );
420
		}
421 2
	}
422
423
	/**
424
	 * Return whether the datastore instance is the default one or has been overriden
425
	 *
426
	 * @return boolean
427
	 **/
428
	public function has_default_datastore() {
429
		return $this->has_default_datastore;
430
	}
431
432
	/**
433
	 * Get the DataStore instance
434
	 *
435
	 * @return Datastore_Interface $datastore
436
	 **/
437 1
	public function get_datastore() {
438 1
		return $this->datastore;
439
	}
440
441
	/**
442
	 * Set datastore instance
443
	 *
444
	 * @param Datastore_Interface $datastore
445
	 * @return object $this
446
	 **/
447 1
	public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) {
448 1
		if ( $set_as_default && ! $this->has_default_datastore() ) {
449
			return $this; // datastore has been overriden with a custom one - abort changing to a default one
450
		}
451 1
		$this->datastore = $datastore;
452 1
		$this->has_default_datastore = $set_as_default;
453 1
		return $this;
454
	}
455
456
	/**
457
	 * Return the type of the container this field is in
458
	 *
459
	 * @return string
460
	 **/
461
	public function get_context() {
462
		return $this->context;
463
	}
464
465
	/**
466
	 * Assign the type of the container this field is in
467
	 *
468
	 * @param string
469
	 * @return object $this
470
	 **/
471
	public function set_context( $context ) {
472
		$this->context = $context;
473
		return $this;
474
	}
475
476
	/**
477
	 * Get the Value_Set object
478
	 *
479
	 * @return Value_Set
480
	 **/
481 2
	public function get_value_set() {
482 2
		if ( $this->value_set === null ) {
483 1
			$this->set_value_set( new Value_Set() );
484 1
		}
485 2
		return $this->value_set;
486
	}
487
488
	/**
489
	 * Set the Value_Set object
490
	 *
491
	 * @param Value_Set $value_set
492
	 **/
493 1
	public function set_value_set( $value_set ) {
494 1
		$this->value_set = $value_set;
495 1
	}
496
497
	/**
498
	 * Alias for $this->get_value_set()->get(); with fallback to default value
499
	 *
500
	 * @return mixed
501
	 **/
502 3
	public function get_value() {
503 3
		if ( $this->get_value_set()->get() === null ) {
504 1
			$this->set_value( $this->get_default_value() );
505 1
		}
506 3
		return $this->get_value_set()->get();
507
	}
508
509
	/**
510
	 * Alias for $this->get_value_set()->get_set(); with fallback to default value
511
	 *
512
	 * @return array<array>
513
	 **/
514
	public function get_full_value() {
515
		if ( $this->get_value_set()->get_set() === null ) {
516
			$this->set_value( $this->get_default_value() );
517
		}
518
		return $this->get_value_set()->get_set();
519
	}
520
521
	/**
522
	 * Return a differently formatted value for end-users
523
	 *
524
	 * @return mixed
525
	 **/
526 2
	public function get_formatted_value() {
527 2
		return $this->get_value();
528
	}
529
530
	/**
531
	 * Alias for $this->get_value_set()->set( $value );
532
	 **/
533 1
	public function set_value( $value ) {
534 1
		$this->get_value_set()->set( $value );
535 1
	}
536
537
	/**
538
	 * Get default field value
539
	 *
540
	 * @return mixed
541
	 **/
542 1
	public function get_default_value() {
543 1
		return $this->default_value;
544
	}
545
546
	/**
547
	 * Set default field value
548
	 *
549
	 * @param mixed $default_value
550
	 **/
551 1
	public function set_default_value( $default_value ) {
552 1
		$this->default_value = $default_value;
553 1
		return $this;
554
	}
555
556
	/**
557
	 * Return the field base name.
558
	 *
559
	 * @return string
560
	 **/
561
	public function get_base_name() {
562
		return $this->base_name;
563
	}
564
565
	/**
566
	 * Set field base name as defined in the container.
567
	 **/
568
	public function set_base_name( $name ) {
569
		$this->base_name = $name;
570
	}
571
572
	/**
573
	 * Return the field name
574
	 *
575
	 * @return string
576
	 **/
577 2
	public function get_name() {
578 2
		return $this->name;
579
	}
580
581
	/**
582
	 * Set field name.
583
	 * Use only if you are completely aware of what you are doing.
584
	 *
585
	 * @param string $name Field name, either sanitized or not
586
	 **/
587 6
	public function set_name( $name ) {
588 6
		if ( empty( $name ) ) {
589 1
			Incorrect_Syntax_Exception::raise( 'Field name can\'t be empty' );
590
		}
591
592 5
		$regex = '/\A[a-z0-9_\-\[\]]+\z/'; // symbols ]-[ are supported in a hidden way - required for widgets to work (WP imposes dashes and square brackets on field names)
593 5
		if ( ! preg_match( $regex, $name ) ) {
594 3
			Incorrect_Syntax_Exception::raise( 'Field name can only contain lowercase alphanumeric characters and underscores.' );
595
		}
596
597 2
		$name_prefix = $this->get_name_prefix();
598 2
		$name = ( substr( $name, 0, strlen( $name_prefix ) ) !== $name_prefix ? $name_prefix . $name : $name );
599
600 2
		$this->name = $name;
601 2
	}
602
603
	/**
604
	 * Return the field name prefix
605
	 *
606
	 * @return string
607
	 **/
608 3
	public function get_name_prefix() {
609 3
		return $this->name_prefix;
610
	}
611
612
	/**
613
	 * Set field name prefix
614
	 * Use only if you are completely aware of what you are doing.
615
	 *
616
	 * @param string $name_prefix
617
	 **/
618 3
	public function set_name_prefix( $name_prefix ) {
619 3
		$name_prefix = strval( $name_prefix );
620 3
		$old_prefix_length = strlen( $this->name_prefix );
621 3
		$this->name_prefix = '';
622 3
		$this->set_name( substr( $this->get_name(), $old_prefix_length ) );
623
624 3
		$this->name_prefix = $name_prefix;
625 3
		$this->set_name( $this->name_prefix . $this->get_name() );
626 3
	}
627
628
	/**
629
	 * Return field label.
630
	 *
631
	 * @return string
632
	 **/
633
	public function get_label() {
634
		return $this->label;
635
	}
636
637
	/**
638
	 * Set field label.
639
	 *
640
	 * @param string $label If null, the label will be generated from the field name
641
	 **/
642 View Code Duplication
	public function set_label( $label ) {
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
643
		// Try to guess field label from it's name
644
		if ( is_null( $label ) ) {
645
			// remove the leading underscore(if it's there)
646
			$label = preg_replace( '~^_~', '', $this->name );
647
648
			// remove the leading "crb_"(if it's there)
649
			$label = preg_replace( '~^crb_~', '', $label );
650
651
			// split the name into words and make them capitalized
652
			$label = mb_convert_case( str_replace( '_', ' ', $label ), MB_CASE_TITLE );
653
		}
654
655
		$this->label = $label;
656
	}
657
658
	/**
659
	 * Return the field help text
660
	 *
661
	 * @return object $this
662
	 **/
663
	public function get_help_text() {
664
		return $this->help_text;
665
	}
666
667
	/**
668
	 * Set additional text to be displayed during field render,
669
	 * containing information and guidance for the user
670
	 *
671
	 * @return object $this
672
	 **/
673
	public function set_help_text( $help_text ) {
674
		$this->help_text = $help_text;
675
		return $this;
676
	}
677
678
	/**
679
	 * Alias for set_help_text()
680
	 *
681
	 * @see set_help_text()
682
	 * @return object $this
683
	 **/
684
	public function help_text( $help_text ) {
685
		return $this->set_help_text( $help_text );
686
	}
687
688
	/**
689
	 * Return whether or not this value should be auto loaded.
690
	 *
691
	 * @return bool
692
	 **/
693
	public function get_autoload() {
694
		return $this->autoload;
695
	}
696
697
	/**
698
	 * Whether or not this value should be auto loaded. Applicable to theme options only.
699
	 *
700
	 * @param bool $autoload
701
	 * @return object $this
702
	 **/
703
	public function set_autoload( $autoload ) {
704
		$this->autoload = $autoload;
705
		return $this;
706
	}
707
708
	/**
709
	 * Return whether or not this field should be lazyloaded.
710
	 *
711
	 * @return bool
712
	 **/
713
	public function get_lazyload() {
714
		return $this->lazyload;
715
	}
716
717
	/**
718
	 * Whether or not this field will be initialized when the field is in the viewport (visible).
719
	 *
720
	 * @param bool $lazyload
721
	 * @return object $this
722
	 **/
723
	public function set_lazyload( $lazyload ) {
724
		$this->lazyload = $lazyload;
725
		return $this;
726
	}
727
728
	/**
729
	 * Get the field width.
730
	 *
731
	 * @return int $width
732
	 **/
733
	public function get_width() {
734
		return $this->width;
735
	}
736
737
	/**
738
	 * Set the field width.
739
	 *
740
	 * @param int $width
741
	 * @return object $this
742
	 **/
743
	public function set_width( $width ) {
744
		$this->width = (int) $width;
745
		return $this;
746
	}
747
748
	/**
749
	 * Get the field custom CSS classes.
750
	 *
751
	 * @return array
752
	 **/
753
	public function get_classes() {
754
		return $this->classes;
755
	}
756
757
	/**
758
	 *  Add custom CSS class to the field html container.
759
	 *
760
	 * @param string|array $classes
761
	 * @return object $this
762
	 **/
763
	public function add_class( $classes ) {
764
		if ( ! is_array( $classes ) ) {
765
			$classes = array_values( array_filter( explode( ' ', $classes ) ) );
766
		}
767
768
		$this->classes = array_map( 'sanitize_html_class', $classes );
769
		return $this;
770
	}
771
772
	/**
773
	 * Whether this field is mandatory for the user
774
	 *
775
	 * @param bool $required
776
	 * @return object $this
777
	 **/
778
	public function set_required( $required = true ) {
779
		$this->required = $required;
780
		return $this;
781
	}
782
783
	/**
784
	 * Return whether this field is mandatory for the user
785
	 *
786
	 * @return bool
787
	 **/
788
	public function is_required() {
789
		return $this->required;
790
	}
791
792
	/**
793
	 * HTML id attribute getter.
794
	 * @return string
795
	 */
796 1
	public function get_id() {
797 1
		return $this->id;
798
	}
799
800
	/**
801
	 * HTML id attribute setter
802
	 * @param string $id
803
	 */
804 1
	public function set_id( $id ) {
805 1
		$this->id = $id;
806 1
	}
807
808
	/**
809
	 * Set the field visibility conditional logic.
810
	 *
811
	 * @param array
812
	 */
813 8
	public function set_conditional_logic( $rules ) {
814 8
		$this->conditional_logic = $this->parse_conditional_rules( $rules );
815
816 3
		return $this;
817
	}
818
819
	/**
820
	 * Get the conditional logic rules
821
	 *
822
	 * @return array
823
	 */
824 3
	public function get_conditional_logic() {
825 3
		return $this->conditional_logic;
826
	}
827
828
	/**
829
	 * Validate and parse the conditional logic rules.
830
	 *
831
	 * @param array $rules
832
	 * @return array
833
	 */
834
	protected function parse_conditional_rules( $rules ) {
835
		if ( ! is_array( $rules ) ) {
836
			Incorrect_Syntax_Exception::raise( 'Conditional logic rules argument should be an array.' );
837
		}
838
839
		$allowed_operators = array( '=', '!=', '>', '>=', '<', '<=', 'IN', 'NOT IN' );
840
841
		$parsed_rules = array(
842
			'relation' => Helper::get_relation_type_from_array( $rules ),
843
			'rules' => array(),
844
		);
845
846
		foreach ( $rules as $key => $rule ) {
847
			if ( $key === 'relation' ) {
848
				continue; // Skip the relation key as it is already handled above
849
			}
850
851
			// Check if the rule is valid
852
			if ( ! is_array( $rule ) || empty( $rule['field'] ) ) {
853
				Incorrect_Syntax_Exception::raise( 'Invalid conditional logic rule format. ' .
854
				'The rule should be an array with the "field" key set.' );
855
			}
856
857
			// Check the compare operator
858
			if ( empty( $rule['compare'] ) ) {
859
				$rule['compare'] = '=';
860
			}
861 View Code Duplication
			if ( ! in_array( $rule['compare'], $allowed_operators ) ) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
862
				Incorrect_Syntax_Exception::raise( 'Invalid conditional logic compare operator: <code>' .
863
					$rule['compare'] . '</code><br>Allowed operators are: <code>' .
864
				implode( ', ', $allowed_operators ) . '</code>' );
865
			}
866
			if ( $rule['compare'] === 'IN' || $rule['compare'] === 'NOT IN' ) {
867
				if ( ! is_array( $rule['value'] ) ) {
868
					Incorrect_Syntax_Exception::raise( 'Invalid conditional logic value format. ' .
869
					'An array is expected, when using the "' . $rule['compare'] . '" operator.' );
870
				}
871
			}
872
873
			// Check the value
874
			if ( ! isset( $rule['value'] ) ) {
875
				$rule['value'] = '';
876
			}
877
878
			$parsed_rules['rules'][] = $rule;
879
		}
880
881
		return $parsed_rules;
882
	}
883
884
	/**
885
	 * Set the REST visibility of the field
886
	 * 
887
	 * @param bool $visible
888
	 */
889
	public function set_visible_in_rest_api( $visible ) {
890
		$this->visible_in_rest_api = $visible;
891
	}
892
	
893
	/**
894
	 * Get the REST visibility of the field
895
	 * 
896
	 * @return bool
897
	 */
898
	public function get_visible_in_rest_api() {
899
		return $this->visible_in_rest_api;
900
	}
901
902
	/**
903
	 * Configuration function for setting the field visibility in the response of the requests to the REST API
904
	 * 
905
	 * @param bool $visible
906
	 * @return Field $this
907
	 */
908
	public function show_in_rest( $visible = true ) {
909
		$this->set_visible_in_rest_api( $visible );
910
		return $this;
911
	}
912
913
	/**
914
	 * Returns an array that holds the field data, suitable for JSON representation.
915
	 * This data will be available in the Underscore template and the Backbone Model.
916
	 *
917
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
918
	 * @return array
919
	 */
920
	public function to_json( $load ) {
921
		if ( $load ) {
922
			$this->load();
923
		}
924
925
		$field_data = array(
926
			'id' => $this->get_id(),
927
			'type' => $this->get_type(),
928
			'label' => $this->get_label(),
929
			'name' => $this->get_name(),
930
			'base_name' => $this->get_base_name(),
931
			'value' => $this->get_value(),
932
			'default_value' => $this->get_default_value(),
933
			'help_text' => $this->get_help_text(),
934
			'context' => $this->get_context(),
935
			'required' => $this->is_required(),
936
			'lazyload' => $this->get_lazyload(),
937
			'width' => $this->get_width(),
938
			'classes' => $this->get_classes(),
939
			'conditional_logic' => $this->get_conditional_logic(),
940
		);
941
942
		return $field_data;
943
	}
944
945
	/**
946
	 * Hook administration scripts.
947
	 */
948
	public static function admin_hook_scripts() {
949
		wp_enqueue_media();
950
	}
951
952
	/**
953
	 * Hook administration styles.
954
	 */
955
	public static function admin_hook_styles() {
956
		wp_enqueue_style( 'thickbox' );
957
	}
958
}
959