Last active
April 23, 2023 19:14
-
-
Save robwent/2b632ac8a2229384a8bb2aff7301cec4 to your computer and use it in GitHub Desktop.
Extends Yoast schema data to add events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use \Yoast\WP\SEO\Generators\Schema\Abstract_Schema_Piece; | |
class Schema_Event extends Abstract_Schema_Piece { | |
/** | |
* Determines whether or not a piece should be added to the graph. | |
* | |
* @return bool | |
*/ | |
public function is_needed() { | |
if ( is_singular( 'event' ) ) { | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Adds our Event piece of the graph. | |
* | |
* @return array $graph Event Schema markup | |
*/ | |
public function generate() { | |
$wa_add_event_structured_data = get_post_meta( $this->context->id, 'wa_add_event_structured_data', true ); | |
if ( empty( $wa_add_event_structured_data ) ) { | |
//No details added | |
return false; | |
} | |
$data = $this->build_event_data(); | |
return $data; | |
} | |
/** | |
* Builds our array of Schema Event data for a given post ID. | |
* | |
* @return array An array of Schema Event data. | |
*/ | |
protected function build_event_data() { | |
$event_name = get_post_meta( $this->context->id, 'wa_event_name', true ); | |
$wa_event_location = get_post_meta( $this->context->id, 'wa_event_location', true ); | |
$wa_event_street_address = get_post_meta( $this->context->id, 'wa_event_street_address', true ); | |
$wa_event_locality = get_post_meta( $this->context->id, 'wa_event_locality', true ); | |
$wa_event_zip = get_post_meta( $this->context->id, 'wa_event_zip', true ); | |
$wa_event_region = get_post_meta( $this->context->id, 'wa_event_region', true ); | |
$wa_event_country = get_post_meta( $this->context->id, 'wa_event_country', true ); | |
$wa_event_start_date = get_post_meta( $this->context->id, 'wa_event_start_date', true ); | |
$wa_event_end_date = get_post_meta( $this->context->id, 'wa_event_end_date', true ); | |
$wa_event_description = get_post_meta( $this->context->id, 'wa_event_description', true ); | |
$image = false; | |
$has_featured_image = has_post_thumbnail( $this->context->id ); | |
if ( $has_featured_image ) { | |
$image = wp_prepare_attachment_for_js( get_post_thumbnail_id( $this->context->id ) ); | |
} | |
$data = array( | |
'@type' => 'Event', | |
'@id' => $this->context->canonical . '#event', | |
'name' => $event_name, | |
'startDate' => date( 'Y-m-d', strtotime( $wa_event_start_date ) ), | |
'location' => array( | |
'@type' => 'Place', | |
'name' => $wa_event_location, | |
'address' => array( | |
'@type' => 'PostalAddress', | |
"streetAddress" => $wa_event_street_address, | |
"addressLocality" => $wa_event_locality, | |
"postalCode" => $wa_event_zip, | |
"addressRegion" => $wa_event_region, | |
"addressCountry" => $wa_event_country | |
) | |
) | |
); | |
if ( $wa_event_end_date ) { | |
$data['endDate'] = date( 'Y-m-d', strtotime( $wa_event_end_date ) ); | |
} | |
if ( $wa_event_description ) { | |
$data['description'] = $wa_event_description; | |
} | |
if ( $image ) { | |
$data['image'] = array( | |
'@type' => 'ImageObject', | |
'url' => $image['url'], | |
'height' => $image['height'], | |
'width' => $image['width'] | |
); | |
} | |
return $data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment