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