Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active October 20, 2022 08:16
Embed
What would you like to do?
Events Manager - List event attendees shortcode
<?php
/*
Plugin Name: Events Manager - List event attendees shortcode
Plugin URI: https://www.damiencarbery.com/
Description: Experiment with listing the attendees of an Events Manager event.
Author: Damien Carbery
Version: 0.1
*/
add_shortcode( 'event_attendees', 'dcwd_event_attendees' );
function dcwd_event_attendees() {
$event_id = 1; // 'Monday event'
ob_start();
echo '<style>pre { font-family: monospace; }
.entry-content td { border: 1px solid rgba(0,0,0,.1); }
</style>';
$event = em_get_event( $event_id );
if (!empty($event->event_name)) {
echo '<p>Event name: <strong>' . $event->event_name . '</strong></p>', "\n";
}
else {
echo '<p>No event name (', $event->event_name, ')</p>', "\n";
}
$show_tickets = true;
$delimiter = !defined('EM_CSV_DELIMITER') ? ',' : EM_CSV_DELIMITER;
$i = 1; // Debug variable.
echo '<table><tr><th>Booking #</th><th>Booked by</th><th>Spaces booked</th></tr>';
foreach ( $event->get_bookings() as $booking ) {
if ( 'Approved' == $booking->get_status() ) {
$person = $booking->get_person();
$EM_Tickets_Bookings = $booking->get_tickets_bookings();
printf( '<tr><td>%d</td><td>%s</td><td>%d</td></tr>', $i, $person->get_name(), $EM_Tickets_Bookings->get_spaces() );
$i++;
}
}
echo '</table>';
return ob_get_clean();
}
<?php $time_start = microtime(true); ?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8" />
<title>Events Manager bookings</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
table { border-collapse: collapse; }
td, th { border: 1px solid rgba(0,0,0,.1); padding: 8px; }
.stats { border-top: 1px solid rgba(0,0,0,.1); font-size: 80%; margin-top: 2em; }
pre { font-family: monospace; }
</style>
</head>
<body>
<h1>Events Manager bookings</h1>
<?php
define('WP_USE_THEMES', false);
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
//echo '<pre>', var_export( $_GET, true ), '</pre>';
$event_id = null;
if ( isset( $_GET['event_id'] ) && ( 0 != intval( $_GET['event_id'] ) ) ) {
$event_id = intval( $_GET['event_id'] );
//echo '<p><strong>Event ID:</strong> ', $event_id, '</p>';
}
else {
// Find post id and event id for future events as an unordered list.
$format = '<li><a href="?event_id=#_EVENTID">#_EVENTNAME</a></li>';
$args = array( 'scope'=>'future', 'format'=>$format, 'format_header'=>null, 'format_footer'=>null, 'limit'=>50);
$events_html = em_get_events( $args, true );
if ( ! empty( $events_html ) ) {
?>
<p>Choose an event to view.</p>
<ul><?php echo $events_html; ?></ul>
<?php
}
?>
<!--<form action="GET">
<p><label>Event ID: <input type="text" name="event_id" /></label></p>
<p><input type="submit" value="View bookings" /></p>
</form>-->
<?php
}
//$event_id = 1; // 'Monday event'
if ( null != $event_id ) {
$event = em_get_event( $event_id );
if ( empty($event->event_id) ) {
echo '<p>Not a valid event.</p>';
//echo '<pre>', var_export( $event, true ), '</pre>';
}
else {
echo '<p>Event name: <strong>' . $event->event_name . '</strong></p>', "\n";
$show_tickets = true;
$delimiter = !defined('EM_CSV_DELIMITER') ? ',' : EM_CSV_DELIMITER;
$i = 1; // Debug variable.
$booking_info = array();
foreach ( $event->get_bookings() as $booking ) {
if ( 'Approved' == $booking->get_status() ) {
$person = $booking->get_person();
$EM_Tickets_Bookings = $booking->get_tickets_bookings();
$booking_info[] = sprintf( '<tr><td>%d</td><td>%s</td><td>%d</td></tr>', $i, $person->get_name(), $EM_Tickets_Bookings->get_spaces() );
$i++;
}
}
if ( count( $booking_info ) ) {
echo '<table><tr><th>Booking #</th><th>Booked by</th><th>Spaces booked</th></tr>';
echo implode( "\n", $booking_info );
echo '</table>';
}
else {
echo '<p>This event does not have any bookings.</p>';
}
}
}
// Display some stats.
$time_end = microtime(true);
$time = $time_end - $time_start;
?>
<div class="stats">
<p>Memory usage: <?php echo intval(memory_get_usage() / (1024 * 1024)); ?>MB
<br />Peak memory usage: <?php echo intval(memory_get_peak_usage() / (1024 * 1024)); ?>MB
<br />Process Time: <?php echo $time; ?> seconds.</p>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment