|
|
|
@ -71,6 +71,11 @@ class Reuna_Mailchimp_Public { |
|
|
|
{ |
|
|
|
$this->plugin_name = $plugin_name; |
|
|
|
$this->version = $version; |
|
|
|
|
|
|
|
if (function_exists('env')) { |
|
|
|
$this->api_key = env('MAILCHIMP_API_KEY'); |
|
|
|
$this->list_id = env('MAILCHIMP_LIST_ID'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@ -111,11 +116,10 @@ class Reuna_Mailchimp_Public { |
|
|
|
*/ |
|
|
|
public function handle_rest_api_request(WP_REST_Request $request) |
|
|
|
{ |
|
|
|
$this->setup_environment(); |
|
|
|
$user = wp_get_current_user(); |
|
|
|
$user_id = get_current_user_id(); |
|
|
|
|
|
|
|
if (! $this->subscriber_exists($user)) { |
|
|
|
$this->create_subscriber($user); |
|
|
|
if (! $this->subscriber_exists($user_id)) { |
|
|
|
$this->create_subscriber($user_id); |
|
|
|
} |
|
|
|
|
|
|
|
$tags = $request->get_param('tags'); |
|
|
|
@ -123,29 +127,7 @@ class Reuna_Mailchimp_Public { |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
$this->add_subscriber_tags($user, $tags); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Setup class properties from environment variables |
|
|
|
* |
|
|
|
* @since 1.0.0 |
|
|
|
*/ |
|
|
|
protected function setup_environment() |
|
|
|
{ |
|
|
|
if (! function_exists('env')) { |
|
|
|
return new WP_Error('missing_env'); |
|
|
|
} |
|
|
|
|
|
|
|
$this->api_key = env('MAILCHIMP_API_KEY'); |
|
|
|
if (! $this->api_key) { |
|
|
|
return new WP_Error('missing_mailchimp_api_key'); |
|
|
|
} |
|
|
|
|
|
|
|
$this->list_id = env('MAILCHIMP_LIST_ID'); |
|
|
|
if (! $this->list_id) { |
|
|
|
return new WP_Error('missing_mailchimp_list_id'); |
|
|
|
} |
|
|
|
$this->add_subscriber_tags($user_id, $tags); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@ -153,8 +135,10 @@ class Reuna_Mailchimp_Public { |
|
|
|
* |
|
|
|
* @since 1.0.0 |
|
|
|
*/ |
|
|
|
protected function subscriber_exists($user) |
|
|
|
protected function subscriber_exists($user_id) |
|
|
|
{ |
|
|
|
$user = get_userdata($user_id); |
|
|
|
|
|
|
|
$mailchimp = new Mailchimp($this->api_key); |
|
|
|
$subscriber_hash = MailChimp::subscriberHash($user->user_email); |
|
|
|
|
|
|
|
@ -168,8 +152,10 @@ class Reuna_Mailchimp_Public { |
|
|
|
* |
|
|
|
* @since 1.0.0 |
|
|
|
*/ |
|
|
|
protected function add_subscriber_tags(WP_User $user, array $tags) |
|
|
|
protected function add_subscriber_tags($user_id, array $tags) |
|
|
|
{ |
|
|
|
$user = get_userdata($user_id); |
|
|
|
|
|
|
|
$mailchimp = new Mailchimp($this->api_key); |
|
|
|
$subscriber_hash = MailChimp::subscriberHash($user->user_email); |
|
|
|
|
|
|
|
@ -194,8 +180,10 @@ class Reuna_Mailchimp_Public { |
|
|
|
* |
|
|
|
* @since 1.0.0 |
|
|
|
*/ |
|
|
|
protected function create_subscriber(WP_User $user) |
|
|
|
public function create_subscriber(int $user_id) |
|
|
|
{ |
|
|
|
$user = get_userdata($user_id); |
|
|
|
|
|
|
|
$mailchimp = new Mailchimp($this->api_key); |
|
|
|
|
|
|
|
$options = [ |
|
|
|
|