Completed
Push — milestone/2.0 ( 5136f5...e2bf74 )
by
unknown
05:04
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
	 * Stores all the field Backbone templates
20
	 *
21
	 * @see factory()
22
	 * @see add_template()
23
	 * @var array
24
	 */
25
	protected $templates = array();
26
27
	/**
28
	 * Globally unique field identificator. Generated randomly
29
	 *
30
	 * @var string
31
	 */
32
	protected $id;
33
34
	/**
35
	 * Stores the initial <kbd>$type</kbd> variable passed to the <code>factory()</code> method
36
	 *
37
	 * @see factory
38
	 * @var string
39
	 */
40
	public $type;
41
42
	/**
43
	 * Array of ancestor field names
44
	 *
45
	 * @var array
46
	 **/
47
	protected $hierarchy = array();
48
49
	/**
50
	 * Array of complex entry ids
51
	 *
52
	 * @var array
53
	 **/
54
	protected $hierarchy_index = array();
55
56
	/**
57
	 * Field value
58
	 *
59
	 * @var Value_Set
60
	 */
61
	protected $value_set;
62
63
	/**
64
	 * Default field value
65
	 *
66
	 * @var mixed
67
	 */
68
	protected $default_value = '';
69
70
	/**
71
	 * Sanitized field name used as input name attribute during field render
72
	 *
73
	 * @see factory()
74
	 * @see set_name()
75
	 * @var string
76
	 */
77
	protected $name;
78
79
	/**
80
	 * Field name prefix
81
	 *
82
	 * @see set_name()
83
	 * @var string
84
	 */
85
	protected $name_prefix = '_';
86
87
	/**
88
	 * The base field name which is used in the container.
89
	 *
90
	 * @see set_base_name()
91
	 * @var string
92
	 */
93
	protected $base_name;
94
95
	/**
96
	 * Field name used as label during field render
97
	 *
98
	 * @see factory()
99
	 * @see set_label()
100
	 * @var string
101
	 */
102
	protected $label;
103
104
	/**
105
	 * Additional text containing information and guidance for the user
106
	 *
107
	 * @see help_text()
108
	 * @var string
109
	 */
110
	protected $help_text;
111
112
	/**
113
	 * Field DataStore instance to which save, load and delete calls are delegated
114
	 *
115
	 * @see set_datastore()
116
	 * @see get_datastore()
117
	 * @var Datastore_Interface
118
	 */
119
	protected $datastore;
120
121
	/**
122
	 * Flag whether the datastore is the default one or replaced with a custom one
123
	 *
124
	 * @see set_datastore()
125
	 * @see get_datastore()
126
	 * @var boolean
127
	 */
128
	protected $has_default_datastore = true;
129
130
	/**
131
	 * The type of the container this field is in
132
	 *
133
	 * @see get_context()
134
	 * @var string
135
	 */
136
	protected $context;
137
138
	/**
139
	 * Whether or not this value should be auto loaded. Applicable to theme options only.
140
	 *
141
	 * @see set_autoload()
142
	 * @var bool
143
	 **/
144
	protected $autoload = false;
145
146
	/**
147
	 * Whether or not this field will be initialized when the field is in the viewport (visible).
148
	 *
149
	 * @see set_lazyload()
150
	 * @var bool
151
	 **/
152
	protected $lazyload = false;
153
154
	/**
155
	 * The width of the field.
156
	 *
157
	 * @see set_width()
158
	 * @var int
159
	 **/
160
	protected $width = 0;
161
162
	/**
163
	 * Custom CSS classes.
164
	 *
165
	 * @see add_class()
166
	 * @var array
167
	 **/
168
	protected $classes = array();
169
170
	/**
171
	 * Whether or not this field is required.
172
	 *
173
	 * @see set_required()
174
	 * @var bool
175
	 **/
176
	protected $required = false;
177
178
	/**
179
	 * Stores the field conditional logic rules.
180
	 *
181
	 * @var array
182
	 **/
183
	protected $conditional_logic = array();
184
185
	/**
186
	 * Whether the field should be included in the response of the requests to the REST API
187
	 *
188
	 * @see  set_visible_in_rest_api
189
	 * @see  get_visible_in_rest_api
190
	 * @var boolean
191
	 */
