validate_product( $product ); if ( $validation['valid'] ) { ++$valid_count; } else { foreach ( $validation['missing'] as $field ) { if ( ! isset( $missing_fields[ $field ] ) ) { $missing_fields[ $field ] = 0; } ++$missing_fields[ $field ]; } } // Check recommended fields. foreach ( $feed->get_recommended_fields() as $field ) { if ( empty( $product[ $field ] ) ) { if ( ! isset( $warnings[ $field ] ) ) { $warnings[ $field ] = 0; } ++$warnings[ $field ]; } } } $coverage = $total > 0 ? round( ( $valid_count / $total ) * 100, 1 ) : 0; return array( 'feed_type' => $feed->get_type(), 'total_products' => $total, 'valid_products' => $valid_count, 'invalid_products' => $total - $valid_count, 'coverage_percent' => $coverage, 'missing_required' => $missing_fields, 'missing_recommended' => $warnings, 'status' => $this->determine_status( $coverage ), ); } /** * Check health across all feed formats. * * @param array $feeds Registered feed formats. * @param array $products Normalized product data. * * @return array Health reports keyed by feed type. */ public function check_all( array $feeds, array $products ): array { $reports = array(); foreach ( $feeds as $type => $feed ) { $reports[ $type ] = $this->check_feed( $feed, $products ); } return $reports; } /** * Get a summary of the most common missing fields across all feeds. * * @param array $health_reports Health reports from check_all(). * * @return array Sorted list of missing fields with counts. */ public function get_missing_field_summary( array $health_reports ): array { $all_missing = array(); foreach ( $health_reports as $report ) { foreach ( $report['missing_required'] as $field => $count ) { if ( ! isset( $all_missing[ $field ] ) ) { $all_missing[ $field ] = array( 'field' => $field, 'total_count' => 0, 'feeds' => array(), ); } $all_missing[ $field ]['total_count'] += $count; $all_missing[ $field ]['feeds'][] = $report['feed_type']; } } // Sort by most impactful (highest count). usort( $all_missing, static function ( array $a, array $b ): int { return $b['total_count'] <=> $a['total_count']; } ); return $all_missing; } /** * Determine health status from coverage percentage. * * @param float $coverage Coverage percentage. * * @return string Status: 'healthy', 'warning', or 'critical'. */ private function determine_status( float $coverage ): string { if ( $coverage >= 90 ) { return 'healthy'; } if ( $coverage >= 70 ) { return 'warning'; } return 'critical'; } }