*/ 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'); } ]); } /** * Subscribe user on registration * * @since 1.1.0 */ public function handle_user_register(int $user_id) { try { $this->create_subscriber($user_id); } catch (\Exception $exception) { // TODO: log the exception } } /** * 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); } }