192
	protected $visible_in_rest_api = false;
193
194
	/**
195
	 * Clone the Value_Set object as well
196
	 *
197
	 * @var array
198
	 **/
199
	public function __clone() {
200
		$this->set_value_set( clone $this->get_value_set() );
201
	}
202
203
	/**
204
	 * Create a new field of type $type and name $name and label $label.
205
	 *
206
	 * @param string $type
207
	 * @param string $name lower case and underscore-delimited
208
	 * @param string $label (optional) Automatically generated from $name if not present
209
	 * @return Field
210
	 **/
211 14
	public static function factory( $type, $name, $label = null ) {
212 14
		$type = str_replace( ' ', '_', ucwords( str_replace( '_', ' ', $type ) ) );
213
214 14
		$class = __NAMESPACE__ . '\\' . $type . '_Field';
215
216 14 View Code Duplication
		if ( ! class_exists( $class ) ) {
217 4
			Incorrect_Syntax_Exception::raise( 'Unknown field "' . $type . '".' );
218 1
			$class = __NAMESPACE__ . '\\Broken_Field';
219 1
		}
220
221 11
		$field = new $class( $type, $name, $label );
222
223 7
		return $field;
224
	}
225
226
	/**
227
	 * An alias of factory().
228
	 *
229
	 * @see Field::factory()
230
	 * @return Field
231
	 **/
232 14
	public static function make( $type, $name, $label = null ) {
233 14
		return static::factory( $type, $name, $label );
234
	}
235
236
	/**
237
	 * Create a field from a certain type with the specified label.
238
	 * 
239
	 * @param string $type  Field type
240
	 * @param string $name  Field name
241
	 * @param string $label Field label
242
	 */
243 11
	protected function __construct( $type, $name, $label ) {
244 11
		App::verify_boot();
245
		
246 11
		$this->type = $type;
247 11
		$this->set_base_name( $name );
248 11
		$this->set_name( $name );
249 7
		$this->set_label( $label );
250
251
		// Pick random ID
252 7
		$random_string = md5( mt_rand() . $this->get_name() . $this->get_label() );
253 7
		$random_string = substr( $random_string, 0, 5 ); // 5 chars should be enough
254 7
		$this->id = 'carbon-' . $random_string;
255
256 7
		$this->init();
257 7
	}
258
259
	/**
260
	 * Activate the field once the container is attached.
261
	 */
262
	public function activate() {
263
		$this->admin_init();
264
		$this->add_template( $this->get_type(), array( $this, 'template' ) );
265
266
		add_action( 'admin_footer', array( get_class(), 'admin_hook_scripts' ), 5 );
267
		add_action( 'admin_footer', array( get_class(), 'admin_hook_styles' ), 5 );
268
		add_action( 'admin_footer', array( get_class( $this ), 'admin_enqueue_scripts' ), 5 );
269
270
		do_action( 'crb_field_activated', $this );
271
	}
272
273
	/**
274
	 * Set array of hierarchy field names
275
	 **/
276
	public function set_hierarchy( $hierarchy ) {
277
		$this->hierarchy = $hierarchy;
278
	}
279
280
	/**
281
	 * Get array of hierarchy field names
282
	 *
283
	 * @return array
284
	 **/
285
	public function get_hierarchy() {
286
		return $this->hierarchy;
287
	}
288
289
	/**
290
	 * Set array of hierarchy indexes
291
	 **/
292
	public function set_hierarchy_index( $hierarchy_index ) {
293
		$hierarchy_index = ( ! empty( $hierarchy_index ) ) ? $hierarchy_index : array( 0 );
294
		$this->hierarchy_index = $hierarchy_index;
295
	}
296
297
	/**
298
	 * Get array of hierarchy indexes
299
	 *
300
	 * @return array
301
	 **/
302
	public function get_hierarchy_index() {
303
		return $this->hierarchy_index;
304
	}
305
306
	/**
307
	 * Return whether the field is a root field and holds a single value
308
	 *
309
	 * @return bool
310
	 **/
311 3
	public function is_simple_root_field() {
312 3
		$hierarchy = $this->get_hierarchy();
313
		return (
314 3
			empty( $hierarchy )
315 3
			&&
316
			(
317 2
				$this->get_value_set()->get_type() === Value_Set::TYPE_SINGLE_VALUE
318 2
				||
319 2
				$this->get_value_set()->get_type() === Value_Set::TYPE_MULTIPLE_PROPERTIES
320 2
			)
321 3
		);
322
	}
323
324
	/**
325
	 * Perform instance initialization
326
	 **/
327
	public function init() {}
328
329
	/**
330
	 * Instance initialization when in the admin area.
331
	 * Called during field boot.
332
	 **/
333
	public function admin_init() {}
334
335
	/**
336
	 * Enqueue admin scripts.
337
	 * Called once per field type.
338
	 **/
339
	public static function admin_enqueue_scripts() {}
340
341
	/**
342
	 * Prints the main Underscore template
343
	 **/
344
	public function template() { }
345
346
	/**
347
	 * Returns all the Backbone templates
348
	 *
349
	 * @return array
350
	 **/
351
	public function get_templates() {
352
		return $this->templates;
353
	}
354
355
	/**
356
	 * Adds a new Backbone template
357
	 **/
358
	protected function add_template( $name, $callback ) {
359
		$this->templates[ $name ] = $callback;
360
	}
361
362
	/**
363
	 * Get value from datastore
364
	 *
365
	 * @param bool $fallback_to_default
366
	 * @return mixed
367
	 **/
368
	protected function get_value_from_datastore( $fallback_to_default = true ) {
369
		$raw_value_set_tree = $this->get_datastore()->load( $this );
370
		
371
		$value = null;
372
		if ( isset( $raw_value_set_tree[ $this->get_base_name() ] ) ) {
373
			$value = $raw_value_set_tree[ $this->get_base_name() ]['value_set'];
374
		}
375
376
		if ( $value === null && $fallback_to_default ) {
377
			$value = $this->get_default_value();
378
		}
379
380
		return $value;
381
	}
382
383
	/**
384
	 * Load value from datastore
385
	 **/
386 2
	public function load() {
387 2
		$value = $this->get_value_from_datastore();
388 2
		$this->set_value( $value );
389 2
	}
390
391
	/**
392
	 * Save value to storage
393
	 **/
394 5
	public function save() {
395 5
		if ( ! in_array( $this->get_value_set()->get_type(), array( Value_Set::TYPE_SINGLE_VALUE, Value_Set::TYPE_MULTIPLE_PROPERTIES ) ) ) {
396 2
			$this->delete();
397 2
		}
398 5
		return $this->get_datastore()->save( $this );
399
	}
400
401
	/**
402
	 * Delete value from storage
403
	 */
404 1
	public function delete() {
405 1
		$this->get_datastore()->delete( $this );
406 1
	}
407
408
	/**
409
	 * Load the field value from an input array based on it's name
410
	 *
411
	 * @param array $input Array of field names and values.
412
	 **/
413 2
	public function set_value_from_input( $input ) {
414 2
		if ( isset( $input[ $this->get_name() ] ) ) {
415 1
			$this->set_value( $input[ $this->get_name() ] );
416 1
		} else {
417 1
			$this->set_value( array() );
418
		}
419 2
	}
420
421
	/**
422
	 * Return whether the datastore instance is the default one or has been overriden
423
	 *
424
	 * @return boolean
425
	 **/
426
	public function has_default_datastore() {
427
		return $this->has_default_datastore;
428
	}
429
430
	/**
431
	 * Set datastore instance
432
	 *
433
	 * @param Datastore_Interface $datastore
434
	 * @return object $this
435
	 **/
436 1
	public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) {
437 1
		if ( $set_as_default && ! $this->has_default_datastore() ) {
438
			return $this; // datastore has been overriden with a custom one - abort changing to a default one
439
		}
440 1
		$this->datastore = $datastore;
441 1
		$this->has_default_datastore = $set_as_default;
442 1
		return $this;
443
	}
444
445
	/**
446
	 * Get the DataStore instance
447
	 *
448
	 * @return Datastore_Interface $datastore
449
	 **/
450 1
	public function get_datastore() {
451 1
		return $this->datastore;
452
	}
453
454
	/**
455
	 * Assign the type of the container this field is in
456
	 *
457
	 * @param string
458
	 * @return object $this
459
	 **/
460
	public function set_context( $context ) {
461
		$this->context = $context;
462
		return $this;
463
	}
464
465
	/**
466
	 * Return the type of the container this field is in
467
	 *
468
	 * @return string
469
	 **/
470
	public function get_context() {
471
		return $this->context;
472
	}
473
474
	/**
475
	 * Get the Value_Set object
476
	 *
477
	 * @return Value_Set
478
	 **/
479 2
	public function get_value_set() {
480 2
		if ( $this->value_set === null ) {
481 1
			$this->set_value_set( new Value_Set() );
482 1
		}
483 2
		return $this->value_set;
484
	}
485
486
	/**
487
	 * Set the Value_Set object
488
	 *
489
	 * @param Value_Set $value_set
490
	 **/
491 1
	public function set_value_set( $value_set ) {
492 1
		$this->value_set = $value_set;
493 1
	}
494
495
	/**
496
	 * Alias for $this->get_value_set()->get(); with fallback to default value
497
	 *
498
	 * @return mixed
499
	 **/
500 3
	public function get_value() {
501 3
		if ( $this->get_value_set()->get() === null ) {
502 1
			$this->set_value( $this->get_default_value() );
503 1
		}
504 3
		return $this->get_value_set()->get();
505
	}
506
507
	/**
508
	 * Return a differently formatted value for end-users
509
	 *
510
	 * @return mixed
511
	 **/
512 2
	public function get_formatted_value() {
513 2
		return $this->get_value();
514
	}
515
516
	/**
517
	 * Alias for $this->get_value_set()->set( $value );
518
	 **/
519 1
	public function set_value( $value ) {
520 1
		$this->get_value_set()->set( $value );
521 1
	}
522
523
	/**
524
	 * Set default field value
525
	 *
526
	 * @param mixed $default_value
527
	 **/
528 1
	public function set_default_value( $default_value ) {
529 1
		$this->default_value = $default_value;
530 1
		return $this;
531
	}
532
533
	/**
534
	 * Get default field value
535
	 *
536
	 * @return mixed
537
	 **/
538 1
	public function get_default_value() {
539 1
		return $this->default_value;
540
	}
541
542
	/**
543
	 * Set field name.
544
	 * Use only if you are completely aware of what you are doing.
545
	 *
546
	 * @param string $name Field name, either sanitized or not
547
	 **/
548 6
	public function set_name( $name ) {
549 6
		if ( empty( $name ) ) {
550 1
			Incorrect_Syntax_Exception::raise( 'Field name can\'t be empty' );
551
		}
552
553 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)
554 5
		if ( ! preg_match( $regex, $name ) ) {
555 3
			Incorrect_Syntax_Exception::raise( 'Field name can only contain lowercase alphanumeric characters and underscores.' );
556
		}
557
558 2
		$name_prefix = $this->get_name_prefix();
559 2
		$name = ( substr( $name, 0, strlen( $name_prefix ) ) !== $name_prefix ? $name_prefix . $name : $name );
560
561 2
		$this->name = $name;
562 2
	}
563
564
	/**
565
	 * Return the field name
566
	 *
567
	 * @return string
568
	 **/
569 2
	public function get_name() {
570 2
		return $this->name;
571
	}
572
573
	/**
574
	 * Set field name prefix
575
	 * Use only if you are completely aware of what you are doing.
576
	 *
577
	 * @param string $name_prefix
578
	 **/
579 3
	public function set_name_prefix( $name_prefix ) {
580 3
		$name_prefix = strval( $name_prefix );
581 3
		$old_prefix_length = strlen( $this->name_prefix );
582 3
		$this->name_prefix = '';
583 3
		$this->set_name( substr( $this->get_name(), $old_prefix_length ) );
584
585 3
		$this->name_prefix = $name_prefix;
586 3
		$this->set_name( $this->name_prefix . $this->get_name() );
587 3
	}
588
589
	/**
590
	 * Return the field name prefix
591
	 *
592
	 * @return string
593
	 **/
594 3
	public function get_name_prefix() {
595 3
		return $this->name_prefix;
596
	}
597
598
	/**
599
	 * Set field base name as defined in the container.
600
	 **/
601
	public function set_base_name( $name ) {
602
		$this->base_name = $name;
603
	}
