PluginProbe
WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation / trunk
WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation vtrunk
1.4.48 1.4.47 1.4.46 1.4.45 1.4.44 1.4.43 1.4.42 1.4.41 1.4.40 1.4.39 1.4.38 1.4.37 1.4.36 1.1.2 1.2.0 1.2.1 1.2.2 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 All 64 releases
optin / includes / integrations / implementations / class-mailchimp.php

class-mailchimp.php in WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation trunk, at includes/integrations/implementations/class-mailchimp.php

271 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore
2
3 namespace OPTN\Includes\Integrations\Implementations;
4
5 use http\Url;
6 use OPTN\Includes\Integrations\Base\BaseMailIntegration;
7 use OPTN\Includes\Utils\Cache;
8 use OPTN\Includes\Utils\Utils;
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Mailchimp Integration class.
14 *
15 * @link https://mailchimp.com/developer/marketing/docs/fundamentals/
16 */
17 class Mailchimp extends BaseMailIntegration {
18
19 /**
20 * Endpoint for Mailchimp
21 *
22 * @var Url $end_point
23 */
24 private $end_point;
25
26 /**
27 * Get name
28 *
29 * @return string
30 */
31 public function get_name() {
32 return 'mailchimp';
33 }
34
35 /**
36 * Constructor
37 *
38 * @param int|null $account_id account id.
39 */
40 public function __construct( $account_id ) {
41 parent::__construct( $account_id );
42 $settings = $this->get_settings();
43 $data = explode( '-', $settings['apiKey'] );
44 $data_center = end( $data );
45 $this->end_point = 'https://' . $data_center . '.api.mailchimp.com/3.0';
46 }
47
48 /**
49 * Adds a subscribe to Mailchimp by list id
50 *
51 * @param array $data data.
52 * @return bool
53 *
54 * @link https://mailchimp.com/developer/marketing/api/list-members/add-member-to-list/
55 */
56 public function add_subscriber( $data ): bool {
57
58 if ( ! isset( $data['fields'] ) || ! isset( $data['fields']['email'] ) ) {
59 return false;
60 }
61
62 $email = $data['fields']['email'];
63 unset( $data['fields']['email'] );
64
65 $body_template = array(
66 'email_address' => $email,
67 'status' => ! empty( $data['doubleOpt'] ) ? 'pending' : 'subscribed',
68 'ip_opt' => Utils::get_user_ip(),
69 );
70
71 if ( ! empty( $data['fields'] ) ) {
72 $body_template['merge_fields'] = $data['fields'];
73 }
74
75 $data['fields']['email'] = $email;
76
77 $success = false;
78
79 foreach ( $data['list'] as $list_id ) {
80 $body = $body_template;
81
82 $res = wp_remote_post(
83 $this->end_point . '/lists/' . $list_id . '/members',
84 array(
85 'body' => wp_json_encode( $body ),
86 'timeout' => 45,
87 'headers' => $this->get_headers(),
88 )
89 );
90
91 // Response code should be either 200 or 201.
92 if ( ! is_wp_error( $res ) && in_array( wp_remote_retrieve_response_code( $res ), array( 200, 201 ) ) ) { // phpcs:ignore
93 $success = true;
94 }
95
96 $resp_body = wp_remote_retrieve_body( $res );
97 $resp_body = json_decode( $resp_body, true );
98
99 // Duplicate email case.
100 if ( 'Member Exists' === ( $resp_body['title'] ?? '' ) ) {
101 $success = true;
102 }
103 }
104
105 if ( $success ) {
106 $this->add_lead( $data );
107 }
108
109 return $success;
110 }
111
112
113 /**
114 * Adds a lead to the leads table
115 *
116 * @param array $data data.
117 * @return void
118 */
119 public function add_lead( $data ) {
120
121 $fields = Utils::sanitize_json_array( $data['fields'] );
122 $fields['ip'] = Utils::get_user_ip();
123
124 $leads_data = array(
125 'email' => sanitize_email( $fields['email'] ),
126 'name' => isset( $fields['name'] ) ? $fields['name'] : null,
127 'conv_id' => intval( $data['conv_id'] ),
128 'data' => $fields,
129 'integration' => 'mailchimp',
130 );
131
132 $this->db->add_lead( $leads_data );
133 }
134
135
136 /**
137 * Gets the fields for Mailchimp by list id
138 *
139 * @param array $args receive data as array.
140 * @return array
141 * @link https://mailchimp.com/developer/marketing/api/list-merges/list-merge-fields/
142 */
143 public function get_fields( $args = array() ): array {
144
145 if ( empty( $args['list_id'] ) ) {
146 return array();
147 }
148 $use_cache = $args['use_cache'];
149
150 $list_ids = is_array( $args['list_id'] ) ? $args['list_id'] : array( $args['list_id'] );
151 $unique_fields = array();
152
153 foreach ( $list_ids as $list_id ) {
154 if ( $use_cache ) {
155 $cached = Cache::get( 'optn_int_mailchimp_fields_' . $this->account_id . '_' . $list_id );
156 if ( ! empty( $cached ) ) {
157 $fields = json_decode( $cached, true );
158 $unique_fields = $this->merge_unique_fields( $unique_fields, $fields );
159 continue;
160 }
161 }
162
163 $res = wp_remote_get(
164 $this->end_point . '/lists/' . $list_id . '/merge-fields',
165 array(
166 'timeout' => 45,
167 'headers' => $this->get_headers(),
168 )
169 );
170
171 if ( ! is_wp_error( $res ) && 200 == wp_remote_retrieve_response_code( $res ) ) { // phpcs:ignore
172 $body = wp_remote_retrieve_body( $res );
173 $body = json_decode( $body, true );
174
175 if ( isset( $body['merge_fields'] ) && is_array( $body['merge_fields'] ) ) {
176 $fields = array();
177
178 foreach ( $body['merge_fields'] as $d ) {
179 $fields[] = array(
180 'value' => $d['tag'],
181 'label' => $d['name'],
182 );
183 }
184
185 $fields[] = array(
186 'value' => 'email',
187 'label' => 'Email',
188 );
189
190 // Cache the fields.
191 Cache::set( 'optn_int_mailchimp_fields_' . $this->account_id . '_' . $list_id, wp_json_encode( $fields ) );
192
193 $unique_fields = $this->merge_unique_fields( $unique_fields, $fields );
194 }
195 }
196 }
197
198 return $unique_fields;
199 }
200
201 /**
202 * Merge fields for multiple list
203 *
204 * @param array $existing_fields existing fields.
205 * @param array $new_fields new fields.
206 * @return array
207 */
208 private function merge_unique_fields( array $existing_fields, array $new_fields ): array {
209 foreach ( $new_fields as $field ) {
210 if ( ! array_key_exists( $field['value'], array_column( $existing_fields, 'value' ) ) ) {
211 $existing_fields[] = $field;
212 }
213 }
214
215 return $existing_fields;
216 }
217
218 /**
219 * Gets list for mailchimp
220 *
221 * @param array $args receive optional data as array.
222 * @return array
223 *
224 * @link https://mailchimp.com/developer/marketing/api/lists/
225 */
226 public function get_lists( $args = array() ): array {
227 if ( ! empty( $args['list_ids'] ) ) {
228 return array();
229 }
230 $use_cache = $args['use_cache'];
231 if ( $use_cache ) {
232 $res = Cache::get( 'optn_int_mailchimp_lists_' . $this->account_id );
233
234 if ( ! empty( $res ) ) {
235 return json_decode( $res );
236 }
237 }
238
239 $res = wp_remote_get(
240 $this->end_point . '/lists',
241 array(
242 'timeout' => 45,
243 'headers' => $this->get_headers(),
244 )
245 );
246
247 if ( ! is_wp_error( $res ) && 200 == wp_remote_retrieve_response_code( $res ) ) { // phpcs:ignore
248 $body = wp_remote_retrieve_body( $res );
249 $body = json_decode( $body, true );
250
251 if ( isset( $body['lists'] ) && is_array( $body['lists'] ) ) {
252
253 $ret = array();
254
255 foreach ( $body['lists'] as $d ) {
256 $ret[] = array(
257 'value' => $d['id'],
258 'label' => $d['name'],
259 );
260 }
261
262 Cache::set( 'optn_int_mailchimp_lists_' . $this->account_id, wp_json_encode( $ret ) );
263
264 return $ret;
265 }
266 }
267
268 return array();
269 }
270 }
271