Passed
Push — master ( 7db120...6e1f17 )
by Brian
05:24 queued 11s
created

GetPaid_Subscriptions_Query::prepare_query_order()   B

Complexity

Conditions 7
Paths 40

Size

Total Lines 41
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 41
rs 8.8333
cc 7
nc 40
nop 2
1
<?php
2
/**
3
 * GetPaid_Subscriptions_Query class
4
 *
5
 * Contains core class used to query for subscriptions.
6
 *
7
 * @since 1.0.19
8
 */
9
10
/**
11
 * Main class used for querying subscriptions.
12
 *
13
 * @since 1.0.19
14
 *
15
 * @see GetPaid_Subscriptions_Query::prepare_query() for information on accepted arguments.
16
 */
17
class GetPaid_Subscriptions_Query {
18
19
	/**
20
	 * Query vars, after parsing
21
	 *
22
	 * @since 1.0.19
23
	 * @var array
24
	 */
25
	public $query_vars = array();
26
27
	/**
28
	 * List of found subscriptions.
29
	 *
30
	 * @since 1.0.19
31
	 * @var array
32
	 */
33
	private $results;
34
35
	/**
36
	 * Total number of found subscriptions for the current query
37
	 *
38
	 * @since 1.0.19
39
	 * @var int
40
	 */
41
	private $total_subscriptions = 0;
42
43
	/**
44
	 * The SQL query used to fetch matching subscriptions.
45
	 *
46
	 * @since 1.0.19
47
	 * @var string
48
	 */
49
	public $request;
50
51
	// SQL clauses
52
53
	/**
54
	 * Contains the 'FIELDS' sql clause
55
	 *
56
	 * @since 1.0.19
57
	 * @var string
58
	 */
59
	public $query_fields;
60
61
	/**
62
	 * Contains the 'FROM' sql clause
63
	 *
64
	 * @since 1.0.19
65
	 * @var string
66
	 */
67
	public $query_from;
68
69
	/**
70
	 * Contains the 'WHERE' sql clause
71
	 *
72
	 * @since 1.0.19
73
	 * @var string
74
	 */
75
	public $query_where;
76
77
	/**
78
	 * Contains the 'ORDER BY' sql clause
79
	 *
80
	 * @since 1.0.19
81
	 * @var string
82
	 */
83
	public $query_orderby;
84
85
	/**
86
	 * Contains the 'LIMIT' sql clause
87
	 *
88
	 * @since 1.0.19
89
	 * @var string
90
	 */
91
	public $query_limit;
92
93
	/**
94
	 * Class constructor.
95
	 *
96
	 * @since 1.0.19
97
	 *
98
	 * @param null|string|array $query Optional. The query variables.
99
	 */
100
	public function __construct( $query = null ) {
101
		if ( ! is_null( $query ) ) {
102
			$this->prepare_query( $query );
103
			$this->query();
104
		}
105
	}
106
107
	/**
108
	 * Fills in missing query variables with default values.
109
	 *
110
	 * @since 1.0.19
111
	 *
112
	 * @param  string|array $args Query vars, as passed to `GetPaid_Subscriptions_Query`.
113
	 * @return array Complete query variables with undefined ones filled in with defaults.
114
	 */
115
	public static function fill_query_vars( $args ) {
116
		$defaults = array(
117
			'status'            => 'all',
118
			'customer_in'       => array(),
119
			'customer_not_in'   => array(),
120
			'product_in'        => array(),
121
			'product_not_in'    => array(),
122
			'include'           => array(),
123
			'exclude'           => array(),
124
			'orderby'           => 'id',
125
			'order'             => 'DESC',
126
			'offset'            => '',
127
			'number'            => 10,
128
			'paged'             => 1,
129
			'count_total'       => true,
130
			'fields'            => 'all',
131
		);
132
133
		return wp_parse_args( $args, $defaults );
134
	}
135
136
	/**
137
	 * Prepare the query variables.
138
	 *
139
	 * @since 1.0.19
140
	 *
141
	 * @global wpdb $wpdb WordPress database abstraction object.
142
	 *
143
	 * @param string|array $query {
144
	 *     Optional. Array or string of Query parameters.
145
	 *
146
	 *     @type string|array $status              The subscription status to filter by. Can either be a single status or an array of statuses.
147
	 *                                             Default is all.
148
	 *     @type int[]        $customer_in         An array of customer ids to filter by.
149
	 *     @type int[]        $customer_not_in     An array of customer ids whose subscriptions should be excluded.
150
	 *     @type int[]        $product_in          An array of product ids to filter by.
151
	 *     @type int[]        $product_not_in      An array of product ids whose subscriptions should be excluded.
152
	 *     @type array        $date_created_query  A WP_Date_Query compatible array use to filter subscriptions by their date of creation.
153
	 *     @type array        $date_expires_query  A WP_Date_Query compatible array use to filter subscriptions by their expiration date.
154
	 *     @type array        $include             An array of subscription IDs to include. Default empty array.
155
	 *     @type array        $exclude             An array of subscription IDs to exclude. Default empty array.
156
	 *     @type string|array $orderby             Field(s) to sort the retrieved subscription by. May be a single value,
157
	 *                                             an array of values, or a multi-dimensional array with fields as
158
	 *                                             keys and orders ('ASC' or 'DESC') as values. Accepted values are
159
	 *                                             'id', 'customer_id', 'frequency', 'period', 'initial_amount,
160
	 *                                             'recurring_amount', 'bill_times', 'parent_payment_id', 'created', 'expiration'
161
	 *                                             'transaction_id', 'product_id', 'trial_period', 'include', 'status', 'profile_id'. Default array( 'id' ).
162
	 *     @type string       $order               Designates ascending or descending order of subscriptions. Order values
163
	 *                                             passed as part of an `$orderby` array take precedence over this
164
	 *                                             parameter. Accepts 'ASC', 'DESC'. Default 'DESC'.
165
	 *     @type int          $offset              Number of subscriptions to offset in retrieved results. Can be used in
166
	 *                                             conjunction with pagination. Default 0.
167
	 *     @type int          $number              Number of subscriptions to limit the query for. Can be used in
168
	 *                                             conjunction with pagination. Value -1 (all) is supported, but
169
	 *                                             should be used with caution on larger sites.
170
	 *                                             Default 10.
171
	 *     @type int          $paged               When used with number, defines the page of results to return.
172
	 *                                             Default 1.
173
	 *     @type bool         $count_total         Whether to count the total number of subscriptions found. If pagination
174
	 *                                             is not needed, setting this to false can improve performance.
175
	 *                                             Default true.
176
	 *     @type string|array $fields              Which fields to return. Single or all fields (string), or array
177
	 *                                             of fields. Accepts 'id', 'customer_id', 'frequency', 'period', 'initial_amount,
178
	 *                                             'recurring_amount', 'bill_times', 'parent_payment_id', 'created', 'expiration'
179
	 *                                             'transaction_id', 'product_id', 'trial_period', 'status', 'profile_id'.
180
	 *                                             Use 'all' for all fields. Default 'all'.
181
	 * }
182
	 */
183
	public function prepare_query( $query = array() ) {
184
		global $wpdb;
185
186
		if ( empty( $this->query_vars ) || ! empty( $query ) ) {
187
			$this->query_limit = null;
188
			$this->query_vars  = $this->fill_query_vars( $query );
189
		}
190
191
		if ( ! empty( $this->query_vars['fields'] ) && 'all' !== $this->query_vars['fields'] ) {
192
			$this->query_vars['fields'] = wpinv_parse_list( $this->query_vars['fields'] );
193
		}
194
195
		do_action( 'getpaid_pre_get_subscriptions', array( &$this ) );
196
197
		// Ensure that query vars are filled after 'getpaid_pre_get_subscriptions'.
198
		$qv                =& $this->query_vars;
199
		$qv                = $this->fill_query_vars( $qv );
200
		$table             = $wpdb->prefix . 'wpinv_subscriptions';
201
		$this->query_from  = "FROM $table";
202
203
		// Prepare query fields.
204
		$this->prepare_query_fields( $qv, $table );
205
206
		// Prepare query where.
207
		$this->prepare_query_where( $qv, $table );
208
209
		// Prepare query order.
210
		$this->prepare_query_order( $qv, $table );
211
212
		// limit
213
		if ( isset( $qv['number'] ) && $qv['number'] > 0 ) {
214
			if ( $qv['offset'] ) {
215
				$this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['offset'], $qv['number'] );
216
			} else {
217
				$this->query_limit = $wpdb->prepare( 'LIMIT %d, %d', $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] );
218
			}
219
		}
