-
Notifications
You must be signed in to change notification settings - Fork 1
/
class.sidebars.php
325 lines (251 loc) · 8.81 KB
/
class.sidebars.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
/**
* Whitelabel Sidebar Generator
* Used to create dynamic sidebars for your theme or plugin.
*
* @since Whitelabel Theme & Plugin Options 1.1
*
* @param string $url Base folder URI of the options page. This paramenter needs
* to be set for plugins, according with the plugins name. This parameter is
* optional for themes.
*
* @return void
*
*/
if ( ! class_exists( 'WhitelabelSidebars' ) ) {
class WhitelabelSidebars {
public $_prefix = 'white';
public function __construct() {
if ( is_admin() ) {
add_action('admin_enqueue_scripts', array($this, 'load_scripts'));
add_action('admin_menu', array($this, 'add_submenu_page'));
add_action('wp_ajax_update_sidebars', array($this, 'update_sidebars'));
add_action('add_meta_boxes', array($this, 'meta_box'));
add_action('save_post', array($this, 'save_meta_box_data'));
}
add_action('widgets_init', array($this, 'create_sidebars'));
add_shortcode('dynamic-sidebar', array($this, 'sidebar_shortcode'));
}
function load_scripts() {
if ( get_current_screen()->id == 'appearance_page_sidebars' ) {
global $_wp_admin_css_colors;
$admin_colors = $_wp_admin_css_colors;
$color_scheme = $admin_colors[get_user_option('admin_color')]->colors;
wp_register_style(
'curly-google-font-roboto',
'//fonts.googleapis.com/css?family=Roboto:400,300,700,900',
true
);
wp_register_style(
'curly-sidebars',
plugins_url('/enqueue-admin/css/sidebars.css', __FILE__ ),
null,
null,
null
);
wp_register_script(
'curly-sidebars',
plugins_url('/enqueue-admin/js/sidebars.js', __FILE__ ),
array('jquery'),
null,
true
);
if ( ! wp_script_is( 'curly-google-font-roboto', 'enqueued' ) ) {
wp_enqueue_style( 'curly-google-font-roboto' );
}
if ( ! wp_script_is( 'curly-google-font-roboto', 'enqueued' ) ) {
wp_enqueue_style( 'curly-sidebars' );
}
if ( ! wp_script_is( 'curly-google-font-roboto', 'enqueued' ) ) {
wp_enqueue_script( 'curly-sidebars' );
}
$js_data = array(
__('Remove','whitelabel'),
__('Are you sure you want to delete this sidebar?','whitelabel'),
__('Sidebar name cannot be empty. Please provide a valid name for your sidebar.','whitelabel'),
__('You already have a sidebar with that name. Please provide a valid name for your sidebar.','whitelabel'),
__('Your sidebar has been succesfully created.','whitelabel'),
__('You currently have no sidebars created. <br>Use the form above to create your first sidebar.','whitelabel')
);
wp_localize_script('curly-sidebars', 'js_data', $js_data);
$color_scheme = '
#sidebars-wrapper input[type=submit],
#sidebar-list li a:hover{
background-color: '.$color_scheme[3].';
color: #fff;
}';
wp_add_inline_style('curly-sidebars', $color_scheme);
}
}
function update_sidebars() {
$name = sanitize_text_field( $_POST['name'] );
$id = sanitize_text_field( $_POST['id'] );
$method = sanitize_text_field( $_POST['method'] );
$sidebars = $this->get_sidebars();
$count = $this->get_sidebars_count() + 1;
if ( $method == 'update' ) {
if ( !empty($name) ) {
if ( !$sidebars ) {
$sidebars = array( $count => $name );
$sidebars = json_encode($sidebars);
update_option( $this->_prefix . '_sidebars_list' , $sidebars );
update_option( $this->_prefix . '_sidebars_list_count' , $count );
echo json_encode( array( $count, $name ) );
} else {
if ( !in_array( $name , $sidebars ) ) {
$sidebars[$count] = $name ;
$sidebars = json_encode($sidebars);
update_option( $this->_prefix . '_sidebars_list' , $sidebars );
update_option( $this->_prefix . '_sidebars_list_count' , $count );
echo json_encode( array( $count, $name ) );
} else {
echo 'duplicate';
}
}
} else {
echo 'empty';
}
}
if ( $method == 'delete' ) {
unset( $sidebars[$id] );
$sidebars = json_encode($sidebars);
update_option( $this->_prefix . '_sidebars_list' , $sidebars );
echo 'success';
}
die();
}
function add_submenu_page(){
add_theme_page( __('Sidebars', 'whitelabel'), __('Sidebars', 'whitelabel'), 'edit_theme_options', 'sidebars', array($this, 'add_submenu_page_cb'));
}
function add_submenu_page_cb( $html = null ) {
$sidebars = $this->get_sidebars();
$html .= '<div id="sidebars-wrapper">';
$html .= '<h1>'.__('Sidebars', 'whitelabel').'</h1>';
$html .= '<form method="post" id="add-sidebar" action="">';
$html .= '<input type="text" id="add-sidebar-field" placeholder="'.__('Enter new sidebar name','whitelabel').'">';
$html .= '<input type="submit" id="add-sidebar-button" value="'.__('Add Sidebar','whitelabel').'">';
$html .= '</form>';
$html .= '<div id="messages"></div>';
$html .= '<h3>'.__('Sidebar List','whitelabel').'</h3>';
$html .= '<ul id="sidebar-list">';
if ( $sidebars ) {
foreach ($sidebars as $id => $name) {
$html .= '<li>'.$name.' <code>[dynamic-sidebar id="'.$id.'"]</code><a href="#" data-sidebar-id="'.$id.'">'.__('Remove','whitelabel').'</a></li>';
}
} else {
$html .= '<li id="no-sidebar">'.__('You currently have no sidebars created. <br>Use the form above to create your first sidebar.','whitelabel').'</li>';
}
$html .= '</ul>';
$html .= '</div>';
echo $html;
}
function get_sidebars() {
$sidebars = get_option( $this->_prefix . '_sidebars_list' );
$sidebars = json_decode( $sidebars , true);
return $sidebars;
}
function get_sidebars_count() {
$count = get_option( $this->_prefix . '_sidebars_list_count', 0 );
return $count;
}
function create_sidebars() {
$sidebars = $this->get_sidebars();
if ( $sidebars ) {
foreach ($sidebars as $id => $name) {
register_sidebar( array(
'name' => $name,
'id' => 'dynamic-sidebar-'.$id,
'before_widget' => '<aside id="%1$s" class="sidebar-widget %2$s curly_animated">',
'after_widget' => '</aside>',
'before_title' => '<h4 class="widget-title">',
'after_title' => '</h4>',
) );
}
}
}
public static function sidebar( $default = null, $logic = false, $return = false ) {
global $post;
$sidebar = get_post_meta( $post->ID, 'white_dynamic_sidebar', true );
if( $return === true ){
if ( is_active_sidebar( $sidebar ) ){
return $sidebar;
}
elseif( is_active_sidebar( $default ) ){
return $default;
}
else {
return;
}
} else {
if ( $logic === true ) {
if ( $sidebar && is_active_sidebar( $sidebar ) ) {
dynamic_sidebar( $sidebar );
} elseif( is_active_sidebar( $default ) ) {
dynamic_sidebar( $default );
} else {
return;
}
} else {
if ( $sidebar ) {
dynamic_sidebar( $sidebar );
} else {
dynamic_sidebar( $default );
}
}
}
}
function sidebar_shortcode( $atts ) {
ob_start();
dynamic_sidebar( 'sidebar_'.$atts['id'] );
$sidebar = ob_get_contents();
ob_end_clean();
return $sidebar;
}
public function meta_box() {
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
add_meta_box('sidebar_metabox', __( 'Sidebar', 'whitelabel' ), array($this, 'meta_box_callback'), $screen, 'side');
}
}
public function meta_box_callback( $post ) {
wp_nonce_field( 'sidebar_meta_box', 'sidebar_meta_box_nonce' );
$default_sidebar = get_post_meta( $post->ID, $this->_prefix . '_dynamic_sidebar', true );
global $wp_registered_sidebars;
echo '<p><strong><label>'.__('Choose Sidebar:','whitelabel').'</label></strong></p>';
echo '<select name="sidebar" id="sidebar">';
echo '<option value="">'.__('Choose Sidebar','whitelabel').'</option>';
foreach ( $wp_registered_sidebars as $value ) {
echo '<option value="'.$value['id'].'" '.selected($default_sidebar, $value['id']).'>'.$value['name'].'</option>';
}
echo '</select>';
echo '<p>'.__('Choose a custom sidebar for this page','whitelabel').'</p>';
}
public function save_meta_box_data( $post_id ) {
if ( ! isset( $_POST['sidebar_meta_box_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['sidebar_meta_box_nonce'], 'sidebar_meta_box' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
if ( ! isset( $_POST['sidebar'] ) ) {
return;
}
$data = sanitize_text_field( $_POST['sidebar'] );
update_post_meta( $post_id, $this->_prefix . '_dynamic_sidebar', $data );
}
}
$sidebars = new WhitelabelSidebars();
}
?>