File.php
8.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
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
<?php
namespace AIOSEO\Plugin\Common\Sitemap;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles the static sitemap.
*
* @since 4.0.0
*/
class File {
/**
* Whether the static files have already been updated during the current request.
*
* We keep track of this so that setting changes to do not trigger the regeneration multiple times.
*
* @since 4.0.0
*
* @var boolean
*/
private static $isUpdated = false;
/**
* Generates the static sitemap files.
*
* @since 4.0.0
*
* @param boolean $force Whether or not to force it through.
* @return void
*/
public function generate( $force = false ) {
foreach ( aioseo()->addons->getLoadedAddons() as $loadedAddon ) {
if ( ! empty( $loadedAddon->file ) && method_exists( $loadedAddon->file, 'generate' ) ) {
$loadedAddon->file->generate( $force );
}
}
// Exit if static sitemap generation isn't enabled.
if (
! $force &&
(
self::$isUpdated ||
! aioseo()->options->sitemap->general->enable ||
! aioseo()->options->sitemap->general->advancedSettings->enable ||
! in_array( 'staticSitemap', aioseo()->internalOptions->internal->deprecatedOptions, true ) ||
aioseo()->options->deprecated->sitemap->general->advancedSettings->dynamic
)
) {
return;
}
$files = [];
self::$isUpdated = true;
// We need to set these values here as setContext() doesn't run.
// Subsequently, we need to manually reset the index name below for each query we run.
// Also, since we need to chunck the entries manually, we cannot limit any queries and need to reset the amount of allowed URLs per index.
aioseo()->sitemap->offset = 0;
aioseo()->sitemap->type = 'general';
$sitemapName = aioseo()->sitemap->helpers->filename();
aioseo()->sitemap->indexes = aioseo()->options->sitemap->general->indexes;
aioseo()->sitemap->linksPerIndex = PHP_INT_MAX;
aioseo()->sitemap->isStatic = true;
$additionalPages = [];
if ( aioseo()->options->sitemap->general->additionalPages->enable ) {
foreach ( aioseo()->options->sitemap->general->additionalPages->pages as $additionalPage ) {
$additionalPage = json_decode( $additionalPage );
if ( empty( $additionalPage->url ) ) {
continue;
}
$additionalPages[] = $additionalPage;
}
}
$postTypes = aioseo()->sitemap->helpers->includedPostTypes();
$additionalPages = apply_filters( 'aioseo_sitemap_additional_pages', $additionalPages );
if (
'posts' === get_option( 'show_on_front' ) ||
count( $additionalPages ) ||
! in_array( 'page', $postTypes, true )
) {
$entries = aioseo()->sitemap->content->addl( false );
$filename = "addl-$sitemapName.xml";
$files[ $filename ] = [
'total' => count( $entries ),
'entries' => $entries
];
}
if (
aioseo()->sitemap->helpers->lastModifiedPost() &&
aioseo()->options->sitemap->general->author
) {
$entries = aioseo()->sitemap->content->author();
$filename = "author-$sitemapName.xml";
$files[ $filename ] = [
'total' => count( $entries ),
'entries' => $entries
];
}
if (
aioseo()->sitemap->helpers->lastModifiedPost() &&
aioseo()->options->sitemap->general->date
) {
$entries = aioseo()->sitemap->content->date();
$filename = "date-$sitemapName.xml";
$files[ $filename ] = [
'total' => count( $entries ),
'entries' => $entries
];
}
$postTypes = aioseo()->sitemap->helpers->includedPostTypes();
if ( $postTypes ) {
foreach ( $postTypes as $postType ) {
aioseo()->sitemap->indexName = $postType;
$posts = aioseo()->sitemap->content->posts( $postType );
if ( ! $posts ) {
continue;
}
$total = aioseo()->sitemap->query->posts( $postType, [ 'count' => true ] );
// We need to temporarily reset the linksPerIndex count here so that we can properly chunk.
aioseo()->sitemap->linksPerIndex = aioseo()->options->sitemap->general->linksPerIndex;
$chunks = aioseo()->sitemap->helpers->chunkEntries( $posts );
aioseo()->sitemap->linksPerIndex = PHP_INT_MAX;
if ( 1 === count( $chunks ) ) {
$filename = "$postType-$sitemapName.xml";
$files[ $filename ] = [
'total' => $total,
'entries' => $chunks[0]
];
} else {
for ( $i = 1; $i <= count( $chunks ); $i++ ) {
$filename = "$postType-$sitemapName$i.xml";
$files[ $filename ] = [
'total' => $total,
'entries' => $chunks[ $i - 1 ]
];
}
}
}
}
$taxonomies = aioseo()->sitemap->helpers->includedTaxonomies();
if ( $taxonomies ) {
foreach ( $taxonomies as $taxonomy ) {
aioseo()->sitemap->indexName = $taxonomy;
$terms = aioseo()->sitemap->content->terms( $taxonomy );
if ( ! $terms ) {
continue;
}
$total = aioseo()->sitemap->query->terms( $taxonomy, [ 'count' => true ] );
// We need to temporarily reset the linksPerIndex count here so that we can properly chunk.
aioseo()->sitemap->linksPerIndex = aioseo()->options->sitemap->general->linksPerIndex;
$chunks = aioseo()->sitemap->helpers->chunkEntries( $terms );
aioseo()->sitemap->linksPerIndex = PHP_INT_MAX;
if ( 1 === count( $chunks ) ) {
$filename = "$taxonomy-$sitemapName.xml";
$files[ $filename ] = [
'total' => $total,
'entries' => $chunks[0]
];
} else {
for ( $i = 1; $i <= count( $chunks ); $i++ ) {
$filename = "$taxonomy-$sitemapName$i.xml";
$files[ $filename ] = [
'total' => $total,
'entries' => $chunks[ $i - 1 ]
];
}
}
}
}
$this->writeSitemaps( $files );
}
/**
* Writes all sitemap files.
*
* @since 4.0.0
*
* @param array $files The sitemap files.
* @return void
*/
public function writeSitemaps( $files ) {
$sitemapName = aioseo()->sitemap->helpers->filename();
if ( aioseo()->sitemap->indexes ) {
$indexes = [];
foreach ( $files as $filename => $data ) {
if ( empty( $data['entries'] ) ) {
continue;
}
$indexes[] = [
'loc' => trailingslashit( home_url() ) . $filename,
'lastmod' => array_values( $data['entries'] )[0]['lastmod'],
'count' => count( $data['entries'] )
];
}
$files[ "$sitemapName.xml" ] = [
'total' => 0,
'entries' => $indexes,
];
foreach ( $files as $filename => $data ) {
$this->writeSitemap( $filename, $data['entries'], $data['total'] );
}
return;
}
$content = [];
foreach ( $files as $filename => $data ) {
foreach ( $data['entries'] as $entry ) {
$content[] = $entry;
}
}
$this->writeSitemap( "$sitemapName.xml", $content, count( $content ) );
}
/**
* Writes a given sitemap file to the root dir.
*
* Helper function for writeSitemaps().
*
* @since 4.0.0
*
* @param string $filename The name of the file.
* @param array $entries The sitemap entries for the file.
* @return void
*/
protected function writeSitemap( $filename, $entries, $total = 0 ) {
$sitemapName = aioseo()->sitemap->helpers->filename();
aioseo()->sitemap->indexName = $filename;
if ( "$sitemapName.xml" === $filename && aioseo()->sitemap->indexes ) {
// Set index name to root so that we use the right output template.
aioseo()->sitemap->indexName = 'root';
}
aioseo()->sitemap->xsl->saveXslData( $filename, $entries, $total );
ob_start();
aioseo()->sitemap->output->output( $entries, $total );
foreach ( aioseo()->addons->getLoadedAddons() as $instance ) {
if ( ! empty( $instance->output ) && method_exists( $instance->output, 'output' ) ) {
$instance->output->output( $entries, $total );
}
}
$content = ob_get_clean();
$fs = aioseo()->core->fs;
$file = ABSPATH . sanitize_file_name( $filename );
$fileExists = $fs->exists( $file );
if ( ! $fileExists || $fs->isWritable( $file ) ) {
$fs->putContents( $file, $content );
}
}
/**
* Return an array of sitemap files.
*
* @since 4.0.0
*
* @return array An array of files.
*/
public function files() {
require_once ABSPATH . 'wp-admin/includes/file.php';
$files = list_files( get_home_path(), 1 );
if ( ! count( $files ) ) {
return [];
}
$sitemapFiles = [];
foreach ( $files as $filename ) {
if ( preg_match( '#.*sitemap.*#', $filename ) ) {
$sitemapFiles[] = $filename;
}
}
return $sitemapFiles;
}
}