PluginProbe ʕ •ᴥ•ʔ
Custom Sidebars – Dynamic Sidebar Classic Widget Area Manager / 3.0.7.1
Custom Sidebars – Dynamic Sidebar Classic Widget Area Manager v3.0.7.1
trunk 2.1.2.0 3.0.0.0 3.0.0.1 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.7.1 3.0.8 3.0.8.1 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.3 3.31 3.32 3.35 3.36 3.37 3.38
custom-sidebars / inc / class-custom-sidebars-editor.php
custom-sidebars / inc Last commit date
external 9 years ago class-custom-sidebars-checkup-notification.php 9 years ago class-custom-sidebars-cloning.php 9 years ago class-custom-sidebars-editor.php 9 years ago class-custom-sidebars-explain.php 9 years ago class-custom-sidebars-export.php 9 years ago class-custom-sidebars-replacer.php 9 years ago class-custom-sidebars-visibility.php 9 years ago class-custom-sidebars-widgets.php 9 years ago class-custom-sidebars.php 9 years ago
class-custom-sidebars-editor.php
1141 lines
1 <?php
2
3 add_action( 'cs_init', array( 'CustomSidebarsEditor', 'instance' ) );
4
5 /**
6 * Provides all the functionality for editing sidebars on the widgets page.
7 */
8 class CustomSidebarsEditor extends CustomSidebars {
9
10 /**
11 * Returns the singleton object.
12 *
13 * @since 2.0
14 */
15 public static function instance() {
16 static $Inst = null;
17
18 if ( null === $Inst ) {
19 $Inst = new CustomSidebarsEditor();
20 }
21
22 return $Inst;
23 }
24
25 /**
26 * Constructor is private -> singleton.
27 *
28 * @since 2.0
29 */
30 private function __construct() {
31 if ( is_admin() ) {
32 // Add the sidebar metabox to posts.
33 add_action(
34 'add_meta_boxes',
35 array( $this, 'add_meta_box' )
36 );
37
38 // Save the options from the sidebars-metabox.
39 add_action(
40 'save_post',
41 array( $this, 'store_replacements' )
42 );
43
44 // Handle ajax requests.
45 add_action(
46 'cs_ajax_request',
47 array( $this, 'handle_ajax' )
48 );
49
50 // Add a custom column to post list.
51 $posttypes = self::get_post_types( 'objects' );
52 foreach ( $posttypes as $pt ) {
53 add_filter(
54 'manage_' . $pt->name . '_posts_columns',
55 array( $this, 'post_columns' )
56 );
57
58 add_action(
59 'manage_' . $pt->name . '_posts_custom_column',
60 array( $this, 'post_column_content' ),
61 10, 2
62 );
63 }
64 /** This action is documented in wp-admin/includes/screen.php */
65 add_filter( 'default_hidden_columns', array( $this, 'default_hidden_columns' ), 10, 2 );
66
67 add_action(
68 'quick_edit_custom_box',
69 array( $this, 'post_quick_edit' ),
70 10, 2
71 );
72
73 add_action(
74 'admin_footer',
75 array( $this, 'post_quick_edit_js' )
76 );
77
78 }
79 }
80
81 /**
82 * Handles the ajax requests.
83 */
84 public function handle_ajax( $action ) {
85 $req = (object) array(
86 'status' => 'ERR',
87 );
88 $is_json = true;
89 $handle_it = false;
90 $view_file = '';
91 $sb_id = '';
92
93 if ( isset( $_POST['sb'] ) ) {
94 $sb_id = $_POST['sb'];
95 }
96
97 switch ( $action ) {
98 case 'get':
99 case 'save':
100 case 'delete':
101 case 'get-location':
102 case 'set-location':
103 case 'replaceable':
104 $handle_it = true;
105 $req->status = 'OK';
106 $req->action = $action;
107 $req->id = $sb_id;
108 break;
109 }
110
111 // The ajax request was not meant for us...
112 if ( ! $handle_it ) {
113 return false;
114 }
115
116 $sb_data = self::get_sidebar( $sb_id );
117
118 if ( ! current_user_can( self::$cap_required ) ) {
119 $req = self::req_err(
120 $req,
121 __( 'You do not have permission for this', 'custom-sidebars' )
122 );
123 } else {
124 switch ( $action ) {
125 // Return details for the specified sidebar.
126 case 'get':
127 $req->sidebar = $sb_data;
128 break;
129
130 // Save or insert the specified sidebar.
131 case 'save':
132 $req = $this->save_item( $req, $_POST );
133 break;
134
135 // Delete the specified sidebar.
136 case 'delete':
137 $req->sidebar = $sb_data;
138 $req = $this->delete_item( $req );
139 break;
140
141 // Get the location data.
142 case 'get-location':
143 $req->sidebar = $sb_data;
144 $req = $this->get_location_data( $req );
145 break;
146
147 // Update the location data.
148 case 'set-location':
149 $req->sidebar = $sb_data;
150 $req = $this->set_location_data( $req );
151 break;
152
153 // Toggle theme sidebar replaceable-flag.
154 case 'replaceable':
155 $req = $this->set_replaceable( $req );
156 break;
157 }
158 }
159
160 // Make the ajax response either as JSON or plain text.
161 if ( $is_json ) {
162 self::json_response( $req );
163 } else {
164 ob_start();
165 include CSB_VIEWS_DIR . $view_file;
166 $resp = ob_get_clean();
167
168 self::plain_response( $resp );
169 }
170 }
171
172 /**
173 * Saves the item specified by $data array and populates the response
174 * object. When $req->id is empty a new sidebar will be created. Otherwise
175 * the existing sidebar is updated.
176 *
177 * @since 2.0
178 * @param object $req Initial response object.
179 * @param array $data Sidebar data to save (typically this is $_POST).
180 * @return object Updated response object.
181 */
182 private function save_item( $req, $data ) {
183 $sidebars = self::get_custom_sidebars();
184 $sb_id = $req->id;
185 $sb_desc = stripslashes( trim( @$_POST['description'] ) );
186
187 if ( function_exists( 'mb_substr' ) ) {
188 $sb_name = mb_substr( stripslashes( trim( @$data['name'] ) ), 0, 40 );
189 } else {
190 $sb_name = substr( stripslashes( trim( @$data['name'] ) ), 0, 40 );
191 }
192
193 if ( empty( $sb_name ) ) {
194 return self::req_err(
195 $req,
196 __( 'Sidebar-name cannot be empty', 'custom-sidebars' )
197 );
198 }
199
200 if ( empty( $sb_id ) ) {
201 // Create a new sidebar.
202 $action = 'insert';
203 $num = count( $sidebars );
204 do {
205 $num += 1;
206 $sb_id = self::$sidebar_prefix . $num;
207 } while ( self::get_sidebar( $sb_id, 'cust' ) );
208
209 $sidebar = array(
210 'id' => $sb_id,
211 );
212 } else {
213 // Update existing sidebar
214 $action = 'update';
215 $sidebar = self::get_sidebar( $sb_id, 'cust' );
216
217 if ( ! $sidebar ) {
218 return self::req_err(
219 $req,
220 __( 'The sidebar does not exist', 'custom-sidebars' )
221 );
222 }
223 }
224
225 if ( function_exists( 'mb_strlen' ) ) {
226 if ( mb_strlen( $sb_desc ) > 200 ) {
227 $sb_desc = mb_substr( $sb_desc, 0, 200 );
228 }
229 } else {
230 if ( strlen( $sb_desc ) > 200 ) {
231 $sb_desc = substr( $sb_desc, 0, 200 );
232 }
233 }
234
235 // Populate the sidebar object.
236 if ( 'insert' == $action || self::wpml_is_default_lang() ) {
237 $sidebar['name'] = $sb_name;
238 $sidebar['description'] = $sb_desc;
239 } else {
240 $sidebar['name_lang'] = $sb_name;
241 $sidebar['description_lang'] = $sb_desc;
242 }
243 $sidebar['before_widget'] = stripslashes( trim( @$_POST['before_widget'] ) );
244 $sidebar['after_widget'] = stripslashes( trim( @$_POST['after_widget'] ) );
245 $sidebar['before_title'] = stripslashes( trim( @$_POST['before_title'] ) );
246 $sidebar['after_title'] = stripslashes( trim( @$_POST['after_title'] ) );
247
248 if ( 'insert' == $action ) {
249 $sidebars[] = $sidebar;
250 $req->message = sprintf(
251 __( 'Created new sidebar <strong>%1$s</strong>', 'custom-sidebars' ),
252 esc_html( $sidebar['name'] )
253 );
254 } else {
255 $found = false;
256 foreach ( $sidebars as $ind => $item ) {
257 if ( $item['id'] == $sb_id ) {
258 $req->message = sprintf(
259 __( 'Updated sidebar <strong>%1$s</strong>', 'custom-sidebars' ),
260 esc_html( $sidebar['name'] )
261 );
262 $sidebars[ $ind ] = $sidebar;
263 $found = true;
264 break;
265 }
266 }
267 if ( ! $found ) {
268 return self::req_err(
269 $req,
270 __( 'The sidebar was not found', 'custom-sidebars' )
271 );
272 }
273 }
274
275 // Save the changes.
276 self::set_custom_sidebars( $sidebars );
277 self::refresh_sidebar_widgets();
278
279 $req->data = $sidebar;
280 $req->action = $action;
281
282 // Allow user to translate sidebar name/description via WPML.
283 self::wpml_update( $sidebars );
284 $req->data = self::wpml_translate( $sidebar );
285
286 return $req;
287 }
288
289 /**
290 * Delete the specified sidebar and update the response object.
291 *
292 * @since 2.0
293 * @param object $req Initial response object.
294 * @return object Updated response object.
295 */
296 private function delete_item( $req ) {
297 $sidebars = self::get_custom_sidebars();
298 $sidebar = self::get_sidebar( $req->id, 'cust' );
299
300 if ( ! $sidebar ) {
301 return self::req_err(
302 $req,
303 __( 'The sidebar does not exist', 'custom-sidebars' )
304 );
305 }
306
307 $found = false;
308 foreach ( $sidebars as $ind => $item ) {
309 if ( $item['id'] == $req->id ) {
310 $found = true;
311 $req->message = sprintf(
312 __( 'Deleted sidebar <strong>%1$s</strong>', 'custom-sidebars' ),
313 esc_html( $req->sidebar['name'] )
314 );
315 unset( $sidebars[ $ind ] );
316 break;
317 }
318 }
319
320 if ( ! $found ) {
321 return self::req_err(
322 $req,
323 __( 'The sidebar was not found', 'custom-sidebars' )
324 );
325 }
326
327 // Save the changes.
328 self::set_custom_sidebars( $sidebars );
329 self::refresh_sidebar_widgets();
330
331 return $req;
332 }
333
334 /**
335 * Save the repaceable flag of a theme sidebar.
336 *
337 * @since 2.0
338 * @param object $req Initial response object.
339 * @return object Updated response object.
340 */
341 private function set_replaceable( $req ) {
342 $state = @$_POST['state'];
343
344 $options = self::get_options();
345 if ( 'true' === $state ) {
346 $req->status = true;
347 if ( ! in_array( $req->id, $options['modifiable'] ) ) {
348 $options['modifiable'][] = $req->id;
349 }
350 } else {
351 $req->status = false;
352 foreach ( $options['modifiable'] as $i => $sb_id ) {
353 if ( $sb_id == $req->id ) {
354 unset( $options['modifiable'][ $i ] );
355 break;
356 }
357 }
358 }
359 $options['modifiable'] = array_values( $options['modifiable'] );
360 self::set_options( $options );
361 $req->replaceable = (object) $options['modifiable'];
362
363 return $req;
364 }
365
366 /**
367 * Populates the response object for the "get-location" ajax call.
368 * Location data defines where a custom sidebar is displayed, i.e. on which
369 * pages it is used and which theme-sidebars are replaced.
370 *
371 * @since 2.0
372 * @param object $req Initial response object.
373 * @return object Updated response object.
374 */
375 private function get_location_data( $req ) {
376 $defaults = self::get_options();
377 $raw_posttype = self::get_post_types( 'objects' );
378 $raw_cat = self::get_all_categories();
379 $raw_taxonomies = array(
380 '_builtin' => self::get_taxonomies( 'objects', true ),
381 'custom' => self::get_taxonomies( 'objects', false ),
382 );
383
384 $archive_type = array(
385 '_blog' => __( 'Front Page', 'custom-sidebars' ),
386 '_search' => __( 'Search Results', 'custom-sidebars' ),
387 '_404' => __( 'Not found (404)', 'custom-sidebars' ),
388 '_authors' => __( 'Any Author Archive', 'custom-sidebars' ),
389 '_date' => __( 'Date Archives', 'custom-sidebars' ),
390 );
391
392 /**
393 * taxonomies
394 *
395 * @since 3.0.7
396 */
397 $default_taxonomies = array();
398 foreach ( $raw_taxonomies['_builtin'] as $taxonomy ) {
399 $default_taxonomies[] = $taxonomy->labels->singular_name;
400 switch ( $taxonomy->name ) {
401 case 'post_format':
402 break;
403 case 'post_tag':
404 /**
405 * this a legacy and backward compatibility
406 */
407 $archive_type['_tags'] = sprintf( __( '%s Archives', 'custom-sidebars' ), $taxonomy->labels->singular_name );
408 break;
409 case 'category':
410 $archive_type[ '_'.$taxonomy->name ] = sprintf( __( '%s Archives', 'custom-sidebars' ), $taxonomy->labels->singular_name );
411 break;
412 }
413 }
414 foreach ( $raw_taxonomies['custom'] as $taxonomy ) {
415 if ( in_array( $taxonomy->labels->singular_name, $default_taxonomies ) ) {
416 $archive_type[ '_taxonomy_'.$taxonomy->name ] = sprintf( __( '%s Archives', 'custom-sidebars' ), ucfirst( $taxonomy->name ) );
417 } else {
418 $archive_type[ '_taxonomy_'.$taxonomy->name ] = sprintf( __( '%s Archives', 'custom-sidebars' ), $taxonomy->labels->singular_name );
419 }
420 }
421
422 /**
423 * sort array by values
424 */
425 asort( $archive_type );
426
427 $raw_authors = array();
428 $raw_authors = get_users(
429 array(
430 'order_by' => 'display_name',
431 'fields' => array( 'ID', 'display_name' ),
432 'who' => 'authors',
433 )
434 );
435
436 // Collect required data for all posttypes.
437 $posttypes = array();
438 foreach ( $raw_posttype as $item ) {
439 $sel_single = @$defaults['post_type_single'][ $item->name ];
440
441 $posttypes[ $item->name ] = array(
442 'name' => $item->labels->name,
443 'single' => self::get_array( $sel_single ),
444 );
445 }
446
447 // Extract the data from categories list that we need.
448 $categories = array();
449 foreach ( $raw_cat as $item ) {
450 $sel_single = @$defaults['category_single'][ $item->term_id ];
451 $sel_archive = @$defaults['category_archive'][ $item->term_id ];
452
453 $categories[ $item->term_id ] = array(
454 'name' => $item->name,
455 'count' => $item->count,
456 'single' => self::get_array( $sel_single ),
457 'archive' => self::get_array( $sel_archive ),
458 );
459 }
460
461 // Build a list of archive types.
462 $archives = array(); // Start with a copy of the posttype list.
463 foreach ( $raw_posttype as $item ) {
464 if ( $item->name == 'post' ) {
465 $label = __( 'Post Index', 'custom-sidebars' );
466 } else {
467 if ( ! $item->has_archive ) { continue; }
468 $label = sprintf(
469 __( '%1$s Archives', 'custom-sidebars' ),
470 $item->labels->singular_name
471 );
472 }
473
474 $sel_archive = @$defaults['post_type_archive'][ $item->name ];
475
476 $archives[ $item->name ] = array(
477 'name' => $label,
478 'archive' => self::get_array( $sel_archive ),
479 );
480 }
481
482 foreach ( $archive_type as $key => $name ) {
483 $sel_archive = @$defaults[ substr( $key, 1 ) ];
484
485 $archives[ $key ] = array(
486 'name' => $name,
487 'archive' => self::get_array( $sel_archive ),
488 );
489 }
490
491 /**
492 * Custom taxonomies
493 *
494 * @since 3.0.7
495 */
496 foreach ( $raw_taxonomies['custom'] as $t ) {
497 $taxonomy = $t->name;
498 if (
499 isset( $defaults['taxonomies_archive'] )
500 && isset( $defaults['taxonomies_archive'][ $taxonomy ] )
501 ) {
502 $name = sprintf( __( '%s Archives', 'custom-sidebars' ), $t->labels->singular_name );
503 if ( in_array( $t->labels->singular_name, $default_taxonomies ) ) {
504 $name = sprintf( __( '%s Archives', 'custom-sidebars' ), ucfirst( $taxonomy ) );
505 }
506 $sel_archive = $defaults['taxonomies_archive'][ $taxonomy ];
507 $key = '_taxonomy_'.$taxonomy;
508 $archives[ $key ] = array(
509 'name' => $name,
510 'archive' => self::get_array( $sel_archive ),
511 );
512 }
513 }
514
515 /**
516 * Category archive.
517 */
518 foreach ( $raw_taxonomies['_builtin'] as $t ) {
519 if ( 'category' == $t->name ) {
520 if ( isset( $defaults['category_archive'] ) ) {
521 $sel_archive = $defaults['category_archive'];
522 $archives[ $key ] = array(
523 'name' => sprintf( __( '%s Archives', 'custom-sidebars' ), $t->labels->singular_name ),
524 'archive' => self::get_array( $sel_archive ),
525 );
526 }
527 }
528 }
529
530 // Build a list of authors.
531 $authors = array();
532 foreach ( $raw_authors as $user ) {
533 $sel_archive = @$defaults['author_archive'][ @$user->ID ];
534
535 $authors[ @$user->ID ] = array(
536 'name' => @$user->display_name,
537 'archive' => self::get_array( $sel_archive ),
538 );
539 }
540 $req->authors = $authors;
541
542 $req->replaceable = $defaults['modifiable'];
543 $req->posttypes = $posttypes;
544 $req->categories = $categories;
545 $req->archives = $archives;
546 return $req;
547 }
548
549 /**
550 * Save location data for a single sidebar and populate the response object.
551 * Location data defines where a custom sidebar is displayed, i.e. on which
552 * pages it is used and which theme-sidebars are replaced.
553 *
554 * @since 2.0
555 * @param object $req Initial response object.
556 * @return object Updated response object.
557 */
558 private function set_location_data( $req ) {
559 $options = self::get_options();
560 $sidebars = $options['modifiable'];
561 $raw_posttype = self::get_post_types( 'objects' );
562 $raw_cat = self::get_all_categories();
563 $raw_taxonomies = self::get_taxonomies();
564 $data = array();
565
566 foreach ( $_POST as $key => $value ) {
567 if ( strlen( $key ) > 8 && '___cs___' == substr( $key, 0, 8 ) ) {
568 list( $prefix, $id ) = explode( '___', substr( $key, 8 ) );
569
570 if ( ! isset( $data[ $prefix ] ) ) {
571 $data[ $prefix ] = array();
572 }
573 $data[ $prefix ][ $id ] = $value;
574 }
575 }
576
577 $special_arc = array(
578 'blog',
579 '404',
580 'tags',
581 'authors',
582 'search',
583 'date',
584 );
585
586 $raw_authors = array();
587 $raw_authors = get_users(
588 array(
589 'order_by' => 'display_name',
590 'fields' => array( 'ID', 'display_name' ),
591 'who' => 'authors',
592 )
593 );
594
595 // == Update the options
596
597 foreach ( $sidebars as $sb_id ) {
598 // Post-type settings.
599 foreach ( $raw_posttype as $item ) {
600 $pt = $item->name;
601 if (
602 is_array( @$data['pt'][ $sb_id ] ) &&
603 in_array( $pt, $data['pt'][ $sb_id ] )
604 ) {
605 $options['post_type_single'][ $pt ][ $sb_id ] = $req->id;
606 } elseif (
607 isset( $options['post_type_single'][ $pt ][ $sb_id ] ) &&
608 $options['post_type_single'][ $pt ][ $sb_id ] == $req->id
609 ) {
610 unset( $options['post_type_single'][ $pt ][ $sb_id ] );
611 }
612
613 if (
614 is_array( @$data['arc'][ $sb_id ] ) &&
615 in_array( $pt, $data['arc'][ $sb_id ] )
616 ) {
617 $options['post_type_archive'][ $pt ][ $sb_id ] = $req->id;
618 } elseif (
619 isset( $options['post_type_archive'][ $pt ][ $sb_id ] ) &&
620 $options['post_type_archive'][ $pt ][ $sb_id ] == $req->id
621 ) {
622 unset( $options['post_type_archive'][ $pt ][ $sb_id ] );
623 }
624 }
625
626 // Category settings.
627 foreach ( $raw_cat as $item ) {
628 $cat = $item->term_id;
629 if (
630 is_array( @$data['cat'][ $sb_id ] ) &&
631 in_array( $cat, $data['cat'][ $sb_id ] )
632 ) {
633 $options['category_single'][ $cat ][ $sb_id ] = $req->id;
634 } elseif (
635 isset( $options['category_single'][ $cat ][ $sb_id ] ) &&
636 $options['category_single'][ $cat ][ $sb_id ] == $req->id
637 ) {
638 unset( $options['category_single'][ $cat ][ $sb_id ] );
639 }
640
641 if (
642 is_array( @$data['arc-cat'][ $sb_id ] ) &&
643 in_array( $cat, $data['arc-cat'][ $sb_id ] )
644 ) {
645 $options['category_archive'][ $cat ][ $sb_id ] = $req->id;
646 } elseif (
647 isset( $options['category_archive'][ $cat ][ $sb_id ] ) &&
648 $options['category_archive'][ $cat ][ $sb_id ] == $req->id
649 ) {
650 unset( $options['category_archive'][ $cat ][ $sb_id ] );
651 }
652 }
653
654 foreach ( $special_arc as $key ) {
655 if (
656 is_array( @$data['arc'][ $sb_id ] ) &&
657 in_array( '_' . $key, $data['arc'][ $sb_id ] )
658 ) {
659 $options[ $key ][ $sb_id ] = $req->id;
660 } elseif (
661 isset( $options[ $key ][ $sb_id ] ) &&
662 $options[ $key ][ $sb_id ] == $req->id
663 ) {
664 unset( $options[ $key ][ $sb_id ] );
665 }
666 }
667
668 // Author settings.
669 foreach ( $raw_authors as $user ) {
670 $key = $user->ID;
671 if (
672 is_array( @$data['arc-aut'][ $sb_id ] ) &&
673 in_array( $key, $data['arc-aut'][ $sb_id ] )
674 ) {
675 $options['author_archive'][ $key ][ $sb_id ] = $req->id;
676 } elseif (
677 isset( $options['author_archive'][ $key ][ $sb_id ] ) &&
678 $options['author_archive'][ $key ][ $sb_id ] == $req->id
679 ) {
680 unset( $options['author_archive'][ $key ][ $sb_id ] );
681 }
682 }
683
684 /**
685 * Custom taxonomies
686 *
687 * @since 3.0.7
688 */
689 foreach ( $raw_taxonomies as $taxonomy ) {
690 $key = '_taxonomy_'.$taxonomy;
691 if (
692 isset( $data['arc'][ $sb_id ] )
693 && is_array( $data['arc'][ $sb_id ] )
694 && in_array( $key, $data['arc'][ $sb_id ] )
695 ) {
696 $options['taxonomies_archive'][ $taxonomy ][ $sb_id ] = $req->id;
697 } elseif (
698 isset( $options['taxonomies_archive'][ $key ][ $sb_id ] ) &&
699 $options['taxonomies_archive'][ $key ][ $sb_id ] == $req->id
700 ) {
701 unset( $options['taxonomies_archive'][ $taxonomy ][ $sb_id ] );
702 }
703 }
704 /**
705 * category Archive
706 *
707 * @since 3.0.7
708 */
709 if (
710 isset( $data['arc'][ $sb_id ] )
711 && is_array( $data['arc'][ $sb_id ] )
712 && in_array( '_category', $data['arc'][ $sb_id ] )
713 ) {
714 $options['category_archive'][ $sb_id ] = $req->id;
715 } elseif (
716 isset( $options['category_archive']['_category'][ $sb_id ] ) &&
717 $options['category_archive']['category_archive'][ $sb_id ] == $req->id
718 ) {
719 unset( $options['category_archive'][ $sb_id ] );
720 }
721 }
722
723 $req->message = sprintf(
724 __( 'Updated sidebar <strong>%1$s</strong> settings.', 'custom-sidebars' ),
725 esc_html( $req->sidebar['name'] )
726 );
727 self::set_options( $options );
728 return $req;
729 }
730
731 /**
732 * Registers the "Sidebars" meta box in the post-editor.
733 */
734 public function add_meta_box() {
735 global $post;
736
737 $post_type = get_post_type( $post );
738 if ( ! $post_type ) { return false; }
739 if ( ! self::supported_post_type( $post_type ) ) { return false; }
740
741 /**
742 * Option that can be set in wp-config.php to remove the custom sidebar
743 * meta box for certain post types.
744 *
745 * @since 2.0
746 *
747 * @option bool TRUE will hide all meta boxes.
748 */
749 if (
750 defined( 'CUSTOM_SIDEBAR_DISABLE_METABOXES' ) &&
751 CUSTOM_SIDEBAR_DISABLE_METABOXES == true
752 ) {
753 return false;
754 }
755
756 $pt_obj = get_post_type_object( $post_type );
757 if ( $pt_obj->publicly_queryable || $pt_obj->public ) {
758 add_meta_box(
759 'customsidebars-mb',
760 __( 'Sidebars', 'custom-sidebars' ),
761 array( $this, 'print_metabox_editor' ),
762 $post_type,
763 'side'
764 );
765 }
766 }
767
768 /**
769 * Renders the Custom Sidebars meta box in the post-editor.
770 */
771 public function print_metabox_editor() {
772 global $post;
773 $this->print_sidebars_form( $post->ID, 'metabox' );
774 }
775
776 /**
777 * Renders the sidebar-fields inside the quick-edit form.
778 */
779 public function print_metabox_quick() {
780 $this->print_sidebars_form( 0, 'quick-edit' );
781 }
782
783 /**
784 * Renders the Custom Sidebars form.
785 *
786 * @param int $post_id The post-ID to display
787 * @param string $type Which form to display. 'metabox/quick-edit/col-sidebars'.
788 */
789 protected function print_sidebars_form( $post_id, $type = 'metabox' ) {
790 global $wp_registered_sidebars;
791 $available = CustomSidebars::sort_sidebars_by_name( $wp_registered_sidebars );
792 $replacements = self::get_replacements( $post_id );
793 $sidebars = self::get_options( 'modifiable' );
794 $selected = array();
795 if ( ! empty( $sidebars ) ) {
796 foreach ( $sidebars as $s ) {
797 if ( isset( $replacements[ $s ] ) ) {
798 $selected[ $s ] = $replacements[ $s ];
799 } else {
800 $selected[ $s ] = '';
801 }
802 }
803 }
804
805 switch ( $type ) {
806 case 'col-sidebars':
807 include CSB_VIEWS_DIR . 'col-sidebars.php';
808 break;
809
810 case 'quick-edit':
811 include CSB_VIEWS_DIR . 'quick-edit.php';
812 break;
813
814 default:
815 include CSB_VIEWS_DIR . 'metabox.php';
816 break;
817 }
818 }
819
820 public function store_replacements( $post_id ) {
821 global $action;
822
823 if ( ! current_user_can( self::$cap_required ) ) {
824 return;
825 }
826
827 /*
828 * Verify if this is an auto save routine. If it is our form has not
829 * been submitted, so we dont want to do anything
830 * (Copied and pasted from wordpress add_metabox_tutorial)
831 */
832 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
833 return $post_id;
834 }
835
836 /*
837 * 'editpost' .. Saved from full Post-Editor screen.
838 * 'inline-save' .. Saved via the quick-edit form.
839 * We do not (yet) offer a bulk-editing option for custom sidebars.
840 */
841 if ( ( isset( $_POST['action'] ) && 'inline-save' == $_POST['action'] ) || 'editpost' != $action ) {
842 return $post_id;
843 }
844
845 // Make sure meta is added to the post, not a revision.
846 if ( $the_post = wp_is_post_revision( $post_id ) ) {
847 $post_id = $the_post;
848 }
849
850 $sidebars = self::get_options( 'modifiable' );
851 $data = array();
852 if ( ! empty( $sidebars ) ) {
853 foreach ( $sidebars as $sb_id ) {
854 if ( isset( $_POST[ 'cs_replacement_' . $sb_id ] ) ) {
855 $replacement = $_POST[ 'cs_replacement_' . $sb_id ];
856 if ( ! empty( $replacement ) ) {
857 $data[ $sb_id ] = $replacement;
858 }
859 }
860 }
861 }
862
863 self::set_post_meta( $post_id, $data );
864 }
865
866 // ========== WPML support.
867
868 /**
869 * Updates the WPML string register with the current sidebar string so the
870 * user can translate the sidebar details using the WPML string translation.
871 *
872 * @since 2.0.9.7
873 * @param array $custom_sidebars List of the custom sidebars.
874 */
875 static protected function wpml_update( $custom_sidebars ) {
876 if ( ! function_exists( 'icl_register_string' ) ) { return false; }
877
878 $theme_sidebars = self::get_sidebars();
879
880 // This is used to identify the sidebar-translations by WPML.
881 $context = 'Sidebar';
882
883 // First do the theme sidebars, so they will be displayed in the
884 // *bottom* of the translations list.
885 foreach ( $theme_sidebars as $fields ) {
886 self::wpml_update_field( $context, $fields['id'] . '-name', @$fields['name'], false );
887 self::wpml_update_field( $context, $fields['id'] . '-description', @$fields['description'], false );
888 }
889
890 foreach ( $custom_sidebars as $fields ) {
891 $name = isset( $fields['name_lang'] ) ? $fields['name_lang'] : $fields['name'];
892 $description = isset( $fields['description_lang'] ) ? $fields['description_lang'] : $fields['description'];
893 self::wpml_update_field( $context, $fields['id'] . '-name', $name, true );
894 self::wpml_update_field( $context, $fields['id'] . '-description', $description, true );
895 }
896 }
897
898 /**
899 * Updates the WPML string register for a single field.
900 *
901 * @since 2.0.9.7
902 * @param string $context
903 * @param string $field
904 * @param string $value
905 * @param bool $update_string If false then the translation will only be
906 * registered but not updated.
907 */
908 static protected function wpml_update_field( $context, $field, $value, $update_string = true ) {
909 global $sitepress, $sitepress_settings;
910
911 if ( empty( $sitepress ) || empty( $sitepress_settings ) ) { return false; }
912 if ( ! function_exists( 'icl_t' ) ) { return false; }
913
914 if ( ! icl_st_is_registered_string( $context, $field ) ) {
915 // Register the field if it does not exist.
916 icl_register_string( $context, $field, $value, false );
917
918 $active_languages = $sitepress->get_active_languages();
919
920 foreach ( $active_languages as $lang => $data ) {
921 icl_update_string_translation( $field, $lang, $value, ICL_STRING_TRANSLATION_COMPLETE );
922 }
923
924 $default_language = ! empty( $sitepress_settings['st']['strings_language'] )
925 ? $sitepress_settings['st']['strings_language']
926 : $sitepress->get_default_language();
927 icl_update_string_translation( $field, $default_language, $value, ICL_STRING_TRANSLATION_COMPLETE );
928
929 } else if ( $update_string ) {
930
931 // Add translation.
932 if ( defined( 'DOING_AJAX' ) ) {
933 $current_language = $sitepress->get_language_cookie();
934 } else {
935 $current_language = $sitepress->get_current_language();
936 }
937
938 icl_update_string_translation( $field, $current_language, $value, ICL_STRING_TRANSLATION_COMPLETE );
939 }
940 }
941
942 /**
943 * Returns boolean true, when site is currently using the default language.
944 *
945 * @since 2.0.9.7
946 * @return bool
947 */
948 static protected function wpml_is_default_lang() {
949 global $sitepress, $sitepress_settings;
950 if ( empty( $sitepress ) || empty( $sitepress_settings ) ) { return true; }
951 if ( ! function_exists( 'icl_t' ) ) { return true; }
952
953 if ( defined( 'DOING_AJAX' ) ) {
954 $current_language = $sitepress->get_language_cookie();
955 } else {
956 $current_language = $sitepress->get_current_language();
957 }
958
959 $default_language = ! empty( $sitepress_settings['st']['strings_language'] )
960 ? $sitepress_settings['st']['strings_language']
961 : $sitepress->get_default_language();
962
963 return $default_language == $current_language;
964 }
965
966 /**
967 * Translates the text inside the specified sidebar object.
968 *
969 * @since 2.0.9.7
970 * @param array $sidebar Sidebar object.
971 * @return array Translated sidebar object.
972 */
973 static protected function wpml_translate( $sidebar ) {
974 if ( ! function_exists( 'icl_t' ) ) { return $sidebar; }
975
976 $context = 'Sidebar';
977
978 // Translate the name and description.
979 // Note: When changing a translation the icl_t() function will not
980 // return the updated value due to caching.
981
982 if ( isset( $sidebar['name_lang'] ) ) {
983 $sidebar['name'] = $sidebar['name_lang'];
984 } else {
985 $sidebar['name'] = icl_t( $context, $sidebar['id'] . '-name', $sidebar['name'] );
986 }
987 if ( isset( $sidebar['description_lang'] ) ) {
988 $sidebar['description'] = $sidebar['description_lang'];
989 } else {
990 $sidebar['description'] = icl_t( $context, $sidebar['id'] . '-description', $sidebar['description'] );
991 }
992
993 return $sidebar;
994 }
995
996
997 //
998 // ========== Custom column an Quick-Edit fields for post list.
999 // http://shibashake.com/wordpress-theme/expand-the-wordpress-quick-edit-menu
1000 //
1001
1002 /**
1003 * Adds a custom column to post-types that support custom sidebars.
1004 *
1005 * @since 2.0.9.7
1006 * @param array $columns Column list.
1007 * @return array Modified column list.
1008 */
1009 public function post_columns( $columns ) {
1010 // This column is added.
1011 $insert = array(
1012 'cs_replacement' => __( 'Custom Sidebars', 'custom-sidebars' ),
1013 );
1014
1015 // Column is added after column 'title'.
1016 $insert_after = 'title';
1017
1018 $pos = array_search( $insert_after, array_keys( $columns ) ) + 1;
1019 $columns = array_merge(
1020 array_slice( $columns, 0, $pos ),
1021 $insert,
1022 array_slice( $columns, $pos )
1023 );
1024
1025 return $columns;
1026 }
1027
1028 /**
1029 * Display values in the custom column.
1030 *
1031 * @since 2.0.9.7
1032 * @param string $column_name Column-Key defined in post_columns above.
1033 * @param int $post_id Post-ID
1034 */
1035 public function post_column_content( $column_name, $post_id ) {
1036 switch ( $column_name ) {
1037 case 'cs_replacement':
1038 $this->print_sidebars_form( $post_id, 'col-sidebars' );
1039 break;
1040 }
1041 }
1042
1043 /**
1044 * Adds a custom field to the quick-edit box to select custom columns.
1045 *
1046 * @since 2.0.9.7
1047 * @param string $column_name Column-Key defined in post_columns above.
1048 * @param string $post_type Post-type that is currently edited.
1049 */
1050 public function post_quick_edit( $column_name, $post_type ) {
1051 if ( ! self::supported_post_type( $post_type ) ) { return false; }
1052
1053 switch ( $column_name ) {
1054 case 'cs_replacement':
1055 $this->print_metabox_quick();
1056 break;
1057 }
1058 }
1059
1060 /**
1061 * Javascript to set the values of the quick-edit form.
1062 *
1063 * Note: There is only 1 quick-edit form on the page. The form is displayed
1064 * when the user clicks the quick edit action; all fields are then populated
1065 * with values of the corresponding post.
1066 *
1067 * @since 2.0.9.7
1068 */
1069 public function post_quick_edit_js() {
1070 global $current_screen;
1071
1072 if ( ( $current_screen->base != 'edit' ) ) { return false; }
1073 if ( ! self::supported_post_type( $current_screen->post_type ) ) { return false; }
1074
1075 ?>
1076 <script type="text/javascript">
1077 <!--
1078 jQuery(function() {
1079 // we create a copy of the WP inline edit post function
1080 var wp_inline_edit = inlineEditPost.edit;
1081
1082 // and then we overwrite the function with our own code
1083 inlineEditPost.edit = function( id ) {
1084
1085 // "call" the original WP edit function
1086 // we don't want to leave WordPress hanging
1087 wp_inline_edit.apply( this, arguments );
1088
1089 // now we take care of our business.
1090
1091 // get the post ID
1092 var post_id = 0;
1093 if ( typeof( id ) == 'object' ) {
1094 post_id = parseInt( this.getId( id ) );
1095 }
1096
1097 if ( post_id > 0 ) {
1098
1099 // define the edit row
1100 var edit_row = jQuery( '#edit-' + post_id );
1101 var post_row = jQuery( '#post-' + post_id );
1102
1103 // Our custom column
1104 var sidebar_col = post_row.find( '.cs_replacement' );
1105
1106 sidebar_col.find( '[data-sidebar]' ).each(function() {
1107 var key = jQuery( this ).attr( 'data-sidebar' ),
1108 val = jQuery( this ).attr( 'data-replaced' ),
1109 hide = 'yes' === jQuery( this ).attr( 'data-cshide' );
1110
1111 if ( hide ) {
1112 edit_row.find( '.cs-replacement-field.' + key ).val( val ).parent().hide();
1113 } else {
1114 edit_row.find( '.cs-replacement-field.' + key ).val( val ).parent().show();
1115 }
1116 });
1117 }
1118 };
1119 });
1120 //-->
1121 </script>
1122 <?php
1123 }
1124
1125 /**
1126 * Hide column "Custom Sidebars" by default.
1127 *
1128 * @since 3.0.5
1129 *
1130 * @param array $hidden Array of hidden columns.
1131 * @param WP_Screen $screen Current WP screen.
1132 * @return array $hidden
1133 */
1134 public function default_hidden_columns( $hidden, $screen ) {
1135 if ( is_object( $screen ) && isset( $screen->post_type ) && 'post' == $screen->post_type ) {
1136 $hidden[] = 'cs_replacement';
1137 }
1138 return $hidden;
1139 }
1140 };
1141