Completed
Pull Request — master (#167)
by
unknown
02:01
created

Field::get_mask()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Carbon_Fields\Field;
4
5
use Carbon_Fields\Datastore\Datastore_Interface;
6
use Carbon_Fields\Exception\Incorrect_Syntax_Exception;
7
8
/**
9
 * Base field class.
10
 * Defines the key container methods and their default implementations.
11
 * Implements factory design pattern.
12
 **/
13
class Field {
14
	/**
15
	 * Stores all the field Backbone templates
16
	 *
17
	 * @see factory()
18
	 * @see add_template()
19
	 * @var array
20
	 */
21
	protected $templates = array();
22
23
	/**
24
	 * Globally unique field identificator. Generated randomly
25
	 *
26
	 * @var string
27
	 */
28
	protected $id;
29
30
	/**
31
	 * Stores the initial <kbd>$type</kbd> variable passed to the <code>factory()</code> method
32
	 *
33
	 * @see factory
34
	 * @var string
35
	 */
36
	public $type;
37
38
	/**
39
	 * Field value
40
	 *
41
	 * @var mixed
42
	 */
43
	protected $value;
44
45
	/**
46
	 * Default field value
47
	 *
48
	 * @var mixed
49
	 */
50
	protected $default_value;
51
52
	/**
53
	 * Sanitized field name used as input name attribute during field render
54
	 *
55
	 * @see factory()
56
	 * @see set_name()
57
	 * @var string
58
	 */
59
	protected $name;
60
61
	/**
62
	 * The base field name which is used in the container.
63
	 *
64
	 * @see set_base_name()
65
	 * @var string
66
	 */
67
	protected $base_name;
68
69
	/**
70
	 * Field name used as label during field render
71
	 *
72
	 * @see factory()
73
	 * @see set_label()
74
	 * @var string
75
	 */
76
	protected $label;
77
78
	/**
79
	 * Additional text containing information and guidance for the user
80
	 *
81
	 * @see help_text()
82
	 * @var string
83
	 */
84
	protected $help_text;
85
86
	/**
87
	 * Field DataStore instance to which save, load and delete calls are delegated
88
	 *
89
	 * @see set_datastore()
90
	 * @see get_datastore()
91
	 * @var Datastore_Interface
92
	 */
93
	protected $store;
94
95
	/**
96
	 * The type of the container this field is in
97
	 *
98
	 * @see get_context()
99
	 * @var string
100
	 */
101
	protected $context;
102
103
	/**
104
	 * Whether or not this value should be auto loaded. Applicable to theme options only.
105
	 *
106
	 * @see set_autoload()
107
	 * @var bool
108
	 **/
109
	protected $autoload = false;
110
111
	/**
112
	 * Whether or not this field will be initialized when the field is in the viewport (visible).
113
	 *
114
	 * @see set_lazyload()
115
	 * @var bool
116
	 **/
117
	protected $lazyload = false;
118
119
	/**
120
	 * The width of the field.
121
	 *
122
	 * @see set_width()
123
	 * @var int
124
	 **/
125
	protected $width = 0;
126
127
	/**
128
	 * Custom CSS classes.
129
	 *
130
	 * @see add_class()
131
	 * @var array
132
	 **/
133
	protected $classes = array();
134
135
	/**
136
	 * Whether or not this field is required.
137
	 *
138
	 * @see set_required()
139
	 * @var bool
140
	 **/
141
	protected $required = false;
142
143
	/**
144
	 * Mask of this field.
145
	 *
146
	 * @see set_required()
147
	 * @var bool
148
	 **/
149
	protected $mask = '';
150
151
	/**
152
	 * jQuery mask options.
153
	 *
154
	 * @see set_required()
155
	 * @var bool
156
	 **/
157
	protected $mask_options = array();
158
159
	/**
160
	 * Prefix to be prepended to the field name during load, save, delete and <strong>render</strong>
161
	 *
162
	 * @var string
163
	 **/
164
	protected $name_prefix = '_';
165
166
	/**
167
	 * Stores the field conditional logic rules.
168
	 *
169
	 * @var array
170
	 **/
171
	protected $conditional_logic = array();
172
173
	/**
174
	 * Create a new field of type $type and name $name and label $label.
175
	 *
176
	 * @param string $type
177
	 * @param string $name lower case and underscore-delimited
178
	 * @param string $label (optional) Automatically generated from $name if not present
179
	 * @return object $field
180
	 **/
181 14
	public static function factory( $type, $name, $label = null ) {
182
		// backward compatibility: `file` type used to be called `attachment`
183 14
		if ( $type === 'attachment' ) {
184
			$type = 'file';
185
		}
186
187 14
		$type = str_replace( ' ', '_', ucwords( str_replace( '_', ' ', $type ) ) );
188
189 14
		$class = __NAMESPACE__ . '\\' . $type . '_Field';
190
191 14 View Code Duplication
		if ( ! class_exists( $class ) ) {
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...
192 4
			Incorrect_Syntax_Exception::raise( 'Unknown field "' . $type . '".' );
193 1
			$class = __NAMESPACE__ . '\\Broken_Field';
194 1
		}
195
196 11
		$field = new $class( $name, $label );
197 10
		$field->type = $type;
198
199 10
		return $field;
200
	}
201
202
	/**
203
	 * An alias of factory().
204
	 *
205
	 * @see Field::factory()
206
	 **/
207 14
	public static function make( $type, $name, $label = null ) {
208 14
		return self::factory( $type, $name, $label );
209
	}
210
211
	/**
212
	 * Create a field from a certain type with the specified label.
213
	 * @param string $name  Field name
214
	 * @param string $label Field label
215
	 */
216 11 View Code Duplication
	protected function __construct( $name, $label ) {
0 ignored issues
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...
217 11
		$this->set_name( $name );
218 10
		$this->set_label( $label );
219 10
		$this->set_base_name( $name );
220
221
		// Pick random ID
222 10
		$random_string = md5( mt_rand() . $this->get_name() . $this->get_label() );
223 10
		$random_string = substr( $random_string, 0, 5 ); // 5 chars should be enough
224 10
		$this->id = 'carbon-' . $random_string;
225
226 10
		$this->init();
227 10
	}
228
229
	/**
230
	 * Boot the field once the container is attached.
231
	 **/
232
	public function boot() {
233
		$this->admin_init();
234
235
		$this->add_template( $this->get_type(), array( $this, 'template' ) );
236
237
		add_action( 'admin_footer', array( get_class(), 'admin_hook_scripts' ), 5 );
238
		add_action( 'admin_footer', array( get_class(), 'admin_hook_styles' ), 5 );
239
240
		add_action( 'admin_footer', array( get_class( $this ), 'admin_enqueue_scripts' ), 5 );
241
	}
242
243
	/**
244
	 * Perform instance initialization after calling setup()
245
	 **/
246
	public function init() {}
247
248
	/**
249
	 * Instance initialization when in the admin area.
250
	 * Called during field boot.
251
	 **/
252
	public function admin_init() {}
253
254
	/**
255
	 * Enqueue admin scripts.
256
	 * Called once per field type.
257
	 **/
258
	public static function admin_enqueue_scripts() {}
259
260
	/**
261
	 * Prints the main Underscore template
262
	 **/
263
	public function template() { }
264
265
	/**
266
	 * Returns all the Backbone templates
267
	 *
268
	 * @return array
269
	 **/
270
	public function get_templates() {
271
		return $this->templates;
272
	}
273
274
	/**
275
	 * Adds a new Backbone template
276
	 **/
277
	public function add_template( $name, $callback ) {
278
		$this->templates[ $name ] = $callback;
279
	}
280
281
	/**
282
	 * Delegate load to the field DataStore instance
283
	 **/
284
	public function load() {
285
		$this->store->load( $this );
286
287
		if ( $this->get_value() === false ) {
288
			$this->set_value( $this->default_value );
289
		}
290
	}
291
292
	/**
293
	 * Delegate save to the field DataStore instance
294
	 **/
295
	public function save() {
296
		return $this->store->save( $this );
297
	}
298
299
	/**
300
	 * Delegate delete to the field DataStore instance
301
	 **/
302
	public function delete() {
303
		return $this->store->delete( $this );
304
	}
305
306
	/**
307
	 * Load the field value from an input array based on it's name
308
	 *
309
	 * @param array $input (optional) Array of field names and values. Defaults to $_POST
310
	 **/
311
	public function set_value_from_input( $input = null ) {
312
		if ( is_null( $input ) ) {
313
			$input = $_POST;
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
314
		}
315
316
		if ( ! isset( $input[ $this->name ] ) ) {
317
			$this->set_value( null );
318
		} else {
319
			$this->set_value( stripslashes_deep( $input[ $this->name ] ) );
320
		}
321
	}
322
323
	/**
324
	 * Assign DataStore instance for use during load, save and delete
325
	 *
326
	 * @param object $store
327
	 * @return object $this
328
	 **/
329
	public function set_datastore( Datastore_Interface $store ) {
330
		$this->store = $store;
331
		return $this;
332
	}
333
334
	/**
335
	 * Return the DataStore instance used by the field
336
	 *
337
	 * @return object $store
338
	 **/
339
	public function get_datastore() {
340
		return $this->store;
341
	}
342
343
	/**
344
	 * Assign the type of the container this field is in
345
	 *
346
	 * @param string
347
	 * @return object $this
348
	 **/
349
	public function set_context( $context ) {
350
		$this->context = $context;
351
		return $this;
352
	}
353
354
	/**
355
	 * Return the type of the container this field is in
356
	 *
357
	 * @return string
358
	 **/
359
	public function get_context() {
360
		return $this->context;
361
	}
362
363
	/**
364
	 * Directly modify the field value
365
	 *
366
	 * @param mixed $value
367
	 **/
368
	public function set_value( $value ) {
369
		$this->value = $value;
370
	}
371
372
	/**
373
	 * Set default field value
374
	 *
375
	 * @param mixed $default_value
376
	 **/
377
	public function set_default_value( $default_value ) {
378
		$this->default_value = $default_value;
379
		return $this;
380
	}
381
382
	/**
383
	 * Get default field value
384
	 *
385
	 * @return mixed
386
	 **/
387
	public function get_default_value() {
388
		return $this->default_value;
389
	}
390
391
	/**
392
	 * Return the field value
393
	 *
394
	 * @return mixed
395
	 **/
396
	public function get_value() {
397
		return $this->value;
398
	}
399
400
	/**
401
	 * Set field name.
402
	 * Use only if you are completely aware of what you are doing.
403
	 *
404
	 * @param string $name Field name, either sanitized or not
405
	 **/
406
	public function set_name( $name ) {
407
		$name = preg_replace( '~\s+~', '_', mb_strtolower( $name ) );
408
409
		if ( empty( $name ) ) {
410
			Incorrect_Syntax_Exception::raise( 'Field name can\'t be empty' );
411
		}
412
413
		if ( $this->name_prefix && strpos( $name, $this->name_prefix ) !== 0 ) {
414
			$name = $this->name_prefix . $name;
415
		}
416
417
		$this->name = $name;
418
	}
419
420
	/**
421
	 * Return the field name
422
	 *
423
	 * @return string
424
	 **/
425
	public function get_name() {
426
		return $this->name;
427
	}
428
429
	/**
430
	 * Set field base name as defined in the container.
431
	 **/
432
	public function set_base_name( $name ) {
433
		$this->base_name = $name;
434
	}
435
436
	/**
437
	 * Return the field base name.
438
	 *
439
	 * @return string
440
	 **/
441
	public function get_base_name() {
442
		return $this->base_name;
443
	}
444
445
	/**
446
	 * Set field name prefix. Calling this method will update the current field
447
	 * name and the conditional logic fields.
448
	 *
449
	 * @param string $prefix
450
	 * @return object $this
451
	 **/
452
	public function set_prefix( $prefix ) {
453
		$escaped_prefix = preg_quote( $this->name_prefix, '~' );
454
		$this->name = preg_replace( '~^' . $escaped_prefix . '~', '', $this->name );
455
		$this->name_prefix = $prefix;
456
		$this->name = $this->name_prefix . $this->name;
457
458
		return $this;
459
	}
460
461
	/**
462
	 * Set field label.
463
	 *
464
	 * @param string $label If null, the label will be generated from the field name
465
	 **/
466
	public function set_label( $label ) {
467
		// Try to guess field label from it's name
468 View Code Duplication
		if ( is_null( $label ) ) {
0 ignored issues
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...
469
			// remove the leading underscore(if it's there)
470
			$label = preg_replace( '~^_~', '', $this->name );
471
472
			// remove the leading "crb_"(if it's there)
473
			$label = preg_replace( '~^crb_~', '', $label );
474
475
			// split the name into words and make them capitalized
476
			$label = mb_convert_case( str_replace( '_', ' ', $label ), MB_CASE_TITLE );
477
		}
478
479
		$this->label = $label;
480
	}
481
482
	/**
483
	 * Return field label.
484
	 *
485
	 * @return string
486
	 **/
487
	public function get_label() {
488
		return $this->label;
489
	}
490
491
	/**
492
	 * Set additional text to be displayed during field render,
493
	 * containing information and guidance for the user
494
	 *
495
	 * @return object $this
496
	 **/
497
	public function set_help_text( $help_text ) {
498
		$this->help_text = $help_text;
499
		return $this;
500
	}
501
502
	/**
503
	 * Alias for set_help_text()
504
	 *
505
	 * @see set_help_text()
506
	 * @return object $this
507
	 **/
508
	public function help_text( $help_text ) {
509
		return $this->set_help_text( $help_text );
510
	}
511
512
	/**
513
	 * Return the field help text
514
	 *
515
	 * @return object $this
516
	 **/
517
	public function get_help_text() {
518
		return $this->help_text;
519
	}
520
521
	/**
522
	 * Whether or not this value should be auto loaded. Applicable to theme options only.
523
	 *
524
	 * @param bool $autoload
525
	 * @return object $this
526
	 **/
527
	public function set_autoload( $autoload ) {
528
		$this->autoload = $autoload;
529
		return $this;
530
	}
531
532
	/**
533
	 * Return whether or not this value should be auto loaded.
534
	 *
535
	 * @return bool
536
	 **/
537
	public function get_autoload() {
538
		return $this->autoload;
539
	}
540
541
	/**
542
	 * Whether or not this field will be initialized when the field is in the viewport (visible).
543
	 *
544
	 * @param bool $lazyload
545
	 * @return object $this
546
	 **/
547
	public function set_lazyload( $lazyload ) {
548
		$this->lazyload = $lazyload;
549
		return $this;
550
	}
551
552
	/**
553
	 * Return whether or not this field should be lazyloaded.
554
	 *
555
	 * @return bool
556
	 **/
557
	public function get_lazyload() {
558
		return $this->lazyload;
559
	}
560
561
	/**
562
	 * Set the field width.
563
	 *
564
	 * @param int $width
565
	 * @return object $this
566
	 **/
567
	public function set_width( $width ) {
568
		$this->width = (int) $width;
569
		return $this;
570
	}
571
572
	/**
573
	 * Get the field width.
574
	 *
575
	 * @return int $width
576
	 **/
577
	public function get_width() {
578
		return $this->width;
579
	}
580
581
	/**
582
	 *  Add custom CSS class to the field html container.
583
	 *
584
	 * @param string|array $classes
585
	 * @return object $this
586
	 **/
587
	public function add_class( $classes ) {
588
		if ( ! is_array( $classes ) ) {
589
			$classes = array_values( array_filter( explode( ' ', $classes ) ) );
590
		}
591
592
		$this->classes = array_map( 'sanitize_html_class', $classes );
593
		return $this;
594
	}
595
596
	/**
597
	 * Get the field custom CSS classes.
598
	 *
599
	 * @return array
600
	 **/
601
	public function get_classes() {
602
		return $this->classes;
603
	}
604
605
	/**
606
	 * Whether this field is mandatory for the user
607
	 *
608
	 * @param bool $required
609
	 * @return object $this
610
	 **/
611
	public function set_required( $required ) {
612
		$this->required = $required;
613
		return $this;
614
	}
615
616
	/**
617
	 * Set a mask format on this field
618
	 *
619
	 * @param string $mask
620
	 * @param array $options
621
	 * @return object $this
622
	 **/
623
	public function set_mask( $mask, $options = array() ) {
624
		$this->mask = $mask;
0 ignored issues
show
Documentation Bug introduced by
The property $mask was declared of type boolean, but $mask is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
625
		$this->mask_options = $options;
0 ignored issues
show
Documentation Bug introduced by
It seems like $options of type array is incompatible with the declared type boolean of property $mask_options.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
626
		return $this;
627
	}
628
629
	/**
630
	 * HTML id attribute getter.
631
	 * @return string
632
	 */
633 1
	public function get_id() {
634 1
		return $this->id;
635
	}
636
637
	/**
638
	 * HTML id attribute setter
639
	 * @param string $id
640
	 */
641 1
	public function set_id( $id ) {
642 1
		$this->id = $id;
643 1
	}
644
645
	/**
646
	 * Return whether this field is mandatory for the user
647
	 *
648
	 * @return bool
649
	 **/
650
	public function is_required() {
651
		return $this->required;
652
	}
653
654
	/**
655
	 * Return mask format on this field
656
	 *
657
	 * @return string
658
	 **/
659
	public function get_mask() {
660
		return $this->mask;
661
	}
662
663
	/**
664
	 * Return jQuery mask options
665
	 *
666
	 * @return array
667
	 **/
668
	public function get_mask_options()
669
	{
670
		return $this->mask_options;
671
	}
672
673
	/**
674
	 * Returns the type of the field based on the class.
675
	 * The class is stripped by the "CarbonFields" prefix.
676
	 * Also the "Field" suffix is removed.
677
	 * Then underscores and backslashes are removed.
678
	 *
679
	 * @return string
680
	 */
681
	public function get_type() {
682
		$class = get_class( $this );
683
684
		return $this->clean_type( $class );
685
	}
686
687
	/**
688
	 * Cleans up an object class for usage as HTML class
689
	 *
690
	 * @return string
691
	 */
692
	protected function clean_type( $type ) {
693
		$remove = array(
694
			'_',
695
			'\\',
696
			'CarbonFields',
697
			'Field',
698
		);
699
		$clean_class = str_replace( $remove, '', $type );
700
701
		return $clean_class;
702
	}
703
704
	/**
705
	 * Return an array of html classes to be used for the field container
706
	 *
707
	 * @return array
708
	 */
709
	public function get_html_class() {
710
		$html_classes = array();
711
712
		$object_class = get_class( $this );
713
		$html_classes[] = $this->get_type();
714
715
		$parent_class = $object_class;
716
		while ( $parent_class = get_parent_class( $parent_class ) ) {
717
			$clean_class = $this->clean_type( $parent_class );
718
719
			if ( $clean_class ) {
720
				$html_classes[] = $clean_class;
721
			}
722
		}
723
724
		return $html_classes;
725
	}
726
727
	/**
728
	 * Allows the value of a field to be processed after loading.
729
	 * Can be implemented by the extending class if necessary.
730
	 *
731
	 * @return array
732
	 */
733
	public function process_value() {
734
735
	}
736
737
	/**
738
	 * Returns an array that holds the field data, suitable for JSON representation.
739
	 * This data will be available in the Underscore template and the Backbone Model.
740
	 *
741
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
742
	 * @return array
743
	 */
744
	public function to_json( $load ) {
745
		if ( $load ) {
746
			$this->load();
747
		}
748
749
		$this->process_value();
750
751
		$field_data = array(
752
			'id' => $this->get_id(),
753
			'type' => $this->get_type(),
754
			'label' => $this->get_label(),
755
			'name' => $this->get_name(),
756
			'base_name' => $this->get_base_name(),
757
			'value' => $this->get_value(),
758
			'default_value' => $this->get_default_value(),
759
			'help_text' => $this->get_help_text(),
760
			'context' => $this->get_context(),
761
			'required' => $this->is_required(),
762
			'mask' => $this->get_mask(),
763
			'mask_options' => $this->get_mask_options(),
764
			'lazyload' => $this->get_lazyload(),
765
			'width' => $this->get_width(),
766
			'classes' => $this->get_classes(),
767
			'conditional_logic' => $this->get_conditional_logic(),
768
		);
769
770
		return $field_data;
771
	}
772
773
	/**
774
	 * Set the field visibility conditional logic.
775
	 *
776
	 * @param array
777
	 */
778 8
	public function set_conditional_logic( $rules ) {
779 8
		$this->conditional_logic = $this->parse_conditional_rules( $rules );
780
781 3
		return $this;
782
	}
783
784
	/**
785
	 * Get the conditional logic rules
786
	 *
787
	 * @return array
788
	 */
789 3
	public function get_conditional_logic() {
790 3
		return $this->conditional_logic;
791
	}
792
793
	/**
794
	 * Validate and parse the conditional logic rules.
795
	 *
796
	 * @param array $rules
797
	 * @return array
798
	 */
799
	protected function parse_conditional_rules( $rules ) {
800
		if ( ! is_array( $rules ) ) {
801
			Incorrect_Syntax_Exception::raise( 'Conditional logic rules argument should be an array.' );
802
		}
803
804
		$allowed_operators = array( '=', '!=', '>', '>=', '<', '<=', 'IN', 'NOT IN' );
805
		$allowed_relations = array( 'AND', 'OR' );
806
807
		$parsed_rules = array(
808
			'relation' => 'AND',
809
			'rules' => array(),
810
		);
811
812
		foreach ( $rules as $key => $rule ) {
813
			// Check if we have a relation key
814
			if ( $key === 'relation' ) {
815
				$relation = strtoupper( $rule );
816
817 View Code Duplication
				if ( ! in_array( $relation, $allowed_relations ) ) {
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...
818
					Incorrect_Syntax_Exception::raise( 'Invalid relation type ' . $rule . '. ' .
819
					'The rule should be one of the following: "' . implode( '", "', $allowed_relations ) . '"' );
820
				}
821
822
				$parsed_rules['relation'] = $relation;
823
				continue;
824
			}
825
826
			// Check if the rule is valid
827
			if ( ! is_array( $rule ) || empty( $rule['field'] ) ) {
828
				Incorrect_Syntax_Exception::raise( 'Invalid conditional logic rule format. ' .
829
				'The rule should be an array with the "field" key set.' );
830
			}
831
832
			// Check the compare operator
833
			if ( empty( $rule['compare'] ) ) {
834
				$rule['compare'] = '=';
835
			}
836 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...
837
				Incorrect_Syntax_Exception::raise( 'Invalid conditional logic compare operator: <code>' .
838
					$rule['compare'] . '</code><br>Allowed operators are: <code>' .
839
				implode( ', ', $allowed_operators ) . '</code>' );
840
			}
841
			if ( $rule['compare'] === 'IN' || $rule['compare'] === 'NOT IN' ) {
842
				if ( ! is_array( $rule['value'] ) ) {
843
					Incorrect_Syntax_Exception::raise( 'Invalid conditional logic value format. ' .
844
					'An array is expected, when using the "' . $rule['compare'] . '" operator.' );
845
				}
846
			}
847
848
			// Check the value
849
			if ( ! isset( $rule['value'] ) ) {
850
				$rule['value'] = '';
851
			}
852
853
			$parsed_rules['rules'][] = $rule;
854
		}
855
856
		return $parsed_rules;
857
	}
858
859
860
	/**
861
	 * Hook administration scripts.
862
	 */
863
	public static function admin_hook_scripts() {
864
		wp_enqueue_media();
865
		wp_enqueue_script( 'carbon-fields', \Carbon_Fields\URL . '/assets/js/fields.js', array( 'carbon-app', 'carbon-containers' ) );
866
		wp_enqueue_script( 'carbon-jquery-mask', \Carbon_Fields\URL . '/assets/js/lib/jquery.mask.js' );
867
		wp_localize_script( 'carbon-fields', 'crbl10n',
868
			array(
869
				'title' => __( 'Files', 'carbon-fields' ),
870
				'geocode_zero_results' => __( 'The address could not be found. ', 'carbon-fields' ),
871
				'geocode_not_successful' => __( 'Geocode was not successful for the following reason: ', 'carbon-fields' ),
872
				'max_num_items_reached' => __( 'Maximum number of items reached (%s items)', 'carbon-fields' ),
873
				'max_num_rows_reached' => __( 'Maximum number of rows reached (%s rows)', 'carbon-fields' ),
874
				'cannot_create_more_rows' => __( 'Cannot create more than %s rows', 'carbon-fields' ),
875
				'enter_name_of_new_sidebar' => __( 'Please enter the name of the new sidebar:', 'carbon-fields' ),
876
				'remove_sidebar_confirmation' => __( 'Are you sure you wish to remove this sidebar?', 'carbon-fields' ),
877
				'add_sidebar' => __( 'Add Sidebar', 'carbon-fields' ),
878
				'complex_no_rows' => __( 'There are no %s yet. Click <a href="#">here</a> to add one.', 'carbon-fields' ),
879
				'complex_add_button' => __( 'Add %s', 'carbon-fields' ),
880
				'complex_min_num_rows_not_reached' => __( 'Minimum number of rows not reached (%d %s)', 'carbon-fields' ),
881
				'message_form_validation_failed' => __( 'Please fill out all fields correctly. ', 'carbon-fields' ),
882
				'message_required_field' => __( 'This field is required. ', 'carbon-fields' ),
883
				'message_choose_option' => __( 'Please choose an option. ', 'carbon-fields' ),
884
			)
885
		);
886
	}
887
888
	/**
889
	 * Hook administration styles.
890
	 */
891
	public static function admin_hook_styles() {
892
		wp_enqueue_style( 'thickbox' );
893
	}
894
} // END Field
895