1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silk\Term; |
4
|
|
|
|
5
|
|
|
use WP_Term; |
6
|
|
|
use Silk\Query\Builder as BaseBuilder; |
7
|
|
|
use Silk\Exception\WP_ErrorException; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
|
10
|
|
|
class QueryBuilder extends BaseBuilder |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The term model |
14
|
|
|
* @var Model |
15
|
|
|
*/ |
16
|
|
|
protected $model; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Collection of arguments |
20
|
|
|
* @var Collection |
21
|
|
|
*/ |
22
|
|
|
protected $args; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Taxonomy Identifier |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $taxonomy; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* TermQueryBuilder Constructor. |
32
|
|
|
* |
33
|
|
|
* @param array $args |
34
|
|
|
*/ |
35
|
|
|
public function __construct(array $args = []) |
36
|
|
|
{ |
37
|
|
|
$this->args = Collection::make($args); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create a new instance. |
42
|
|
|
* |
43
|
|
|
* @return static |
44
|
|
|
*/ |
45
|
|
|
public static function make() |
46
|
|
|
{ |
47
|
|
|
return new static; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Restrict the query to terms of the provided Taxonomy. |
52
|
|
|
* |
53
|
|
|
* @param string $taxonomy |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
public function forTaxonomy($taxonomy) |
58
|
|
|
{ |
59
|
|
|
$this->taxonomy = $taxonomy; |
60
|
|
|
|
61
|
|
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get all terms. |
66
|
|
|
* |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
|
|
public function all() |
70
|
|
|
{ |
71
|
|
|
return $this->includeEmpty() |
72
|
|
|
->limit('all'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Include terms that have no related objects in the results. |
77
|
|
|
* |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
|
|
public function includeEmpty() |
81
|
|
|
{ |
82
|
|
|
$this->args->put('hide_empty', false); |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Limit the maximum number of results returned. |
89
|
|
|
* |
90
|
|
|
* @param int $max_results Maximum number to return. 0 or 'all' for unlimited. |
91
|
|
|
* |
92
|
|
|
* @return $this |
93
|
|
|
*/ |
94
|
|
|
public function limit($max_results) |
95
|
|
|
{ |
96
|
|
|
$this->args->put('number', intval($max_results)); |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the query results. |
103
|
|
|
* |
104
|
|
|
* @throws WP_ErrorException |
105
|
|
|
* |
106
|
|
|
* @return Collection |
107
|
|
|
*/ |
108
|
|
|
public function results() |
109
|
|
|
{ |
110
|
|
|
if ($this->model) { |
111
|
|
|
return $this->collectModels(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ($this->taxonomy) { |
115
|
|
|
$this->args->put('taxonomy', $this->taxonomy); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return Collection::make($this->fetchTerms()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get the results as a collection of models. |
123
|
|
|
* |
124
|
|
|
* @return Collection |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
protected function collectModels() |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$this->args->put('taxonomy', $this->model->taxonomy); |
129
|
|
|
$this->args->put('fields', 'all'); |
130
|
|
|
|
131
|
|
|
$modelClass = get_class($this->model); |
132
|
|
|
|
133
|
|
|
return Collection::make($this->fetchTerms()) |
134
|
|
|
->map(function (WP_Term $term) use ($modelClass) { |
135
|
|
|
return new $modelClass($term); |
136
|
|
|
}); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Perform the term query and return the results. |
141
|
|
|
* |
142
|
|
|
* @throws WP_ErrorException |
143
|
|
|
* |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
protected function fetchTerms() |
147
|
|
|
{ |
148
|
|
|
if (is_wp_error($terms = get_terms($this->args->toArray()))) { |
149
|
|
|
throw new WP_ErrorException($terms); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $terms; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.