| 1 |
<?php |
| 2 |
|
| 3 |
namespace UltimatePostKit\Modules\Newsletter; |
| 4 |
|
| 5 |
use UltimatePostKit\Base\Ultimate_Post_Kit_Module_Base; |
| 6 |
|
| 7 |
if (!defined('ABSPATH')) exit; // Exit if accessed directly |
| 8 |
|
| 9 |
class Module extends Ultimate_Post_Kit_Module_Base |
| 10 |
{ |
| 11 |
|
| 12 |
public function __construct() |
| 13 |
{ |
| 14 |
parent::__construct(); |
| 15 |
|
| 16 |
add_action('wp_ajax_ultimate_post_kit_mailchimp_subscribe', [$this, 'mailchimp_subscribe']); |
| 17 |
add_action('wp_ajax_nopriv_ultimate_post_kit_mailchimp_subscribe', [$this, 'mailchimp_subscribe']); |
| 18 |
} |
| 19 |
|
| 20 |
public function get_name() |
| 21 |
{ |
| 22 |
return 'newsletter'; |
| 23 |
} |
| 24 |
|
| 25 |
public function get_widgets() |
| 26 |
{ |
| 27 |
|
| 28 |
$widgets = ['Newsletter']; |
| 29 |
|
| 30 |
return $widgets; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* subscribe mailchimp with api key |
| 35 |
* @param string $email any valid email |
| 36 |
* @param string $status subscribe or unsubscribe |
| 37 |
* @param array $merge_fields First name and last name of subscriber |
| 38 |
* @return [type] [description] |
| 39 |
*/ |
| 40 |
public function mailchimp_subscriber_status($email, $status, $merge_fields = array('FNAME' => '', 'LNAME' => '')) |
| 41 |
{ |
| 42 |
|
| 43 |
$options = get_option('ultimate_post_kit_api_settings'); |
| 44 |
|
| 45 |
$list_id = (!empty($options['mailchimp_list_id'])) ? $options['mailchimp_list_id'] : ''; // Your list is here |
| 46 |
$api_key = (!empty($options['mailchimp_api_key'])) ? $options['mailchimp_api_key'] : ''; // Your mailchimp api key here |
| 47 |
|
| 48 |
$args = array( |
| 49 |
'method' => 'PUT', |
| 50 |
'headers' => array( |
| 51 |
'Authorization' => 'Basic ' . base64_encode('user:' . $api_key) |
| 52 |
), |
| 53 |
'body' => json_encode(array( |
| 54 |
'email_address' => $email, |
| 55 |
'status' => $status, |
| 56 |
'merge_fields' => $merge_fields |
| 57 |
)) |
| 58 |
); |
| 59 |
$response = wp_remote_post('https://' . substr($api_key, strpos($api_key, '-') + 1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($email)), $args); |
| 60 |
|
| 61 |
$body = json_decode($response['body']); |
| 62 |
|
| 63 |
return $body; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
public function mailchimp_subscribe() |
| 68 |
{ |
| 69 |
|
| 70 |
$fname = (isset($_POST['fname']) && !empty($_POST['fname'])) ? sanitize_text_field($_POST['fname']) : ''; |
| 71 |
|
| 72 |
$result = $this->mailchimp_subscriber_status(sanitize_text_field($_POST['email']), 'subscribed', ['FNAME' => $fname, 'LNAME' => '']); |
| 73 |
|
| 74 |
if ($result->status == 400) { |
| 75 |
if (isset($result->detail) && !empty($result->detail)) { |
| 76 |
echo '<div class="upk-text-warning">' . esc_html($result->detail) . '</div>'; |
| 77 |
} else { |
| 78 |
echo '<div class="upk-text-warning">' . esc_html_x('Your request could not be processed', 'Mailchimp String', 'ultimate-post-kit') . '</div>'; |
| 79 |
} |
| 80 |
} elseif ($result->status == 401) { |
| 81 |
echo '<div class="upk-text-warning">' . esc_html_x('Error: You did not set the API keys or List ID in admin settings!', 'Mailchimp String', 'ultimate-post-kit') . '</div>'; |
| 82 |
} elseif ($result->status == 200 || $result->status == 'subscribed') { |
| 83 |
echo '<div class="upk-text-success"><span class="upk-success-icon"></span> ' . esc_html_x('Thank you, You have subscribed successfully.', 'Mailchimp String', 'ultimate-post-kit') . '</div>'; |
| 84 |
} else { |
| 85 |
echo '<div class="upk-text-danger">' . esc_html_x('An unexpected internal error has occurred. Please contact Support for more information.', 'Mailchimp String', 'ultimate-post-kit') . '</div>'; |
| 86 |
} |
| 87 |
die; |
| 88 |
} |
| 89 |
} |
| 90 |
|