*/ public function scan(): array { $result = array( 'time' => time(), 'counts' => $this->counts(), ); update_option( self::SCAN_OPTION, $result, false ); return $result; } /** * Returns the last scan, or null if the library was never scanned. * * @return array|null */ public function last_scan(): ?array { $scan = get_option( self::SCAN_OPTION ); return is_array( $scan ) ? $scan : null; } /** * Counts supported images by format and status. * * @return array{total: int, pending: int, by_format: array, by_status: array, unsupported: int} */ public function counts(): array { global $wpdb; $placeholders = implode( ',', array_fill( 0, count( self::SUPPORTED_MIMES ), '%s' ) ); $rows = $wpdb->get_results( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- Placeholders are generated above. "SELECT p.post_mime_type AS mime, COALESCE(m.meta_value, 'unprocessed') AS status, COUNT(*) AS total FROM {$wpdb->posts} p LEFT JOIN {$wpdb->postmeta} m ON m.post_id = p.ID AND m.meta_key = %s WHERE p.post_type = 'attachment' AND p.post_status <> 'trash' AND p.post_mime_type IN ({$placeholders}) GROUP BY p.post_mime_type, COALESCE(m.meta_value, 'unprocessed')", array_merge( array( AttachmentState::META_STATUS ), self::SUPPORTED_MIMES ) ), ARRAY_A ); $by_format = array(); $by_status = array(); $total = 0; foreach ( is_array( $rows ) ? $rows : array() as $row ) { $format = str_replace( 'image/', '', (string) $row['mime'] ); $count = (int) $row['total']; $by_format[ $format ] = ( $by_format[ $format ] ?? 0 ) + $count; $by_status[ (string) $row['status'] ] = ( $by_status[ (string) $row['status'] ] ?? 0 ) + $count; $total += $count; } $unsupported = (int) $wpdb->get_var( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- Placeholders are generated above. "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = 'attachment' AND post_status <> 'trash' AND post_mime_type LIKE %s AND post_mime_type NOT IN ({$placeholders})", array_merge( array( $wpdb->esc_like( 'image/' ) . '%' ), self::SUPPORTED_MIMES ) ) ); return array( 'total' => $total, 'pending' => ( $by_status['unprocessed'] ?? 0 ) + ( $by_status['pending'] ?? 0 ), 'by_format' => $by_format, 'by_status' => $by_status, 'unsupported' => $unsupported, ); } /** * Returns IDs in a scope, in ascending order. * * @param string $scope One of the SCOPE constants. * @param int $after_id Exclusive lower bound. * @param int $limit Maximum number of IDs. * @return int[] */ public function ids_in_scope( string $scope, int $after_id, int $limit ): array { global $wpdb; $condition = $this->scope_condition( $scope ); $placeholders = implode( ',', array_fill( 0, count( self::SUPPORTED_MIMES ), '%s' ) ); $ids = $wpdb->get_col( // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- The condition comes from a fixed list; placeholders are generated above. $wpdb->prepare( "SELECT p.ID FROM {$wpdb->posts} p LEFT JOIN {$wpdb->postmeta} m ON m.post_id = p.ID AND m.meta_key = %s WHERE p.post_type = 'attachment' AND p.post_status <> 'trash' AND p.post_mime_type IN ({$placeholders}) AND {$condition} AND p.ID > %d ORDER BY p.ID ASC LIMIT %d", array_merge( array( AttachmentState::META_STATUS ), self::SUPPORTED_MIMES, array( $after_id, $limit ) ) ) ); return array_map( 'intval', is_array( $ids ) ? $ids : array() ); } /** * Counts the images in a scope. * * @param string $scope One of the SCOPE constants. * @return int */ public function count_scope( string $scope ): int { global $wpdb; $condition = $this->scope_condition( $scope ); $placeholders = implode( ',', array_fill( 0, count( self::SUPPORTED_MIMES ), '%s' ) ); return (int) $wpdb->get_var( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- The condition comes from a fixed list; placeholders are generated above. "SELECT COUNT(*) FROM {$wpdb->posts} p LEFT JOIN {$wpdb->postmeta} m ON m.post_id = p.ID AND m.meta_key = %s WHERE p.post_type = 'attachment' AND p.post_status <> 'trash' AND p.post_mime_type IN ({$placeholders}) AND {$condition}", array_merge( array( AttachmentState::META_STATUS ), self::SUPPORTED_MIMES ) ) ); } /** * Picks a random sample of unprocessed images for the savings estimate. * * @param int $size Sample size. * @return int[] */ public function sample_pending_ids( int $size ): array { $pool = $this->ids_in_scope( self::SCOPE_PENDING, 0, 500 ); shuffle( $pool ); return array_slice( $pool, 0, $size ); } /** * Returns images marked pending. With no jobs in flight, these are jobs that were lost. * * @param int $limit Maximum number of IDs. * @param string $key Status meta key. * @return int[] */ public function marked_pending_ids( int $limit, string $key = AttachmentState::META_STATUS ): array { global $wpdb; $ids = $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value = 'pending' ORDER BY post_id ASC LIMIT %d", $key, $limit ) ); return array_map( 'intval', is_array( $ids ) ? $ids : array() ); } /** * Counts a bulk run's processed images by status. * * Derived from durable post meta, so totals stay correct across retries and restarts. * * @param string $run_id Run ID. * @param string $key Status meta key. * @return array */ public function run_counts( string $run_id, string $key = AttachmentState::META_STATUS ): array { global $wpdb; $rows = $wpdb->get_results( $wpdb->prepare( "SELECT s.meta_value AS status, COUNT(*) AS total FROM {$wpdb->postmeta} r JOIN {$wpdb->postmeta} s ON s.post_id = r.post_id AND s.meta_key = %s WHERE r.meta_key = %s AND r.meta_value = %s GROUP BY s.meta_value", $key, AttachmentState::META_RUN, $run_id ), ARRAY_A ); $counts = array(); foreach ( is_array( $rows ) ? $rows : array() as $row ) { $counts[ (string) $row['status'] ] = (int) $row['total']; } return $counts; } /** * Returns the SQL condition for a scope. Only ever produces fixed strings. * * @param string $scope One of the SCOPE constants. * @return string */ private function scope_condition( string $scope ): string { if ( self::SCOPE_FAILED === $scope ) { return "m.meta_value = 'failed'"; } if ( self::SCOPE_RESTORED === $scope ) { return "m.meta_value = 'restored'"; } if ( self::SCOPE_CONVERT === $scope ) { global $wpdb; // Restored and skipped images stay as they are. The hash re-includes images when conversion settings change. return $wpdb->prepare( "(m.meta_value IS NULL OR m.meta_value NOT IN ('restored', 'skipped', 'failed')) AND NOT EXISTS (SELECT 1 FROM {$wpdb->postmeta} c WHERE c.post_id = p.ID AND c.meta_key = %s AND c.meta_value = %s)", AttachmentState::META_CONVERTED, $this->settings->conversion_hash() ); } return "(m.meta_value IS NULL OR m.meta_value = 'pending')"; } }