Plugin Directory


Ignore:
Timestamp:
10/17/2026 09:24:28 PM (13 hours ago)
Author:
rebelcode
Message:

Update to version 0.4.0 from GitHub

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sigil-2fa/trunk/includes/class-cli.php

    r3634087 r3752373  
    146146
    147147        /**
     148         * Import users' two-factor methods from another plugin.
     149         *
     150         * Reads the source plugin's data and never changes or deletes it. Anything
     151         * already set up in Sigil is left alone, so it is safe to run again.
     152         *
     153         * ## OPTIONS
     154         *
     155         * <source>
     156         * : Plugin to import from.
     157         * ---
     158         * options:
     159         *   - two-factor
     160         * ---
     161         *
     162         * [--dry-run]
     163         * : Report what would be imported without writing anything.
     164         *
     165         * [--user=<user>]
     166         * : Only this user (ID, login, or email).
     167         *
     168         * ## EXAMPLES
     169         *
     170         *     wp sigil import two-factor --dry-run
     171         *     wp sigil import two-factor
     172         *     wp sigil import two-factor --user=12
     173         *
     174         * @param list<string>               $args
     175         * @param array<string, string|bool> $assoc_args
     176         */
     177        public function import( array $args, array $assoc_args = [] ): void {
     178                if ( 'two-factor' !== ( $args[0] ?? '' ) ) {
     179                        \WP_CLI::error( 'Unknown source. Supported: two-factor' );
     180                }
     181
     182                $dry_run = ! empty( $assoc_args['dry-run'] );
     183                $ids     = null;
     184
     185                if ( isset( $assoc_args['user'] ) ) {
     186                        $user = $this->resolve_user( (string) $assoc_args['user'] );
     187                        if ( null === $user ) {
     188                                \WP_CLI::error( 'User not found.' );
     189                        }
     190                        $ids = [ (int) $user->ID ];
     191                }
     192
     193                $report = Two_Factor_Import::run( $ids, $dry_run );
     194
     195                $items = [];
     196                foreach ( $report['users'] as $row ) {
     197                        $items[] = [
     198                                'ID'        => $row['user_id'],
     199                                'login'     => $row['login'],
     200                                'status'    => $row['status'],
     201                                'imported'  => implode( ',', $row['imported'] ),
     202                                'already'   => implode( ',', $row['already'] ),
     203                                'preferred' => $row['preferred'],
     204                                'flags'     => implode( ',', $row['flags'] ),
     205                        ];
     206                }
     207
     208                if ( [] !== $items ) {
     209                        \WP_CLI\Utils\format_items( 'table', $items, [ 'ID', 'login', 'status', 'imported', 'already', 'preferred', 'flags' ] );
     210                }
     211
     212                $c = $report['counts'];
     213                \WP_CLI::log(
     214                        sprintf(
     215                                '%sUsers: %d, not using Two Factor: %d, email: %d, authenticator: %d, backup codes: %d, already in Sigil: %d, flagged: %d, needs attention: %d',
     216                                $dry_run ? '[dry run] ' : '',
     217                                $c['users'],
     218                                $c['not_using'],
     219                                $c['email'],
     220                                $c['totp'],
     221                                $c['backup'],
     222                                $c['already'],
     223                                $c['flagged'],
     224                                $c['attention']
     225                        )
     226                );
     227
     228                if ( $c['attention'] > 0 ) {
     229                        \WP_CLI::warning( 'Some users had a method Sigil could not carry over. Two Factor keeps protecting them while it is active, so leave it active until they have set up a method in Sigil.' );
     230                        return;
     231                }
     232
     233                \WP_CLI::success( $dry_run ? 'Dry run complete. Nothing was written.' : 'Import complete. Two Factor data was left in place; deactivate Two Factor once you have checked the result.' );
     234        }
     235
     236        /**
    148237         * @return list<string>
    149238         */
Note: See TracChangeset for help on using the changeset viewer.