In the order of $ids. */ public static function weigh( array $ids, bool $all = false ): array { $ids = array_values( array_unique( array_filter( array_map( 'absint', $ids ) ) ) ); if ( ! $ids ) { return array(); } // One query for the posts and one for their meta, not two per image. _prime_post_caches( $ids, false, true ); $rows = array(); foreach ( $ids as $id ) { $row = self::weigh_one( $id, $all ); if ( null !== $row ) { $rows[] = $row; } } return $rows; } /** * Heaviest first; of two that weigh the same, the older upload first, so the * order is the same every time. * * @param array $rows Rows from weigh(). * @param int $number Keep this many. 0 keeps them all. * @return array */ public static function rank( array $rows, int $number = 0 ): array { usort( $rows, static function ( array $a, array $b ): int { return array( $b['bytes'], $a['id'] ) <=> array( $a['bytes'], $b['id'] ); } ); return $number > 0 ? array_slice( $rows, 0, $number ) : $rows; } /** * The heaviest images across the library, or across part of it. * * @param array $filters What ATR_Attachments::query() takes: only_featured, filename, include. * @param bool $all Include images that are already optimised. * @param int $number Keep this many. 0 keeps them all. * @return array Ranked rows. */ public static function find( array $filters = array(), bool $all = false, int $number = 0 ): array { $ids = array_map( 'intval', wp_list_pluck( ATR_Attachments::query( $filters ), 'id' ) ); $rows = array(); foreach ( array_chunk( $ids, self::CHUNK ) as $chunk ) { $rows = array_merge( $rows, self::weigh( $chunk, $all ) ); /* Every post and its meta stays in the runtime cache once read, which on a library of tens of thousands is the memory limit long before the end. A row is all that is needed from each chunk. */ if ( function_exists( 'wp_cache_flush_runtime' ) ) { wp_cache_flush_runtime(); } } return self::rank( $rows, $number ); } /** * @param int $id Attachment id. * @param bool $all Include it when it is already optimised. * @return array|null Null when it is not listed. */ private static function weigh_one( int $id, bool $all ): ?array { $post = get_post( $id ); if ( ! $post instanceof WP_Post || 'attachment' !== $post->post_type || ! ATR_Optimizer::optimizes( (string) $post->post_mime_type ) ) { return null; } // An old import can leave anything here; the optimiser reads it the same way. $stored = wp_get_attachment_metadata( $id, true ); $metadata = is_array( $stored ) ? $stored : array(); $done = ! ATR_Optimizer::needs_optimizing( $metadata, (string) $post->post_mime_type ); if ( $done && ! $all ) { return null; } $file = get_attached_file( $id ); if ( ! $file || ! file_exists( $file ) ) { return null; } $bytes = 0; $files = 0; foreach ( ATR_Optimizer::paths( $file, $metadata ) as $path ) { if ( file_exists( $path ) ) { $bytes += (int) filesize( $path ); $files++; } } return array( 'id' => $id, 'file' => wp_basename( $file ), 'bytes' => $bytes, 'files' => $files, 'done' => $done, ); } }