per node (an Article * everywhere, a LocalBusiness on the front page) and the nodes knew nothing * about each other: no @id, no WebSite or Organization entity, no breadcrumb. * This class turns the same facts into a single `@graph` in which every node has * a stable @id and the nodes reference each other by @id, the shape Google's * documentation and the established SEO plugins use. * * Pure PHP, no WordPress calls: the WordPress-aware gathering lives in * ABDS_SEO_Schema_Context, the printing in ABDS_SEO_Frontend_Output. * * @id rules: page-level ids hang off the CANONICAL address the policy engine * reports (never the request URL, or a UTM-tagged or paginated request would mint * a second entity for the same document); site-level ids hang off the home URL. * * {home}#website {home}#organization (or #person) {home}#logo * {canonical}#webpage {canonical}#article {canonical}#breadcrumb * {canonical}#primaryimage {home}#/schema/person/{author} * * Which nodes a request gets: * - search results, 404 and any noindex page: WebSite + Organization/Person only. * Google asks that pages carrying structured data are not blocked with * noindex (https://developers.google.com/search/docs/appearance/structured-data/sd-policies), * so a noindex page cannot earn rich results from page-level markup, and an * @id on the canonical address of a document the owner told engines to drop * would still assert that document as an entity. The site-level entities stay * because they describe the site, not the excluded document. * - paginated archives and every non-singular view (posts index, archives): a * CollectionPage, never an Article. * - a singular page: WebPage plus, for article types, an Article that points back * at it. Non-article types (WebPage and subtypes) carry no headline, author or * datePublished: they mean nothing there and trigger Google warnings. * * @package ABDS_SEO */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Pure graph assembly from an already-gathered context. */ class ABDS_SEO_Schema_Graph { /** * Article-family page types (they get an Article node and article fields). */ const ARTICLE_TYPES = array( 'Article', 'NewsArticle', 'BlogPosting' ); /** * Assembles the graph. * * Input keys (all optional unless marked): * - home_url (required) Site root with a trailing slash. * - site_name, language Plain strings ('' omits the property). * - search_enabled bool, print the WebSite SearchAction. * - entity { kind: organization|person, name, logo: {url,width,height}|null, same_as: string[] } * - local_node The full LocalBusiness node (ABDS_SEO_Meta_Builder), already licence-gated; null = none. * Front page only: it brings the local fields (address, phone, hours...) and the extra @type. * - local_identity The same licensed node on EVERY view (null = unlicensed or no profile). Only its * organization-level fields (name, sameAs, logo fallback) are used, so the one * single organization id has the same name on every page. * - request { is_search, is_404, is_paged, is_singular, is_front_page, index: bool, * canonical, schema_type (already validated), title, description, * published, modified, author: {name,id}|null, * image: {url,width,height}|null, breadcrumbs: list<{name,url}> } * * @param array $in Context, see above. * @return array{'@context':string,'@graph':list} */ public static function build( array $in ): array { $home = self::home( (string) ( $in['home_url'] ?? '' ) ); $site = (string) ( $in['site_name'] ?? '' ); $language = (string) ( $in['language'] ?? '' ); $entity = (array) ( $in['entity'] ?? array() ); $request = (array) ( $in['request'] ?? array() ); $is_person = 'person' === ( $entity['kind'] ?? '' ); $entity_id = $home . ( $is_person ? '#person' : '#organization' ); $nodes = array(); $nodes[] = self::website_node( $home, $site, $language, $entity_id, ! empty( $in['search_enabled'] ) ); $local = is_array( $in['local_node'] ?? null ) ? $in['local_node'] : null; $identity = is_array( $in['local_identity'] ?? null ) ? $in['local_identity'] : null; if ( ! $is_person && ( null !== $local || null !== $identity ) ) { $nodes[] = self::merge_local( self::entity_node( $home, $entity, $site, false ), $local ?? $identity, $home, null !== $local ); $local = null; } else { $nodes[] = self::entity_node( $home, $entity, $site, $is_person ); } if ( self::wants_page_nodes( $request ) ) { $nodes = array_merge( $nodes, ! empty( $request['is_singular'] ) && empty( $request['is_paged'] ) ? self::singular_nodes( $request, $home, $language, $entity_id ) : self::collection_nodes( $request, $home, $language, $site ) ); } if ( null !== $local ) { // A person-represented site with a licensed local profile: the // business stays its own node (an Organization merge would invent // an organization the owner did not declare). $local['@id'] = $home . '#localbusiness'; $local['url'] = $home; unset( $local['@context'] ); $nodes[] = $local; } return array( '@context' => 'https://schema.org', '@graph' => self::unique_by_id( array_map( array( self::class, 'clean' ), $nodes ) ), ); } /** * The graph as the JSON placed inside the