Skip to content

Instantly share code, notes, and snippets.

@pommiegranit
Last active October 13, 2015 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pommiegranit/47c639afff4eff8fd9ff to your computer and use it in GitHub Desktop.
Save pommiegranit/47c639afff4eff8fd9ff to your computer and use it in GitHub Desktop.
MailChimp webhooks for WordPress
<?php
function mcwh_action_webhook() {
$mcwh_settings = get_option( 'mcwh_settings' );
mcwh_log('==================[ Incoming Request ]==================');
// mcwh_log('Full _REQUEST dump:\n'.print_r($_REQUEST,true));
if ( empty($_POST) ) {
mcwh_log('No request details found.');
die('No request details found.');
}
if ( !isset($_GET['key']) ){
mcwh_log('FAILED! No security key specified, ignoring request');
} elseif ($_GET['key'] != $mcwh_settings['webhook_key']) {
mcwh_log('FAILED: Security key specified, but not correct');
// mcwh_log("\t".'Wanted: "'.$webhook_key.'", but received "'.$_GET['key'].'"');
} else {
//process the request
mcwh_log('Processing a "'.$_POST['type'].'" request for email address ' . $_POST['data']['email'] . '...');
switch($_POST['type']){
case 'subscribe' : mcwh_subscribe($_POST['data']); break;
case 'unsubscribe': mcwh_unsubscribe($_POST['data']); break;
case 'cleaned' : mcwh_cleaned($_POST['data']); break;
case 'upemail' : mcwh_upemail($_POST['data']); break;
case 'profile' : mcwh_profile($_POST['data']); break;
default:
mcwh_log('Request type "'.$_POST['type'].'" unknown, ignoring.');
}
}
mcwh_log('Finished processing request.');
}
?>
<?php
function mcwh_parse_request( &$wp )
{
if ( array_key_exists( 'webhook', $wp->query_vars ) ) {
mcwh_action_webhook();
exit();
}
}
?>
<?php
function mcwh_endpoint(){
// access webhook at url such as http://[your site]/webhook
add_rewrite_rule( 'webhook' , 'index.php?webhook=1', 'top' );
add_rewrite_tag( '%webhook%' , '([^&]+)' );
}
?>
<?php
function mcwh_subscribe($data){
$mcwh_settings = get_option( 'mcwh_settings' );
$user_email = $data['email'];
/* get existing user record */
$thisuser = get_user_by( 'email', $user_email );
/* if new user... */
if ( $thisuser === false )
/* if hard_subscribe then create new user record */
if ( $mcwh_settings['hard_subscribe'] ) {
$userdata = array(
'user_pass' => wp_generate_password( $length=12, $include_standard_special_chars=false ),
'user_login' => $data['id'],
'user_email' => $user_email,
'first_name' => $data['merges']['FNAME'],
'last_name' => $data['merges']['LNAME'],
'role' => 'subscriber'
);
$user_id = wp_insert_user( $userdata );
if ( !is_wp_error( $user_id ) ) {
mcwh_log( 'SUBSCRIBE: Created new user [ ' . $user_email . ' ]' );
} else {
mcwh_log( 'SUBSCRIBE: FAILED! Problem encountered trying to create new user [ ' . $user_email . ' ]' );
return;
}
} else {
/* if no user found and not creating accounts then exit */
mcwh_log( 'SUBSCRIBE: FAILED! No user found with this email address and hard_(un)subscribe is false' );
return;
} else {
mcwh_log( 'SUBSCRIBE: Existing user found with this email address ' . $user_email );
$user_id = $thisuser->ID;
}
/* update the user meta regardless - if hard_unsubscribe just won't be shown on the user profile */
$subscribed = update_user_meta( $user_id, '_newsletter_subscriber', 1, 0 );
mcwh_log( 'SUBSCRIBE: ' . ( $subscribed ? 'SUCCESS! User subscribed' : 'FAILED! User already subscribed' ) );
}
?>
<?php
function mcwh_unsubscribe($data){
$mcwh_settings = get_option( 'mcwh_settings' );
$user_email = $data['email'];
/* get existing user */
$thisuser = get_user_by( 'email', $user_email );
/* if user exists then unsubscribe */
if( $thisuser ) {
/* if hard unsubscribe then delete the user */
if ( $mcwh_settings['hard_unsubscribe'] ) {
/* have to include this file to get access to wp_delete_user function */
require_once(ABSPATH.'wp-admin/includes/user.php' );
wp_delete_user( $thisuser->ID );
mcwh_log( 'UNSUBSCRIBE: SUCCESS! ' . $user_email . ' deleted (hard unsubscribe) ');
} else {
/* soft unsubscribe - just change _newsletter_subscriber meta to 0 */
$unsubscribed = update_user_meta( $thisuser->ID, '_newsletter_subscriber', 0, 1 );
mcwh_log( 'UNSUBSCRIBE: ' . ( $unsubscribed ? 'SUCCESS! User unsubscribed' : 'FAILED: User already unsubscribed' ) );
}
} else {
mcwh_log ( 'UNSUBSCRIBE: FAILED! User with email address ' . $user_email . ' does not exist. Cannot unsubscribe.' );
}
}
?>
<?php
/* add hooks for showing _newsletter_subscriber value in profile */
add_action( 'show_user_profile', 'mcwh_show_user_profile' );
add_action( 'edit_user_profile', 'mcwh_show_user_profile' );
function mcwh_show_user_profile( $user ) {
$subscribed = get_user_meta( $user->ID, '_newsletter_subscriber', true );
echo '<h3>MailChimp Newsletter</h3>
<table class="form-table">
<tr>
<th><label for="_newsletter_subscriber">Current Subscriber</label></th>
<td>';
echo '<input type="checkbox" name="_newsletter_subscriber" id="_newsletter_subscriber" ';
echo $subscribed ? "checked" : "";
echo ' disabled/><br />
<span class="description">Is this user subscribed to the MailChimp newsletter?</span>
</td>
</tr>
</table>';
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment