Completed
Pull Request — master (#34)
by
unknown
03:04
created

Field::set_autoload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 3
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
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
	 * Prefix to be prepended to the field name during load, save, delete and <strong>render</strong>
145
	 *
146
	 * @var string
147
	 **/
148
	protected $name_prefix = '_';
149
150
	/**
151
	 * Stores the field conditional logic rules.
152
	 *
153
	 * @var array
154
	 **/
155
	protected $conditional_logic = array();
156
157
	/**
158
	 * HTML other attributes ( like data-var="value", or anything you need ).
159
	 *
160
	 * @var string
161
	 **/
162
	protected $special_attrs = '';
163
164
	/**
165
	 * Create a new field of type $type and name $name and label $label.
166
	 *
167
	 * @param string $type
168
	 * @param string $name lower case and underscore-delimited
169
	 * @param string $label (optional) Automatically generated from $name if not present
170
	 * @return object $field
171
	 **/
172 15
	public static function factory( $type, $name, $label = null ) {
173
		// backward compatibility: `file` type used to be called `attachment`
174 15
		if ( $type === 'attachment' ) {
175
			$type = 'file';
176
		}
177
178 15
		$type = str_replace( ' ', '_', ucwords( str_replace( '_', ' ', $type ) ) );
179
180 15
		$class = __NAMESPACE__ . '\\' . $type . '_Field';
181
182 15 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...
183 4
			Incorrect_Syntax_Exception::raise( 'Unknown field "' . $type . '".' );
184 1
			$class = __NAMESPACE__ . '\\Broken_Field';
185 1
		}
186
187 12
		if ( strpos( $name, '-' ) !== false ) {
188 1
			Incorrect_Syntax_Exception::raise( 'Forbidden character "-" in name "' . $name . '".' );
189
			$class = __NAMESPACE__ . '\\Broken_Field';
190
		}
191
192 11
		$field = new $class( $name, $label );
193 10
		$field->type = $type;
194 10
		$field->add_template( $field->get_type(), array( $field, 'template' ) );
195
196 10
		return $field;
197
	}
198
199
	/**
200
	 * An alias of factory().
201
	 *
202
	 * @see Field::factory()
203
	 **/
204 15
	public static function make( $type, $name, $label = null ) {
205 15
		return self::factory( $type, $name, $label );
206
	}
207
208
	/**
209
	 * Create a field from a certain type with the specified label.
210
	 * @param string $name  Field name
211
	 * @param string $label Field label
212
	 */
213 11
	protected function __construct( $name, $label ) {
214 11
		$this->set_name( $name );
215 10
		$this->set_label( $label );
216 10
		$this->set_base_name( $name );
217
218
		// Pick random ID
219 10
		$random_string = md5( mt_rand() . $this->get_name() . $this->get_label() );
220 10
		$random_string = substr( $random_string, 0, 5 ); // 5 chars should be enough
221 10
		$this->id = 'carbon-' . $random_string;
222
223 10
		$this->init();
224 10
		if ( is_admin() ) {
225
			$this->admin_init();
226
		}
227
228 10
		add_action( 'admin_print_scripts', array( $this, 'admin_hook_scripts' ) );
229 10
		add_action( 'admin_print_styles', array( $this, 'admin_hook_styles' ) );
230 10
	}
231
232
	/**
233
	 * Perform instance initialization after calling setup()
234
	 **/
235
	public function init() {}
236
237
	/**
238
	 * Instance initialization when in the admin area.
239
	 * Called during object construction.
240
	 **/
241
	public function admin_init() {}
242
243
	/**
244
	 * Enqueue admin scripts.
245
	 * Called once per field type.
246
	 **/
247
	public function admin_enqueue_scripts() {}
248
249
	/**
250
	 * Prints the main Underscore template
251
	 **/
252
	public function template() { }
253
254
	/**
255
	 * Returns all the Backbone templates
256
	 *
257
	 * @return array
258
	 **/
259
	public function get_templates() {
260
		return $this->templates;
261
	}
262
263
	/**
264
	 * Adds a new Backbone template
265
	 **/
266
	public function add_template( $name, $callback ) {
267
		$this->templates[ $name ] = $callback;
268
	}
269
270
	/**
271
	 * Delegate load to the field DataStore instance
272
	 **/
273
	public function load() {
274
		$this->store->load( $this );
275
276
		if ( $this->get_value() === false ) {
277
			$this->set_value( $this->default_value );
278
		}
279
	}
