'critical', ABDS_SEO_Site_Scanner::SEVERITY_WARNING => 'medium', ABDS_SEO_Site_Scanner::SEVERITY_INFO => 'notice', ); /** * Site_Scanner instance, injected so tests can substitute a stub. * * @var ABDS_SEO_Site_Scanner */ protected ABDS_SEO_Site_Scanner $scanner; /** * Constructor. * * @param ?ABDS_SEO_Site_Scanner $scanner Scanner to wrap; defaults to a real instance. */ public function __construct( ?ABDS_SEO_Site_Scanner $scanner = null ) { $this->scanner = $scanner ?? new ABDS_SEO_Site_Scanner(); } /** * Runs Site_Scanner against this post and translates its issues. * * @param WP_Post $post Post being audited. * @param string $html Pre-fetched rendered HTML, shared with Site_Scanner's own schema check. * @return array */ public function run( WP_Post $post, string $html ): array { $result = $this->scanner->scan_post( $post->ID, $html ); return array_map( function ( array $issue ) use ( $post ): array { return array( 'issue_key' => $issue['code'], 'category' => self::CATEGORY, 'severity' => self::SEVERITY_MAP[ $issue['severity'] ] ?? 'notice', 'message' => $issue['message'], 'details' => self::locators( $issue['code'], (string) $post->post_content ), ); }, $result['issues'] ); } /** * M2 2.3: element locators for the two technical issue_keys that have * one to give — `src` for images missing alt text, ordinal position for * extra H1 headings (missing_h1 has nothing to point at: there's no * element, that's the whole problem). Deliberately re-scans * $post_content with the SAME regexes * ABDS_SEO_Site_Scanner::check_images()/check_headings() use, rather * than asking Site_Scanner itself to expose this — those methods return * only {code,severity,message} today and changing that return shape * would ripple to every other Site_Scanner consumer (the classic scan * report, etc.) for a detail only the audit's own details_json needs. * Small, deliberate duplication, same precedent * ABDS_SEO_Rendered_HTML_Content_Adapter's own docblock documents for * its strip_html(). Pure (string in, array out) so it's unit-testable * without a WP_Post/WP bootstrap, same split this codebase applies * throughout content-analysis. * * @param string $issue_key Site_Scanner issue code. * @param string $post_content Raw post_content — what Site_Scanner itself scanned. * @return array */ public static function locators( string $issue_key, string $post_content ): array { if ( 'missing_alt' === $issue_key ) { if ( ! preg_match_all( '/]*>/i', $post_content, $matches ) ) { return array(); } $srcs = array(); foreach ( $matches[0] as $img_tag ) { if ( preg_match( '/\balt\s*=\s*["\'][^"\']+["\']/i', $img_tag ) ) { continue; } if ( preg_match( '/\bsrc\s*=\s*["\']([^"\']+)["\']/i', $img_tag, $src_match ) ) { $srcs[] = $src_match[1]; } } return array() !== $srcs ? array( 'srcs' => $srcs ) : array(); } if ( 'multiple_h1' === $issue_key ) { $h1_count = preg_match_all( '/]/i', $post_content ); return $h1_count > 0 ? array( 'level' => 1, 'positions' => range( 1, $h1_count ), ) : array(); } return array(); } }