get_post_data( $post_id ); $settings = get_option( 'schema_ai_settings', array() ); // Resolve the correct @type from site type settings. $entity_type = class_exists( 'Schema_AI_Site_Type' ) ? Schema_AI_Site_Type::get_homepage_entity() : $this->type; // If a specific business subtype is set for this site type category, // prefer its more specific schema.org type over the generic // category-level entity, mirroring how Schema_AI_Local_Business_Schema // already resolves subtypes. Only government/news_media/nonprofit/ // organizations_groups have subtypes today, but this applies to any // non-local category that gains one later without further changes. $subtype_slug = isset( $settings['business_subtype'] ) ? $settings['business_subtype'] : ''; if ( $subtype_slug && class_exists( 'Schema_AI_Business_Subtype' ) && Schema_AI_Business_Subtype::is_valid( $subtype_slug ) ) { $entity_type = Schema_AI_Business_Subtype::get_type( $subtype_slug ); } if ( empty( $post_data ) ) { return array( '@type' => $entity_type ); } $org = isset( $settings['default_organization'] ) ? $settings['default_organization'] : array(); $name = ! empty( $org['name'] ) ? $org['name'] : get_bloginfo( 'name' ); $url = ! empty( $org['url'] ) ? $org['url'] : home_url( '/' ); $schema = array( '@type' => $entity_type, '@id' => Schema_AI_ID_Builder::organization(), 'name' => $name, 'url' => $url, ); if ( ! empty( $post_data['excerpt'] ) ) { $schema['description'] = $post_data['excerpt']; } // Logo - from publisher data or fields. $publisher = $this->get_publisher_data(); if ( ! empty( $publisher['logo'] ) ) { $schema['logo'] = $publisher['logo']; } if ( ! empty( $fields['logo'] ) ) { $logo = $fields['logo']; if ( is_string( $logo ) ) { $logo = array( '@type' => 'ImageObject', 'url' => $logo, ); } $schema['logo'] = $logo; } // Image. $image = $this->get_image_data( $post_id ); if ( ! empty( $image ) ) { $schema['image'] = $image; } // Contact point. A configured Entity Profile phone/email (the // default_org_phone / default_org_email settings, two-way synced with // the dashboard) is authoritative over anything AI-detected from page // content for this site-wide entity node. $entity_phone = ''; $entity_email = ''; if ( class_exists( 'Schema_AI_Entity_Settings' ) ) { $entity_contact = Schema_AI_Entity_Settings::get_all(); $entity_phone = ! empty( $entity_contact['org_phone'] ) ? $entity_contact['org_phone'] : ''; $entity_email = ! empty( $entity_contact['org_email'] ) ? $entity_contact['org_email'] : ''; } $contact_phone = $entity_phone ? $entity_phone : ( ! empty( $fields['telephone'] ) ? $fields['telephone'] : '' ); $contact_email = $entity_email ? $entity_email : ( ! empty( $fields['email'] ) ? $fields['email'] : '' ); if ( ! empty( $fields['contactPoint'] ) ) { $schema['contactPoint'] = $this->format_contact_points( $fields['contactPoint'] ); } elseif ( $contact_phone || $contact_email ) { $contact = array( '@type' => 'ContactPoint', ); if ( $contact_phone ) { $contact['telephone'] = $contact_phone; $contact['contactType'] = 'customer service'; } if ( $contact_email ) { $contact['email'] = $contact_email; } $schema['contactPoint'] = $contact; } // Address — omitted entirely for a declared service-area business // (has_physical_location === false), even if AI-detected fields still // carry one, matching Google's guidance not to claim a location that // doesn't exist. Can only be false for a local business type (the // setting is only ever exposed for those), but checked directly here // rather than re-deriving "is local" so this stays correct even if // that assumption ever changes. $has_physical_location = class_exists( 'Schema_AI_Entity_Settings' ) ? Schema_AI_Entity_Settings::get_all()['has_physical_location'] : true; if ( $has_physical_location && ! empty( $fields['address'] ) ) { $address = $fields['address']; if ( is_array( $address ) && ! isset( $address['@type'] ) ) { $address['@type'] = 'PostalAddress'; } $schema['address'] = $address; } // sameAs — merge entity authority settings with AI-detected values. $entity_same_as = class_exists( 'Schema_AI_Entity_Settings' ) ? Schema_AI_Entity_Settings::get_same_as() : array(); $field_same_as = ! empty( $fields['sameAs'] ) ? ( is_array( $fields['sameAs'] ) ? $fields['sameAs'] : array( $fields['sameAs'] ) ) : array(); $merged_same_as = array_values( array_unique( array_merge( $entity_same_as, $field_same_as ) ) ); if ( ! empty( $merged_same_as ) ) { $schema['sameAs'] = $merged_same_as; } // Entity authority signals from settings. if ( class_exists( 'Schema_AI_Entity_Settings' ) ) { $entity = Schema_AI_Entity_Settings::get_all(); if ( ! empty( $entity['knows_about'] ) ) { $schema['knowsAbout'] = $this->format_knows_about( $entity['knows_about'] ); } if ( ! empty( $entity['legal_name'] ) ) { $schema['legalName'] = $entity['legal_name']; } if ( empty( $schema['description'] ) && ! empty( $entity['description'] ) ) { $schema['description'] = $entity['description']; } if ( ! empty( $entity['num_employees'] ) ) { $schema['numberOfEmployees'] = array( '@type' => 'QuantitativeValue', 'value' => (int) $entity['num_employees'], ); } } // Founding date — entity settings take precedence over AI-detected. $founding_date = class_exists( 'Schema_AI_Entity_Settings' ) ? Schema_AI_Entity_Settings::get_all()['founding_date'] : ''; if ( ! empty( $founding_date ) ) { $schema['foundingDate'] = $founding_date; } elseif ( ! empty( $fields['foundingDate'] ) ) { $schema['foundingDate'] = $fields['foundingDate']; } // Founders — a persistent entity-settings founder list takes precedence // over AI-detected ones (which are re-guessed from page content on every // generation and can't be configured once for the whole site). $entity_founders = class_exists( 'Schema_AI_Entity_Settings' ) ? Schema_AI_Entity_Settings::get_all()['founders'] : array(); // Property name is "founder" per schema.org (not "founders") — an // array value is how schema.org represents more than one. if ( ! empty( $entity_founders ) ) { $schema['founder'] = $this->format_founders( $entity_founders ); } elseif ( ! empty( $fields['founders'] ) ) { $schema['founder'] = $this->format_founders( $fields['founders'] ); } // Registry IDs — entity settings take precedence over AI-detected. if ( class_exists( 'Schema_AI_Entity_Settings' ) ) { $registry = Schema_AI_Entity_Settings::get_all(); if ( ! empty( $registry['tax_id'] ) ) { $schema['taxID'] = $registry['tax_id']; } if ( ! empty( $registry['vat_id'] ) ) { $schema['vatID'] = $registry['vat_id']; } // schema.org has no dedicated "company registration number" // property — PropertyValue under `identifier` is the documented // pattern for exactly this, matching the SaaS backend's own // convention (backend/src/services/entityMerge.ts). if ( ! empty( $registry['company_number'] ) ) { $schema['identifier'] = array( '@type' => 'PropertyValue', 'propertyID' => 'UK Companies House Number', 'value' => $registry['company_number'], ); } if ( ! empty( $registry['service_area'] ) ) { $areas = array_map( function ( $area_name ) { return array( '@type' => 'Place', 'name' => $area_name, ); }, $registry['service_area'] ); $schema['areaServed'] = 1 === count( $areas ) ? $areas[0] : $areas; } } // Merge remaining AI-detected fields. $schema = $this->merge_fields( $schema, $fields, array( 'name', 'description', 'url', 'legalName', 'taxID', 'vatID', 'numberOfEmployees', 'areaServed', 'award', 'knowsAbout', 'slogan', ) ); return $schema; } /** * Format contact point data. * * @param array $contact_points Contact point data. * @return array Formatted ContactPoint(s). */ private function format_contact_points( $contact_points ) { if ( ! is_array( $contact_points ) ) { return $contact_points; } // Single contact point. if ( isset( $contact_points['telephone'] ) || isset( $contact_points['email'] ) ) { if ( ! isset( $contact_points['@type'] ) ) { $contact_points['@type'] = 'ContactPoint'; } return $contact_points; } // Multiple contact points. $formatted = array(); foreach ( $contact_points as $point ) { if ( is_array( $point ) ) { if ( ! isset( $point['@type'] ) ) { $point['@type'] = 'ContactPoint'; } $formatted[] = $point; } } return $formatted; } /** * Format founders data. * * @param array $founders Founders data. * @return array Array of Person schema objects. */ private function format_founders( $founders ) { if ( ! is_array( $founders ) ) { return array( array( '@type' => 'Person', 'name' => $founders, ), ); } $formatted = array(); foreach ( $founders as $founder ) { if ( is_string( $founder ) ) { $formatted[] = array( '@type' => 'Person', 'name' => $founder, ); } elseif ( is_array( $founder ) ) { if ( ! isset( $founder['@type'] ) ) { $founder['@type'] = 'Person'; } $formatted[] = $founder; } } return $formatted; } /** * Format knowsAbout topics. Plain strings stay strings; a topic with a * Wikidata match becomes a Thing object linking to it — both are valid * knowsAbout values, so a mixed array is fine. * * @param array $topics Topics from entity settings ({name, wikidataUrl?}[] or strings). * @return array */ private function format_knows_about( $topics ) { if ( ! is_array( $topics ) ) { return $topics; } $formatted = array(); foreach ( $topics as $topic ) { if ( is_string( $topic ) ) { $formatted[] = $topic; } elseif ( is_array( $topic ) && ! empty( $topic['name'] ) ) { if ( ! empty( $topic['wikidataUrl'] ) ) { $formatted[] = array( '@type' => 'Thing', 'name' => $topic['name'], 'sameAs' => $topic['wikidataUrl'], ); } else { $formatted[] = $topic['name']; } } } return $formatted; } }