function silverghyll_get_terms_tree($taxonomies, $args) { if (is_array($taxonomies)) { foreach ($taxonomies as $taxonomy) { $terms[] = silverghyll_get_terms_tree($taxonomy, $args); } // Recurse return $terms; } $args['parent'] = 0; // Get top level only $args['orderby'] = 'name'; // Want alphabetically within our tree $terms = get_terms($taxonomies, $args); //Get top level terms $result = array(); if ($terms) { foreach ($terms as $term) { $result = array_merge($result, silverghyll_get_term_subtree($taxonomies, $term, $args)); } } // Recurse sub-trees return $result; }
private function build_taxonomy($tax_label, $data_item, $css_class) { $result = ''; // This will be our output // Set up any request for the sorting of the terms $term_args = $this->term_args; // Read down defaults if ($data_item['orderby']) { $term_args['orderby'] = $data_item['orderby']; } if ($data_item['sort']) { $term_args['order'] = $data_item['sort']; } $labels_after = isset($this->options['labels_after']); $tax_label_bare = rstrip_punctuation($tax_label = trim($tax_label)); if ($tax_label_bare == 'Category' and $this->choose_categories != 'A') { $taxonomy_name = $data_item['name']; $terms = $this->categories; } elseif ($tax_label_bare == 'Post Type' or $tax_label_bare == ' post_type') { $taxonomy_name = 'post_type'; $args = array('_builtin' => true); $terms = get_post_types($args, 'names'); } elseif ($tax_label_bare == 'post_tag' or $tax_label_bare == 'Tags') { $taxonomy_name = 'tag'; $terms = get_tags($term_args); } else { $taxonomy_name = $data_item['name']; $terms = substr($data_item['orderby'], -4) == 'tree' ? silverghyll_get_terms_tree($taxonomy_name, $term_args) : get_terms($taxonomy_name, $term_args); } if ($data_item['hidden']) { return "<input type='hidden' name='{$taxonomy_name}' value='" . $data_item['value'] . "' />"; } // Hidden $css_class .= $data_item['orderby'] == 'pruned_tree' ? 'tree pruned' : $data_item['orderby']; // Set the class for the containing <li> $this->tax_type = $data_item['orderby']; $css_class = ''; // After home reset to '' // Pre-process tree to find ancestors - for non-trees will just do selected foreach ($terms as $term) { $current = new taxonomy_picker_ancestor($taxonomy_name, $term); $term_slug = is_object($term) ? $term->slug : $term; if (empty($this->inputs) or !array_key_exists($taxonomy_name, $this->inputs)) { $current->selected = $data_item['value'] == $taxonomy_name . '=' . $term_slug ? 'selected' : ''; } else { $current->selected = $this->inputs[$taxonomy_name] == $term_slug ? 'selected' : ''; } if (substr($data_item['orderby'], -4) == 'tree') { // For trees we need more if (!empty($current->parent) and $current->level < 6) { $parent = $this->taxonomies[$tax_label]['ancestors'][$current->parent]; if ($parent->selected) { $current->is_kid = true; } // Immediate child of selected item if ($current->selected) { $parent->is_father = true; } // Immediate parent $current->level = $parent->level + 1; } else { $current->level = 1; // No parent so level 1 } if ($current->selected) { // Only need to look for ancestors of selected items in trees $ancestor = $current; while (!empty($ancestor->parent)) { // For the selected item we need to identify the ancestral trail. Will work fine for trees, otherwise who cares? $ancestor = $this->taxonomies[$tax_label]['ancestors'][$ancestor->parent]; // Get the parent ancestor $ancestor->is_ancestor = true; // Define as an ancestor $this->taxonomies[$tax_label]['ancestors'][$ancestor->slug] = $ancestor; // Store before looping } } } // Trees $this->taxonomies[$tax_label]['ancestors'][$term_slug] = $current; } // Terms $this_label = "<label style='float:left;'>{$tax_label}</label>"; $result .= "<li class='{$css_class}'>" . ($labels_after ? "" : $this_label); switch ($this->combo) { case '': case 'flat': case 'multi': // Normal combo box if (apply_filters('tpicker_multi_select', $this->combo, $taxonomy_name) == 'multi') { // Filter allows one to be turned on or off $result .= "<select name='{$taxonomy_name}[]' multiple class='tpicker-select tpicker-{$taxonomy_name} multiple'>"; } else { $result .= "<select name='{$taxonomy_name}' class='tpicker-select tpicker-{$taxonomy_name} single'>"; } if (taxonomy_picker_all_text($tax_label) != 'N/A') { $result .= "<option value='{$taxonomy_name}=tp-all'>" . taxonomy_picker_all_text($tax_label) . "</option>"; // ** ALL ** } foreach ($terms as $term) { if (is_object($term)) { $term_slug = $term->slug; $term_id = @isset($term->id) ? $term->id : $term_slug; } else { $term_slug = $term_id = $term; } $option_name = $taxonomy_name == 'category' ? 'cat=' . $term->term_id : $taxonomy_name . '=' . $term_slug; $result .= $this->build_term_select($tax_label, $term, $option_name); // Loop through terms in the taxonomy } $result .= "</select>"; break; case 'radio': // Radio buttons if (taxonomy_picker_all_text($tax_label) != 'N/A') { $result .= "<p><input type='radio' name='{$taxonomy_name}' value='{$taxonomy_name}=tp-all' class='radio tpicker-radio tpicker-{$taxonomy_name}' />" . taxonomy_picker_all_text($tax_label) . "</p>"; // ** ALL ** } foreach ($terms as $term) { if (is_object($term)) { $term_slug = $term->slug; $term_id = $term->id; } else { $term_slug = $term_id = $term; } $option_name = $taxonomy_name == 'category' ? 'cat=' . $term->term_id : $taxonomy_name . '=' . $term->slug; $t_name = $term->name . ($this->options['show-count'] ? " ({$term->count})" : ""); $result .= "<p><input type='radio' name='{$taxonomy_name}' value='{$option_name}' />" . $t_name . "</p>"; // ** ALL ** } $result .= "<br/>"; break; case 'jQuery-tree': // Uses http://kotowicz.net/jquery-option-tree/demo/demo.html OptionTree plugin $result .= "<p><input type='text' name='{$taxonomy_name}' /></p><script type='text/javascript'>"; // $result .= "jQuery(document).ready(function($){" ; $result .= "(function(\$){"; $result .= "alert('BOUNCE');"; $result .= "\$(function() {var option_tree = {'Option 1': {'Suboption':200}, 'Option 2': {'Suboption 2': {'Subsub 1':201, 'Subsub 2':202},\r\n 'Suboption 3': {'Subsub 3':203, 'Subsub 4':204, 'Subsub 5':205}}};"; $result .= "\$('input[name={$taxonomy_name}].nested').optionTree(option_tree);});"; $result .= "}) (jQuery)"; // $result .= "});"; $result .= "</script>"; } $result .= ($labels_after ? $this_label : "") . "</li>"; return $result; }
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; }