220
221
		do_action_ref_array( 'getpaid_after_subscriptions_query', array( &$this ) );
222
	}
223
224
	/**
225
	 * Prepares the query fields.
226
	 *
227
	 * @since 1.0.19
228
	 *
229
	 * @param array $qv Query vars.
230
	 * @param string $table Table name.
231
	 */
232
	protected function prepare_query_fields( &$qv, $table ) {
233
234
		if ( is_array( $qv['fields'] ) ) {
235
			$qv['fields'] = array_unique( $qv['fields'] );
236
237
			$query_fields = array();
238
			foreach ( $qv['fields'] as $field ) {
239
				$field          = sanitize_key( $field );
240
				$query_fields[] = "$table.`$field`";
241
			}
242
			$this->query_fields = implode( ',', $query_fields );
243
		} else {
244
			$this->query_fields = "$table.*";
245
		}
246
247
		if ( isset( $qv['count_total'] ) && $qv['count_total'] ) {
248
			$this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields;
249
		}
250
251
	}
252
253
	/**
254
	 * Prepares the query where.
255
	 *
256
	 * @since 1.0.19
257
	 *
258
	 * @param array $qv Query vars.
259
	 * @param string $table Table name.
260
	 */
261
	protected function prepare_query_where( &$qv, $table ) {
262
		global $wpdb;
263
		$this->query_where = 'WHERE 1=1';
264
265
		// Status.
266
		if ( 'all' !== $qv['status'] ) {
267
			$statuses           = wpinv_clean( wpinv_parse_list( $qv['status'] ) );
268
			$prepared_statuses  = join( ',', array_fill( 0, count( $statuses ), '%s' ) );
269
			$this->query_where .= $wpdb->prepare( " AND $table.`status` IN ( $prepared_statuses )", $statuses );
270
		}
271
272
		if ( ! empty( $qv['customer_in'] ) ) {
273
			$customer_in        = implode( ',', wp_parse_id_list( $qv['customer_in'] ) );
274
			$this->query_where .= " AND $table.`customer_id` IN ($customer_in)";
275
		} elseif ( ! empty( $qv['customer_not_in'] ) ) {
276
			$customer_not_in    = implode( ',', wp_parse_id_list( $qv['customer_not_in'] ) );
277
			$this->query_where .= " AND $table.`customer_id` NOT IN ($customer_not_in)";
278
		}
279
280
		if ( ! empty( $qv['product_in'] ) ) {
281
			$product_in         = implode( ',', wp_parse_id_list( $qv['product_in'] ) );
282
			$this->query_where .= " AND $table.`product_id` IN ($product_in)";
283
		} elseif ( ! empty( $qv['product_not_in'] ) ) {
284
			$product_not_in     = implode( ',', wp_parse_id_list( $qv['product_not_in'] ) );
285
			$this->query_where .= " AND $table.`product_id` NOT IN ($product_not_in)";
286
		}
287
288
		if ( ! empty( $qv['include'] ) ) {
289
			$include            = implode( ',', wp_parse_id_list( $qv['include'] ) );
290
			$this->query_where .= " AND $table.`id` IN ($include)";
291
		} elseif ( ! empty( $qv['exclude'] ) ) {
292
			$exclude            = implode( ',', wp_parse_id_list( $qv['exclude'] ) );
293
			$this->query_where .= " AND $table.`id` NOT IN ($exclude)";
294
		}
295
296
		// Date queries are allowed for the subscription creation date.
297
		if ( ! empty( $qv['date_created_query'] ) && is_array( $qv['date_created_query'] ) ) {
298
			$date_created_query = new WP_Date_Query( $qv['date_created_query'], "$table.created" );
299
			$this->query_where .= $date_created_query->get_sql();
300
		}
301
302
		// Date queries are also allowed for the subscription expiration date.
303
		if ( ! empty( $qv['date_expires_query'] ) && is_array( $qv['date_expires_query'] ) ) {
304
			$date_expires_query = new WP_Date_Query( $qv['date_expires_query'], "$table.expiration" );
305
			$this->query_where .= $date_expires_query->get_sql();
306
		}
307
308
	}
