PluginProbe
Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets / trunk
Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets vtrunk
4.5.4 4.2.1 4.2.2 4.2.3 4.5.0 4.5.2 4.5.3 4.2.0 4.1.18 4.1.17 4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.1.9 4.1.8 4.0.9 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 All 146 releases
ultimate-post-kit / modules / newsletter / module.php

module.php in Ultimate Post Kit – Elementor Post Grid, Post Carousel, Post Slider & Blog Layout Widgets trunk, at modules/newsletter/module.php

120 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // This endpoint is registered on wp_ajax_nopriv_, so it is reachable before the
49 // site owner has configured Mailchimp at all. Without credentials the request
50 // below would be built against an unresolvable host and fail.
51 if (empty($api_key) || empty($list_id) || false === strpos($api_key, '-')) {
52 return null;
53 }
54
55 $args = array(
56 'method' => 'PUT',
57 'headers' => array(
58 'Authorization' => 'Basic ' . base64_encode('user:' . $api_key)
59 ),
60 'body' => json_encode(array(
61 'email_address' => $email,
62 'status' => $status,
63 'merge_fields' => $merge_fields
64 ))
65 );
66 $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);
67
68 // A transport failure returns WP_Error, which is an object: indexing it as an
69 // array is a fatal. The caller already handles a null/!is_object result.
70 if (is_wp_error($response)) {
71 return null;
72 }
73
74 $body = json_decode(wp_remote_retrieve_body($response));
75
76 return $body;
77 }
78
79
80 public function mailchimp_subscribe()
81 {
82
83 // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended -- unauthenticated public newsletter subscribe form; input is sanitized and the e-mail validated before use, no nonce is expected from anonymous visitors.
84 $fname = (isset($_POST['fname']) && !empty($_POST['fname'])) ? sanitize_text_field(wp_unslash($_POST['fname'])) : '';
85
86 // Validate the address before hitting the Mailchimp API. This endpoint is
87 // unauthenticated, so reject anything that is not a real e-mail rather
88 // than forwarding arbitrary input to the list.
89 // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended -- unauthenticated public newsletter subscribe form; input is sanitized and the e-mail validated before use, no nonce is expected from anonymous visitors.
90 $email = isset($_POST['email']) ? sanitize_email(wp_unslash($_POST['email'])) : '';
91
92 if (empty($email) || ! is_email($email)) {
93 echo '<div class="upk-text-warning">' . esc_html_x('Please enter a valid email address.', 'Mailchimp String', 'ultimate-post-kit') . '</div>';
94 die;
95 }
96
97 $result = $this->mailchimp_subscriber_status($email, 'subscribed', ['FNAME' => $fname, 'LNAME' => '']);
98
99 if (! is_object($result) || ! isset($result->status)) {
100 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>';
101 die;
102 }
103
104 if ($result->status == 400) {
105 if (isset($result->detail) && !empty($result->detail)) {
106 echo '<div class="upk-text-warning">' . esc_html($result->detail) . '</div>';
107 } else {
108 echo '<div class="upk-text-warning">' . esc_html_x('Your request could not be processed', 'Mailchimp String', 'ultimate-post-kit') . '</div>';
109 }
110 } elseif ($result->status == 401) {
111 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>';
112 } elseif ($result->status == 200 || $result->status == 'subscribed') {
113 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>';
114 } else {
115 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>';
116 }
117 die;
118 }
119 }
120