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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
<?php // phpcs:ignoreFile
namespace AutomateWoo;
use WP_Query;
if ( ! defined( 'ABSPATH' ) ) exit;
/** * @class Workflow_Query */ class Workflow_Query {
/** @var string|array */ public $trigger;
/** @var int */ public $limit = -1;
/** @var array */ public $args;
/** @var string */ public $return = 'objects';
/** * WP_Query instance of the last query. * * @since 4.9.0 * * @var WP_Query */ protected $last_wp_query;
function __construct() { $this->args = [ 'post_type' => 'aw_workflow', 'post_status' => 'publish', 'order' => 'ASC', 'orderby' => 'menu_order', 'posts_per_page' => $this->limit, 'meta_query' => [], 'suppress_filters' => true, 'no_found_rows' => true ]; }
/** * Set trigger name or array of names to query. * * @param string|array $trigger */ function set_trigger( $trigger ) { if ( $trigger instanceof Trigger ) { $this->trigger = $trigger->get_name(); } else { $this->trigger = $trigger; } }
/** * Set included ids array to query. * * @param string|array $ids */ function set_include( $ids ) { $this->args['post__in'] = wp_parse_list( $ids ); }
/** * Set per page limit query param. * * @param int $limit */ public function set_limit( $limit ) { $this->args['posts_per_page'] = absint( $limit ); }
/** * Set page query param. * * @since 4.9.0 * * @param int $page */ public function set_page( $page ) { $this->args['paged'] = absint( $page ); }
/** * Set stats query param. * * Default status is active. * * @param string $status One of: any|active|disabled */ public function set_status( $status ) { $status_map = [ 'any' => 'any', 'active' => 'publish', 'disabled' => 'aw-disabled' ];
$this->args['post_status'] = isset( $status_map[ $status ] ) ? $status_map[ $status ] : $status; }
/** * Set type query param. * * @param string $type * * @since 5.0.0 */ public function set_type( $type ) { $this->add_meta_query( 'type', $type ); }
/** * Set search query param. * * @since 4.9.0 * * @param string $term */ public function set_search( $term ) { $this->args['s'] = $term; }
/** * Set the value of the no_found_rows query param. * * Default value is true. * * Must be set to false in order to use $this->get_found_rows() * * @since 4.9.0 * * @param bool $no_found_rows */ public function set_no_found_rows( $no_found_rows ) { $this->args['no_found_rows'] = (bool) $no_found_rows; }
/** * @param $return - objects|ids */ function set_return( $return ) { $this->return = $return; }
/** * Add a meta query. * * @since 5.1.0 * * @param string $key The meta key. * @param string $value The meta value. * @param string $compare The meta compare. */ public function add_meta_query( $key, $value, $compare = '=' ) { $this->args['meta_query'][] = [ 'key' => $key, 'value' => $value, 'compare' => $compare, ]; }
/** * @return Workflow[] */ function get_results() {
if ( $this->trigger ) { $this->args['meta_query'][] = [ 'key' => 'trigger_name', 'value' => $this->trigger, ]; }
if ( $this->return == 'ids' ) { $this->args['fields'] = 'ids'; } $query = new WP_Query( $this->args ); $posts = $query->posts;
if ( ! $posts ) { return []; }
$workflows = [];
foreach ( $posts as $post ) {
if ( $this->return == 'ids' ) { $workflows[] = $post; } else { $workflow = new Workflow($post); $workflows[] = $workflow; }
}
$this->last_wp_query = $query; return $workflows; }
/** * Get found rows for the last query. * * @since 4.9.0 * * @return int */ public function get_found_rows() { if ( ! $this->last_wp_query instanceof WP_Query ) { return 0; }
return $this->last_wp_query->found_posts; }
/** * Alias of self::set_trigger() * * @param string|array $trigger */ function set_triggers( $trigger ) { $this->set_trigger( $trigger ); }
}
|