] * : table, json, csv. Default table. * * [--fail-on=] * : Exit with code 1 if any finding at or above this severity exists (CI / deploy gates). * * @param array $args Args. * @param array $assoc Assoc. */ public function scan( $args, $assoc ) { if ( ! Sunsetly_Settings::remote_allowed() ) { WP_CLI::warning( 'Online checks are off, so only local checks will run. Enable them under Sunsetly > Settings.' ); } $r = Sunsetly_Scanner::run( array( 'trigger' => 'cli' ) ); if ( is_wp_error( $r ) ) { WP_CLI::error( $r->get_error_message() ); } $this->output( $r, $assoc ); } /** * Show the last report without rescanning. * * [--format=] * : table, json, csv. * * [--fail-on=] * : Exit 1 if a finding at or above this severity exists. * * @param array $args Args. * @param array $assoc Assoc. */ public function status( $args, $assoc ) { $r = Sunsetly_Scanner::report(); if ( ! $r ) { WP_CLI::error( 'No scan yet. Run: wp sunsetly scan' ); } $this->output( $r, $assoc ); } /** * Output. * * @param array $r Report. * @param array $assoc Assoc. */ private function output( $r, $assoc ) { $format = isset( $assoc['format'] ) ? $assoc['format'] : 'table'; if ( 'json' === $format ) { WP_CLI::line( wp_json_encode( $r, JSON_PRETTY_PRINT ) ); } else { WP_CLI::line( sprintf( 'Grade %s (%d/100) — PHP %s, WordPress %s', $r['grade'], $r['score'], $r['env']['php'], $r['env']['wp'] ) ); $rows = array_map( function ( $f ) { return array( 'severity' => $f['severity'], 'category' => $f['category'], 'title' => $f['title'], 'fix' => $f['fix'], ); }, $r['findings'] ); if ( $rows ) { WP_CLI\Utils\format_items( $format, $rows, array( 'severity', 'category', 'title', 'fix' ) ); } else { WP_CLI::success( 'No issues found.' ); } } if ( ! empty( $assoc['fail-on'] ) ) { $limit = Sunsetly_Util::severity_rank( $assoc['fail-on'] ); foreach ( $r['findings'] as $f ) { if ( Sunsetly_Util::severity_rank( $f['severity'] ) <= $limit && 'update' !== $f['category'] ) { WP_CLI::halt( 1 ); } } } } } WP_CLI::add_command( 'sunsetly', 'Sunsetly_CLI' );