Skip to content

Commit

Permalink
Move post type registration into includes file. Add transient caching…
Browse files Browse the repository at this point in the history
… logic #1
  • Loading branch information
bobbingwide committed Nov 24, 2021
1 parent 202fe2e commit 713bbc3
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 59 deletions.
70 changes: 11 additions & 59 deletions ebps-meetings.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,69 +40,21 @@ function ebps_init() {
}

/**
* Registers the post type if it's not already registered.
* Implements the [ebps-meetings] shortcode.
*
* We could have hooked into `registered_post_type` and checked the post types that have been registered
* but that's less efficient than waiting until other plugins have done their bit
* then checking if the post type is already registered.
* If it's not been registered then register it.
* Note: The values were obtained by seeing what the parameters are to `tribe_events_register_event_type_args`.
* @param $atts
* @param $content
* @param $tag
* @return string
*/
function ebps_maybe_register_tribe_events() {
if ( post_type_exists( 'tribe_events') ) {
return;
}
$post_type_args = [
'rewrite' => [ 'slug' => "event", 'with_front' => false ]
, 'menu_position' => 6
, 'supports' => [ 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'custom-fields', 'comments', 'revisions']
// [taxonomies] => Array [0] => (string) "post_tag"
// [capability_type] => Array
//
// [0] => (string) "tribe_event"
// [1] => (string) "tribe_events"
, 'map_meta_cap' => true
, 'has_archive' => true
, 'menu_icon' => "dashicons-calendar"
, 'show_in_rest' => true
, 'labels' => [
'name' => "Events"
, 'singular_name' => "Event"
, 'add_new' => "Add New"
, 'add_new_item' => "Add New Event"
, 'edit_item' => "Edit Event"
, 'new_item' => "New Event"
, 'view_item' => "View Event"
, 'search_items' => "Search Events"
, 'not_found' => "No events found"
, 'not_found_in_trash' => "No events found in Trash"
, 'item_published' => "Event published."
, 'item_published_privately' => "Event published privately."
, 'item_reverted_to_draft' => "Event reverted to draft."
, 'item_scheduled' => "Event scheduled."
, 'item_updated' => "Event updated."
, 'item_link' => "Event Link"
, 'item_link_description' => "A link to a particular Event."
]
];
register_post_type( 'tribe_events', $post_type_args );
}

function ebps_meetings_shortcode( $atts, $content, $tag ) {
ebps_maybe_register_tribe_events();
require_once __DIR__ . '/includes/ebps-tribe-events.php';
$events = ebps_get_upcoming_events();
$html = '<div class="tribe-common tribe-events tribe-events-view tribe-events-view--widget-events-list tribe-events-widget">';
$html .= ebps_meetings_upcoming_events( $events );
$html .= '</div>';
$html .= ebps_meetings_view_calendar_link();
return $html;
}

function ebps_meetings_view_calendar_link() {
$html = '<a href="';
$html .= home_url('events');
$html .= '">View Calendar</a>';
$html = ebps_get_cached_meetings();
if ( false === $html ) {
ebps_maybe_register_tribe_events();
$html = ebps_meetings_lazy_shortcode($atts, $content, $tag);
ebps_save_cached_meetings($html);
}
return $html;
}

Expand Down
82 changes: 82 additions & 0 deletions includes/ebps-tribe-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,86 @@ function ebps_get_event_date( $event, $meta_key='_EventStartDate' ) {
$_EventDate = get_post_meta( $event->ID, $meta_key, true);
$datetime = strtotime( $_EventDate);
return $datetime;
}

/**
* Registers the post type if it's not already registered.
*
* We could have hooked into `registered_post_type` and checked the post types that have been registered
* but that's less efficient than waiting until other plugins have done their bit
* then checking if the post type is already registered.
* If it's not been registered then register it.
* Note: The values were obtained by seeing what the parameters are to `tribe_events_register_event_type_args`.
*/
function ebps_maybe_register_tribe_events() {
if ( post_type_exists( 'tribe_events') ) {
return;
}
$post_type_args = [
'rewrite' => [ 'slug' => "event", 'with_front' => false ]
, 'menu_position' => 6
, 'supports' => [ 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'custom-fields', 'comments', 'revisions']
// [taxonomies] => Array [0] => (string) "post_tag"
// [capability_type] => Array
//
// [0] => (string) "tribe_event"
// [1] => (string) "tribe_events"
, 'map_meta_cap' => true
, 'has_archive' => true
, 'menu_icon' => "dashicons-calendar"
, 'show_in_rest' => true
, 'labels' => [
'name' => "Events"
, 'singular_name' => "Event"
, 'add_new' => "Add New"
, 'add_new_item' => "Add New Event"
, 'edit_item' => "Edit Event"
, 'new_item' => "New Event"
, 'view_item' => "View Event"
, 'search_items' => "Search Events"
, 'not_found' => "No events found"
, 'not_found_in_trash' => "No events found in Trash"
, 'item_published' => "Event published."
, 'item_published_privately' => "Event published privately."
, 'item_reverted_to_draft' => "Event reverted to draft."
, 'item_scheduled' => "Event scheduled."
, 'item_updated' => "Event updated."
, 'item_link' => "Event Link"
, 'item_link_description' => "A link to a particular Event."
]
];
register_post_type( 'tribe_events', $post_type_args );
}

function ebps_meetings_lazy_shortcode( $atts, $content, $tag ) {
$events = ebps_get_upcoming_events();
$html = '<div class="tribe-common tribe-events tribe-events-view tribe-events-view--widget-events-list tribe-events-widget">';
$html .= ebps_meetings_upcoming_events($events);
$html .= '</div>';
$html .= ebps_meetings_view_calendar_link();
return $html;
}


function ebps_meetings_view_calendar_link() {
$html = '<a href="';
$html .= home_url('events');
$html .= '">View Calendar</a>';
return $html;
}

/**
* Returns the cached output from [ebps-meetings]
*
* @return mixed cached HTML or false
*/

function ebps_get_cached_meetings() {
$html = get_transient( 'ebps-meetings' );
return $html;
}

function ebps_save_cached_meetings( $html ) {
$result = set_transient( 'ebps-meetings', $html, 86400 );
bw_trace2( $result, "result", true, BW_TRACE_VERBOSE );
}

0 comments on commit 713bbc3

Please sign in to comment.