/
abstract.llms.database.query.php
413 lines (353 loc) · 10.5 KB
/
abstract.llms.database.query.php
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?php
/**
* Database Query Abstract
*
* @package LifterLMS/Abstracts/Classes
*
* @since 3.8.0
* @version 6.0.0
*/
defined( 'ABSPATH' ) || exit;
/**
* Database Query abstract class.
*
* @since 3.8.0
* @since 3.30.3 `is_last_page()` method returns `true` when no results are found.
* @since 3.34.0 Sanitizes sort parameters.
*/
abstract class LLMS_Database_Query extends LLMS_Abstract_Query {
/**
* Identify the extending query.
*
* @var string
*/
protected $id = 'database';
/**
* Retrieve query argument default values.
*
* @since 6.0.0
*
* @return array
*/
protected function default_arguments() {
return wp_parse_args(
array(
'per_page' => 25,
'sort' => array(
'id' => 'ASC',
),
),
parent::default_arguments()
);
}
/**
* Escape and add quotes to a string, useful for array mapping when building queries.
*
* @since 3.8.0
* @since 6.0.0 Use {@see llms_esc_and_quote_str()}.
*
* @param mixed $input Input data.
* @return string
*/
public function escape_and_quote_string( $input ) {
return llms_esc_and_quote_str( $input );
}
/**
* Retrieve default arguments for the query.
*
* @since 3.8.0
* @since 4.5.1 Added new default arg `no_found_rows` set to false.
* @since 6.0.0 Call parent method.
*
* @todo This should be removed in favor of the parent method only when the
* `llms_db_query_get_default_args` hook is removed.
*
* @return array
*/
protected function get_default_args() {
if ( $this->get( 'suppress_filters' ) ) {
return $this->default_arguments();
}
// Get them from the parent with the new replacement filter.
$args = parent::get_default_args();
/**
* Filters the query default args.
*
* @since 3.8.0
* @deprecated 6.0.0 Filter `llms_db_query_get_default_args` is deprecated in favor of `llms_{$this->id}_query_get_default_args`.
*
* @param array $args Array of default arguments to set up the query with.
*/
return apply_filters_deprecated( 'llms_db_query_get_default_args', array( $args ), '6.0.0', "llms_{$this->id}_query_get_default_args" );
}
/**
* Get a string used as filter names unique to the extending query.
*
* @since 3.8.0
*
* @todo Deprecate.
*
* @param string $filter Filter name.
* @return string
*/
protected function get_filter( $filter ) {
return 'llms_' . $this->id . '_query_' . $filter;
}
/**
* Get the number of results to skip for the query based on the current page and per_page vars.
*
* @since 3.8.0
*
* @return int
*/
protected function get_skip() {
return absint( ( $this->get( 'page' ) - 1 ) * $this->get( 'per_page' ) );
}
/**
* Performs the SQL query.
*
* @since 6.0.0
*
* @return array An integer-keyed array of row objects.
*/
protected function perform_query() {
global $wpdb;
return $wpdb->get_results( $this->query ); // phpcs:ignore: WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared
}
/**
* Set variables related to total number of results and pages possible with supplied arguments.
*
* @since 3.8.0
* @since 4.5.1 Bail early if the query arg `no_found_rows` is true, b/c no reason to calculate anything.
* @deprecated 6.0.0 `LLMS_Database_Query::set_found_results()` is deprecated.
*
* @return void
*/
protected function set_found_results() {
_deprecated_function( 'LLMS_Database_Query::set_found_results()', '6.0.0' );
// If no results, or found rows not required, bail early b/c no reason to calculate anything.
if ( ! $this->number_results || $this->get( 'no_found_rows' ) ) {
return;
}
$this->found_results = $this->found_results();
$this->max_pages = absint( ceil( $this->found_results / $this->get( 'per_page' ) ) );
}
/**
* Perform a SQL to retrieve the total number of found results for the given query.
*
* @since 6.0.0
*
* @return int
*/
protected function found_results() {
global $wpdb;
return (int) $wpdb->get_var( 'SELECT FOUND_ROWS()' ); // db call ok; no-cache ok.
}
/**
* Retrieve the prepared SQL for the SELECT clause.
*
* @since 4.5.1
*
* @param string $select_columns Optional. Columns to select. Default '*'.
* @return string
*/
protected function sql_select_columns( $select_columns = '*' ) {
if ( ! $this->get( 'no_found_rows' ) ) {
$select_columns = 'SQL_CALC_FOUND_ROWS ' . $select_columns;
}
if ( $this->get( 'suppress_filters' ) ) {
return $select_columns;
}
/**
* Filters the query SELECT columns.
*
* The dynamic part of the filter `$this->id` identifies the extending query.
*
* @since 4.5.1
*
* @param string $select_columns Columns to select.
* @param LLMS_Database_Query $db_query Instance of LLMS_Database_Query.
*/
return apply_filters( "llms_{$this->id}_query_select_columns", $select_columns, $this );
}
/**
* Retrieve the prepared SQL for the LIMIT clause.
*
* @since 3.16.0
* @since 4.5.1 Drop use of `$this->get_filter('limit')` in favor of `"llms_{$this->id}_query_limit"`.
*
* @return string
*/
protected function sql_limit() {
global $wpdb;
$sql = $wpdb->prepare( 'LIMIT %d, %d', $this->get_skip(), $this->get( 'per_page' ) );
/**
* Filters the query LIMIT clause.
*
* The dynamic part of the filter `$this->id` identifies the extending query.
*
* @since 3.16.0
*
* @param string $sql The LIMIT clause of the query.
* @param LLMS_Database_Query $db_query The LLMS_Database_Query instance.
*/
return apply_filters( "llms_{$this->id}_query_limit", $sql, $this );
}
/**
* Retrieve the prepared SQL for the ORDER BY clause.
*
* @since 3.8.0
* @since 3.34.0 Returns an empty string if no sort fields are available.
* @since 4.5.1 Drop use of `$this->get_filter('orderby')` in favor of `"llms_{$this->id}_query_orderby"`.
*
* @return string
*/
protected function sql_orderby() {
$sql = '';
$sort = $this->get( 'sort' );
if ( $sort ) {
$sql = 'ORDER BY';
$comma = false;
foreach ( $sort as $orderby => $order ) {
$pre = ( $comma ) ? ', ' : ' ';
$sql .= $pre . "{$orderby} {$order}";
$comma = true;
}
}
if ( $this->get( 'suppress_filters' ) ) {
return $sql;
}
/**
* Filters the query ORDER BY clause.
*
* The dynamic part of the filter `$this->id` identifies the extending query.
*
* @since 3.8.0
*
* @param string $sql The ORDER BY clause of the query.
* @param LLMS_Database_Query $db_query The LLMS_Database_Query instance.
*/
return apply_filters( "llms_{$this->id}_query_orderby", $sql, $this );
}
/**
* Gets information about properties that used to be public and have been replaced with public getters.
*
* Used by `__get()` and `__set()` and will be removed when these are properly removed in the next
* major release.
*
* @since 6.0.0
*
* @return array
*/
private function legacy_public_props() {
return array(
// Property => $0 = alternative prop or method, $1 = has replacement.
'found_results' => array( 'get_found_results', true ),
'max_pages' => array( 'get_max_pages', true ),
'number_results' => array( 'get_number_results', true ),
'query_vars' => array( 'query_vars', false ),
'results' => array( 'get_results', true ),
);
}
/**
* Throws a deprecation message when a formerly public property is accessed directly.
*
* @since 6.0.0
*
* @param string $prop Property name.
* @return void
*/
private function public_prop_deprecation( $prop ) {
$legacy_props = $this->legacy_public_props();
list( $val, $has_replacement ) = $legacy_props[ $prop ];
$class = get_called_class();
$is_method = method_exists( $this, $val );
$suffix = $is_method ? '()' : '';
_deprecated_function( "Public access to property {$class}::{$prop}", '6.0.0', $has_replacement ? "{$class}::{$val}{$suffix}" : '' );
}
/**
* Preserve backwards compat for read access to formerly public and removed class properties.
*
* @since 6.0.0
*
* @param string $key Property key name.
* @return mixed
*/
public function __get( $key ) {
// Handle formerly public properties.
$legacy_props = $this->legacy_public_props();
if ( array_key_exists( $key, $legacy_props ) ) {
$this->public_prop_deprecation( $key );
$val = $legacy_props[ $key ][0];
return method_exists( $this, $val ) ? $this->$val() : $this->$val;
} elseif ( 'sql' === $key ) {
$class = get_called_class();
_deprecated_function( "Property {$class}::sql", '6.0.0', "{$class}::get_query()" );
return $this->query;
}
}
/**
* Preserve backwards compat for write access to formerly public and removed class properties.
*
* @since 6.0.0
*
* @param string $key Property name.
* @param mixed $val Property value.
* @return void
*/
public function __set( $key, $val ) {
$legacy_props = $this->legacy_public_props();
if ( array_key_exists( $key, $legacy_props ) ) {
$this->public_prop_deprecation( $key );
$this->$key = $val;
} elseif ( 'sql' === $key ) {
$class = get_called_class();
_deprecated_function( "Property {$class}::sql", '6.0.0', "{$class}::query" );
$this->query = $val;
}
}
/**
* Handle backwards compatibility for the misspelled (and removed) method `preprare_query()`.
*
* @since 6.0.0
*
* @param string $name Method name.
* @param array $args Arguments passed to the method.
* @return void|string
*/
public function __call( $name, $args ) {
if ( 'preprare_query' === $name ) {
$class = get_called_class();
_deprecated_function( "{$class}::preprare_query()", '6.0.0', "{$class}::prepare_query()" );
return $this->prepare_query();
}
}
/**
* Prepare the query.
*
* Should return the query which will be used by `query()`.
*
* This *should* be an abstract method but is defined here for backwards compatibility
* to preserve the previous method, `preprare_query()` (notice the misspelling).
*
* Once the `preprare_query()` method is fully removed in the next major release this
* method can be removed in favor of the abstract from the parent class.
*
* @since 6.0.0
*
* @return mixed
*/
protected function prepare_query() {
if ( method_exists( $this, 'preprare_query' ) ) {
$class = get_called_class();
_deprecated_function( "{$class}::preprare_query()", '6.0.0', "{$class}::prepare_query()" );
return $this->preprare_query();
} else {
_doing_it_wrong(
__METHOD__,
sprintf( __( "Method '%s' not implemented. Must be overridden in subclass.", 'lifterlms' ), __METHOD__ ),
'6.0.0'
);
}
}
}