1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silk\Post; |
4
|
|
|
|
5
|
|
|
use WP_Query; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Silk\Query\Builder as BaseBuilder; |
8
|
|
|
|
9
|
|
|
class QueryBuilder extends BaseBuilder |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* WP_Query instance |
13
|
|
|
* |
14
|
|
|
* @var WP_Query |
15
|
|
|
*/ |
16
|
|
|
protected $query; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Post Model instance |
20
|
|
|
* |
21
|
|
|
* @var Model |
22
|
|
|
*/ |
23
|
|
|
protected $model; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Builder constructor. |
27
|
|
|
* |
28
|
|
|
* @param WP_Query $query |
29
|
|
|
*/ |
30
|
|
|
public function __construct(WP_Query $query = null) |
31
|
|
|
{ |
32
|
|
|
if (! $query) { |
33
|
|
|
$query = new WP_Query(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$this->query = $query; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create a new instance. |
41
|
|
|
* |
42
|
|
|
* @param WP_Query $query |
43
|
|
|
* |
44
|
|
|
* @return static |
45
|
|
|
*/ |
46
|
|
|
public static function make(WP_Query $query = null) |
47
|
|
|
{ |
48
|
|
|
return new static($query); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Limit the number of returned results |
53
|
|
|
* |
54
|
|
|
* @param integer $limit The maximum number of results to return |
55
|
|
|
* use -1 for no limit |
56
|
|
|
* |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function limit($limit) |
60
|
|
|
{ |
61
|
|
|
$this->query->set('posts_per_page', (int) $limit); |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Return an unlimited number of results. |
68
|
|
|
* |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
|
|
public function all() |
72
|
|
|
{ |
73
|
|
|
return $this->limit(-1); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set the order for the query |
78
|
|
|
* |
79
|
|
|
* @param string $order |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function order($order) |
84
|
|
|
{ |
85
|
|
|
$this->query->set('order', strtoupper($order)); |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Query by post status |
92
|
|
|
* |
93
|
|
|
* @param string|array $status the post status or stati to match |
94
|
|
|
* |
95
|
|
|
* @return $this |
96
|
|
|
*/ |
97
|
|
|
public function whereStatus($status) |
98
|
|
|
{ |
99
|
|
|
$this->query->set('post_status', $status); |
100
|
|
|
|
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Query by slug |
106
|
|
|
* |
107
|
|
|
* @param string $slug the post slug to query by |
108
|
|
|
* |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
|
|
public function whereSlug($slug) |
112
|
|
|
{ |
113
|
|
|
$this->query->set('name', $slug); |
114
|
|
|
|
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the results as a collection |
120
|
|
|
* |
121
|
|
|
* @return Collection |
122
|
|
|
*/ |
123
|
|
|
public function results() |
124
|
|
|
{ |
125
|
|
|
if ($this->model) { |
126
|
|
|
return $this->collectModels(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return Collection::make($this->query->get_posts()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Get the results as a collection of post model instances |
134
|
|
|
* |
135
|
|
|
* @return Collection |
136
|
|
|
*/ |
137
|
|
View Code Duplication |
protected function collectModels() |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$this->query->set('post_type', $this->model->post_type); |
140
|
|
|
$this->query->set('fields', ''); // as WP_Post objects |
141
|
|
|
$modelClass = get_class($this->model); |
142
|
|
|
|
143
|
|
|
return Collection::make($this->query->get_posts()) |
144
|
|
|
->map(function ($post) use ($modelClass) { |
145
|
|
|
return new $modelClass($post); |
146
|
|
|
}); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Set a query variable on the query |
151
|
|
|
* |
152
|
|
|
* @param string $var Query variable key |
153
|
|
|
* @param mixed $value Query value for key |
154
|
|
|
* |
155
|
|
|
* @return $this |
156
|
|
|
*/ |
157
|
|
|
public function set($var, $value) |
158
|
|
|
{ |
159
|
|
|
$this->query->set($var, $value); |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
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.