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