@@ -24,11 +24,11 @@ function () {
24
24
register_rest_route (
25
25
$ my_namespace ,
26
26
$ my_endpoint ,
27
- array (
27
+ [
28
28
'methods ' => 'GET ' ,
29
29
'callback ' => 'alm_get_posts ' ,
30
30
'permission_callback ' => '__return_true ' ,
31
- )
31
+ ]
32
32
);
33
33
}
34
34
);
@@ -43,218 +43,42 @@ function () {
43
43
* @since 1.0
44
44
*/
45
45
function alm_get_posts ( $ data ) {
46
+ $ response = [];
46
47
47
- $ response = array ();
48
48
// Set Defaults.
49
+ $ args = [];
50
+ $ page = $ data ['page ' ];
49
51
50
- $ page = $ data ['page ' ]; // the current page from ALM
51
- $ posts_per_page = ! empty ( $ data ['posts_per_page ' ] ) ? $ data ['posts_per_page ' ] : 5 ;
52
- $ post_status = ! empty ( $ data ['post_status ' ] ) ? $ data ['post_status ' ] : 'publish ' ;
53
-
54
- /*
55
- Set Query Arguments
56
- */
57
-
58
- $ args = array (
59
- 'post_type ' => $ data ['post_type ' ],
60
- 'posts_per_page ' => $ posts_per_page ,
61
- 'offset ' => $ data ['offset ' ] + $ page * $ posts_per_page ,
62
- 'order ' => $ data ['order ' ],
63
- 'orderby ' => $ data ['orderby ' ],
64
- 'post_status ' => $ post_status ,
65
- 'ignore_sticky_posts ' => false ,
66
- );
67
-
68
- // Post Format - we can combine these queries
69
- if ( ! empty ( $ data ['post_format ' ] ) || ! empty ( $ data ['taxonomy ' ] ) ) {
70
- $ tax_query_total = count ( explode ( ': ' , $ data ['taxonomy ' ] ) ); // Total $taxonomy objects
71
- $ taxonomy = explode ( ': ' , $ data ['taxonomy ' ] ); // convert to array
72
- $ taxonomy_terms = explode ( ': ' , $ data ['taxonomy_terms ' ] ); // convert to array
73
- $ taxonomy_operator = explode ( ': ' , $ data ['taxonomy_operator ' ] ); // convert to array
74
- $ taxonomy_relation = ! empty ( $ data ['taxonomy_relation ' ] ) ? $ data ['taxonomy_relation ' ] : 'AND ' ;
75
- $ post_format = ! empty ( $ data ['post_format ' ] ) ? $ data ['post_format ' ] : '' ;
76
- if ( empty ( $ taxonomy ) ) { // Post Format only
77
- $ args ['tax_query ' ] = array (
78
- alm_get_post_format ( $ post_format ),
79
- );
80
- } else { // Taxonomy and possibly Post Formats
81
- if ( $ tax_query_total === 1 ) {
82
- $ args ['tax_query ' ] = array (
83
- 'relation ' => $ taxonomy_relation ,
84
- alm_get_post_format ( $ post_format ),
85
- alm_get_taxonomy_query ( $ taxonomy [0 ], $ taxonomy_terms [0 ], $ taxonomy_operator [0 ] ),
86
- );
87
- }
88
- if ( $ tax_query_total === 2 ) {
89
- $ args ['tax_query ' ] = array (
90
- 'relation ' => $ taxonomy_relation ,
91
- alm_get_post_format ( $ post_format ),
92
- alm_get_taxonomy_query ( $ taxonomy [0 ], $ taxonomy_terms [0 ], $ taxonomy_operator [0 ] ),
93
- alm_get_taxonomy_query ( $ taxonomy [1 ], $ taxonomy_terms [1 ], $ taxonomy_operator [1 ] ),
94
- );
95
- }
96
- if ( $ tax_query_total === 3 ) {
97
- $ args ['tax_query ' ] = array (
98
- 'relation ' => $ taxonomy_relation ,
99
- alm_get_post_format ( $ post_format ),
100
- alm_get_taxonomy_query ( $ taxonomy [0 ], $ taxonomy_terms [0 ], $ taxonomy_operator [0 ] ),
101
- alm_get_taxonomy_query ( $ taxonomy [1 ], $ taxonomy_terms [1 ], $ taxonomy_operator [1 ] ),
102
- alm_get_taxonomy_query ( $ taxonomy [2 ], $ taxonomy_terms [2 ], $ taxonomy_operator [2 ] ),
103
- );
104
- }
105
- }
106
- }
107
-
108
- // Category
109
- if ( ! empty ( $ data ['category ' ] ) ) {
110
- $ args ['category_name ' ] = $ data ['category ' ];
111
- }
112
-
113
- // Category Not In
114
- if ( ! empty ( $ data ['category__not_in ' ] ) ) {
115
- $ exclude_cats = explode ( ', ' , $ data ['category__not_in ' ] );
116
- $ args ['category__not_in ' ] = $ exclude_cats ;
117
- }
118
-
119
- // Tag
120
- if ( ! empty ( $ data ['tag ' ] ) ) {
121
- $ args ['tag ' ] = $ data ['tag ' ];
122
- }
123
-
124
- // Tag Not In
125
- if ( ! empty ( $ data ['tag__not_in ' ] ) ) {
126
- $ exclude_tags = explode ( ', ' , $ data ['tag__not_in ' ] );
127
- $ args ['tag__not_in ' ] = $ exclude_tags ;
128
- }
129
-
130
- // Date Query
131
- if ( ! empty ( $ data ['year ' ] ) ) {
132
- $ args ['year ' ] = $ data ['year ' ];
133
- }
134
- if ( ! empty ( $ data ['month ' ] ) ) {
135
- $ args ['monthnum ' ] = $ data ['month ' ];
136
- }
137
- if ( ! empty ( $ data ['day ' ] ) ) {
138
- $ args ['day ' ] = $ data ['day ' ];
139
- }
140
-
141
- // Meta Query
142
- $ meta_key = ( isset ( $ data ['meta_key ' ] ) ) ? $ data ['meta_key ' ] : '' ;
143
- $ meta_value = ( isset ( $ data ['meta_value ' ] ) ) ? $ data ['meta_value ' ] : '' ;
144
- $ meta_compare = ! empty ( $ data ['meta_compare ' ] ) ? $ data ['meta_compare ' ] : 'IN ' ;
145
- if ( $ meta_compare === 'lessthan ' ) {
146
- $ meta_compare = '< ' ; // do_shortcode fix (shortcode was rendering as HTML)
147
- }
148
- if ( $ meta_compare === 'lessthanequalto ' ) {
149
- $ meta_compare = '<= ' ; // do_shortcode fix (shortcode was rendering as HTML)
150
- }
151
- $ meta_relation = ! empty ( $ data ['meta_relation ' ] ) ? $ data ['meta_relation ' ] : 'AND ' ;
152
- $ meta_type = ! empty ( $ data ['meta_type ' ] ) ? $ data ['meta_type ' ] : 'CHAR ' ;
153
-
154
- if ( ! empty ( $ meta_key ) && ! empty ( $ meta_value ) || ! empty ( $ meta_key ) && $ meta_compare !== 'IN ' ) {
155
- $ meta_query_total = count ( explode ( ': ' , $ meta_key ) ); // Total meta_query objects
156
- $ meta_keys = explode ( ': ' , $ meta_key ); // convert to array
157
- $ meta_value = explode ( ': ' , $ meta_value ); // convert to array
158
- $ meta_compare = explode ( ': ' , $ meta_compare ); // convert to array
159
- $ meta_type = explode ( ': ' , $ meta_type ); // convert to array
160
- if ( $ meta_query_total == 1 ) {
161
- $ args ['meta_query ' ] = array (
162
- alm_get_meta_query ( $ meta_keys [0 ], $ meta_value [0 ], $ meta_compare [0 ], $ meta_type [0 ] ),
163
- );
164
- }
165
- if ( $ meta_query_total == 2 ) {
166
- $ args ['meta_query ' ] = array (
167
- 'relation ' => $ meta_relation ,
168
- alm_get_meta_query ( $ meta_keys [0 ], $ meta_value [0 ], $ meta_compare [0 ], $ meta_type [0 ] ),
169
- alm_get_meta_query ( $ meta_keys [1 ], $ meta_value [1 ], $ meta_compare [1 ], $ meta_type [1 ] ),
170
- );
171
- }
172
- if ( $ meta_query_total == 3 ) {
173
- $ args ['meta_query ' ] = array (
174
- 'relation ' => $ meta_relation ,
175
- alm_get_meta_query ( $ meta_keys [0 ], $ meta_value [0 ], $ meta_compare [0 ], $ meta_type [0 ] ),
176
- alm_get_meta_query ( $ meta_keys [1 ], $ meta_value [1 ], $ meta_compare [1 ], $ meta_type [1 ] ),
177
- alm_get_meta_query ( $ meta_keys [2 ], $ meta_value [2 ], $ meta_compare [2 ], $ meta_type [2 ] ),
178
- );
179
- }
180
- if ( $ meta_query_total == 4 ) {
181
- $ args ['meta_query ' ] = array (
182
- 'relation ' => $ meta_relation ,
183
- alm_get_meta_query ( $ meta_keys [0 ], $ meta_value [0 ], $ meta_compare [0 ], $ meta_type [0 ] ),
184
- alm_get_meta_query ( $ meta_keys [1 ], $ meta_value [1 ], $ meta_compare [1 ], $ meta_type [1 ] ),
185
- alm_get_meta_query ( $ meta_keys [2 ], $ meta_value [2 ], $ meta_compare [2 ], $ meta_type [2 ] ),
186
- alm_get_meta_query ( $ meta_keys [3 ], $ meta_value [3 ], $ meta_compare [3 ], $ meta_type [3 ] ),
187
- );
188
- }
189
- }
190
-
191
- // Meta_key [ordering by meta value]
192
- if ( ! empty ( $ meta_key ) ) {
193
- if ( strpos ( $ data ['orderby ' ], 'meta_value ' ) !== false ) {
194
- // Order by meta_key, if $data['orderby'] is set to meta_value{_num}
195
- $ meta_key_single = explode ( ': ' , $ meta_key );
196
- $ args ['meta_key ' ] = $ meta_key_single [0 ];
197
- }
198
- }
199
-
200
- // Author
201
- if ( ! empty ( $ data ['author ' ] ) ) {
202
- $ args ['author ' ] = $ data ['author ' ];
203
- }
204
-
205
- // Include posts
206
- if ( ! empty ( $ data ['post__in ' ] ) ) {
207
- $ post__in = explode ( ', ' , $ data ['post__in ' ] );
208
- $ args ['post__in ' ] = $ post__in ;
52
+ if ( method_exists ( 'ALM_QUERY_ARGS ' , 'alm_build_queryargs ' ) ) {
53
+ /**
54
+ * Pluck query args from core ALM class.
55
+ *
56
+ * @see ajax-load-more/core/classes/class-queryargs.php
57
+ */
58
+ $ args = ALM_QUERY_ARGS ::alm_build_queryargs ( $ data );
59
+ $ args ['offset ' ] = $ args ['offset ' ] + $ page * $ args ['posts_per_page ' ];
209
60
}
210
61
211
- // Exclude posts
212
- if ( ! empty ( $ data ['post__not_in ' ] ) ) {
213
- $ post__not_in = explode ( ', ' , $ data ['post__not_in ' ] );
214
- $ args ['post__not_in ' ] = $ post__not_in ;
215
- }
216
-
217
- // Custom Args
218
- if ( ! empty ( $ data ['custom_args ' ] ) ) {
219
- $ custom_args_array = explode ( '; ' , $ data ['custom_args ' ] ); // Split the $custom_args at ','
220
- foreach ( $ custom_args_array as $ argument ) { // Loop each $argument
221
- $ argument = preg_replace ( '/\s+/ ' , '' , $ argument ); // Remove all whitespace
222
- $ argument = explode ( ': ' , $ argument ); // Split the $argument at ':'
223
- $ argument_arr = explode ( ', ' , $ argument [1 ] ); // explode $argument[1] at ','
224
- if ( sizeof ( $ argument_arr ) > 1 ) {
225
- $ args [ $ argument [0 ] ] = $ argument_arr ;
226
- } else {
227
- $ args [ $ argument [0 ] ] = $ argument [1 ];
228
- }
229
- }
230
- }
231
-
232
- // Search Term
233
- if ( ! empty ( $ data ['search ' ] ) ) {
234
- $ args ['s ' ] = $ data ['search ' ];
235
- }
236
-
237
- // Language
238
- if ( ! empty ( $ data ['lang ' ] ) ) {
239
- $ args ['lang ' ] = $ data ['lang ' ];
240
- }
241
-
242
- // Run Query
62
+ // Run Query.
243
63
$ posts = new WP_Query ( $ args );
244
64
245
- // ALM Template vars [https://connekthq.com/plugins/ajax-load-more/docs/variables/]
246
- $ alm_item = $ page * $ posts_per_page ;
65
+ /**
66
+ * ALM Template vars.
67
+ *
68
+ * @see https://connekthq.com/plugins/ajax-load-more/docs/variables/
69
+ */
70
+ $ alm_item = $ args ['page ' ] * $ posts_per_page ;
247
71
$ alm_found_posts = $ posts ->found_posts ;
248
72
$ alm_post_count = $ posts ->post_count ;
249
73
$ alm_current = 0 ;
250
- $ data = array ();
251
74
75
+ $ data = [];
252
76
while ( $ posts ->have_posts () ) :
253
77
$ posts ->the_post ();
254
78
255
79
$ alm_current ++;
256
80
257
- // Get post thumbnail
81
+ // Get post thumbnail.
258
82
$ thumbnail_id = get_post_thumbnail_id ();
259
83
$ thumbnail = '' ;
260
84
$ alt = '' ;
@@ -264,9 +88,9 @@ function alm_get_posts( $data ) {
264
88
$ alt = get_post_meta ( $ thumbnail_id , '_wp_attachment_image_alt ' , true );
265
89
}
266
90
267
- // Build $data JSON object
268
- $ data [] = array (
269
- 'alm_page ' => $ page + 1 ,
91
+ // Build $data JSON object.
92
+ $ data [] = [
93
+ 'alm_page ' => $ args [ ' page ' ] + 1 ,
270
94
'alm_item ' => ( $ alm_item ++ ) + 1 ,
271
95
'alm_current ' => $ alm_current ,
272
96
'alm_found_posts ' => $ alm_found_posts ,
@@ -276,28 +100,31 @@ function alm_get_posts( $data ) {
276
100
'post_excerpt ' => get_the_excerpt (),
277
101
'thumbnail ' => $ thumbnail ,
278
102
'thumbnail_alt ' => $ alt ,
279
- ) ;
103
+ ] ;
280
104
281
- // Content [Apply shortcode filter for loaded shortcodes]
282
- // $content = get_the_content();
283
- // $data['post_content'] = apply_filters('the_content', $content);
105
+ /**
106
+ * Content [Apply shortcode filter for loaded shortcodes].
107
+ * $content = get_the_content();.
108
+ * $data['post_content'] = apply_filters('the_content', $content);.
109
+ */
284
110
285
- endwhile ;
286
- wp_reset_query ();
111
+ endwhile ;
112
+ wp_reset_query (); // phpcs:ignore
287
113
288
- if ( empty ( $ data ) ) { // Empty results
114
+ if ( empty ( $ data ) ) {
115
+ // Empty results.
289
116
$ data = null ;
290
117
$ alm_post_count = null ;
291
118
$ alm_found_posts = null ;
292
119
}
293
120
294
- $ return = array (
121
+ $ return = [
295
122
'html ' => $ data ,
296
- 'meta ' => array (
123
+ 'meta ' => [
297
124
'postcount ' => $ alm_post_count ,
298
125
'totalposts ' => $ alm_found_posts ,
299
- ) ,
300
- ) ;
126
+ ] ,
127
+ ] ;
301
128
302
129
wp_send_json ( $ return );
303
130
}
0 commit comments