start(); $phase = ''; while ( 'done' !== $state['phase'] ) { if ( $state['phase'] !== $phase ) { $phase = $state['phase']; WP_CLI::log( ucfirst( $phase ) . '...' ); } $state = $scanner->step(); } $t = ( new Mediawell_Report() )->totals(); WP_CLI::success( sprintf( '%d media items. Unused: %d (%s). Possibly used: %d. Duplicate groups: %d (%s). Unregistered files: %d (%s). Missing files: %d.', $t['library'], $t['unused'], size_format( $t['unused_bytes'], 1 ), $t['possibly'], $t['groups'], size_format( $t['dupe_bytes'], 1 ), $t['files'], size_format( $t['files_bytes'], 1 ), $t['missing'] ) ); } /** * Show a list from the last scan. * * ## OPTIONS * * * : unused, possibly, duplicates, unregistered, missing or quarantine. * * [--format=] * : table, csv, json or count. * --- * default: table * --- * * ## EXAMPLES * * wp mediawell report unused --format=csv * * @param array $args Arguments. * @param array $assoc_args Options. */ public function report( $args, $assoc_args ) { $report = new Mediawell_Report(); $format = isset( $assoc_args['format'] ) ? $assoc_args['format'] : 'table'; switch ( $args[0] ) { case 'unused': case 'possibly': case 'missing': $items = 'unused' === $args[0] ? $report->unused() : ( 'possibly' === $args[0] ? $report->possibly_used() : $report->missing() ); $rows = array(); foreach ( $items as $item ) { $rows[] = array( 'id' => $item['id'], 'title' => $item['title'], 'file' => get_post_meta( $item['id'], '_wp_attached_file', true ), 'bytes' => $item['bytes'], 'used' => $item['used'], 'found' => $item['deep'], ); } $fields = array( 'id', 'title', 'file', 'bytes', 'used', 'found' ); break; case 'duplicates': $rows = array(); foreach ( $report->duplicates() as $n => $group ) { foreach ( $group['ids'] as $id ) { $rows[] = array( 'group' => $n + 1, 'type' => $group['type'], 'id' => $id, 'file' => get_post_meta( $id, '_wp_attached_file', true ), 'keep' => $id === $group['keeper'] ? 'yes' : '', ); } } $fields = array( 'group', 'type', 'id', 'file', 'keep' ); break; case 'unregistered': $rows = $report->unregistered(); $fields = array( 'path', 'bytes' ); break; case 'quarantine': $rows = Mediawell_DB::qitems(); $fields = array( 'id', 'label', 'attachment_id', 'merged_into', 'bytes', 'created', 'hits' ); break; default: WP_CLI::error( 'Unknown list. Use unused, possibly, duplicates, unregistered, missing or quarantine.' ); return; } if ( 'count' === $format ) { WP_CLI::log( (string) count( $rows ) ); return; } WP_CLI\Utils\format_items( $format, $rows, $fields ); } }