Skip to content

Instantly share code, notes, and snippets.

@code-flow
Created February 15, 2022 11:36
Show Gist options
  • Save code-flow/a37266b57c15d622f6ef694d94c9713e to your computer and use it in GitHub Desktop.
Save code-flow/a37266b57c15d622f6ef694d94c9713e to your computer and use it in GitHub Desktop.
Connects the Events Manager plugin with SNIP.
<?php
/*
Plugin Name: SNIP bridge to the Events Manager Plugin.
Description: Bridge-Plugin to connect SNIP with the Events Manager Plugin.
Author: Florian Simeth
Version: 0.1.0
Author URI: https://rich-snippets.io
Plugin URI: https://rich-snippets.io/how-to-generate-structured-data-for-the-events-manager-plugin/
*/
namespace snip\bridge\events_manager;
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
add_filter(
'wpbuddy/rich_snippets/fields/internal_subselect/values',
'snip\bridge\events_manager\subselects',
30
);
/**
* Adds new field to use in Global Snippets in the SNIP plugin.
*
* @param array $values
*
* @return array
* @since 0.1.0
*
*/
function subselects( $values ) {
$values['http://schema.org/PostalAddress'][] = [
'id' => 'events_manager_location',
'label' => esc_html_x( 'Events Manager: Location', 'subselect field', 'snip-b-events-manager' ),
'method' => 'snip\bridge\events_manager\location',
];
$values['http://schema.org/Date'][] = [
'id' => 'events_manager_date_start',
'label' => esc_html_x( 'Events Manager: Start date and time', 'subselect field', 'snip-b-events-manager' ),
'method' => 'snip\bridge\events_manager\date_start',
];
$values['http://schema.org/Date'][] = [
'id' => 'events_manager_date_end',
'label' => esc_html_x( 'Events Manager: End date and time', 'subselect field', 'snip-b-events-manager' ),
'method' => 'snip\bridge\events_manager\date_end',
];
$values['http://schema.org/EventAttendanceModeEnumeration'][] = [
'id' => 'events_manager_date_end',
'label' => esc_html_x( 'Events Manager: End date and time', 'subselect field', 'snip-b-events-manager' ),
'method' => 'snip\bridge\events_manager\date_end',
];
return $values;
}
function get_location_from_post( $post_id ) {
$location_id = (int) get_post_meta( $post_id, '_location_id', true );
if ( $location_id <= 0 ) {
return false;
}
if ( ! function_exists( '\em_get_location' ) ) {
return false;
}
/**
* @var \EM_Location $location
*/
$location = em_get_location( $location_id );
if ( ! $location instanceof \EM_Location ) {
return false;
}
return $location;
}
function location( $val, \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet, array $meta_info ) {
$location_type = (string) get_post_meta( $meta_info['current_post_id'], '_event_location_type', true );
$o = new \stdClass();
$o->{'@context'} = "https://schema.org";
if ( 'url' === $location_type ) {
$location_url = (string) get_post_meta( $meta_info['current_post_id'], '_event_location_url', true );
if ( empty( $location_url ) ) {
return '';
}
$o->{'@type'} = "VirtualLocation";
$o->url = esc_url( $location_url );
return $o;
}
$location = get_location_from_post( $meta_info['current_post_id'] );
if ( ! $location ) {
return '';
}
$o->{'@type'} = "Place";
$o->name = $location->name;
$o->address = new \stdClass();
$o->address->{'@context'} = "https://schema.org";
$o->address->{'@type'} = "PostalAddress";
$o->address->postalCode = $location->location_postcode;
$o->address->streetAddress = $location->location_address;
$o->address->addressLocality = $location->location_town;
$o->address->addressCountry = $location->location_country;
$o->address->addressRegion = $location->location_region;
return $o;
}
function date_start( $val, \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet, array $meta_info ) {
$is_all_day_event = 1 === (int) get_post_meta( $meta_info['current_post_id'], '_event_all_day', true );
$start_date = get_post_meta( $meta_info['current_post_id'], '_event_start', true );
$timezone = get_post_meta( $meta_info['current_post_id'], '_event_timezone', true );
try {
$start_date = new \DateTime(
$start_date,
new \DateTimeZone( $timezone )
);
} catch ( \Exception $e ) {
return '';
}
if ( $is_all_day_event ) {
return $start_date->format( 'o-m-d' );
}
return $start_date->format( 'c' );
}
function date_end( $val, \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet, array $meta_info ) {
$is_all_day_event = 1 === (int) get_post_meta( $meta_info['current_post_id'], '_event_all_day', true );
$end_date = get_post_meta( $meta_info['current_post_id'], '_event_end', true );
$timezone = get_post_meta( $meta_info['current_post_id'], '_event_timezone', true );
try {
$end_date = new \DateTime(
$end_date,
new \DateTimeZone( $timezone )
);
} catch ( \Exception $e ) {
return '';
}
if ( $is_all_day_event ) {
return $end_date->format( 'o-m-d' );
}
return $end_date->format( 'c' );
}
@code-flow
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment