Skip to content

Instantly share code, notes, and snippets.

@Jursdotme
Created November 13, 2018 12:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Jursdotme/b4cb0fb7e8c76433cc6f69c4edec4df4 to your computer and use it in GitHub Desktop.
Save Jursdotme/b4cb0fb7e8c76433cc6f69c4edec4df4 to your computer and use it in GitHub Desktop.
<?php
/**
* Get current subscriber list
*/
function bl_get_subs() {
global $wpdb;
$table_name = $wpdb->prefix . "mailpoet_subscribers";
$retrieve_data = $wpdb->get_results("SELECT * FROM $table_name");
$subscribers_array = [];
foreach ($retrieve_data as $retrieved_data) {
$subscriber_array = array(
'id' => $retrieved_data->id,
'email' => $retrieved_data->email,
'status' => $retrieved_data->status
);
array_push($subscribers_array, $subscriber_array);
}
/**
* Validate and output
*/
$creds = array();
$headers = getallheaders();
$required_capability = 'newsletter_robot';
if ( array_key_exists( 'username', $headers ) && array_key_exists( 'password', $headers ) ) {
$creds['user_login'] = $headers["username"];
$creds['user_password'] = $headers["password"];
$creds['remember'] = false;
$user = wp_signon( $creds, false ); // Verify the user.
// TODO: Consider sending custom message because the default error
// message reveals if the username or password are correct.
if ( is_wp_error($user) ) {
echo $user->get_error_message();
return $user;
}
wp_set_current_user( $user->ID, $user->user_login );
// A subscriber has 'read' access so a very basic user account can be used.
if ( ! current_user_can( $required_capability ) ) {
return new WP_Error( 'rest_forbidden', 'You do not have permissions to view this data.', array( 'status' => 401 ) );
}
// TODO: Run real code here.
return $subscribers_array;
}
else {
return new WP_Error( 'invalid-method', 'You must specify a valid username and password.', array( 'status' => 400 /* Bad Request */ ) );
}
}
add_action( 'rest_api_init', function () {
register_rest_route(
'blnews/v1',
'/getsubscribers',
array(
'methods' => 'GET',
'callback' => 'bl_get_subs',
)
);
} );
/**
* Update current subscriber list
*/
add_action( 'rest_api_init', function(){
register_rest_route(
'blnews/v1',
'/updatesubscribers',
array(
'methods' => 'POST',
'callback' => 'bl_set_subs',
)
);
} );
function bl_set_subs( $request ) {
$data['message'] = 'Post Request was received.';
$int_data = json_decode($request->get_param( 'list' ));
/**
* Split data into subscribe and unsubscribe
*/
$unsubscribers = [];
$subscribers = [];
$failiure = '';
foreach( $int_data as $subscriber ) {
//$data['test'] = $subscriber->status;
if($subscriber->status == 'unsubscribed') {
array_push($unsubscribers, $subscriber->email);
} else {
$subscriber_item = array(
'email' => $subscriber->email,
'first_name' => isset($subscriber->first_name) ? $subscriber->first_name : '',
'last_name' => isset($subscriber->last_name) ? $subscriber->last_name : '',
'list' => $subscriber->list
);
array_push($subscribers, $subscriber_item);
}
}
$data['unsubscribers'] = $unsubscribers;
$data['subscribers'] = $subscribers;
$data['subscriber_fields'] = \MailPoet\API\API::MP('v1')->getSubscriberFields();
/**
* Unsibscribe users
*/
$lists = \MailPoet\API\API::MP('v1')->getLists();
$list_ids = [];
foreach ($lists as $list) {
array_push($list_ids, $list["id"]);
}
$data['lists'] = $list_ids;
foreach ($unsubscribers as $unsubscriber) {
try {
$subscriber = \MailPoet\API\API::MP('v1')->unsubscribeFromList($unsubscriber, $list_ids);
} catch (Exception $exception) {
//return $exception->getMessage();
}
}
/**
* Subscribe users
*/
$subscription_options = array(
'send_confirmation_email' => false, // default: true
'schedule_welcome_email' => false // default: true
);
foreach ($subscribers as $newsubscriber) {
try {
$lists = array();
array_push($lists,$newsubscriber['list']);
unset($newsubscriber['list']);
$data['log'] = $lists;
$subscriber = \MailPoet\API\API::MP('v1')->addSubscriber($newsubscriber, $lists, $subscription_options);
} catch(Exception $exception) {
$failiure = $exception->getMessage();
}
}
/**
* Validate and output
*/
$creds = array();
$headers = getallheaders();
$required_capability = 'newsletter_robot';
if ( array_key_exists( 'username', $headers ) && array_key_exists( 'password', $headers ) ) {
$creds['user_login'] = $headers["username"];
$creds['user_password'] = $headers["password"];
$creds['remember'] = false;
$user = wp_signon( $creds, false ); // Verify the user.
// TODO: Consider sending custom message because the default error
// message reveals if the username or password are correct.
if ( is_wp_error($user) ) {
echo $user->get_error_message();
return $user;
}
wp_set_current_user( $user->ID, $user->user_login );
// A subscriber has 'read' access so a very basic user account can be used.
if ( ! current_user_can( $required_capability ) ) {
return new WP_Error( 'rest_forbidden', 'You do not have permissions to view this data.', array( 'status' => 401 ) );
}
// TODO: Run real code here.
//return 'ok';
return $data;
}
else {
return new WP_Error( 'invalid-method', 'You must specify a valid username and password.', array( 'status' => 400 /* Bad Request */ ) );
}
}
@oregondean
Copy link

Does this code work with MailPoet 3?

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