-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathxml-sitemap.php
330 lines (280 loc) · 8.72 KB
/
xml-sitemap.php
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
<?php
/**
* Plugin Name: XML Sitemap & Google News
* Plugin URI: https://status301.net/wordpress-plugins/xml-sitemap-feed/
* Description: Feed the hungry spiders in compliance with the XML Sitemap and Google News protocols. Happy with the results? Please leave me a <strong><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=ravanhagen%40gmail%2ecom&item_name=XML%20Sitemap%20Feed">tip</a></strong> for continued development and support. Thanks :)
* Version: 5.4.9
* Text Domain: xml-sitemap-feed
* Requires at least: 4.4
* Requires PHP: 5.6
* Author: RavanH
* Author URI: https://status301.net/
*
* @package XML Sitemap & Google News
*/
define( 'XMLSF_VERSION', '5.4.9' );
/**
* Copyright 2024 RavanH
* https://status301.net/
* mailto: [email protected]
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
/**
* --------------------
* AVAILABLE HOOKS
* --------------------
*
* Documented on https://premium.status301.com/knowledge-base/xml-sitemap-google-news/action-and-filter-hooks/
*
* ---------------------
* AVAILABLE FUNCTIONS
* ---------------------
*
* Conditional tags https://premium.status301.com/knowledge-base/xml-sitemap-google-news/conditional-tags/
*
* Feel free to request, suggest or submit more :)
*/
defined( 'WPINC' ) || die;
define( 'XMLSF_DIR', __DIR__ );
define( 'XMLSF_BASENAME', plugin_basename( __FILE__ ) );
// Main plugin init.
add_action( 'init', 'xmlsf_init', 9 );
register_activation_hook( __FILE__, 'xmlsf_activate' );
register_deactivation_hook( __FILE__, 'xmlsf_deactivate' );
/**
* Plugin initialization
*
* @since 1.0
* @return void
*/
function xmlsf_init() {
// Prepare hooks for debugging.
WP_DEBUG && require_once XMLSF_DIR . '/inc/functions-debugging.php';
// Add robots.txt filter.
add_filter( 'robots_txt', 'xmlsf_robots_txt', 0 );
// If XML Sitemaps Manager is installed, remove its init and admin_init hooks.
if ( function_exists( 'xmlsm_init' ) ) {
remove_action( 'init', 'xmlsm_init', 9 );
remove_action( 'admin_init', 'xmlsm_admin_init' );
}
// Upgrade/install, maybe...
$db_version = get_option( 'xmlsf_version', 0 );
if ( ! version_compare( XMLSF_VERSION, $db_version, '=' ) ) {
require_once XMLSF_DIR . '/upgrade.php';
}
// If sitemaps enabled, do our thing. Otherwise disable core.
if ( xmlsf_sitemaps_enabled() ) {
// Shared functions.
require_once XMLSF_DIR . '/inc/functions.php';
$sitemaps = (array) get_option( 'xmlsf_sitemaps', array() );
// Google News sitemap?
if ( ! empty( $sitemaps['sitemap-news'] ) ) {
global $xmlsf_sitemap_news;
require XMLSF_DIR . '/inc/class-xmlsf-sitemap-news.php';
$xmlsf_sitemap_news = new XMLSF_Sitemap_News();
}
// XML Sitemap?
if ( ! empty( $sitemaps['sitemap'] ) ) {
global $xmlsf_sitemap;
require XMLSF_DIR . '/inc/class-xmlsf-sitemap.php';
require XMLSF_DIR . '/inc/functions-sitemap.php';
if ( xmlsf_uses_core_server() ) {
// Extend core sitemap.
require XMLSF_DIR . '/inc/class-xmlsf-sitemap-core.php';
$xmlsf_sitemap = new XMLSF_Sitemap_Core();
} else {
// Replace core sitemap.
remove_action( 'init', 'wp_sitemaps_get_server' );
require XMLSF_DIR . '/inc/class-xmlsf-sitemap-plugin.php';
$xmlsf_sitemap = new XMLSF_Sitemap_Plugin();
}
} else {
// Disable core sitemap.
add_filter( 'wp_sitemaps_enabled', '__return_false' );
}
// Include and instantiate main class.
xmlsf();
} else {
add_filter( 'wp_sitemaps_enabled', '__return_false' );
}
if ( is_admin() ) {
xmlsf_admin();
}
}
/**
* Plugin activation
*
* @since 5.0
* @return void
*/
function xmlsf_activate() {
// Flush rewrite rules on next init.
delete_option( 'rewrite_rules' );
}
/**
* Plugin de-activation
*
* @since 5.0
* @return void
*/
function xmlsf_deactivate() {
// Clear all cache metadata.
if ( ! function_exists( 'xmlsf_clear_metacache' ) ) {
// Needed for wp-cli.
include_once XMLSF_DIR . '/inc/functions-sitemap.php';
}
xmlsf_clear_metacache();
// Remove old rules.
// TODO but how? remove_rewrite_rule() does not exist yet :/
// Re-add core rules.
function_exists( 'wp_sitemaps_get_server' ) && wp_sitemaps_get_server();
// Then flush.
flush_rewrite_rules( false );
}
/**
* Get instantiated sitemap class
*
* @since 5.0
*
* @global XMLSitemapFeed $xmlsf
* @return XMLSitemapFeed object by reference
*/
function &xmlsf() {
global $xmlsf;
if ( ! isset( $xmlsf ) ) {
if ( ! class_exists( 'XMLSitemapFeed' ) ) {
require_once XMLSF_DIR . '/inc/class-xmlsitemapfeed.php';
}
$xmlsf = new XMLSitemapFeed();
}
return $xmlsf;
}
/**
* Get instantiated sitemap admin class
*
* @since 5.4
*
* @global XMLSF_Admin $xmlsf_admin
* @return XMLSF_Admin object by reference
*/
function &xmlsf_admin() {
global $xmlsf_admin;
if ( ! isset( $xmlsf_admin ) ) {
if ( ! class_exists( 'XMLSF_Admin' ) ) {
require XMLSF_DIR . '/inc/class-xmlsf-admin.php';
}
$xmlsf_admin = new XMLSF_Admin();
}
return $xmlsf_admin;
}
/**
* Filter robots.txt rules
*
* @param string $output Default robots.txt content.
*
* @return string
*/
function xmlsf_robots_txt( $output ) {
// CUSTOM ROBOTS.
$robots_custom = get_option( 'xmlsf_robots' );
$output .= $robots_custom ? $robots_custom . PHP_EOL : '';
// SITEMAPS.
$output .= PHP_EOL . '# XML Sitemap & Google News version ' . XMLSF_VERSION . ' - https://status301.net/wordpress-plugins/xml-sitemap-feed/' . PHP_EOL;
if ( 1 !== (int) get_option( 'blog_public' ) ) {
$output .= '# XML Sitemaps are disabled because of this site\'s privacy settings.' . PHP_EOL;
} elseif ( ! xmlsf_sitemaps_enabled() ) {
$output .= '# No XML Sitemaps are enabled.' . PHP_EOL;
} else {
xmlsf_uses_core_server() || xmlsf_sitemaps_enabled( 'sitemap' ) && $output .= 'Sitemap: ' . xmlsf_sitemap_url() . PHP_EOL;
xmlsf_sitemaps_enabled( 'news' ) && $output .= 'Sitemap: ' . xmlsf_sitemap_url( 'news' );
}
return $output;
}
/**
* Are any sitemaps enabled?
*
* @since 5.4
*
* @param string $which Which sitemap to check for. Default any sitemap.
*
* @return false|array
*/
function xmlsf_sitemaps_enabled( $which = 'any' ) {
static $enabled;
if ( null === $enabled ) {
$sitemaps = (array) get_option( 'xmlsf_sitemaps', array() );
switch ( true ) {
case isset( $sitemaps['sitemap'] ) && isset( $sitemaps['sitemap-news'] ):
$enabled = array( 'sitemap', 'news' );
break;
case isset( $sitemaps['sitemap'] ):
$enabled = array( 'sitemap' );
break;
case isset( $sitemaps['sitemap-news'] ):
$enabled = array( 'news' );
break;
default:
case 1 !== (int) get_option( 'blog_public' ):
$enabled = array();
}
}
if ( 'sitemap' === $which ) {
// Looking for regular sitemap.
return apply_filters( 'xmlsf_sitemaps_enabled', in_array( 'sitemap', $enabled, true ), 'sitemap' );
}
if ( 'news' === $which ) {
// Looking for news sitemap.
return apply_filters( 'xmlsf_sitemaps_enabled', in_array( 'news', $enabled, true ), 'news' );
}
// Looking for any sitemap.
return apply_filters( 'xmlsf_sitemaps_enabled', ! empty( $enabled ), $which );
}
/**
* CONDITIONAL TAGS
*/
if ( ! function_exists( 'is_sitemap' ) ) {
/**
* Is the query for a sitemap?
*
* @since 4.8
* @return bool
*/
function is_sitemap() {
if ( function_exists( 'wp_sitemaps_loaded' ) ) {
global $wp_query;
if ( ! isset( $wp_query ) ) {
_doing_it_wrong( __FUNCTION__, esc_html__( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1.0' );
return false;
}
return property_exists( $wp_query, 'is_sitemap' ) ? $wp_query->is_sitemap : false;
}
global $xmlsf;
if ( ! is_object( $xmlsf ) || false === $xmlsf->request_filtered ) {
_doing_it_wrong( __FUNCTION__, esc_html__( 'Conditional sitemap tags do not work before the sitemap request filter is run. Before then, they always return false.', 'xml-sitemap-feed' ), '4.8' );
return false;
}
return $xmlsf->is_sitemap;
}
}
if ( ! function_exists( 'is_news' ) ) {
/**
* Is the query for a news sitemap?
*
* @since 4.8
* @return bool
*/
function is_news() {
global $xmlsf;
if ( ! is_object( $xmlsf ) || false === $xmlsf->request_filtered_news ) {
_doing_it_wrong( __FUNCTION__, esc_html__( 'Conditional sitemap tags do not work before the sitemap request filter is run. Before then, they always return false.', 'xml-sitemap-feed' ), '4.8' );
return false;
}
return $xmlsf->is_news;
}
}