604
605
	/**
606
	 * Return the field base name.
607
	 *
608
	 * @return string
609
	 **/
610
	public function get_base_name() {
611
		return $this->base_name;
612
	}
613
614
	/**
615
	 * Set field label.
616
	 *
617
	 * @param string $label If null, the label will be generated from the field name
618
	 **/
619 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...
620
		// Try to guess field label from it's name
621
		if ( is_null( $label ) ) {
622
			// remove the leading underscore(if it's there)
623
			$label = preg_replace( '~^_~', '', $this->name );
624
625
			// remove the leading "crb_"(if it's there)
626
			$label = preg_replace( '~^crb_~', '', $label );
627
628
			// split the name into words and make them capitalized
629
			$label = mb_convert_case( str_replace( '_', ' ', $label ), MB_CASE_TITLE );
630
		}
631
632
		$this->label = $label;
633
	}
634
635
	/**
636
	 * Return field label.
637
	 *
638
	 * @return string
639
	 **/
640
	public function get_label() {
641
		return $this->label;
642
	}
643
644
	/**
645
	 * Set additional text to be displayed during field render,
646
	 * containing information and guidance for the user
647
	 *
648
	 * @return object $this
649
	 **/
650
	public function set_help_text( $help_text ) {
651
		$this->help_text = $help_text;
652
		return $this;
653
	}
654
655
	/**
656
	 * Alias for set_help_text()
657
	 *
658
	 * @see set_help_text()
659
	 * @return object $this
660
	 **/
661
	public function help_text( $help_text ) {
662
		return $this->set_help_text( $help_text );
663
	}
664
665
	/**
666
	 * Return the field help text
667
	 *
668
	 * @return object $this
669
	 **/
670
	public function get_help_text() {
671
		return $this->help_text;
672
	}
673
674
	/**
675
	 * Whether or not this value should be auto loaded. Applicable to theme options only.
676
	 *
677
	 * @param bool $autoload
678
	 * @return object $this
679
	 **/
680
	public function set_autoload( $autoload ) {
681
		$this->autoload = $autoload;
682
		return $this;
683
	}
684
685
	/**
686
	 * Return whether or not this value should be auto loaded.
687
	 *
688
	 * @return bool
689
	 **/
690
	public function get_autoload() {
691
		return $this->autoload;
692
	}
693
694
	/**
695
	 * Whether or not this field will be initialized when the field is in the viewport (visible).
696
	 *
697
	 * @param bool $lazyload
698
	 * @return object $this
699
	 **/
700
	public function set_lazyload( $lazyload ) {
701
		$this->lazyload = $lazyload;
702
		return $this;
703
	}
704
705
	/**
706
	 * Return whether or not this field should be lazyloaded.
707
	 *
708
	 * @return bool
709
	 **/
710
	public function get_lazyload() {
711
		return $this->lazyload;
712
	}
713
714
	/**
715
	 * Set the field width.
716
	 *
717
	 * @param int $width
718
	 * @return object $this
719
	 **/
720
	public function set_width( $width ) {
721
		$this->width = (int) $width;
722
		return $this;
723
	}
724
725
	/**
726
	 * Get the field width.
727
	 *
728
	 * @return int $width
729
	 **/
730
	public function get_width() {
731
		return $this->width;
732
	}
733
734
	/**
735
	 *  Add custom CSS class to the field html container.
736
	 *
737
	 * @param string|array $classes
738
	 * @return object $this
739
	 **/
740
	public function add_class( $classes ) {
741
		if ( ! is_array( $classes ) ) {
742
			$classes = array_values( array_filter( explode( ' ', $classes ) ) );
743
		}
744
745
		$this->classes = array_map( 'sanitize_html_class', $classes );
746
		return $this;
747
	}
748
749
	/**
750
	 * Get the field custom CSS classes.
751
	 *
752
	 * @return array
753
	 **/
754
	public function get_classes() {
755
		return $this->classes;
756
	}
757
758
	/**
759
	 * Whether this field is mandatory for the user
760
	 *
761
	 * @param bool $required
762
	 * @return object $this
763
	 **/
764
	public function set_required( $required = true ) {
765
		$this->required = $required;
766
		return $this;
767
	}
768
769
	/**
770
	 * HTML id attribute getter.
771
	 * @return string
772
	 */
