Skip to content

Instantly share code, notes, and snippets.

@davidfcarr
Last active February 8, 2021 00:11
Show Gist options
  • Save davidfcarr/9d9dc94a37beca6c93ace6bcaa9e4bf9 to your computer and use it in GitHub Desktop.
Save davidfcarr/9d9dc94a37beca6c93ace6bcaa9e4bf9 to your computer and use it in GitHub Desktop.
<?php
add_filter('mailpoet_newsletter_shortcode', 'mailpoet_rsvpmaker_shortcode', 10, 5);
function mailpoet_rsvpmaker_shortcode($shortcode, $newsletter, $subscriber, $queue, $newsletter_body) {
// always return the shortcode if it doesn't match your own!
if (!strpos($shortcode,'rsvpmaker') && !strpos($shortcode,'event_listing') ) return $shortcode;
global $email_context;
$email_context = true;
$shortcode = str_replace('custom:','',$shortcode);
return do_shortcode($shortcode);
}
//this function is called whenever a submitted RSVP form includes the "Add me to your email list" checkbox
function mailpoet_email_list_okay($rsvp) {
if (class_exists(\MailPoet\API\API::class)) {
$mailpoet_api = \MailPoet\API\API::MP('v1');
$list = get_option('rsvpmaker_mailpoet_list');
if(!$list)
return;
$list_ids = array($list);
$mailpoet_api = \MailPoet\API\API::MP('v1');
$first = (empty($rsvp['first'])) ? '' : $rsvp['first'];
$last = (empty($rsvp['last'])) ? '' : $rsvp['last'];
$subscriber = array('email' => $rsvp['email'], 'first_name' => $first,'last_name' => $last);
try {
$get_subscriber = $mailpoet_api->getSubscriber($subscriber['email']);
} catch (\Exception $e) {}
try {
if (!$get_subscriber) {
// Subscriber doesn't exist let's create one
$mailpoet_api->addSubscriber($subscriber, $list_ids);
} else {
// In case subscriber exists just add him to new lists
$mailpoet_api->subscribeToLists($subscriber['email'], $list_ids);
}
} catch (\Exception $e) {
$error_message = $e->getMessage();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment