1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
| /*a function to check if post_is_in_descendant_category ,which will work with */
/**
* a function to check if post_is_in_descendant_category
* @param $cats the category object to check
* @return true if post_is_in_descendant_category or false if not in
*/
function zxd_post_is_in_descendant_category( $cats, $_post = null ){
foreach ( (array) $cats as $cat ) {
$descendants = get_term_children( (int) $cat, 'category');
if ( $descendants && in_category( $descendants, $_post ) ) return true;
}
return false;
}
/**
* get all top level categories
* @return top level categories objects array
*/
function getTopLevelCats() {
$allCats = get_all_category_ids();
$cats = array();
foreach ($allCats as $id){
$obj = get_category($id);
$name = get_cat_name($id);
if (!$obj -> parent) array_push($cats,$obj);
}
return $cats;
}
/**
* wp ajax collapsing categories class
*/
class ajax_cc_Widget extends WP_Widget{
function ajax_cc_Widget() {
$widget_ops = array('classname' => 'widget_ajax_cc', 'description' => 'WP ajax Collapsing Categories' );
$this->WP_Widget('ajax_cc_widget','ajax_cc_widget',$widget_ops);
}
/**
* implent widget fucntion
*/
function widget($args, $instance) {
extract($args, EXTR_SKIP);
echo $before_widget;
$title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
echo '<ul class="zxd_ajax_cc"' ;
echo ' zid="';
the_ID();
echo '">';
if(!$instance['zxdIncludeCats']) {
$zxdCatArrayPool = getTopLevelCats();
}
else {
$zxdCatArrayPool = array();
$zxdCatTempPool = preg_split('/[,]+/',$instance['zxdIncludeCats']);
sort($zxdCatTempPool);
foreach($zxdCatTempPool as $a) {
if(!get_category_by_slug($a)) {
$zxdCatArrayPool = getTopLevelCats();
break;
}
$obj = get_category_by_slug($a);
array_push($zxdCatArrayPool,$obj);
}
}
$zxdPostId = is_single()?get_the_ID():0;
foreach ($zxdCatArrayPool as $zxdCatObj){
$zxdCatid = $zxdCatObj -> term_id;
$zxdCatdesc = $zxdCatObj->description;
$zxdCatCount = $zxdCatObj->count;
echo '<li class="zxd_ajax_cc_li"><span class="zxd_expand">[+]</span>'.'<a ';
if(in_category( $zxdCatid,$zxdPostId)||zxd_post_is_in_descendant_category($zxdCatid,$zxdPostId)) {
echo 'class="zxd_current_cat" ';
}
echo 'class="'.get_category_link($zxdCatid).'" '.'title="'.$zxdCatdesc.'">'.get_cat_name( $zxdCatid ).'</a>'.'('.$zxdCatCount.')'.'</li>';
}
echo '</ul>' ;
echo $after_widget;
}
/**
* implent widget fucntion
*/
function update($new_instance, $old_instance) {
$instance = $old_instance;
$title = strip_tags($new_instance['title']);
$zxdIncludeCats=$new_instance['zxdIncludeCats'];
$instance = compact('title','zxdIncludeCats');
return $instance;
}
/**
* implent widget fucntion
*/
function form($instance) {
$defaults=array(
'title' => __('ajax Categories', 'WP ajax collapsing categories'),
'zxdIncludeCats' => '',
);
$options = wp_parse_args( $instance,$defaults );
extract($options);
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this-/>get_field_id('title'); ?>" name="< ?php echo $this->get_field_name('title'); ?>" type="text" value="< ?php echo esc_attr($title); ?>" /></label></p>
<p>include these categories (category slugs,separated by commas): </p>
<p><input type="text" name="<?php echo $this-/>get_field_name('zxdIncludeCats'); ?>" value="< ?php echo $zxdIncludeCats ?>" id="< ?php echo $this->get_field_id('zxdIncludeCats') ?>" </p>
< ?php
}
}
/**
* register_widget
*/
function zxd_widgets_init() {
register_widget('ajax_cc_Widget');
}
add_action( 'widgets_init', 'zxd_widgets_init' );
if ( !is_admin() ) {
function zxd_init_method() {
wp_register_script( 'wp-ajax-collapsing-categories', get_template_directory_uri().'/wp-ajax-collapsing-categories.js',array('jquery'),'1.0',true);
wp_enqueue_script( 'wp-ajax-collapsing-categories');
wp_localize_script( 'wp-ajax-collapsing-categories', 'ajaxCC', array( 'aj
|