license = $license; $this->register_default_checks(); } /** * Register the built-in v1 checks and expose a filter for extensions. * * @return void */ protected function register_default_checks() { $defaults = array( new ARWC_Check_Identifiers(), new ARWC_Check_Attributes(), new ARWC_Check_Description(), new ARWC_Check_Inventory(), new ARWC_Check_Schema(), ); /** * Filter the list of readiness checks. * * Add an object implementing ARWC_Check to extend the audit. This is the * primary extension point for new checks and for Pro-tier checks. * * @param ARWC_Check[] $defaults Default check instances. * @param ARWC_Scorer $scorer The scorer instance. */ $checks = apply_filters( 'arwc_checks', $defaults, $this ); foreach ( $checks as $check ) { if ( $check instanceof ARWC_Check ) { $this->checks[ $check->get_id() ] = $check; } } } /** * Get all registered checks. * * @return ARWC_Check[] */ public function get_checks() { return $this->checks; } /** * Get only the checks that are active for the current tier. * * Pro checks are skipped entirely when Pro is not active, so they neither * contribute to the score nor distort the weight normalization. * * @return ARWC_Check[] */ public function get_active_checks() { $active = array(); foreach ( $this->checks as $id => $check ) { if ( $check->is_pro() && ! $this->license->is_pro() ) { continue; } $active[ $id ] = $check; } return $active; } /** * Score a single product. * * @param WC_Product $product The product to score. * @return array{ * score:int, * breakdown:array>, * issues:array> * } */ public function score_product( WC_Product $product ) { $checks = $this->get_active_checks(); $total_weight = 0.0; $weighted_sum = 0.0; $breakdown = array(); $issues = array(); foreach ( $checks as $id => $check ) { $weight = (float) $check->get_weight(); if ( $weight <= 0 ) { continue; } $result = $check->run( $product ); if ( ! $result instanceof ARWC_Check_Result ) { // Defensive: a misbehaving check should not break the scan. $result = new ARWC_Check_Result( 0.0 ); } $score = $result->get_score(); $total_weight += $weight; $weighted_sum += $score * $weight; $check_issues = $result->get_issues(); $issues = array_merge( $issues, $check_issues ); $breakdown[ $id ] = array( 'label' => $check->get_label(), 'score' => (int) round( $score * 100 ), 'weight' => $weight, 'issues' => $check_issues, 'is_pro' => $check->is_pro(), ); } $final = ( $total_weight > 0 ) ? (int) round( ( $weighted_sum / $total_weight ) * 100 ) : 0; return array( 'score' => $final, 'breakdown' => $breakdown, 'issues' => $issues, ); } /** * Map a numeric score to a qualitative grade used by the UI. * * @param int $score 0–100 score. * @return string One of: good, fair, poor. */ public static function grade( $score ) { $score = (int) $score; if ( $score >= 80 ) { return 'good'; } if ( $score >= 50 ) { return 'fair'; } return 'poor'; } }