$fields Field values. * @param WP_Post $post The post. * @param Node_Context $c Page information. * @param Graph $graph The graph. * @return array|null */ public function build( string $type, array $fields, WP_Post $post, Node_Context $c, Graph $graph ): ?array { $name = $this->text( $fields, 'name', $this->title( $post ) ); $start = $this->text( $fields, 'start' ); $mode = $this->text( $fields, 'attendance', 'offline' ); $place = 'online' !== $mode ? $this->place( $fields ) : null; $virtual = 'offline' !== $mode ? $this->virtual_location( $fields ) : null; if ( '' === $name || '' === $start || ( null === $place && null === $virtual ) ) { return null; } $location = array_values( array_filter( array( $place, $virtual ) ) ); $status = $this->text( $fields, 'status' ); $organizer = $this->text( $fields, 'organizer_name' ); return array( '@type' => 'Event', '@id' => $c->id( 'event' ), 'mainEntityOfPage' => array( '@id' => $c->id( 'webpage' ) ), 'name' => $name, 'startDate' => $start, 'endDate' => $this->text( $fields, 'end' ), 'eventStatus' => '' !== $status ? 'https://schema.org/' . $status : null, 'eventAttendanceMode' => 'https://schema.org/' . ucfirst( $mode ) . 'EventAttendanceMode', 'location' => 1 === count( $location ) ? $location[0] : $location, 'description' => $this->text( $fields, 'description', $this->description( $post, $c ) ), 'image' => $this->image_url( $fields, 'image', $post ), 'organizer' => '' !== $organizer ? array( '@type' => 'Organization', 'name' => $organizer, 'url' => $this->text( $fields, 'organizer_url' ), ) : null, 'offers' => $this->offer( $fields, 'offer_', $c->url ), ); } /** * The Place, only with an address. * * @since 1.0.0 * * @param array $fields Field values. * @return array|null */ private function place( array $fields ): ?array { $street = $this->text( $fields, 'street' ); $city = $this->text( $fields, 'city' ); if ( '' === $street && '' === $city ) { return null; } return array( '@type' => 'Place', 'name' => $this->text( $fields, 'location_name' ), 'address' => array( '@type' => 'PostalAddress', 'streetAddress' => $street, 'addressLocality' => $city, 'addressRegion' => $this->text( $fields, 'region' ), 'postalCode' => $this->text( $fields, 'postal_code' ), 'addressCountry' => $this->text( $fields, 'country' ), ), ); } /** * The online location. * * @since 1.0.0 * * @param array $fields Field values. * @return array|null */ private function virtual_location( array $fields ): ?array { $url = $this->text( $fields, 'online_url' ); return '' === $url ? null : array( '@type' => 'VirtualLocation', 'url' => $url, ); } }