/**
  * 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;
 }
Beispiel #3
0
 /**
  * Load the terms
  * @return bool
  */
 public function loadTerms()
 {
     $taxonomy_keys = $this->getTaxonomyKeys();
     if (!Arr::iterable($taxonomy_keys)) {
         return false;
     }
     // TODO Move this to somewhere more efficient
     // Check if this should be an instance of TacoTerm.
     // If not, the object will just be a default WP object from wp_get_post_terms below.
     $taxonomies_subclasses = array();
     $subclasses = Term\Loader::getSubclasses();
     foreach ($subclasses as $subclass) {
         $term_instance = new $subclass();
         $term_instance_taxonomy_key = $term_instance->getKey();
         foreach ($taxonomy_keys as $taxonomy_key) {
             if (array_key_exists($taxonomy_key, $taxonomies_subclasses)) {
                 continue;
             }
             if ($term_instance_taxonomy_key !== $taxonomy_key) {
                 continue;
             }
             $taxonomies_subclasses[$taxonomy_key] = $subclass;
             break;
         }
     }
     foreach ($taxonomy_keys as $taxonomy_key) {
         $terms = wp_get_post_terms($this->get(self::ID), $taxonomy_key);
         if (!Arr::iterable($terms)) {
             continue;
         }
         $terms = array_combine(array_map('intval', Collection::pluck($terms, 'term_id')), $terms);
         // Load Taco\Term if applicable
         if (array_key_exists($taxonomy_key, $taxonomies_subclasses)) {
             $terms = Term\Factory::createMultiple($terms, $taxonomy_key);
         }
         $this->_terms[$taxonomy_key] = $terms;
     }
     return true;
 }
 /**
  * Get Taco Posts or Terms in the order specficied using a string of comma seperated ids
  * @param string $string_order comma seperated list of ids
  * @param bool $reverse should the collection be reversed
  * @param bool $is_term should the method return terms instead of posts
  * @param string $taxonomy if this method is returning terms, what taxonomy do they belong to
  * @param bool $load_terms should terms be loaded with posts
  * @return array
  */
 public static function getPostsFromOrder($string_order = '', $reverse = false, $is_term = false, $taxonomy = null, $load_terms = true)
 {
     if (!strlen($string_order)) {
         return array();
     }
     if (!preg_match('/\\d+/', $string_order)) {
         return array();
     }
     $ids_array = explode(',', trim(strip_tags($string_order)));
     if (!$is_term) {
         $ids_array = self::getItemsIfExists($ids_array, $is_term);
         $items = \Taco\Post\Factory::createMultiple($ids_array, $load_terms);
     } else {
         $ids_array = self::getItemsIfExists($ids_array, true);
         $items = \Taco\Term\Factory::createMultiple($ids_array, $taxonomy);
     }
     $items = $reverse ? array_reverse($items) : $items;
     return Arr::iterable($items) ? $items : array();
 }