WPDK  1.5.0
WordPress Development Kit
 All Data Structures Files Functions Variables Pages
wpdk-terms.php
Go to the documentation of this file.
1 <?php
24 class WPDKTerm {
25 
26  public $count;
27  public $description;
28  public $name;
29  public $parent;
30  public $parent_term;
31  public $slug;
32  public $taxonomy;
33  public $term_group;
34  public $term_id;
36  public $permalink;
37 
47  private function __construct( $term )
48  {
49  if ( !is_null( $term ) ) {
50  $term_exists = term_exists( $term->name, $term->taxonomy, $term->parent );
51  if ( $term_exists ) {
52  foreach ( $term as $property => $value ) {
53  $this->{$property} = $value;
54  }
55  $this->permalink = get_term_link( $term );
56  }
57  }
58  }
59 
77  public static function term( $term, $taxonomy = false, $output = OBJECT, $filter = 'raw', $parent = false )
78  {
79  global $wpdb;
80 
81  if ( empty( $taxonomy ) ) {
82  $sql = sprintf( 'SELECT t.*, tt.description, tt.taxonomy FROM %s AS t LEFT JOIN %s AS tt ON ( tt.term_id = t.term_id )', $wpdb->terms, $wpdb->term_taxonomy );
83  if ( is_numeric( $term ) ) {
84  $sql .= sprintf( ' WHERE t.term_id = %s', $term );
85  }
86  elseif ( is_string( $term ) ) {
87  $sql .= sprintf( ' WHERE t.slug = "%s"', $term );
88  }
89  elseif ( is_object( $term ) ) {
90  $sql .= sprintf( ' WHERE t.term_id = "%s"', $term->term_id );
91  }
92  else {
93  return false;
94  }
95 
96  $row = $wpdb->get_row( $sql );
97  if ( is_null( $row ) ) {
98  return false;
99  }
100  $term = $row->term_id;
101  $taxonomy = $row->taxonomy;
102  }
103 
104 
105  if ( is_object( $term ) || is_numeric( $term ) ) {
106  $term = get_term( $term, $taxonomy, $output, $filter );
107  }
108  else {
109  $by = ( '%' === substr( $term, 0, 1 ) ) ? 'name' : 'slug';
110  $term = ltrim( $term, '%' );
111  $term = get_term_by( $by, $term, $taxonomy, $output, $filter );
112  }
113 
114  if ( !is_wp_error( $term ) ) {
115  $instance = new WPDKTerm( $term );
116  /* Get a WPDKTerm object if $parent param is TRUE and a parent id exists. */
117  if ( true === $parent && !empty( $instance->parent ) ) {
118  $instance->term_parent = self::term( $instance->parent, $taxonomy );
119  }
120  return $instance;
121  }
122  return $term;
123  }
124 
132  public function ancestor()
133  {
134  return $this->ancestorOfTerm( $this );
135  }
136 
146  public static function ancestorOfTerm( WPDKterm $term )
147  {
148 
149  $term_id = false;
150  $result = false;
151 
152  if ( is_a( $term, 'WPDKTerm' ) ) {
153  $term_id = $term->term_id;
154  }
155 
156  if ( !empty( $term_id ) ) {
157  while ( !empty( $term->parent ) ) {
158  $term = self::term( $term->parent, $term->taxonomy );
159  $result = self::term( $term->term_id, $term->taxonomy );
160  }
161  }
162  return $result;
163  }
164 
165 }
166 
180 class WPDKTerms {
181 
200  public $child_of;
208  public $exclude;
231  public $fields;
240  public $get;
251  public $hide_empty;
269  public $include;
278  public $name__like;
286  public $number;
294  public $offset;
305  public $order;
320  public $orderby;
330  public $pad_counts;
339  public $parent;
348  public $search;
356  public $slug;
364  private $_taxonomy;
365 
375  public function __construct( $taxonomy = 'category' )
376  {
377  $this->_taxonomy = $taxonomy;
378 
379  $defaults = array(
380  'orderby' => 'name',
381  'order' => 'ASC',
382  'hide_empty' => true,
383  'exclude' => array(),
384  'exclude_tree' => array(),
385  'include' => array(),
386  'number' => '',
387  'fields' => 'all',
388  'slug' => '',
389  'parent' => '',
390  'hierarchical' => true,
391  'child_of' => 0,
392  'get' => '',
393  'name__like' => '',
394  'pad_counts' => false,
395  'offset' => '',
396  'search' => '',
397  'cache_domain' => 'core'
398  );
399 
400  foreach ( $defaults as $property => $value ) {
401  $this->{$property} = $value;
402  }
403  }
404 
414  public function terms()
415  {
416  $args = (array)$this;
417 
418  /* Remove private. */
419  unset( $args['WPDKTerms_taxonomy'] );
420  $terms = get_terms( $this->_taxonomy, $args );
421 
422  return $terms;
423  }
424 
434  public function tree( $child_of = false )
435  {
436  if ( empty( $child_of ) ) {
437  $args = (array)$this;
438  $args['parent'] = 0;
439  unset( $args['WPDKTerms_taxonomy'] );
440  $terms = get_terms( $this->_taxonomy, $args );
441  foreach ( $terms as $term ) {
442  $term->children = $this->tree( $term );
443  }
444  }
445  else {
446  $args = (array)$this;
447  $args['parent'] = $child_of->term_id;
448  unset( $args['WPDKTerms_taxonomy'] );
449  $terms = get_terms( $this->_taxonomy, $args );
450  foreach ( $terms as $term ) {
451  $term->children = $this->tree( $term );
452  }
453  }
454 
455  return $terms;
456  }
457 
470  public function breadCrumbs( $term )
471  {
472  $from = $this->term( $term );
473  $stack = array( $from );
474  $id_parent = $from->parent;
475 
476  while ( !empty( $id_parent ) ) {
477  $stack[] = $term = $this->term( $id_parent );
478  $id_parent = $term->parent;
479  }
480 
481  return array_reverse( $stack );
482  }
483 
496  public function term( $term )
497  {
498  $term = WPDKTerm::term( $term, $this->_taxonomy );
499 
500  return $term;
501 
502  }
503 }