Skip to content

Instantly share code, notes, and snippets.

@joshfeck
Last active February 6, 2020 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshfeck/4ecebc7171dbd7c7a34ffc87e6d5b34d to your computer and use it in GitHub Desktop.
Save joshfeck/4ecebc7171dbd7c7a34ffc87e6d5b34d to your computer and use it in GitHub Desktop.
Past events grid shortcode plugin. Event Espresso 4 + the Grid template add-on
<?php
/**
* @package EE_Event_List_Past_Events_Grid_Shortcode
* @version 1.0
*/
/*
Plugin Name: Event Espresso Past Events Grid Shortcode
Plugin URI: https://github.com/eventespresso/ee-code-snippet-library
Description: Add [espresso_past_events_grid] shortcode to a post or page to display a grid of past events.
Author: Josh Feck
Version: 1.0
Author URI: https://github.com/joshfeck
*/
add_action('wp_enqueue_scripts', 'grid_load_event_grid_assets', 9);
function grid_load_event_grid_assets() {
if(class_exists('EES_Espresso_Grid_Template')) {
add_action('wp_enqueue_scripts', array(EES_Espresso_Grid_Template::instance(), 'enqueue_scripts'), 10);
}
}
function grid_ee_past_events_shortcode($args) {
if(!class_exists('EventEspresso\core\domain\services\wp_queries\EventListQuery')){
return 'no event query class';
}
if(!defined('EE_GRID_TEMPLATE_VERSION')) {
return;
}
$args = shortcode_atts(
array(
'limit' => 20,
'show_expired' => TRUE,
'category_slug' => NULL,
'sort' => 'DESC',
'order_by' => 'start_date',
),
$args,
'espresso_past_events_grid'
);
add_filter( 'posts_where', 'ee_pels_custom_posts_where_sql_for_only_expired' );
$loop = new EventEspresso\core\domain\services\wp_queries\EventListQuery( $args );
$big = 999999999; // need an unlikely integer
$pagination = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages,
'show_all' => TRUE,
'end_size' => 10,
'mid_size' => 6,
'prev_next' => TRUE,
'prev_text' => '&lsaquo; PREV',
'next_text' => 'NEXT &rsaquo;',
'type' => 'plain',
'add_args' => FALSE,
'add_fragment' => ''
));
ob_start();
include_once( plugin_dir_path( __FILE__ ) . '/grid-shortcode-template.php');
return ob_get_clean();
}
add_shortcode('espresso_past_events_grid', 'grid_ee_past_events_shortcode');
function ee_pels_custom_posts_where_sql_for_only_expired($where) {
$where .= ' AND ' . EEM_Datetime::instance()->table() . '.DTT_EVT_end < "' . current_time( 'mysql', TRUE ) . '" ';
return $where;
}
<?php
// Options
$date_format = get_option( 'date_format' );
$time_format = get_option( 'time_format' );
$reg_button_text = !isset($button_text) ? __('View Details', 'event_espresso') : $button_text;
$alt_button_text = !isset($alt_button_text) ? __('View Details', 'event_espresso') : $alt_button_text;//For alternate registration pages
if ( $loop->have_posts() ) :
// allow other stuff
do_action( 'AHEE__espresso_grid_template_template__before_loop' );
?>
<div style="clear:both" class="wrapper ee-clearfix">
<?php
// Start the Loop.
while ( $loop->have_posts() ) : $loop->the_post();
// Include the post TYPE-specific template for the content.
global $post;
//Create the event link
$external_url = $post->EE_Event->external_url();
$button_text = !empty($external_url) ? $alt_button_text : $reg_button_text;
$registration_url = !empty($external_url) ? $post->EE_Event->external_url() : $post->EE_Event->get_permalink();
$feature_image_url = $post->EE_Event->feature_image_url();
if(!isset($default_image) || $default_image == '') {
$default_image = EE_GRID_TEMPLATE_URL .'images/default.jpg';
}
$image = !empty($feature_image_url) ? $feature_image_url : $default_image;
$datetimes = EEM_Datetime::instance()->get_datetimes_for_event_ordered_by_start_time( $post->ID, true, false, 1 );
$datetime = end( $datetimes );
if ($datetime instanceof EE_Datetime) {
$start_date = date_i18n( $date_format, strtotime( $datetime->start_date('Y-m-d') ) );
$start_time = date_i18n( $time_format, strtotime( $datetime->start_time('H:i:s') ) );
?>
<div id="event-id-<?php echo $post->ID; ?>" class="ee_grid_box_v2 item">
<?php do_action( 'AHEE__espresso_grid_template_template__grid_item_start', $post ); ?>
<img src="<?php echo $image; ?>" alt="<?php echo sprintf( esc_attr__( '%s Feature Image', 'event_espresso'), $post->post_title ); ?>" />
<div onclick="" class="darken ee_overlay">
<p class="event-link"><?php echo '<a class="register-link button" id="a_register_link-' . $post->ID .'" href="' . $registration_url . '">' . $button_text . '</a>'; ?></p>
<div class="event-title title"><?php echo $post->post_title; ?></div>
<p class="start-date">
<span class="event-start-date"><?php echo $start_date; ?></span>
&nbsp;
<span class="event-start-time"><?php echo $start_time; ?></span>
</p>
</div>
<?php do_action( 'AHEE__espresso_grid_template_template__grid_item_end', $post ); ?>
</div>
<?php
}
endwhile;
echo '</div>';
// pagination links
if(! empty($pagination)) {
echo '<div class="ee-pagination-dv clear">' . $pagination . '</div>';
}
// allow moar other stuff
do_action( 'AHEE__espresso_grid_template_template__after_loop' );
else :
echo 'Nothing found';
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment