Skip to content

Instantly share code, notes, and snippets.

@Pebblo
Last active May 10, 2019 15:14
Show Gist options
  • Save Pebblo/89058e64c05bd44a4b61eae13878401f to your computer and use it in GitHub Desktop.
Save Pebblo/89058e64c05bd44a4b61eae13878401f to your computer and use it in GitHub Desktop.
Example of how to add various different EE Messages shortcodes and their parses, whilst jumping through some hoops to make sure we have the correct data.
<?php
/* This filter allows you to add custom shortcodes to the message system
* $shortcodes is an array of the available shortcodes for the current library
* $lib is the current shortcode library
*/
function ee_register_new_custom_message_shortcodes( $shortcodes, EE_Shortcodes $lib ) {
//Add a shortcode to be used with the EE Datetimes within messages
if ( $lib instanceof EE_Datetime_Shortcodes ) {
//Add your shortcode to the add as the key, the value should be a description of the shortcode.
$shortcodes['[CUSTOM_DATETIME_SHORTCODE]'] = _('This is a custom DATETIME shortcode!');
}
//Add a shortcode to be used with the EE Event List within messages
if ( $lib instanceof EE_Event_Shortcodes ) {
//Add your shortcode to the add as the key, the value should be a description of the shortcode.
$shortcodes['[CUSTOM_EVENT_SHORTCODE]'] = _('This is a custom EVENT shortcode!');
}
//Add a shortcode to be used with the EE Recipient within messages
if ( $lib instanceof EE_Recipient_Details_Shortcodes ) {
//Add your shortcode to the add as the key, the value should be a description of the shortcode.
$shortcodes['[CUSTOM_RECIPIENT_SHORTCODE]'] = _('This is a custom RECIPIENT shortcode!');
}
//Return the shortcodes.
return $shortcodes;
}
add_filter( 'FHEE__EE_Shortcodes__shortcodes', 'ee_register_new_custom_message_shortcodes', 10, 2 );
/*
* This filter allows you to hook in to the shortcode parser, check for a shortcode added above and return a value for it using the data passed to the parser.
* $parsed are the current values being parsed.
* $shortcode is the current shortcode passed to the parser.
* $data is the current data available, this can be different types of objects depending on the parser.
* $extra_data is a collaction of various data available within the messages system.
*/
function ee_register_new_custom_messages_shortcode_parser( $parsed, $shortcode, $data, $extra_data, EE_Shortcodes $lib ) {
//Check for the datetime shortcodes library as that's one of the libraries we added a custom shortcode to above.
//also check that $data is the expected object (in this case an EE_Datetime as its for the EE_Datetime_Shortcodes library)
if ( $lib instanceof EE_Datetime_Shortcodes && $data instanceof EE_Datetime ) {
//Then check if we are parsing one of our custom shortcodes above.
if ( $shortcode === '[CUSTOM_DATETIME_SHORTCODE]' ) {
//Do whatever you need to do here and return the value for that specific datetime.
//In this example it simply returns the ID of the related EE_Datetime.
return $data->ID();
}
}
//Check for the EE Events shortcodes library as that's one of the libraries we added a custom shortcode to above.
//$data may not be an EE_Event object, but either $data or $extra_data will be an instance of EE_Messages_Addressee
//Using that object you can pull an EE_Event object
if ( $lib instanceof EE_Event_Shortcodes ) {
//Then check if we are parsing one of our custom shortcodes above.
if ( $shortcode === '[CUSTOM_EVENT_SHORTCODE]' ) {
//Now pull the related Event object using all of the available data we have.
//First check if we have an event object in $data
$event = $data instanceof EE_Event ? $data : null;
//If no event, then let's see if there is a reg_obj. If there IS, then we'll try and grab the event from the reg_obj instead.
if ( empty( $event ) ) {
$aee = $data instanceof EE_Messages_Addressee ? $data : null;
$aee = $extra_data instanceof EE_Messages_Addressee ? $extra_data : $aee;
$event = $aee instanceof EE_Messages_Addressee && $aee->reg_obj instanceof EE_Registration ? $aee->reg_obj->event() : null;
}
//Check we do now actually have an event object.
if ( !empty( $event ) ) {
//Do whatever you need to do using the event object.
//In this example it simply returns the ID of the related EE_Event.
return $event->ID();
}
}
}
//Check for the EE Recipient Details shortcodes library as that's one of the libraries we added a custom shortcode to above.
//$data or $extra_data will be an instance of EE_Messages_Addressee which can be used to pull the required details.
if ( $lib instanceof EE_Recipient_Details_Shortcodes ) {
if ( $shortcode === '[CUSTOM_RECIPIENT_SHORTCODE]' ) {
//Make sure we end up with a copy of the EE_Messages_Addressee object
$recipient = $data instanceof EE_Messages_Addressee ? $data : null;
$recipient = ! $recipient instanceof EE_Messages_Addressee
&& is_array($data)
&& isset($data['data'])
&& $data['data'] instanceof EE_Messages_Addressee
? $data['data']
: $recipient;
$recipient = ! $recipient instanceof EE_Messages_Addressee
&& ! empty($extra_data['data'])
&& $extra_data['data'] instanceof EE_Messages_Addressee
? $extra_data['data']
: $recipient;
//Confirm we have a valid receipient
if (! $recipient instanceof EE_Messages_Addressee) {
return '';
}
//Confirm we have a valid EE_Attendee object
$attendee = $recipient->att_obj;
if (! $attendee instanceof EE_Attendee) {
return '';
}
return $attendee->ID();
}
}
//If not within the correct section, or parsing the correct shortcode,
//Return the currently parsed content.
return $parsed;
}
add_filter( 'FHEE__EE_Shortcodes__parser_after', 'ee_register_new_custom_message_shortcodes_parser', 10, 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment