function form($instance) { // Form for configuring the widget instance // Set up some defaults $defaults = array('title' => __('Example', 'example'), 'choose_categories' => 'A', 'choose_pages' => 'A'); $instance = wp_parse_args((array) $instance, $defaults); // Widget title $title_id = $this->get_field_id('title'); $title_name = $this->get_field_name('title'); $title_value = $instance['title']; //Show search? $search_id = $this->get_field_id('hidesearch'); $search_name = $this->get_field_name('hidesearch'); $radio_checked = $instance['hidesearch'] == 'on' ? 'checked ' : ''; ?> <p><fieldset id="taxonomy-picker-title"> <label for='<?php echo $title_id; ?> ' style="float:left;">Title: </label> <input id='<?php echo $title_id; ?> ' name='<?php echo $title_name; ?> ' value='<?php echo $title_value; ?> ' style='width:60%;' /> <?php echo "<table><tbody><tr><td><input id='{$search_id}' class='checkbox' type='checkbox' name='{$search_name}' {$radio_checked} />"; echo " <label for='{$search_id}' title='showsearch'><span style='font-size:85%;'>Hide text search?</span></label></td></tr>"; echo "<tr><td>Combobox type:</td><td><select name='" . $this->get_field_name('combo') . "'>"; foreach (array('flat', 'multi') as $combo) { $selected = $instance['combo'] == $combo ? 'selected=selected' : ''; echo "<option value='{$combo}' {$selected}>" . ucwords($combo) . "</option>"; } echo '</select></td><td>(Multi-select may not work with all themes)</td></tr>'; echo "</tbody></table></fieldset></p><hr>"; unset($title_id, $title_name, $title_value, $search_id, $search_value, $combo, $selected); // Build taxonomy selection boxes $taxes = get_taxonomies('', 'names'); if (count($taxes) > 0) { ?> <fieldset id="taxonomy-picker-taxonomoies"><h3>Taxonomies</h3><div> <table style="width:400px;"><thead><tr> <td><strong>Taxonomy</strong></td> <td><strong>Fix/Initial</strong></td> <td><strong>Order By</strong></td> <td><strong>Sort</strong></td> </tr></thead><tbody><?php foreach ($taxes as $tax) { if ($tax == 'link_category' or $tax == 'nav_menu' or $tax == 'post_format' or $tax == 'post_type') { continue; } // Disable types not handled by basic widget $tax_stem = 'taxonomy_' . $tax; $tax_id = $this->get_field_id($tax_stem); $taxonomy = get_taxonomy($tax); $tax_name = $this->get_field_name($tax_stem); $radio_checked = $instance[$tax_stem] == 'on' ? 'checked ' : ''; if ($tax != 'category') { // Custom taxonomy - build fix/initial value combobox $terms = get_terms($taxonomy->name, array('orderby' => 'name')); $select_name = $this->get_field_name("fix_" . $tax); $tax_select = "<select name='{$select_name}' style='width:90%;font-size:85%;'>"; $tax_select .= "<option value='{$taxonomy->name}=tp-all'>" . taxonomy_picker_all_text($tax_label) . "</option>"; foreach ($terms as $term) { // Loop through the terms to build the options $option_name = $taxonomy->name . '=' . $term->slug; $selected = $instance['fix_' . $tax] == $option_name ? 'selected="selected"' : ''; $tax_select .= "<option value='{$option_name}' {$selected}>{$term->name}</option>"; } $tax_select .= "</select>"; // Orderby comboboxes $select_name = $this->get_field_name("orderby_" . $tax); $order_select = "<select name='{$select_name}' style='width:90%;font-size:90%;'>"; $orders = array('name', 'slug', 'id', 'count', 'tree'); foreach ($orders as $order) { $selected = $instance['orderby_' . $tax] == $order ? 'selected="selected"' : ''; $select_label = $order == 'name' ? 'Label' : ucwords(str_replace('_', ' ', $order)); $order_select .= "<option value='{$order}' {$selected}>{$select_label}</option>"; } unset($orders, $order); // Sort order comboboxes $select_name = $this->get_field_name("sort_" . $tax); $sort_select = "<select name='{$select_name}' style='width:90%;font-size:90%;'>"; foreach (array('Asc', 'Desc') as $term) { $selected = $instance['sort_' . $tax] == $term ? 'selected="selected"' : ''; $sort_select .= "<option value='{$term}' {$selected}>{$term}</option>"; } } echo "<tr><td><input id='{$tax_id}' class='checkbox' type='checkbox' name='{$tax_name}' {$radio_checked} />"; echo " <label for='{$tax_id}' title='{$tax_stem}'><span style='font-size:85%;'>{$taxonomy->label}</span></label></td>"; echo "<td>{$tax_select}</td><td>{$order_select}</td><td>{$sort_select}</td></tr>"; } echo '</tbody></table><i style="font-size:75%">If on, the value is the initial one; if off, value is fixed to restrict search</i></div></fieldset><hr>'; } // Select Categories $title_id = $this->get_field_id('category_title'); $title_name = $this->get_field_name('category_title'); $title_value = $instance['category_title']; echo '<fieldset id="taxonomy-picker-categories"<p><h3>Categories</h3></p>'; echo '<p style="float:left;"><label for="$cat_title_id"><b>Title:</b></label></p>'; echo '<p style="float:right;width:75%;">'; echo "<input id='{$title_id}' name='{$title_name}' value='{$title_value}' style='width:90%;' />"; echo '</p>'; echo '<br style="clear:both;"/><label><b>Select: </b></label>'; // Build radio buttons for All, Incl , Excl for categories $radio_id = $this->get_field_id('choose_categories'); $radio_name = $this->get_field_name('choose_categories'); $radio_value = $instance['choose_categories']; $radio_checked = $instance['choose_categories'] == 'A' ? 'checked' : ''; echo "All: <input type='radio' name='{$radio_name}' value='A' {$radio_checked} /> | "; $radio_checked = $instance['choose_categories'] == 'I' ? 'checked' : ''; echo "Incl: <input type='radio' name='{$radio_name}' value='I' {$radio_checked} /> | "; $radio_checked = $instance['choose_categories'] == 'E' ? 'checked' : ''; echo "Excl: <input type='radio' name='{$radio_name}' value='E' {$radio_checked} /><br/>"; $input_id = $this->get_field_id('set_categories'); $input_name = $this->get_field_name('set_categories'); $input_value = $instance['set_categories']; echo "<input id='{$input_id}' name='{$input_name}' value='{$input_value}' style='width:100%;margin-top:2px;'/>"; echo '<i style="font-size:75%">Enter category IDs separated by commas</i>'; echo '</fieldset><hr>'; echo '<fieldset id="taxonomy-picker-pages">'; echo '<p><h3>Pages</h3></p><label><b>Select: </b></label>'; $radio_id = $this->get_field_id('choose_pages'); $radio_name = $this->get_field_name('choose_pages'); $radio_value = $instance['choose_pages']; $radio_checked = $instance['choose_pages'] == 'A' ? 'checked' : ''; echo "All: <input type='radio' name='{$radio_name}' value='A' {$radio_checked} /> | "; $radio_checked = $instance['choose_pages'] == 'I' ? 'checked' : ''; echo "Incl: <input type='radio' name='{$radio_name}' value='I' {$radio_checked} /> | "; $radio_checked = $instance['choose_pages'] == 'E' ? 'checked' : ''; echo "Excl: <input type='radio' name='{$radio_name}' value='E' {$radio_checked} /><br/>"; $input_id = $this->get_field_id('set_pages'); $input_name = $this->get_field_name('set_pages'); $input_value = $instance['set_pages']; echo "<input id='{$input_id}' name='{$input_name}' value='{$input_value}' style='width:100%;margin-top:2px;'/>"; echo '<i style="font-size:75%">Enter page IDs separated by commas</i>'; echo '</fieldset>'; }
function form($instance) { // Form for configuring the widget instance // Set up some defaults $defaults = array('title' => __('Example', 'example'), 'choose_categories' => 'A', 'choose_pages' => 'A'); $instance = wp_parse_args((array) $instance, $defaults); $options = get_option('taxonomy-picker-options'); // ID $instanceid = $this->get_field_id('id'); $instance_name = $this->get_field_name('id'); if (empty($instance['id'])) { $instance['id'] = substr(md5(uniqid(rand(), true)), 0, 6); } // Widget title $titleid = $this->get_field_id('title'); $title_name = $this->get_field_name('title'); $title_value = $instance['title'] . ' <span class="instance-ID">(ID = ' . $instance['id'] . ')</span>'; //Show search? $searchid = $this->get_field_id('hidesearch'); $search_name = $this->get_field_name('hidesearch'); $radio_checked = (array_key_exists('hidesearch', $instance) and $instance['hidesearch']) ? 'checked ' : ''; echo '<p class="tpicker-title"><fieldset id="taxonomy-picker-title">'; echo "<td><label for='{$titleid}' title='title'>Title:</label>\r\n\t\t\t\t\t\t\t<input id='{$titleid}' class='textbox' type='textbox' name='{$title_name}' value='" . $instance['title'] . "' /></td>"; echo "<td><label for='{$instanceid}' title='id'>Unique ID:</label>\r\n\t\t\t\t\t\t\t<input id='{$instanceid}' class='textbox' type='textbox' name='{$instance_name}' value = '" . $instance['id'] . "' /></td>"; echo "<td><label for='{$searchid}' title='showsearch'>Hide text search?</label>\r\n\t\t\t\t\t\t\t<input id='{$searchid}' class='checkbox' type='checkbox' name='{$search_name}' {$radio_checked} /></td>"; echo "</tr></tbody></table></fieldset></p><hr>"; /*********** Premium options *********/ // Radio buttons alternative ?> <p><fieldset id="tpicker_premium"><h3>Premium Options</h3><table><tbody><tr><td><?php // Date Match option echo "Post date:</td><td><select name='" . $this->get_field_name('date_match') . "'>"; foreach (array('N/A', 'Y', 'YM', 'YMD', 'M', 'D') as $item) { $selected = $instance['date_match'] == $item ? 'selected=selected' : ''; echo "<option value='{$item}' {$selected}>{$item}</option>"; } ?> </select></td><?php // Code for sort results options $selected = (array_key_exists('results_orderby', $instance) and $instance['results_orderby'] == '_default') ? 'selected=selected' : ''; echo "<td>Sort results by:</td><td><select name='" . $this->get_field_name('results_orderby') . "'>\r\n\t\t\t\t\t<option value='_default' {$selected}>** Default **</option>"; $user_choice = false; foreach (array('author', 'comment_count', 'date', 'ID', 'modified', 'title') as $item) { if ($options["results_sort_{$item}"]) { $item_text = str_replace('_', ' ', ucfirst($item)); $selected = (array_key_exists('results_orderby', $instance) and $instance['results_orderby'] == $item) ? 'selected=selected' : ''; echo "<option value='{$item}' {$selected}>{$item_text}</option>"; $user_choice = true; // We have at least one option to allow a user choice } } if ($user_choice) { // There are enough options for user_choice to be sensible $selected = (array_key_exists('results_orderby', $instance) and $instance['results_orderby'] == '_choice') ? 'selected=selected' : ''; echo "<option value='_choice' {$selected}>** Visitors' Choice **</option>"; } echo "</select></td><td><select name='" . $this->get_field_name('results_order') . "'>\r\n\t\t\t\t\t\t<option value='ASC'>Ascending</option><option value='DSC'>Descending</option></select></td></tr>"; echo "<tr><td>Combobox type:</td><td><select name='" . $this->get_field_name('combo') . "'>"; foreach (array('flat', 'multi', 'radio') as $combo) { $selected = (array_key_exists('combo', $instance) and $instance['combo'] == $combo) ? 'selected=selected' : ''; echo "<option value='{$combo}' {$selected}>" . ucwords($combo) . "</option>"; } ?> </select></td><?php echo "<p>(<b>NB - Release Notes</b> - Post Date is alpha trial functionality in this release)<p>"; ?> </tr></tbody></table></fieldset></p><hr><?php unset($titleid, $title_name, $title_value, $searchid, $search_value, $combo, $selected, $item, $item_text, $combo, $user_choice); /************ Taxonomies Section ************/ // Build taxonomy selection boxes $taxes = get_taxonomies('', 'names'); if (isset($options['post_type'])) { $taxes['post_type'] = "post_type"; } // Option to add post_type if (count($taxes) > 0) { $priority_name = isset($options['sort-priority']) ? '<td><strong>Priority</strong></td>' : ''; ?> <fieldset id="taxonomy-picker-taxonomoies"><h3>Taxonomies</h3><div> <table style="width:400px;"><thead><tr> <td><strong>Taxonomy</strong></td> <td><strong>Fix/Initial</strong></td> <td><strong>Order By</strong></td> <td><strong>Sort</strong> </td><?php echo $priority_name; ?> </tr></thead><tbody><?php foreach ($taxes as $tax) { if ($tax == 'link_category' or $tax == 'nav_menu' or $tax == 'post_format' and !isset($options['post_format']) or $tax == 'post_type' and !isset($options['post_type'])) { continue; } // Skip any un-supported or de-selected taxonomy $tax_stem = 'taxonomy_' . $tax; if ($tax == 'post_type') { $tax_label = 'Post Type'; } else { $taxonomy = get_taxonomy($tax); $tax_label = $taxonomy->label; } $taxid = $this->get_field_id($tax_stem); $tax_name = $this->get_field_name($tax_stem); $radio_checked = (array_key_exists($tax_stem, $instance) and $instance[$tax_stem] == 'on') ? 'checked ' : ''; if ($tax != 'category') { // Custom taxonomy or post_type or post_format - build fix/initial value combobox if ($tax == 'post_type') { $taxonomy_name = 'post_type'; $args = array('_builtin' => true); $terms = get_post_types($args, 'names'); } else { $taxonomy_name = $taxonomy->name; $terms = get_terms($taxonomy->name, array('orderby' => 'name')); } if (!empty($terms)) { // Check we have some terms to process ... $select_name = $this->get_field_name("fix_{$tax}"); // The name of the Fix field $tax_select = "<select name='{$select_name}' style='width:90%;font-size:85%;'>"; $tax_select .= "<option value='{$taxonomy_name}=tp-all'>" . taxonomy_picker_all_text($tax_label) . "</option>"; foreach ((array) $terms as $term) { // Loop through the terms to build the options $option_name = $taxonomy_name . '=' . (is_string($term) ? $term : $term->slug); $selected = (array_key_exists('fix_' . $tax, $instance) and $instance['fix_' . $tax] == $option_name) ? 'selected="selected"' : ''; $tax_select .= "<option value='{$option_name}' {$selected}>" . (is_string($term) ? $term : $term->name) . '</option>'; } $tax_select .= "</select>"; } // !empty() // Orderby comboboxes $select_name = $this->get_field_name("orderby_" . $tax); $order_select = "<select name='{$select_name}' style='width:90%;font-size:90%;'>"; if ($tax == 'post_type' or $tax == 'post_format') { $orders = 'name'; } else { $orders = array('name', 'slug', 'id', 'count', 'tree'); } // Add 'pruned-tree' when ready foreach ((array) $orders as $order) { $selected = (array_key_exists("orderby_{$tax}", $instance) and $instance["orderby_{$tax}"] == $order) ? 'selected="selected"' : ''; $select_label = $order == 'name' ? 'Label' : ucwords(str_replace('_', ' ', $order)); $order_select .= "<option value='{$order}' {$selected}>{$select_label}</option>"; } unset($orders, $order); // Sort order comboboxes $select_name = $this->get_field_name("sort_" . $tax); $sort_select = "<select name='{$select_name}' style='width:90%;font-size:90%;'>"; foreach (array('Asc', 'Desc') as $term) { $selected = (array_key_exists('sort_' . $tax, $instance) and $instance['sort_' . $tax] == $term) ? 'selected="selected"' : ''; $sort_select .= "<option value='{$term}' {$selected}>{$term}</option>"; } // Priority inputs if (@isset($options['sort-priority'])) { // Only show priority field if required $select_name = $this->get_field_name("priority_" . $tax); $priority_input = "<input name='{$select_name}' style='width:60px;font-size:90%;'>"; } else { $priority_input = ''; } } echo "<tr><td><input id='{$taxid}' class='checkbox' type='checkbox' name='{$tax_name}' {$radio_checked} />"; echo " <label for='{$taxid}' title='{$tax_stem}'>{$tax_label}</span></label></td>"; // Output any cells we have build foreach (array('tax_select', 'order_select', 'sort_select', 'priority_input') as $i) { if (isset(${$i})) { echo '<td>' . ${$i} . '</td>'; } else { echo '<td> </td>'; } } echo "</tr>"; } echo '</tbody></table><i style="font-size:75%">If on, the value is the initial one; if off, value is fixed to restrict search</i></div></fieldset><hr>'; } // Select Categories $titleid = $this->get_field_id('category_title'); $title_name = $this->get_field_name('category_title'); $title_value = array_key_exists('category_title', $instance) ? $instance['category_title'] : ''; echo '<fieldset id="taxonomy-picker-categories"<p><h3>Categories</h3></p>'; echo '<p style="float:left;"><label for="$cat_titleid"><b>Title:</b></label></p>'; echo '<p style="float:right;width:75%;">'; echo "<input id='{$titleid}' name='{$title_name}' value='{$title_value}' style='width:90%;' />"; echo '</p>'; echo '<br style="clear:both;"/><label><b>Select: </b></label>'; // Build radio buttons for All, Incl , Excl for categories $radioid = $this->get_field_id('choose_categories'); $radio_name = $this->get_field_name('choose_categories'); $radio_value = $instance['choose_categories']; $radio_checked = $instance['choose_categories'] == 'A' ? 'checked' : ''; echo "All: <input type='radio' name='{$radio_name}' value='A' {$radio_checked} /> | "; $radio_checked = $instance['choose_categories'] == 'I' ? 'checked' : ''; echo "Incl: <input type='radio' name='{$radio_name}' value='I' {$radio_checked} /> | "; $radio_checked = $instance['choose_categories'] == 'E' ? 'checked' : ''; echo "Excl: <input type='radio' name='{$radio_name}' value='E' {$radio_checked} /><br/>"; $inputid = $this->get_field_id('set_categories'); $input_name = $this->get_field_name('set_categories'); $input_value = array_key_exists('set_categories', $instance) ? $instance['set_categories'] : ''; echo "<input id='{$inputid}' name='{$input_name}' value='{$input_value}' style='width:100%;margin-top:2px;'/>"; echo '<i style="font-size:75%">Enter category IDs separated by commas</i>'; echo '</fieldset><hr>'; echo '<fieldset id="taxonomy-picker-pages">'; echo '<p><h3>Pages</h3></p><label><b>Select: </b></label>'; $radioid = $this->get_field_id('choose_pages'); $radio_name = $this->get_field_name('choose_pages'); $radio_value = $instance['choose_pages']; $radio_checked = $instance['choose_pages'] == 'A' ? 'checked' : ''; echo "All: <input type='radio' name='{$radio_name}' value='A' {$radio_checked} /> | "; $radio_checked = $instance['choose_pages'] == 'I' ? 'checked' : ''; echo "Incl: <input type='radio' name='{$radio_name}' value='I' {$radio_checked} /> | "; $radio_checked = $instance['choose_pages'] == 'E' ? 'checked' : ''; echo "Excl: <input type='radio' name='{$radio_name}' value='E' {$radio_checked} /><br/>"; $inputid = $this->get_field_id('set_pages'); $input_name = $this->get_field_name('set_pages'); $input_value = array_key_exists('set_pages', $instance) ? $instance['set_pages'] : ''; echo "<input id='{$inputid}' name='{$input_name}' value='{$input_value}' style='width:100%;margin-top:2px;'/>"; echo '<i style="font-size:75%">Enter page IDs separated by commas</i>'; echo '</fieldset>'; }
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; }