] * : Render output in a particular format. * --- * default: table * options: * - table * - json * - yaml * - csv * --- * * ## EXAMPLES * * wp agent-toolbelt list * wp agent-toolbelt list --format=json * * @when after_wp_load * * @param array $atb_args * @param array $atb_assoc_args */ public function list( $atb_args, $atb_assoc_args ) { $atb_format = isset( $atb_assoc_args['format'] ) ? $atb_assoc_args['format'] : 'table'; $atb_rows = array(); foreach ( Agent_Toolbelt::get_operations( true ) as $atb_slug => $atb_operation ) { $atb_meta = $atb_operation->describe(); $atb_rows[] = array( 'ability' => Agent_Toolbelt_Abilities::NAMESPACE_PREFIX . $atb_slug, 'risk' => isset( $atb_meta['risk'] ) ? (string) $atb_meta['risk'] : '', 'enabled' => Agent_Toolbelt::is_ability_disabled( $atb_slug ) ? 'no' : 'yes', ); } WP_CLI\Utils\format_items( $atb_format, $atb_rows, array( 'ability', 'risk', 'enabled' ) ); } /** * Runs an ability by slug, mirroring the REST/MCP execute() semantics * (including the dry-run / confirm flow). * * ## OPTIONS * * * : Ability slug, with or without the `agent-toolbelt/` prefix. * * [--input=] * : JSON object of ability input, e.g. '{"dry_run":true}'. * * [--porcelain] * : Print the result as single-line JSON (machine-friendly). * * ## EXAMPLES * * wp agent-toolbelt run site-status * wp agent-toolbelt run purge-all-caches --input='{"dry_run":true}' * wp agent-toolbelt run maintenance-mode --input='{"enable":true,"duration_minutes":10}' * * @when after_wp_load * * @param array $atb_args * @param array $atb_assoc_args */ public function run( $atb_args, $atb_assoc_args ) { $atb_slug = self::normalize_slug( isset( $atb_args[0] ) ? $atb_args[0] : '' ); if ( '' === $atb_slug || null === Agent_Toolbelt::get_operation( $atb_slug ) || Agent_Toolbelt::is_ability_disabled( $atb_slug ) ) { WP_CLI::error( sprintf( 'Unknown or disabled ability: %s', $atb_slug ) ); return; } $atb_input = array(); if ( isset( $atb_assoc_args['input'] ) && '' !== $atb_assoc_args['input'] ) { $atb_input = json_decode( $atb_assoc_args['input'], true ); if ( ! is_array( $atb_input ) ) { WP_CLI::error( 'Invalid --input: expected a JSON object.' ); return; } } $atb_output = Agent_Toolbelt::execute( $atb_slug, $atb_input, 'cli' ); if ( ! empty( $atb_assoc_args['porcelain'] ) ) { WP_CLI::line( wp_json_encode( $atb_output ) ); return; } WP_CLI::line( wp_json_encode( $atb_output, JSON_PRETTY_PRINT ) ); } /** * Reads the audit log, newest first. * * ## OPTIONS * * [--limit=] * : How many entries to return (1-100). * --- * default: 20 * --- * * [--ability=] * : Filter to one ability slug. * * [--result=] * : Filter by result. * --- * options: * - ok * - error * - rolled_back * - denied * --- * * [--format=] * : Render output in a particular format. * --- * default: table * options: * - table * - json * - yaml * - csv * --- * * ## EXAMPLES * * wp agent-toolbelt log * wp agent-toolbelt log --limit=50 --format=json * wp agent-toolbelt log --ability=purge-all-caches * wp agent-toolbelt log --result=denied * * @when after_wp_load * * @param array $atb_args * @param array $atb_assoc_args */ public function log( $atb_args, $atb_assoc_args ) { $atb_read_args = array( 'limit' => isset( $atb_assoc_args['limit'] ) ? (int) $atb_assoc_args['limit'] : 20, ); if ( ! empty( $atb_assoc_args['ability'] ) ) { $atb_read_args['ability'] = self::normalize_slug( $atb_assoc_args['ability'] ); } if ( ! empty( $atb_assoc_args['result'] ) ) { $atb_read_args['result'] = (string) $atb_assoc_args['result']; } $atb_format = isset( $atb_assoc_args['format'] ) ? $atb_assoc_args['format'] : 'table'; $atb_rows = Agent_Toolbelt_Log::read( $atb_read_args ); $atb_columns = array( 'created_at', 'ability', 'caller', 'user_id', 'dry_run', 'result', 'duration_ms', 'summary' ); if ( empty( $atb_rows ) ) { WP_CLI::success( 'No audit-log entries yet.' ); return; } WP_CLI\Utils\format_items( $atb_format, $atb_rows, $atb_columns ); } /** * Strips the ability namespace prefix from a slug argument. * * @param string $atb_slug * @return string */ protected static function normalize_slug( $atb_slug ) { $atb_slug = (string) $atb_slug; $atb_prefix = Agent_Toolbelt_Abilities::NAMESPACE_PREFIX; if ( 0 === strpos( $atb_slug, $atb_prefix ) ) { $atb_slug = substr( $atb_slug, strlen( $atb_prefix ) ); } return $atb_slug; } } }