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