Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Plugin Name: Multisite Network Upgrade WP-CLI Command
* Plugin URI: https://gist.github.com/westonruter/beb6120e61e9691e88ac
* Author: Weston Ruter, Mo Jangda
* Description: From command line run <code>wp multisite-network-upgrade</code>. Script converted into WP-CLI command from Mo's <a href="https://gist.github.com/mjangda/986838">CLI script</a>.
* License: GPL2
*/
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
return;
}
class Multisite_Network_Upgrade_WP_CLI_Command extends WP_CLI_Command {
/**
* Upgrade all sites on the network.
*
* ## EXAMPLES
*
* wp multisite-network-upgrade
*/
function __invoke() {
global $wpdb;
if ( ! is_multisite() ) {
WP_CLI::error( __( 'Multisite is not enabled.' ) );
}
global $wp_db_version;
update_site_option( 'wpmu_upgrade_site', $wp_db_version );
$success_count = 0;
$failure_count = 0;
$n = 0;
do {
$blogs = $wpdb->get_results( "SELECT * FROM {$wpdb->blogs} WHERE site_id = '{$wpdb->siteid}' AND spam = '0' AND deleted = '0' AND archived = '0' ORDER BY registered DESC LIMIT {$n}, 5", ARRAY_A ); // db call ok, no cache ok
foreach ( (array) $blogs as $details ) {
$siteurl = get_blog_option( $details['blog_id'], 'siteurl' );
// Do the actual upgrade
$response = wp_remote_get( trailingslashit( $siteurl ) . 'wp-admin/upgrade.php?step=upgrade_db', array( 'timeout' => 120, 'httpversion' => '1.1' ) );
if ( is_wp_error( $response ) ) {
WP_CLI::warning( sprintf( __( '%1$s: Your server may not be able to connect to sites running on it. Error message: %2$s' ), $siteurl, $response->get_error_message() ) );
$failure_count += 1;
} else {
WP_CLI::success( sprintf( __( 'Upgraded %s' ), $siteurl ) );
$success_count += 1;
do_action( 'after_mu_upgrade', $response );
do_action( 'wpmu_upgrade_site', $details['blog_id'] );
}
}
$n += 5;
} while ( empty( $blogs ) );
if ( 0 !== $failure_count ) {
WP_CLI::warning( sprintf( __( 'All done! Upgraded %1$d site(s). Failed upgrading %2$s site(s).' ), $success_count, $failure_count ) );
} else if ( 0 !== $success_count ) {
WP_CLI::success( sprintf( __( 'Complete. Upgraded %1$d site(s).' ), $success_count ) );
} else {
WP_CLI::warning( __( 'No sites upgraded.' ) );
}
}
}
WP_CLI::add_command( 'multisite-network-upgrade', 'Multisite_Network_Upgrade_WP_CLI_Command' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.