|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Field; |
|
4
|
|
|
|
|
5
|
|
|
class Sidebar_Field extends Select_Field { |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Allow the user to add new sidebars |
|
9
|
|
|
* |
|
10
|
|
|
* @var boolean |
|
11
|
|
|
*/ |
|
12
|
|
|
private $enable_add_new = true; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Array of sidebars to exclude from the select menu |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
private $excluded_sidebars = array(); |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Disable adding new sidebars. |
|
23
|
|
|
* |
|
24
|
|
|
* @return Field $this |
|
25
|
|
|
*/ |
|
26
|
|
|
public function disable_add_new() { |
|
27
|
|
|
$this->enable_add_new = false; |
|
28
|
|
|
return $this; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Specify sidebars to be excluded. |
|
33
|
|
|
* |
|
34
|
|
|
* @param array $sidebars |
|
35
|
|
|
* @return Field $this |
|
36
|
|
|
*/ |
|
37
|
|
|
public function set_excluded_sidebars( $sidebars ) { |
|
38
|
|
|
$this->excluded_sidebars = $sidebars; |
|
39
|
|
|
return $this; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Returns an array that holds the field data, suitable for JSON representation. |
|
44
|
|
|
* |
|
45
|
|
|
* @param bool $load Should the value be loaded from the database or use the value from the current instance. |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public function to_json( $load ) { |
|
49
|
|
|
if ( $this->enable_add_new ) { |
|
50
|
|
|
$this->add_options( array( 'new' => _x( 'Add New', 'sidebar', 'carbon-fields' ) ) ); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$field_data = parent::to_json( $load ); |
|
54
|
|
|
|
|
55
|
|
|
// override default value assigning behavior since sidebars are |
|
56
|
|
|
// loaded separately and not as a part of the field options |
|
57
|
|
|
$field_data = array_merge( $field_data, array( |
|
58
|
|
|
'value' => $this->get_formatted_value(), |
|
59
|
|
|
) ); |
|
60
|
|
|
|
|
61
|
|
|
if ( ! empty( $this->excluded_sidebars ) ) { |
|
62
|
|
|
if ( ! is_array( $this->excluded_sidebars ) ) { |
|
63
|
|
|
$this->excluded_sidebars = array( $this->excluded_sidebars ); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$field_data = array_merge( $field_data, array( |
|
67
|
|
|
'excluded_sidebars' => $this->excluded_sidebars, |
|
68
|
|
|
) ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $field_data; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|