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; } // Start date (required by Google). if ( ! empty( $fields['startDate'] ) ) { $schema['startDate'] = $this->format_date( $fields['startDate'] ); } // End date. if ( ! empty( $fields['endDate'] ) ) { $schema['endDate'] = $this->format_date( $fields['endDate'] ); } // Event status. if ( ! empty( $fields['eventStatus'] ) ) { $schema['eventStatus'] = $this->format_event_status( $fields['eventStatus'] ); } else { $schema['eventStatus'] = 'https://schema.org/EventScheduled'; } // Event attendance mode. if ( ! empty( $fields['eventAttendanceMode'] ) ) { $schema['eventAttendanceMode'] = $this->format_attendance_mode( $fields['eventAttendanceMode'] ); } else { $schema['eventAttendanceMode'] = 'https://schema.org/OfflineEventAttendanceMode'; } // Location. if ( ! empty( $fields['location'] ) ) { $schema['location'] = $this->format_location( $fields['location'] ); } // Organizer. if ( ! empty( $fields['organizer'] ) ) { $organizer = $fields['organizer']; if ( is_string( $organizer ) ) { $organizer = array( '@type' => 'Organization', 'name' => $organizer, ); } elseif ( is_array( $organizer ) && ! isset( $organizer['@type'] ) ) { $organizer['@type'] = 'Organization'; } $schema['organizer'] = $organizer; } // Offers (tickets). if ( ! empty( $fields['offers'] ) ) { $offers = $fields['offers']; if ( is_array( $offers ) && ! isset( $offers['@type'] ) ) { $offers['@type'] = 'Offer'; } $schema['offers'] = $offers; } // Performer. if ( ! empty( $fields['performer'] ) ) { $performer = $fields['performer']; if ( is_string( $performer ) ) { $performer = array( '@type' => 'Person', 'name' => $performer, ); } elseif ( is_array( $performer ) && ! isset( $performer['@type'] ) ) { $performer['@type'] = 'Person'; } $schema['performer'] = $performer; } // Merge remaining AI-detected fields. $schema = $this->merge_fields( $schema, $fields, array( 'name', 'description', 'image', 'doorTime', 'duration', 'inLanguage', 'isAccessibleForFree', 'maximumAttendeeCapacity', 'previousStartDate', ) ); return $schema; } /** * Format event status to a full schema.org URL. * * @param string $status The event status. * @return string Full schema.org URL. */ private function format_event_status( $status ) { $statuses = array( 'EventScheduled' => 'https://schema.org/EventScheduled', 'EventCancelled' => 'https://schema.org/EventCancelled', 'EventMovedOnline' => 'https://schema.org/EventMovedOnline', 'EventPostponed' => 'https://schema.org/EventPostponed', 'EventRescheduled' => 'https://schema.org/EventRescheduled', ); if ( isset( $statuses[ $status ] ) ) { return $statuses[ $status ]; } // Already a full URL. if ( 0 === strpos( $status, 'https://schema.org/' ) ) { return $status; } return 'https://schema.org/' . $status; } /** * Format attendance mode to a full schema.org URL. * * @param string $mode The attendance mode. * @return string Full schema.org URL. */ private function format_attendance_mode( $mode ) { $modes = array( 'OfflineEventAttendanceMode' => 'https://schema.org/OfflineEventAttendanceMode', 'OnlineEventAttendanceMode' => 'https://schema.org/OnlineEventAttendanceMode', 'MixedEventAttendanceMode' => 'https://schema.org/MixedEventAttendanceMode', ); if ( isset( $modes[ $mode ] ) ) { return $modes[ $mode ]; } if ( 0 === strpos( $mode, 'https://schema.org/' ) ) { return $mode; } return 'https://schema.org/' . $mode; } /** * Format location data into a Place object. * * @param array|string $location Location data. * @return array Schema.org Place object. */ private function format_location( $location ) { if ( is_string( $location ) ) { return array( '@type' => 'Place', 'name' => $location, ); } if ( is_array( $location ) ) { if ( ! isset( $location['@type'] ) ) { $location['@type'] = 'Place'; } // Format nested address. if ( isset( $location['address'] ) && is_array( $location['address'] ) && ! isset( $location['address']['@type'] ) ) { $location['address']['@type'] = 'PostalAddress'; } return $location; } return $location; } }