post_content; $stripped = wp_strip_all_tags( $content ); $word_count = str_word_count( $stripped ); return array( 'title' => $post->post_title, 'content' => $content, 'excerpt' => has_excerpt( $post_id ) ? get_the_excerpt( $post_id ) : wp_trim_words( $stripped, 55, '...' ), 'url' => get_permalink( $post_id ), 'post_type' => $post->post_type, 'date' => get_the_date( 'c', $post_id ), 'modified' => get_the_modified_date( 'c', $post_id ), 'author_id' => (int) $post->post_author, 'word_count' => $word_count, ); } /** * Get author data as a schema.org Person object. * * @param int $post_id The WordPress post ID. * @return array Schema.org Person type array. */ protected function get_author_data( $post_id ) { $post = get_post( $post_id ); if ( ! $post ) { return array(); } $author_id = (int) $post->post_author; $user = get_userdata( $author_id ); if ( ! $user ) { return array(); } $author = array( '@type' => 'Person', 'name' => $user->display_name, ); $author_url = get_author_posts_url( $author_id ); if ( $author_url ) { $author['url'] = $author_url; } $avatar = get_avatar_url( $author_id, array( 'size' => 96 ) ); if ( $avatar ) { $author['image'] = $avatar; } return $author; } /** * Get publisher data as a schema.org Organization object. * * Reads organisation name and logo from the schema_ai_settings option. * Falls back to the site name and custom logo. * * @return array Schema.org Organization type array. */ protected function get_publisher_data() { $settings = get_option( 'schema_ai_settings', array() ); $org = isset( $settings['default_organization'] ) ? $settings['default_organization'] : array(); $org_name = ! empty( $org['name'] ) ? $org['name'] : get_bloginfo( 'name' ); $org_logo = ! empty( $settings['default_logo'] ) ? $settings['default_logo'] : ''; if ( empty( $org_logo ) ) { $custom_logo_id = get_theme_mod( 'custom_logo' ); if ( $custom_logo_id ) { $logo_src = wp_get_attachment_image_src( $custom_logo_id, 'full' ); if ( $logo_src ) { $org_logo = $logo_src[0]; } } } $publisher = array( '@type' => 'Organization', 'name' => $org_name, 'url' => home_url( '/' ), ); if ( ! empty( $org_logo ) ) { $publisher['logo'] = array( '@type' => 'ImageObject', 'url' => $org_logo, ); } return $publisher; } /** * Cross-reference to the site-wide publisher entity. * * Returns {"@id": "..."} — never redefines the entity inline. * Routes to LocalBusiness for local-business sites, Organization for all others, * matching whichever entity is actually defined on the homepage. * * @return array */ protected function get_publisher_reference() { return Schema_AI_ID_Builder::ref( Schema_AI_ID_Builder::primary_entity() ); } /** * Cross-reference to the site-wide LocalBusiness entity. * * @param string|null $location_url For multi-location; null = single location. * @return array */ protected function get_local_business_reference( $location_url = null ) { return Schema_AI_ID_Builder::ref( Schema_AI_ID_Builder::local_business( $location_url ) ); } /** * Cross-reference to the author entity: a Person when the WordPress * author account is a specific, named individual (see * Schema_AI_Author_Utils); otherwise the site's own Organization/ * LocalBusiness entity, since schema.org's "author" property accepts * Organization as well as Person and a generic/shared/team account * ("Editorial Team", "Admin") is not a person. Previously this always * returned a Person reference regardless of the account, but nothing * ever built a matching Person entity for a team-style account, so * every such post shipped a dangling @id; then it was changed to return * nothing at all for those, dropping authorship entirely even though a * real byline existed. This is the correct middle ground. * Schema_AI_Auto_Apply::generate_for_post() builds the actual Person * entity in the same batch whenever this returns a Person reference. * * @param int $post_id The WordPress post ID. * @return array */ protected function get_author_reference( $post_id ) { $post = get_post( $post_id ); $author_id = $post ? (int) $post->post_author : 0; if ( ! $author_id ) { return array(); } if ( class_exists( 'Schema_AI_Author_Utils' ) && Schema_AI_Author_Utils::is_named_individual( $author_id ) ) { return Schema_AI_ID_Builder::ref( Schema_AI_ID_Builder::person( $author_id ) ); } return $this->get_publisher_reference(); } /** * Cross-reference to the site-wide WebSite entity. * * @return array */ protected function get_website_reference() { return Schema_AI_ID_Builder::ref( Schema_AI_ID_Builder::website() ); } /** * Cross-reference to this page's own BreadcrumbList entity. * * @param int $post_id The WordPress post ID. * @return array */ protected function get_breadcrumb_reference( $post_id ) { return Schema_AI_ID_Builder::ref( Schema_AI_ID_Builder::breadcrumb( $post_id ) ); } /** * Cross-reference to this page's own FAQPage entity. * * @param int $post_id The WordPress post ID. * @return array */ protected function get_faq_reference( $post_id ) { return Schema_AI_ID_Builder::ref( Schema_AI_ID_Builder::faq_page( $post_id ) ); } /** * Get featured image data as a schema.org ImageObject. * * @param int $post_id The WordPress post ID. * @return array|string Schema.org ImageObject array, or empty string if none. */ protected function get_image_data( $post_id ) { $thumbnail_id = get_post_thumbnail_id( $post_id ); if ( ! $thumbnail_id ) { return ''; } $image = wp_get_attachment_image_src( $thumbnail_id, 'full' ); if ( ! $image ) { return ''; } return array( '@type' => 'ImageObject', 'url' => $image[0], 'width' => $image[1], 'height' => $image[2], ); } /** * Format a date string into ISO 8601. * * @param string $date Any date string parseable by strtotime(). * @return string ISO 8601 formatted date, or the original string on failure. */ protected function format_date( $date ) { if ( empty( $date ) ) { return ''; } $timestamp = strtotime( $date ); if ( false === $timestamp ) { return $date; } return gmdate( 'c', $timestamp ); } /** * Get the first image found in post content. * * @param string $content The post content HTML. * @return string Image URL or empty string. */ protected function get_first_content_image( $content ) { if ( preg_match( '/]+src=["\']([^"\']+)["\']/', $content, $matches ) ) { return $matches[1]; } return ''; } /** * Extract categories for a post. * * @param int $post_id The WordPress post ID. * @return array List of category names. */ protected function get_categories( $post_id ) { $categories = get_the_category( $post_id ); $names = array(); if ( ! empty( $categories ) && ! is_wp_error( $categories ) ) { foreach ( $categories as $cat ) { $names[] = $cat->name; } } return $names; } /** * Merge AI-detected fields into the schema, preferring existing values. * * @param array $schema The current schema array. * @param array $fields The AI-detected/extracted fields. * @param array $keys List of keys to merge. * @return array The merged schema. */ protected function merge_fields( $schema, $fields, $keys ) { foreach ( $keys as $key ) { if ( ! isset( $schema[ $key ] ) && isset( $fields[ $key ] ) && '' !== $fields[ $key ] ) { $schema[ $key ] = $fields[ $key ]; } } return $schema; } }