get_post_data( $post_id ); if ( empty( $post_data ) ) { return array( '@type' => $this->type ); } $settings = get_option( 'schema_ai_settings', array() ); $org = isset( $settings['default_organization'] ) ? $settings['default_organization'] : array(); $name = ! empty( $settings['default_org_name'] ) ? $settings['default_org_name'] : ( ! empty( $org['name'] ) ? $org['name'] : get_bloginfo( 'name' ) ); $canonical_url = home_url( '/' ); // Resolve the most specific schema.org subtype from settings. if ( class_exists( 'Schema_AI_Business_Subtype' ) ) { $resolved_type = Schema_AI_Business_Subtype::get_type_from_settings(); } else { $resolved_type = $this->type; } $schema = array( '@type' => $resolved_type, '@id' => Schema_AI_ID_Builder::local_business(), 'name' => $name, 'url' => $canonical_url, ); if ( ! empty( $post_data['excerpt'] ) ) { $schema['description'] = $post_data['excerpt']; } // Image. $image = $this->get_image_data( $post_id ); if ( ! empty( $image ) ) { $schema['image'] = $image; } // Telephone / email. A configured Entity Profile value (default_org_phone // / default_org_email, two-way synced with the dashboard) wins over // anything AI-detected from this page's content. $entity_contact = class_exists( 'Schema_AI_Entity_Settings' ) ? Schema_AI_Entity_Settings::get_all() : array(); $telephone = ! empty( $entity_contact['org_phone'] ) ? $entity_contact['org_phone'] : ( ! empty( $fields['telephone'] ) ? $fields['telephone'] : '' ); if ( $telephone ) { $schema['telephone'] = $telephone; } $email = ! empty( $entity_contact['org_email'] ) ? $entity_contact['org_email'] : ( ! empty( $fields['email'] ) ? $fields['email'] : '' ); if ( $email ) { $schema['email'] = $email; } // 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. $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; } // Geo coordinates. if ( ! empty( $fields['geo'] ) ) { $geo = $fields['geo']; if ( is_array( $geo ) && ! isset( $geo['@type'] ) ) { $geo['@type'] = 'GeoCoordinates'; } $schema['geo'] = $geo; } elseif ( ! empty( $fields['latitude'] ) && ! empty( $fields['longitude'] ) ) { $schema['geo'] = array( '@type' => 'GeoCoordinates', 'latitude' => $fields['latitude'], 'longitude' => $fields['longitude'], ); } // Opening hours. if ( ! empty( $fields['openingHoursSpecification'] ) ) { $schema['openingHoursSpecification'] = $this->format_opening_hours( $fields['openingHoursSpecification'] ); } // Price range. if ( ! empty( $fields['priceRange'] ) ) { $schema['priceRange'] = $fields['priceRange']; } // 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( $entity['founding_date'] ) ) { $schema['foundingDate'] = $entity['founding_date']; } 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'], ); } // 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'] ); } if ( ! empty( $entity['tax_id'] ) ) { $schema['taxID'] = $entity['tax_id']; } if ( ! empty( $entity['vat_id'] ) ) { $schema['vatID'] = $entity['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( $entity['company_number'] ) ) { $schema['identifier'] = array( '@type' => 'PropertyValue', 'propertyID' => 'UK Companies House Number', 'value' => $entity['company_number'], ); } if ( ! empty( $entity['service_area'] ) ) { $areas = array_map( function ( $area_name ) { return array( '@type' => 'Place', 'name' => $area_name, ); }, $entity['service_area'] ); $schema['areaServed'] = 1 === count( $areas ) ? $areas[0] : $areas; } } // Merge remaining AI-detected fields. $schema = $this->merge_fields( $schema, $fields, array( 'name', 'description', 'image', 'url', 'currenciesAccepted', 'paymentAccepted', 'areaServed', 'hasMap', 'aggregateRating', 'review', 'taxID', 'vatID', ) ); return $schema; } /** * Format opening hours into OpeningHoursSpecification objects. * * @param array $hours Array of opening hours data. * @return array Array of OpeningHoursSpecification schema objects. */ private function format_opening_hours( $hours ) { if ( ! is_array( $hours ) ) { return $hours; } $specifications = array(); foreach ( $hours as $entry ) { if ( is_array( $entry ) ) { if ( ! isset( $entry['@type'] ) ) { $entry['@type'] = 'OpeningHoursSpecification'; } $specifications[] = $entry; } } return ! empty( $specifications ) ? $specifications : $hours; } /** * Format founders data into Person schema objects. * * @param array $founders Founders data ({name, jobTitle?, sameAs?}[] from * entity settings, or plain strings). * @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; } }