Making WordPress.org

Changeset 7470


Ignore:
Timestamp:
07/16/2018 11:07:06 PM (8 years ago)
Author:
coreymckrill
Message:

WordCamp CLI: Add subcommand for changing a skip feature flag on a single site

Also update set-skip-feature-flag to use get_sites instead of wp_get_sites,
which is deprecated.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/wp-cli-commands/miscellaneous.php

    r4314 r7470  
    3939                $max_site_id = empty( $args[1] ) ? false : absint( $args[1] );
    4040                $dry_run     = isset( $assoc_args[ 'dry-run' ] );
    41                 $sites       = wp_get_sites( array( 'limit' => false ) );
     41                $sites       = get_sites( array( 'number' => 0 ) );
    4242                $notify      = new \cli\progress\Bar( 'Applying flag', count( $sites ) );
    4343                $results     = array();
     
    9292
    9393        /**
     94         * Get or modify the state of a skip-feature flag on a single site.
     95         *
     96         * See wcorg_skip_feature() for context.
     97         *
     98         * ## OPTIONS
     99         *
     100         * <command>
     101         * : The skip-feature command to execute on a site.
     102         * ---
     103         * options:
     104         *   - get
     105         *   - set
     106         *   - unset
     107         * ---
     108         *
     109         * <flag_name>
     110         * : The name of the flag to get or modify.
     111         *
     112         * <blog_id>
     113         * : The numeric ID of the site on which the skip-feature command will be executed.
     114         *
     115         * ## EXAMPLES
     116         *
     117         * wp wc-misc skip-feature-flag get wcb_viewport_initial_scale 437
     118         * wp wc-misc skip-feature-flag set wcb_viewport_initial_scale 437
     119         * wp wc-misc skip-feature-flag unset wcb_viewport_initial_scale 437
     120         *
     121         * @subcommand skip-feature-flag
     122         *
     123         * @param array $args
     124         *
     125         * @throws \WP_CLI\ExitException
     126         */
     127        public function skip_feature_flag( $args ) {
     128                $command   = ( $args[0] ) ?: '';
     129                $flag_name = ( $args[1] ) ?: '';
     130                $blog_id   = ( $args[2] ) ? absint( $args[2] ) : 0;
     131
     132                WP_CLI::line();
     133
     134                $commands = array( 'get', 'set', 'unset' );
     135
     136                if ( ! in_array( $command, $commands, true ) ) {
     137                        WP_CLI::error( 'Invalid command. Use `get`, `set`, or `unset` for the first argument.' );
     138                }
     139
     140                if ( ! $flag_name ) {
     141                        WP_CLI::error( 'Invalid flag name.' );
     142                }
     143
     144                if ( ! $blog_id ) {
     145                        WP_CLI::error( 'Invalid blog ID.' );
     146                }
     147
     148                switch_to_blog( $blog_id );
     149
     150                $flags = get_option( 'wordcamp_skip_features', array() );
     151
     152                switch ( $command ) {
     153                        case 'get':
     154                                if ( array_key_exists( $flag_name, $flags ) && true === $flags[ $flag_name ] ) {
     155                                        $message = sprintf(
     156                                                'The %s flag is SET on blog %d.',
     157                                                $flag_name,
     158                                                $blog_id
     159                                        );
     160                                } else {
     161                                        $message = sprintf(
     162                                                'The %s flag is NOT SET on blog %d.',
     163                                                $flag_name,
     164                                                $blog_id
     165                                        );
     166                                }
     167                                break;
     168                        case 'set':
     169                                $flags[ $flag_name ] = true;
     170                                update_option( 'wordcamp_skip_features', $flags );
     171                                $message = sprintf(
     172                                        'The %s flag was successfully SET for blog %d.',
     173                                        $flag_name,
     174                                        $blog_id
     175                                );
     176                                break;
     177                        case 'unset':
     178                                unset( $flags[ $flag_name ] );
     179                                update_option( 'wordcamp_skip_features', $flags );
     180                                $message = sprintf(
     181                                        'The %s flag was successfully UNSET for blog %d.',
     182                                        $flag_name,
     183                                        $blog_id
     184                                );
     185                                break;
     186                }
     187
     188                restore_current_blog();
     189
     190                WP_CLI::line( $message );
     191        }
     192
     193        /**
    94194         * Print a log with our custom entries formatted for humans
    95195         *
Note: See TracChangeset for help on using the changeset viewer.