/**
  * Get list of links of available taxonomy terms
  * @param string $taxonomy
  * @param string $current_class
  * @param string $link_prefix
  * @param obj $current_term
  * @param obj $current_post
  * @return string HTML
  */
 public static function getTermsNav($taxonomy = 'category', $current_class = 'current-term', $link_prefix = null, $current_term = null, $current_post = null)
 {
     $terms = \Taco\Term\Factory::createMultiple(get_terms($taxonomy));
     if (!Arr::iterable($terms)) {
         return null;
     }
     if (!Obj::iterable($current_term)) {
         $current_term = \Taco\Term\Factory::create(get_queried_object());
     }
     $term_links = array();
     $current_class = ' class="' . $current_class . '"';
     if (is_null($link_prefix)) {
         $link_prefix = '/' . $taxonomy . '/';
     }
     foreach ($terms as $term) {
         $term_class = null;
         if (Obj::iterable($current_term)) {
             // We are on a taxonomy/category template
             if ((int) $current_term->term_id === (int) $term->term_id) {
                 $term_class = $current_class;
             }
         } else {
             // We are on a single page/post
             if (!Obj::iterable($current_post)) {
                 global $post;
                 $current_post = \Taco\Post\Factory::create($post);
             }
             $current_post_term = reset($current_post->getTerms($taxonomy));
             if ((int) $current_post_term->term_id === (int) $term->term_id) {
                 $term_class = $current_class;
             }
         }
         $term_links[] = sprintf('<li%s><a href="%s%s/">%s</a></li>', $term_class, $link_prefix, $term->slug, $term->name);
     }
     return sprintf('<ul>
     %s
   </ul>', join('', $term_links));
 }
Beispiel #2
0
 /**
  * Get terms with conditions
  * @param array $args
  * @return array
  */
 public static function getWhere($args = array())
 {
     $instance = Term\Factory::create(get_called_class());
     // Allow sorting both by core fields and custom fields
     // See: http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
     $default_args = array('orderby' => $instance->getDefaultOrderBy(), 'order' => $instance->getDefaultOrder(), 'hide_empty' => false);
     $criteria = array_merge($default_args, $args);
     // Custom ordering
     $orderby = null;
     $order = null;
     $wordpress_sortable_fields = array('id', 'name', 'count', 'slug', 'term_group', 'none');
     if (array_key_exists('orderby', $criteria) && !in_array($criteria['orderby'], $wordpress_sortable_fields)) {
         $orderby = $criteria['orderby'];
         $order = array_key_exists('order', $criteria) ? strtoupper($criteria['order']) : 'ASC';
         unset($criteria['orderby']);
         unset($criteria['order']);
     }
     $taxonomy = $instance->getTaxonomyKey();
     $terms = Term\Factory::createMultiple(get_terms($taxonomy, $criteria));
     // We might be done
     if (!Arr::iterable($terms)) {
         return $terms;
     }
     if (!$orderby) {
         return $terms;
     }
     // Custom sorting that WordPress can't do
     $field = $instance->getField($orderby);
     // Make sure we're sorting numerically if appropriate
     // because WordPress is storing strings for all vals
     if ($field['type'] === 'number') {
         foreach ($terms as &$term) {
             if (!isset($term->{$orderby})) {
                 continue;
             }
             if ($term->{$orderby} === '') {
                 continue;
             }
             $term->{$orderby} = (double) $term->{$orderby};
         }
     }
     // Sorting
     $sort_flag = $field['type'] === 'number' ? SORT_NUMERIC : SORT_STRING;
     $terms = Collection::sortBy($terms, $orderby, $sort_flag);
     if (strtoupper($order) === 'DESC') {
         $terms = array_reverse($terms, true);
     }
     // Convert back to string as WordPress stores it
     if ($field['type'] === 'number') {
         foreach ($terms as &$term) {
             if (!isset($term->{$orderby})) {
                 continue;
             }
             if ($term->{$orderby} === '') {
                 continue;
             }
             $term->{$orderby} = (string) $term->{$orderby};
         }
     }
     return $terms;
 }