/tw_ee_my_events_cancel_link.php Secret
Last active
March 25, 2020 09:46
An example function that adds a 'Cancel Registration' option to the WP User integration add-on's 'My Events' section.
This file contains 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 | |
/* This function adds an icon to the 'My Events' section of the WP User intergration add-on which allows the user to cancel their registrations. | |
* IMPORTANT NOTE - This function DOES NOT issue any refunds whatsoever, it simply changes the registration status to 'Cancelled' and triggers any related messages. | |
* | |
* It is HIGHLY recommended that you enable the 'Event Admin' context of the 'Registration Cancelled' message type: | |
* Event Espresso -> Messages -> Default message templates -> Registration Cancelled -> Edit Event Admim | |
* Make sure the context is active (the toggle switch) and that the TO field has a shortcode for the correct email to parse ( e.g [EVENT_AUTHOR_EMAIL] ) | |
* This way the event admin at least receives an email that shows the user cancelled their registration. | |
* | |
* The function checks for a custom value on each event with key 'allow_user_cancellations', set that to have any value and that single event will allow cancellations. | |
* Use the filter below to allow cancellations on all events by default. | |
*/ | |
// Uncomment this line to allow cancellations on all events. | |
// add_filter('AHEE__loop-espresso_my_events__allow_user_cancellations', '__return_true'); | |
function tw_add_cancel_reg_to_my_events( $actions, $registration) { | |
// Add a cancel registration link to my events. | |
if ( $registration instanceof EE_Registration ) { | |
if( $registration->status_ID() !== \EEM_Registration::status_id_cancelled && | |
(apply_filters('AHEE__loop-espresso_my_events__allow_user_cancellations', false) || | |
get_post_meta( $registration->event_ID(), 'allow_user_cancellations', true)) | |
) { | |
$query_args = array( | |
'ee_cancel_registration' => $registration->ID(), | |
); | |
$cancel_reg_url = esc_url(add_query_arg( $query_args )); | |
$cancel_reg_text = __( 'Link to cancel registration', 'event_espresso' ); | |
$actions['cancel_reg'] = '<a aria-label="' . $cancel_reg_text | |
. '" title="' . $cancel_reg_text | |
. '" href="' . $cancel_reg_url . '">' | |
. '<span class="dashicons dashicons-dismiss ee-icon-size-18"></span></a>'; | |
} | |
} | |
return $actions; | |
} | |
add_filter( 'FHEE__EES_Espresso_My_Events__actions', 'tw_add_cancel_reg_to_my_events', 10, 2); | |
function tw_add_cancel_reg_to_my_events_legend( $items ) { | |
// Add the cancel registration icon to the legend. | |
$items['cancel_reg'] = array( | |
'class' => 'dashicons dashicons-dismiss', | |
'desc' => esc_html__( 'Cancel registration', 'event_espresso' ) | |
); | |
return $items; | |
} | |
add_filter( 'FHEE__status-legend-espresso_my_events__legend_items', 'tw_add_cancel_reg_to_my_events_legend'); | |
function tw_ee_cancel_reg_from_my_events($object_type, $objects, $template_slug, $att_id) { | |
if( empty($att_id) || $att_id != get_user_option('EE_Attendee_ID', get_current_user_id()) ){ | |
// For whatever reason, the att_id is not set or doens't match the value from the current user, | |
// Return and do nothing as we want to be sure we are only cancelling regs linked to the current user. | |
return; | |
} | |
// Get the REG ID passed from my events. | |
$loader = EventEspresso\core\services\loaders\LoaderFactory::getLoader(); | |
$request = $loader->getShared('EventEspresso\core\services\request\Request'); | |
$REG_ID = $request->getRequestParam('ee_cancel_registration'); | |
// Get out if no REG_ID | |
if(empty($REG_ID)) { | |
return; | |
} | |
// Pull the registration using the ID passed. | |
$registration = EEM_Registration::instance()->get_one_by_ID($REG_ID); | |
// Check we have a valid registration. | |
if( $registration instanceof EE_Registration && | |
$registration->status_ID() !== \EEM_Registration::status_id_cancelled && | |
(apply_filters('AHEE__loop-espresso_my_events__allow_user_cancellations', false) || | |
get_post_meta( $registration->event_ID(), 'allow_user_cancellations', true )) | |
) { | |
// Pull the attendee linked to the registration. | |
$attendee = $registration->attendee(); | |
// Confirm that the attendee linked to the registration in question has the same ID as the attende linked to the user. | |
if( $attendee->ID() == $att_id ) { | |
//Set the registration to cancelled. | |
$registration->set_status( \EEM_Registration::status_id_cancelled ); | |
// Trigger notifications. | |
$registration_processor = \EE_Registry::instance()->load_class('Registration_Processor'); | |
add_filter('FHEE__EED_Messages___maybe_registration__deliver_notifications', '__return_true', 10); | |
$extra_details = array( | |
'finalized' => true, | |
); | |
$registration_processor->trigger_registration_update_notifications($registration, $extra_details); | |
// Add notice showing the registration has been cancelled. | |
EE_Error::add_success( | |
esc_html__('The selected registration has been cancelled.', 'event_espresso') | |
); | |
EE_Error::stashNoticesBeforeRedirect(); | |
// Redirect back to 'My Events' to prevent multiple notifications. | |
wp_safe_redirect($_SERVER['HTTP_REFERER']); | |
exit(); | |
} | |
} | |
} | |
add_action( 'AHEE__loop-espresso_my_events__before', 'tw_ee_cancel_reg_from_my_events', 10, 4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment