Changeset 605991
- Timestamp:
- 09/30/2012 03:21:12 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cat-tag-filter-widget/trunk/cat-tag-filter.php
r548888 r605991 5 5 Description: This plugin adds a widget to your WordPress site that allows your visitors to filter posts by category and tag. 6 6 Author: Ajay Verma 7 Version: 0. 77 Version: 0.8 8 8 Author URI: http://ajayver.com/ 9 9 */ … … 12 12 */ 13 13 14 if ($_POST["ctf_submit"] == 1) { 15 require( '../../../wp-load.php' ); 16 $cat = ''; 14 if ($_POST["ctf_submit"] == 1) { //if submit button was pressed 15 require( '../../../wp-load.php' ); //require wordpress core functions 16 $cat = ''; //initialize variables for category and tag parts of the url 17 17 $tag = ''; 18 $tag_prefix = '?'. $_POST["tag_prefix"] .'='; 18 $tag_prefix = '?'. $_POST["tag_prefix"] .'='; //get tag and category prefix 19 19 $cat_prefix = '?'. $_POST["cat_prefix"] .'='; 20 if ($_POST["tag_logic"] == 1) $tag_logic = '+'; 20 if ($_POST["tag_logic"] == 1) $tag_logic = '+'; //logic for tags (AND or OR) 21 21 else $tag_logic = ','; 22 if ($_POST["cat"] != -1) { 23 $_POST['cat'] = get_term($_POST['cat'], $_POST['category_tax']); 24 $cat = '/'. $cat_prefix . $_POST['cat']->slug; 25 $tag_prefix = '&' . $_POST["tag_prefix"] . '='; 22 if ($_POST["cat"] != -1) { //if a category was chosen 23 $_POST['cat'] = get_term($_POST['cat'], $_POST['category_tax']); //get category slug from it's id 24 $cat = '/'. $cat_prefix . $_POST['cat']->slug; //add it to category part of the url 25 $tag_prefix = '&' . $_POST["tag_prefix"] . '='; //add & to tag part of url 26 26 } 27 27 28 if ($_POST["tag"] && $_POST["tag"][0] != -1) { 29 $tag .= $tag_prefix; 28 if ($_POST["tag"] && $_POST["tag"][0] != -1) { //if any tags were chosen 29 $tag .= $tag_prefix; //add tag prefix to tag part of url 30 30 $i = 0; 31 31 foreach ($_POST["tag"] as $tags){ 32 if ($i > 0) $tag .= $tag_logic; 32 if ($i > 0) $tag .= $tag_logic; //add all tags with ',' or '+' between them depends on logic 33 33 $tag .= $tags; 34 34 $i++; … … 36 36 } 37 37 38 if ($cat == '' && $tag == '' && $_POST["blog_url"] != '') $url = $_POST["home_url"] . '/' . $_POST["blog_url"]; 39 else $url = $_POST["home_url"] . $cat . $tag; 40 header('Location: ' . $url); 38 if ($cat == '' && $tag == '' && $_POST["blog_url"] != '') $url = $_POST["home_url"] . '/' . $_POST["blog_url"]; //if no category or no tag was chosen, just create a link to blog 39 else $url = $_POST["home_url"] . $cat . $tag; //else, create a link to blog + category and tag arguments 40 header('Location: ' . $url); //redirect user to the created url 41 41 exit; 42 42 … … 46 46 } 47 47 48 function current_tax() { 49 global $wp_query, $ctf_options, $current_tax; 50 51 $queries = $wp_query->tax_query->queries; 52 53 if(!empty($queries)){ 54 foreach ($queries as $query) { 55 if ($query['taxonomy'] == $ctf_options['tag_tax']) $current_tax['tags'] = $query['terms']; 56 else if ($query['taxonomy'] == $ctf_options['category_tax']) $current_tax['cats'] = $query['terms']; 57 } 58 59 if(!empty($current_tax['cats'])){ 60 foreach ($current_tax['cats'] as $current_cat => $value) { 61 $cat_id = get_term_by('slug', $value, $ctf_options['category_tax']); 62 $current_tax['cats'][$current_cat] = $cat_id->term_id; 63 }} 64 65 return $current_tax; }} 66 67 68 function cat_options(){ 48 function current_tax() { //determine current tag and category 49 global $wp_query, $ctf_options, $current_tax; 50 51 $queries = $wp_query->tax_query->queries; 52 53 if(!empty($queries)){ 54 foreach ($queries as $query) { 55 if ($query['taxonomy'] == $ctf_options['tag_tax']) $current_tax['tags'] = $query['terms']; 56 else if ($query['taxonomy'] == $ctf_options['category_tax']) $current_tax['cats'] = $query['terms']; 57 } 58 59 if(!empty($current_tax['cats'])){ 60 foreach ($current_tax['cats'] as $current_cat => $value) { 61 $cat_id = get_term_by('slug', $value, $ctf_options['category_tax']); 62 $current_tax['cats'][$current_cat] = $cat_id->term_id; 63 } 64 } 65 66 return $current_tax; 67 } 68 } 69 70 71 function cat_options(){ //get categories dropdown list 69 72 global $ctf_options, $categories, $current_tax; 70 73 … … 76 79 'hierarchical' => 1, 77 80 'selected' => $current_tax['cats'][0], 78 'exclude'=> $ctf_options['exclude_cats'],81 $ctf_options['clude_cats'] => $ctf_options['exclude_cats'], 79 82 'orderby' => 'name' 80 83 ); … … 84 87 85 88 } 86 function tag_options($type){ 89 function tag_options($type){ //get tags 87 90 global $ctf_options, $current_tax; 88 91 89 if ($ctf_options['exclude_tags'] != '') $args[ 'exclude'] = $ctf_options['exclude_tags'];92 if ($ctf_options['exclude_tags'] != '') $args[$ctf_options['clude_tags']] = $ctf_options['exclude_tags']; 90 93 else $args = ''; 91 94 if($current_tax[cats][0]){ 92 95 93 94 'cat' => $current_tax[cats][0]95 );96 query_posts($cat_args);96 $cat_args = array( 97 'cat' => $current_tax[cats][0] 98 ); 99 query_posts($cat_args); 97 100 if(have_posts()): while (have_posts()) : the_post(); 98 101 $all_tag_objects = get_the_terms($post->ID, $ctf_options['tag_tax']); … … 111 114 if (!empty($tags)){ 112 115 foreach ($tags as $tag){ 113 $include .= $tag . ',';116 $include .= $tag . ','; 114 117 } 115 118 $include = substr($include, 0, -1); … … 122 125 123 126 124 125 126 127 128 129 $options .= "tag[]";130 $options .= '" value="' . $tag->slug . '"';131 if (is_array($current_tax['tags'])) {if (in_array($tag->slug, $current_tax['tags'])) $options .= ' checked ';}132 $options .= '>';133 134 135 136 137 127 if ($type == 1){ 128 $options .= '<ul>'; 129 foreach ($tags as $tag) { 130 $options .= '<li>'; 131 $options .= '<input type="checkbox" name="'; 132 $options .= "tag[]"; 133 $options .= '" value="' . $tag->slug . '"'; 134 if (is_array($current_tax['tags'])) {if (in_array($tag->slug, $current_tax['tags'])) $options .= ' checked ';} 135 $options .= '>'; 136 $options .= $tag->name; 137 if ($ctf_options['tags_count'] == 1) $options .= ' (' . $tag->count . ')'; 138 $options .= '</li>'; 139 } 140 $options .= '</ul>'; 138 141 } 139 142 else { 140 143 141 $options .= '<select name="' . "tag[]" . '" id="ctf-tag-select" ><option value="-1">'; 142 if ($ctf_options['all_tags_text'] != '') $options .= $ctf_options['all_tags_text']; else $options .=__('Any tag', 'cat-tag-filter'); 143 $options .= '</option>'; 144 foreach ($tags as $tag) { 145 $options .= '<option value="' . $tag->slug . '"'; 146 if (is_array($current_tax['tags'])) {if (in_array($tag->slug, $current_tax['tags'])) $options .= ' selected="selected" ';} 147 $options .= '>'; 148 $options .= $tag->name; 149 if ($ctf_options['tags_count'] == 1) $options .= ' (' . $tag->count . ')'; 150 $options .= '</option>'; 151 } 144 $options .= '<select name="' . "tag[]" . '" id="ctf-tag-select" ><option value="-1">'; 145 if ($ctf_options['all_tags_text'] != '') $options .= $ctf_options['all_tags_text']; 146 else $options .=__('Any tag', 'cat-tag-filter'); 147 $options .= '</option>'; 148 foreach ($tags as $tag) { 149 $options .= '<option value="' . $tag->slug . '"'; 150 if (is_array($current_tax['tags'])) { 151 if (in_array($tag->slug, $current_tax['tags'])) $options .= ' selected="selected" '; 152 } 153 $options .= '>'; 154 $options .= $tag->name; 155 if ($ctf_options['tags_count'] == 1) $options .= ' (' . $tag->count . ')'; 156 $options .= '</option>'; 157 } 152 158 $options .= '</select>'; } 153 159 return $options; 154 160 } 155 161 function ctf_widget(){ 156 162 global $ctf_options; 157 163 158 164 current_tax(); … … 160 166 ?> 161 167 162 <form action="<?php echo get_bloginfo('wpurl') . '/' . PLUGINDIR . '/' . dirname(plugin_basename(__FILE__)) . '/cat-tag-filter.php'; ?>" method="post"> 163 <?php if ($ctf_options['cat_list_label']) echo '<label for="ctf-cat-select">' . $ctf_options['cat_list_label'] . '</label>'; ?> 164 165 <?php cat_options(); ?> <br /> 166 167 <?php if ($ctf_options['tag_list_label']) echo '<label for="ctf-tag-select">' . $ctf_options['tag_list_label'] . '</label>'; ?> 168 <?php 169 170 echo tag_options($ctf_options['tag_type']); 171 172 ?> 168 <form action="<?php echo get_bloginfo('wpurl') . '/' . PLUGINDIR . '/' . dirname(plugin_basename(__FILE__)) . '/cat-tag-filter.php'; ?>" method="post"> 169 <div id="ctf-category-selector" <?php if ($ctf_options['cat_list_hide']) echo 'style="display:none;"'; ?> > 170 <?php if ($ctf_options['cat_list_label']) echo '<label for="ctf-cat-select">' . $ctf_options['cat_list_label'] . '</label>'; ?> 171 <?php cat_options(); ?> 172 </div> 173 <div id="ctf-tag-selector"> 174 <?php if ($ctf_options['tag_list_label']) echo '<label for="ctf-tag-select">' . $ctf_options['tag_list_label'] . '</label>'; ?> 175 <?php echo tag_options($ctf_options['tag_type']); ?> 176 </div> 173 177 <input type="hidden" name="ctf_submit" value="1" /> 174 178 <input type="hidden" name="home_url" value="<?php bloginfo('url'); ?>" /> … … 195 199 /** @see WP_Widget::widget */ 196 200 function widget($args, $instance) { 197 $defaults = array( 'title' => __('Filter', 'cat-tag-filter'), 'button_title' => __('Show posts', 'cat-tag-filter'), 'cat_list_ label' => __('Show posts from:', 'cat-tag-filter'), 'tag_list_label' => __('With tag:', 'cat-tag-filter'), 'all_cats_text' => __('Any category', 'cat-tag-filter'), 'all_tags_text' => __('Any tag', 'cat-tag-filter'), 'cats_count' => 1, 'tags_count' => 0, 'tag_logic' => 1, 'tag_type' => 0, 'exclude_tags' => '', 'exclude_cats' => '', 'tag_tax' => 'post_tag', 'category_tax' => 'category');201 $defaults = array( 'title' => __('Filter', 'cat-tag-filter'), 'button_title' => __('Show posts', 'cat-tag-filter'), 'cat_list_hide' => false, 'cat_list_label' => __('Show posts from:', 'cat-tag-filter'), 'tag_list_label' => __('With tag:', 'cat-tag-filter'), 'all_cats_text' => __('Any category', 'cat-tag-filter'), 'all_tags_text' => __('Any tag', 'cat-tag-filter'), 'cats_count' => 1, 'tags_count' => 0, 'tag_logic' => 1, 'tag_type' => 0, 'exclude_tags' => '', 'exclude_cats' => '', 'clude_tags' => 'exclude', 'clude_cats' => 'exclude', 'tag_tax' => 'post_tag', 'category_tax' => 'category'); 198 202 $instance = wp_parse_args( (array) $instance, $defaults ); 199 203 extract( $args ); … … 201 205 $ctf_options['title'] = apply_filters('widget_title', $instance['title']); 202 206 $ctf_options['button_title'] = apply_filters('widget_title', $instance['button_title']); 207 $ctf_options['cat_list_hide'] = $instance['cat_list_hide']; 203 208 $ctf_options['cat_list_label'] = apply_filters('widget_title', $instance['cat_list_label']); 204 209 $ctf_options['tag_list_label'] = apply_filters('widget_title', $instance['tag_list_label']); … … 210 215 $ctf_options['tag_type'] = $instance['tag_type']; 211 216 $ctf_options['exclude_tags'] = $instance['exclude_tags']; 217 $ctf_options['clude_tags'] = $instance['clude_tags']; 212 218 $ctf_options['exclude_cats'] = $instance['exclude_cats']; 219 $ctf_options['clude_cats'] = $instance['clude_cats']; 213 220 $ctf_options['category_tax'] = $instance['category_tax']; 214 221 $ctf_options['tag_tax'] = $instance['tag_tax']; … … 223 230 $instance['title'] = strip_tags($new_instance['title']); 224 231 $instance['button_title'] = strip_tags($new_instance['button_title']); 232 $instance['cat_list_hide'] = $new_instance['cat_list_hide']; 225 233 $instance['cat_list_label'] = strip_tags($new_instance['cat_list_label']); 226 234 $instance['tag_list_label'] = strip_tags($new_instance['tag_list_label']); … … 233 241 $instance['exclude_tags'] = $new_instance['exclude_tags']; 234 242 $instance['exclude_cats'] = $new_instance['exclude_cats']; 243 $instance['clude_tags'] = $new_instance['clude_tags']; 244 $instance['clude_cats'] = $new_instance['clude_cats']; 235 245 $instance['category_tax'] = $new_instance['category_tax']; 236 246 $instance['tag_tax'] = $new_instance['tag_tax']; … … 239 249 /** @see WP_Widget::form */ 240 250 function form($instance) { 241 $defaults = array( 'title' => __('Filter', 'cat-tag-filter'), 'button_title' => __('Show posts', 'cat-tag-filter'), 'cat_list_ label' => __('Show posts from:', 'cat-tag-filter'), 'tag_list_label' => __('With tag:', 'cat-tag-filter'), 'all_cats_text' => __('Any category', 'cat-tag-filter'), 'all_tags_text' => __('Any tag', 'cat-tag-filter'), 'cats_count' => 1, 'tags_count' => 0, 'tag_logic' => 1, 'tag_type' => 0, 'exclude_tags' => '', 'exclude_cats' => '', 'tag_tax' => 'post_tag', 'category_tax' => 'category' );251 $defaults = array( 'title' => __('Filter', 'cat-tag-filter'), 'button_title' => __('Show posts', 'cat-tag-filter'), 'cat_list_hide' => false, 'cat_list_label' => __('Show posts from:', 'cat-tag-filter'), 'tag_list_label' => __('With tag:', 'cat-tag-filter'), 'all_cats_text' => __('Any category', 'cat-tag-filter'), 'all_tags_text' => __('Any tag', 'cat-tag-filter'), 'cats_count' => 1, 'tags_count' => 0, 'tag_logic' => 1, 'tag_type' => 0, 'exclude_tags' => '', 'exclude_cats' => '', 'clude_tags' => 'exclude', 'clude_cats' => 'exclude', 'tag_tax' => 'post_tag', 'category_tax' => 'category' ); 242 252 $instance = wp_parse_args( (array) $instance, $defaults ); 243 253 ?> … … 253 263 <input type="text" id="ctf-widget-title" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($instance['title']);?>" /> 254 264 </p> 265 266 <p> 267 <input type="checkbox" id="ctf-cat-list-hide" name="<?php echo $this->get_field_name('cat_list_hide'); ?>" value="true" <?php if ($instance['cat_list_hide'] == true) echo "checked ";?>/> 268 <label for="ctf-cat-list-hide"> 269 <?php _e('Hide categories dropdown', 'cat-tag-filter'); ?> 270 </label> 271 </p> 255 272 256 273 <p> … … 274 291 <p> 275 292 <label for="ctf-exclude_cats"> 276 <?php _e('Coma separated category id\'s to exclude', 'cat-tag-filter'); ?>: 293 <?php _e('Coma separated category id\'s to ', 'cat-tag-filter'); ?><select name="<?php echo $this->get_field_name('clude_cats'); ?>" id="ctf-clude-cats" > 294 <option value="exclude" <?php if ($instance['clude_cats'] == 'exclude') echo 'selected="selected" ';?> > 295 <?php _e('exclude', 'cat-tag-filter'); ?> 296 </option> 297 <option value="include" <?php if ($instance['clude_cats'] == 'include') echo 'selected="selected" ';?> > 298 <?php _e('include', 'cat-tag-filter'); ?> 299 </option></select>: 277 300 </label> 278 301 <input type="text" id="ctf-exclude_cats" name="<?php echo $this->get_field_name('exclude_cats'); ?>" value="<?php echo esc_attr($instance['exclude_cats']);?>" /> … … 325 348 <p> 326 349 <label for="ctf-exclude_tags"> 327 <?php _e('Coma separated tag id\'s to exclude', 'cat-tag-filter'); ?>: 350 <?php _e('Coma separated tag id\'s to ', 'cat-tag-filter'); ?><select name="<?php echo $this->get_field_name('clude_tags'); ?>" id="ctf-clude-tags" > 351 <option value="exclude" <?php if ($instance['clude_tags'] == 'exclude') echo 'selected="selected" ';?> > 352 <?php _e('exclude', 'cat-tag-filter'); ?> 353 </option> 354 <option value="include" <?php if ($instance['clude_tags'] == 'include') echo 'selected="selected" ';?> > 355 <?php _e('include', 'cat-tag-filter'); ?> 356 </option></select>: 328 357 </label> 329 358 <input type="text" id="ctf-exclude_tags" name="<?php echo $this->get_field_name('exclude_tags'); ?>" value="<?php echo esc_attr($instance['exclude_tags']);?>" />
Note: See TracChangeset
for help on using the changeset viewer.