1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
<?php
namespace Tev\Term\Model;
use WP_Term;
use Tev\Contracts\WordpressWrapperInterface,
Tev\Term\Factory as TermFactory,
Tev\Taxonomy\Model\Taxonomy;
/**
* Wordpress Term entity.
*
* Provides a nicely object-oriented interface to a Wordpress taxonomy term.
*/
class Term implements WordpressWrapperInterface
{
/**
* Base term oject.
*
* @var \WP_Term
*/
private $base;
/**
* Term taxonomy.
*
* @var \Tev\Taxonomy\Model\Taxonomy
*/
private $taxonomy;
/**
* Term factory.
*
* @var \Tev\Term\Factory
*/
private $termFactory;
/**
* Parent term.
*
* @var \Tev\Term\Model\Term
*/
private $parent;
/**
* Constructor.
*
* Inject dependencies.
*
* @param \WP_Term $base Base term oject
* @param \Tev\Taxonomy\Model\Taxonomy $taxonomy Term taxonomy
* @param \Tev\Term\Factory $termFactory Term factory
* @return void
*/
public function __construct(WP_Term $base,
Taxonomy $taxonomy,
TermFactory $termFactory)
{
$this->base = $base;
$this->taxonomy = $taxonomy;
$this->termFactory = $termFactory;
$this->parent = null;
}
/**
* Get the term ID.
*
* @return int
*/
public function getId()
{
return $this->base->term_id;
}
/**
* Get the term name.
*
* @return string
*/
public function getName()
{
return $this->base->name;
}
/**
* Get the term slug.
*
* @return string
*/
public function getSlug()
{
return $this->base->slug;
}
/**
* Get the taxonomy for this term.
*
* @return \Tev\Taxonomy\Model\Taxonomy
*/
public function getTaxonomy()
{
return $this->taxonomy;
}
/**
* Get the term description.
*
* @return string
*/
public function getDescription()
{
return $this->base->description;
}
/**
* Get term URL.
*
* @param array $query Query string args. Key value pairs
* @return string
*/
public function getUrl($query = array())
{
return add_query_arg($query, get_term_link($this->base));
}
/**
* Get the parent term ID of this term.
*
* @return int|null
*/
public function getParentId()
{
return $this->base->parent ?: null;
}
/**
* Get the parent term of this term.
*
* @return \Tev\Term\Model\Term
*/
public function getParent()
{
if (($this->parent === null) && $this->getParentId()) {
$this->parent = $this->termFactory->create($this->getParentId(), $this->taxonomy);
}
return $this->parent;
}
/**
* Check whether or not this Term has a parent Term.
*
* @return boolean True if has a parent, false if not
*/
public function hasParent()
{
return (boolean) $this->getParentId();
}
/**
* Return array of direct child terms of this term.
*
* @return \Tev\Term\Model\Term[]
*/
public function getChildren()
{
$children = array();
$wpTerms = get_terms($this->taxonomy->getName(), array(
'hide_empty' => false,
'parent' => (int) $this->getId()
));
foreach ($wpTerms as $termData) {
$children[] = $this->termFactory->create($termData, $this->taxonomy);
}
return $children;
}
/**
* Get the underlying term object.
*
* @return \WP_Term
*/
public function getBaseObject()
{
return $this->base;
}
}