Created
March 13, 2019 20:37
-
-
Save Pebblo/b8b9b804bcd709c988ad6794f990b4dc to your computer and use it in GitHub Desktop.
Example of how to add a custom 'Recipient' shortcode to the messages system, this adds a [RECIPIENT_ATTENDEE_ID] shortcode which parses to the recipients EE Attendee ID.
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 //Please do no include the opening PHP tag if you already have one. | |
/* 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_messages_shortcodes( $shortcodes, EE_Shortcodes $lib ) { | |
//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['[RECIPIENT_ATTENDEE_ID]'] = _('This is a custom RECIPIENT shortcode!'); | |
} | |
//Return the shortcodes. | |
return $shortcodes; | |
} | |
add_filter( 'FHEE__EE_Shortcodes__shortcodes', 'ee_register_new_custom_messages_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_shortcodes_parser( $parsed, $shortcode, $data, $extra_data, EE_Shortcodes $lib ) { | |
//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 == '[RECIPIENT_ATTENDEE_ID]' ) { | |
//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_messages_shortcodes_parser', 10, 5 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment