Skip to content

Instantly share code, notes, and snippets.

@davidfcarr
Last active October 20, 2022 14:51
Show Gist options
  • Save davidfcarr/ef41b357e28a63e2dade34230b459cd2 to your computer and use it in GitHub Desktop.
Save davidfcarr/ef41b357e28a63e2dade34230b459cd2 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Multisite Hacks for MailPoet
Version: 1.1
Description: Example but DOES NOT SEEM TO WORK WITH THE LATEST MAILPOET VERSION. My recommended alternative to MailPoet https://rsvpmaker.com/blog/2022/08/27/rsvpmaker-postmark-free-email-marketing/
*/
function mailpoet_site_subscribers () {
if(!strpos($_SERVER['REQUEST_URI'],'mailpoet'))
return;
global $wpdb;
$segment_table = $wpdb->prefix.'mailpoet_segments';
$subscriber_segment_table = $wpdb->prefix.'mailpoet_subscriber_segment';
$subscribers_table = $wpdb->prefix.'mailpoet_subscribers';
$where = ' WHERE 1 ';
$sql = "SELECT * FROM `$subscriber_segment_table` JOIN `$segment_table` ON `$subscriber_segment_table`.`segment_id`= `$segment_table`.id WHERE `$segment_table`.type != 'wp_users' ORDER BY `wpt_109_mailpoet_subscriber_segment`.`segment_id` ASC #toastmost";
$results = $wpdb->get_results($sql);
if(empty($results))
return;
foreach($results as $row)
{
$sub[] = $row->subscriber_id;
$where .= " AND id != ".$row->subscriber_id;
}
//update_option('mailpoet_club_subscribers',$sub);
$sql = "DELETE FROM $subscribers_table ".$where.' #toastmost';
$wpdb->query($sql);
$sql = "SELECT * FROM $segment_table";
$results = $wpdb->get_results($sql);
$sw = ' WHERE 1 ';
foreach($results as $row) {
$sw .= ' AND segment_id !='.$row->id;
}
$sql = "DELETE FROM $subscriber_segment_table $sw #toastmost";
$wpdb->query($sql);
}
add_action('admin_notices','mailpoet_site_subscribers',1);
function mailpoet_toastmost_notice() {
$uri = $_SERVER['REQUEST_URI'];
if(!strpos($uri,'mailpoet') || strpos($uri,'plugins.php'))
return;
$timestamp = wp_next_scheduled( 'toastmost_email_list_cron' );
if($timestamp)
return; // don't show if invitations are going out now
global $wpdb;
$segments_table = $wpdb->prefix.'mailpoet_segments';
$subscriber_table = $wpdb->prefix.'mailpoet_subscribers';
$subscriber_segment_table = $wpdb->prefix.'mailpoet_subscriber_segment';
$deleted = date('Y-m-d H:i:s');
$sql = "DELETE FROM $segments_table WHERE type='wp_users' ";
$wpdb->query($sql);
$sql = "UPDATE $segments_table set description='For communication with club members' WHERE type='default'";
$wpdb->query($sql);
$results = $wpdb->get_results("SELECT * FROM $subscriber_table #toastmost");
$current_list = array();
foreach($results as $row) {
$current_list[] = strtolower($row->email);
}
$unsub = get_option('rsvpmail_unsubscribed');
if(is_array($unsub))
foreach($unsub as $email)
$current_list[] = $email;
$members = get_site_users();
$count = 0;
$csv = '';
foreach($members as $member) {
$user = get_userdata($member->ID);
if(in_array(strtolower($user->user_email),$current_list))
continue;
$subscriber = array('email' => $user->user_email, 'first_name' => $user->first_name, 'last_name' => $user->last_name);
$csv .= "$user->user_email,$user->first_name,$user->last_name\n";
$subscribers_to_add[] = $subscriber;
}
if(isset($_POST['add_club_members']))
{
wp_schedule_event( strtotime('+2 minutes'), 'doubleminute', 'toastmost_email_list_cron' );
echo '<div class="notice notice-success">Email list invitations will be sent to members over the next several minutes.</div>';
return;
}
if(!empty($subscribers_to_add))
printf('<div class="notice notice-info"><form method="post" action="%s"><p><input type="hidden" name="add_club_members" value="1"><button>Update List</button> - updates Club Members email list. Members will need to confirm. %d members are not yet on the list.</p></form><pre>%s</pre></div>',site_url($uri),sizeof($subscribers_to_add),$csv);
}
add_action('admin_notices','mailpoet_toastmost_notice');
add_action('toastmost_email_list_cron','toastmost_email_list_cron');
function toastmost_email_list_cron() {
global $wpdb;
$subscriber_table = $wpdb->prefix.'mailpoet_subscribers';
$results = $wpdb->get_results("SELECT * FROM $subscriber_table");
$current_list = array();
foreach($results as $row) {
$current_list[] = strtolower($row->email);
}
$unsub = get_option('rsvpmail_unsubscribed');
if(is_array($unsub))
foreach($unsub as $email)
$current_list[] = $email;
$members = get_site_users();
$count = 0;
foreach($members as $member) {
$user = get_userdata($member->ID);
if(in_array(strtolower($user->user_email),$current_list))
continue;
$subscriber = array('email' => $user->user_email, 'first_name' => $user->first_name, 'last_name' => $user->last_name);
$subscribers_to_add[] = $subscriber;
}
if(empty($subscribers_to_add)) {
$timestamp = wp_next_scheduled( 'toastmost_email_list_cron' );
wp_unschedule_event( $timestamp, 'toastmost_email_list_cron' );
return;
}
$list_ids = array(3);
$mailpoet_api = \MailPoet\API\API::MP('v1');
$count = 0;
$subscribers_to_add = array_slice($subscribers_to_add,0,5);
foreach($subscribers_to_add as $subscriber) {
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);
//printf('<div class="notice notice-success">%s added</div>',$subscriber['email']);
} else {
// In case subscriber exists just add him to new lists
$mailpoet_api->subscribeToLists($subscriber['email'], $list_ids);
//printf('<div class="notice notice-success">%s added</div>',$subscriber['email']);
}
} catch (\Exception $e) {
$error_message = $e->getMessage();
//printf('<div class="notice notice-error">%s</div>',$error_message);
}
}
}
function get_site_users( $blog_id = 0 ) {
if ( empty( $blog_id ) ) {
$blog_id = get_current_blog_id();
}
return get_users(
array(
'blog_id' => $blog_id,
'orderby' => 'display_name',
)
);
}
@bderaguts
Copy link

Hey David, does this still work with the newest versions of Mailpoet to fix the network users issue?

@davidfcarr
Copy link
Author

@bderaguts as far as I know, yes. I have since shifted my energy to rounding out the mailing tools built into my RSVPMaker plugin, which use the Postmark email service and are designed with multisite in mind. See https://rsvpmaker.com/blog/2022/08/27/rsvpmaker-postmark-free-email-marketing/

@davidfcarr
Copy link
Author

@bderaguts Looks like I'm losing the battle with MailPoet, since it seems very intentional that they are frustrating the way multisite users would prefer that the software work.

@bderaguts
Copy link

@bderaguts Looks like I'm losing the battle with MailPoet, since it seems very intentional that they are frustrating the way multisite users would prefer that the software work.

Does this mean that the Multisite hack plugin no longer solves the issue for the newest versions of Mailpoet?

@davidfcarr
Copy link
Author

I'm afraid so. I hadn't found a way to prevent MailPoet from adding the whole user table to your subscriber list, so I was trying to instead automate the process of removing those entries. It was a crude hack, and it seems to have broken with the latest update.

As I mentioned, I've been working on an alternative to MailPoet for my own clients that explicitly supports multisite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment