コード例 #1
0
 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;
 }
コード例 #2
0
function taxonomy_picker_all_text($tax_name)
{
    $tax_name = rstrip_punctuation($tax_name);
    $options = get_option('taxonomy-picker-options');
    $all_text = trim($options['all-format']);
    // Just in case!
    $override = trim($options['all-override']);
    // Just in case!
    if (!empty($override)) {
        $all_text = $override;
    }
    // Override option for international users
    if ($all_text == __('Blank', 'tpicker')) {
        $all_text = '&emsp;';
    } elseif (substr($all_text, -6) == '{name}') {
        $all_text = str_replace('{name}', ucfirst($tax_name), $all_text);
    } elseif (substr($all_text, -7) == '{name}s') {
        $all_text = trim(str_replace('{name}', ucfirst($tax_name), $all_text));
        if (substr($all_text, -2) == 'ys') {
            $all_text = substr_replace($all_text, 'ies', -2);
            // ys => ies for neat plurals
        } elseif (substr($all_text, -2) == 'ss') {
            $all_text = substr($all_text, 0, strlen($all_text) - 1);
            // Drop the last s
        }
    }
    return $all_text;
}