Rewrite.php
3.68 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
<?php
namespace AIOSEO\Plugin\Common\Sitemap;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Handles our sitemap rewrite rules.
*
* @since 4.0.0
*/
class Rewrite {
/**
* Returns our sitemap rewrite rules.
*
* @since 4.0.0
*
* @return array $rules The compiled array of rewrite rules, keyed by their regex pattern.
*/
private static function getRewriteRules() {
$rules = [];
foreach ( aioseo()->sitemap->addons as $classes ) {
if ( ! empty( $classes['rewrite'] ) ) {
$rules += $classes['rewrite']->get();
}
}
if ( aioseo()->options->sitemap->general->enable ) {
$filename = aioseo()->sitemap->helpers->filename( 'general' );
$indexesEnabled = aioseo()->options->sitemap->general->indexes();
if ( $filename && 'sitemap' !== $filename ) {
// Check if the user has a custom filename from the v3 migration.
$rules += [
"$filename.xml" => 'index.php?aiosp_sitemap_path=root',
];
if ( $indexesEnabled ) {
$rules += [
"(.+)-$filename.xml" => 'index.php?aiosp_sitemap_path=$matches[1]',
"(.+)-$filename(\d+).xml" => 'index.php?aiosp_sitemap_path=$matches[1]&aiosp_sitemap_page=$matches[2]'
];
}
}
$rules += [
'sitemap.xml' => 'index.php?aiosp_sitemap_path=root',
];
if ( $indexesEnabled ) {
$rules += [
'(.+)-sitemap.xml' => 'index.php?aiosp_sitemap_path=$matches[1]',
'(.+)-sitemap(\d+).xml' => 'index.php?aiosp_sitemap_path=$matches[1]&aiosp_sitemap_page=$matches[2]'
];
}
}
if ( aioseo()->options->sitemap->rss->enable ) {
$rules += [
'sitemap.rss' => 'index.php?aiosp_sitemap_path=rss',
'sitemap.latest.rss' => 'index.php?aiosp_sitemap_path=rss'
];
}
return $rules;
}
/**
* Updates the rewrite rules.
*
* @since 4.0.0
*
* @param boolean $forceUpdate Force an update of the rewrite rules.
* @return void
*/
public static function updateRewriteRules( $forceUpdate = false ) {
$existingRules = (array) get_option( 'rewrite_rules', [] );
if ( empty( $existingRules ) ) {
// We need to flush the rules after the Search Appearance options in Yoast SEO are updated; otherwise the regular rewrite rules are missing.
return flush_rewrite_rules( false );
}
// Don't update rewrite rules unless a sitemap is requested or it is forced.
if (
! $forceUpdate &&
(
is_admin() ||
! isset( $_SERVER['REQUEST_URI'] ) ||
// Don't escape the subject here - this will break the pattern.
! preg_match( '/(\.xml|\.xml\.gz|\.rss)$/', wp_unslash( $_SERVER['REQUEST_URI'] ), $matches ) // phpcs:ignore HM.Security.ValidatedSanitizedInput.InputNotSanitized
)
) {
return;
}
// Remove all our existing rules before we update the option.
$existingRules = self::removeRewriteRules( $existingRules );
// Push our own sitemap rewrite rules to the top of the list.
$newRules = self::getRewriteRules() + $existingRules;
update_option( 'rewrite_rules', $newRules );
}
/**
* Removes our sitemap rewrite rules.
*
* @since 4.0.12
*
* @param array $rules The rewrite rules.
* @param boolean $update Whether the rewrite rules should be updated.
* @return array $rules The rewrite rules without ours.
*/
public static function removeRewriteRules( $rules = [], $update = false ) {
$rules = ! empty( $rules ) ? $rules : get_option( 'rewrite_rules' );
if ( empty( $rules ) ) {
return;
}
// Remove all our existing rules before we update the option.
foreach ( $rules as $k => $v ) {
if ( preg_match( '/.*aiosp_.*/', aioseo()->helpers->escapeRegex( $v ) ) ) {
unset( $rules[ $k ] );
}
}
if ( $update ) {
update_option( 'rewrite_rules', $rules );
}
return $rules;
}
}