Query.php
6.35 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
<?php
namespace AIOSEO\Plugin\Common\Sitemap\Html;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles all queries for the HTML sitemap.
*
* @since 4.1.3
*/
class Query {
/**
* Returns all eligble sitemap entries for a given post type.
*
* @since 4.1.3
*
* @param string $postType The post type.
* @param array $attributes The attributes.
* @return array The post objects.
*/
public function posts( $postType, $attributes ) {
$fields = '`ID`, `post_title`,';
$fields .= '`post_parent`, `post_date_gmt`, `post_modified_gmt`';
$orderBy = '';
switch ( $attributes['order_by'] ) {
case 'last_updated':
$orderBy = 'post_modified_gmt';
break;
case 'alphabetical':
$orderBy = 'post_title';
break;
case 'id':
$orderBy = 'ID';
case 'publish_date':
default:
$orderBy = 'post_date_gmt';
break;
}
switch ( strtolower( $attributes['order'] ) ) {
case 'desc':
$orderBy .= ' DESC';
break;
default:
$orderBy .= ' ASC';
}
$query = aioseo()->core->db
->start( 'posts' )
->select( $fields )
->where( 'post_status', 'publish' )
->where( 'post_type', $postType );
$excludedPosts = $this->getExcludedObjects( $attributes );
if ( $excludedPosts ) {
$query->whereRaw( "( `ID` NOT IN ( $excludedPosts ) )" );
}
$posts = $query->orderBy( $orderBy )
->run()
->result();
foreach ( $posts as $post ) {
$post->ID = (int) $post->ID;
}
return $posts;
}
/**
* Returns all eligble sitemap entries for a given taxonomy.
*
* @since 4.1.3
*
* @param string $taxonomy The taxonomy name.
* @param array $attributes The attributes.
* @return array The term objects.
*/
public function terms( $taxonomy, $attributes = [] ) {
$fields = 't.term_id, t.name, tt.parent';
$termRelationshipsTable = aioseo()->core->db->db->prefix . 'term_relationships';
$termTaxonomyTable = aioseo()->core->db->db->prefix . 'term_taxonomy';
$orderBy = '';
switch ( $attributes['order_by'] ) {
case 'alphabetical':
$orderBy = 't.name';
break;
// We can only sort by date after getting the terms.
case 'id':
case 'publish_date':
case 'last_updated':
default:
$orderBy = 't.term_id';
break;
}
switch ( strtolower( $attributes['order'] ) ) {
case 'desc':
$orderBy .= ' DESC';
break;
default:
$orderBy .= ' ASC';
}
$query = aioseo()->core->db
->start( 'terms as t' )
->select( $fields )
->join( 'term_taxonomy as tt', 't.term_id = tt.term_id' )
->whereRaw( "
( `t`.`term_id` IN
(
SELECT `tt`.`term_id`
FROM `$termTaxonomyTable` as tt
WHERE `tt`.`taxonomy` = '$taxonomy'
AND `tt`.`count` > 0
)
)" );
$excludedTerms = $this->getExcludedObjects( $attributes, false );
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 )
)
)" );
}
$terms = $query->orderBy( $orderBy )
->run()
->result();
foreach ( $terms as $term ) {
$term->term_id = (int) $term->term_id;
$term->taxonomy = $taxonomy;
}
$shouldSort = false;
if ( 'last_updated' === $attributes['order_by'] ) {
$shouldSort = true;
foreach ( $terms as $term ) {
$term->timestamp = strtotime( aioseo()->sitemap->content->getTermLastModified( $term->term_id ) );
}
}
if ( 'publish_date' === $attributes['order_by'] ) {
$shouldSort = true;
foreach ( $terms as $term ) {
$term->timestamp = strtotime( $this->getTermPublishDate( $term->term_id ) );
}
}
if ( $shouldSort ) {
if ( 'asc' === strtolower( $attributes['order'] ) ) {
usort( $terms, function( $term1, $term2 ) {
return $term1->timestamp > $term2->timestamp ? 1 : 0;
} );
} else {
usort( $terms, function( $term1, $term2 ) {
return $term1->timestamp < $term2->timestamp ? 1 : 0;
} );
}
}
return $terms;
}
/**
* Returns a list of date archives that can be included.
*
* @since 4.1.3
*
* @return array The date archives.
*/
public function archives() {
$result = aioseo()->core->db
->start( 'posts', false, 'SELECT DISTINCT' )
->select( 'YEAR(post_date) AS year, MONTH(post_date) AS month' )
->where( 'post_type', 'post' )
->where( 'post_status', 'publish' )
->whereRaw( "post_password=''" )
->orderBy( '`year` DESC, `month` DESC' )
->run()
->result();
$dates = [];
foreach ( $result as $date ) {
$dates[ $date->year ][ $date->month ] = 1;
}
return $dates;
}
/**
* Returns the publish date for a given term.
* This is the publish date of the oldest post that is assigned to the term.
*
* @since 4.1.3
*
* @param int $termId The term ID.
* @return int The publish date timestamp.
*/
public function getTermPublishDate( $termId ) {
$termRelationshipsTable = aioseo()->core->db->db->prefix . 'term_relationships';
$post = aioseo()->core->db
->start( 'posts as p' )
->select( 'MIN(`p`.`post_date_gmt`) as publish_date' )
->whereRaw( "
( `p`.`ID` IN
(
SELECT `tr`.`object_id`
FROM `$termRelationshipsTable` as tr
WHERE `tr`.`term_taxonomy_id` = '$termId'
)
)" )
->run()
->result();
return ! empty( $post[0]->publish_date ) ? strtotime( $post[0]->publish_date ) : 0;
}
/**
* Returns a comma-separated string of excluded object IDs.
*
* @since 4.1.3
*
* @param array $attributes The attributes.
* @param boolean $posts Whether the objects are posts.
* @return string The excluded object IDs.
*/
private function getExcludedObjects( $attributes, $posts = true ) {
$excludedObjects = $posts
? aioseo()->sitemap->helpers->excludedPosts()
: aioseo()->sitemap->helpers->excludedTerms();
$key = $posts ? 'excluded_posts' : 'excluded_terms';
if ( ! empty( $attributes[ $key ] ) ) {
$ids = explode( ',', $excludedObjects );
$extraIds = [];
if ( is_array( $attributes[ $key ] ) ) {
$extraIds = $attributes[ $key ];
}
if ( is_string( $attributes[ $key ] ) ) {
$extraIds = array_map( 'trim', explode( ',', $attributes[ $key ] ) );
}
$ids = array_filter( array_merge( $ids, $extraIds ), 'is_numeric' );
$excludedObjects = esc_sql( implode( ', ', $ids ) );
}
return $excludedObjects;
}
}