Making WordPress.org


Ignore:
Timestamp:
04/24/2015 04:07:50 AM (11 years ago)
Author:
iandunn
Message:

WordCamp Misc: Add wp-cli command to flush rewrite rules on all sites.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/wcorg-misc.php

    r1231 r1514  
    158158}
    159159add_action( 'template_redirect', 'wcorg_subdomactories_redirect' );
     160
     161/*
     162 * Flush the rewrite rules on the current site.
     163 *
     164 * See WordCamp_Miscellaneous_Commands::flush_rewrite_rules_everywhere() for an explanation.
     165 *
     166 * Requires authentication because flush_rewrite_rules() is expensive and could be used as a DoS vector.
     167 */
     168function wcorg_flush_rewrite_rules() {
     169        if ( isset( $_GET['nonce'] ) && wp_verify_nonce( $_GET['nonce'], 'flush-rewrite-rules-everywhere-' . get_current_blog_id() ) ) {
     170                flush_rewrite_rules();
     171                wp_send_json_success( 'Rewrite rules have been flushed.' );
     172        } else {
     173                wp_send_json_error( 'You are not authorized to flush the rewrite rules.' );
     174        }
     175}
     176add_action( 'wp_ajax_wcorg_flush_rewrite_rules_everywhere',        'wcorg_flush_rewrite_rules' ); // This isn't used by the wp-cli command, but is useful for manual testing
     177add_action( 'wp_ajax_nopriv_wcorg_flush_rewrite_rules_everywhere', 'wcorg_flush_rewrite_rules' );
     178
     179
     180/*
     181 * WP-CLI Commands
     182 */
     183if ( defined( 'WP_CLI' ) && WP_CLI ) {
     184        /**
     185         * WordCamp.org Miscellaneous Commands
     186         */
     187        class WordCamp_Miscellaneous_Commands extends WP_CLI_Command {
     188                /**
     189                 * Flush rewrite rules on all sites.
     190                 *
     191                 * Periodically they break for various reasons and need to be reset on all sites. If we
     192                 * just called flush_rewrite_rules() inside a switch_to_blog() loop then each site's
     193                 * plugins wouldn't be loaded and the rewrite rules wouldn't be correct.
     194                 *
     195                 * So instead, this issues an HTTP request to wcorg_flush_rewrite_rules() on each site so
     196                 * that flush_rewrite_rules() will run in the context of the loaded site.
     197                 *
     198                 * @subcommand flush-rewrite-rules-everywhere
     199                 */
     200                public function flush_rewrite_rules_everywhere() {
     201                        $start_timestamp = microtime( true );
     202                        $error           = '';
     203                        $sites           = wp_get_sites( array( 'limit' => false ) );
     204
     205                        WP_CLI::line();
     206
     207                        foreach ( $sites as $site ) {
     208                                $ajax_url    = sprintf( 'http://%s%swp-admin/admin-ajax.php', $site['domain'], $site['path'] );
     209                                $display_url = $site['domain'] . rtrim( $site['path'], '/' );
     210                                $nonce       = wp_create_nonce( 'flush-rewrite-rules-everywhere-' . $site['blog_id'] );
     211
     212                                $response = wp_remote_get( esc_url_raw( add_query_arg(
     213                                        array(
     214                                                'action' => 'wcorg_flush_rewrite_rules_everywhere',
     215                                                'nonce'  => $nonce,
     216                                        ),
     217                                        $ajax_url
     218                                ) ) );
     219
     220                                if ( is_wp_error( $response ) ) {
     221                                        $success = false;
     222                                        $error   = $response->get_error_message();
     223                                } else {
     224                                        $response = json_decode( wp_remote_retrieve_body( $response ) );
     225
     226                                        if ( isset( $response->success ) && $response->success ) {
     227                                                $success = true;
     228                                        } else {
     229                                                $success = false;
     230                                                $error   = isset( $response->data ) ? $response->data : 'Unknown error';
     231                                        }
     232                                }
     233
     234                                if ( $success ) {
     235                                        WP_CLI::line( sprintf( '%s: Flushed', $display_url ) );
     236                                } else {
     237                                        WP_CLI::warning( sprintf( '%s: Failed with error: %s', $display_url, $error     ) );
     238                                }
     239                        }
     240                        $execution_time = microtime( true ) - $start_timestamp;
     241
     242                        WP_CLI::line();
     243                        WP_CLI::line( sprintf(
     244                                'Flushed all rewrite rules in %d minute(s) and %d second(s).',
     245                                floor( $execution_time / 60 ),
     246                                $execution_time % 60
     247                        ) );
     248                }
     249        }
     250
     251        WP_CLI::add_command( 'wcorg-misc', 'WordCamp_Miscellaneous_Commands' );
     252}
Note: See TracChangeset for help on using the changeset viewer.