280
281
	/**
282
	 * Delegate save to the field DataStore instance
283
	 **/
284
	public function save() {
285
		return $this->store->save( $this );
286
	}
287
288
	/**
289
	 * Delegate delete to the field DataStore instance
290
	 **/
291
	public function delete() {
292
		return $this->store->delete( $this );
293
	}
294
295
	/**
296
	 * Load the field value from an input array based on it's name
297
	 *
298
	 * @param array $input (optional) Array of field names and values. Defaults to $_POST
299
	 **/
300
	public function set_value_from_input( $input = null ) {
301
		if ( is_null( $input ) ) {
302
			$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...
303
		}
304
305
		if ( ! isset( $input[ $this->name ] ) ) {
306
			$this->set_value( null );
307
		} else {
308
			$this->set_value( stripslashes_deep( $input[ $this->name ] ) );
309
		}
310
	}
311
312
	/**
313
	 * Assign DataStore instance for use during load, save and delete
314
	 *
315
	 * @param object $store
316
	 * @return object $this
317
	 **/
318
	public function set_datastore( Datastore_Interface $store ) {
319
		$this->store = $store;
320
		return $this;
321
	}
322
323
	/**
324
	 * Return the DataStore instance used by the field
325
	 *
326
	 * @return object $store
327
	 **/
328
	public function get_datastore() {
329
		return $this->store;
330
	}
331
332
	/**
333
	 * Assign the type of the container this field is in
334
	 *
335
	 * @param string
336
	 * @return object $this
337
	 **/
338
	public function set_context( $context ) {
339
		$this->context = $context;
340
		return $this;
341
	}
342
343
	/**
344
	 * Return the type of the container this field is in
345
	 *
346
	 * @return string
347
	 **/
348
	public function get_context() {
349
		return $this->context;
350
	}
351
352
	/**
353
	 * Directly modify the field value
354
	 *
355
	 * @param mixed $value
356
	 **/
357
	public function set_value( $value ) {
358
		$this->value = $value;
359
	}
360
361
	/**
362
	 * Set default field value
363
	 *
364
	 * @param mixed $default_value
365
	 **/
366
	public function set_default_value( $default_value ) {
367
		$this->default_value = $default_value;
368
		return $this;
369
	}
370
371
	/**
372
	 * Get default field value
373
	 *
374
	 * @return mixed
375
	 **/
376
	public function get_default_value() {
377
		return $this->default_value;
378
	}
379
380
	/**
381
	 * Return the field value
382
	 *
383
	 * @return mixed
384
	 **/
385
	public function get_value() {
386
		return $this->value;
387
	}
388
389
	/**
390
	 * Set field name.
391
	 * Use only if you are completely aware of what you are doing.
392
	 *
393
	 * @param string $name Field name, either sanitized or not
394
	 **/
395
	public function set_name( $name ) {
396
		$name = preg_replace( '~\s+~', '_', mb_strtolower( $name ) );
397
398
		if ( empty( $name ) ) {
399
			Incorrect_Syntax_Exception::raise( 'Field name can\'t be empty' );
400
		}
401
402
		if ( $this->name_prefix && strpos( $name, $this->name_prefix ) !== 0 ) {
403
			$name = $this->name_prefix . $name;
404
		}
405
406
		$this->name = $name;
407
	}
408
409
	/**
410
	 * Return the field name
411
	 *
412
	 * @return string
413
	 **/
414
	public function get_name() {
415
		return $this->name;
416
	}
417
418
	/**
419
	 * Set field base name as defined in the container.
420
	 **/
421
	public function set_base_name( $name ) {
422
		$this->base_name = $name;
423
	}
424
425
	/**
426
	 * Return the field base name.
427
	 *
428
	 * @return string
429
	 **/
430
	public function get_base_name() {
431
		return $this->base_name;
432
	}
433
434
	/**
435
	 * Set field name prefix. Calling this method will update the current field
436
	 * name and the conditional logic fields.
437
	 *
438
	 * @param string $prefix
439
	 * @return object $this
440
	 **/
441
	public function set_prefix( $prefix ) {
442
		$escaped_prefix = preg_quote( $this->name_prefix, '~' );
443
		$this->name = preg_replace( '~^' . $escaped_prefix . '~', '', $this->name );
444
		$this->name_prefix = $prefix;
445
		$this->name = $this->name_prefix . $this->name;
446
447
		return $this;
448
	}
449
450
	/**
451
	 * Set field label.
452
	 *
453
	 * @param string $label If null, the label will be generated from the field name
454
	 **/
455
	public function set_label( $label ) {
456
		// Try to guess field label from it's name
457 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...
458
			// remove the leading underscore(if it's there)
459
			$label = preg_replace( '~^_~', '', $this->name );
460
461
			// remove the leading "crb_"(if it's there)
462
			$label = preg_replace( '~^crb_~', '', $label );
463
464
			// split the name into words and make them capitalized
465
			$label = mb_convert_case( str_replace( '_', ' ', $label ), MB_CASE_TITLE );
466
		}
467
468
		$this->label = $label;
469
	}
470
471
	/**
472
	 * Return field label.
473
	 *
474
	 * @return string
475
	 **/
476
	public function get_label() {
477
		return $this->label;
478
	}
479
480
	/**
481
	 * Set additional text to be displayed during field render,
482
	 * containing information and guidance for the user
483
	 *
484
	 * @return object $this
485
	 **/
486
	public function set_help_text( $help_text ) {
487
		$this->help_text = $help_text;
488
		return $this;
489
	}
490
491
	/**
492
	 * Alias for set_help_text()
493
	 *
494
	 * @see set_help_text()
495
	 * @return object $this
496
	 **/
497
	public function help_text( $help_text ) {
498
		return $this->set_help_text( $help_text );
499
	}
500
501
	/**
502
	 * Return the field help text
503
	 *
504
	 * @return object $this
505
	 **/
506
	public function get_help_text() {
507
		return $this->help_text;
508
	}
509
510
	/**
511
	 * Whether or not this value should be auto loaded. Applicable to theme options only.
512
	 *
513
	 * @param bool $autoload
514
	 * @return object $this
515
	 **/
516
	public function set_autoload( $autoload ) {
517
		$this->autoload = $autoload;
518
		return $this;
519
	}
520
521
	/**
522
	 * Return whether or not this value should be auto loaded.
523
	 *
524
	 * @return bool
525
	 **/
526
	public function get_autoload() {
527
		return $this->autoload;
528
	}
529
530
	/**
531
	 * Whether or not this field will be initialized when the field is in the viewport (visible).
532
	 *
533
	 * @param bool $lazyload
534
	 * @return object $this
535
	 **/
536
	public function set_lazyload( $lazyload ) {
537
		$this->lazyload = $lazyload;
538
		return $this;
539
	}
540
541
	/**
542
	 * Return whether or not this field should be lazyloaded.
543
	 *
544
	 * @return bool
545
	 **/
546
	public function get_lazyload() {
547
		return $this->lazyload;
548
	}
549
550
	/**
551
	 * Set the field width.
552
	 *
553
	 * @param int $width
554
	 * @return object $this
555
	 **/
556
	public function set_width( $width ) {
557
		$this->width = (int) $width;
558
		return $this;
559
	}
560
561
	/**
562
	 * Get the field width.
563
	 *
564
	 * @return int $width
565
	 **/
566
	public function get_width() {
567
		return $this->width;
568
	}
569
570
	/**
571
	 *  Add custom CSS class to the field html container.
572
	 *
573
	 * @param string|array $classes
574
	 * @return object $this
575
	 **/
576
	public function add_class( $classes ) {
577
		if ( ! is_array( $classes ) ) {
578
			$classes = array_values( array_filter( explode( ' ', $classes ) ) );
579
		}
580
581
		$this->classes = array_map( 'sanitize_html_class', $classes );
582
		return $this;
583
	}
584
585
	/**
586
	 * Get the field custom CSS classes.
587
	 *
588
	 * @return array
589
	 **/
590
	public function get_classes() {
591
		return $this->classes;
592
	}
593
594
	/**
595
	 * Whether this field is mandatory for the user
596
	 *
597
	 * @param bool $required
598
	 * @return object $this
599
	 **/
600
	public function set_required( $required ) {
601
		$this->required = $required;
602
		return $this;
603
	}
604
605
	/**
606
	 * HTML id attribute getter.
607
	 * @return string
608
	 */
609 1
	public function get_id() {
610 1
		return $this->id;
611
	}
612
613
	/**
614
	 * HTML id attribute setter
615
	 * @param string $id
616
	 */
617 1
	public function set_id( $id ) {
618 1
		$this->id = $id;
619 1
	}
620
621
	/**
622
	 * Return whether this field is mandatory for the user
623
	 *
624
	 * @return bool
625
	 **/
626
	public function is_required() {
627
		return $this->required;
628
	}
