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'], 'uploadDate' => $post_data['date'], ); // Thumbnail from featured image. $image = $this->get_image_data( $post_id ); if ( ! empty( $image ) ) { $schema['thumbnailUrl'] = is_array( $image ) ? $image['url'] : $image; } // Detect video embeds in content. $video_data = $this->detect_video_embeds( $post_data['content'] ); if ( ! empty( $video_data ) ) { if ( ! empty( $video_data['contentUrl'] ) ) { $schema['contentUrl'] = $video_data['contentUrl']; } if ( ! empty( $video_data['embedUrl'] ) ) { $schema['embedUrl'] = $video_data['embedUrl']; } if ( ! empty( $video_data['thumbnailUrl'] ) && empty( $schema['thumbnailUrl'] ) ) { $schema['thumbnailUrl'] = $video_data['thumbnailUrl']; } } // Duration. if ( ! empty( $fields['duration'] ) ) { $schema['duration'] = $this->format_duration( $fields['duration'] ); } // Content URL from fields. if ( ! empty( $fields['contentUrl'] ) ) { $schema['contentUrl'] = $fields['contentUrl']; } // Embed URL from fields. if ( ! empty( $fields['embedUrl'] ) ) { $schema['embedUrl'] = $fields['embedUrl']; } // Thumbnail from fields. if ( ! empty( $fields['thumbnailUrl'] ) ) { $schema['thumbnailUrl'] = $fields['thumbnailUrl']; } // Author. $author = $this->get_author_data( $post_id ); if ( ! empty( $fields['author'] ) ) { $author = $fields['author']; if ( is_string( $author ) ) { $author = array( '@type' => 'Person', 'name' => $author, ); } elseif ( is_array( $author ) && ! isset( $author['@type'] ) ) { $author['@type'] = 'Person'; } } if ( ! empty( $author ) ) { $schema['author'] = $author; } // Publisher. $publisher = $this->get_publisher_data(); if ( ! empty( $publisher ) ) { $schema['publisher'] = $publisher; } // Merge remaining AI-detected fields. $schema = $this->merge_fields( $schema, $fields, array( 'name', 'description', 'thumbnailUrl', 'uploadDate', 'duration', 'contentUrl', 'embedUrl', 'interactionStatistic', 'expires', 'isFamilyFriendly', 'inLanguage', ) ); return $schema; } /** * Detect YouTube and Vimeo video embeds in content. * * @param string $content Post content HTML. * @return array Video data with contentUrl, embedUrl, and thumbnailUrl. */ private function detect_video_embeds( $content ) { $data = array(); // YouTube - watch URL. if ( preg_match( '/youtube\.com\/watch\?v=([a-zA-Z0-9_-]+)/', $content, $yt_match ) ) { $video_id = $yt_match[1]; $data['contentUrl'] = 'https://www.youtube.com/watch?v=' . $video_id; $data['embedUrl'] = 'https://www.youtube.com/embed/' . $video_id; $data['thumbnailUrl'] = 'https://img.youtube.com/vi/' . $video_id . '/maxresdefault.jpg'; return $data; } // YouTube - short URL. if ( preg_match( '/youtu\.be\/([a-zA-Z0-9_-]+)/', $content, $yt_short ) ) { $video_id = $yt_short[1]; $data['contentUrl'] = 'https://www.youtube.com/watch?v=' . $video_id; $data['embedUrl'] = 'https://www.youtube.com/embed/' . $video_id; $data['thumbnailUrl'] = 'https://img.youtube.com/vi/' . $video_id . '/maxresdefault.jpg'; return $data; } // YouTube - embed URL. if ( preg_match( '/youtube\.com\/embed\/([a-zA-Z0-9_-]+)/', $content, $yt_embed ) ) { $video_id = $yt_embed[1]; $data['contentUrl'] = 'https://www.youtube.com/watch?v=' . $video_id; $data['embedUrl'] = 'https://www.youtube.com/embed/' . $video_id; $data['thumbnailUrl'] = 'https://img.youtube.com/vi/' . $video_id . '/maxresdefault.jpg'; return $data; } // Vimeo. if ( preg_match( '/vimeo\.com\/(\d+)/', $content, $vimeo_match ) ) { $video_id = $vimeo_match[1]; $data['contentUrl'] = 'https://vimeo.com/' . $video_id; $data['embedUrl'] = 'https://player.vimeo.com/video/' . $video_id; return $data; } // Vimeo player embed. if ( preg_match( '/player\.vimeo\.com\/video\/(\d+)/', $content, $vimeo_embed ) ) { $video_id = $vimeo_embed[1]; $data['contentUrl'] = 'https://vimeo.com/' . $video_id; $data['embedUrl'] = 'https://player.vimeo.com/video/' . $video_id; return $data; } // Generic video tag. if ( preg_match( '/]+src=["\']([^"\']+)["\']/', $content, $video_tag ) ) { $data['contentUrl'] = $video_tag[1]; return $data; } // Source inside video tag. if ( preg_match( '/]+src=["\']([^"\']+)["\']/', $content, $source_tag ) ) { $data['contentUrl'] = $source_tag[1]; return $data; } return $data; } /** * Format a duration value into ISO 8601 duration. * * @param mixed $duration Duration in seconds, or already-formatted string. * @return string ISO 8601 duration string. */ private function format_duration( $duration ) { if ( is_string( $duration ) && 0 === strpos( $duration, 'PT' ) ) { return $duration; } $seconds = (int) $duration; if ( $seconds <= 0 ) { return (string) $duration; } $hours = floor( $seconds / 3600 ); $minutes = floor( ( $seconds % 3600 ) / 60 ); $secs = $seconds % 60; $iso = 'PT'; if ( $hours > 0 ) { $iso .= $hours . 'H'; } if ( $minutes > 0 ) { $iso .= $minutes . 'M'; } if ( $secs > 0 ) { $iso .= $secs . 'S'; } return $iso; } }