You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 

200 lines
4.5 KiB

<?php
/**
* The public-facing functionality of the plugin.
*
* @link https://horizontes.info
* @since 1.0.0
*
* @package Reuna_Mailchimp
* @subpackage Reuna_Mailchimp/public
*/
use \DrewM\MailChimp\MailChimp;
/**
* The public-facing functionality of the plugin.
*
* Defines the plugin name, version, and two examples hooks for how to
* enqueue the public-facing stylesheet and JavaScript.
*
* @package Reuna_Mailchimp
* @subpackage Reuna_Mailchimp/public
* @author Horizontes Coop. <contato@horizontes.info>
*/
class Reuna_Mailchimp_Public {
/**
* The ID of this plugin.
*
* @since 1.0.0
* @access private
* @var string $plugin_name The ID of this plugin.
*/
private $plugin_name;
/**
* The version of this plugin.
*
* @since 1.0.0
* @access private
* @var string $version The current version of this plugin.
*/
private $version;
/**
* The MailChimp API key
*
* @since 1.0.0
* @access private
* @var string $api_key The MailChimp API key
*/
private $api_key;
/**
* The MailChimp list ID
*
* @since 1.0.0
* @access private
* @var string $list_id The MailChimp list ID
*/
private $list_id;
/**
* Initialize the class and set its properties.
*
* @since 1.0.0
* @param string $plugin_name The name of the plugin.
* @param string $version The version of this plugin.
*/
public function __construct( $plugin_name, $version )
{
$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');
}
}
/**
* Register the JavaScript for the public-facing side of the site.
*
* @since 1.0.0
*/
public function enqueue_scripts()
{
wp_enqueue_script($this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/reuna-mailchimp-public.js', array( 'jquery' ), $this->version, false);
wp_localize_script($this->plugin_name, 'wpApiSettings', [
'root' => esc_url_raw( rest_url() ),
'nonce' => wp_create_nonce( 'wp_rest' )
]);
}
/**
* Register REST API routes provided by the plugin
*
* @since 1.0.0
*/
public function register_rest_api_routes()
{
register_rest_route($this->plugin_name . '/v1', '/subscribe', [
'methods' => 'POST',
'callback' => [$this, 'handle_rest_api_request'],
'permission_callback' => function () {
return current_user_can('read');
}
]);
}
/**
* Handle the REST API Request
*
* @since 1.0.0
*/
public function handle_rest_api_request(WP_REST_Request $request)
{
$user_id = get_current_user_id();
if (! $this->subscriber_exists($user_id)) {
$this->create_subscriber($user_id);
}
$tags = $request->get_param('tags');
if (! is_array($tags)) {
return;
}
$this->add_subscriber_tags($user_id, $tags);
}
/**
* Check if a user is already subscribed to a list
*
* @since 1.0.0
*/
protected function subscriber_exists($user_id)
{
$user = get_userdata($user_id);
$mailchimp = new Mailchimp($this->api_key);
$subscriber_hash = MailChimp::subscriberHash($user->user_email);
$mailchimp->get('lists/' . $this->list_id . '/members/' . $subscriber_hash);
return $mailchimp->success();
}
/**
* Add tags to an existing subscriber
*
* @since 1.0.0
*/
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);
$existing_tags = $mailchimp->get('lists/' . $this->list_id . '/members/' . $subscriber_hash . '/tags');
$existing_tags = array_column($existing_tags, 'name');
$new_tags = array_values(array_unique(array_merge($existing_tags, $tags)));
$new_tags = array_map(function($tag) {
return [
'name' => $tag,
'status' => 'active',
];
}, $new_tags);
return $mailchimp->post('lists/' . $this->list_id . '/members/' . $subscriber_hash . '/tags', [
'tags' => $new_tags,
]);
}
/**
* Subscribe user to list
*
* @since 1.0.0
*/
public function create_subscriber(int $user_id)
{
$user = get_userdata($user_id);
$mailchimp = new Mailchimp($this->api_key);
$options = [
'email_address' => $user->user_email,
'status' => 'subscribed',
'merge_fields' => [
'FNAME' => $user->first_name,
'LNAME' => $user->last_name,
],
];
return $mailchimp->post('lists/' . $this->list_id . '/members', $options);
}
}