parsing — one extraction implementation, several validators over * it. * * "Missing a required field" is an error (the type won't qualify for its * rich result); "missing a recommended field" is a warning (it qualifies * but weaker). A page with no schema of a given type is neither — absence * is not invalidity. * * @package ABDS_SEO */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Rich-result schema validation. */ class ABDS_SEO_Schema_Validator { /** * Required and recommended fields per validated type. Required fields * are what Google's rich-result docs list as mandatory for the type to * be eligible; recommended fields strengthen it. Kept as data so the * rules read at a glance and a new type is a new entry, not new code. */ const TYPE_RULES = array( 'Article' => array( 'required' => array( 'headline' ), 'recommended' => array( 'image', 'datePublished', 'author' ), ), 'Product' => array( 'required' => array( 'name' ), 'recommended' => array( 'image' ), ), 'FAQPage' => array( 'required' => array( 'mainEntity' ), 'recommended' => array(), ), 'BreadcrumbList' => array( 'required' => array( 'itemListElement' ), 'recommended' => array(), ), // The page types the graph can print (see PAGE_TYPES). Google asks for no // specific field on a plain page, so nothing is required; a name is // recommended. NewsArticle and BlogPosting are not listed on purpose: they // validate as Article (matched_type() maps them), which keeps the stored // schema_invalid_article issue key stable. 'WebPage' => array( 'required' => array(), 'recommended' => array( 'name' ), ), 'AboutPage' => array( 'required' => array(), 'recommended' => array( 'name' ), ), 'ContactPage' => array( 'required' => array(), 'recommended' => array( 'name' ), ), 'CollectionPage' => array( 'required' => array(), 'recommended' => array( 'name' ), ), 'ItemPage' => array( 'required' => array(), 'recommended' => array( 'name' ), ), ); /** * The schema_type values a post type may be set to: the ONE list the * Search Appearance screen offers, the policy engine accepts and the graph * prints. Anything else stored is rejected with a fallback to Article and an * audit finding (see resolve_page_type()). */ const PAGE_TYPES = array( 'Article', 'NewsArticle', 'BlogPosting', 'WebPage', 'AboutPage', 'ContactPage', 'CollectionPage', 'ItemPage' ); /** * Node types of which a page should carry exactly one, grouped. Two of a * group (a theme printing its own Organization next to ours, two Article * nodes from two plugins) make Google pick one. */ const SINGLE_INSTANCE_GROUPS = array( 'WebSite' => array( 'WebSite' ), 'Organization' => array( 'Organization' ), 'WebPage' => array( 'WebPage', 'AboutPage', 'ContactPage', 'CollectionPage', 'ItemPage' ), 'Article' => array( 'Article', 'NewsArticle', 'BlogPosting' ), 'BreadcrumbList' => array( 'BreadcrumbList' ), ); /** * Maps a stored schema_type value to the one the graph will print. * Empty means "never set" and is the normal default, not a problem. * * @param string $stored Stored value (may be empty or unknown). * @param string $post_type Post type slug; picks the default (see default_page_type()). * @return array{type:string, valid:bool} type is always one of PAGE_TYPES. */ public static function resolve_page_type( string $stored, string $post_type = '' ): array { $stored = trim( $stored ); $default = self::default_page_type( $post_type ); if ( '' === $stored ) { return array( 'type' => $default, 'valid' => true, ); } if ( in_array( $stored, self::PAGE_TYPES, true ) ) { return array( 'type' => $stored, 'valid' => true, ); } return array( 'type' => $default, 'valid' => false, ); } /** * The type printed for a content type nobody configured: WebPage for * static pages (an Article on a static page is a wrong signal), Article * for everything else. Mirrored in src/search-appearance.js. * * @param string $post_type Post type slug ('' when unknown). * @return string One of PAGE_TYPES. */ public static function default_page_type( string $post_type ): string { return 'page' === $post_type ? 'WebPage' : 'Article'; } /** * Whole-page structured-data findings, independent of any rich-result rule: * how many nodes the page carries, references that lead nowhere, ids * defined twice, and node types that should be unique but are not. * * A "reference" is an object whose only key is @id; it must be defined (as an * object with more than @id) somewhere in the SAME JSON-LD block, since * separate