Skip to content

Instantly share code, notes, and snippets.

@Pebblo
Created January 14, 2021 19:03
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 Pebblo/d2ec2141ac616c2793124fd124d9d9eb to your computer and use it in GitHub Desktop.
Save Pebblo/d2ec2141ac616c2793124fd124d9d9eb to your computer and use it in GitHub Desktop.
Uses hooks within Event Espresso to add 'Datetime Meta' fields for Zoom and Skype. This allows you to set unqiue values on datetime which can be used in messages (See DATETIME_LIST for example usage)
<li>
[DATETIME_START] - [DATETIME_END]
<ul>
<li>[DTT_META_*zoom_url]</li>
<li>[DTT_META_*zoom_meeting_id]</li>
<li>[DTT_META_*zoom_passcode]</li>
<li>[DTT_META_*skype]</li>
</ul>
</li>
<?php
/*
Plugin Name: EE Datetime Meta Fields
Description: A plugin used to hold custom functions.
*/
/* Begin Adding Functions Below This Line; Do not include an opening PHP tag as this sample code already includes one! */
// hook into ticket editor in event editor.
add_action(
'AHEE__event_tickets_datetime_attached_tickets_row_template__advanced_details_after_dtt_description',
'tw_ee_insert_zoom_datetime_meta_interface',
20,
2
);
function tw_ee_insert_zoom_datetime_meta_interface($dtt_row, $DTT_ID) {
echo tw_ee_get_zoom_datetime_meta_interface($dtt_row, $DTT_ID)->get_html_and_js();
}
function tw_ee_get_zoom_datetime_meta_interface($dtt_row, $DTT_ID) {
$datetime = EE_Registry::instance()->load_model('Datetime')->get_one_by_ID($DTT_ID);
$zoom_url = $meeting_id = $passcode = $skype = '';
if( $datetime instanceof EE_Datetime) {
$zoom_url = $datetime->get_extra_meta('ee_datetime_zoom_url', true, '');
$meeting_id = $datetime->get_extra_meta('ee_datetime_zoom_meeting_id', true, '');
$passcode = $datetime->get_extra_meta('ee_datetime_zoom_passcode', true, '');
$skype = $datetime->get_extra_meta('ee_datetime_skype', true, '');
}
EE_Registry::instance()->load_helper('HTML');
return new EE_Form_Section_Proper(
array(
'name' => 'tw-ee-datetime-zoom-details-' . $dtt_row,
'html_class' => 'tw-ee-datetime-zoom-details-container',
'layout_strategy' => new EE_Div_Per_Section_Layout(),
'subsections' => array(
'zoome_details_hdr-' . $dtt_row => new EE_Form_Section_HTML(
EEH_HTML::h5(
esc_html__('Datetime Zoom Details', 'event_espresso'),
'',
'datetime-zoom-details datetime-tickets-heading'
)
),
'DTT_zoom_url' => new EE_Text_Input(
array(
'html_class' => 'DTT-zoom-url',
'html_name' => 'zoom_datetime_details_input['
. $dtt_row
. '][DTT_zoom_url]',
'html_label_text' => esc_html__(
'Zoom URL linked to this datetime (zoom_url):',
'event_espresso'
),
'default' => $zoom_url,
'display_html_label_text' => true
)
),
'DTT_meeting_id' => new EE_Text_Input(
array(
'html_class' => 'DTT-meeting-id',
'html_name' => 'zoom_datetime_details_input['
. $dtt_row
. '][DTT_meeting_id]',
'html_label_text' => esc_html__(
'Zoom Meeting ID for the above URL (zoom_meeting_id):',
'event_espresso'
),
'default' => $meeting_id,
'display_html_label_text' => true
)
),
'DTT_passcode' => new EE_Text_Input(
array(
'html_class' => 'DTT-passcode',
'html_name' => 'zoom_datetime_details_input['
. $dtt_row
. '][DTT_passcode]',
'html_label_text' => esc_html__(
'Zoom Passcode for the above URL (zoom_passcode):',
'event_espresso'
),
'default' => $passcode,
'display_html_label_text' => true
)
),
'DTT_skype' => new EE_Text_Input(
array(
'html_class' => 'DTT-skype',
'html_name' => 'zoom_datetime_details_input['
. $dtt_row
. '][DTT_skype]',
'html_label_text' => esc_html__(
'Skype Details linked to this meeting (skype):',
'event_espresso'
),
'default' => $skype,
'display_html_label_text' => true
)
)
) // end EE_Form_Section_Proper subsections
) // end main EE_Form_Section_Proper options array
); // end EE_Form_Section_Proper
}
add_action(
'AHEE__espresso_events_Pricing_Hooks___update_datetimes_after_save',
'tw_ee_update_zoom_datetime_meta',
10,
4
);
function tw_ee_update_zoom_datetime_meta(EE_Datetime $datetime, $dtt_row, $dtt_form_data, $all_form_data)
{
try {
$datetime_id = $dtt_form_data instanceof EE_Datetime ? $dtt_form_data->ID() : $datetime->ID();
$form = tw_ee_get_zoom_datetime_meta_interface($dtt_row, $datetime_id);
if ($form->was_submitted()) {
$form->receive_form_submission();
if ($form->is_valid()) {
$valid_data = $form->valid_data();
if( !empty($valid_data['DTT_zoom_url'])) {
$datetime->update_extra_meta('ee_datetime_zoom_url', $valid_data['DTT_zoom_url']);
}
if( !empty($valid_data['DTT_meeting_id'])) {
$datetime->update_extra_meta('ee_datetime_zoom_meeting_id', $valid_data['DTT_meeting_id']);
}
if( !empty($valid_data['DTT_passcode'])) {
$datetime->update_extra_meta('ee_datetime_zoom_passcode', $valid_data['DTT_passcode']);
}
if( !empty($valid_data['DTT_skype'])) {
$datetime->update_extra_meta('ee_datetime_skype', $valid_data['DTT_skype']);
}
}
}
} catch (EE_Error $e) {
$e->get_error();
}
}
function tw_ee_register_datetime_meta_messages_shortcode( $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['[DTT_META_*]'] = _('This is a special dynamic shortcode. After the "*", add the exact name for your custom field, if there is a value set for that custom field on the datetime then it will be output in place of this shortcode.');
}
//Return the shortcodes.
return $shortcodes;
}
add_filter( 'FHEE__EE_Shortcodes__shortcodes', 'tw_ee_register_datetime_meta_messages_shortcode', 10, 2 );
function tw_ee_register_datetime_meta_messages_shortcode_parser( $parsed, $shortcode, $data, $extra_data, EE_Shortcodes $lib ) {
//Check we are within the EE_Ticket_Shortcodes
if ( $lib instanceof EE_Datetime_Shortcodes ) {
// Confirm we are parsing the TKT_META_* shortcode
if (strpos($shortcode, '[DTT_META_*') !== false) {
// If we don't have a EE_Datetime object, get out.
if (! $data instanceof EE_Datetime) {
return '';
}
//Pull the transaction from the EE_Messages_Addressee object passed to parser.
$shortcode = str_replace('[DTT_META_*', '', $shortcode);
$shortcode = trim(str_replace(']', '', $shortcode));
// make sure the required wp helper function is present
// require the shortcode file if necessary
if (! function_exists('shortcode_parse_atts')) {
require_once(ABSPATH . WPINC . '/shortcodes.php');
}
// let's get any attributes that may be present and set the defaults.
//$shortcode_to_parse = str_replace('[', '', str_replace(']', '', $shortcode));
$attrs = shortcode_parse_atts(str_replace('[', '', str_replace(']', '', $shortcode)));
// The meta_key set on the shortcode should always be the first value in the array.
$meta_key = $attrs[0];
// Pull the meta value from the event post.
$datetime_meta = $data->get_extra_meta('ee_datetime_' . $meta_key, true, '');
// If we have no event_meta, just return an empty string.
if (empty($datetime_meta)) {
return '';
}
return $datetime_meta;
}
}
//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', 'tw_ee_register_datetime_meta_messages_shortcode_parser', 10, 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment