status(); } /** * Show index status counts. */ public function status() { $rows = array(); foreach ( MediaXray_Index::counts() as $st => $c ) { $rows[] = array( 'status' => $st, 'files' => $c['n'], 'size' => size_format( $c['bytes'] ) ); } WP_CLI\Utils\format_items( 'table', $rows, array( 'status', 'files', 'size' ) ); } /** * List attachments by status. * * ## OPTIONS * [--status=] * : used, global, review, unused * --- * default: unused * --- * [--format=] * --- * default: table * --- */ public function list_( $args, $assoc ) { global $wpdb; $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT attachment_id id, status, refs, sources, bytes, flags FROM %i' . ' WHERE status = %s ORDER BY bytes DESC', MediaXray_DB::t( 'summary' ), $assoc['status'] ), ARRAY_A ); // phpcs:ignore foreach ( $rows as &$r ) { $r['file'] = wp_basename( (string) get_attached_file( $r['id'] ) ); } WP_CLI\Utils\format_items( $assoc['format'], $rows, array( 'id', 'file', 'status', 'refs', 'sources', 'bytes', 'flags' ) ); } /** * Show where an attachment is used. * * * : Attachment ID */ public function usage( $args ) { $d = MediaXray_REST::usage_data( (int) $args[0] ); WP_CLI::log( sprintf( '%s — %s — %d references, %d unique content', $d['file'], $d['label'], $d['refs'], $d['sources'] ) ); foreach ( $d['groups'] as $g ) { WP_CLI::log( ' ' . $g['title'] . ' (' . $g['type'] . ( $g['live'] ? '' : ', not live' ) . ')' ); foreach ( $g['refs'] as $r ) { WP_CLI::log( ' ' . $r['label'] . ' [' . $r['locator'] . ']' ); } } } /** * Deep-verify attachments. * * ... * : Attachment IDs */ public function verify( $args ) { foreach ( MediaXray_Verify::run( array_map( 'intval', $args ) ) as $id => $r ) { WP_CLI::log( $id . ': ' . ( $r['ok'] ? 'not found anywhere else' : count( $r['hits'] ) . ' hit(s): ' . implode( ', ', wp_list_pluck( $r['hits'], 'label' ) ) ) ); } } }