forked from Automattic/msm-sitemap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msm-sitemap.php
920 lines (767 loc) · 29.1 KB
/
msm-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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
<?php
/*
Plugin Name: Metro Sitemap
Description: Comprehensive sitemaps for your WordPress site. Joint collaboration between Metro.co.uk, MAKE, Alley Interactive, and WordPress.com VIP.
Author: Artur Synowiec, Paul Kevan, and others
Version: 1.4.1
Stable tag: 1.4.1
License: GPLv2
Requires PHP: 7.4
*/
if ( defined( 'WP_CLI' ) && true === WP_CLI ) {
require dirname( __FILE__ ) . '/includes/wp-cli.php';
}
require dirname( __FILE__ ) . '/includes/class-msm-sitemap-admin-renderer.php';
class Metro_Sitemap {
const DEFAULT_POSTS_PER_SITEMAP_PAGE = 500;
const SITEMAP_CPT = 'msm_sitemap';
public static $index_by_year = false;
/**
* Register actions for our hook
*/
public static function setup() {
define( 'MSM_INTERVAL_PER_GENERATION_EVENT', 60 ); // how far apart should full cron generation events be spaced
add_filter( 'cron_schedules', array( __CLASS__, 'sitemap_15_min_cron_interval' ) );
// Filter to allow the sitemap to be indexed by year
self::$index_by_year = apply_filters( 'msm_sitemap_index_by_year', false );
// A cron schedule for creating/updating sitemap posts based on updated content since the last run
add_action( 'init', array( __CLASS__, 'sitemap_init' ) );
add_action( 'admin_init', array( __CLASS__, 'sitemap_init_cron' ) );
add_action( 'redirect_canonical', array( __CLASS__, 'disable_canonical_redirects_for_sitemap_xml' ), 10, 2 );
add_action( 'init', array( __CLASS__, 'create_post_type' ) );
add_filter( 'posts_pre_query', array( __CLASS__, 'disable_main_query_for_sitemap_xml' ), 10, 2 );
add_filter( 'template_include', array( __CLASS__, 'load_sitemap_template' ) );
// Disable WordPress 5.5-era sitemaps.
add_filter( 'wp_sitemaps_enabled', '__return_false' );
// By default, we use wp-cron to help generate the full sitemap.
// However, this will let us override it, if necessary, like on WP.com
if ( true === apply_filters( 'msm_sitemap_use_cron_builder', true ) ) {
require dirname( __FILE__ ) . '/includes/msm-sitemap-builder-cron.php';
MSM_Sitemap_Builder_Cron::setup();
}
}
public static function use_custom_queries(): bool {
return ! apply_filters( 'msm_sitemap_avoid_custom_queries', false );
}
public static function get_partition_suffix(): string {
$partition = apply_filters( 'msm_sitemap_partition', '' );
return ( empty( $partition ) ? '' : "-$partition" );
}
public static function max_sitemap_length(): int {
return filter_var(
apply_filters( 'msm_sitemap_max_sitemap_length', get_option( 'msm_sitemap_max_sitemap_length', 10000 ) ),
FILTER_VALIDATE_INT
);
}
/**
* Register 15 minute cron interval for latest articles
* @param array[] $schedules
* @return array[] modified schedules
*/
public static function sitemap_15_min_cron_interval( $schedules ) {
$schedules[ 'ms-sitemap-15-min-cron-interval' ] = array(
'interval' => 900,
'display' => __( 'Every 15 minutes', 'metro-sitemaps' ),
);
return $schedules;
}
/**
* Register endpoint for sitemap and other hooks
*/
public static function sitemap_init() {
if ( ! defined( 'WPCOM_SKIP_DEFAULT_SITEMAP' ) ) {
define( 'WPCOM_SKIP_DEFAULT_SITEMAP', true );
}
self::sitemap_rewrite_init();
add_filter( 'robots_txt', array( __CLASS__, 'robots_txt' ), 10, 2 );
add_action( 'admin_menu', array( __CLASS__, 'metro_sitemap_menu' ) );
add_action( 'msm_cron_update_sitemap', array( __CLASS__, 'update_sitemap_from_modified_posts' ) );
add_action( 'wp_ajax_msm-sitemap-get-sitemap-counts', array( __CLASS__, 'ajax_get_sitemap_counts' ) );
}
/**
* Setup rewrite rules for the sitemap
*/
public static function sitemap_rewrite_init() {
// Allow 'sitemap=true' parameter
add_rewrite_tag( '%sitemap%', 'true' );
// Define rewrite rules for the index based on the setup
if ( self::$index_by_year ) {
add_rewrite_tag( '%sitemap-year%', '[0-9]{4}' );
add_rewrite_rule( '^sitemap-([0-9]{4}).xml$','index.php?sitemap=true&sitemap-year=$matches[1]','top' );
} else {
add_rewrite_rule( '^sitemap.xml$','index.php?sitemap=true','top' );
}
}
/**
* Register admin menu for sitemap
*/
public static function metro_sitemap_menu() {
$partitions = apply_filters( 'msm_sitemap_partitions', ['default'] );
if ( 0 === count( $partitions ) ) {
return;
} elseif ( 1 < count( $partitions ) ) {
$page_hooks = array_map(
function ( string $name ) {
$polite_name = empty( $name ) ? __( 'Sitemap', 'metro-sitemaps' ) : implode( ' ', [ ucfirst( $name ), __( 'Sitemap', 'metro-sitemaps' ) ] );
$parts = [ 0 => 'metro', 2 => 'sitemap' ];
if ( ! empty( $name ) ) {
$parts[1] = $name;
}
ksort( $parts );
return add_management_page(
$polite_name,
$polite_name,
'manage_options',
implode( '-', $parts ),
[ new Metro_Sitemap_Admin_Renderer( $name, $polite_name ), 'render_sitemap_options_page' ]
);
},
$partitions
);
array_walk(
$page_hooks,
function( string $hook ) {
add_action( 'admin_print_scripts-' . $hook, array( __CLASS__, 'add_admin_scripts' ) );
}
);
return;
}
$page_hook = add_management_page( __( 'Sitemap', 'metro-sitemaps' ), __( 'Sitemap', 'metro-sitemaps' ), 'manage_options', 'metro-sitemap', array( new Metro_Sitemap_Admin_Renderer(), 'render_sitemap_options_page' ) );
add_action( 'admin_print_scripts-' . $page_hook, array( __CLASS__, 'add_admin_scripts' ) );
}
public static function add_admin_scripts() {
wp_enqueue_script( 'flot', plugins_url( '/js/jquery.flot.js', __FILE__ ), array( 'jquery' ) );
wp_enqueue_script( 'msm-sitemap-admin', plugins_url( '/js/msm-sitemap-admin.js', __FILE__ ), array( 'jquery', 'flot' ) );
wp_enqueue_script( 'flot-time', plugins_url( '/js/jquery.flot.time.js', __FILE__ ), array( 'jquery', 'flot' ) );
wp_enqueue_style( 'msm-sitemap-css', plugins_url( 'css/style.css', __FILE__ ) );
wp_enqueue_style( 'noticons', '//s0.wordpress.com/i/noticons/noticons.css' );
}
public static function ajax_get_sitemap_counts() {
check_admin_referer( 'msm-sitemap-action' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.', 'metro-sitemaps' ) );
}
$n = 10;
if ( isset( $_REQUEST['num_days'] ) ) {
$n = intval( $_REQUEST['num_days'] );
}
if ( isset( $_REQUEST['partition'] ) ) {
$name = sanitize_key( $_REQUEST['partition'] );
do_action( 'msm_sitemap_select_partition', $name );
}
$data = array(
'total_indexed_urls' => number_format( Metro_Sitemap::get_total_indexed_url_count() ),
'total_sitemaps' => number_format( Metro_Sitemap::count_sitemaps() ),
'sitemap_indexed_urls' => self::get_recent_sitemap_url_counts( $n ),
);
wp_send_json( $data );
}
/**
* Displays a notice, error or warning to the user
* @param str $message The message to show to the user
*/
public static function show_action_message( $message, $level = 'notice' ) {
$class = 'updated';
if ( $level === 'warning' )
$class = 'update-nag';
elseif ( $level === 'error' )
$class = 'error';
echo '<div class="' . esc_attr( $class ) . ' msm-sitemap-message"><p>' . wp_kses( $message, wp_kses_allowed_html( 'post' ) ) . '</p></div>';
}
/**
* Counts the number of sitemaps that have been generated.
*
* @return int The number of sitemaps that have been generated
*/
public static function count_sitemaps() {
if ( self::use_custom_queries() ) {
$count = wp_count_posts( Metro_Sitemap::SITEMAP_CPT );
return (int) $count->publish;
}
$args = [
'post_type' => Metro_Sitemap::SITEMAP_CPT,
'post_status' => 'publish',
];
$query = new \WP_Query( $args );
return (int) $query->found_posts;
}
/**
* Gets the current number of URLs indexed by msm-sitemap accross all sitemaps.
*
* @return int The number of total number URLs indexed
*/
public static function get_total_indexed_url_count() {
return intval( get_option( 'msm_sitemap_indexed_url_count' . self::get_partition_suffix(), 0 ) );
}
/**
* Returns the $n most recent sitemap indexed url counts.
*
* @param int $n The number of days of sitemap stats to grab.
* @return array An array of sitemap stats
*/
public static function get_recent_sitemap_url_counts( $n = 7 ) {
$stats = array();
for ( $i = 0; $i < $n; $i++ ) {
$date = date( 'Y-m-d', strtotime( "-$i days" ) );
list( $year, $month, $day ) = explode( '-', $date );
$stats[ $date ] = self::get_indexed_url_count( $year, $month, $day );
}
return $stats;
}
public static function is_blog_public() {
return ( 1 == get_option( 'blog_public' ) )
|| apply_filters( 'msm_sitemap_blog_public', false );
}
/**
* Gets the number of URLs indexed for the given sitemap.
*
* @param array $sitemaps The sitemaps to retrieve counts for.
*/
public static function get_indexed_url_count( $year, $month, $day ) {
$sitemap_id = self::get_sitemap_post_id( $year, $month, $day );
if ( $sitemap_id ) {
return intval( get_post_meta( $sitemap_id, 'msm_indexed_url_count', true ) );
}
return false;
}
/**
* Add entries to the bottom of robots.txt
*/
public static function robots_txt( $output, $public ) {
// Make sure the site isn't private
if ( '1' == $public ) {
$output .= '# Sitemap archive' . PHP_EOL;
if ( self::$index_by_year ) {
$years = self::check_year_has_posts();
foreach ( $years as $year ) {
$output .= 'Sitemap: ' . home_url( '/sitemap-' . absint( $year ) . '.xml' ) . PHP_EOL;
}
$output .= PHP_EOL;
} else {
$output .= 'Sitemap: ' . home_url( '/sitemap.xml' ) . PHP_EOL . PHP_EOL;
}
}
return $output;
}
/**
* Add cron jobs required to generate these sitemaps
*/
public static function sitemap_init_cron() {
if ( self::is_blog_public() ) {
$partitions = apply_filters( 'msm_sitemap_partitions', [''] );
array_walk(
$partitions,
function ( string $partition_name ) {
$args = [ $partition_name ];
if ( ! wp_next_scheduled( 'msm_cron_update_sitemap', $args ) ) {
wp_schedule_event( time(), 'ms-sitemap-15-min-cron-interval', 'msm_cron_update_sitemap', $args );
}
}
);
}
}
/**
* Disable canonical redirects for the sitemap files
* @see http://codex.wordpress.org/Function_Reference/redirect_canonical
* @param string $redirect_url
* @param string $requested_url
* @return string URL to redirect
*/
public static function disable_canonical_redirects_for_sitemap_xml( $redirect_url, $requested_url ) {
if ( self::$index_by_year ) {
$pattern = '|sitemap-([0-9]{4})\.xml|';
} else {
$pattern = '|sitemap\.xml|';
}
if ( preg_match( $pattern, $requested_url ) ) {
return $requested_url;
}
return $redirect_url;
}
/**
* Hook allows developers to extend the sitemap functionality easily and integrate their custom post statuses.
*
* Rather than having to modify the plugin code, developers can use this filter to add their custom post statuses.
*
* @since 1.4.3
*
*/
public static function get_post_status(): string {
$default_status = 'publish';
$post_status = apply_filters('msm_sitemap_post_status', $default_status);
$allowed_statuses = get_post_stati();
if (!in_array($post_status, $allowed_statuses)) {
$post_status = $default_status;
}
return $post_status;
}
/**
* Return range of years for posts in the database
* @return int[] valid years
*/
public static function get_post_year_range() {
global $wpdb;
$post_status = self::get_post_status();
$oldest_post_date_year = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT YEAR(post_date) as year FROM $wpdb->posts WHERE post_status = %s AND post_date > 0 ORDER BY year ASC LIMIT 1", $post_status ) );
if ( null !== $oldest_post_date_year ) {
$current_year = date( 'Y' );
return range( (int) $oldest_post_date_year, $current_year );
}
return array();
}
/**
* Get every year that has valid posts in a range
* @return int[] years with posts
*/
public static function check_year_has_posts() {
$all_years = self::get_post_year_range();
$years_with_posts = array();
foreach ( $all_years as $year ) {
if ( self::date_range_has_posts( self::get_date_stamp( $year, 1, 1 ), self::get_date_stamp( $year, 12, 31 ) ) ) {
$years_with_posts[] = $year;
}
}
return $years_with_posts;
}
/**
* Get properly formatted data stamp from year, month, and day
* @param int $year
* @param int $month
* @param int $day
* @return string formatted stamp
*/
public static function get_date_stamp( $year, $month, $day ) {
return sprintf( '%s-%s-%s', $year, str_pad( $month, 2, '0', STR_PAD_LEFT ), str_pad( $day, 2, '0', STR_PAD_LEFT ) );
}
/**
* Does a current date range have posts?
* @param string $start_date
* @param string $end_date
* @return int|false
*/
public static function date_range_has_posts( $start_date, $end_date ) {
global $wpdb;
$start_date .= ' 00:00:00';
$end_date .= ' 23:59:59';
$post_status = self::get_post_status();
$post_types_in = self::get_supported_post_types_in();
return $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_status = %s AND post_date >= %s AND post_date <= %s AND post_type IN ( {$post_types_in} ) LIMIT 1", $post_status, $start_date, $end_date ) );
}
/**
* Get a list of support post_type IDs for a given date
*
* @param string $sitemap_date Date in Y-m-d
* @param int Number of post IDs to return
* @return array IDs of posts
*/
public static function get_post_ids_for_date( $sitemap_date, $limit = 500 ) {
global $wpdb;
$post_status = self::get_post_status();
$start_date = $sitemap_date . ' 00:00:00';
$end_date = $sitemap_date . ' 23:59:59';
$post_types_in = self::get_supported_post_types_in();
$posts = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_date FROM $wpdb->posts WHERE post_status = %s AND post_date >= %s AND post_date <= %s AND post_type IN ( {$post_types_in} ) LIMIT %d", $post_status, $start_date, $end_date, $limit ) );
usort( $posts, array( __CLASS__ , 'order_by_post_date' ) );
$post_ids = wp_list_pluck( $posts, 'ID' );
return array_map( 'intval', $post_ids );
}
/**
* Generate sitemap for a date; this is where XML is rendered.
* @param string $sitemap_date
*/
public static function generate_sitemap_for_date( $sitemap_date ) {
list( $year, $month, $day ) = explode( '-', $sitemap_date );
$sitemap_name = $sitemap_date;
$sitemap_exists = false;
$sitemap_data = array(
'post_name' => $sitemap_name,
'post_title' => $sitemap_name,
'post_type' => self::SITEMAP_CPT,
'post_status' => self::get_post_status(),
'post_date' => $sitemap_date,
);
if ( self::use_custom_queries() ) {
global $wpdb;
$sitemap_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = %s AND post_name = %s LIMIT 1", self::SITEMAP_CPT, $sitemap_name ) );
} else {
$sitemap_id = self::get_sitemap_post_id( $year, $month, $day );
}
if ( $sitemap_id ) {
$sitemap_exists = true;
} else {
$sitemap_id = wp_insert_post( $sitemap_data );
$sitemap_exists = true;
}
$per_page = apply_filters( 'msm_sitemap_entry_posts_per_page', self::DEFAULT_POSTS_PER_SITEMAP_PAGE );
$post_ids = self::get_post_ids_for_date( $sitemap_date, $per_page );
if ( empty( $post_ids ) ) {
// If no entries - delete the whole sitemap post
if ( $sitemap_exists ) {
self::delete_sitemap_by_id( $sitemap_id );
}
return;
}
$total_url_count = self::get_total_indexed_url_count();
// For migration: in case the previous version used an array for this option
if ( is_array( $total_url_count ) ) {
$total_url_count = array_sum( $total_url_count );
update_option( 'msm_sitemap_indexed_url_count' . self::get_partition_suffix(), $total_url_count, false );
}
// SimpleXML doesn't allow us to define namespaces using addAttribute, so we need to specify them in the construction instead.
$namespaces = apply_filters( 'msm_sitemap_namespace', array(
'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9',
'xmlns:n' => 'http://www.google.com/schemas/sitemap-news/0.9',
'xmlns:image' => 'http://www.google.com/schemas/sitemap-image/1.1',
'xsi:schemaLocation' => 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd',
) );
$namespace_str = '<?xml version="1.0" encoding="utf-8"?><urlset';
foreach ( $namespaces as $ns => $value ) {
$namespace_str .= sprintf( ' %s="%s"', esc_attr( $ns ), esc_attr( $value ) );
}
$namespace_str .= '></urlset>';
// Create XML
$xml = new SimpleXMLElement( $namespace_str );
$url_count = 0;
foreach ( $post_ids as $post_id ) {
$GLOBALS['post'] = get_post( $post_id );
setup_postdata( $GLOBALS['post'] );
if ( apply_filters( 'msm_sitemap_skip_post', false ) )
continue;
$url = $xml->addChild( 'url' );
$url->addChild( 'loc', esc_url( get_permalink() ) );
$url->addChild( 'lastmod', get_post_modified_time( 'c', true ) );
$url->addChild( 'changefreq', 'monthly' );
$url->addChild( 'priority', '0.7' );
apply_filters( 'msm_sitemap_entry', $url );
++$url_count;
// TODO: add images to sitemap via <image:image> tag
}
$generated_xml_string = $xml->asXML();
// Save the sitemap
if ( $sitemap_exists ) {
// Get the previous post count
$previous_url_count = intval( get_post_meta( $sitemap_id, 'msm_indexed_url_count', true ) );
// Update the total post count with the difference
$total_url_count += $url_count - $previous_url_count;
update_post_meta( $sitemap_id, 'msm_sitemap_xml', $generated_xml_string );
update_post_meta( $sitemap_id, 'msm_indexed_url_count', $url_count );
do_action( 'msm_update_sitemap_post', $sitemap_id, $year, $month, $day, $generated_xml_string, $url_count );
} else {
/* Should no longer hit this */
$sitemap_id = wp_insert_post( $sitemap_data );
add_post_meta( $sitemap_id, 'msm_sitemap_xml', $generated_xml_string );
add_post_meta( $sitemap_id, 'msm_indexed_url_count', $url_count );
do_action( 'msm_insert_sitemap_post', $sitemap_id, $year, $month, $day, $generated_xml_string, $url_count );
// Update the total url count
$total_url_count += $url_count;
}
// Update indexed url counts
update_option( 'msm_sitemap_indexed_url_count' . self::get_partition_suffix(), $total_url_count, false );
wp_reset_postdata();
}
public static function delete_sitemap_for_date( $sitemap_date ) {
list( $year, $month, $day ) = explode( '-', $sitemap_date );
$sitemap_id = self::get_sitemap_post_id( $year, $month, $day );
if ( ! $sitemap_id ) {
return false;
}
return self::delete_sitemap_by_id( $sitemap_id );
}
public static function delete_sitemap_by_id( $sitemap_id ) {
$sitemap = get_post( $sitemap_id );
if ( ! $sitemap ) {
return false;
}
$sitemap_date = date( 'Y-m-d', strtotime( $sitemap->post_date ) );
list( $year, $month, $day ) = explode( '-', $sitemap_date );
$total_url_count = self::get_total_indexed_url_count();
$total_url_count -= intval( get_post_meta( $sitemap_id, 'msm_indexed_url_count', true ) );
update_option( 'msm_sitemap_indexed_url_count' . self::get_partition_suffix(), $total_url_count, false );
wp_delete_post( $sitemap_id, true );
do_action( 'msm_delete_sitemap_post', $sitemap_id, $year, $month, $day );
}
/**
* Register our CPT
*/
public static function create_post_type() {
register_post_type(
self::SITEMAP_CPT,
array(
'labels' => array(
'name' => __( 'Sitemaps' ),
'singular_name' => __( 'Sitemap' ),
),
'public' => false,
'has_archive' => false,
'rewrite' => false,
'show_ui' => true, // TODO: should probably have some sort of custom UI
'show_in_menu' => false, // Since we're manually adding a Sitemaps menu, no need to auto-add one through the CPT.
'supports' => array(
'title',
),
)
);
}
/**
* Get posts modified within the last hour
* @return object[] modified posts
*/
public static function get_last_modified_posts() {
global $wpdb;
$sitemap_last_run = get_option( 'msm_sitemap_update_last_run' . self::get_partition_suffix() , false );
$date = date( 'Y-m-d H:i:s', ( current_time( 'timestamp', 1 ) - 3600 ) ); // posts changed within the last hour
if ( $sitemap_last_run ) {
$date = date( 'Y-m-d H:i:s', $sitemap_last_run );
}
$post_types_in = self::get_supported_post_types_in();
$query = $wpdb->prepare( "SELECT ID, post_date FROM $wpdb->posts WHERE post_type IN ( {$post_types_in} ) AND post_modified_gmt >= %s LIMIT 1000", $date );
/**
* Filter the query used to get the last modified posts.
* $wpdb->prepare() should be used for security if a new replacement query is created in the callback.
*
* @param string $query The query to use to get the last modified posts.
* @param string $post_types_in A comma-separated list of post types to include in the query.
* @param string $date The date to use as the cutoff for the query.
*/
$query = apply_filters( 'msm_pre_get_last_modified_posts', $query, $post_types_in, $date );
$modified_posts = $wpdb->get_results( $query );
return $modified_posts;
}
/**
* Get dates for an array of posts
* @param object[] $posts
* @return string[] unique dates of each post.
*/
public static function get_post_dates( $posts ) {
$dates = array();
foreach ( $posts as $post ) {
$dates[] = date( 'Y-m-d', strtotime( $post->post_date ) );
}
$dates = array_unique( $dates );
return $dates;
}
/**
* Update the sitemap with changes from recently modified posts
*/
public static function update_sitemap_from_modified_posts( $partition_name = '' ) {
if ( ! empty( $partition_name ) ) {
do_action( 'msm_sitemap_select_partition', $partition_name );
}
$time = current_time( 'timestamp', 1 );
$last_modified_posts = self::get_last_modified_posts();
$dates = self::get_post_dates( $last_modified_posts );
foreach ( $dates as $date ) {
list( $year, $month, $day ) = explode( '-', $date );
// Do not allow non-existant or future dates to be queued
if ( false === checkdate( $month, $day, $year ) || $time <= mktime( 0, 0, 0, $month, $day, $year ) ) {
continue;
}
$time += MSM_INTERVAL_PER_GENERATION_EVENT;
do_action( 'msm_update_sitemap_for_year_month_date', array( $year, $month, $day ), $time );
}
update_option( 'msm_sitemap_update_last_run' . self::get_partition_suffix(), current_time( 'timestamp', 1 ), false );
}
/**
* Trigger rendering of the actual sitemap
*/
public static function load_sitemap_template( $template ) {
if ( get_query_var( 'sitemap' ) === 'true' ) {
$template = dirname( __FILE__ ) . '/templates/full-sitemaps.php';
}
return $template;
}
/**
* Disable Main Query when rendering sitemaps
*
* @param array|null $posts array of post data or null
* @param WP_Query $query The WP_Query instance.
*/
public static function disable_main_query_for_sitemap_xml( $posts, $query ) {
if ( $query->is_main_query() && isset($query->query_vars['sitemap']) && 'true' === $query->query_vars['sitemap'] ) {
$posts = array();
}
return $posts;
}
/**
* Build Root sitemap XML
* Can be all days (default) or a specific year.
* @param int|boolean $year
*/
public static function build_root_sitemap_xml( $year = false ) {
$xml_prefix = '<?xml version="1.0" encoding="utf-8"?>';
if ( self::use_custom_queries() ) {
global $wpdb;
// Direct query because we just want dates of the sitemap entries and this is much faster than WP_Query
if ( is_numeric( $year ) ) {
$query = $wpdb->prepare( "SELECT post_date FROM $wpdb->posts WHERE post_type = %s AND YEAR(post_date) = %s ORDER BY post_date DESC LIMIT %d", Metro_Sitemap::SITEMAP_CPT, $year, self::max_sitemap_length() );
} else {
$query = $wpdb->prepare( "SELECT post_date FROM $wpdb->posts WHERE post_type = %s ORDER BY post_date DESC LIMIT %d", Metro_Sitemap::SITEMAP_CPT, self::max_sitemap_length() );
}
$sitemaps = $wpdb->get_col( $query );
} else {
$sitemaps = self::get_sitemap_dates( $year );
}
// Sometimes duplicate sitemaps exist, lets make sure so they are not output
$sitemaps = array_unique( $sitemaps );
/**
* Filter daily sitemaps from the index by date.
*
* Expects an array of dates in MySQL DATETIME format [ Y-m-d H:i:s ].
*
* Since adding dates that do not have posts is pointless, this filter is primarily intended for removing
* dates before or after a specific date or possibly targeting specific dates to exclude.
*
* @since 1.4.0
*
* @param array $sitemaps Array of dates in MySQL DATETIME format [ Y-m-d H:i:s ].
* @param string $year Year that sitemap is being generated for.
*/
$sitemaps = apply_filters( 'msm_sitemap_index', $sitemaps, $year );
$xml = new SimpleXMLElement( $xml_prefix . '<sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"></sitemapindex>' );
foreach ( $sitemaps as $sitemap_date ) {
$sitemap = $xml->addChild( 'sitemap' );
$sitemap->loc = self::build_sitemap_url( $sitemap_date ); // manually set the child instead of addChild to prevent "unterminated entity reference" warnings due to encoded ampersands http://stackoverflow.com/a/555039/169478
}
return $xml->asXML();
}
/**
* Return sitemap post_dates.
*
* @param ?int $year year to list.
*
* @return string[] dates of sitemap posts.
*/
public static function get_sitemap_dates( $year = false ): array {
global $wpdb;
$args = [
'post_type' => Metro_Sitemap::SITEMAP_CPT,
'orderby' => 'post_date',
'order' => 'DESC',
'fields' => 'ids',
'numberposts' => self::max_sitemap_length(),
];
if ( is_numeric( $year ) ) {
$args['m'] = $year;
}
return $wpdb->get_col(
sprintf(
"SELECT post_date FROM $wpdb->posts WHERE ID IN (%s) ORDER BY post_date DESC",
implode( ',', get_posts( $args ) )
)
);
return $sitemaps;
}
/**
* Build the sitemap URL for a given date
* @param string $sitemap_date
* @return string
*/
public static function build_sitemap_url( $sitemap_date ) {
$sitemap_time = strtotime( $sitemap_date );
if ( self::$index_by_year ) {
$sitemap_url = add_query_arg(
array(
'mm' => date( 'm', $sitemap_time ),
'dd' => date( 'd', $sitemap_time ),
),
home_url( '/sitemap-' . date( 'Y', $sitemap_time ) . '.xml' )
);
} else {
$sitemap_url = add_query_arg(
array(
'yyyy' => date( 'Y', $sitemap_time ),
'mm' => date( 'm', $sitemap_time ),
'dd' => date( 'd', $sitemap_time ),
),
home_url( '/sitemap.xml' )
);
}
return apply_filters( 'msm_sitemap_url', $sitemap_url );
}
public static function get_sitemap_post_id( $year, $month, $day ) {
$ymd = self::get_date_stamp( $year, $month, $day );
$sitemap_args = array(
'date_query' => array(
array(
'before' => sprintf( '%s 00:00:00', $ymd ),
'after' => sprintf( '%s 00:00:00', $ymd ),
'inclusive' => true,
),
),
'orderby' => 'ID',
'order' => 'ASC',
'posts_per_page' => 1,
'fields' => 'ids',
'post_type' => self::SITEMAP_CPT,
'no_found_rows' => true,
'update_term_cache' => false,
'suppress_filters' => false,
);
$sitemap_query = get_posts( $sitemap_args );
if ( ! empty( $sitemap_query ) ) {
return $sitemap_query[0];
}
return false;
}
/**
* Get XML for individual day
*/
public static function build_individual_sitemap_xml( $year, $month, $day ) {
// Get XML for an individual day. Stored as full xml
$sitemap_id = self::get_sitemap_post_id( $year, $month, $day );
if ( $sitemap_id ) {
$sitemap_content = get_post_meta( $sitemap_id, 'msm_sitemap_xml', true );
// Return is now as it should be valid xml!
return $sitemap_content;
}
/* There are no posts for this day */
return false;
}
/**
* Build XML for output to clean up the template file
*/
public static function build_xml( $request = array() ) {
$year = $request['year'];
$month = $request['month'];
$day = $request['day'];
if ( ( false === $year || is_numeric( $year ) ) && false === $month && false === $day ) {
$xml = self::build_root_sitemap_xml( $year );
} else if ( $year > 0 && $month > 0 && $day > 0 ) {
$xml = self::build_individual_sitemap_xml( $year, $month, $day );
} else {
/* Invalid options sent */
return false;
}
return $xml;
}
public static function get_supported_post_types() {
return apply_filters( 'msm_sitemap_entry_post_type', array( 'post' ) );
}
private static function get_supported_post_types_in() {
global $wpdb;
$post_types = self::get_supported_post_types();
$post_types_prepared = array();
foreach ( $post_types as $post_type ) {
$post_types_prepared[] = $wpdb->prepare( '%s', $post_type );
}
return implode( ', ', $post_types_prepared );
}
/**
* Helper function for PHP ordering of posts by date, desc.
*
* @param object $post_a StdClass object, or WP_Post object to order.
* @param object $post_b StdClass object or WP_Post object to order.
*
* @return int
*/
private static function order_by_post_date( $post_a, $post_b ) {
$a_date = strtotime( $post_a->post_date );
$b_date = strtotime( $post_b->post_date );
if ( $a_date === $b_date ) {
return 0;
}
return ( $a_date < $b_date ) ? -1 : 1;
}
}
add_action( 'after_setup_theme', array( 'Metro_Sitemap', 'setup' ) );