Content Aware Sidebars – Unlimited Widget Areas - Version 0.6.3

Version Description

  • Added: scheduled and private singulars are selectable in sidebar editor
  • Added: combined cache for manual and automatically handled sidebars
  • Added: display_ca_sidebar accepts specific ids to be included
  • Fixed: only a limited amount of sidebars were present in widgets area
  • Fixed: better caching in sidebar editor
  • Fixed: page list in sidebar editor could behave incorrectly if some pages were static
Download this release

Release Info

Developer intoxstudio
Plugin Icon 128x128 Content Aware Sidebars – Unlimited Widget Areas
Version 0.6.3
Comparing to
See all releases

Code changes from version 0.6.2 to 0.6.3

Files changed (3) hide show
  1. content-aware-sidebars.php +81 -52
  2. readme.txt +12 -4
  3. walker.php +58 -71
content-aware-sidebars.php CHANGED
@@ -6,7 +6,7 @@
6
  Plugin Name: Content Aware Sidebars
7
  Plugin URI: http://www.intox.dk/
8
  Description: Manage and show sidebars according to the content being viewed.
9
- Version: 0.6.2
10
  Author: Joachim Jensen
11
  Author URI: http://www.intox.dk/
12
  Text Domain: content-aware-sidebars
@@ -36,6 +36,7 @@ class ContentAwareSidebars {
36
  protected $post_type_objects = array();
37
  protected $taxonomies = array();
38
  protected $taxonomy_objects = array();
 
39
 
40
  /**
41
  *
@@ -253,12 +254,12 @@ class ContentAwareSidebars {
253
 
254
  /**
255
  *
256
- * Create sidebars from content types
257
  *
258
  */
259
  public function create_sidebars() {
260
  $posts = get_posts(array(
261
- 'numberposts' => 0,
262
  'post_type' => 'sidebar',
263
  'post_status' => array('publish','private','future')
264
  ));
@@ -293,7 +294,9 @@ class ContentAwareSidebars {
293
  }
294
 
295
  /**
 
296
  * Manage custom column sorting
 
297
  */
298
  public function admin_column_orderby($vars) {
299
  if (isset($vars['orderby']) && in_array($vars['orderby'],array('exposure','handle','merge-pos'))) {
@@ -368,11 +371,11 @@ class ContentAwareSidebars {
368
  return;
369
 
370
  foreach($posts as $post) {
371
-
372
  $id = 'ca-sidebar-'.$post->ID;
373
 
374
- // Check if sidebar exists
375
- if (!isset($_wp_sidebars_widgets[$id]))
376
  continue;
377
 
378
  // If host has already been replaced, merge with it instead. Might change in future.
@@ -394,9 +397,17 @@ class ContentAwareSidebars {
394
  * @return array|bool
395
  *
396
  */
397
- public function get_sidebars($handle = "!= '2'") {
398
  global $wpdb, $post_type;
399
 
 
 
 
 
 
 
 
 
400
  $errors = 1;
401
 
402
  $joins = "";
@@ -503,18 +514,19 @@ class ContentAwareSidebars {
503
  }
504
 
505
  // Check if any errors are left
506
- if($errors)
507
  return false;
 
508
 
509
  // Show private sidebars or not
510
  if(current_user_can('read_private_posts'))
511
  $post_status = "IN('publish','private')";
512
  else
513
  $post_status = "= 'publish'";
514
- $where .= "posts.post_status ".$post_status." AND ";
515
 
516
- // Return proper sidebars
517
- return $wpdb->get_results("
518
  SELECT
519
  posts.ID,
520
  handle.meta_value handle,
@@ -537,10 +549,13 @@ class ContentAwareSidebars {
537
  WHERE
538
  posts.post_type = 'sidebar' AND
539
  $where
540
- handle.meta_value $handle
541
  GROUP BY posts.ID
542
  ORDER BY posts.menu_order ASC, handle.meta_value DESC, posts.post_date DESC
543
  ");
 
 
 
 
544
  }
545
 
546
  /**
@@ -621,47 +636,46 @@ class ContentAwareSidebars {
621
  <?php
622
  }
623
 
624
- public function meta_box_taxonomy($post, $tax) {
625
  $meta = get_post_meta($post->ID, 'taxonomies', true);
626
  $current = $meta != '' ? $meta : array();
627
 
628
- $taxonomy = $tax['args']->name;
629
 
630
- $terms = get_terms( $taxonomy);
631
 
632
- if ( ! $terms || is_wp_error($terms) ) {
633
- echo '<p>' . __( 'No items.' ) . '</p>';
634
-
635
  } else {
636
 
637
- ?>
638
- <div id="taxonomy-<?php echo $taxonomy; ?>" class="categorydiv">
639
- <ul id="<?php echo $taxonomy; ?>-tabs" class="category-tabs">
640
- <li class="hide-if-no-js"><a href="#<?php echo $taxonomy; ?>-pop" tabindex="3"><?php _e( 'Most Used' ); ?></a></li>
641
- <li class="tabs"><a href="#<?php echo $taxonomy; ?>-all" tabindex="3"><?php _e('View All');; ?></a></li>
642
  </ul>
643
 
644
- <div id="<?php echo $taxonomy; ?>-pop" class="tabs-panel" style="display: none;height:inherit;max-height:200px;">
645
- <ul id="<?php echo $taxonomy; ?>checklist-pop" class="categorychecklist form-no-clear" >
646
- <?php $popular_ids = cas_popular_terms_checklist($tax['args']); ?>
647
  </ul>
648
  </div>
649
 
650
- <div id="<?php echo $taxonomy; ?>-all" class="tabs-panel" style="height:inherit;max-height:200px;">
651
  <?php
652
- $name = ( $taxonomy == 'category' ) ? 'post_category[]' : 'tax_input[' . $taxonomy . ']';
653
  echo "<input type='hidden' name='{$name}' value='0' />"; // Allows for an empty term set to be sent. 0 is an invalid Term ID and will be ignored by empty() checks.
654
  ?>
655
- <ul id="<?php echo $taxonomy; ?>checklist" class="list:<?php echo $taxonomy?> categorychecklist form-no-clear">
656
- <?php cas_terms_checklist($post->ID, array( 'taxonomy' => $taxonomy, 'popular_cats' => $popular_ids ) ) ?>
657
  </ul>
658
  </div>
659
  </div>
660
- <?php
661
  }
662
 
663
  echo '<p style="padding:6px 0 4px;">'."\n";
664
- echo '<label><input type="checkbox" name="taxonomies[]" value="'.$tax['args']->name.'"'.(in_array($tax['args']->name,$current) ? ' checked="checked"' : '').' /> '.sprintf(__('Show with %s'),$tax['args']->labels->all_items).'</label>'."\n";
665
  echo '</p>'."\n";
666
 
667
  }
@@ -677,29 +691,29 @@ class ContentAwareSidebars {
677
  $exclude[] = get_option('page_for_posts');
678
  }
679
 
680
- $posts = get_posts(array('post_type'=>$post_type->name,'exclude'=>$exclude));
 
 
 
 
 
681
 
682
- if ( ! $posts || is_wp_error($posts) ) {
683
- echo '<p>' . __( 'No items.' ) . '</p>';
684
-
685
  } else {
686
 
687
- ?>
688
-
689
  <div id="posttype-<?php echo $post_type->name; ?>" class="categorydiv">
690
  <ul id="posttype-<?php echo $post_type->name; ?>-tabs" class="category-tabs">
691
  <li class="tabs"><a href="#<?php echo $post_type->name; ?>-all" tabindex="3"><?php _e('View All');; ?></a></li>
692
- </ul>
693
-
694
  <div id="<?php echo $post_type->name; ?>-all" class="tabs-panel" style="height:inherit;max-height:200px;">
695
  <ul id="<?php echo $post_type->name; ?>checklist" class="list:<?php echo $post_type->name?> categorychecklist form-no-clear">
696
- <?php cas_posts_checklist($post->ID, array( 'post_type' => $post_type->name) ) ?>
697
  </ul>
698
  </div>
699
- </div>
700
-
701
- <?php
702
-
703
  }
704
 
705
  echo '<p style="padding:6px 0 4px;">'."\n";
@@ -843,7 +857,12 @@ class ContentAwareSidebars {
843
  flush_rewrite_rules();
844
  }
845
 
846
- public function load_dependencies() {
 
 
 
 
 
847
 
848
  require_once('walker.php');
849
 
@@ -859,25 +878,35 @@ $ca_sidebars = new ContentAwareSidebars();
859
  function display_ca_sidebar($args = array()) {
860
  global $ca_sidebars, $_wp_sidebars_widgets;
861
 
862
- // Grab args or defaults
863
  $defaults = array (
 
864
  'before' => '<div id="sidebar" class="widget-area"><ul class="xoxo">',
865
  'after' => '</ul></div>'
866
  );
867
  $args = wp_parse_args($args,$defaults);
868
  extract($args,EXTR_SKIP);
869
-
870
- $posts = $ca_sidebars->get_sidebars("='2'");
 
871
  if(!$posts)
872
  return;
873
 
 
 
 
 
 
 
 
 
874
  $i = $host = 0;
875
  foreach($posts as $post) {
876
-
877
  $id = 'ca-sidebar-'.$post->ID;
878
 
879
- // Check if sidebar exists
880
- if (!isset($_wp_sidebars_widgets[$id]))
881
  continue;
882
 
883
  // Merge if more than one. First one is host.
6
  Plugin Name: Content Aware Sidebars
7
  Plugin URI: http://www.intox.dk/
8
  Description: Manage and show sidebars according to the content being viewed.
9
+ Version: 0.6.3
10
  Author: Joachim Jensen
11
  Author URI: http://www.intox.dk/
12
  Text Domain: content-aware-sidebars
36
  protected $post_type_objects = array();
37
  protected $taxonomies = array();
38
  protected $taxonomy_objects = array();
39
+ protected $sidebar_cache = array();
40
 
41
  /**
42
  *
254
 
255
  /**
256
  *
257
+ * Add sidebars to widgets area
258
  *
259
  */
260
  public function create_sidebars() {
261
  $posts = get_posts(array(
262
+ 'numberposts' => -1,
263
  'post_type' => 'sidebar',
264
  'post_status' => array('publish','private','future')
265
  ));
294
  }
295
 
296
  /**
297
+ *
298
  * Manage custom column sorting
299
+ *
300
  */
301
  public function admin_column_orderby($vars) {
302
  if (isset($vars['orderby']) && in_array($vars['orderby'],array('exposure','handle','merge-pos'))) {
371
  return;
372
 
373
  foreach($posts as $post) {
374
+
375
  $id = 'ca-sidebar-'.$post->ID;
376
 
377
+ // Check for correct handling and if sidebar exists
378
+ if ($post->handle == 2 || !isset($_wp_sidebars_widgets[$id]))
379
  continue;
380
 
381
  // If host has already been replaced, merge with it instead. Might change in future.
397
  * @return array|bool
398
  *
399
  */
400
+ public function get_sidebars() {
401
  global $wpdb, $post_type;
402
 
403
+ // Return cache if present
404
+ if(!empty($this->sidebar_cache)) {
405
+ if($this->sidebar_cache[0] == false)
406
+ return false;
407
+ else
408
+ return $this->sidebar_cache;
409
+ }
410
+
411
  $errors = 1;
412
 
413
  $joins = "";
514
  }
515
 
516
  // Check if any errors are left
517
+ if($errors) {
518
  return false;
519
+ }
520
 
521
  // Show private sidebars or not
522
  if(current_user_can('read_private_posts'))
523
  $post_status = "IN('publish','private')";
524
  else
525
  $post_status = "= 'publish'";
526
+ $where .= "posts.post_status ".$post_status."";
527
 
528
+ // Do query and cache it
529
+ $this->sidebar_cache = $wpdb->get_results("
530
  SELECT
531
  posts.ID,
532
  handle.meta_value handle,
549
  WHERE
550
  posts.post_type = 'sidebar' AND
551
  $where
 
552
  GROUP BY posts.ID
553
  ORDER BY posts.menu_order ASC, handle.meta_value DESC, posts.post_date DESC
554
  ");
555
+
556
+ // Return proper cache. If query was empty, tell the cache.
557
+ return empty($this->sidebar_cache) ? $this->sidebar_cache[0] = false : $this->sidebar_cache;
558
+
559
  }
560
 
561
  /**
636
  <?php
637
  }
638
 
639
+ public function meta_box_taxonomy($post, $box) {
640
  $meta = get_post_meta($post->ID, 'taxonomies', true);
641
  $current = $meta != '' ? $meta : array();
642
 
643
+ $taxonomy = $box['args'];
644
 
645
+ $terms = get_terms($taxonomy->name, array('get' => 'all'));
646
 
647
+ if (!$terms || is_wp_error($terms)) {
648
+ echo '<p>'.__('No items.').'</p>';
 
649
  } else {
650
 
651
+ ?>
652
+ <div id="taxonomy-<?php echo $taxonomy->name; ?>" class="categorydiv">
653
+ <ul id="<?php echo $taxonomy->name; ?>-tabs" class="category-tabs">
654
+ <li class="hide-if-no-js"><a href="#<?php echo $taxonomy->name; ?>-pop" tabindex="3"><?php _e( 'Most Used' ); ?></a></li>
655
+ <li class="tabs"><a href="#<?php echo $taxonomy->name; ?>-all" tabindex="3"><?php _e('View All');; ?></a></li>
656
  </ul>
657
 
658
+ <div id="<?php echo $taxonomy->name; ?>-pop" class="tabs-panel" style="display: none;height:inherit;max-height:200px;">
659
+ <ul id="<?php echo $taxonomy->name; ?>checklist-pop" class="categorychecklist form-no-clear" >
660
+ <?php $popular_ids = cas_popular_terms_checklist($taxonomy); ?>
661
  </ul>
662
  </div>
663
 
664
+ <div id="<?php echo $taxonomy->name; ?>-all" class="tabs-panel" style="height:inherit;max-height:200px;">
665
  <?php
666
+ $name = ( $taxonomy->name == 'category' ) ? 'post_category[]' : 'tax_input[' . $taxonomy->name . ']';
667
  echo "<input type='hidden' name='{$name}' value='0' />"; // Allows for an empty term set to be sent. 0 is an invalid Term ID and will be ignored by empty() checks.
668
  ?>
669
+ <ul id="<?php echo $taxonomy->name; ?>checklist" class="list:<?php echo $taxonomy->name?> categorychecklist form-no-clear">
670
+ <?php cas_terms_checklist($post->ID, array('taxonomy' => $taxonomy,'popular_terms' => $popular_ids, 'terms' => $terms)) ?>
671
  </ul>
672
  </div>
673
  </div>
674
+ <?php
675
  }
676
 
677
  echo '<p style="padding:6px 0 4px;">'."\n";
678
+ echo '<label><input type="checkbox" name="taxonomies[]" value="'.$taxonomy->name.'"'.(in_array($taxonomy->name,$current) ? ' checked="checked"' : '').' /> '.sprintf(__('Show with %s'),$taxonomy->labels->all_items).'</label>'."\n";
679
  echo '</p>'."\n";
680
 
681
  }
691
  $exclude[] = get_option('page_for_posts');
692
  }
693
 
694
+ $posts = get_posts(array(
695
+ 'numberposts' => -1,
696
+ 'post_type' => $post_type->name,
697
+ 'post_status' => array('publish','private','future'),
698
+ 'exclude' => $exclude
699
+ ));
700
 
701
+ if (!$posts || is_wp_error($posts)) {
702
+ echo '<p>'.__('No items.').'</p>';
 
703
  } else {
704
 
705
+ ?>
 
706
  <div id="posttype-<?php echo $post_type->name; ?>" class="categorydiv">
707
  <ul id="posttype-<?php echo $post_type->name; ?>-tabs" class="category-tabs">
708
  <li class="tabs"><a href="#<?php echo $post_type->name; ?>-all" tabindex="3"><?php _e('View All');; ?></a></li>
709
+ </ul>
 
710
  <div id="<?php echo $post_type->name; ?>-all" class="tabs-panel" style="height:inherit;max-height:200px;">
711
  <ul id="<?php echo $post_type->name; ?>checklist" class="list:<?php echo $post_type->name?> categorychecklist form-no-clear">
712
+ <?php cas_posts_checklist($post->ID, array('post_type' => $post_type,'posts'=>$posts)); ?>
713
  </ul>
714
  </div>
715
+ </div>
716
+ <?php
 
 
717
  }
718
 
719
  echo '<p style="padding:6px 0 4px;">'."\n";
857
  flush_rewrite_rules();
858
  }
859
 
860
+ /**
861
+ *
862
+ * Load dependencies
863
+ *
864
+ */
865
+ private function load_dependencies() {
866
 
867
  require_once('walker.php');
868
 
878
  function display_ca_sidebar($args = array()) {
879
  global $ca_sidebars, $_wp_sidebars_widgets;
880
 
881
+ // Grab args or defaults
882
  $defaults = array (
883
+ 'include' => '',
884
  'before' => '<div id="sidebar" class="widget-area"><ul class="xoxo">',
885
  'after' => '</ul></div>'
886
  );
887
  $args = wp_parse_args($args,$defaults);
888
  extract($args,EXTR_SKIP);
889
+
890
+ // Get sidebars
891
+ $posts = $ca_sidebars->get_sidebars();
892
  if(!$posts)
893
  return;
894
 
895
+ // Handle include argument
896
+ if(!empty($include)) {
897
+ if(!is_array($include))
898
+ $include = explode(',',$include);
899
+ // Fast lookup
900
+ $include = array_flip($include);
901
+ }
902
+
903
  $i = $host = 0;
904
  foreach($posts as $post) {
905
+
906
  $id = 'ca-sidebar-'.$post->ID;
907
 
908
+ // Check for manual handling, if sidebar exists and if id should be included
909
+ if ($post->handle != 2 || !isset($_wp_sidebars_widgets[$id]) || (!empty($include) && !isset($include[$post->ID])))
910
  continue;
911
 
912
  // Merge if more than one. First one is host.
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=KPZHE
4
  Tags: sidebar, widget, content aware, post type, taxonomy, term, archive, singular
5
  Requires at least: 3.1
6
  Tested up to: 3.2.1
7
- Stable tag: 0.6.2
8
 
9
  Manage and show sidebars according to the content being viewed.
10
 
@@ -21,6 +21,7 @@ No extra database tables or table columns will be added.
21
  * Singulars containing specific taxonomies or taxonomy terms
22
  * Specific post type archives, taxonomy archives or taxonomy term archives
23
  * Search results, 404 page and front page
 
24
  * Merge new sidebars with others, replace them or simply add them to your theme manually
25
  * Create complex content with nested sidebars
26
  * Private sidebars only for members
@@ -36,8 +37,6 @@ www.intox.dk
36
 
37
  == Installation ==
38
 
39
- Easy install:
40
-
41
  1. Upload the full plugin directory to your `/wp-content/plugins/` directory or install the plugin through `Plugins` in the administration
42
  1. Activate the plugin through `Plugins` in the administration
43
  1. Have fun creating your first sidebar
@@ -59,6 +58,15 @@ You are.
59
 
60
  == Changelog ==
61
 
 
 
 
 
 
 
 
 
 
62
  = 0.6.2 =
63
 
64
  * Fixed: array_flip triggered type mismatch errors in some cases
@@ -118,4 +126,4 @@ You are.
118
 
119
  = 0.1 =
120
 
121
- * Hello World
4
  Tags: sidebar, widget, content aware, post type, taxonomy, term, archive, singular
5
  Requires at least: 3.1
6
  Tested up to: 3.2.1
7
+ Stable tag: 0.6.3
8
 
9
  Manage and show sidebars according to the content being viewed.
10
 
21
  * Singulars containing specific taxonomies or taxonomy terms
22
  * Specific post type archives, taxonomy archives or taxonomy term archives
23
  * Search results, 404 page and front page
24
+ * Any combination of the above
25
  * Merge new sidebars with others, replace them or simply add them to your theme manually
26
  * Create complex content with nested sidebars
27
  * Private sidebars only for members
37
 
38
  == Installation ==
39
 
 
 
40
  1. Upload the full plugin directory to your `/wp-content/plugins/` directory or install the plugin through `Plugins` in the administration
41
  1. Activate the plugin through `Plugins` in the administration
42
  1. Have fun creating your first sidebar
58
 
59
  == Changelog ==
60
 
61
+ = 0.6.3 =
62
+
63
+ * Added: scheduled and private singulars are selectable in sidebar editor
64
+ * Added: combined cache for manual and automatically handled sidebars
65
+ * Added: display_ca_sidebar accepts specific ids to be included
66
+ * Fixed: only a limited amount of sidebars were present in widgets area
67
+ * Fixed: better caching in sidebar editor
68
+ * Fixed: page list in sidebar editor could behave incorrectly if some pages were static
69
+
70
  = 0.6.2 =
71
 
72
  * Fixed: array_flip triggered type mismatch errors in some cases
126
 
127
  = 0.1 =
128
 
129
+ * Hello World
walker.php CHANGED
@@ -33,9 +33,9 @@ class CAS_Walker_Tax_Checklist extends Walker {
33
 
34
  $name = $taxonomy->name == 'category' ? 'post_category' : 'tax_input['.$taxonomy->name.']';
35
  $value = $taxonomy->hierarchical ? 'term_id' : 'slug';
36
- $class = in_array( $term->term_id, $popular_cats ) ? ' class="popular-category"' : '';
37
 
38
- $output .= "\n".'<li id="'.$taxonomy->name.'-'.$term->term_id.'"$class><label class="selectit"><input value="'.$term->$value.'" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy->name.'-'.$term->term_id.'"'.checked(in_array($term->term_id,$selected_cats),true,false).disabled(empty($disabled),false,false).'/>'.esc_html( apply_filters('the_category', $term->name )) . '</label>';
39
  }
40
 
41
  public function end_el(&$output, $term, $depth, $args) {
@@ -70,7 +70,7 @@ class CAS_Walker_Post_Checklist extends Walker {
70
  return;
71
  }
72
 
73
- $output .= "\n".'<li id="'.$post_type->name.'-'.$term->ID.'"><label class="selectit"><input value="'.$term->ID.'" type="checkbox" name="post_types[]" id="in-'.$post_type->name.'-'.$term->ID.'"'.checked( in_array($term->ID,$selected_cats),true,false).disabled(empty($disabled),false,false).'/>'.esc_html( $term->post_title ) . '</label>';
74
  }
75
 
76
  public function end_el(&$output, $term, $depth, $args) {
@@ -85,62 +85,54 @@ class CAS_Walker_Post_Checklist extends Walker {
85
  */
86
  function cas_terms_checklist($post_id = 0, $args = array()) {
87
  $defaults = array(
88
- 'descendants_and_self' => 0,
89
- 'selected_cats' => false,
90
- 'popular_cats' => false,
91
  'walker' => null,
92
  'taxonomy' => 'category',
 
93
  'checked_ontop' => true
94
  );
95
- extract( wp_parse_args($args, $defaults), EXTR_SKIP );
96
 
97
- if ( empty($walker) || !is_a($walker, 'Walker') )
98
  $walker = new CAS_Walker_Tax_Checklist();
99
 
100
- $descendants_and_self = (int) $descendants_and_self;
101
-
102
- $tax = get_taxonomy($taxonomy);
103
 
104
- $args = array('taxonomy' => $tax);
105
- $args['disabled'] = !current_user_can($tax->cap->assign_terms);
 
 
106
 
107
- if ( is_array( $selected_cats ) )
108
- $args['selected_cats'] = $selected_cats;
109
- elseif ( $post_id )
110
- $args['selected_cats'] = wp_get_object_terms($post_id, $taxonomy, array_merge($args, array('fields' => 'ids')));
111
  else
112
- $args['selected_cats'] = array();
113
 
114
- if ( is_array( $popular_cats ) )
115
- $args['popular_cats'] = $popular_cats;
116
  else
117
- $args['popular_cats'] = get_terms( $taxonomy, array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
118
-
119
- if ( $descendants_and_self ) {
120
- $categories = (array) get_terms($taxonomy, array( 'child_of' => $descendants_and_self, 'hierarchical' => 0, 'hide_empty' => 0 ) );
121
- $self = get_term( $descendants_and_self, $taxonomy );
122
- array_unshift( $categories, $self );
123
- } else {
124
- $categories = (array) get_terms($taxonomy, array('get' => 'all'));
125
- }
126
 
127
- if ( $checked_ontop ) {
128
- // Post process $categories rather than adding an exclude to the get_terms() query to keep the query the same across all posts (for any query cache)
129
- $checked_categories = array();
130
- $keys = array_keys( $categories );
131
 
132
- foreach( $keys as $k ) {
133
- if ( in_array( $categories[$k]->term_id, $args['selected_cats'] ) ) {
134
- $checked_categories[] = $categories[$k];
135
- unset( $categories[$k] );
 
 
 
 
136
  }
137
  }
138
 
139
- // Put checked cats on top
140
- echo call_user_func_array(array(&$walker, 'walk'), array($checked_categories, 0, $args));
141
  }
142
  // Then the rest of them
143
- echo call_user_func_array(array(&$walker, 'walk'), array($categories, 0, $args));
144
  }
145
 
146
  /**
@@ -182,63 +174,58 @@ function cas_popular_terms_checklist( $taxonomy, $default = 0, $number = 10, $ec
182
 
183
  /**
184
  *
185
- * Show terms checklist
186
  *
187
  */
188
  function cas_posts_checklist($post_id = 0, $args = array()) {
189
  $defaults = array(
190
- 'descendants_and_self' => 0,
191
- 'selected_cats' => false,
192
  'walker' => null,
193
  'post_type' => 'post',
 
194
  'checked_ontop' => true
195
  );
196
- extract( wp_parse_args($args, $defaults), EXTR_SKIP );
197
 
198
- if ( empty($walker) || !is_a($walker, 'Walker') )
199
  $walker = new CAS_Walker_Post_Checklist();
200
 
201
- $descendants_and_self = (int) $descendants_and_self;
 
202
 
203
- $ptype = get_post_type_object($post_type);
204
-
205
- $args = array('post_type' => $ptype);
206
- $args['disabled'] = !current_user_can($ptype->cap->edit_post);
207
 
208
- if ( is_array( $selected_cats ) )
209
- $args['selected_cats'] = $selected_cats;
210
- elseif ( $post_id )
211
  $args['selected_cats'] = (array)get_post_meta($post_id, 'post_types', true);
212
  else
213
  $args['selected_cats'] = array();
214
 
215
- if ( $descendants_and_self ) {
216
- $categories = (array) get_posts(array('post_type'=>$post_type,'post_parent' => $descendants_and_self, 'numberposts'=>-1) );
217
- $self = get_post($descendants_and_self);
218
- array_unshift($categories, $self);
219
- } else {
220
- $categories = (array) get_posts(array('post_type'=>$post_type, 'numberposts'=>-1));
221
- }
222
 
223
  if ( $checked_ontop ) {
224
- //Post process $categories rather than adding an exclude to the get_terms() query to keep the query the same across all posts (for any query cache)
225
- $checked_categories = array();
226
- $keys = array_keys( $categories );
227
 
228
  foreach( $keys as $k ) {
229
- if ( in_array( $categories[$k]->ID, $args['selected_cats'] ) ) {
230
- $checked_categories[] = $categories[$k];
231
- unset( $categories[$k] );
232
  }
233
- elseif($post_type == 'page' && 'page' == get_option( 'show_on_front') && (get_option( 'page_on_front' ) == $categories[$k]->ID || get_option('page_for_posts') == $categories[$k]->ID))
234
- unset( $categories[$k] );
235
  }
236
 
237
- //Put checked cats on top
238
- echo call_user_func_array(array(&$walker, 'walk'), array($checked_categories, 0, $args));
239
  }
 
240
  // Then the rest of them
241
- echo call_user_func_array(array(&$walker, 'walk'), array($categories, 0, $args));
242
  }
243
 
244
  ?>
33
 
34
  $name = $taxonomy->name == 'category' ? 'post_category' : 'tax_input['.$taxonomy->name.']';
35
  $value = $taxonomy->hierarchical ? 'term_id' : 'slug';
36
+ $class = in_array( $term->term_id, $popular_terms ) ? ' class="popular-category"' : '';
37
 
38
+ $output .= "\n".'<li id="'.$taxonomy->name.'-'.$term->term_id.'"$class><label class="selectit"><input value="'.$term->$value.'" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy->name.'-'.$term->term_id.'"'.checked(in_array($term->term_id,$selected_terms),true,false).disabled(empty($disabled),false,false).'/> '.esc_html( apply_filters('the_category', $term->name )) . '</label>';
39
  }
40
 
41
  public function end_el(&$output, $term, $depth, $args) {
70
  return;
71
  }
72
 
73
+ $output .= "\n".'<li id="'.$post_type->name.'-'.$term->ID.'"><label class="selectit"><input value="'.$term->ID.'" type="checkbox" name="post_types[]" id="in-'.$post_type->name.'-'.$term->ID.'"'.checked(in_array($term->ID,$selected_cats),true,false).disabled(empty($disabled),false,false).'/> '.esc_html( $term->post_title ).'</label>';
74
  }
75
 
76
  public function end_el(&$output, $term, $depth, $args) {
85
  */
86
  function cas_terms_checklist($post_id = 0, $args = array()) {
87
  $defaults = array(
88
+ 'popular_terms' => false,
 
 
89
  'walker' => null,
90
  'taxonomy' => 'category',
91
+ 'terms' => null,
92
  'checked_ontop' => true
93
  );
94
+ extract(wp_parse_args($args, $defaults), EXTR_SKIP);
95
 
96
+ if (empty($walker) || !is_a($walker, 'Walker'))
97
  $walker = new CAS_Walker_Tax_Checklist();
98
 
99
+ if(!is_object($taxonomy))
100
+ $taxonomy = get_taxonomy($taxonomy);
 
101
 
102
+ $args = array(
103
+ 'taxonomy' => $taxonomy,
104
+ 'disabled' => !current_user_can($taxonomy->cap->assign_terms)
105
+ );
106
 
107
+ if ($post_id)
108
+ $args['selected_terms'] = wp_get_object_terms($post_id, $taxonomy->name, array_merge($args, array('fields' => 'ids')));
 
 
109
  else
110
+ $args['selected_terms'] = array();
111
 
112
+ if (is_array($popular_terms))
113
+ $args['popular_terms'] = $popular_terms;
114
  else
115
+ $args['popular_terms'] = get_terms( $taxonomy->name, array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
 
 
 
 
 
 
 
 
116
 
117
+ if(!$terms)
118
+ $terms = (array) get_terms($taxonomy->name, array('get' => 'all'));
 
 
119
 
120
+ if ($checked_ontop) {
121
+ $checked_terms = array();
122
+ $keys = array_keys( $terms );
123
+
124
+ foreach($keys as $k) {
125
+ if (in_array($terms[$k]->term_id, $args['selected_terms'])) {
126
+ $checked_terms[] = $terms[$k];
127
+ unset($terms[$k]);
128
  }
129
  }
130
 
131
+ // Put checked terms on top
132
+ echo call_user_func_array(array(&$walker, 'walk'), array($checked_terms, 0, $args));
133
  }
134
  // Then the rest of them
135
+ echo call_user_func_array(array(&$walker, 'walk'), array($terms, 0, $args));
136
  }
137
 
138
  /**
174
 
175
  /**
176
  *
177
+ * Show posts checklist
178
  *
179
  */
180
  function cas_posts_checklist($post_id = 0, $args = array()) {
181
  $defaults = array(
 
 
182
  'walker' => null,
183
  'post_type' => 'post',
184
+ 'posts' => null,
185
  'checked_ontop' => true
186
  );
187
+ extract(wp_parse_args($args, $defaults), EXTR_SKIP);
188
 
189
+ if (empty($walker) || !is_a($walker, 'Walker'))
190
  $walker = new CAS_Walker_Post_Checklist();
191
 
192
+ if(!is_object($post_type))
193
+ $post_type = get_post_type_object($post_type);
194
 
195
+ $args = array(
196
+ 'post_type' => $post_type,
197
+ 'disabled' => !current_user_can($post_type->cap->edit_post)
198
+ );
199
 
200
+ if($post_id)
 
 
201
  $args['selected_cats'] = (array)get_post_meta($post_id, 'post_types', true);
202
  else
203
  $args['selected_cats'] = array();
204
 
205
+ if(!$posts)
206
+ $posts = get_posts(array(
207
+ 'numberposts' => -1,
208
+ 'post_type' => $post_type->name,
209
+ 'post_status' => array('publish','private','future'),
210
+ ));
 
211
 
212
  if ( $checked_ontop ) {
213
+ $checked_posts = array();
214
+ $keys = array_keys($posts);
 
215
 
216
  foreach( $keys as $k ) {
217
+ if (in_array($posts[$k]->ID, $args['selected_cats'])) {
218
+ $checked_posts[] = $posts[$k];
219
+ unset($posts[$k]);
220
  }
 
 
221
  }
222
 
223
+ //Put checked posts on top
224
+ echo call_user_func_array(array(&$walker, 'walk'), array($checked_posts, 0, $args));
225
  }
226
+
227
  // Then the rest of them
228
+ echo call_user_func_array(array(&$walker, 'walk'), array($posts, 0, $args));
229
  }
230
 
231
  ?>