1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carbon_Fields\Libraries\Sidebar_Manager; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This class is responsible for handling custom sidebars. |
7
|
|
|
*/ |
8
|
|
|
class Sidebar_Manager { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Singleton implementation. |
12
|
|
|
* |
13
|
|
|
* @return Sidebar_Manager |
14
|
|
|
*/ |
15
|
|
|
public static function instance() { |
16
|
|
|
// Store the instance locally to avoid private static replication. |
17
|
|
|
static $instance; |
18
|
|
|
|
19
|
|
|
if ( ! is_a( $instance, 'Sidebar_Manager' ) ) { |
20
|
|
|
$instance = new Sidebar_Manager; |
21
|
|
|
$instance->setup(); |
22
|
|
|
} |
23
|
|
|
return $instance; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Register actions, filters, etc... |
28
|
|
|
*/ |
29
|
|
|
private function setup() { |
30
|
|
|
// Register the custom sidebars |
31
|
|
|
add_action( 'widgets_init', array( $this, 'register_sidebars' ), 100 ); |
32
|
|
|
|
33
|
|
|
// Enqueue the UI scripts on the widgets page |
34
|
|
|
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
35
|
|
|
|
36
|
|
|
// Set the default options |
37
|
|
|
if ( function_exists( 'crb_get_default_sidebar_options' ) ) { |
38
|
|
|
add_filter( 'carbon_custom_sidebar_default_options', 'crb_get_default_sidebar_options', -1 ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// Ajax listeners |
42
|
|
|
add_action( 'wp_ajax_carbon_add_sidebar', array( $this, 'action_handler' ) ); |
43
|
|
|
add_action( 'wp_ajax_carbon_remove_sidebar', array( $this, 'action_handler' ) ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Handle action requests. |
48
|
|
|
* |
49
|
|
|
* @return array|void Output JSON if DOING_AJAX, otherwise return an array |
50
|
|
|
*/ |
51
|
|
|
public function action_handler() { |
52
|
|
|
$response = array( |
53
|
|
|
'success' => false, |
54
|
|
|
'error' => null, |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
if ( empty( $_POST['action'] ) || empty( $_POST['name'] ) ) { |
|
|
|
|
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$action = $_POST['action']; |
|
|
|
|
62
|
|
|
$name = $_POST['name']; |
|
|
|
|
63
|
|
|
$result = false; |
64
|
|
|
|
65
|
|
|
switch ( $action ) { |
66
|
|
|
case 'carbon_add_sidebar': |
67
|
|
|
$result = $this->add_sidebar( $name ); |
68
|
|
|
break; |
69
|
|
|
|
70
|
|
|
case 'carbon_remove_sidebar': |
71
|
|
|
$result = $this->remove_sidebar( $name ); |
72
|
|
|
break; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ( is_wp_error( $result ) ) { |
76
|
|
|
$response['error'] = $result->get_error_message(); |
77
|
|
|
} else { |
78
|
|
|
$response['success'] = (bool) $result; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
82
|
|
|
wp_send_json( $response ); |
83
|
|
|
} else { |
84
|
|
|
return $response; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Add a new custom sidebar. |
90
|
|
|
* |
91
|
|
|
* @see Sidebar_Manager::register_sidebars() |
92
|
|
|
* @param string $id Sidebar ID |
93
|
|
|
* @param string $name Sidebar Name |
94
|
|
|
* @return bool|WP_Error |
95
|
|
|
*/ |
96
|
|
|
public function add_sidebar( $name, $id = '' ) { |
97
|
|
|
$registered_sidebars = $this->get_sidebars(); |
98
|
|
|
$id = $id ? $id : $name; |
99
|
|
|
|
100
|
|
|
// Sanitize the sidebar ID the same way as dynamic_sidebar() |
101
|
|
|
$id = sanitize_title( $id ); |
102
|
|
|
|
103
|
|
|
if ( isset( $registered_sidebars[ $id ] ) ) { |
104
|
|
|
return new \WP_Error( 'sidebar-exists', __( 'Sidebar with the same ID is already registered.', 'carbon-fields' ) ); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$registered_sidebars[ $id ] = array( |
108
|
|
|
'id' => $id, |
109
|
|
|
'name' => $name, |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
return update_option( 'carbon_custom_sidebars', $registered_sidebars ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Remove a custom sidebar by ID. |
117
|
|
|
* |
118
|
|
|
* @see Sidebar_Manager::register_sidebars() |
119
|
|
|
* @param string $id Sidebar ID |
120
|
|
|
* @return bool|WP_Error |
121
|
|
|
*/ |
122
|
|
|
public function remove_sidebar( $id ) { |
123
|
|
|
$registered_sidebars = $this->get_sidebars(); |
124
|
|
|
|
125
|
|
|
// Sanitize the sidebar ID the same way as dynamic_sidebar() |
126
|
|
|
$id = sanitize_title( $id ); |
127
|
|
|
|
128
|
|
|
if ( isset( $registered_sidebars[ $id ] ) ) { |
129
|
|
|
unset( $registered_sidebars[ $id ] ); |
130
|
|
|
} else { |
131
|
|
|
return new \WP_Error( 'sidebar-not-found', __( 'Sidebar not found.', 'carbon-fields' ) ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return update_option( 'carbon_custom_sidebars', $registered_sidebars ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get all the registered custom sidebars. |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
public function get_sidebars() { |
143
|
|
|
return apply_filters( 'carbon_custom_sidebars', get_option( 'carbon_custom_sidebars', array() ) ); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Register the custom sidebars. |
148
|
|
|
*/ |
149
|
|
|
public function register_sidebars() { |
150
|
|
|
$registered_sidebars = $this->get_sidebars(); |
151
|
|
|
$default_options = apply_filters( 'carbon_custom_sidebar_default_options', array() ); |
152
|
|
|
|
153
|
|
|
foreach ( $registered_sidebars as $id => $options ) { |
154
|
|
|
$options['class'] = 'carbon-sidebar'; |
155
|
|
|
$options = wp_parse_args( $options, $default_options ); |
156
|
|
|
$options = apply_filters( 'carbon_custom_sidebar_options', $options, $id ); |
157
|
|
|
|
158
|
|
|
register_sidebar( $options ); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Enqueue the UI scripts. |
164
|
|
|
*/ |
165
|
|
|
public function enqueue_scripts() { |
166
|
|
|
wp_enqueue_style( 'carbon-sidebar-manager', \Carbon_Fields\URL . '/core/Libraries/Sidebar_Manager/assets/css/app.css' ); |
167
|
|
|
wp_enqueue_script( 'carbon-sidebar-manager', \Carbon_Fields\URL . '/core/Libraries/Sidebar_Manager/assets/js/app.js' ); |
168
|
|
|
wp_localize_script( 'carbon-sidebar-manager', 'crbSidebarl10n', |
169
|
|
|
array( |
170
|
|
|
'add_sidebar' => __( 'Add Sidebar', 'carbon-fields' ), |
171
|
|
|
'enter_name_of_new_sidebar' => __( 'Please enter the name of the new sidebar:', 'carbon-fields' ), |
172
|
|
|
'remove_sidebar_confirmation' => __( 'Are you sure you wish to remove this sidebar?', 'carbon-fields' ), |
173
|
|
|
) |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|