773 1
	public function get_id() {
774 1
		return $this->id;
775
	}
776
777
	/**
778
	 * HTML id attribute setter
779
	 * @param string $id
780
	 */
781 1
	public function set_id( $id ) {
782 1
		$this->id = $id;
783 1
	}
784
785
	/**
786
	 * Return whether this field is mandatory for the user
787
	 *
788
	 * @return bool
789
	 **/
790
	public function is_required() {
791
		return $this->required;
792
	}
793
794
	/**
795
	 * Returns the type of the field based on the class.
796
	 * The class is stripped by the "CarbonFields" prefix.
797
	 * Also the "Field" suffix is removed.
798
	 * Then underscores and backslashes are removed.
799
	 *
800
	 * @return string
801
	 */
802
	public function get_type() {
803
		$class = get_class( $this );
804
805
		return $this->clean_type( $class );
806
	}
807
808
	/**
809
	 * Cleans up an object class for usage as HTML class
810
	 *
811
	 * @param string $type
812
	 * @return string
813
	 */
814
	protected function clean_type( $type ) {
815
		$remove = array(
816
			'_',
817
			'\\',
818
			'CarbonFields',
819
			'Field',
820
		);
821
		$clean_class = str_replace( $remove, '', $type );
822
823
		return $clean_class;
824
	}
825
826
	/**
827
	 * Returns an array that holds the field data, suitable for JSON representation.
828
	 * This data will be available in the Underscore template and the Backbone Model.
829
	 *
830
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
831
	 * @return array
832
	 */
833
	public function to_json( $load ) {
834
		if ( $load ) {
835
			$this->load();
836
		}
837
838
		$field_data = array(
839
			'id' => $this->get_id(),
840
			'type' => $this->get_type(),
841
			'label' => $this->get_label(),
842
			'name' => $this->get_name(),
843
			'base_name' => $this->get_base_name(),
844
			'value' => $this->get_value(),
845
			'default_value' => $this->get_default_value(),
846
			'help_text' => $this->get_help_text(),
847
			'context' => $this->get_context(),
848
			'required' => $this->is_required(),
849
			'lazyload' => $this->get_lazyload(),
850
			'width' => $this->get_width(),
851
			'classes' => $this->get_classes(),
852
			'conditional_logic' => $this->get_conditional_logic(),
853
		);
854
855
		return $field_data;
856
	}
857
858
	/**
859
	 * Set the field visibility conditional logic.
860
	 *
861
	 * @param array
862
	 */
863 8
	public function set_conditional_logic( $rules ) {
864 8
		$this->conditional_logic = $this->parse_conditional_rules( $rules );
865
866 3
		return $this;
867
	}
868
869
	/**
870
	 * Get the conditional logic rules
871
	 *
872
	 * @return array
873
	 */
874 3
	public function get_conditional_logic() {
875 3
		return $this->conditional_logic;
876
	}
877
878
	/**
879
	 * Validate and parse the conditional logic rules.
880
	 *
881
	 * @param array $rules
882
	 * @return array
883
	 */
884
	protected function parse_conditional_rules( $rules ) {
885
		if ( ! is_array( $rules ) ) {
886
			Incorrect_Syntax_Exception::raise( 'Conditional logic rules argument should be an array.' );
887
		}
888
889
		$allowed_operators = array( '=', '!=', '>', '>=', '<', '<=', 'IN', 'NOT IN' );
890
891
		$parsed_rules = array(
892
			'relation' => Helper::get_relation_type_from_array( $rules ),
893
			'rules' => array(),
894
		);
895
896
		foreach ( $rules as $key => $rule ) {
897
			if ( $key === 'relation' ) {
898
				continue; // Skip the relation key as it is already handled above
899
			}
900
901
			// Check if the rule is valid
902
			if ( ! is_array( $rule ) || empty( $rule['field'] ) ) {
903
				Incorrect_Syntax_Exception::raise( 'Invalid conditional logic rule format. ' .
904
				'The rule should be an array with the "field" key set.' );
905
			}
906
907
			// Check the compare operator
908
			if ( empty( $rule['compare'] ) ) {
909
				$rule['compare'] = '=';
910
			}
911
			if ( ! in_array( $rule['compare'], $allowed_operators ) ) {
912
				Incorrect_Syntax_Exception::raise( 'Invalid conditional logic compare operator: <code>' .
913
					$rule['compare'] . '</code><br>Allowed operators are: <code>' .
914
				implode( ', ', $allowed_operators ) . '</code>' );
915
			}
916
			if ( $rule['compare'] === 'IN' || $rule['compare'] === 'NOT IN' ) {
917
				if ( ! is_array( $rule['value'] ) ) {
918
					Incorrect_Syntax_Exception::raise( 'Invalid conditional logic value format. ' .
919
					'An array is expected, when using the "' . $rule['compare'] . '" operator.' );
920
				}
921
			}
922
923
			// Check the value
924
			if ( ! isset( $rule['value'] ) ) {
925
				$rule['value'] = '';
926
			}
927
928
			$parsed_rules['rules'][] = $rule;
929
		}
930
931
		return $parsed_rules;
932
	}
