CLI Role Switching

<?php
        function mmg_roles_check() {
                global $wp_roles;
                if ( [is_multisite](http://codex.wordpress.org/Function_Reference/is_multisite)() ) {
                        $sites = [wp_get_sites](http://codex.wordpress.org/Function_Reference/wp_get_sites)();
                        if ( ! empty( $sites ) ) {
                                $blog_ids = [wp_list_pluck](http://codex.wordpress.org/Function_Reference/wp_list_pluck)( $sites, 'blog_id' );
                                foreach( $blog_ids as $blog_id ) {
                                        WP_CLI::line( "Checking roles for blog id " . $blog_id );

                                        [switch_to_blog](http://codex.wordpress.org/Function_Reference/switch_to_blog)( $blog_id );

                                        $current_roles = array_keys( $wp_roles->roles );
                                        WP_CLI::line( 'Current roles: ' . implode( ', ', $current_roles ) );

                                        $keep_roles = array( 'administrator', 'editor', 'author', 'ad_editor', 'attributed' );
                                        WP_CLI::line( 'Roles to keep: ' . implode( ',', $keep_roles ) );

                                        $deleted_these_roles = array_diff( $current_roles, $keep_roles );
                                        WP_CLI::line( 'Roles to delete: ' . implode( ',', $deleted_these_roles ) );

                                        foreach ( $current_roles as $role ) {
                                                if ( ! $this->mmg_role_conversion( $role ) ) {
                                                        WP_CLI::error( $role . " was not found in the conversion table. FIX IT or else ...");
                                                }
                                        }
                                        [restore_current_blog](http://codex.wordpress.org/Function_Reference/restore_current_blog)();
                                }
                                [update_site_option](http://codex.wordpress.org/Function_Reference/update_site_option)( 'roles_stability_check', true );
                        } else {
                                WP_CLI::error( "Can't get sites list. Roles where NOT reset." );
                        }
                } else {
                        $current_roles = array_keys( $wp_roles->roles );
                        WP_CLI::line( 'Current roles: ' . implode( ', ', $current_roles ) );

                        $keep_roles = array(
                                'administrator',
                                'editor',
                                'author',
                                'ad_editor',
                                'attributed',
                        );
                        WP_CLI::line( 'Roles to keep: ' . implode( ',', $keep_roles ) );

                        $deleted_these_roles = array_diff( $current_roles, $keep_roles );
                        WP_CLI::line( 'Roles to delete: ' . implode( ',', $deleted_these_roles ) );
                        foreach ( $current_roles as $role ) {
                                if ( ! $this->mmg_role_conversion( $role ) ) {
                                        WP_CLI::error( $role . " was not found in the conversion table. FIX IT or else ...");
                                }
                        }
                        [update_option](http://codex.wordpress.org/Function_Reference/update_option)( 'roles_stability_check', true );
                }
                WP_CLI::success( "Good to go ..." );
        }

        /*
         * Reset ALL user roles to MMG's defaults. Deletes any custom roles.
         *
         * MMG roles:
         * administrator,
         * editor (deputy editor),
         * author (author / freelancer),
         * ad_editor,
         * attributed (no access)
         *
         */
         function mmg_set_user_roles() {

                if ( [is_multisite](http://codex.wordpress.org/Function_Reference/is_multisite)() ) {
                        $stability_check = [get_site_option](http://codex.wordpress.org/Function_Reference/get_site_option)( 'roles_stability_check' );
                        if ( ! $stability_check )
                                WP_CLI::error( "Do mmg_roles_check FIRST!!!!" );

                        [update_site_option](http://codex.wordpress.org/Function_Reference/update_site_option)( 'roles_stability_check', false);

                        WP_CLI::line( 'Running in Multisite mode' );

                        $sites = [wp_get_sites](http://codex.wordpress.org/Function_Reference/wp_get_sites)();
                        if ( ! empty( $sites ) ) {
                                $blog_ids = [wp_list_pluck](http://codex.wordpress.org/Function_Reference/wp_list_pluck)( $sites, 'blog_id' );
                                foreach( $blog_ids as $blog_id ) {
                                        WP_CLI::line( "Fixing roles for blog id " . $blog_id );

                                        [switch_to_blog](http://codex.wordpress.org/Function_Reference/switch_to_blog)( $blog_id );
                                        $this->mmg_reset_roles();

                                        [restore_current_blog](http://codex.wordpress.org/Function_Reference/restore_current_blog)();
                                }
                        } else {
                                WP_CLI::error( "Can't get sites list. Roles where NOT reset." );
                        }

                } else {
                        $stability_check = [get_option](http://codex.wordpress.org/Function_Reference/get_option)( 'roles_stability_check' );
                        if ( ! $stability_check )
                                WP_CLI::error( "Do mmg_roles_check FIRST!!!!" );

                        [update_option](http://codex.wordpress.org/Function_Reference/update_option)( 'roles_stability_check', false );

                        WP_CLI::line( 'Running in Single mode' );
                        $this->mmg_reset_roles();
                }
        }

        private function mmg_role_conversion( $role ) {
                $conversion = array(

                        //ORIGINAL ROLE         => Convert to this role

                        // ...
                        'attributed' => 'attributed',

                        // Default wordpress roles
                        'administrator'         => 'administrator',
                        'editor'                => 'editor',
                        'author'                => 'author',
                        'contributor'           => 'attributed',
                        'subscriber'            => 'attributed',

                        // Extra roles
                        'ad_editor'             => 'ad_editor',
                        'ad_content_editor'     => 'ad_editor',
                        'content_editor'        => 'editor',
                );

                if ( isset( $conversion[ $role ] ) )
                        return $conversion[ $role ];
                else
                        return false;
        }

        private function mmg_reset_roles() {
                global $wp_roles;

                $current_roles = array_keys( $wp_roles->roles );
                WP_CLI::line( 'Current roles: ' . implode( ', ', $current_roles ) );

                $keep_roles = array(
                        'administrator',
                        'editor',
                        'author',
                        'ad_editor',
                        'attributed',
                );
                WP_CLI::line( 'Roles to keep: ' . implode( ',', $keep_roles ) );

                $deleted_these_roles = array_diff( $current_roles, $keep_roles );
                WP_CLI::line( 'Roles to delete: ' . implode( ',', $deleted_these_roles ) );
                $admin = [get_role](http://codex.wordpress.org/Function_Reference/get_role)( 'administrator' );
                $admin->[add_cap](http://codex.wordpress.org/Function_Reference/add_cap)( 'edit_ads' );
                $admin->[add_cap](http://codex.wordpress.org/Function_Reference/add_cap)( 'curate' );
                $admin->[add_cap](http://codex.wordpress.org/Function_Reference/add_cap)( 'unfiltered_html' );


                $editor_caps_mod = $wp_roles->roles[ 'editor' ][ 'capabilities' ];
                $editor_caps_mod[ 'curate' ] = true;
                $editor_caps_mod[ 'manage_options' ] = true;
                [remove_role](http://codex.wordpress.org/Function_Reference/remove_role)( 'editor' );
                [add_role](http://codex.wordpress.org/Function_Reference/add_role)( 'editor', 'Deputy Editor', $editor_caps_mod );


                $author_caps_mod = $wp_roles->roles[ 'author' ][ 'capabilities' ];
                $author_caps_mod[ 'delete_posts' ] = false;
                $author_caps_mod[ 'publish_posts' ] = false;
                [remove_role](http://codex.wordpress.org/Function_Reference/remove_role)( 'author' );
                [add_role](http://codex.wordpress.org/Function_Reference/add_role)( 'author', 'Author / Freelancer', $author_caps_mod );

                [remove_role](http://codex.wordpress.org/Function_Reference/remove_role)( 'ad_editor' );
                $ad_editor_caps = $editor_caps_mod;
                $ad_editor_caps[ 'edit_ads' ] = true;
                [add_role](http://codex.wordpress.org/Function_Reference/add_role)( 'ad_editor', 'Ad Editor', $ad_editor_caps );

                [add_role](http://codex.wordpress.org/Function_Reference/add_role)( 'attributed', 'Attributed / No Access', array(
                        'read'                  => false,
                        'level0'                => false,
                ));

                $this->do_roles();

                foreach( $deleted_these_roles as $role ) {
                        [remove_role](http://codex.wordpress.org/Function_Reference/remove_role)( $role );
                        $test = [get_role](http://codex.wordpress.org/Function_Reference/get_role)( $role );

                        if ( null === $test )
                                WP_CLI::line( 'Successfully removed role ' . $role );
                        else
                                WP_CLI::warning( 'Error deleting role ' . $role );
                }

                [update_option](http://codex.wordpress.org/Function_Reference/update_option)( 'default_role', 'attributed' );
        }
        // Used in mmg_reset_roles()
        private function do_roles() {

                $users = [get_users](http://codex.wordpress.org/Function_Reference/get_users)();
                foreach ( $users as $user ) {
                        if ( count( $user->roles ) > 0 ) {

                                $user_roles = $user->roles;
                                $convert_to = $this->mmg_role_conversion( $user_roles[ 0 ] );   // Don't care about multiple roles ... just use the first one.

                                if ( $convert_to !== false ) {
                                        $user->set_role( $convert_to );
                                } else {
                                        WP_CLI::warning( $user->login . " was skipped. Current role(s): " . implode( ', ' , $user_roles ) );
                                }

                        } else {

                                $user->set_role( 'attributed' );
                                WP_CLI::line( "No role found for " . $user->user_login . " assigned to 'attributed'" );
                        }
                }
        }

        public function mmg_roles_display() {
                global $wp_roles;

                print_r( $wp_roles );
        }