629
630
	/**
631
	 * Returns the type of the field based on the class.
632
	 * The class is stripped by the "CarbonFields" prefix.
633
	 * Also the "Field" suffix is removed.
634
	 * Then underscores and backslashes are removed.
635
	 *
636
	 * @return string
637
	 */
638
	public function get_type() {
639
		$class = get_class( $this );
640
641
		return $this->clean_type( $class );
642
	}
643
644
	/**
645
	 * Cleans up an object class for usage as HTML class
646
	 *
647
	 * @return string
648
	 */
649
	protected function clean_type( $type ) {
650
		$remove = array(
651
			'_',
652
			'\\',
653
			'CarbonFields',
654
			'Field',
655
		);
656
		$clean_class = str_replace( $remove, '', $type );
657
658
		return $clean_class;
659
	}
660
661
	/**
662
	 * Return an array of html classes to be used for the field container
663
	 *
664
	 * @return array
665
	 */
666
	public function get_html_class() {
667
		$html_classes = array();
668
669
		$object_class = get_class( $this );
670
		$html_classes[] = $this->get_type();
671
672
		$parent_class = $object_class;
673
		while ( $parent_class = get_parent_class( $parent_class ) ) {
674
			$clean_class = $this->clean_type( $parent_class );
675
676
			if ( $clean_class ) {
677
				$html_classes[] = $clean_class;
678
			}
679
		}
680
681
		return $html_classes;
682
	}
683
684
	/**
685
	 * Allows the value of a field to be processed after loading.
686
	 * Can be implemented by the extending class if necessary.
687
	 *
688
	 * @return array
689
	 */
690
	public function process_value() {
691
692
	}
693
694
	/**
695
	 * Returns an array that holds the field data, suitable for JSON representation.
696
	 * This data will be available in the Underscore template and the Backbone Model.
697
	 *
698
	 * @param bool $load  Should the value be loaded from the database or use the value from the current instance.
699
	 * @return array
700
	 */
701
	public function to_json( $load ) {
702
		if ( $load ) {
703
			$this->load();
704
		}
705
706
		$this->process_value();
707
708
		$field_data = array(
709
			'id' => $this->get_id(),
710
			'type' => $this->get_type(),
711
			'label' => $this->get_label(),
712
			'name' => $this->get_name(),
713
			'base_name' => $this->get_base_name(),
714
			'value' => $this->get_value(),
715
			'default_value' => $this->get_default_value(),
716
			'help_text' => $this->get_help_text(),
717
			'context' => $this->get_context(),
718
			'required' => $this->is_required(),
719
			'lazyload' => $this->get_lazyload(),
720
			'width' => $this->get_width(),
721
			'classes' => $this->get_classes(),
722
			'conditional_logic' => $this->get_conditional_logic(),
723
            'special_attrs' => $this->get_special_attrs(),
0 ignored issues
show
Coding Style introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
724
		);
725
726
		return $field_data;
727
	}
728
729
	/**
730
	 * Set the field visibility conditional logic.
731
	 *
732
	 * @param array
733
	 */
734 8
	public function set_conditional_logic( $rules ) {
735 8
		$this->conditional_logic = $this->parse_conditional_rules( $rules );
736
737 3
		return $this;
738
	}
739
740
	/**
741
	 * Get the conditional logic rules
742
	 *
743
	 * @return array
744
	 */
745 3
	public function get_conditional_logic() {
746 3
		return $this->conditional_logic;
747
	}
748
749
	/**
750
	 * Validate and parse the conditional logic rules.
751
	 *
752
	 * @param array $rules
753
	 * @return array
754
	 */
755
	protected function parse_conditional_rules( $rules ) {
756
		if ( ! is_array( $rules ) ) {
757
			Incorrect_Syntax_Exception::raise( 'Conditional logic rules argument should be an array.' );
758
		}
759
760
		$allowed_operators = array( '=', '!=', '>', '>=', '<', '<=', 'IN', 'NOT IN' );
761
		$allowed_relations = array( 'AND', 'OR' );
762
763
		$parsed_rules = array(
764
			'relation' => 'AND',
765
			'rules' => array(),
766
		);
767
768
		foreach ( $rules as $key => $rule ) {
769
			// Check if we have a relation key
770
			if ( $key === 'relation' ) {
771
				$relation = strtoupper( $rule );
772
773 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...
774
					Incorrect_Syntax_Exception::raise( 'Invalid relation type ' . $rule . '. ' .
775
					'The rule should be one of the following: "' . implode( '", "', $allowed_relations ) . '"' );
776
				}
777
778
				$parsed_rules['relation'] = $relation;
779
				continue;
780
			}
781
782
			// Check if the rule is valid
783
			if ( ! is_array( $rule ) || empty( $rule['field'] ) ) {
784
				Incorrect_Syntax_Exception::raise( 'Invalid conditional logic rule format. ' .
785
				'The rule should be an array with the "field" key set.' );
786
			}
787
788
			// Check the compare operator
789
			if ( empty( $rule['compare'] ) ) {
790
				$rule['compare'] = '=';
791
			}
792 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...
793
				Incorrect_Syntax_Exception::raise( 'Invalid conditional logic compare operator: <code>' .
794
					$rule['compare'] . '</code><br>Allowed operators are: <code>' .
795
				implode( ', ', $allowed_operators ) . '</code>' );
796
			}
