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