|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Field; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon_Fields\Exception\Incorrect_Syntax_Exception; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Choose sidebar field. |
|
9
|
|
|
* Deprecated in favor of the 'Sidebar' field and its sidebar manager. |
|
10
|
|
|
* |
|
11
|
|
|
* @deprecated |
|
12
|
|
|
*/ |
|
13
|
|
|
class Choose_Sidebar_Field extends Sidebar_Field { |
|
14
|
|
|
private $custom_sidebars = array(); |
|
15
|
|
|
private $sidebar_options = array(); |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Initialization tasks |
|
19
|
|
|
*/ |
|
20
|
|
|
public function init() { |
|
21
|
|
|
// Set default sidebar options |
|
22
|
|
|
$this->sidebar_options['default'] = $this->get_default_sidebar_options(); |
|
23
|
|
|
|
|
24
|
|
|
// Setup the sidebars after all fields are registered |
|
25
|
|
|
add_action( 'carbon_after_register_fields', array( $this, 'setup_sidebar_options' ), 20 ); |
|
26
|
|
|
add_action( 'carbon_after_register_fields', array( $this, 'register_custom_sidebars' ), 21 ); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Returns an array with the default sidebar options |
|
31
|
|
|
*/ |
|
32
|
|
|
public function get_default_sidebar_options() { |
|
33
|
|
|
$sidebar_options = array( |
|
34
|
|
|
'before_widget' => '<li id="%1$s" class="widget %2$s">', |
|
35
|
|
|
'after_widget' => '</li>', |
|
36
|
|
|
'before_title' => '<h2 class="widgettitle">', |
|
37
|
|
|
'after_title' => '</h2>', |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
if ( function_exists( 'crb_get_default_sidebar_options' ) ) { |
|
41
|
|
|
$sidebar_options = crb_get_default_sidebar_options(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return $sidebar_options; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Sets the Sidebar Options |
|
49
|
|
|
*/ |
|
50
|
|
|
public function set_sidebar_options( $sidebar_options ) { |
|
51
|
|
|
// Make sure that all needed fields are in the options array |
|
52
|
|
|
$required_arguments = array( 'before_widget', 'after_widget', 'before_title', 'after_title' ); |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set default settings |
|
56
|
|
|
*/ |
|
57
|
|
|
$sidebar_options_first_element = array_values( $sidebar_options ); |
|
58
|
|
|
$sidebar_options_first_element = $sidebar_options_first_element[0]; |
|
59
|
|
|
|
|
60
|
|
|
if ( ! is_array( $sidebar_options_first_element ) ) { |
|
61
|
|
|
/** |
|
62
|
|
|
* Enters here, if one array with settings is passed |
|
63
|
|
|
* Makes the passed settings, the default ones |
|
64
|
|
|
*/ |
|
65
|
|
|
$sidebar_options = array( |
|
66
|
|
|
'default' => $sidebar_options, |
|
67
|
|
|
); |
|
68
|
|
|
} elseif ( ! isset( $sidebar_options['default'] ) ) { |
|
69
|
|
|
/** |
|
70
|
|
|
* Enters here, if the default settings are not passed |
|
71
|
|
|
* Sets the default settings |
|
72
|
|
|
* |
|
73
|
|
|
* @see get_default_sidebar_options() |
|
74
|
|
|
*/ |
|
75
|
|
|
$sidebar_options['default'] = $this->get_default_sidebar_options(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Check if all required arguments are passed for each of the sidebars |
|
80
|
|
|
*/ |
|
81
|
|
|
foreach ( $sidebar_options as $options ) { |
|
82
|
|
|
foreach ( $required_arguments as $arg ) { |
|
83
|
|
|
if ( ! isset( $options[ $arg ] ) ) { |
|
84
|
|
|
throw new Incorrect_Syntax_Exception( |
|
85
|
|
|
'Provide all sidebar options for ' . $this->name . ': <code>' . |
|
86
|
|
|
implode( ', ', $required_arguments ) . '</code>' |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$this->sidebar_options = $sidebar_options; |
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Generate options for the choose sidebar field. |
|
98
|
|
|
*/ |
|
99
|
|
|
public function setup_sidebar_options() { |
|
100
|
|
|
global $wp_registered_sidebars; |
|
101
|
|
|
$custom_sidebars = $this->get_custom_sidebars(); |
|
102
|
|
|
|
|
103
|
|
|
// Add field options |
|
104
|
|
|
$sidebars = array(); |
|
105
|
|
|
|
|
106
|
|
|
foreach ( $wp_registered_sidebars as $sidebar ) { |
|
107
|
|
|
$sidebars[] = $sidebar['name']; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$options = array_merge( $sidebars, $custom_sidebars ); |
|
111
|
|
|
$options = array_combine( $options, $options ); |
|
112
|
|
|
$this->add_options( $options ); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Register all custom sidebars. |
|
117
|
|
|
*/ |
|
118
|
|
|
public function register_custom_sidebars() { |
|
119
|
|
|
$custom_sidebars = $this->get_custom_sidebars(); |
|
120
|
|
|
|
|
121
|
|
|
foreach ( $custom_sidebars as $sidebar ) { |
|
122
|
|
|
$slug = sanitize_title_with_dashes( $sidebar ); |
|
123
|
|
|
|
|
124
|
|
|
// Handles which options to use for the current sidebar |
|
125
|
|
|
if ( isset( $this->sidebar_options[ $slug ] ) ) { |
|
126
|
|
|
$sidebar_options = $this->sidebar_options[ $slug ]; |
|
127
|
|
|
} else { |
|
128
|
|
|
$sidebar_options = $this->sidebar_options['default']; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
// Registers the Sidebar |
|
132
|
|
|
register_sidebar( array_merge( $sidebar_options, array( |
|
133
|
|
|
'name' => $sidebar, |
|
134
|
|
|
'id' => $slug, |
|
135
|
|
|
) ) ); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Retrieve all custom sidebars from the database. |
|
141
|
|
|
* |
|
142
|
|
|
* @return array |
|
143
|
|
|
*/ |
|
144
|
|
|
public function get_custom_sidebars() { |
|
145
|
|
|
global $wpdb; |
|
146
|
|
|
|
|
147
|
|
|
if ( ! empty( $this->custom_sidebars ) ) { |
|
148
|
|
|
return $this->custom_sidebars; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$sidebars = array(); |
|
152
|
|
|
|
|
153
|
|
|
$query_string = ''; |
|
154
|
|
|
switch ( $this->context ) { |
|
155
|
|
|
case 'Post_Meta': |
|
156
|
|
|
$query_string = 'SELECT meta_value AS sidebar FROM ' . $wpdb->postmeta . ' WHERE meta_key = "' . esc_sql( $this->name ) . '"'; |
|
157
|
|
|
break; |
|
158
|
|
|
case 'Term_Meta': |
|
159
|
|
|
$query_string = 'SELECT meta_value AS sidebar FROM ' . $wpdb->termmeta . ' WHERE meta_key = "' . esc_sql( $this->name ) . '"'; |
|
160
|
|
|
break; |
|
161
|
|
|
case 'Theme_Options': |
|
162
|
|
|
$query_string = 'SELECT option_value AS sidebar FROM ' . $wpdb->options . ' WHERE option_name = "' . esc_sql( $this->name ) . '"'; |
|
163
|
|
|
break; |
|
164
|
|
|
case 'User_Meta': |
|
165
|
|
|
$query_string = 'SELECT meta_value AS sidebar FROM ' . $wpdb->usermeta . ' WHERE meta_key = "' . esc_sql( $this->name ) . '"'; |
|
|
|
|
|
|
166
|
|
|
break; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
$sidebar_names = array_filter( $wpdb->get_col( $query_string ) ); |
|
|
|
|
|
|
170
|
|
|
|
|
171
|
|
|
foreach ( $sidebar_names as $sidebar_name ) { |
|
172
|
|
|
$sidebars[ $sidebar_name ] = 1; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$this->custom_sidebars = array_keys( $sidebars ); |
|
176
|
|
|
|
|
177
|
|
|
return $this->custom_sidebars; |
|
178
|
|
|
} |
|
179
|
|
|
} |