AVH Extended Categories Widgets - Version 1.4

Version Description

Download this release

Release Info

Developer petervanderdoes
Plugin Icon wp plugin AVH Extended Categories Widgets
Version 1.4
Comparing to
See all releases

Code changes from version 1.3 to 1.4

Files changed (2) hide show
  1. readme.txt +4 -2
  2. widget_extended_categories.php +215 -87
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: petervanderdoes, datafeedr.com
3
  Donate link: http://blog.avirtualhome.com/wordpress-plugins/
4
  Tags: extended, categories, widget
5
  Requires at least: 2.3
6
- Tested up to: 2.6
7
- Stable tag: 1.3
8
 
9
  Replacement of the category widget to allow for greater customization of the category widget.
10
 
@@ -40,6 +40,8 @@ I created a support site at http://forums.avirtualhome.com where you can ask que
40
  None
41
 
42
  == Arbitrary section ==
 
 
43
  * Version 1.3
44
  * You can select which categories to show (Requires WordPress 2.5.1 or higher).
45
  * Version 1.2
3
  Donate link: http://blog.avirtualhome.com/wordpress-plugins/
4
  Tags: extended, categories, widget
5
  Requires at least: 2.3
6
+ Tested up to: 2.6.1
7
+ Stable tag: 1.4
8
 
9
  Replacement of the category widget to allow for greater customization of the category widget.
10
 
40
  None
41
 
42
  == Arbitrary section ==
43
+ * Version 1.4
44
+ * Ability to have up to 9 widgets.
45
  * Version 1.3
46
  * You can select which categories to show (Requires WordPress 2.5.1 or higher).
47
  * Version 1.2
widget_extended_categories.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Extended Category Widget
4
  Plugin URI: http://blog.avirtualhome.com/wordpress-plugins
5
  Description: Replacement of the category widget to allow for greater customization of the category widget.
6
- Version: 1.3
7
  Author: Peter van der Does
8
  Author URI: http://blog.avirtualhome.com/
9
 
@@ -30,7 +30,7 @@ function widget_extended_categories_init() {
30
  return;
31
  }
32
 
33
- function widget_extended_categories($args) {
34
  // Check for version
35
  global $wp_version;
36
  if ( version_compare($wp_version, '2.5.1', '<') ) {
@@ -40,16 +40,16 @@ function widget_extended_categories_init() {
40
  }
41
  extract ( $args );
42
  $options = get_option ( 'widget_extended_categories' );
43
- $c = $options ['count'] ? '1' : '0';
44
- $h = $options ['hierarchical'] ? '1' : '0';
45
- $e = $options ['hide_empty'] ? '1' : '0';
46
- $s = $options ['sort_column'] ? $options ['sort_column'] : 'name';
47
- $o = $options ['sort_order'] ? $options ['sort_order'] : 'asc';
48
- $title = empty ( $options ['title'] ) ? __ ( 'Categories' ) : $options ['title'];
49
- $style = empty ( $options ['style'] ) ? 'list' : $options ['style'];
50
  if ($avh_extcat_canselectcats) {
51
- if ($options ['post_category']) {
52
- $post_category = unserialize ( $options ['post_category'] );
53
  $included_cats = implode ( ",", $post_category );
54
  }
55
  $cat_args = array ('include' => $included_cats, 'orderby' => $s, 'order' => $o, 'show_count' => $c, 'hide_empty' => $e, 'hierarchical' => $h, 'title_li' => '', 'show_option_none' => __ ( 'Select Category' ) );
@@ -83,7 +83,7 @@ function widget_extended_categories_init() {
83
  echo $after_widget;
84
  }
85
 
86
- function widget_extended_categories_control() {
87
  // Check for version
88
  global $wp_version;
89
  if ( version_compare($wp_version, '2.5.1', '<') ) {
@@ -96,21 +96,21 @@ function widget_extended_categories_init() {
96
  if (! is_array ( $options )) {
97
  $options = $newoptions = array ( );
98
  }
99
-
100
  // Post to new options array
101
- if ($_POST ['categories-submit']) {
102
- $newoptions ['title'] = strip_tags ( stripslashes ( $_POST ['categories-title'] ) );
103
- $newoptions ['count'] = isset ( $_POST ['categories-count'] );
104
- $newoptions ['hierarchical'] = isset ( $_POST ['categories-hierarchical'] );
105
- $newoptions ['hide_empty'] = isset ( $_POST ['categories-hide_empty'] );
106
- $newoptions ['sort_column'] = strip_tags ( stripslashes ( $_POST ['categories-sort_column'] ) );
107
- $newoptions ['sort_order'] = strip_tags ( stripslashes ( $_POST ['categories-sort_order'] ) );
108
- $newoptions ['style'] = strip_tags ( stripslashes ( $_POST ['categories-style'] ) );
 
109
  if ($avh_extcat_canselectcats) {
110
- if (in_array ( '-1', $_POST ['post_category'], true )) {
111
- $newoptions ['post_category'] = false;
112
  } else {
113
- $newoptions ['post_category'] = serialize ( $_POST ['post_category'] );
114
  }
115
  }
116
 
@@ -123,94 +123,222 @@ function widget_extended_categories_init() {
123
  }
124
 
125
  // Prepare data for display
126
- $title = htmlspecialchars ( $options ['title'], ENT_QUOTES );
127
- $count = $options ['count'] ? 'checked="checked"' : '';
128
- $hierarchical = $options ['hierarchical'] ? 'checked="checked"' : '';
129
- $hide_empty = $options ['hide_empty'] ? 'checked="checked"' : '';
130
- $sort_id = ($options ['sort_column'] == 'ID') ? ' SELECTED' : '';
131
- $sort_name = ($options ['sort_column'] == 'name') ? ' SELECTED' : '';
132
- $sort_count = ($options ['sort_column'] == 'count') ? ' SELECTED' : '';
133
- $sort_order_a = ($options ['sort_order'] == 'asc') ? ' SELECTED' : '';
134
- $sort_order_d = ($options ['sort_order'] == 'desc') ? ' SELECTED' : '';
135
- $style_list = ($options ['style'] == 'list') ? ' SELECTED' : '';
136
- $style_drop = ($options ['style'] == 'drop') ? ' SELECTED' : '';
137
  if ($avh_extcat_canselectcats) {
138
- $selected_cats = ($options ['post_category'] != '') ? unserialize ( $options ['post_category'] ) : false;
139
  }
140
  ?>
141
  <div>
142
- <label for="categories-title"><?php _e ( 'Title:' ); ?>
143
- <input style="width: 250px;" id="categories-title" name="categories-title" type="text" value="<?php echo $title; ?>" />
144
  </label>
145
- <label for="categories-count" style="line-height: 35px; display: block;">Show post counts <input class="checkbox" type="checkbox" <?php echo $count;
146
- ?>
147
- id="categories-count" name="categories-count" /> </label> <label
148
- for="categories-hierarchical"
149
- style="line-height: 35px; display: block;">Show hierarchy <input
150
- class="checkbox" type="checkbox" <?php
151
- echo $hierarchical;
152
- ?>
153
- id="categories-hierarchical" name="categories-hierarchical" /> </label>
154
 
155
- <label for="categories-hide_empty"
156
- style="line-height: 35px; display: block;">Hide empty categories <input
157
- class="checkbox" type="checkbox" <?php
158
- echo $hide_empty;
159
- ?>
160
- id="categories-hide_empty" name="categories-hide_empty" /> </label> <label
161
- for="categories-sort_column" style="line-height: 35px; display: block;">Sort
162
- by <select id="categories-sort_column" name="categories-sort_column">
163
- <option value="ID" <?php
164
- echo $sort_id?>>ID</option>
165
- <option value="name" <?php
166
- echo $sort_name?>>Name</option>
167
- <option value="count" <?php
168
- echo $sort_count?>>Count</option>
169
- </select> </label> <label for="categories-sort_order"
170
- style="line-height: 35px; display: block;">Sort order <select
171
- id="categories-sort_order" name="categories-sort_order">
172
- <option value="asc" <?php
173
- echo $sort_order_a?>>Ascending</option>
174
- <option value="desc" <?php
175
- echo $sort_order_d?>>Descending</option>
176
- </select> </label> <label for="categories-style"
177
- style="line-height: 35px; display: block;">Display style <select
178
- id="categories-style" name="categories-style">
179
- <option value='list' <?php
180
- echo $style_list;
181
- ?>>List</option>
182
- <option value='drop' <?php
183
- echo $style_drop;
184
- ?>>Drop down</option>
185
- </select> </label>
 
 
186
  <?php
187
  if ($avh_extcat_canselectcats) {
188
  echo ' <b>Include these categories</b><hr />';
189
  echo ' <ul id="categorychecklist" class="list:category categorychecklist form-no-clear" style="list-style-type: none; margin-left: 5px; padding-left: 0px; margin-bottom: 20px;">';
190
- echo ' <li id="category--1" class="popular-category">';
191
- echo ' <label for="in-category--1" class="selectit">';
192
- echo ' <input value="-1" name="post_category[]" id="in-category--1" type="checkbox"';
193
  if (!$selected_cats) { echo 'checked'; }
194
  echo '> Include All Categories';
195
  echo ' </label>';
196
  echo ' </li>';
197
- wp_category_checklist(0,0,$selected_cats);
198
  echo ' </ul>';
199
  }
200
  ?>
201
 
202
- <input type="hidden" id="categories-submit" name="categories-submit"
203
- value="1" /></div>
204
  <?php
205
  }
206
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
  function widget_extended_categories_register() {
208
- wp_register_sidebar_widget ( 'extended-categories', 'Extended Categories', 'widget_extended_categories' );
209
- wp_register_widget_control ( 'extended-categories', 'Extended Categories', 'widget_extended_categories_control', array ('width' => 300, 'height' => 245 ) );
 
 
 
 
 
 
 
 
 
 
 
210
  }
211
 
212
  // Launch Widgets
213
  widget_extended_categories_register ();
214
  }
215
  add_action ( 'plugins_loaded', 'widget_extended_categories_init' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216
  ?>
3
  Plugin Name: Extended Category Widget
4
  Plugin URI: http://blog.avirtualhome.com/wordpress-plugins
5
  Description: Replacement of the category widget to allow for greater customization of the category widget.
6
+ Version: 1.4
7
  Author: Peter van der Does
8
  Author URI: http://blog.avirtualhome.com/
9
 
30
  return;
31
  }
32
 
33
+ function widget_extended_categories($args, $number = 1) {
34
  // Check for version
35
  global $wp_version;
36
  if ( version_compare($wp_version, '2.5.1', '<') ) {
40
  }
41
  extract ( $args );
42
  $options = get_option ( 'widget_extended_categories' );
43
+ $c = $options [$number]['count'] ? '1' : '0';
44
+ $h = $options [$number]['hierarchical'] ? '1' : '0';
45
+ $e = $options [$number]['hide_empty'] ? '1' : '0';
46
+ $s = $options [$number]['sort_column'] ? $options [$number]['sort_column'] : 'name';
47
+ $o = $options [$number]['sort_order'] ? $options [$number]['sort_order'] : 'asc';
48
+ $title = empty ( $options [$number]['title'] ) ? __ ( 'Categories' ) : $options [$number]['title'];
49
+ $style = empty ( $options [$number]['style'] ) ? 'list' : $options [$number]['style'];
50
  if ($avh_extcat_canselectcats) {
51
+ if ($options [$number]['post_category']) {
52
+ $post_category = unserialize ( $options [$number]['post_category'] );
53
  $included_cats = implode ( ",", $post_category );
54
  }
55
  $cat_args = array ('include' => $included_cats, 'orderby' => $s, 'order' => $o, 'show_count' => $c, 'hide_empty' => $e, 'hierarchical' => $h, 'title_li' => '', 'show_option_none' => __ ( 'Select Category' ) );
83
  echo $after_widget;
84
  }
85
 
86
+ function widget_extended_categories_control($number = 1) {
87
  // Check for version
88
  global $wp_version;
89
  if ( version_compare($wp_version, '2.5.1', '<') ) {
96
  if (! is_array ( $options )) {
97
  $options = $newoptions = array ( );
98
  }
 
99
  // Post to new options array
100
+
101
+ if ($_POST ['categories-submit-' . $number]) {
102
+ $newoptions [$number]['title'] = strip_tags ( stripslashes ( $_POST ['categories-title-' . $number] ) );
103
+ $newoptions [$number]['count'] = isset ( $_POST ['categories-count-' . $number] );
104
+ $newoptions [$number]['hierarchical'] = isset ( $_POST ['categories-hierarchical-' . $number] );
105
+ $newoptions [$number]['hide_empty'] = isset ( $_POST ['categories-hide_empty-' . $number] );
106
+ $newoptions [$number]['sort_column'] = strip_tags ( stripslashes ( $_POST ['categories-sort_column-' . $number] ) );
107
+ $newoptions [$number]['sort_order'] = strip_tags ( stripslashes ( $_POST ['categories-sort_order-' . $number] ) );
108
+ $newoptions [$number]['style'] = strip_tags ( stripslashes ( $_POST ['categories-style-' . $number] ) );
109
  if ($avh_extcat_canselectcats) {
110
+ if (in_array ( '-1', $_POST ['post_category-' . $number], true )) {
111
+ $newoptions [$number]['post_category'] = false;
112
  } else {
113
+ $newoptions [$number]['post_category'] = serialize ( $_POST ['post_category-' . $number] );
114
  }
115
  }
116
 
123
  }
124
 
125
  // Prepare data for display
126
+ $title = htmlspecialchars ( $options [$number]['title'], ENT_QUOTES );
127
+ $count = $options [$number]['count'] ? 'checked="checked"' : '';
128
+ $hierarchical = $options [$number]['hierarchical'] ? 'checked="checked"' : '';
129
+ $hide_empty = $options [$number]['hide_empty'] ? 'checked="checked"' : '';
130
+ $sort_id = ($options [$number]['sort_column'] == 'ID') ? ' SELECTED' : '';
131
+ $sort_name = ($options [$number]['sort_column'] == 'name') ? ' SELECTED' : '';
132
+ $sort_count = ($options [$number]['sort_column'] == 'count') ? ' SELECTED' : '';
133
+ $sort_order_a = ($options [$number]['sort_order'] == 'asc') ? ' SELECTED' : '';
134
+ $sort_order_d = ($options [$number]['sort_order'] == 'desc') ? ' SELECTED' : '';
135
+ $style_list = ($options [$number]['style'] == 'list') ? ' SELECTED' : '';
136
+ $style_drop = ($options [$number]['style'] == 'drop') ? ' SELECTED' : '';
137
  if ($avh_extcat_canselectcats) {
138
+ $selected_cats = ($options [$number]['post_category'] != '') ? unserialize ( $options [$number]['post_category'] ) : false;
139
  }
140
  ?>
141
  <div>
142
+ <label for="categories-title-<?php echo $number; ?>"><?php _e ( 'Title:' ); ?>
143
+ <input style="width: 250px;" id="categories-title-<?php echo $number; ?>" name="categories-title-<?php echo $number; ?>" type="text" value="<?php echo $title; ?>" />
144
  </label>
 
 
 
 
 
 
 
 
 
145
 
146
+ <label for="categories-count-<?php echo $number; ?>" style="line-height: 35px; display: block;">Show post counts
147
+ <input class="checkbox" type="checkbox" <?php echo $count; ?> id="categories-count-<?php echo $number; ?>" name="categories-count-<?php echo $number; ?>" />
148
+ </label>
149
+
150
+ <label for="categories-hierarchical" style="line-height: 35px; display: block;">Show hierarchy
151
+ <input class="checkbox" type="checkbox" <?php echo $hierarchical; ?> id="categories-hierarchical-<?php echo $number; ?>" name="categories-hierarchical-<?php echo $number; ?>" />
152
+ </label>
153
+
154
+ <label for="categories-hide_empty-<?php echo $number; ?>" style="line-height: 35px; display: block;">Hide empty categories
155
+ <input class="checkbox" type="checkbox" <?php echo $hide_empty; ?> id="categories-hide_empty-<?php echo $number; ?>" name="categories-hide_empty-<?php echo $number; ?>" />
156
+ </label>
157
+
158
+ <label for="categories-sort_column-<?php echo $number; ?>" style="line-height: 35px; display: block;">Sort by
159
+ <select id="categories-sort_column-<?php echo $number; ?>" name="categories-sort_column-<?php echo $number; ?>">
160
+ <option value="ID" <?php echo $sort_id?>>ID</option>
161
+ <option value="name" <?php echo $sort_name?>>Name</option>
162
+ <option value="count" <?php echo $sort_count?>>Count</option>
163
+ </select>
164
+ </label>
165
+
166
+ <label for="categories-sort_order-<?php echo $number; ?>" style="line-height: 35px; display: block;">Sort order
167
+ <select id="categories-sort_order-<?php echo $number; ?>" name="categories-sort_order-<?php echo $number; ?>">
168
+ <option value="asc" <?php echo $sort_order_a?>>Ascending</option>
169
+ <option value="desc" <?php echo $sort_order_d?>>Descending</option>
170
+ </select>
171
+ </label>
172
+
173
+ <label for="categories-style-<?php echo $number; ?>" style="line-height: 35px; display: block;">Display style
174
+ <select id="categories-style-<?php echo $number; ?>" name="categories-style-<?php echo $number; ?>">
175
+ <option value='list' <?php echo $style_list; ?>>List</option>
176
+ <option value='drop' <?php echo $style_drop; ?>>Drop down</option>
177
+ </select>
178
+ </label>
179
  <?php
180
  if ($avh_extcat_canselectcats) {
181
  echo ' <b>Include these categories</b><hr />';
182
  echo ' <ul id="categorychecklist" class="list:category categorychecklist form-no-clear" style="list-style-type: none; margin-left: 5px; padding-left: 0px; margin-bottom: 20px;">';
183
+ echo ' <li id="category--1-'.$number.'" class="popular-category">';
184
+ echo ' <label for="in-category--1-'.$number.'" class="selectit">';
185
+ echo ' <input value="-1" name="post_category-'.$number.'[]" id="in-category--1-'.$number.'" type="checkbox"';
186
  if (!$selected_cats) { echo 'checked'; }
187
  echo '> Include All Categories';
188
  echo ' </label>';
189
  echo ' </li>';
190
+ avh_wp_category_checklist(0,0,$selected_cats,false,$number);
191
  echo ' </ul>';
192
  }
193
  ?>
194
 
195
+ <input type="hidden" id="categories-submit-<?php echo $number; ?>" name="categories-submit-<?php echo $number; ?>" value="1" /></div>
 
196
  <?php
197
  }
198
+ /**
199
+ * Called after the widget_extended_categories_page form has been submitted.
200
+ * Set the amount of widgets wanted and register the widgets
201
+ *
202
+ */
203
+ function widget_extended_categories_setup() {
204
+ $options = $newoptions = get_option('widget_extended_categories');
205
+ if ( isset($_POST['extended_categories-number-submit']) ) {
206
+ $number = (int) $_POST['extended_categories-number'];
207
+ if ( $number > 9 ) $number = 9;
208
+ if ( $number < 1 ) $number = 1;
209
+ $newoptions['number'] = $number;
210
+ }
211
+ if ( $options != $newoptions ) {
212
+ $options = $newoptions;
213
+ update_option('widget_extended_categories', $options);
214
+ widget_extended_categories_register($options['number']);
215
+ }
216
+ }
217
+ /**
218
+ * How many Wish List widgets are wanted.
219
+ *
220
+ */
221
+ function widget_extended_categories_page() {
222
+ $options = get_option('widget_extended_categories');
223
+ ?>
224
+ <div class="wrap">
225
+ <form method="post">
226
+ <h2><?php _e('AVH Extended Categories Widgets', 'avhextendedcategories'); ?></h2>
227
+ <p style="line-height: 30px;"><?php _e('How many wishlist widgets would you like?', 'avhextendedcategories'); ?>
228
+ <select id="extended_categories-number" name="extended_categories-number" value="<?php echo $options['number']; ?>">
229
+ <?php for ( $i = 1; $i < 10; ++$i ) echo "<option value='$i' ".($options['number']==$i ? "selected='selected'" : '').">$i</option>"; ?>
230
+ </select>
231
+ <span class="submit"><input type="submit" name="extended_categories-number-submit" id="extended_categories-number-submit" value="<?php echo attribute_escape(__('Save', 'avhextendedcategories')); ?>" /></span></p>
232
+ </form>
233
+ </div>
234
+ <?php
235
+ }
236
  function widget_extended_categories_register() {
237
+ $options = get_option( 'widget_extended_categories' );
238
+
239
+ $number = ( int ) $options['number'];
240
+ if ( $number < 1 ) $number = 1;
241
+ if ( $number > 9 ) $number = 9;
242
+ for ( $i = 1; $i <= 9; $i ++ ) {
243
+ $id = "extended-categories-$i";
244
+ $name = sprintf( __('Extended Categories %d'), $i );
245
+ wp_register_sidebar_widget( $id, $name, $i <= $number ? 'widget_extended_categories' : /* unregister */ '', array ( 'classname'=>'widget_extended_categories_init'), $i );
246
+ wp_register_widget_control( $id, $name, $i <= $number ? 'widget_extended_categories_control' : /* unregister */ '', array ( 'width'=>300, 'height'=>270), $i );
247
+ }
248
+ add_action('sidebar_admin_setup', 'widget_extended_categories_setup');
249
+ add_action('sidebar_admin_page', 'widget_extended_categories_page');
250
  }
251
 
252
  // Launch Widgets
253
  widget_extended_categories_register ();
254
  }
255
  add_action ( 'plugins_loaded', 'widget_extended_categories_init' );
256
+
257
+ /**
258
+ * As the original wp_category_checklist doesn't support multiple lists on the same page I needed to duplicate the functions
259
+ * use by the wp_category_checklist function
260
+ *
261
+ */
262
+ /**
263
+ * Class that will display the categories
264
+ *
265
+ */
266
+ class AVH_Walker_Category_Checklist extends Walker {
267
+ var $tree_type = 'category';
268
+ var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
269
+ var $number;
270
+
271
+ function start_lvl(&$output, $depth, $args) {
272
+ $indent = str_repeat("\t", $depth);
273
+ $output .= "$indent<ul class='children'>\n";
274
+ }
275
+
276
+ function end_lvl(&$output, $depth, $args) {
277
+ $indent = str_repeat("\t", $depth);
278
+ $output .= "$indent</ul>\n";
279
+ }
280
+
281
+ function start_el(&$output, $category, $depth, $args) {
282
+ extract($args);
283
+
284
+ $class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : '';
285
+ $output .= "\n<li id='category-$category->term_id-$this->number'$class>" . '<label for="in-category-' . $category->term_id . '-' .$this->number. '" class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="post_category-' . $this->number . '[]" id="in-category-' . $category->term_id . '-'.$this->number.'"' . (in_array( $category->term_id, $selected_cats ) ? ' checked="checked"' : "" ) . '/> ' . wp_specialchars( apply_filters('the_category', $category->name )) . '</label>';
286
+ }
287
+
288
+ function end_el(&$output, $category, $depth, $args) {
289
+ $output .= "</li>\n";
290
+ }
291
+ }
292
+
293
+ /**
294
+ * Creates the categories checklist
295
+ *
296
+ * @param unknown_type $post_id
297
+ * @param unknown_type $descendants_and_self
298
+ * @param unknown_type $selected_cats
299
+ * @param unknown_type $popular_cats
300
+ * @param unknown_type $number
301
+ */
302
+ function avh_wp_category_checklist( $post_id = 0, $descendants_and_self = 0, $selected_cats = false, $popular_cats = false, $number ) {
303
+ $walker = new AVH_Walker_Category_Checklist;
304
+ $walker->number = $number;
305
+
306
+ $descendants_and_self = (int) $descendants_and_self;
307
+
308
+ $args = array();
309
+ if ( is_array( $selected_cats ) )
310
+ $args['selected_cats'] = $selected_cats;
311
+ elseif ( $post_id )
312
+ $args['selected_cats'] = wp_get_post_categories($post_id);
313
+ else
314
+ $args['selected_cats'] = array();
315
+
316
+ if ( is_array( $popular_cats ) )
317
+ $args['popular_cats'] = $popular_cats;
318
+ else
319
+ $args['popular_cats'] = get_terms( 'category', array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
320
+
321
+ if ( $descendants_and_self ) {
322
+ $categories = get_categories( "child_of=$descendants_and_self&hierarchical=0&hide_empty=0" );
323
+ $self = get_category( $descendants_and_self );
324
+ array_unshift( $categories, $self );
325
+ } else {
326
+ $categories = get_categories('get=all');
327
+ }
328
+
329
+ // 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)
330
+ $checked_categories = array();
331
+ for ( $i = 0; isset($categories[$i]); $i++ ) {
332
+ if ( in_array($categories[$i]->term_id, $args['selected_cats']) ) {
333
+ $checked_categories[] = $categories[$i];
334
+ unset($categories[$i]);
335
+ }
336
+ }
337
+
338
+ // Put checked cats on top
339
+ echo call_user_func_array(array(&$walker, 'walk'), array($checked_categories, 0, $args));
340
+ // Then the rest of them
341
+ echo call_user_func_array(array(&$walker, 'walk'), array($categories, 0, $args));
342
+ }
343
+
344
  ?>