get_post_data( $post_id ); if ( empty( $post_data ) ) { return array( '@type' => $this->type ); } $schema = array( '@type' => $this->type, 'name' => $post_data['title'], 'description' => $post_data['excerpt'], 'datePublished' => $post_data['date'], ); // Use featured image as primary source. $thumbnail_id = get_post_thumbnail_id( $post_id ); if ( $thumbnail_id ) { $image_data = $this->get_attachment_details( $thumbnail_id ); if ( ! empty( $image_data ) ) { $schema['contentUrl'] = $image_data['url']; $schema['url'] = $image_data['url']; $schema['width'] = $image_data['width']; $schema['height'] = $image_data['height']; if ( ! empty( $image_data['caption'] ) ) { $schema['caption'] = $image_data['caption']; } } } else { // Fallback to first image in content. $first_image = $this->get_first_content_image( $post_data['content'] ); if ( ! empty( $first_image ) ) { $schema['contentUrl'] = $first_image; $schema['url'] = $first_image; // Try to get dimensions from the attachment. $attachment_id = $this->url_to_attachment_id( $first_image ); if ( $attachment_id ) { $details = $this->get_attachment_details( $attachment_id ); if ( ! empty( $details ) ) { $schema['width'] = $details['width']; $schema['height'] = $details['height']; } } } } // Author. $author = $this->get_author_data( $post_id ); if ( ! empty( $author ) ) { $schema['author'] = $author; } // Override with AI-detected fields. if ( ! empty( $fields['contentUrl'] ) ) { $schema['contentUrl'] = $fields['contentUrl']; } if ( ! empty( $fields['url'] ) ) { $schema['url'] = $fields['url']; } if ( ! empty( $fields['width'] ) ) { $schema['width'] = $fields['width']; } if ( ! empty( $fields['height'] ) ) { $schema['height'] = $fields['height']; } // Merge remaining AI-detected fields. $schema = $this->merge_fields( $schema, $fields, array( 'name', 'description', 'author', 'datePublished', 'caption', 'representativeOfPage', 'license', 'acquireLicensePage', 'creditText', 'copyrightHolder', 'copyrightYear', 'encodingFormat', ) ); return $schema; } /** * Get detailed attachment data. * * @param int $attachment_id WordPress attachment ID. * @return array Attachment details. */ private function get_attachment_details( $attachment_id ) { $image = wp_get_attachment_image_src( $attachment_id, 'full' ); if ( ! $image ) { return array(); } $attachment = get_post( $attachment_id ); $caption = $attachment ? $attachment->post_excerpt : ''; return array( 'url' => $image[0], 'width' => $image[1], 'height' => $image[2], 'caption' => $caption, ); } /** * Attempt to get the attachment ID from an image URL. * * @param string $url The image URL. * @return int|null Attachment ID or null. */ private function url_to_attachment_id( $url ) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Required for custom table; no WP API alternative exists. $attachment_id = $wpdb->get_var( $wpdb->prepare( 'SELECT ID FROM ' . $wpdb->posts . " WHERE guid = %s AND post_type = 'attachment' LIMIT 1", $url ) ); return $attachment_id ? (int) $attachment_id : null; } }