797
			if ( $rule['compare'] === 'IN' || $rule['compare'] === 'NOT IN' ) {
798
				if ( ! is_array( $rule['value'] ) ) {
799
					Incorrect_Syntax_Exception::raise( 'Invalid conditional logic value format. ' .
800
					'An array is expected, when using the "' . $rule['compare'] . '" operator.' );
801
				}
802
			}
803
804
			// Check the value
805
			if ( ! isset( $rule['value'] ) ) {
806
				$rule['value'] = '';
807
			}
808
809
			$parsed_rules['rules'][] = $rule;
810
		}
811
812
		return $parsed_rules;
813
	}
814
815
816
	/**
817
	 *  Add other atttribute to the field html container (data-var="val" for example).
818
	 *
819
	 * @param string $special_attrs
820
	 * @return object $this
821
	 **/
822
	public function add_special_attrs( $special_attrs ) {
823
		$this->special_attrs = sanitize_text_field( $special_attrs );
824
		return $this;
825
	}
826
827
    /**
0 ignored issues
show
Coding Style introduced by
Tabs must be used to indent lines; spaces are not allowed
Loading history...
828
	 * Return field data-attrs.
829
	 *
830
	 * @return array
831
	 **/
832
	public function get_special_attrs() {
833
		return $this->special_attrs;
834
	}
835
836
837
	/**
838
	 * Hook administration scripts.
839
	 */
840
	public function admin_hook_scripts() {
841
		wp_enqueue_media();
842
		wp_enqueue_script( 'carbon-fields', \Carbon_Fields\URL . '/assets/js/fields.js', array( 'carbon-app', 'carbon-containers' ) );
843
		wp_localize_script( 'carbon-fields', 'crbl10n',
844
			array(
845
				'title' => __( 'Files', 'carbon_fields' ),
846
				'geocode_zero_results' => __( 'The address could not be found. ', 'carbon_fields' ),
847
				'geocode_not_successful' => __( 'Geocode was not successful for the following reason: ', 'carbon_fields' ),
848
				'max_num_items_reached' => __( 'Maximum number of items reached (%s items)', 'carbon_fields' ),
849
				'max_num_rows_reached' => __( 'Maximum number of rows reached (%s rows)', 'carbon_fields' ),
850
				'cannot_create_more_rows' => __( 'Cannot create more than %s rows', 'carbon_fields' ),
851
				'enter_name_of_new_sidebar' => __( 'Please enter the name of the new sidebar:', 'carbon_fields' ),
852
				'remove_sidebar_confirmation' => __( 'Are you sure you wish to remove this sidebar?', 'carbon_fields' ),
853
				'add_sidebar' => __( 'Add Sidebar', 'carbon_fields' ),
854
				'complex_no_rows' => __( 'There are no %s yet. Click <a href="#">here</a> to add one.', 'carbon_fields' ),
855
				'complex_add_button' => __( 'Add %s', 'carbon_fields' ),
856
				'complex_min_num_rows_not_reached' => __( 'Minimum number of rows not reached (%d %s)', 'carbon_fields' ),
857
				'message_form_validation_failed' => __( 'Please fill out all fields correctly. ', 'carbon_fields' ),
858
				'message_required_field' => __( 'This field is required. ', 'carbon_fields' ),
859
				'message_choose_option' => __( 'Please choose an option. ', 'carbon_fields' ),
860
			)
861
		);
862
	}
863
864
	/**
865
	 * Hook administration styles.
866
	 */
867
	public function admin_hook_styles() {
868
		wp_enqueue_style( 'thickbox' );
869
	}
870
} // END Field
871