-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.php
More file actions
1024 lines (880 loc) · 34.8 KB
/
query.php
File metadata and controls
1024 lines (880 loc) · 34.8 KB
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php defined( 'ABSPATH' ) || exit;
/**
* Query class
*
* Based on WP_Query class
*/
class TDB_Query {
/**
* Query vars set by the user
*
* @since 1.0.0
* @access public
* @var array
*/
public $query;
/**
* Query vars, after parsing
*
* @since 1.0.0
* @access public
* @var array
*/
public $query_vars = array();
/**
* Holds the data for a single object that is queried.
*
* Holds the contents of a post, page, category, attachment.
*
* @since 1.0.0
* @access public
* @var object|array
*/
public $queried_object;
/**
* The ID of the queried object.
*
* @since 1.0.0
* @access public
* @var int
*/
public $queried_object_id;
/**
* Get post database query.
*
* @since 1.0.0
* @access public
* @var string
*/
public $request;
/**
* List of posts.
*
* @since 1.0.0
* @access public
* @var array
*/
public $results;
/**
* The amount of posts for the current query.
*
* @since 1.0.0
* @access public
* @var int
*/
public $result_count = 0;
/**
* Index of the current item in the loop.
*
* @since 1.0.0
* @access public
* @var int
*/
public $current_result = -1;
/**
* Whether the loop has started and the caller is in the loop.
*
* @since 1.0.0
* @access public
* @var bool
*/
public $in_the_loop = false;
/**
* The current post.
*
* @since 1.0.0
* @access public
* @var WP_Post
*/
public $result;
/**
* The amount of found posts for the current query.
*
* If limit clause was not used, equals $post_count.
*
* @since 1.0.0
* @access public
* @var int
*/
public $found_results = 0;
/**
* The amount of pages.
*
* @since 1.0.0
* @access public
* @var int
*/
public $max_num_pages = 0;
public function __construct( $query = '' ) {
$this->init();
$this->query = $this->query_vars = wp_parse_args( $query );
}
public function init() {
unset($this->results);
unset($this->query);
$this->query_vars = array();
unset($this->queried_object);
unset($this->queried_object_id);
$this->result_count = 0;
$this->current_result = -1;
$this->in_the_loop = false;
unset( $this->request );
unset( $this->result );
$this->found_results = 0;
$this->max_num_pages = 0;
}
public function set( $query_var, $value ) {
$this->query_vars[$query_var] = $value;
}
public function get_results() {
global $wpdb, $tdb_table;
$this->parse_query();
/**
* Fires after the query variable object is created, but before the actual query is run.
*
* Note: If using conditional tags, use the method versions within the passed instance
* (e.g. $this->is_main_query() instead of is_main_query()). This is because the functions
* like is_main_query() test against the global $wp_query instance, not the passed one.
*
* @since 1.0.0
*
* @param TDB_Query &$this The TDB_Query instance (passed by reference).
*/
do_action_ref_array( 'tdb_pre_get_results', array( &$this ) );
// Shorthand.
$q = &$this->query_vars;
// Fill again in case pre_get_posts unset some vars.
$q = $this->fill_query_vars($q);
// Suppress filters
if ( ! isset($q['suppress_filters']) )
$q['suppress_filters'] = false;
if ( empty( $q['items_per_page'] ) ) {
$q['items_per_page'] = get_option( 'items_per_page', 20 );
}
// Pagination
if ( ! isset( $q['nopaging'] ) ) {
if ( $q['items_per_page'] == -1 ) {
$q['nopaging'] = true;
} else {
$q['nopaging'] = false;
}
}
// Items per page
$q['items_per_page'] = (int) $q['items_per_page'];
if ( $q['items_per_page'] < -1 )
$q['items_per_page'] = abs($q['items_per_page']);
elseif ( $q['items_per_page'] == 0 )
$q['items_per_page'] = 1;
// page
if ( isset($q['page']) ) {
$q['page'] = trim($q['page'], '/');
$q['page'] = absint($q['page']);
}
// If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
if ( isset($q['no_found_rows']) ) {
$q['no_found_rows'] = (bool) $q['no_found_rows'];
} else {
$q['no_found_rows'] = false;
}
switch ( $q['fields'] ) {
case 'ids':
$fields = "{$tdb_table->db->table_name}.{$tdb_table->db->primary_key}";
break;
default:
$fields = "{$tdb_table->db->table_name}.*";
}
// First let's clear some variables
$distinct = '';
$where = '';
$limits = '';
$join = '';
$search = '';
$groupby = '';
$orderby = '';
$page = 1;
// If a search pattern is specified, load the posts that match.
if ( strlen( $q['s'] ) ) {
$search = $this->parse_search( $q );
}
if ( ! $q['suppress_filters'] ) {
/**
* Filters the search SQL that is used in the WHERE clause of WP_Query.
*
* @since 1.0.0
*
* @param string $search Search SQL for WHERE clause.
* @param WP_Query $this The current WP_Query object.
*/
$search = apply_filters_ref_array( 'tdb_query_search', array( $search, &$this ) );
}
$where .= $search;
// Rand order by
$rand = ( isset( $q['orderby'] ) && 'rand' === $q['orderby'] );
if ( ! isset( $q['order'] ) ) {
$q['order'] = $rand ? '' : 'DESC';
} else {
$q['order'] = $rand ? '' : $this->parse_order( $q['order'] );
}
// Order by.
if ( empty( $q['orderby'] ) ) {
/*
* Boolean false or empty array blanks out ORDER BY,
* while leaving the value unset or otherwise empty sets the default.
*/
if ( isset( $q['orderby'] ) && ( is_array( $q['orderby'] ) || false === $q['orderby'] ) ) {
$orderby = '';
} else {
// Default order by primary key
$orderby = "{$tdb_table->db->table_name}.{$tdb_table->db->primary_key} " . $q['order'];
}
} elseif ( 'none' == $q['orderby'] ) {
$orderby = '';
} else {
$orderby_array = array();
if ( is_array( $q['orderby'] ) ) {
foreach ( $q['orderby'] as $_orderby => $order ) {
$orderby = addslashes_gpc( urldecode( $_orderby ) );
$orderby_array[] = $orderby . ' ' . $this->parse_order( $order );
}
$orderby = implode( ', ', $orderby_array );
} else {
$q['orderby'] = urldecode( $q['orderby'] );
$q['orderby'] = addslashes_gpc( $q['orderby'] );
foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
$orderby_array[] = $orderby;
}
$orderby = implode( ' ' . $q['order'] . ', ', $orderby_array );
if ( empty( $orderby ) ) {
$orderby = "{$tdb_table->db->table_name}.{$tdb_table->db->primary_key} " . $q['order'];
} elseif ( ! empty( $q['order'] ) ) {
$orderby .= " {$q['order']}";
}
}
}
/*
* Apply filters on where and join prior to paging so that any
* manipulations to them are reflected in the paging by day queries.
*/
if ( !$q['suppress_filters'] ) {
/**
* Filters the WHERE clause of the query.
*
* @since 1.0.0
*
* @param string $where The WHERE clause of the query.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$where = apply_filters_ref_array( 'tdb_query_where', array( $where, &$this ) );
/**
* Filters the JOIN clause of the query.
*
* @since 1.0.0
*
* @param string $where The JOIN clause of the query.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$join = apply_filters_ref_array( 'tdb_query_join', array( $join, &$this ) );
}
// Paging
if ( empty($q['nopaging']) ) {
$page = absint($q['paged']);
if ( !$page )
$page = 1;
// If 'offset' is provided, it takes precedence over 'paged'.
if ( isset( $q['offset'] ) && is_numeric( $q['offset'] ) ) {
$q['offset'] = absint( $q['offset'] );
$pgstrt = $q['offset'] . ', ';
} else {
$pgstrt = absint( ( $page - 1 ) * $q['items_per_page'] ) . ', ';
}
$limits = 'LIMIT ' . $pgstrt . $q['items_per_page'];
}
$pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
/*
* Apply post-paging filters on where and join. Only plugins that
* manipulate paging queries should use these hooks.
*/
if ( !$q['suppress_filters'] ) {
/**
* Filters the WHERE clause of the query.
*
* Specifically for manipulating paging queries.
*
* @since 1.0.0
*
* @param string $where The WHERE clause of the query.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$where = apply_filters_ref_array( 'tdb_query_where_paged', array( $where, &$this ) );
/**
* Filters the GROUP BY clause of the query.
*
* @since 1.0.0
*
* @param string $groupby The GROUP BY clause of the query.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$groupby = apply_filters_ref_array( 'tdb_query_groupby', array( $groupby, &$this ) );
/**
* Filters the JOIN clause of the query.
*
* Specifically for manipulating paging queries.
*
* @since 1.0.0
*
* @param string $join The JOIN clause of the query.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$join = apply_filters_ref_array( 'tdb_query_join_paged', array( $join, &$this ) );
/**
* Filters the ORDER BY clause of the query.
*
* @since 1.0.0
*
* @param string $orderby The ORDER BY clause of the query.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$orderby = apply_filters_ref_array( 'tdb_query_orderby', array( $orderby, &$this ) );
/**
* Filters the DISTINCT clause of the query.
*
* @since 1.0.0
*
* @param string $distinct The DISTINCT clause of the query.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$distinct = apply_filters_ref_array( 'tdb_query_distinct', array( $distinct, &$this ) );
/**
* Filters the LIMIT clause of the query.
*
* @since 1.0.0
*
* @param string $limits The LIMIT clause of the query.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$limits = apply_filters_ref_array( 'tdb_querylimits', array( $limits, &$this ) );
/**
* Filters the SELECT clause of the query.
*
* @since 1.0.0
*
* @param string $fields The SELECT clause of the query.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$fields = apply_filters_ref_array( 'tdb_query_fields', array( $fields, &$this ) );
/**
* Filters all query clauses at once, for convenience.
*
* Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT,
* fields (SELECT), and LIMITS clauses.
*
* @since 1.0.0
*
* @param array $clauses The list of clauses for the query.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$clauses = (array) apply_filters_ref_array( 'tdb_query_clauses', array( compact( $pieces ), &$this ) );
$where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
$groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : '';
$join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
$orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
$distinct = isset( $clauses[ 'distinct' ] ) ? $clauses[ 'distinct' ] : '';
$fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
$limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
}
/**
* Fires to announce the query's current selection parameters.
*
* For use by caching plugins.
*
* @since 1.0.0
*
* @param string $selection The assembled selection query.
*/
do_action( 'tdb_query_selection', $where . $groupby . $orderby . $limits . $join );
/*
* Filters again for the benefit of caching plugins.
* Regular plugins should use the hooks above.
*/
if ( !$q['suppress_filters'] ) {
/**
* Filters the WHERE clause of the query.
*
* For use by caching plugins.
*
* @since 1.0.0
*
* @param string $where The WHERE clause of the query.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$where = apply_filters_ref_array( 'tdb_query_where_request', array( $where, &$this ) );
/**
* Filters the GROUP BY clause of the query.
*
* For use by caching plugins.
*
* @since 1.0.0
*
* @param string $groupby The GROUP BY clause of the query.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$groupby = apply_filters_ref_array( 'tdb_query_groupby_request', array( $groupby, &$this ) );
/**
* Filters the JOIN clause of the query.
*
* For use by caching plugins.
*
* @since 1.0.0
*
* @param string $join The JOIN clause of the query.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$join = apply_filters_ref_array( 'tdb_query_join_request', array( $join, &$this ) );
/**
* Filters the ORDER BY clause of the query.
*
* For use by caching plugins.
*
* @since 1.0.0
*
* @param string $orderby The ORDER BY clause of the query.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$orderby = apply_filters_ref_array( 'tdb_query_orderby_request', array( $orderby, &$this ) );
/**
* Filters the DISTINCT clause of the query.
*
* For use by caching plugins.
*
* @since 1.0.0
*
* @param string $distinct The DISTINCT clause of the query.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$distinct = apply_filters_ref_array( 'tdb_query_distinct_request', array( $distinct, &$this ) );
/**
* Filters the SELECT clause of the query.
*
* For use by caching plugins.
*
* @since 1.0.0
*
* @param string $fields The SELECT clause of the query.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$fields = apply_filters_ref_array( 'tdb_query_fields_request', array( $fields, &$this ) );
/**
* Filters the LIMIT clause of the query.
*
* For use by caching plugins.
*
* @since 1.0.0
*
* @param string $limits The LIMIT clause of the query.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$limits = apply_filters_ref_array( 'tdb_query_limits_request', array( $limits, &$this ) );
/**
* Filters all query clauses at once, for convenience.
*
* For use by caching plugins.
*
* Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT,
* fields (SELECT), and LIMITS clauses.
*
* @since 1.0.0
*
* @param array $pieces The pieces of the query.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$clauses = (array) apply_filters_ref_array( 'tdb_query_clauses_request', array( compact( $pieces ), &$this ) );
$where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
$groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : '';
$join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
$orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
$distinct = isset( $clauses[ 'distinct' ] ) ? $clauses[ 'distinct' ] : '';
$fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
$limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
}
if ( ! empty($groupby) )
$groupby = 'GROUP BY ' . $groupby;
if ( !empty( $orderby ) )
$orderby = 'ORDER BY ' . $orderby;
$found_rows = '';
if ( !$q['no_found_rows'] && !empty($limits) )
$found_rows = 'SQL_CALC_FOUND_ROWS';
$this->request = $old_request = "SELECT $found_rows $distinct $fields FROM {$tdb_table->db->table_name} $join WHERE 1=1 $where $groupby $orderby $limits";
if ( !$q['suppress_filters'] ) {
/**
* Filters the completed SQL query before sending.
*
* @since 1.0.0
*
* @param string $request The complete SQL query.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$this->request = apply_filters_ref_array( 'tdb_query_request', array( $this->request, &$this ) );
}
/**
* Filters the posts array before the query takes place.
*
* Return a non-null value to bypass WordPress's default post queries.
*
* Filtering functions that require pagination information are encouraged to set
* the `found_posts` and `max_num_pages` properties of the WP_Query object,
* passed to the filter by reference. If WP_Query does not perform a database
* query, it will not have enough information to generate these values itself.
*
* @since 1.0.0
*
* @param array|null $posts Return an array of post data to short-circuit WP's query,
* or null to allow WP to run its normal queries.
* @param WP_Query $this The WP_Query instance, passed by reference.
*/
$this->results = apply_filters_ref_array( 'tdb_results_pre_query', array( null, &$this ) );
if ( 'ids' == $q['fields'] ) {
if ( null === $this->results ) {
$this->results = $wpdb->get_col( $this->request );
}
$this->results = array_map( 'intval', $this->results );
$this->result_count = count( $this->results );
$this->set_found_results( $q, $limits );
return $this->results;
}
if ( null === $this->results ) {
$split_the_query = ( $old_request == $this->request && "{$tdb_table->db->table_name}.*" == $fields && !empty( $limits ) && $q['items_per_page'] < 500 );
/**
* Filters whether to split the query.
*
* Splitting the query will cause it to fetch just the IDs of the found posts
* (and then individually fetch each post by ID), rather than fetching every
* complete row at once. One massive result vs. many small results.
*
* @since 1.0.0
*
* @param bool $split_the_query Whether or not to split the query.
* @param WP_Query $this The WP_Query instance.
*/
$split_the_query = apply_filters( 'split_the_query', $split_the_query, $this );
if ( $split_the_query ) {
// First get the IDs and then fill in the objects
$this->request = "SELECT $found_rows $distinct {$tdb_table->db->table_name}.{$tdb_table->db->primary_key} FROM {$tdb_table->db->table_name} $join WHERE 1=1 $where $groupby $orderby $limits";
/**
* Filters the Post IDs SQL request before sending.
*
* @since 1.0.0
*
* @param string $request The post ID request.
* @param WP_Query $this The WP_Query instance.
*/
$this->request = apply_filters( 'tdb_query_request_ids', $this->request, $this );
$ids = $wpdb->get_col( $this->request );
if ( $ids ) {
$this->results = $ids;
$this->set_found_results( $q, $limits );
// TODO: Add caching utility
//_prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
} else {
$this->results = array();
}
} else {
$this->results = $wpdb->get_results( $this->request );
$this->set_found_results( $q, $limits );
}
}
if ( $this->results ) {
if ( empty($q['skip_convert_objects']) ) {
/**
* Convert to objects
*
* This is applied by default for backward compatibility. It fetches results
* from **only the first table** of the query. If you're using a JOIN clause,
* set this query parameter to TRUE, to get unprocessed results.
*
* @see ./functions.php, tdb_get_object and tdb_get_object_instance
*/
$this->results = array_map( 'tdb_get_object', $this->results );
}
if ( ! $q['suppress_filters'] ) {
/**
* Filters the array of retrieved posts after they've been fetched and
* internally processed.
*
* @since 1.0.0
*
* @param array $posts The post results array.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$this->results = apply_filters_ref_array( 'tdb_query_results', array( $this->results, &$this ) );
}
$this->result_count = count( $this->results );
//if ( $q['cache_results'] )
//update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
//$this->post = reset( $this->posts );
} else {
$this->result_count = 0;
$this->results = array();
}
return $this->results;
}
public function parse_query( $query = '' ) {
if ( ! empty( $query ) ) {
$this->init();
$this->query = $this->query_vars = wp_parse_args( $query );
} elseif ( ! isset( $this->query ) ) {
$this->query = $this->query_vars;
}
$this->query_vars = $this->fill_query_vars($this->query_vars);
$qv = &$this->query_vars;
$qv['paged'] = absint($qv['paged']);
// Fairly insane upper bound for search string lengths.
if ( ! is_scalar( $qv['s'] ) || ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 ) ) {
$qv['s'] = '';
}
/**
* Fires after the main query vars have been parsed.
*
* @since 1.0.0
*
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
do_action_ref_array( 'tdb_parse_query', array( &$this ) );
}
/**
* Fills in the query variables, which do not exist within the parameter.
*
* @since 1.0.0
* @access public
*
* @param array $array Defined query variables.
* @return array Complete query variables with undefined ones filled in empty.
*/
public function fill_query_vars($array) {
$keys = array(
'paged'
, 's'
, 'sentence'
, 'fields'
);
foreach ( $keys as $key ) {
if ( ! isset($array[$key]) )
$array[$key] = '';
}
return $array;
}
/**
* Generate SQL for the WHERE clause based on passed search terms.
*
* @since 1.0.0
*
* @param array $q Query variables.
* @return string WHERE clause.
*/
protected function parse_search( &$q ) {
global $wpdb, $tdb_table;
$search = '';
$search_fields = apply_filters( "tdb_query_{$tdb_table->name}_search_fields", array() );
if( empty( $search_fields ) ) {
return $search;
}
// added slashes screw with quote grouping when done early, so done later
$q['s'] = stripslashes( $q['s'] );
if ( empty( $_GET['s'] ) )
$q['s'] = urldecode( $q['s'] );
// there are no line breaks in <input /> fields
$q['s'] = str_replace( array( "\r", "\n" ), '', $q['s'] );
$q['search_terms_count'] = 1;
if ( ! empty( $q['sentence'] ) ) {
$q['search_terms'] = array( $q['s'] );
} else {
if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) {
$q['search_terms_count'] = count( $matches[0] );
$q['search_terms'] = $this->parse_search_terms( $matches[0] );
// if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence
if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 )
$q['search_terms'] = array( $q['s'] );
} else {
$q['search_terms'] = array( $q['s'] );
}
}
$n = ! empty( $q['exact'] ) ? '' : '%';
$searchand = '';
$q['search_orderby_title'] = array();
/**
* Filters the prefix that indicates that a search term should be excluded from results.
*
* @since 1.0.0
*
* @param string $exclusion_prefix The prefix. Default '-'. Returning
* an empty value disables exclusions.
*/
$exclusion_prefix = apply_filters( 'tdb_query_search_exclusion_prefix', '-' );
foreach ( $q['search_terms'] as $term ) {
// If there is an $exclusion_prefix, terms prefixed with it should be excluded.
$exclude = $exclusion_prefix && ( $exclusion_prefix === substr( $term, 0, 1 ) );
if ( $exclude ) {
$like_op = 'NOT LIKE';
$andor_op = 'AND';
$term = substr( $term, 1 );
} else {
$like_op = 'LIKE';
$andor_op = 'OR';
}
if ( $n && ! $exclude ) {
$like = '%' . $wpdb->esc_like( $term ) . '%';
//$q['search_orderby_title'][] = $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", $like );
}
$like = $n . $wpdb->esc_like( $term ) . $n;
$search_fields_where = array();
$search_fields_args = array();
foreach( $search_fields as $search_field ) {
$search_fields_where[] = "{$tdb_table->db->table_name}.$search_field $like_op %s";
$search_fields_args[] = $like;
}
$search_where = "(" . implode( ") $andor_op (", $search_fields_where ) . ")";
$search .= $wpdb->prepare( "{$searchand}($search_where)", $search_fields_args );
//$search .= $wpdb->prepare( "{$searchand}(({$wpdb->posts}.post_title $like_op %s) $andor_op ({$wpdb->posts}.post_excerpt $like_op %s) $andor_op ({$wpdb->posts}.post_content $like_op %s))", $like, $like, $like );
$searchand = ' AND ';
}
if ( ! empty( $search ) ) {
$search = " AND ({$search}) ";
}
return $search;
}
/**
* Check if the terms are suitable for searching.
*
* Uses an array of stopwords (terms) that are excluded from the separate
* term matching when searching for posts. The list of English stopwords is
* the approximate search engines list, and is translatable.
*
* @since 1.0.0
*
* @param array $terms Terms to check.
* @return array Terms that are not stopwords.
*/
protected function parse_search_terms( $terms ) {
$strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
$checked = array();
$stopwords = $this->get_search_stopwords();
foreach ( $terms as $term ) {
// keep before/after spaces when term is for exact match
if ( preg_match( '/^".+"$/', $term ) )
$term = trim( $term, "\"'" );
else
$term = trim( $term, "\"' " );
// Avoid single A-Z and single dashes.
if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z\-]$/i', $term ) ) )
continue;
if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) )
continue;
$checked[] = $term;
}
return $checked;
}
/**
* Retrieve stopwords used when parsing search terms.
*
* @since 1.0.0
*
* @return array Stopwords.
*/
protected function get_search_stopwords() {
if ( isset( $this->stopwords ) )
return $this->stopwords;
/* translators: This is a comma-separated list of very common words that should be excluded from a search,
* like a, an, and the. These are usually called "stopwords". You should not simply translate these individual
* words into your language. Instead, look for and provide commonly accepted stopwords in your language.
*/
$words = explode( ',', _x( 'about,an,are,as,at,be,by,com,for,from,how,in,is,it,of,on,or,that,the,this,to,was,what,when,where,who,will,with,www',
'Comma-separated list of search stopwords in your language' ) );
$stopwords = array();
foreach ( $words as $word ) {
$word = trim( $word, "\r\n\t " );
if ( $word )
$stopwords[] = $word;
}
/**
* Filters stopwords used when parsing search terms.
*
* @since 1.0.0
*
* @param array $stopwords Stopwords.
*/
$this->stopwords = apply_filters( 'tdb_search_stopwords', $stopwords );
return $this->stopwords;
}
/**
* Parse an 'order' query variable and cast it to ASC or DESC as necessary.
*
* @since 1.0.0
* @access protected
*
* @param string $order The 'order' query variable.
* @return string The sanitized 'order' query variable.
*/
protected function parse_order( $order ) {
if ( ! is_string( $order ) || empty( $order ) ) {
return 'DESC';
}
if ( 'ASC' === strtoupper( $order ) ) {
return 'ASC';
} else {
return 'DESC';
}
}
/**
* Set up the amount of found posts and the number of pages (if limit clause was used)
* for the current query.
*
* @since 1.0.0
* @access private
*
* @param array $q Query variables.
* @param string $limits LIMIT clauses of the query.
*/
private function set_found_results( $q, $limits ) {
global $wpdb;
// Bail if posts is an empty array. Continue if posts is an empty string,
// null, or false to accommodate caching plugins that fill posts later.
if ( $q['no_found_rows'] || ( is_array( $this->results ) && ! $this->results ) )
return;
if ( ! empty( $limits ) ) {
/**
* Filters the query to run for retrieving the found posts.
*
* @since 1.0.0
*
* @param string $found_posts The query to run to find the found posts.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$this->found_results = $wpdb->get_var( apply_filters_ref_array( 'tdb_found_results_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
} else {
$this->found_results = count( $this->results );
}
/**
* Filters the number of found posts for the query.
*
* @since 1.0.0
*