1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Field; |
4
|
|
|
|
5
|
|
|
use Carbon_Fields\Helper\Helper; |
6
|
|
|
use Carbon_Fields\Value_Set\Value_Set; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Set field class. |
10
|
|
|
* Allows to create a set of checkboxes where multiple can be selected. |
11
|
|
|
*/ |
12
|
|
|
class Set_Field extends Predefined_Options_Field { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The options limit. |
16
|
|
|
* |
17
|
|
|
* @var int |
18
|
|
|
*/ |
19
|
|
|
protected $limit_options = 0; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Default field value |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $default_value = array(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Create a field from a certain type with the specified label. |
30
|
|
|
* |
31
|
|
|
* @param string $type Field type |
32
|
|
|
* @param string $name Field name |
33
|
|
|
* @param string $label Field label |
34
|
|
|
*/ |
35
|
|
|
public function __construct( $type, $name, $label ) { |
36
|
|
|
$this->set_value_set( new Value_Set( Value_Set::TYPE_MULTIPLE_VALUES ) ); |
37
|
|
|
parent::__construct( $type, $name, $label ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
*/ |
43
|
|
|
public function set_value_from_input( $input ) { |
44
|
|
|
if ( ! isset( $input[ $this->get_name() ] ) ) { |
45
|
|
|
return $this->set_value( array() ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$options_values = $this->get_options_values(); |
49
|
|
|
|
50
|
|
|
$value = stripslashes_deep( $input[ $this->get_name() ] ); |
51
|
|
|
$value = Helper::get_valid_options( $value, $options_values ); |
52
|
|
|
|
53
|
|
|
return $this->set_value( $value ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritDoc} |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function to_json( $load ) { |
60
|
|
|
$field_data = parent::to_json( $load ); |
61
|
|
|
|
62
|
|
|
$options = $this->parse_options( $this->get_options(), true ); |
63
|
|
|
$value = array_map( 'strval', $this->get_formatted_value() ); |
64
|
|
|
|
65
|
|
|
$field_data = array_merge( $field_data, array( |
66
|
|
|
'options' => $options, |
67
|
|
|
'value' => $value, |
68
|
|
|
'limit_options' => $this->limit_options, |
69
|
|
|
) ); |
70
|
|
|
|
71
|
|
|
return $field_data; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritDoc} |
76
|
|
|
*/ |
77
|
|
|
public function get_formatted_value() { |
78
|
|
|
$value = $this->get_value(); |
79
|
|
|
$value = $this->get_values_from_options( $value ); |
80
|
|
|
return $value; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|