'administrator', 'number' => 1, 'fields' => 'ID' ] ); if ( $admins ) { wp_set_current_user( (int) $admins[0] ); } } $ability = function_exists( 'wp_get_ability' ) ? wp_get_ability( $name ) : null; if ( ! $ability ) { \WP_CLI::error( "Unknown ability $name." ); } $result = $ability->execute( $input ); if ( is_wp_error( $result ) ) { \WP_CLI::error( $result->get_error_message() ); } \WP_CLI::line( (string) wp_json_encode( $result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) ); } public function undo( array $args ): void { $result = Changes::undo( (string) ( $args[0] ?? '' ) ); if ( is_wp_error( $result ) ) { \WP_CLI::error( $result->get_error_message() ); } \WP_CLI::success( sprintf( 'Restored %d operation(s).', (int) $result['restored'] ) ); } public function log( array $args, array $assoc ): void { $rows = Changes::recent( (int) ( $assoc['limit'] ?? 20 ), true ); \WP_CLI\Utils\format_items( 'table', $rows, [ 'id', 'created_at', 'user_id', 'ability', 'change_id', 'summary', 'applied_at', 'undone_at', 'undo_token' ] ); } /** * @subcommand migrate-roosterx */ public function migrate_roosterx( array $args, array $assoc ): void { $dry = ! empty( $assoc['dry-run'] ); $report = \Rooster\Core\Compat\Roosterx::migrate( $dry ); foreach ( $report as $what => $count ) { \WP_CLI::line( sprintf( '%-22s %d', $what, $count ) ); } \WP_CLI::success( $dry ? 'Dry run: nothing changed.' : 'Migrated from roosterx.' ); } public function perf( array $args ): void { $action = (string) ( $args[0] ?? '' ); if ( 'import' !== $action ) { \WP_CLI::error( 'Usage: wp rooster perf import ' ); } $file = (string) ( $args[1] ?? '' ); if ( ! is_readable( $file ) ) { \WP_CLI::error( "Cannot read $file" ); } $data = json_decode( (string) file_get_contents( $file ), true ); if ( ! is_array( $data ) || empty( $data['results'] ) ) { \WP_CLI::error( 'Not a lighthouse-matrix report.' ); } $store = [ 'generated' => (string) ( $data['generated'] ?? '' ), 'label' => (string) ( $data['label'] ?? '' ), 'lighthouse' => (string) ( $data['lighthouse'] ?? '' ), 'pass' => (bool) ( $data['pass'] ?? false ), 'results' => [] ]; foreach ( (array) $data['results'] as $r ) { $store['results'][] = [ 'url' => (string) $r['url'], 'device' => (string) $r['device'], 'scores' => (array) $r['scores'], 'metrics' => (array) ( $r['metrics'] ?? [] ), 'failing' => array_column( (array) ( $r['failing'] ?? [] ), 'id' ) ]; } update_option( 'rooster_perf_last', $store, false ); \WP_CLI::success( sprintf( 'Imported %d result(s) from %s.', count( $store['results'] ), basename( $file ) ) ); } }