Advertisement
Guest User

multiple-category-selection-widget (v3.1.2)/mcsw.php

a guest
May 23rd, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 18.06 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Multiple Category Selection Widget
  4. Plugin URI: http://wp.zackdesign.biz/category-selection-widget/
  5. Description: Select multiple categories at once using this widget!
  6. Author: Isaac Rowntree
  7. Version: 3.1.2
  8. Author URI: http://zackdesign.biz
  9.  
  10.     Copyright (c) 2005, 2006 Isaac Rowntree (http://zackdesign.biz)
  11.     QuickShop is released under the GNU General Public
  12.     License (GPL) http://www.gnu.org/licenses/gpl.txt
  13.  
  14.     This is a WordPress plugin (http://wordpress.org).
  15.  
  16. */
  17.  
  18. function wpmm_load_category()
  19. {    
  20.     session_start(); // Load the session in
  21.    
  22.     if (isset($_REQUEST['reset_mcsw']))
  23.     {
  24.        unset($_SESSION['wpmm_cats']);
  25.        unset($_SESSION['wpmm_search_vars']);
  26.        
  27.        wp_redirect(qtrans_convertURL(get_bloginfo('url')));
  28.        exit();
  29.     }
  30.    
  31.     if (!empty($_POST['wpmm']))
  32.     {      
  33.         $cats = $_POST['wpmm'];
  34.        
  35.         $sql = '';
  36.           $count = 0;
  37.         foreach ($cats as $cat)
  38.         {
  39.             if ($cat == 0)
  40.                 unset($cats[$count]);
  41.             else
  42.                 $sql .= $cat.',';
  43.             $count++;
  44.         }
  45.         $sql = trim ($sql, ',');
  46.         if (!empty($sql))
  47.         {
  48.           if (get_option('permalink_structure')!= '')
  49.           {
  50.               wp_redirect(qtrans_convertURL(get_bloginfo('url')).'/categories/'.$sql.'/search_type/'.$_POST['mmctype'].'/order/'.$_POST['order'].'/');
  51.                   exit();
  52.           }
  53.           else
  54.           {
  55.               wp_redirect(get_bloginfo('url').'/?cat='.$sql.'&search_type='.$_POST['mmctype'].'&order='.$_POST['order']);
  56.                   exit();
  57.           }
  58.         }
  59.         else if ($_POST['blank'] != 'none') // Blank search
  60.         {
  61.             if (!$_POST['default'])
  62.             {
  63.                 if ((get_option('show_on_front') == 'page') && (get_option('page_for_posts')))
  64.                 {
  65.                     wp_redirect(get_permalink(get_option('page_for_posts')));
  66.                     exit();
  67.                 }
  68.                 else
  69.                 {
  70.                     wp_redirect(get_bloginfo('url'));
  71.                     exit();
  72.                 }
  73.             }
  74.             else
  75.             {
  76.                 wp_redirect(get_category_link($_POST['default']));
  77.                 exit();
  78.             }
  79.         }
  80.     }
  81. }
  82.  
  83.  
  84. function categories_queryvars( $qvars )
  85. {
  86.   $qvars[] = 'search_type';  
  87.   return $qvars;
  88. }
  89.  
  90. function wpmm_parse($vars)
  91. {
  92.     if (!empty($vars->query_vars['search_type']))
  93.     {
  94.         $cats = explode(',',$vars->query_vars['cat']);
  95.         $type = $vars->query_vars['search_type'];
  96.         $order = $vars->query_vars['order'];
  97.    
  98.         global $wp;
  99.         $wp->set_query_var('category__'.$type, $cats);
  100.        
  101.         //$taxonomy = array('relation' => 'OR', array('terms' => $cats));
  102.         //$wp->set_query_var('tax_query', $taxonomy);
  103.         //print_r($wp);
  104.        
  105.         if ($order == 'title')
  106.         {
  107.             $wp->set_query_var('orderby', 'title');
  108.             $wp->set_query_var('order', 'asc');
  109.         }
  110.        
  111.         $_SESSION['wpmm_cats'] = $vars->query_vars['cat'];
  112.         $_SESSION['wpmm_search_vars'] = $type;
  113.     }
  114. }
  115.  
  116. function wpmm_add_rewrite_rules( $wp_rewrite )
  117. {
  118.   $new_rules = array(
  119.      'categories/(.+?)/search_type/(.+?)/order/(.+?)/page/(.+?)/?$' => 'index.php?cat=' .
  120.        $wp_rewrite->preg_index(1).'&search_type=' . $wp_rewrite->preg_index(2).'&order='.$wp_rewrite->preg_index(3).'&paged='. $wp_rewrite->preg_index(4),
  121.      'categories/(.+?)/search_type/(.+?)/order/(.+?)/?$' => 'index.php?cat=' .
  122.        $wp_rewrite->preg_index(1).'&search_type=' . $wp_rewrite->preg_index(2).'&order='.$wp_rewrite->preg_index(3),    
  123.      'categories/(.+)/page/(.+)/?$' => 'index.php?cat=' .
  124.        $wp_rewrite->preg_index(1).'&paged=' . $wp_rewrite->preg_index(2),
  125.      'categories/(.+)/?$' => 'index.php?cat=' .
  126.        $wp_rewrite->preg_index(1));
  127.  
  128.   $wp_rewrite->rules = array_merge($new_rules, $wp_rewrite->rules);
  129.    
  130. }
  131.  
  132.  
  133.  
  134.  
  135. function mcsw_admin_add_page()
  136. {
  137.   add_options_page('Multiple Category Selection', 'Multiple Category Selection', 'administrator', __FILE__, 'wpmcsw_options_page',plugins_url('/images/icon.png', __FILE__));
  138.   add_action('admin_init', 'mcsw_settings_api_init');
  139. }
  140.  
  141. function wpmcsw_options_page()
  142. {
  143.   require_once('admin-form.php');
  144. }
  145.  
  146. function mcsw_settings_api_init()
  147. {
  148.   register_setting( 'mcsw', 'mcsw_ajax' );
  149.   register_setting( 'mcsw', 'mcsw_form_display' );
  150.   register_setting( 'mcsw', 'mcsw_form' );
  151.   add_settings_section('mcsw_main', 'Main Settings', 'mcsw_main_text', 'mcsw');  
  152.   add_settings_section('mcsw_form', 'Shortcode and Results Form Settings', 'mcsw_shortcode_text', 'mcsw');
  153.   add_settings_field('mcsw_ajax', 'AJAX Chaining', 'mcsw_ajax', 'mcsw', 'mcsw_main');  
  154.   add_settings_field('mcsw_form_display', 'Display Form', 'mcsw_form_display', 'mcsw', 'mcsw_main');  
  155.   add_settings_field('mcsw_form', '', 'mcsw_form', 'mcsw', 'mcsw_form');
  156. }
  157.  
  158. function mcsw_main_text() {
  159. }
  160.  
  161. function mcsw_shortcode_text()
  162. {
  163.   echo '<p>Use the shortcode [mcsw] to generate a form on any page you wish.</p>';
  164. }
  165.  
  166. function mcsw_ajax() {
  167.   $options = get_option('mcsw_ajax');
  168.   if ($options)
  169.     $selected = 'checked="checked"';
  170.   else
  171.     $selected=  '';
  172.   echo "<fieldset><legend class='screen-reader-text'><span>AJAX Chaining</span></legend><label for='mcsw_ajax'>
  173.        <input name='mcsw_ajax' type='checkbox' id='mcsw_ajax' value='1' {$selected} />
  174.        Allow more than one level of categories to be displayed using AJAX</label>
  175.        </fieldset>";
  176. }
  177.  
  178. function mcsw_form_display() {
  179.   $options = get_option('mcsw_form_display');
  180.   if ($options)
  181.     $selected = 'checked="checked"';
  182.   else
  183.     $selected=  '';
  184.   echo "<fieldset><legend class='screen-reader-text'><span>Display form above results</span></legend><label for='mcsw_form_display'>
  185.        <input name='mcsw_form_display' type='checkbox' id='mcsw_form_display' value='1' {$selected} />
  186.        Above results</label>
  187.        </fieldset>";
  188. }
  189.  
  190. function mcsw_form()
  191. {
  192.   require_once('settings_fields.php');
  193. }
  194.  
  195. function wpmm_activate() {
  196.   global $wp_rewrite;
  197.   $wp_rewrite->flush_rules();
  198. }
  199.  
  200. register_activation_hook( __FILE__, 'wpmm_activate' );
  201.  
  202. // Get Wordpress to parse URL for query categories
  203. add_filter('query_vars', 'categories_queryvars' );
  204.  
  205. // Run the category selection
  206. add_action('parse_request','wpmm_parse');
  207.  
  208. // Rewrite URL
  209. add_action('init','wpmm_load_category');  
  210. add_action('init','wpmm_activate');
  211. add_action('generate_rewrite_rules', 'wpmm_add_rewrite_rules');  
  212. add_action('loop_start', 'mcsw_loop_form');
  213.  
  214. add_action('widgets_init', create_function('', 'return register_widget("MCSWwidget");'));
  215.  
  216. // Shortcode for page form
  217. add_shortcode( 'mcsw', 'mcsw_shortcode_handler' );
  218.  
  219. // Options page
  220. add_action('admin_menu', 'mcsw_admin_add_page');  
  221.  
  222. /**
  223.  * wpmm widget Class
  224.  */
  225. class MCSWwidget extends WP_Widget {
  226.     /** constructor */
  227.     function MCSWwidget() {
  228.         parent::WP_Widget(false, $name = 'Multi-Category');
  229.     }
  230.  
  231.     /** @see WP_Widget::widget */
  232.     function widget($args, $instance) {
  233.    
  234.         prepare_mcsw_form($args, $instance);
  235.        
  236.     }
  237.  
  238.     /** @see WP_Widget::update */
  239.     function update($new_instance, $old_instance) {            
  240.         return $new_instance;
  241.     }
  242.  
  243.     /** @see WP_Widget::form */
  244.     function form($instance) {             
  245.         if (isset($instance['title'])) : $title = esc_attr($instance['title']); else: $title = ''; endif;
  246.         if (isset($instance['excluded_cats'])) : $excluded_cats = esc_attr($instance['excluded_cats']); else: $excluded_cats = ''; endif;
  247.         if (isset($instance['default_category'])) : $default_category = esc_attr($instance['default_category']); else: $default_category = ''; endif;
  248.        
  249.         if (isset($instance['def_search_type'])) : $def_search_type = esc_attr($instance['def_search_type']); else: $def_search_type = ''; endif;
  250.        
  251.         if ($def_search_type == 'in')
  252.             $def_search_type_in = 'checked="checked"';
  253.         else
  254.             $def_search_type_and = 'checked="checked"';
  255.        
  256.         if (isset($instance['blank_search_type'])) :
  257.             $blank_search_type = esc_attr($instance['blank_search_type']);
  258.             if ($blank_search_type == 'none')
  259.                 $blank_search_none = 'checked="checked"';
  260.             else
  261.                 $blank_search_all = 'checked="checked"';
  262.         else :
  263.                 $blank_search_none = 'checked="checked"';
  264.         endif;
  265.    
  266.         if (isset($instance['order'])) :
  267.             $order = esc_attr($instance['order']);
  268.             if ($order == 'default')
  269.                 $order_default = 'checked="checked"';
  270.             else
  271.                 $order_title = 'checked="checked"';
  272.         else :
  273.                 $order_default = 'checked="checked"';
  274.         endif;
  275.            
  276.         if (isset($instance['user_choice'])) : $user_choice = esc_attr($instance['user_choice']); $user_choice_checked = 'checked="checked"'; endif;
  277.        
  278.         ?>
  279.             <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('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 $title; ?>" /></label></p>
  280.         <p><label for="<?php echo $this->get_field_id('excluded_cats'); ?>"><?php _e('Comma Seperated Excluded Category IDs:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('excluded_cats'); ?>" name="<?php echo $this->get_field_name('excluded_cats'); ?>" type="text" value="<?php echo $excluded_cats; ?>" /></label></p>
  281.         <p><input id="<?php echo $this->get_field_id('user_choice'); ?>" name="<?php echo $this->get_field_name('user_choice'); ?>" type="checkbox" <?php echo $user_choice_checked; ?> /> <label for="<?php echo $this->get_field_id('user_choice'); ?>"><?php _e('User Chooses Search Type'); ?> </label></p>
  282.         <p><label for="<?php echo $this->get_field_id('def_search_type'); ?>"><?php _e('Default Search Type:'); ?></label></p>
  283.         <p><input id="<?php echo $this->get_field_id('def_search_type'); ?>" name="<?php echo $this->get_field_name('def_search_type'); ?>" type="radio" value="and" <?php echo $def_search_type_and; ?> /> <?php _e('All'); ?> </p>
  284.         <p><input id="<?php echo $this->get_field_id('def_search_type'); ?>" name="<?php echo $this->get_field_name('def_search_type'); ?>" type="radio" value="in"  <?php echo $def_search_type_in; ?> /> <?php _e('Any'); ?> </p>
  285.         <p><label for="<?php echo $this->get_field_id('default_category'); ?>"><?php _e('Default category ID shown for blank search:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('default_category'); ?>" name="<?php echo $this->get_field_name('default_category'); ?>" type="text" value="<?php echo $default_category; ?>" /></label></p>
  286.          <p><label for="<?php echo $this->get_field_id('blank_search_type'); ?>"><?php _e('Blank Search Results:'); ?></label></p>
  287.         <p><input id="<?php echo $this->get_field_id('blank_search_type'); ?>" name="<?php echo $this->get_field_name('blank_search_type'); ?>" type="radio" value="none" <?php echo $blank_search_none; ?> /> <?php _e('None'); ?> </p>
  288.         <p><input id="<?php echo $this->get_field_id('blank_search_type'); ?>" name="<?php echo $this->get_field_name('blank_search_type'); ?>" type="radio" value="all"  <?php echo $blank_search_all; ?> /> <?php _e('All, or the default category ID above'); ?> </p>
  289.         <p><label for="<?php echo $this->get_field_id('order'); ?>"><?php _e('Ordering:'); ?></label></p>
  290.         <p><input id="<?php echo $this->get_field_id('order'); ?>" name="<?php echo $this->get_field_name('order'); ?>" type="radio" value="default" <?php echo $order_default; ?> /> <?php _e('Default'); ?> </p>
  291.         <p><input id="<?php echo $this->get_field_id('order'); ?>" name="<?php echo $this->get_field_name('order'); ?>" type="radio" value="title"  <?php echo $order_title; ?> /> <?php _e('Title'); ?> </p>
  292.         <?php
  293.     }
  294.  
  295. } // class MCSWidget    
  296.  
  297. function prepare_categories($categories, $callback = 0)
  298. {
  299.     // Get custom post taxonomies
  300.     $args=array(
  301.       'public'   => true,
  302.       '_builtin' => false
  303.     );    
  304.    
  305.     //$custom = get_taxonomies($args, 'objects');
  306.     if (!empty($custom)) :
  307.     foreach ($custom as $name => $object)
  308.     {
  309.       $custom_taxonomy = get_categories('taxonomy='.$name);
  310.      
  311.       if (!empty($custom_taxonomy))
  312.         $categories = array_merge($categories, $custom_taxonomy);
  313.     }
  314.     endif;
  315.    
  316.     $parents = array();
  317.     $children = array();
  318.    
  319.     $cat_id = NULL;
  320.    
  321.    if (isset($_POST['cat_id'])) : $cat_id = $_POST['cat_id']; endif;
  322.    
  323.     foreach ($categories as $cat)
  324.     {
  325.         if (!$cat->parent || $cat->parent == $cat_id)
  326.             $parents[] = $cat;
  327.         else
  328.             $children[] = $cat;
  329.     }
  330.    
  331.     $html = '';
  332.    
  333.     foreach ($parents as $p)
  334.     {
  335.         if ($callback)
  336.         $class = ' class="callback"';
  337.     else
  338.         $class = '';
  339.    
  340.     $html .= '
  341.    <div class="select_wrapper">
  342.        <select name="wpmm[]" '.$class.'>  
  343.        <option value="0">&raquo; '.apply_filters('single_cat_title', stripslashes(str_replace('"', '', $p->name))).'</option>';
  344.        
  345.         foreach ($children as $c)
  346.         {
  347.             $selected = '';
  348.            
  349.             if (!empty($_SESSION['wpmm_cats']))
  350.             {
  351.                 $cats = explode(',', $_SESSION['wpmm_cats']);
  352.                
  353.                 foreach ($cats as $cat)
  354.                 {
  355.                     if ($cat == $c->cat_ID)
  356.                         $selected = 'selected="selected"';
  357.                 }
  358.             }
  359.            
  360.             if ($p->cat_ID == $c->parent)
  361.                 $html .= '<option '.$selected.' value="'.$c->cat_ID.'"> '.apply_filters('single_cat_title', stripslashes(str_replace('"', '', $c->name))).'</option>';
  362.         }
  363.        
  364.         $html .= '</select></div>';
  365.     }
  366.    
  367.     if (empty($html))
  368.         return $html;
  369.    
  370.     if ($callback)
  371.         $html .='<br />';
  372.    
  373.     return $html;
  374. }
  375.  
  376. function prepare_mcsw_form($args, $instance)
  377. {
  378.   extract($args);
  379.  
  380.   $title = apply_filters('widget_title', $instance['title']);  
  381.   global $wpdb;
  382.                                  
  383.   $categories = prepare_categories(get_categories('orderby=name&hide_empty=1&exclude=1,'.$instance['excluded_cats']));
  384.  
  385.  
  386.   echo $before_widget;
  387.   echo $before_title . $title. $after_title .'
  388.  
  389.  <form action="" method="post" class="wpmcsw form">
  390.    <fieldset>
  391.    <input type="hidden" name="ajax_url" class="ajax_url" value="'.admin_url('admin-ajax.php').'"  />
  392.    <input type="hidden" name="ex_cats" class="ex_cats" value="'.$instance['excluded_cats'].'"  />
  393.    <input type="hidden" name="default" value="'.$instance['default_category'].'"  />
  394.    <input type="hidden" name="blank" value="'.$instance['blank_search_type'].'" />
  395.    <input type="hidden" name="order" value="'.$instance['order'].'"  />
  396.    <input type="hidden" name="mcsw" value="1"  />    
  397.  ';
  398.  
  399.   echo $categories;
  400.  
  401.   $checked1 = '';
  402.   $checked2 = '';  
  403.  
  404.   if (!isset($_SESSION['wpmm_search_vars']))
  405.     $_SESSION['wpmm_search_vars'] = $instance['def_search_type'];      
  406.  
  407.   //$instance['def_search_type']  $_SESSION['wpmm_search_vars']
  408.  
  409.   if ($_SESSION['wpmm_search_vars'] == 'and')
  410.       $checked2 = 'checked="checked"';
  411.   else
  412.       $checked1 = 'checked="checked"';
  413.  
  414.   if (!empty($instance['user_choice']))
  415.   {    
  416.       echo '
  417.      <dl><dt>Search Type</dt>
  418.      <dd><input type="radio" name="mmctype" value="in" '.$checked1.' title="Any" /><label for="mmctype">Any</label><br />
  419.      <input type="radio" name="mmctype" value="and" '.$checked2.' /><label for="mmctype">All</label></dd></dl>
  420.      <input type="submit" value="Search-Cerca-Buscar" name="search" class="search" /></fieldset></form>';
  421.   }
  422.   else
  423.   {
  424.       echo '<input type="hidden" name="mmctype" value="'.$_SESSION['wpmm_search_vars'].'" />
  425.      <input type="submit" value="Search-Cerca-Buscar" name="search" class="search" />
  426.      </fieldset>
  427.      </form>';
  428.   }
  429.  
  430.   echo '<form action="" method="post" class="wpmcsw reset"><input type="submit" value="Reset-Azzerare-Reajustar" /><input type="hidden" value="1" name="reset_mcsw" /></form>';
  431.  
  432.   echo $after_widget;
  433. }
  434.  
  435.  
  436. function mcsw_shortcode_handler( $atts, $content=null, $code="" ) {
  437.   $instance = get_option('mcsw_form');
  438.   $atts = array(    'before_widget' => '',
  439.                     'after_widget' => '',
  440.                     'before_title' => '<h3>',
  441.                     'after_title' => '</h3>',  );
  442.    
  443.   prepare_mcsw_form($atts, $instance);
  444. }
  445.  
  446. function mcsw_loop_form( ) {
  447.  
  448.   if (is_category()) :
  449.    
  450.     $form_view = get_option('mcsw_form_display');
  451.  
  452.     if ($form_view) :
  453.       $instance = get_option('mcsw_form');
  454.       $atts = array(    'before_widget' => '',
  455.                         'after_widget' => '',
  456.                         'before_title' => '<h3>',
  457.                         'after_title' => '</h3>' );
  458.      
  459.      prepare_mcsw_form($atts, $instance);
  460.    endif;
  461.  
  462.   endif;
  463. }
  464.  
  465. /* Chained selection code */
  466.  
  467. add_action('init', 'do_AJAX_Chain');
  468.  
  469. function do_AJAX_Chain()
  470. {
  471.   if (!is_admin()) // Don't load the scripts in the admin area ever
  472.   {
  473.     $ajax = get_option('mcsw_ajax');
  474.    
  475.     if ($ajax)
  476.     {
  477.       add_action('wp_print_scripts', 'select_chain_list_scripts');
  478.     }
  479.   }
  480. }
  481.  
  482. function select_chain_list_scripts ( )
  483. {
  484.   wp_enqueue_script( "mcsw-select-chain", path_join(WP_PLUGIN_URL, basename( dirname( __FILE__ ) )."/select-chain.js"), array( 'jquery' ) );
  485. }
  486.  
  487.  
  488. add_action('wp_ajax_show_chain', 'show_chain_callback');
  489. add_action('wp_ajax_nopriv_show_chain', 'show_chain_callback');
  490.  
  491. function show_chain_callback()
  492. {  
  493.     $categories = prepare_categories(get_categories('child_of='.$_REQUEST['cat_id'].'&orderby=name&hide_empty=1&exclude=1,'.$_REQUEST['ex_cats']), 1);
  494.    
  495.     if ($_POST['cat_id'])
  496.         print '<div class="callback">'.$categories.'</div>';
  497.  
  498.     die();
  499. }
  500.  
  501. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement