function vipgo_generate_sitemap_for_year_month_day($args) { $year = $args['year']; $month = $args['month']; $day = $args['day']; $date_stamp = Metro_Sitemap::get_date_stamp($year, $month, $day); if (Metro_Sitemap::date_range_has_posts($date_stamp, $date_stamp)) { Metro_Sitemap::generate_sitemap_for_date($date_stamp); } else { Metro_Sitemap::delete_sitemap_for_date($date_stamp); } }
/** * Checks that the stats are correct for each individual created post */ function check_stats_for_created_posts() { $dates = array(); // Count the number of posts for each date foreach ($this->posts as $post) { $date = date_format(new DateTime($post['post_date']), 'Y-m-d'); if (isset($dates[$date])) { $dates[$date]++; } else { $dates[$date] = 1; } } // Check counts for each date foreach ($dates as $date => $count) { list($year, $month, $day) = explode('-', $date, 3); $indexed_url_count = Metro_Sitemap::get_indexed_url_count($year, $month, $day); if ($indexed_url_count != $count) { echo "\nExpected url count of {$indexed_url_count} but had count of {$count} on {$year}-{$month}-{$day}\n"; return false; } } return true; }
/** * Find the next day with posts to process * @param int $year * @param int $month * @param int $day * @return void, just updates options. */ public static function find_next_day_to_process($year, $month, $day) { $halt = get_option('msm_stop_processing') === true; if ($halt || !Metro_Sitemap::is_blog_public()) { // Allow user to bail out of the current process, doesn't remove where the job got up to // or If the blog became private while sitemaps were enabled, stop here. delete_option('msm_stop_processing'); delete_option('msm_sitemap_create_in_progress'); return; } update_option('msm_sitemap_create_in_progress', true); $days_being_processed = get_option('msm_days_to_process'); $months_being_processed = get_option('msm_months_to_process'); $years_being_processed = get_option('msm_years_to_process'); $total_days = count($days_being_processed); $total_months = count($months_being_processed); $total_years = count($years_being_processed); if ($total_days && $day > 1) { // Day has finished unset($days_being_processed[$total_days - 1]); update_option('msm_days_to_process', $days_being_processed); self::generate_sitemap_for_year_month(array('year' => $year, 'month' => $month)); } else { if ($total_months and $month > 1) { // Month has finished unset($months_being_processed[$total_months - 1]); delete_option('msm_days_to_process'); update_option('msm_months_to_process', $months_being_processed); self::generate_sitemap_for_year(array('year' => $year)); } else { if ($total_years > 1) { // Year has finished unset($years_being_processed[$total_years - 1]); delete_option('msm_days_to_process'); delete_option('msm_months_to_process'); update_option('msm_years_to_process', $years_being_processed); self::generate_full_sitemap(); } else { // We've finished - remove all options delete_option('msm_days_to_process'); delete_option('msm_months_to_process'); delete_option('msm_years_to_process'); delete_option('msm_sitemap_create_in_progress'); } } } }
/** * @subcommand generate-sitemap-for-year-month-day */ function generate_sitemap_for_year_month_day($args, $assoc_args) { if (empty($this->command)) { $this->command = 'day'; } $assoc_args = wp_parse_args($assoc_args, array('year' => false, 'month' => false, 'day' => false)); $year = intval($assoc_args['year']); $month = intval($assoc_args['month']); $day = intval($assoc_args['day']); $valid = $this->validate_year_month_day($year, $month, $day); if (is_wp_error($valid)) { WP_CLI::error($valid->get_error_message()); } WP_CLI::line(sprintf('Generating sitemap for %s-%s-%s', $year, $month, $day)); $date_stamp = Metro_Sitemap::get_date_stamp($year, $month, $day); if (Metro_Sitemap::date_range_has_posts($date_stamp, $date_stamp)) { Metro_Sitemap::generate_sitemap_for_date($date_stamp); // TODO: simplify; this function should accept the year, month, day and translate accordingly } }
/** * Render admin options page */ public static function render_sitemap_options_page() { if (!current_user_can('manage_options')) { wp_die(__('You do not have sufficient permissions to access this page.', 'metro-sitemaps')); } // Array of possible user actions $actions = apply_filters('msm_sitemap_actions', array()); // Start outputting html echo '<div class="wrap">'; screen_icon(); echo '<h2>' . __('Sitemap', 'metro-sitemaps') . '</h2>'; if (!self::is_blog_public()) { self::show_action_message(__('Oops! Sitemaps are not supported on private blogs. Please make your blog public and try again.', 'metro-sitemaps'), 'error'); echo '</div>'; return; } if (isset($_POST['action'])) { check_admin_referer('msm-sitemap-action'); foreach ($actions as $slug => $action) { if ($action['text'] !== $_POST['action']) { continue; } do_action('msm_sitemap_action-' . $slug); break; } } // All the settings we need to read to display the page $sitemap_create_in_progress = get_option('msm_sitemap_create_in_progress') === true; $sitemap_update_last_run = get_option('msm_sitemap_update_last_run'); // Determine sitemap status text $sitemap_create_status = apply_filters('msm_sitemap_create_status', $sitemap_create_in_progress ? __('Running', 'metro-sitemaps') : __('Not Running', 'metro-sitemaps')); ?> <div class="stats-container"> <div class="stats-box"><strong id="sitemap-count"><?php echo number_format(Metro_Sitemap::count_sitemaps()); ?> </strong><?php _e('Sitemaps', 'metro-sitemaps'); ?> </div> <div class="stats-box"><strong id="sitemap-indexed-url-count"><?php echo number_format(Metro_Sitemap::get_total_indexed_url_count()); ?> </strong><?php _e('Indexed URLs', 'metro-sitemaps'); ?> </div> <div class="stats-footer"><span><span class="noticon noticon-time"></span><?php _e('Updated', 'metro-sitemaps'); ?> <strong><?php echo human_time_diff($sitemap_update_last_run); ?> <?php _e('ago', 'metro-sitemaps'); ?> </strong></span></div> </div> <h3><?php _e('Latest Sitemaps', 'metro-sitemaps'); ?> </h3> <div class="stats-container stats-placeholder"></div> <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> <h3><?php _e('Generate', 'metro-sitemaps'); ?> </h3> <p><strong><?php _e('Sitemap Create Status:', 'metro-sitemaps'); ?> </strong> <?php echo esc_html($sitemap_create_status); ?> </p> <form action="<?php echo menu_page_url('metro-sitemap', false); ?> " method="post" style="float: left;"> <?php wp_nonce_field('msm-sitemap-action'); ?> <?php foreach ($actions as $action) { if (!$action['enabled']) { continue; } ?> <input type="submit" name="action" class="button-secondary" value="<?php echo esc_attr($action['text']); ?> "> <?php } ?> </form> </div> <div id="tooltip"><strong class="content"></strong> <?php _e('indexed urls', 'metro-sitemaps'); ?> </div> <?php }
function test_site_stats_for_deleted_post() { $today_str = date('Y') . '-' . date('m') . '-' . date('d'); // Insert a new post for today $this->test_base->create_dummy_posts(array($today_str . ' 00:00:00')); // Build sitemaps $this->test_base->build_sitemaps(); // Delete all posts (going backwards in time) $post_count = count($this->test_base->posts); $deleted_dates = array(); while ($post_count) { $last_post = array_pop($this->test_base->posts); wp_delete_post($last_post['ID'], true); $post_count -= 1; $deleted_dates[trim(substr($last_post['post_date'], 0, 10))] = 0; // Build sitemaps $this->test_base->build_sitemaps(); // Check stats $this->assertEquals($post_count, Metro_Sitemap::get_total_indexed_url_count()); // Check specific stats $this->assertTrue($this->test_base->check_stats_for_created_posts()); } // Make sure that there is nothing left over $this->assertEquals(0, Metro_Sitemap::count_sitemaps()); $this->assertEquals(0, Metro_Sitemap::get_total_indexed_url_count()); }
<?php if (!Metro_Sitemap::is_blog_public()) { wp_die(__('Sorry, this site is not public so sitemaps are not available.', 'msm-sitemap'), __('Sitemap Not Available', 'msm-sitemap'), array('response' => 404)); } $req_year = isset($_GET['yyyy']) ? intval($_GET['yyyy']) : false; $req_month = isset($_GET['mm']) ? intval($_GET['mm']) : false; $req_day = isset($_GET['dd']) ? intval($_GET['dd']) : false; header('Content-type: application/xml; charset=UTF-8'); $build_xml = Metro_Sitemap::build_xml(array('year' => $req_year, 'month' => $req_month, 'day' => $req_day)); if ($build_xml === false) { wp_die(__('Sorry, no sitemap available here.', 'msm-sitemap')); } else { echo $build_xml; }