Plugin Directory

Changeset 605991


Ignore:
Timestamp:
09/30/2012 03:21:12 AM (12 years ago)
Author:
ajayver
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • cat-tag-filter-widget/trunk/cat-tag-filter.php

    r548888 r605991  
    55Description: This plugin adds a widget to your WordPress site that allows your visitors to filter posts by category and tag.
    66Author: Ajay Verma
    7 Version: 0.7
     7Version: 0.8
    88Author URI: http://ajayver.com/
    99*/
     
    1212*/
    1313
    14 if ($_POST["ctf_submit"] == 1) {
    15     require( '../../../wp-load.php' );
    16     $cat = '';
     14if ($_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
    1717    $tag = '';
    18     $tag_prefix = '?'. $_POST["tag_prefix"] .'=';
     18    $tag_prefix = '?'. $_POST["tag_prefix"] .'='; //get tag and category prefix
    1919    $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)
    2121    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
    2626    }
    2727
    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
    3030        $i = 0;
    3131        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
    3333            $tag .= $tags; 
    3434            $i++;
     
    3636    }   
    3737   
    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
    4141    exit;
    4242   
     
    4646    }
    4747   
    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(){
     48function 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
     71function cat_options(){ //get categories dropdown list
    6972  global $ctf_options, $categories, $current_tax;
    7073       
     
    7679  'hierarchical' => 1,
    7780  'selected' => $current_tax['cats'][0],
    78   'exclude'      => $ctf_options['exclude_cats'],
     81  $ctf_options['clude_cats']     => $ctf_options['exclude_cats'],
    7982  'orderby' => 'name'
    8083  );
     
    8487 
    8588}
    86 function tag_options($type){
     89function tag_options($type){ //get tags
    8790  global $ctf_options, $current_tax;
    8891 
    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'];
    9093  else $args = '';
    9194  if($current_tax[cats][0]){
    9295
    93   $cat_args = array(
    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);
    97100    if(have_posts()): while (have_posts()) : the_post();
    98101        $all_tag_objects = get_the_terms($post->ID, $ctf_options['tag_tax']);
     
    111114    if (!empty($tags)){
    112115        foreach ($tags as $tag){
    113         $include .= $tag . ',';
     116            $include .= $tag . ',';
    114117        }
    115118        $include = substr($include, 0, -1);
     
    122125
    123126 
    124   if ($type == 1){
    125    $options .= '<ul>';
    126   foreach ($tags as $tag) {
    127    $options .= '<li>';
    128     $options .= '<input type="checkbox" name="';
    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     $options .= $tag->name;
    134     if ($ctf_options['tags_count'] == 1) $options .= ' (' . $tag->count . ')';
    135     $options .= '</li>';   
    136   }
    137   $options .= '</ul>';
     127if ($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>';
    138141  }
    139142  else { 
    140143 
    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    }
    152158  $options .= '</select>'; }
    153159  return $options;
    154160}
    155161function ctf_widget(){
    156   global $ctf_options;
     162    global $ctf_options;
    157163   
    158164    current_tax();
     
    160166  ?>
    161167
    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>
    173177    <input type="hidden" name="ctf_submit" value="1" />
    174178    <input type="hidden" name="home_url" value="<?php bloginfo('url'); ?>" />
     
    195199  /** @see WP_Widget::widget */
    196200  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');
    198202    $instance = wp_parse_args( (array) $instance, $defaults );
    199203    extract( $args );
     
    201205    $ctf_options['title'] = apply_filters('widget_title', $instance['title']);
    202206    $ctf_options['button_title'] = apply_filters('widget_title', $instance['button_title']);
     207    $ctf_options['cat_list_hide'] =  $instance['cat_list_hide'];
    203208    $ctf_options['cat_list_label'] = apply_filters('widget_title', $instance['cat_list_label']);
    204209    $ctf_options['tag_list_label'] = apply_filters('widget_title', $instance['tag_list_label']);
     
    210215    $ctf_options['tag_type'] =  $instance['tag_type'];
    211216    $ctf_options['exclude_tags'] = $instance['exclude_tags'];
     217    $ctf_options['clude_tags'] = $instance['clude_tags'];
    212218    $ctf_options['exclude_cats'] = $instance['exclude_cats'];
     219    $ctf_options['clude_cats'] = $instance['clude_cats'];
    213220    $ctf_options['category_tax'] = $instance['category_tax'];
    214221    $ctf_options['tag_tax'] = $instance['tag_tax'];
     
    223230    $instance['title'] = strip_tags($new_instance['title']);
    224231    $instance['button_title'] = strip_tags($new_instance['button_title']);
     232    $instance['cat_list_hide'] = $new_instance['cat_list_hide'];
    225233    $instance['cat_list_label'] = strip_tags($new_instance['cat_list_label']);
    226234    $instance['tag_list_label'] = strip_tags($new_instance['tag_list_label']);
     
    233241    $instance['exclude_tags'] = $new_instance['exclude_tags'];
    234242    $instance['exclude_cats'] = $new_instance['exclude_cats'];
     243    $instance['clude_tags'] = $new_instance['clude_tags'];
     244    $instance['clude_cats'] = $new_instance['clude_cats'];
    235245    $instance['category_tax'] = $new_instance['category_tax'];
    236246    $instance['tag_tax'] = $new_instance['tag_tax'];
     
    239249  /** @see WP_Widget::form */
    240250  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' );
    242252    $instance = wp_parse_args( (array) $instance, $defaults );             
    243253    ?>   
     
    253263      <input type="text" id="ctf-widget-title" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($instance['title']);?>" />   
    254264    </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>
    255272       
    256273    <p>   
     
    274291    <p>   
    275292      <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>:
    277300      </label>   
    278301      <input type="text" id="ctf-exclude_cats" name="<?php echo $this->get_field_name('exclude_cats'); ?>" value="<?php echo esc_attr($instance['exclude_cats']);?>" />   
     
    325348    <p>   
    326349      <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>:
    328357      </label>   
    329358      <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.