Query.php
11.3 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
<?php
namespace AIOSEO\Plugin\Common\Sitemap;
use AIOSEO\Plugin\Common\Utils as CommonUtils;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles all complex queries for the sitemap.
*
* @since 4.0.0
*/
class Query {
/**
* Returns all eligble sitemap entries for a given post type.
*
* @since 4.0.0
*
* @param mixed $postTypes The post type(s). Either a singular string or an array of strings.
* @param array $additionalArgs Any additional arguments for the post query.
* @return array|int The post objects or the post count.
*/
public function posts( $postTypes, $additionalArgs = [] ) {
$includedPostTypes = $postTypes;
$postTypesArray = ! is_array( $postTypes ) ? [ $postTypes ] : $postTypes;
if ( is_array( $postTypes ) ) {
$includedPostTypes = implode( "', '", $postTypes );
}
if (
empty( $includedPostTypes ) ||
( 'attachment' === $includedPostTypes && 'disabled' !== aioseo()->dynamicOptions->searchAppearance->postTypes->attachment->redirectAttachmentUrls )
) {
return [];
}
// Set defaults.
$fields = '`p`.`ID`, `p`.`post_title`, `p`.`post_content`, `p`.`post_excerpt`, `p`.`post_type`, `p`.`post_password`, ';
$fields .= '`p`.`post_parent`, `p`.`post_date_gmt`, `p`.`post_modified_gmt`, `ap`.`priority`, `ap`.`frequency`';
$maxAge = '';
// Order by highest priority first (highest priority at the top),
// then by post modified date (most recently updated at the top).
$orderBy = '`ap`.`priority` DESC, `p`.`post_modified_gmt` DESC';
// Override defaults if passed as additional arg.
foreach ( $additionalArgs as $name => $value ) {
// Attachments need to be fetched with all their fields because we need to get their post parent further down the line.
$$name = esc_sql( $value );
if ( 'root' === $name && $value && 'attachment' !== $includedPostTypes ) {
$fields = '`p`.`ID`, `p`.`post_type`';
}
if ( 'count' === $name && $value ) {
$fields = 'count(`p`.`ID`) as total';
}
}
$query = aioseo()->core->db
->start( aioseo()->core->db->db->posts . ' as p', true )
->select( $fields )
->leftJoin( 'aioseo_posts as ap', '`ap`.`post_id` = `p`.`ID`' )
->where( 'p.post_status', 'attachment' === $includedPostTypes ? 'inherit' : 'publish' )
->whereRaw( "p.post_type IN ( '$includedPostTypes' )" );
$homePageId = (int) get_option( 'page_on_front' );
if ( ! is_array( $postTypes ) ) {
if ( ! aioseo()->helpers->isPostTypeNoindexed( $includedPostTypes ) ) {
$query->whereRaw( "( `ap`.`robots_noindex` IS NULL OR `ap`.`robots_default` = 1 OR `ap`.`robots_noindex` = 0 OR post_id = $homePageId )" );
} else {
$query->whereRaw( "( `ap`.`robots_default` = 0 AND `ap`.`robots_noindex` = 0 OR post_id = $homePageId )" );
}
} else {
$robotsMetaSql = [];
foreach ( $postTypes as $postType ) {
if ( ! aioseo()->helpers->isPostTypeNoindexed( $postType ) ) {
$robotsMetaSql[] = "( `p`.`post_type` = '$postType' AND ( `ap`.`robots_noindex` IS NULL OR `ap`.`robots_default` = 1 OR `ap`.`robots_noindex` = 0 OR post_id = $homePageId ) )";
} else {
$robotsMetaSql[] = "( `p`.`post_type` = '$postType' AND ( `ap`.`robots_default` = 0 AND `ap`.`robots_noindex` = 0 OR post_id = $homePageId ) )";
}
}
$query->whereRaw( '( ' . implode( ' OR ', $robotsMetaSql ) . ' )' );
}
$excludedPosts = aioseo()->sitemap->helpers->excludedPosts();
$isStaticHomepage = 'page' === get_option( 'show_on_front' );
if ( $isStaticHomepage ) {
$excludedPostIds = explode( ',', $excludedPosts );
$blogPageId = (int) get_option( 'page_for_posts' );
if ( in_array( 'page', $postTypesArray, true ) ) {
// Exclude the blog page from the pages post type.
if ( $blogPageId ) {
$query->whereRaw( "`p`.`ID` != $blogPageId" );
}
// Custom order by statement to always move the home page to the top.
if ( $homePageId ) {
$orderBy = "case when `p`.`ID` = $homePageId then 0 else 1 end, $orderBy";
}
}
// Include the blog page in the posts post type unless manually excluded.
if (
$blogPageId &&
! in_array( $blogPageId, $excludedPostIds, true ) &&
in_array( 'post', $postTypesArray, true )
) {
// We are using a database class hack to get in an OR clause to
// bypass all the other WHERE statements and just include the
// blog page ID manually.
$query->whereRaw( "1=1 OR `p`.`ID` = $blogPageId" );
// Custom order by statement to always move the blog posts page to the top.
$orderBy = "case when `p`.`ID` = $blogPageId then 0 else 1 end, $orderBy";
}
}
if ( $excludedPosts ) {
$query->whereRaw( "( `p`.`ID` NOT IN ( $excludedPosts ) OR post_id = $homePageId )" );
}
// Exclude posts assigned to excluded terms.
$excludedTerms = aioseo()->sitemap->helpers->excludedTerms();
if ( $excludedTerms ) {
$termRelationshipsTable = aioseo()->core->db->db->prefix . 'term_relationships';
$query->whereRaw("
( `p`.`ID` NOT IN
(
SELECT `tr`.`object_id`
FROM `$termRelationshipsTable` as tr
WHERE `tr`.`term_taxonomy_id` IN ( $excludedTerms )
)
)" );
}
if ( $maxAge ) {
$query->whereRaw( "( `p`.`post_date_gmt` >= '$maxAge' )" );
}
if (
'rss' === aioseo()->sitemap->type ||
(
aioseo()->sitemap->indexes &&
empty( $additionalArgs['root'] ) &&
( empty( $additionalArgs['count'] ) || ! $additionalArgs['count'] )
)
) {
$query->limit( aioseo()->sitemap->linksPerIndex, aioseo()->sitemap->offset );
}
$query->orderBy( $orderBy );
$query = $this->filterPostQuery( $query, $postTypes );
// Return the total if we are just counting the posts.
if ( ! empty( $additionalArgs['count'] ) && $additionalArgs['count'] ) {
return (int) $query->run( true, 'var' )
->result();
}
$posts = $query->run()
->result();
// Convert ID from string to int.
foreach ( $posts as $post ) {
$post->ID = (int) $post->ID;
}
return $this->filterPosts( $posts );
}
/**
* Filters the post query.
*
* @since 4.1.4
*
* @param \AIOSEO\Plugin\Common\Utils\Database $query The query.
* @param string $postType The post type.
* @return \AIOSEO\Plugin\Common\Utils\Database The filtered query.
*/
private function filterPostQuery( $query, $postType ) {
switch ( $postType ) {
case 'product':
return $this->excludeHiddenProducts( $query );
default:
break;
}
return $query;
}
/**
* Adds a condition to the query to exclude hidden WooCommerce products.
*
* @since 4.1.4
*
* @param \AIOSEO\Plugin\Common\Utils\Database $query The query.
* @return \AIOSEO\Plugin\Common\Utils\Database The filtered query.
*/
private function excludeHiddenProducts( $query ) {
if (
! aioseo()->helpers->isWooCommerceActive() ||
! apply_filters( 'aioseo_sitemap_woocommerce_exclude_hidden_products', true )
) {
return $query;
}
static $hiddenProductIds = null;
if ( null === $hiddenProductIds ) {
$tempDb = new CommonUtils\Database();
$hiddenProducts = $tempDb->start( 'term_relationships as tr' )
->select( 'tr.object_id' )
->join( 'term_taxonomy as tt', 'tr.term_taxonomy_id = tt.term_taxonomy_id' )
->join( 'terms as t', 'tt.term_id = t.term_id' )
->where( 't.name', 'exclude-from-catalog' )
->run()
->result();
if ( empty( $hiddenProducts ) ) {
return $query;
}
$hiddenProductIds = [];
foreach ( $hiddenProducts as $hiddenProduct ) {
$hiddenProductIds[] = (int) $hiddenProduct->object_id;
}
$hiddenProductIds = esc_sql( implode( ', ', $hiddenProductIds ) );
}
$query->whereRaw( "p.ID NOT IN ( $hiddenProductIds )" );
return $query;
}
/**
* Filters the queried posts.
*
* @since 4.0.0
*
* @param array $posts The posts.
* @return array $remainingPosts The remaining posts.
*/
public function filterPosts( $posts ) {
$remainingPosts = [];
foreach ( $posts as $post ) {
switch ( $post->post_type ) {
case 'attachment':
if ( ! $this->isInvalidAttachment( $post ) ) {
$remainingPosts[] = $post;
}
break;
default:
$remainingPosts[] = $post;
break;
}
}
return $remainingPosts;
}
/**
* Excludes attachments if their post parent isn't published or parent post type isn't registered anymore.
*
* @since 4.0.0
*
* @param Object $post The post.
* @return boolean Whether the attachment is invalid.
*/
private function isInvalidAttachment( $post ) {
if ( empty( $post->post_parent ) ) {
return false;
}
$parent = get_post( $post->post_parent );
if ( ! is_object( $parent ) ) {
return false;
}
if (
'publish' !== $parent->post_status ||
! in_array( $parent->post_type, get_post_types(), true ) ||
$parent->post_password
) {
return true;
}
return false;
}
/**
* Returns all eligble sitemap entries for a given taxonomy.
*
* @since 4.0.0
*
* @param string $taxonomy The taxonomy.
* @param array $additionalArgs Any additional arguments for the term query.
* @return array|int The term objects or the term count.
*/
public function terms( $taxonomy, $additionalArgs = [] ) {
// Set defaults.
$fields = 't.term_id';
$offset = aioseo()->sitemap->offset;
// Override defaults if passed as additional arg.
foreach ( $additionalArgs as $name => $value ) {
$$name = esc_sql( $value );
if ( 'root' === $name && $value ) {
$fields = 't.term_id';
}
if ( 'count' === $name && $value ) {
$fields = 'count(t.term_id) as total';
}
}
$termRelationshipsTable = aioseo()->core->db->db->prefix . 'term_relationships';
$termTaxonomyTable = aioseo()->core->db->db->prefix . 'term_taxonomy';
$query = aioseo()->core->db
->start( aioseo()->core->db->db->terms . ' as t', true )
->select( $fields )
->whereRaw( "
( `t`.`term_id` IN
(
SELECT `tt`.`term_id`
FROM `$termTaxonomyTable` as tt
WHERE `tt`.`taxonomy` = '$taxonomy'
AND `tt`.`count` > 0
)
)" );
$excludedTerms = aioseo()->sitemap->helpers->excludedTerms();
if ( $excludedTerms ) {
$query->whereRaw("
( `t`.`term_id` NOT IN
(
SELECT `tr`.`term_taxonomy_id`
FROM `$termRelationshipsTable` as tr
WHERE `tr`.`term_taxonomy_id` IN ( $excludedTerms )
)
)" );
}
if (
aioseo()->sitemap->indexes &&
empty( $additionalArgs['root'] ) &&
( empty( $additionalArgs['count'] ) || ! $additionalArgs['count'] )
) {
$query->limit( aioseo()->sitemap->linksPerIndex, $offset );
}
// Return the total if we are just counting the terms.
if ( ! empty( $additionalArgs['count'] ) && $additionalArgs['count'] ) {
return (int) $query->run( true, 'var' )
->result();
}
$terms = $query->orderBy( '`t`.`term_id` ASC' )
->run()
->result();
foreach ( $terms as $term ) {
// Convert ID from string to int.
$term->term_id = (int) $term->term_id;
// Add taxonomy name to object manually instead of querying it to prevent redundant join.
$term->taxonomy = $taxonomy;
}
return $terms;
}
/**
* Wipes all data and forces the plugin to rescan the site for images.
*
* @since 4.0.13
*
* @return void
*/
public function resetImages() {
aioseo()->core->db
->update( 'aioseo_posts' )
->set(
[
'images' => null,
'image_scan_date' => null
]
)
->run();
}
}