933
934
	/**
935
	 * Configuration function for setting the field visibility in the response of the requests to the REST API
936
	 * 
937
	 * @param bool $visible
938
	 * @return Field $this
939
	 */
940
	public function show_in_rest( $visible = true ) {
941
		$this->set_visible_in_rest_api( $visible );
942
		return $this;
943
	}
944
	/**
945
	 * Set the REST visibility of the field
946
	 * 
947
	 * @param bool $visible
948
	 */
949
	public function set_visible_in_rest_api( $visible ) {
950
		$this->visible_in_rest_api = $visible;
951
	}
952
	/**
953
	 * Get the REST visibility of the field
954
	 * 
955
	 * @return bool
956
	 */
957
	public function get_visible_in_rest_api() {
958
		return $this->visible_in_rest_api;
959
	}
960
961
	/**
962
	 * Hook administration scripts.
963
	 */
964
	public static function admin_hook_scripts() {
965
		wp_enqueue_media();
966
		wp_enqueue_script( 'carbon-fields', \Carbon_Fields\URL . '/assets/js/fields.js', array( 'carbon-app', 'carbon-containers' ), \Carbon_Fields\VERSION );
967
		wp_localize_script( 'carbon-fields', 'crbl10n',
968
			array(
969
				'title' => __( 'Files', \Carbon_Fields\TEXT_DOMAIN ),
970
				'geocode_zero_results' => __( 'The address could not be found. ', \Carbon_Fields\TEXT_DOMAIN ),
971
				'geocode_not_successful' => __( 'Geocode was not successful for the following reason: ', \Carbon_Fields\TEXT_DOMAIN ),
972
				'max_num_items_reached' => __( 'Maximum number of items reached (%s items)', \Carbon_Fields\TEXT_DOMAIN ),
973
				'max_num_rows_reached' => __( 'Maximum number of rows reached (%s rows)', \Carbon_Fields\TEXT_DOMAIN ),
974
				'cannot_create_more_rows' => __( 'Cannot create more than %s rows', \Carbon_Fields\TEXT_DOMAIN ),
975
				'complex_no_rows' => __( 'There are no %s yet. Click <a href="#">here</a> to add one.', \Carbon_Fields\TEXT_DOMAIN ),
976
				'complex_add_button' => __( 'Add %s', \Carbon_Fields\TEXT_DOMAIN ),
977
				'complex_min_num_rows_not_reached' => __( 'Minimum number of rows not reached (%1$d %2$s)', \Carbon_Fields\TEXT_DOMAIN ),
978
				'message_form_validation_failed' => __( 'Please fill out all fields correctly. ', \Carbon_Fields\TEXT_DOMAIN ),
979
				'message_required_field' => __( 'This field is required. ', \Carbon_Fields\TEXT_DOMAIN ),
980
				'message_choose_option' => __( 'Please choose an option. ', \Carbon_Fields\TEXT_DOMAIN ),
981
982
				'enter_name_of_new_sidebar' => __( 'Please enter the name of the new sidebar:', \Carbon_Fields\TEXT_DOMAIN ),
983
			)
984
		);
985
	}
986
987
	/**
988
	 * Hook administration styles.
989
	 */
990
	public static function admin_hook_styles() {
991
		wp_enqueue_style( 'thickbox' );
992
	}
993
}
994