Skip to content

Instantly share code, notes, and snippets.

@mjangda
Created January 14, 2017 00:09
Show Gist options
  • Save mjangda/466bded7ad9de17169b1bccfc4ffd454 to your computer and use it in GitHub Desktop.
Save mjangda/466bded7ad9de17169b1bccfc4ffd454 to your computer and use it in GitHub Desktop.
Fixer command for fixing broken sitemap dates
<?php
class MSM_Sitemap_Fixers_Command extends WP_CLI_Command {
/**
* Regenerate sitemaps with broken dates
*
* @subcommand fix-msm-sitemap-dates
*/
function fix_msm_sitemap_dates( $args, $assoc_args ) {
$defaults = array();
$assoc_args = wp_parse_args( $assoc_args, $defaults );
// Find sitemaps with "UTC" which was added to all the busted dates.
// Not perfect, but better than regenerating ALL sitemaps.
global $wpdb;
$broken_sitemaps = $wpdb->get_col( "SELECT post_title FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON ID = post_id
WHERE post_type = 'msm_sitemap'
AND meta_key = 'msm_sitemap_xml'
AND meta_value LIKE BINARY '%UTC%'" );
WP_CLI::line( 'Found and fixing ' . count( $broken_sitemaps ) . ' sitemaps' );
foreach ( $broken_sitemaps as $sitemap_date ) {
WP_CLI::line( '-- fixing ' . $sitemap_date );
list( $year, $month, $day ) = explode( '-', $sitemap_date );
$cmd = sprintf( '--url=%s msm-sitemap generate-sitemap-for-year-month-day --year=%d --month=%d --day=%d', escapeshellarg( home_url() ), $year, $month, $day );
WP_CLI::line( '--> ' . $cmd );
$output = WP_CLI::runcommand( $cmd );
WP_CLI::log( $output );
}
WP_CLI::success( 'All done!' );
}
}
WP_CLI::add_command( 'msm-sitemap-fixer', 'MSM_Sitemap_Fixers_Command' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment