' . $svg . ''; } /** * Grade colour. * * @param string $grade Letter grade. * @return string Hex colour. */ function badge_color( string $grade ): string { $colors = array( 'A+' => '#22c55e', 'A' => '#22c55e', 'B' => '#3b82f6', 'C' => '#eab308', 'D' => '#f97316', 'F' => '#ef4444', ); return isset( $colors[ $grade ] ) ? $colors[ $grade ] : '#8c8f94'; } /** * Build the badge SVG (236×76, dark card). * * @param array|null $scan Last scan, or null when none is stored. * @return string */ function build_badge_svg( ?array $scan ): string { $has_score = is_array( $scan ) && isset( $scan['score']['grade'] ) && '' !== $scan['score']['grade']; $score = $has_score ? (int) $scan['score']['score'] : null; $grade = $has_score ? (string) $scan['score']['grade'] : ''; $level = is_array( $scan ) && isset( $scan['level'] ) ? (int) $scan['level'] : null; $color = $has_score ? badge_color( $grade ) : '#8c8f94'; $year = is_array( $scan ) && ! empty( $scan['scannedAt'] ) ? gmdate( 'Y', strtotime( (string) $scan['scannedAt'] ) ) : gmdate( 'Y' ); $label = $has_score ? __( 'AGENT READY', 'aj-agent-crawl-optimizer' ) : __( 'NOT SCANNED', 'aj-agent-crawl-optimizer' ); $sub = null !== $level /* translators: %d: readiness level 0-5. */ ? sprintf( __( 'Level %d of 5', 'aj-agent-crawl-optimizer' ), $level ) : __( 'Run a scan to score this site', 'aj-agent-crawl-optimizer' ); $title = $has_score /* translators: 1: score, 2: grade. */ ? sprintf( __( 'Agent-readiness score %1$d of 100, grade %2$s', 'aj-agent-crawl-optimizer' ), $score, $grade ) : __( 'Agent-readiness score: not scanned yet', 'aj-agent-crawl-optimizer' ); $font = 'font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica,Arial,sans-serif"'; $svg = ''; $svg .= '' . esc_html( $title ) . ''; $svg .= ''; $svg .= ''; $svg .= ''; $svg .= '' . esc_html( $label ) . ''; if ( $has_score ) { $svg .= '' . (int) $score . ''; $svg .= '' . esc_html( $grade ) . ''; } $svg .= '' . esc_html( $sub ) . ''; $svg .= '' . esc_html( $year ) . ''; $svg .= ''; return $svg; }