309
310
	/**
311
	 * Prepares the query order.
312
	 *
313
	 * @since 1.0.19
314
	 *
315
	 * @param array $qv Query vars.
316
	 * @param string $table Table name.
317
	 */
318
	protected function prepare_query_order( &$qv, $table ) {
319
320
		// sorting.
321
		$qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : '';
322
		$order       = $this->parse_order( $qv['order'] );
323
324
		// Default order is by 'id' (latest subscriptions).
325
		if ( empty( $qv['orderby'] ) ) {
326
			$qv['orderby'] = array( 'id' );
327
		}
328
329
		// 'orderby' values may be an array, comma- or space-separated list.
330
		$ordersby      = array_filter( wpinv_parse_list(  $qv['orderby'] ) );
331
332
		$orderby_array = array();
333
		foreach ( $ordersby as $_key => $_value ) {
334
335
			if ( is_int( $_key ) ) {
336
				// Integer key means this is a flat array of 'orderby' fields.
337
				$_orderby = $_value;
338
				$_order   = $order;
339
			} else {
340
				// Non-integer key means that the key is the field and the value is ASC/DESC.
341
				$_orderby = $_key;
342
				$_order   = $_value;
343
			}
344
345
			$parsed = $this->parse_orderby( $_orderby, $table );
346
347
			if ( $parsed ) {
348
				$orderby_array[] = $parsed . ' ' . $this->parse_order( $_order );
349
			}
350
351
		}
352
353
		// If no valid clauses were found, order by id.
354
		if ( empty( $orderby_array ) ) {
355
			$orderby_array[] = "id $order";
356
		}
357
358
		$this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array );
359
360
	}
361
362
	/**
363
	 * Execute the query, with the current variables.
364
	 *
365
	 * @since 1.0.19
366
	 *
367
	 * @global wpdb $wpdb WordPress database abstraction object.
368
	 */
369
	public function query() {
370
		global $wpdb;
371
372
		$qv =& $this->query_vars;
373
374
		// Return a non-null value to bypass the default GetPaid subscriptions query and remember to set the
375
		// total_subscriptions property.
376
		$this->results = apply_filters_ref_array( 'getpaid_subscriptions_pre_query', array( null, &$this ) );
377
378
		if ( null === $this->results ) {
379
			$this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit";
380
381
			if ( ( is_array( $qv['fields'] ) && 1 != count( $qv['fields'] ) ) || 'all' == $qv['fields'] ) {
382
				$this->results = $wpdb->get_results( $this->request );
383
			} else {
384
				$this->results = $wpdb->get_col( $this->request );
385
			}
386
387
			if ( isset( $qv['count_total'] ) && $qv['count_total'] ) {
388
				$found_subscriptions_query = apply_filters( 'getpaid_found_subscriptions_query', 'SELECT FOUND_ROWS()', $this );
389
				$this->total_subscriptions   = (int) $wpdb->get_var( $found_subscriptions_query );
390
			}
391
		}
392
393
		if ( 'all' == $qv['fields'] ) {
394
			foreach ( $this->results as $key => $subscription ) {
395
				$this->results[ $key ] = new WPInv_Subscription( $subscription );
396
			}
397
		}
398
399
	}
400
401
	/**
402
	 * Retrieve query variable.
403
	 *
404
	 * @since 1.0.19
405
	 *
406
	 * @param string $query_var Query variable key.
407
	 * @return mixed
408
	 */
409
	public function get( $query_var ) {
410
		if ( isset( $this->query_vars[ $query_var ] ) ) {
411
			return $this->query_vars[ $query_var ];
412
		}
413
414
		return null;
415
	}
416
417
	/**
418
	 * Set query variable.
419
	 *
420
	 * @since 1.0.19
421
	 *
422
	 * @param string $query_var Query variable key.
423
	 * @param mixed $value Query variable value.
424
	 */
425
	public function set( $query_var, $value ) {
426
		$this->query_vars[ $query_var ] = $value;
427
	}
428
429
	/**
430
	 * Return the list of subscriptions.
431
	 *
432
	 * @since 1.0.19
433
	 *
434
	 * @return WPInv_Subscription[]|array Found subscriptions.
435
	 */
436
	public function get_results() {
437
		return $this->results;
438
	}
439
440
	/**
441
	 * Return the total number of subscriptions for the current query.
442
	 *
443
	 * @since 1.0.19
444
	 *
445
	 * @return int Number of total subscriptions.
446
	 */
447
	public function get_total() {
448
		return $this->total_subscriptions;
449
	}
450
451
	/**
452
	 * Parse and sanitize 'orderby' keys passed to the subscriptions query.
453
	 *
454
	 * @since 1.0.19
455
	 *
456
	 * @param string $orderby Alias for the field to order by.
457
	 *  @param string $table The current table.
458
	 * @return string Value to use in the ORDER clause, if `$orderby` is valid.
459
	 */
460
	protected function parse_orderby( $orderby, $table ) {
461
462
		$_orderby = '';
463
		if ( in_array( $orderby, array( 'customer_id', 'frequency', 'period', 'initial_amount', 'recurring_amount', 'bill_times', 'transaction_id', 'parent_payment_id', 'product_id', 'created', 'expiration', 'trial_period', 'status', 'profile_id' ) ) ) {
464
			$_orderby = "$table.`$orderby`";
465
		} elseif ( 'id' === strtolower( $orderby ) ) {
466
			$_orderby = "$table.id";
467
		} elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) {
468
			$include     = wp_parse_id_list( $this->query_vars['include'] );
469
			$include_sql = implode( ',', $include );
470
			$_orderby    = "FIELD( $table.id, $include_sql )";
471
		}
472
473
		return $_orderby;
474
	}
475
476
	/**
477
	 * Parse an 'order' query variable and cast it to ASC or DESC as necessary.
478
	 *
479
	 * @since 1.0.19
480
	 *
481
	 * @param string $order The 'order' query variable.
482
	 * @return string The sanitized 'order' query variable.
483
	 */
484
	protected function parse_order( $order ) {
485
		if ( ! is_string( $order ) || empty( $order ) ) {
0 ignored issues
show
introduced by
The condition is_string($order) is always true.
Loading history...
486
			return 'DESC';
487
		}
488
489
		if ( 'ASC' === strtoupper( $order ) ) {
490
			return 'ASC';
491
		} else {
492
			return 'DESC';
493
		}
494
	}
495
496
}
497