/**
  * Updates the $instance of the widget in the database on Save
  *
  * @param $new_instance		array	New instance proposed for save
  * @param $old_instance		array	Old version of the instance
  *
  * return array 	Cleansed $instance with pre-processed taxonomies field added to save processing when displayed
  */
 function update($new_instance, $old_instance)
 {
     // Tidy up inputs
     $instance = $new_instance;
     $instance['title'] = strip_tags($new_instance['title']);
     $instance['category_title'] = strip_tags($new_instance['category_title']);
     $instance['set_categories'] = str_replace(' ', '', strip_tags($new_instance['set_categories']));
     $instance['set_pages'] = strip_tags($new_instance['set_pages']);
     return taxonomy_picker_taxonomies_array($instance);
     // Pre-process the instance for efficiency
 }
 public function __construct($instance, $args)
 {
     // Check whether we displaying the results of a prevous use (ie. silverghyll_tpicker is set)
     $this->inputs = apply_filters('tpicker_inputs', taxonomy_picker_tpicker_array());
     // Get the configuration options from the database
     $this->options = apply_filters('tpicker_options', get_option('taxonomy-picker-options'));
     if (empty($this->options['empty-terms'])) {
         $this->options['empty-terms'] = 'always';
     }
     // Upgrade defence for new option
     // Upgrade defence for v1.8 - won't be needed long term.  If taxonomies haven't been set, process the instance
     if (empty($instance['taxonomies'])) {
         $instance = taxonomy_picker_taxonomies_array($instance);
     }
     // Pre-process the instance for efficiency
     $this->combo = $instance['combo'];
     // Saved our combobox type
     $set_categories = '';
     //We may set a value
     if (!empty($instance['taxonomies']) and is_array($instance['taxonomies'])) {
         // Protect from nothing chosen
         foreach ($instance['taxonomies'] as $taxonomy_name => $data_item) {
             // Loop through chosen list of taxonomies
             if ($taxonomy_name == 'post_type') {
                 $tax_label = 'Post Type';
             } else {
                 $taxonomy = get_taxonomy($taxonomy_name);
                 // Taxonomy name is dynamic in case it has changed
                 if ($taxonomy_name == 'category' and !empty($instance['category_title'])) {
                     $tax_label = $instance['category_title'];
                 } elseif (is_object($taxonomy)) {
                     $tax_label = $taxonomy->label;
                 } else {
                     // Probably a string (e.g. post_type) so use the information we have
                     $tax_Label = $taxonomy_name;
                 }
             }
             // ==Post Type?
             $tax_label = __($tax_label, 'tpicker') . $this->options['punctuation'];
             $this->taxonomies[$tax_label] = $data_item;
         }
         ksort($this->taxonomies);
         //Put taxonomies into alpha label order
     }
     $this->taxonomies = apply_filters('tpicker_taxonomies', $this->taxonomies);
     // Filter taxonomy order
     if ($args) {
         extract($args);
     }
     // Unpack $before_widget etc
     $this->title = apply_filters('widget_title', $instance['title']);
     if ($this->title) {
         $this->title = $this->before_title . $this->title . $this->after_title;
     }
     // Wrap it
     $this->id = array_key_exists('id', $instance) ? $instance['id'] : 'unset';
     $this->before_widget = apply_filters('tpicker_before', $before_widget ? $before_widget : $this->before_widget);
     $this->after_widget = apply_filters('tpicker_after', $after_widget ? $after_widget : $this->after_widget);
     $this->hidesearch = array_key_exists('hidesearch', $instance) ? true : false;
     // Defaults to show (false)
     $this->date_match = $instance['date_match'];
     // Must be present so just read in
     $this->choose_categories = $instance['choose_categories'];
     $cats = explode(',', $this->set_categories);
     // Limit list of categories
     if ($this->choose_categories == 'I') {
         // Only allow specified categories
         $set_categories = 'cat=' . $instance['set_categories'];
         // We can pass it as is because it will become the list of all categories for query_posts
         $cats = explode(',', $instance['set_categories']);
         // Should be a list of cat IDs
         foreach ($cats as $cat) {
             // Test against each of our permitted categories
             $this->categories[$cat] = get_term_by('ID', $cat, 'category');
             // Add individual categories to the array
         }
     } elseif ($instance['choose_categories'] == 'E') {
         // Reject specified categories
         $set_categories = 'cat=-' . str_replace(',', ',-', $instance['set_categories']);
         // Prefix each cat id with - to exclude it
         $all_cats[] = get_terms('category');
         // Add individual categories to the array
         foreach ($all_cats as $acat) {
             // Test against each of our permitted categories
             $allowed = true;
             foreach ($cats as $cat) {
                 // Test against each of our permitted categories
                 if ($acat->ID == $cat) {
                     $allowed = false;
                     break;
                 }
             }
             if ($allowed) {
                 $this->categories[$cat] = get_term($cat, 'category');
                 // Add individual categories to the array
             }
         }
     }
     // Set default term arguments for get_terms
     switch ($this->options['empty-terms']) {
         // How to handle empty items
         case 'always':
             $this->term_args['hide_empty'] = 0;
             break;
         case 'never':
             $this->term_args['hide_empty'] = 1;
             $this->term_args['hierarchical'] = 1;
             break;
         case 'sometimes':
             $this->term_args['hide_empty'] = 1;
             $this->term_args['hierarchical'] = 1;
     }
     $this->term_args['pad_counts'] = 1;
     // Add options for optional ordering of results
     $this->orderby = $instance['results_orderby'] ? $instance['results_orderby'] : '_default';
     // Default value for people upgrading from earlier widget versions
     $this->order = $instance['results_order'] ? $instance['results_order'] : '_DESC';
     return true;
 }
function taxonomy_picker_display_widget($instance, $args = null)
{
    // Check whether we displaying the results of a prevous use (ie. silverghyll_tpicker is set)
    $tpicker_inputs = taxonomy_picker_tpicker_array();
    // Get the configuration options from the database
    $tpicker_options = get_option('taxonomy-picker-options');
    $labels_after = isset($tpicker_options['labels_after']);
    $show_count = array_key_exists('show-count', $tpicker_options) ? 'on' : '';
    // Upgrade defence for v1.8 - won't be needed long term.  If taxonomies haven't been set, process the instance
    if (empty($instance['taxonomies'])) {
        $instance = taxonomy_picker_taxonomies_array($instance);
    }
    // Pre-process the instance for efficiency
    // Main display section starts here - builds a form which is passed via POST
    if ($args) {
        extract($args);
        // Unpack $before_widget etc
    } else {
        $before_widget = '<div class="widget taxonomy-picker widget-taxonomy-picker"><div class="widget-inside">';
        $after_widget = '</div></div>';
    }
    $title = apply_filters('widget_title', $instance['title']);
    $result = $before_widget;
    if ($title) {
        $result .= $before_title . $title . $after_title;
    }
    $result .= '<form method="post" action="' . $_SERVER['REQUEST_URI'] . '" class="taxonomy-picker" id="taxonomy-picker"><ul class="taxonomy-list">';
    $search_text = $tpicker_options['search-text'] ? $tpicker_options['search-text'] : __('Search');
    if (!array_key_exists('hidesearch', $instance)) {
        // Option suppresses
        $result .= "<li class='home search first'>";
        if ($labels_after) {
            $result .= "<input name='s' value='' type='text' style='width:90%;'></li>";
            // Search text box
            $result .= "<label>" . apply_filters('tpicker_search-text', $search_text) . "</label><br/>";
        } else {
            $result .= "<label>" . apply_filters('tpicker_search-text', $search_text) . "</label><br/>";
            $result .= "<input name='s' value='' type='text' style='width:90%;'></li>";
            // Search text box
        }
        $css_class = "";
    } else {
        $css_class = 'first home ';
    }
    foreach ($instance['taxonomies'] as $taxonomy_name => $data_item) {
        // Loop through chosen list of taxonomies
        $taxonomy = get_taxonomy($taxonomy_name);
        // Get the taxonomy object
        $tax_label = ($taxonomy_name == 'category' ? $instance['category_title'] : $taxonomy->label) . $tpicker_options['punctuation'];
        $taxies[$tax_label] = $data_item;
    }
    ksort($taxies);
    //Put taxonomies into alpha label order
    $taxies = apply_filters('tpicker_taxonomies', $taxies);
    // Filter taxonomy order
    foreach ($taxies as $tax_label => $data_item) {
        // Loop through chosen list of taxonomies (by string detection on all items in the array)
        // Set up any request for the sorting of the terms
        if ($data_item['orderby']) {
            $term_args['orderby'] = $data_item['orderby'];
        }
        if ($data_item['sort']) {
            $term_args['order'] = $data_item['sort'];
        }
        switch ($tpicker_options['empty-terms']) {
            // How to handle empty items
            case 'always':
                $term_args['hide_empty'] = 0;
                break;
            case 'never':
                $term_args['hide_empty'] = 1;
                $term_args['hierarchical'] = 1;
                break;
            case 'sometimes':
                $term_args['hide_empty'] = 1;
                $term_args['hierarchical'] = 1;
        }
        $taxonomy_name = $data_item['name'];
        $taxonomy = get_taxonomy($taxonomy_name);
        // Get the taxonomy object
        if ($taxonomy_name == "post_tag") {
            $terms = get_tags($term_args);
            $taxonomy_name = "tag";
        } else {
            $terms = $data_item['orderby'] == 'tree' ? silverghyll_get_terms_tree($taxonomy_name, $term_args) : get_terms($taxonomy_name, $term_args);
        }
        if ($data_item['hidden']) {
            $result .= "<input type='hidden' name='{$taxonomy_name}' value='" . $data_item['value'] . "' />";
        } elseif (taxonomy_picker_all_text($tax_label) != 'N/A') {
            // Main live display of combobox
            $css_class .= $data_item['orderby'];
            $result .= "<li class='{$css_class}'>";
            if (!$labels_after) {
                $result .= "<label style='float:left;'>{$tax_label}</label>";
            }
            // Multi-select combo boxes?
            $multi = array_key_exists('combo', $instance) ? $instance['combo'] : 'flat';
            // Transitional code as combo may not exist if widget has not been saved since 1.13.0
            if (apply_filters('tpicker_multi_select', $multi, $tax_label) == 'multi') {
                // Filter allows one to be turned on or off
                $result .= "<select name='{$taxonomy_name}[]' multiple>";
            } else {
                $result .= "<select name='{$taxonomy_name}'>";
            }
            $result .= "<option value='{$taxonomy_name}=tp-all'>" . taxonomy_picker_all_text($tax_label) . "</option>";
            $css_class = '';
            // After home reset to ''
            foreach ($terms as $term) {
                // Loop through terms in the taxonomy
                // ** Categories only ** //
                if ($taxonomy_name == 'category') {
                    $option_name = 'cat=' . $term->term_id;
                    // Pass in a format which suits query_posts - for categories cat=id works best
                    $cats = explode(',', $instance['set_categories']);
                    if ($instance['choose_categories'] == 'I') {
                        // Only allow specified categories
                        $set_categories = 'cat=' . $instance['set_categories'];
                        // We can pass it as is because it will become the list of all categories for query_posts
                        $allowed = false;
                        foreach ($cats as $cat) {
                            // Test against each of our permitted categories
                            if ($cat == $term->term_id) {
                                // Category matches so allowed
                                $allowed = true;
                                break;
                            }
                        }
                    } elseif ($instance['choose_categories'] == 'E') {
                        // Reject specified categories
                        $set_categories = 'cat=-' . str_replace(',', ',-', $instance['set_categories']);
                        // Prefix each cat id with - to exclude it
                        $allowed = true;
                        foreach ($cats as $cat) {
                            if ($cat == $term->term_id) {
                                // Category matches so disallowed - break out of loop
                                $allowed = false;
                                break;
                            }
                        }
                        // No category match so allow to proceed
                    } else {
                        // all - no display testing needed but we need to set $set_categories;
                        $set_categories = '';
                        $allowed = true;
                        // All categories allowed
                    }
                    // ** Other Taxonomies ** //
                } else {
                    $allowed = true;
                    $option_name = $taxonomy_name . '=' . $term->slug;
                }
                $t_name = __($term->name);
                $selected = '';
                if (empty($tpicker_inputs)) {
                    $selected = $data_item['value'] == $taxonomy_name . '=' . $term->slug ? 'on' : '';
                } else {
                    $input_value = $tpicker_inputs[$taxonomy_name];
                    $selected = $input_value == $term->slug ? 'on' : '';
                }
                if ($show_count and $allowed) {
                    $result .= taxonomy_picker_widget_select_option($option_name, "{$t_name} ({$term->count})", $selected, $term->parent);
                } elseif ($allowed) {
                    $result .= taxonomy_picker_widget_select_option($option_name, $t_name, $selected, $term->parent);
                }
            }
            $result .= "</select>";
            if ($labels_after) {
                $result .= "<label style='float:left;'>{$tax_label}</label>";
            }
            $result .= "</li>";
        }
        // Hidden?
    }
    unset($taxies);
    $result .= apply_filters('tpicker_form_after_fields', "");
    // Filter taxonomy order
    $result .= "<input type='hidden' name='set_categories' value='" . $set_categories . "' />";
    $result .= "<input type='hidden' name='kate-phizackerley' value='taxonomy-picker' />";
    $result .= '<li style="height:8px;" class="last"></li></ul><p style="text-align:center;margin:0 auto;">';
    if (isset($tpicker_options['remember'])) {
        // $result .= "<p onclick='document.getElementById(\"taxonomy-picker\").reset()';>Clear</p>";  // Sort out in v2.0
    } else {
        $result .= '<input type="reset" value="' . apply_filters('tpicker_reset', 'Reset') . '" style="margin-right:10%;" />';
    }
    $result .= "<input type='submit' value='{$search_text}' class='tpicker-submit'/></p></form>";
    $result .= $after_widget;
    return $result;
}