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'], 'url' => $post_data['url'], ); // Image. $image = $this->get_image_data( $post_id ); if ( ! empty( $image ) ) { $schema['image'] = $image; } // Provider. if ( ! empty( $fields['provider'] ) ) { $provider = $fields['provider']; if ( is_string( $provider ) ) { $provider = array( '@type' => 'Organization', 'name' => $provider, ); } elseif ( is_array( $provider ) && ! isset( $provider['@type'] ) ) { $provider['@type'] = 'Organization'; } $schema['provider'] = $provider; } else { // Default provider from site settings. $schema['provider'] = $this->get_publisher_data(); } // Instructor. if ( ! empty( $fields['instructor'] ) ) { $instructor = $fields['instructor']; if ( is_string( $instructor ) ) { $instructor = array( '@type' => 'Person', 'name' => $instructor, ); } elseif ( is_array( $instructor ) && ! isset( $instructor['@type'] ) ) { $instructor['@type'] = 'Person'; } $schema['instructor'] = $instructor; } else { // Fall back to post author. $author = $this->get_author_data( $post_id ); if ( ! empty( $author ) ) { $schema['instructor'] = $author; } } // Course code. if ( ! empty( $fields['courseCode'] ) ) { $schema['courseCode'] = $fields['courseCode']; } // Course instances. if ( ! empty( $fields['hasCourseInstance'] ) ) { $schema['hasCourseInstance'] = $this->format_course_instances( $fields['hasCourseInstance'] ); } // Offers. if ( ! empty( $fields['offers'] ) ) { $offers = $fields['offers']; if ( is_array( $offers ) && ! isset( $offers['@type'] ) ) { $offers['@type'] = 'Offer'; } $schema['offers'] = $offers; } // Aggregate rating. if ( ! empty( $fields['aggregateRating'] ) ) { $rating = $fields['aggregateRating']; if ( is_array( $rating ) && ! isset( $rating['@type'] ) ) { $rating['@type'] = 'AggregateRating'; } $schema['aggregateRating'] = $rating; } // Merge remaining AI-detected fields. $schema = $this->merge_fields( $schema, $fields, array( 'name', 'description', 'url', 'coursePrerequisites', 'educationalCredentialAwarded', 'numberOfCredits', 'occupationalCredentialAwarded', 'timeRequired', 'inLanguage', 'availableLanguage', 'about', 'teaches', 'financialAidEligible', ) ); return $schema; } /** * Format course instance data. * * @param array $instances Course instance data. * @return array Formatted CourseInstance objects. */ private function format_course_instances( $instances ) { if ( ! is_array( $instances ) ) { return $instances; } // Single instance. if ( isset( $instances['courseMode'] ) || isset( $instances['startDate'] ) ) { if ( ! isset( $instances['@type'] ) ) { $instances['@type'] = 'CourseInstance'; } return $instances; } // Multiple instances. $formatted = array(); foreach ( $instances as $instance ) { if ( is_array( $instance ) ) { if ( ! isset( $instance['@type'] ) ) { $instance['@type'] = 'CourseInstance'; } $formatted[] = $instance; } } return $formatted; } }