array( 25, 50 ), 'high' => array( 8, 24 ), 'medium' => array( 3, 12 ), 'low' => array( 1, 5 ), 'info' => array( 0, 0 ), ); const FULL = array( 'confirmed', 'integrity_mismatch', 'signature_match' ); const HALF = array( 'potential', 'suspicious', 'unverified' ); /** * Per-module scores for one scan. * * @param int $scan_id Scan. * @return array module => score */ public static function for_scan( $scan_id ) { $scan = Repository::get_scan( $scan_id ); if ( ! $scan ) { return array(); } $modules = 'integrity' === $scan->scan_type ? array( 'integrity' ) : array( 'seo', 'a11y', 'health', 'plugins' ); $out = array(); foreach ( $modules as $module ) { $deduct = 0.0; foreach ( Repository::check_counts( $scan_id, $module ) as $row ) { $factor = in_array( $row->classification, self::FULL, true ) ? 1.0 : ( in_array( $row->classification, self::HALF, true ) ? 0.5 : 0.0 ); if ( ! $factor || ! isset( self::WEIGHTS[ $row->severity ] ) ) { continue; } list( $weight, $cap ) = self::WEIGHTS[ $row->severity ]; $deduct += $factor * min( $cap, $weight * (int) $row->c ); } $out[ $module ] = (int) max( 0, round( 100 - $deduct ) ); } return $out; } /** * Overall estimate from the latest completed site and integrity scans. * * @return array {overall:int|null, modules: array} */ public static function overall() { $site = Repository::latest_completed( 'site' ); $integrity = Repository::latest_completed( 'integrity' ); $modules = array(); if ( $site ) { $modules = isset( $site->summary['scores'] ) ? (array) $site->summary['scores'] : self::for_scan( $site->id ); } if ( $integrity ) { $scores = isset( $integrity->summary['scores'] ) ? (array) $integrity->summary['scores'] : self::for_scan( $integrity->id ); $modules['integrity'] = isset( $scores['integrity'] ) ? $scores['integrity'] : null; } $weights = array( 'seo' => 30, 'a11y' => 25, 'health' => 20, 'plugins' => 10, 'integrity' => 15, ); $sum = 0; $total = 0; foreach ( $modules as $module => $score ) { if ( null !== $score && isset( $weights[ $module ] ) ) { $sum += $weights[ $module ] * $score; $total += $weights[ $module ]; } } return array( 'overall' => $total ? (int) round( $sum / $total ) : null, 'modules' => $modules, ); } }