1212if ( defined ( 'WP_CLI ' ) && true === WP_CLI ) {
1313 require dirname ( __FILE__ ) . '/includes/wp-cli.php ' ;
1414}
15+ require dirname ( __FILE__ ) . '/includes/class-msm-sitemap-admin-renderer.php ' ;
1516
1617class Metro_Sitemap {
1718
@@ -113,7 +114,38 @@ public static function sitemap_rewrite_init() {
113114 * Register admin menu for sitemap
114115 */
115116 public static function metro_sitemap_menu () {
116- $ page_hook = add_management_page ( __ ( 'Sitemap ' , 'metro-sitemaps ' ), __ ( 'Sitemap ' , 'metro-sitemaps ' ), 'manage_options ' , 'metro-sitemap ' , array ( __CLASS__ , 'render_sitemap_options_page ' ) );
117+ $ partitions = apply_filters ( 'msm_sitemap_partitions ' , ['default ' ] );
118+ if ( 0 === count ( $ partitions ) ) {
119+ return ;
120+ } elseif ( 1 < count ( $ partitions ) ) {
121+ $ page_hooks = array_map (
122+ function ( string $ name ) {
123+ $ polite_name = empty ( $ name ) ? __ ( 'Sitemap ' , 'metro-sitemaps ' ) : implode ( ' ' , [ ucfirst ( $ name ), __ ( 'Sitemap ' , 'metro-sitemaps ' ) ] );
124+ $ parts = [ 0 => 'metro ' , 2 => 'sitemap ' ];
125+ if ( ! empty ( $ name ) ) {
126+ $ parts [1 ] = $ name ;
127+ }
128+ ksort ( $ parts );
129+ return add_management_page (
130+ $ polite_name ,
131+ $ polite_name ,
132+ 'manage_options ' ,
133+ implode ( '- ' , $ parts ),
134+ [ new Metro_Sitemap_Admin_Renderer ( $ name , $ polite_name ), 'render_sitemap_options_page ' ]
135+ );
136+ },
137+ $ partitions
138+ );
139+ array_walk (
140+ $ page_hooks ,
141+ function ( string $ hook ) {
142+ add_action ( 'admin_print_scripts- ' . $ hook , array ( __CLASS__ , 'add_admin_scripts ' ) );
143+ }
144+ );
145+ return ;
146+ }
147+
148+ $ 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 ' ) );
117149 add_action ( 'admin_print_scripts- ' . $ page_hook , array ( __CLASS__ , 'add_admin_scripts ' ) );
118150 }
119151
@@ -137,6 +169,10 @@ public static function ajax_get_sitemap_counts() {
137169 if ( isset ( $ _REQUEST ['num_days ' ] ) ) {
138170 $ n = intval ( $ _REQUEST ['num_days ' ] );
139171 }
172+ if ( isset ( $ _REQUEST ['partition ' ] ) ) {
173+ $ name = sanitize_key ( $ _REQUEST ['partition ' ] );
174+ do_action ( 'msm_sitemap_select_partition ' , $ name );
175+ }
140176
141177 $ data = array (
142178 'total_indexed_urls ' => number_format ( Metro_Sitemap::get_total_indexed_url_count () ),
@@ -147,73 +183,6 @@ public static function ajax_get_sitemap_counts() {
147183 wp_send_json ( $ data );
148184 }
149185
150- /**
151- * Render admin options page
152- */
153- public static function render_sitemap_options_page () {
154- if ( ! current_user_can ( 'manage_options ' ) ) {
155- wp_die ( __ ( 'You do not have sufficient permissions to access this page. ' , 'metro-sitemaps ' ) );
156- }
157-
158- // Array of possible user actions
159- $ actions = apply_filters ( 'msm_sitemap_actions ' , array () );
160-
161- // Start outputting html
162- echo '<div class="wrap"> ' ;
163- screen_icon ();
164- echo '<h2> ' . __ ( 'Sitemap ' , 'metro-sitemaps ' ) . '</h2> ' ;
165-
166- if ( ! self ::is_blog_public () ) {
167- self ::show_action_message ( __ ( 'Oops! Sitemaps are not supported on private blogs. Please make your blog public and try again. ' , 'metro-sitemaps ' ), 'error ' );
168- echo '</div> ' ;
169- return ;
170- }
171-
172- if ( isset ( $ _POST ['action ' ] ) ) {
173- check_admin_referer ( 'msm-sitemap-action ' );
174- foreach ( $ actions as $ slug => $ action ) {
175- if ( $ action ['text ' ] !== $ _POST ['action ' ] ) continue ;
176-
177- do_action ( 'msm_sitemap_action- ' . $ slug );
178- break ;
179- }
180- }
181-
182- // All the settings we need to read to display the page
183- $ sitemap_create_in_progress = (bool ) get_option ( 'msm_sitemap_create_in_progress ' ) === true ;
184- $ sitemap_update_last_run = get_option ( 'msm_sitemap_update_last_run ' );
185-
186- // Determine sitemap status text
187- $ sitemap_create_status = apply_filters (
188- 'msm_sitemap_create_status ' ,
189- $ sitemap_create_in_progress ? __ ( 'Running ' , 'metro-sitemaps ' ) : __ ( 'Not Running ' , 'metro-sitemaps ' )
190- );
191-
192- ?>
193- <div class="stats-container">
194- <div class="stats-box"><strong id="sitemap-count"><?php echo number_format ( Metro_Sitemap::count_sitemaps () ); ?> </strong><?php esc_html_e ( 'Sitemaps ' , 'metro-sitemaps ' ); ?> </div>
195- <div class="stats-box"><strong id="sitemap-indexed-url-count"><?php echo number_format ( Metro_Sitemap::get_total_indexed_url_count () ); ?> </strong><?php esc_html_e ( 'Indexed URLs ' , 'metro-sitemaps ' ); ?> </div>
196- <div class="stats-footer"><span><span class="noticon noticon-time"></span><?php esc_html_e ( 'Updated ' , 'metro-sitemaps ' ); ?> <strong><?php echo human_time_diff ( $ sitemap_update_last_run ); ?> <?php esc_html_e ( 'ago ' , 'metro-sitemaps ' ) ?> </strong></span></div>
197- </div>
198-
199- <h3><?php esc_html_e ( 'Latest Sitemaps ' , 'metro-sitemaps ' ); ?> </h3>
200- <div class="stats-container stats-placeholder"></div>
201- <div id="stats-graph-summary"><?php printf ( __ ( 'Max: %s on %s. Showing the last %s days. ' , 'metro-sitemaps ' ), '<span id="stats-graph-max"></span> ' , '<span id="stats-graph-max-date"></span> ' , '<span id="stats-graph-num-days"></span> ' ); ?> </div>
202-
203- <h3><?php esc_html_e ( 'Generate ' , 'metro-sitemaps ' ); ?> </h3>
204- <p><strong><?php esc_html_e ( 'Sitemap Create Status: ' , 'metro-sitemaps ' ) ?> </strong> <?php echo esc_html ( $ sitemap_create_status ); ?> </p>
205- <form action="<?php echo menu_page_url ( 'metro-sitemap ' , false ) ?> " method="post" style="float: left;">
206- <?php wp_nonce_field ( 'msm-sitemap-action ' ); ?>
207- <?php foreach ( $ actions as $ action ):
208- if ( ! $ action ['enabled ' ] ) continue ; ?>
209- <input type="submit" name="action" class="button-secondary" value="<?php echo esc_attr ( $ action ['text ' ] ); ?> ">
210- <?php endforeach ; ?>
211- </form>
212- </div>
213- <div id="tooltip"><strong class="content"></strong> <?php esc_html_e ( 'indexed urls ' , 'metro-sitemaps ' ); ?> </div>
214- <?php
215- }
216-
217186 /**
218187 * Displays a notice, error or warning to the user
219188 * @param str $message The message to show to the user
@@ -234,8 +203,16 @@ public static function show_action_message( $message, $level = 'notice' ) {
234203 * @return int The number of sitemaps that have been generated
235204 */
236205 public static function count_sitemaps () {
237- $ count = wp_count_posts ( Metro_Sitemap::SITEMAP_CPT );
238- return (int ) $ count ->publish ;
206+ if ( self ::use_custom_queries () ) {
207+ $ count = wp_count_posts ( Metro_Sitemap::SITEMAP_CPT );
208+ return (int ) $ count ->publish ;
209+ }
210+ $ args = [
211+ 'post_type ' => Metro_Sitemap::SITEMAP_CPT ,
212+ 'post_status ' => 'publish ' ,
213+ ];
214+ $ query = new \WP_Query ( $ args );
215+ return (int ) $ query ->found_posts ;
239216 }
240217
241218 /**
@@ -244,7 +221,7 @@ public static function count_sitemaps() {
244221 * @return int The number of total number URLs indexed
245222 */
246223 public static function get_total_indexed_url_count () {
247- return intval ( get_option ( 'msm_sitemap_indexed_url_count ' , 0 ) );
224+ return intval ( get_option ( 'msm_sitemap_indexed_url_count ' . self :: get_partition_suffix () , 0 ) );
248225 }
249226
250227 /**
@@ -508,7 +485,7 @@ public static function generate_sitemap_for_date( $sitemap_date ) {
508485 // For migration: in case the previous version used an array for this option
509486 if ( is_array ( $ total_url_count ) ) {
510487 $ total_url_count = array_sum ( $ total_url_count );
511- update_option ( 'msm_sitemap_indexed_url_count ' , $ total_url_count , false );
488+ update_option ( 'msm_sitemap_indexed_url_count ' . self :: get_partition_suffix () , $ total_url_count , false );
512489 }
513490
514491 // SimpleXML doesn't allow us to define namespaces using addAttribute, so we need to specify them in the construction instead.
@@ -574,7 +551,7 @@ public static function generate_sitemap_for_date( $sitemap_date ) {
574551 }
575552
576553 // Update indexed url counts
577- update_option ( 'msm_sitemap_indexed_url_count ' , $ total_url_count , false );
554+ update_option ( 'msm_sitemap_indexed_url_count ' . self :: get_partition_suffix () , $ total_url_count , false );
578555
579556 wp_reset_postdata ();
580557 }
@@ -599,7 +576,7 @@ public static function delete_sitemap_by_id( $sitemap_id ) {
599576
600577 $ total_url_count = self ::get_total_indexed_url_count ();
601578 $ total_url_count -= intval ( get_post_meta ( $ sitemap_id , 'msm_indexed_url_count ' , true ) );
602- update_option ( 'msm_sitemap_indexed_url_count ' , $ total_url_count , false );
579+ update_option ( 'msm_sitemap_indexed_url_count ' . self :: get_partition_suffix () , $ total_url_count , false );
603580
604581 wp_delete_post ( $ sitemap_id , true );
605582 do_action ( 'msm_delete_sitemap_post ' , $ sitemap_id , $ year , $ month , $ day );
@@ -635,7 +612,7 @@ public static function create_post_type() {
635612 public static function get_last_modified_posts () {
636613 global $ wpdb ;
637614
638- $ sitemap_last_run = get_option ( 'msm_sitemap_update_last_run ' , false );
615+ $ sitemap_last_run = get_option ( 'msm_sitemap_update_last_run ' . self :: get_partition_suffix () , false );
639616
640617 $ date = date ( 'Y-m-d H:i:s ' , ( current_time ( 'timestamp ' , 1 ) - 3600 ) ); // posts changed within the last hour
641618
@@ -680,7 +657,11 @@ public static function get_post_dates( $posts ) {
680657 /**
681658 * Update the sitemap with changes from recently modified posts
682659 */
683- public static function update_sitemap_from_modified_posts () {
660+ public static function update_sitemap_from_modified_posts ( $ partition_name = '' ) {
661+ if ( ! empty ( $ partition_name ) ) {
662+ do_action ( 'msm_sitemap_select_partition ' , $ partition_name );
663+ }
664+
684665 $ time = current_time ( 'timestamp ' , 1 );
685666 $ last_modified_posts = self ::get_last_modified_posts ();
686667 $ dates = self ::get_post_dates ( $ last_modified_posts );
@@ -697,7 +678,7 @@ public static function update_sitemap_from_modified_posts() {
697678
698679 do_action ( 'msm_update_sitemap_for_year_month_date ' , array ( $ year , $ month , $ day ), $ time );
699680 }
700- update_option ( 'msm_sitemap_update_last_run ' , current_time ( 'timestamp ' , 1 ), false );
681+ update_option ( 'msm_sitemap_update_last_run ' . self :: get_partition_suffix () , current_time ( 'timestamp ' , 1 ), false );
701682 }
702683
703684 /**
0 commit comments