|
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
|
|
|
* 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
|
15 |
|
public static function factory( $type, $name, $label = null ) { |
|
166
|
15 |
|
$type = str_replace( ' ', '_', ucwords( str_replace( '_', ' ', $type ) ) ); |
|
167
|
|
|
|
|
168
|
15 |
|
$class = __NAMESPACE__ . '\\' . $type . '_Field'; |
|
169
|
|
|
|
|
170
|
15 |
|
if ( ! class_exists( $class ) ) { |
|
171
|
4 |
|
Incorrect_Syntax_Exception::raise( 'Unknown field "' . $type . '".' ); |
|
172
|
1 |
|
$class = __NAMESPACE__ . '\\Broken_Field'; |
|
173
|
1 |
|
} |
|
174
|
|
|
|
|
175
|
12 |
|
if ( strpos( $name, '-' ) !== false ) { |
|
176
|
1 |
|
Incorrect_Syntax_Exception::raise( 'Forbidden character "-" in name "' . $name . '".' ); |
|
177
|
|
|
$class = __NAMESPACE__ . '\\Broken_Field'; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
11 |
|
$field = new $class( $name, $label ); |
|
181
|
10 |
|
$field->type = $type; |
|
182
|
10 |
|
$field->add_template( $field->get_type(), array( $field, 'template' ) ); |
|
183
|
|
|
|
|
184
|
10 |
|
return $field; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* An alias of factory(). |
|
189
|
|
|
* |
|
190
|
|
|
* @see Field::factory() |
|
191
|
|
|
**/ |
|
192
|
15 |
|
public static function make( $type, $name, $label = null ) { |
|
193
|
15 |
|
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
|
11 |
|
protected function __construct( $name, $label ) { |
|
202
|
11 |
|
$this->set_name( $name ); |
|
203
|
10 |
|
$this->set_label( $label ); |
|
204
|
10 |
|
$this->set_base_name( $name ); |
|
205
|
|
|
|
|
206
|
|
|
// Pick random ID |
|
207
|
10 |
|
$random_string = md5( mt_rand() . $this->get_name() . $this->get_label() ); |
|
208
|
10 |
|
$random_string = substr( $random_string, 0, 5 ); // 5 chars should be enough |
|
209
|
10 |
|
$this->id = 'carbon-' . $random_string; |
|
210
|
|
|
|
|
211
|
10 |
|
$this->init(); |
|
212
|
10 |
|
if ( is_admin() ) { |
|
213
|
|
|
$this->admin_init(); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
10 |
|
add_action( 'admin_print_scripts', array( $this, 'admin_hook_scripts' ) ); |
|
217
|
10 |
|
add_action( 'admin_print_styles', array( $this, 'admin_hook_styles' ) ); |
|
218
|
10 |
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Perform instance initialization after calling setup() |
|
222
|
|
|
**/ |
|
223
|
|
|
public function init() {} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Instance initialization when in the admin area. |
|
227
|
|
|
* Called during object construction. |
|
228
|
|
|
**/ |
|
229
|
|
|
public function admin_init() {} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Enqueue admin scripts. |
|
233
|
|
|
* Called once per field type. |
|
234
|
|
|
**/ |
|
235
|
|
|
public function admin_enqueue_scripts() {} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Prints the main Underscore template |
|
239
|
|
|
**/ |
|
240
|
|
|
public function template() { } |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Returns all the Backbone templates |
|
244
|
|
|
* |
|
245
|
|
|
* @return array |
|
246
|
|
|
**/ |
|
247
|
|
|
public function get_templates() { |
|
248
|
|
|
return $this->templates; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Adds a new Backbone template |
|
253
|
|
|
**/ |
|
254
|
|
|
public function add_template( $name, $callback ) { |
|
255
|
|
|
$this->templates[ $name ] = $callback; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Delegate load to the field DataStore instance |
|
260
|
|
|
**/ |
|
261
|
|
|
public function load() { |
|
262
|
|
|
$this->store->load( $this ); |
|
263
|
|
|
|
|
264
|
|
|
if ( $this->get_value() === false ) { |
|
265
|
|
|
$this->set_value( $this->default_value ); |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Delegate save to the field DataStore instance |
|
271
|
|
|
**/ |
|
272
|
|
|
public function save() { |
|
273
|
|
|
return $this->store->save( $this ); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Delegate delete to the field DataStore instance |
|
278
|
|
|
**/ |
|
279
|
|
|
public function delete() { |
|
280
|
|
|
return $this->store->delete( $this ); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Load the field value from an input array based on it's name |
|
285
|
|
|
* |
|
286
|
|
|
* @param array $input (optional) Array of field names and values. Defaults to $_POST |
|
287
|
|
|
**/ |
|
288
|
|
|
public function set_value_from_input( $input = null ) { |
|
289
|
|
|
if ( is_null( $input ) ) { |
|
290
|
|
|
$input = $_POST; |
|
|
|
|
|
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
if ( ! isset( $input[ $this->name ] ) ) { |
|
294
|
|
|
$this->set_value( null ); |
|
295
|
|
|
} else { |
|
296
|
|
|
$this->set_value( stripslashes_deep( $input[ $this->name ] ) ); |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* Assign DataStore instance for use during load, save and delete |
|
302
|
|
|
* |
|
303
|
|
|
* @param object $store |
|
304
|
|
|
* @return object $this |
|
305
|
|
|
**/ |
|
306
|
|
|
public function set_datastore( Datastore_Interface $store ) { |
|
307
|
|
|
$this->store = $store; |
|
308
|
|
|
return $this; |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* Return the DataStore instance used by the field |
|
313
|
|
|
* |
|
314
|
|
|
* @return object $store |
|
315
|
|
|
**/ |
|
316
|
|
|
public function get_datastore() { |
|
317
|
|
|
return $this->store; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* Assign the type of the container this field is in |
|
322
|
|
|
* |
|
323
|
|
|
* @param string |
|
324
|
|
|
* @return object $this |
|
325
|
|
|
**/ |
|
326
|
|
|
public function set_context( $context ) { |
|
327
|
|
|
$this->context = $context; |
|
328
|
|
|
return $this; |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* Return the type of the container this field is in |
|
333
|
|
|
* |
|
334
|
|
|
* @return string |
|
335
|
|
|
**/ |
|
336
|
|
|
public function get_context() { |
|
337
|
|
|
return $this->context; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* Directly modify the field value |
|
342
|
|
|
* |
|
343
|
|
|
* @param mixed $value |
|
344
|
|
|
**/ |
|
345
|
|
|
public function set_value( $value ) { |
|
346
|
|
|
$this->value = $value; |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
/** |
|
350
|
|
|
* Set default field value |
|
351
|
|
|
* |
|
352
|
|
|
* @param mixed $default_value |
|
353
|
|
|
**/ |
|
354
|
|
|
public function set_default_value( $default_value ) { |
|
355
|
|
|
$this->default_value = $default_value; |
|
356
|
|
|
return $this; |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
/** |
|
360
|
|
|
* Get default field value |
|
361
|
|
|
* |
|
362
|
|
|
* @return mixed |
|
363
|
|
|
**/ |
|
364
|
|
|
public function get_default_value() { |
|
365
|
|
|
return $this->default_value; |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
/** |
|
369
|
|
|
* Return the field value |
|
370
|
|
|
* |
|
371
|
|
|
* @return mixed |
|
372
|
|
|
**/ |
|
373
|
|
|
public function get_value() { |
|
374
|
|
|
return $this->value; |
|
375
|
|
|
} |
|
376
|
|
|
|
|
377
|
|
|
/** |
|
378
|
|
|
* Set field name. |
|
379
|
|
|
* Use only if you are completely aware of what you are doing. |
|
380
|
|
|
* |
|
381
|
|
|
* @param string $name Field name, either sanitized or not |
|
382
|
|
|
**/ |
|
383
|
|
|
public function set_name( $name ) { |
|
384
|
|
|
$name = preg_replace( '~\s+~', '_', mb_strtolower( $name ) ); |
|
385
|
|
|
|
|
386
|
|
|
if ( empty( $name ) ) { |
|
387
|
|
|
Incorrect_Syntax_Exception::raise( 'Field name can\'t be empty' ); |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
if ( $this->name_prefix && strpos( $name, $this->name_prefix ) !== 0 ) { |
|
391
|
|
|
$name = $this->name_prefix . $name; |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
$this->name = $name; |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
/** |
|
398
|
|
|
* Return the field name |
|
399
|
|
|
* |
|
400
|
|
|
* @return string |
|
401
|
|
|
**/ |
|
402
|
|
|
public function get_name() { |
|
403
|
|
|
return $this->name; |
|
404
|
|
|
} |
|
405
|
|
|
|
|
406
|
|
|
/** |
|
407
|
|
|
* Set field base name as defined in the container. |
|
408
|
|
|
**/ |
|
409
|
|
|
public function set_base_name( $name ) { |
|
410
|
|
|
$this->base_name = $name; |
|
411
|
|
|
} |
|
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
|
|
|
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
|
|
|
public function set_label( $label ) { |
|
444
|
|
|
// Try to guess field label from it's name |
|
445
|
|
View Code Duplication |
if ( is_null( $label ) ) { |
|
|
|
|
|
|
446
|
|
|
// remove the leading underscore(if it's there) |
|
447
|
|
|
$label = preg_replace( '~^_~', '', $this->name ); |
|
448
|
|
|
|
|
449
|
|
|
// remove the leading "crb_"(if it's there) |
|
450
|
|
|
$label = preg_replace( '~^crb_~', '', $label ); |
|
451
|
|
|
|
|
452
|
|
|
// split the name into words and make them capitalized |
|
453
|
|
|
$label = mb_convert_case( str_replace( '_', ' ', $label ), MB_CASE_TITLE ); |
|
454
|
|
|
} |
|
455
|
|
|
|
|
456
|
|
|
$this->label = $label; |
|
457
|
|
|
} |
|
458
|
|
|
|
|
459
|
|
|
/** |
|
460
|
|
|
* Return field label. |
|
461
|
|
|
* |
|
462
|
|
|
* @return string |
|
463
|
|
|
**/ |
|
464
|
|
|
public function get_label() { |
|
465
|
|
|
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
|
|
|
public function get_type() { |
|
627
|
|
|
$class = get_class( $this ); |
|
628
|
|
|
|
|
629
|
|
|
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
|
|
|
protected function clean_type( $type ) { |
|
638
|
|
|
$remove = array( |
|
639
|
|
|
'_', |
|
640
|
|
|
'\\', |
|
641
|
|
|
'CarbonFields', |
|
642
|
|
|
'Field', |
|
643
|
|
|
); |
|
644
|
|
|
$clean_class = str_replace( $remove, '', $type ); |
|
645
|
|
|
|
|
646
|
|
|
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
|
|
|
protected function parse_conditional_rules( $rules ) { |
|
743
|
|
|
if ( ! is_array( $rules ) ) { |
|
744
|
|
|
Incorrect_Syntax_Exception::raise( 'Conditional logic rules argument should be an array.' ); |
|
745
|
|
|
} |
|
746
|
|
|
|
|
747
|
|
|
$allowed_operators = array( '=', '!=', '>', '>=', '<', '<=', 'IN', 'NOT IN' ); |
|
748
|
|
|
$allowed_relations = array( 'AND', 'OR' ); |
|
749
|
|
|
|
|
750
|
|
|
$parsed_rules = array( |
|
751
|
|
|
'relation' => 'AND', |
|
752
|
|
|
'rules' => array(), |
|
753
|
|
|
); |
|
754
|
|
|
|
|
755
|
|
|
foreach ( $rules as $key => $rule ) { |
|
756
|
|
|
// Check if we have a relation key |
|
757
|
|
|
if ( $key === 'relation' ) { |
|
758
|
|
|
$relation = strtoupper( $rule ); |
|
759
|
|
|
|
|
760
|
|
View Code Duplication |
if ( ! in_array( $relation, $allowed_relations ) ) { |
|
|
|
|
|
|
761
|
|
|
Incorrect_Syntax_Exception::raise( 'Invalid relation type ' . $rule . '. ' . |
|
762
|
|
|
'The rule should be one of the following: "' . implode( '", "', $allowed_relations ) . '"' ); |
|
|
|
|
|
|
763
|
|
|
} |
|
764
|
|
|
|
|
765
|
|
|
$parsed_rules['relation'] = $relation; |
|
766
|
|
|
continue; |
|
767
|
|
|
} |
|
768
|
|
|
|
|
769
|
|
|
// Check if the rule is valid |
|
770
|
|
|
if ( ! is_array( $rule ) || empty( $rule['field'] ) ) { |
|
771
|
|
|
Incorrect_Syntax_Exception::raise( 'Invalid conditional logic rule format. ' . |
|
772
|
|
|
'The rule should be an array with the "field" key set.' ); |
|
|
|
|
|
|
773
|
|
|
} |
|
774
|
|
|
|
|
775
|
|
|
// Check the compare operator |
|
776
|
|
|
if ( empty( $rule['compare'] ) ) { |
|
777
|
|
|
$rule['compare'] = '='; |
|
778
|
|
|
} |
|
779
|
|
View Code Duplication |
if ( ! in_array( $rule['compare'], $allowed_operators ) ) { |
|
|
|
|
|
|
780
|
|
|
Incorrect_Syntax_Exception::raise( 'Invalid conditional logic compare operator: <code>' . |
|
781
|
|
|
$rule['compare'] . '</code><br>Allowed operators are: <code>' . |
|
782
|
|
|
implode( ', ', $allowed_operators ) . '</code>' ); |
|
|
|
|
|
|
783
|
|
|
} |
|
784
|
|
|
if ( $rule['compare'] === 'IN' || $rule['compare'] === 'NOT IN' ) { |
|
785
|
|
|
if ( ! is_array( $rule['value'] ) ) { |
|
786
|
|
|
Incorrect_Syntax_Exception::raise( 'Invalid conditional logic value format. ' . |
|
787
|
|
|
'An array is expected, when using the "' . $rule['compare'] . '" operator.' ); |
|
|
|
|
|
|
788
|
|
|
} |
|
789
|
|
|
} |
|
790
|
|
|
|
|
791
|
|
|
// Check the value |
|
792
|
|
|
if ( ! isset( $rule['value'] ) ) { |
|
793
|
|
|
$rule['value'] = ''; |
|
794
|
|
|
} |
|
795
|
|
|
|
|
796
|
|
|
$parsed_rules['rules'][] = $rule; |
|
797
|
|
|
} |
|
798
|
|
|
|
|
799
|
|
|
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
|
|
|
|