PluginProbe
Getwid – Gutenberg Blocks / 3.0.2
Getwid – Gutenberg Blocks v3.0.2
3.0.2 3.0.1 3.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.2.0 trunk 1.8.7 1.9.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
← All changes | includes/blocks/mailchimp.php +268 -298 2.0.133.0.2 View file →
@@ -1,396 +1,366 @@
1 1 <?php
2 2
3 3 namespace Getwid\Blocks;
4 4
5 -use DrewM\MailChimp\MailChimp as MC;
5 +class Mailchimp extends AbstractBlock {
6 6
7 -class MailChimp extends \Getwid\Blocks\AbstractBlock {
7 + private $mailchimp;
8 8
9 - private $mailchimp;
9 + public function __construct() {
10 10
11 - protected static $blockName = 'getwid/mailchimp';
11 + parent::__construct( 'getwid/mailchimp' );
12 12
13 - public function __construct() {
13 + add_action( 'wp_ajax_getwid_mailchimp_api_key_manage', array( $this, 'mailchimp_api_key_manage' ) );
14 + add_action( 'wp_ajax_getwid_subscribe', array( $this, 'subscribe' ) );
15 + add_action( 'wp_ajax_nopriv_getwid_subscribe', array( $this, 'subscribe' ) );
14 16
15 - parent::__construct( self::$blockName );
17 + $this->register_mailchimp_blocks();
18 + }
16 19
17 - add_action( 'wp_ajax_getwid_mailchimp_api_key_manage', [ $this, 'mailchimp_api_key_manage'] );
18 -
19 - add_action( 'wp_ajax_getwid_subscribe' , [ $this, 'subscribe' ] );
20 - add_action( 'wp_ajax_nopriv_getwid_subscribe', [ $this, 'subscribe' ] );
21 -
22 - $this->register_mailchimp_blocks();
23 - }
24 -
25 - public function getLabel() {
26 - return __('Mailchimp', 'getwid');
20 + public function get_label() {
21 + return __( 'Mailchimp', 'getwid' );
27 22 }
28 23
29 - private function register_mailchimp_blocks() {
24 + private function register_mailchimp_blocks() {
30 25
31 - /* #region register all blocks */
32 - register_block_type(
33 - 'getwid/mailchimp',
34 - array(
35 - 'render_callback' => [ $this, 'render_callback' ]
36 - )
37 - );
26 + register_block_type(
27 + getwid_get_plugin_path( 'assets/blocks/mailchimp/mailchimp' ),
28 + array(
29 + 'render_callback' => array( $this, 'render_callback' ),
30 + )
31 + );
38 32
39 - $field_email = 'getwid/mailchimp-field-email';
40 - register_block_type(
41 - $field_email,
42 - array(
43 - 'render_callback' => [ $this, 'render_mailchimp_field_email' ]
44 - )
45 - );
33 + register_block_type(
34 + getwid_get_plugin_path( 'assets/blocks/mailchimp/mailchimp-field-email' ),
35 + array(
36 + 'render_callback' => array( $this, 'render_mailchimp_field_email' ),
37 + )
38 + );
46 39
47 - $field_first_name = 'getwid/mailchimp-field-first-name';
48 - register_block_type(
49 - $field_first_name,
50 - array(
51 - 'render_callback' => [ $this, 'render_mailchimp_field_first_name' ]
52 - )
53 - );
40 + register_block_type(
41 + getwid_get_plugin_path( 'assets/blocks/mailchimp/mailchimp-field-first-name' ),
42 + array(
43 + 'render_callback' => array( $this, 'render_mailchimp_field_first_name' ),
44 + )
45 + );
54 46
55 - $field_last_name = 'getwid/mailchimp-field-last-name';
56 - register_block_type(
57 - $field_last_name,
58 - array(
59 - 'render_callback' => [ $this, 'render_mailchimp_field_last_name' ]
60 - )
61 - );
62 - /* #endregion */
63 - }
47 + register_block_type(
48 + getwid_get_plugin_path( 'assets/blocks/mailchimp/mailchimp-field-last-name' ),
49 + array(
50 + 'render_callback' => array( $this, 'render_mailchimp_field_last_name' ),
51 + )
52 + );
53 + }
64 54
65 - public function block_frontend_assets() {
55 + public function block_frontend_assets() {
66 56
67 - if ( is_admin() ) {
68 - return;
69 - }
70 -
71 - if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) {
57 + if ( is_admin() ) {
72 58 return;
73 59 }
74 60
75 - $rtl = is_rtl() ? '.rtl' : '';
61 + $inline_script = 'var Getwid = Getwid || {};';
62 + $inline_script .= 'Getwid["ajax_url"] = ' . wp_json_encode( admin_url( 'admin-ajax.php' ) ) . ';';
76 63
77 - wp_enqueue_style(
78 - self::$blockName,
79 - getwid_get_plugin_url( 'assets/blocks/mailchimp/style' . $rtl . '.css' ),
80 - [],
81 - getwid()->settings()->getVersion()
82 - );
83 -
84 - wp_enqueue_script(
85 - self::$blockName,
86 - getwid_get_plugin_url( 'assets/blocks/mailchimp/frontend.js' ),
87 - [ 'jquery' ],
88 - getwid()->settings()->getVersion(),
89 - true
90 - );
91 -
92 - /*
93 - * var Getwid = {"ajax_url":"https:\/\/getwid.loc\/wp-admin\/admin-ajax.php"};
94 - */
95 - $inline_script =
96 - 'var Getwid = Getwid || {};' .
97 - 'Getwid["ajax_url"] = ' . json_encode( admin_url( 'admin-ajax.php' ) ) . ';'
98 - ;
99 -
100 64 wp_add_inline_script(
101 - self::$blockName,
65 + 'getwid-mailchimp-view-script',
102 66 $inline_script,
103 67 'before'
104 68 );
69 + }
105 70
106 - }
71 + public function render_callback( $attributes, $content ) {
107 72
108 - public function render_callback( $attributes, $content ) {
73 + $class = 'wp-block-getwid-mailchimp';
74 + $block_name = 'wp-block-getwid-mailchimp';
109 75
110 - $class = 'wp-block-getwid-mailchimp';
111 - $block_name = $class;
76 + if ( isset( $attributes['className'] ) ) {
77 + $class .= ' ' . $attributes['className'];
78 + }
112 79
113 - if ( isset( $attributes[ 'className' ] ) ) {
114 - $class .= ' ' . $attributes[ 'className' ];
115 - }
80 + if ( isset( $attributes['align'] ) ) {
81 + $class .= ' align' . $attributes['align'];
82 + }
116 83
117 - if ( isset( $attributes[ 'align' ] ) ) {
118 - $class .= ' align' . $attributes[ 'align' ];
119 - }
84 + $button_style = '';
85 + $button_class = '';
120 86
121 - $button_style = $button_class = '';
87 + getwid_custom_color_style_and_class( $button_style, $button_class, $attributes, 'color' );
88 + getwid_custom_color_style_and_class( $button_style, $button_class, $attributes, 'background' );
122 89
123 - getwid_custom_color_style_and_class( $button_style, $button_class, $attributes, 'color' );
124 - getwid_custom_color_style_and_class( $button_style, $button_class, $attributes, 'background' );
90 + $extra_attr = array(
91 + 'class' => $class,
92 + 'block_name' => $block_name,
93 + 'content' => $content,
94 + 'button_style' => $button_style,
95 + 'button_class' => $button_class,
96 + );
125 97
126 - $extra_attr = array(
127 - 'class' => $class,
128 - 'block_name' => $block_name,
129 - 'content' => $content,
98 + ob_start();
99 + ?>
100 + <div class="<?php echo esc_attr( $class ); ?>">
101 + <?php getwid_get_template_part( 'mailchimp/mailchimp', $attributes, false, $extra_attr ); ?>
102 + </div>
103 + <?php
130 104
131 - 'button_style' => $button_style,
132 - 'button_class' => $button_class
133 - );
105 + $result = ob_get_clean();
134 106
135 - ob_start();
136 -
137 - if ( isset( $attributes[ 'ids' ] ) ) {?>
138 - <div class='<?php echo esc_attr( $class ); ?>'>
139 - <?php getwid_get_template_part( 'mailchimp/mailchimp', $attributes, false, $extra_attr ); ?>
140 - </div><?php
141 - } else {?>
142 - <p><?php echo esc_html__( 'Select at least one MailChimp list.', 'getwid' ); ?></p><?php
143 - }
144 -
145 - $chash = ob_get_clean();
146 -
147 107 $this->block_frontend_assets();
148 108
149 - return $chash;
150 - }
109 + return $result;
110 + }
151 111
152 - /* #region render inner blocks methods */
153 - public function render_mailchimp_field_email( $attributes ) {
154 - ob_start();?>
155 - <?php getwid_get_template_part( 'mailchimp/field-email', $attributes, false ); ?><?php
112 + public function render_mailchimp_field_email( $attributes ) {
113 + ob_start();
114 + getwid_get_template_part( 'mailchimp/field-email', $attributes, false );
156 115
157 - $chash = ob_get_clean();
158 - return $chash;
159 - }
116 + return ob_get_clean();
117 + }
160 118
161 - public function render_mailchimp_field_first_name( $attributes ) {
162 - $extra_attr = array( 'name' => 'first_name' );
119 + public function render_mailchimp_field_first_name( $attributes ) {
120 + $extra_attr = array( 'name' => 'first_name' );
163 121
164 - if ( ! isset( $attributes[ 'label' ] ) ) {
165 - $attributes[ 'label' ] = __( 'First name', 'getwid' );
166 - }
122 + if ( ! isset( $attributes['label'] ) ) {
123 + $attributes['label'] = __( 'First name', 'getwid' );
124 + }
167 125
168 - ob_start();?>
169 - <?php getwid_get_template_part( 'mailchimp/field-first-name', $attributes, false, $extra_attr ); ?><?php
126 + ob_start();
127 + getwid_get_template_part( 'mailchimp/field-first-name', $attributes, false, $extra_attr );
170 128
171 - $chash = ob_get_clean();
172 - return $chash;
173 - }
129 + return ob_get_clean();
130 + }
174 131
175 - public function render_mailchimp_field_last_name( $attributes ) {
176 - $extra_attr = array( 'name' => 'last_name' );
132 + public function render_mailchimp_field_last_name( $attributes ) {
133 + $extra_attr = array( 'name' => 'last_name' );
177 134
178 - if ( ! isset( $attributes[ 'label' ] ) ) {
179 - $attributes[ 'label' ] = __( 'Last name', 'getwid' );
180 - }
135 + if ( ! isset( $attributes['label'] ) ) {
136 + $attributes['label'] = __( 'Last name', 'getwid' );
137 + }
181 138
182 - ob_start();?>
183 - <?php getwid_get_template_part( 'mailchimp/field-last-name', $attributes, false, $extra_attr ); ?><?php
139 + ob_start();
140 + getwid_get_template_part( 'mailchimp/field-last-name', $attributes, false, $extra_attr );
184 141
185 - $chash = ob_get_clean();
186 - return $chash;
187 - }
188 - /* #endregion */
142 + return ob_get_clean();
143 + }
189 144
190 - public function mailchimp_api_key_manage() {
191 - $nonce = sanitize_key( $_POST[ 'nonce' ] );
145 + public function mailchimp_api_key_manage() {
146 + $nonce = sanitize_key( $_POST['nonce'] );
192 147
193 - if ( ! wp_verify_nonce( $nonce, 'getwid_nonce_mailchimp_api_key' ) ) {
194 - wp_send_json_error();
195 - }
148 + if ( ! wp_verify_nonce( $nonce, 'getwid_nonce_mailchimp_api_key' ) ) {
149 + wp_send_json_error();
150 + }
196 151
197 - $option = sanitize_text_field( wp_unslash( $_POST[ 'option' ] ) );
152 + $option = sanitize_text_field( wp_unslash( $_POST['option'] ) );
198 153
199 - if ( ! current_user_can( 'manage_options' ) && ! in_array( $option, [ 'sync', 'load' ] ) ) {
200 - wp_send_json_error( esc_html__( 'You are not allowed to perform this action. Please contact the administrator.', 'getwid' ) );
201 - }
154 + if ( ! current_user_can( 'manage_options' ) && ! in_array( $option, array( 'sync', 'load' ), true ) ) {
155 + wp_send_json_error( esc_html__( 'You are not allowed to perform this action. Please contact the administrator.', 'getwid' ) );
156 + }
202 157
203 - switch ( $option ) {
204 - case 'save':
205 - $api_key = sanitize_text_field( wp_unslash( $_POST[ 'data' ][ 'api_key' ] ) );
206 - update_option( 'getwid_mailchimp_api_key', $api_key );
207 - break;
208 - case 'delete':
209 - delete_option( 'getwid_mailchimp_api_key' );
210 - delete_option( 'audiences_list_chash' );
211 - wp_send_json_success();
212 - die();
213 - break;
214 - }
158 + switch ( $option ) {
159 + case 'save':
160 + $api_key = sanitize_text_field( wp_unslash( $_POST['data']['api_key'] ) );
161 + update_option( 'getwid_mailchimp_api_key', $api_key );
162 + break;
163 + case 'delete':
164 + delete_option( 'getwid_mailchimp_api_key' );
165 + delete_option( 'audiences_list_chash' );
166 + wp_send_json_success();
167 + return;
168 + }
215 169
216 - if ( ! isset( $api_key ) ) {
217 - $api_key = get_option( 'getwid_mailchimp_api_key', '' );
218 - }
170 + if ( ! isset( $api_key ) ) {
171 + $api_key = get_option( 'getwid_mailchimp_api_key', '' );
172 + }
219 173
220 - if ( ! empty( $api_key ) ) {
174 + if ( ! empty( $api_key ) ) {
175 + try {
176 + $this->mailchimp = new \DrewM\MailChimp\MailChimp( $api_key );
177 + } catch ( \Exception $exception ) {
178 + wp_send_json_error( $exception->getMessage() );
179 + }
221 180
222 - try {
223 - $this->mailchimp = new MC( $api_key );
224 - } catch ( \Exception $exception ) {
225 - wp_send_json_error( $exception->getMessage() );
226 - }
181 + $maybe_cached = $this->get_account_subscribe_lists( 'sync' === $option );
227 182
228 - $maybe_cached = $this->get_account_subscribe_lists( $option === 'sync' );
183 + wp_send_json_success( $maybe_cached );
184 + }
185 + }
229 186
230 - wp_send_json_success( $maybe_cached );
231 - }
232 - }
187 + public function get_lists() {
233 188
234 - public function get_lists() {
189 + $response = $this->mailchimp->get( 'lists' );
235 190
236 - $response = $this->mailchimp->get( 'lists' );
191 + if ( $this->mailchimp->success() ) {
192 + if ( isset( $response['lists'] ) ) {
193 + $response = array_map(
194 + function ( $item ) {
195 + return array(
196 + 'id' => $item['id'],
197 + 'title' => $item['name'],
198 + );
199 + },
200 + $response['lists']
201 + );
202 + }
203 + } else {
204 + $error = $this->mailchimp->getLastError();
205 + wp_send_json_error( $error );
206 + }
237 207
238 - if ( $this->mailchimp->success() ) {
239 - if ( isset( $response[ 'lists' ] ) ) {
240 - $response = array_map( function ( $item ) {
241 - return array( 'id' => $item[ 'id' ], 'title' => $item[ 'name' ] );
242 - }, $response[ 'lists' ] );
243 - }
244 - } else {
245 - $error = $this->mailchimp->getLastError();
246 - wp_send_json_error( $error );
247 - }
208 + return $response;
209 + }
248 210
249 - return $response;
250 - }
211 + public function get_account_subscribe_lists( $sync = false ) {
251 212
252 - public function get_account_subscribe_lists( $sync = false ) {
213 + if ( ! $sync ) {
214 + $cache = get_option( 'audiences_list_chash' );
215 + }
253 216
254 - if ( ! $sync ) {
255 - $cache = get_option( 'audiences_list_chash' );
256 - }
217 + if ( $sync || empty( $cache ) ) {
218 + $cache = array();
257 219
258 - if ( $sync || empty( $cache ) ) {
259 - $cache = array();
220 + $list = $this->get_lists();
260 221
261 - $list = $this->get_lists();
222 + if ( count( $list ) > 0 ) {
223 + $cache = $list;
262 224
263 - if ( count( $list ) > 0 ) {
264 - $cache = $list;
225 + foreach ( $list as $key => $list_item ) {
226 + $categories = $this->get_interest_categories( $list_item['id'] );
265 227
266 - foreach ( $list as $key => $list_item ) {
267 - $categories = $this->get_interest_categories( $list_item[ 'id' ] );
228 + $cache[ $key ]['categories'] = $categories;
229 + foreach ( $cache[ $key ]['categories'] as $k => $category_item ) {
230 + $interests = $this->get_interests( $list_item['id'], $category_item['id'] );
231 + $cache[ $key ]['categories'][ $k ]['interests'] = $interests;
232 + }
233 + }
234 + }
268 235
269 - $cache[ $key ][ 'categories' ] = $categories;
270 - foreach ( $cache[ $key ][ 'categories' ] as $k => $category_item ) {
271 - $interests = $this->get_interests( $list_item[ 'id' ], $category_item[ 'id' ] );
272 - $cache[ $key ][ 'categories' ][ $k ][ 'interests' ] = $interests;
273 - }
274 - }
275 - }
236 + if ( ! empty( $cache ) ) {
237 + update_option( 'audiences_list_chash', $cache );
238 + }
239 + }
276 240
277 - if ( ! empty( $cache ) ) {
278 - update_option( 'audiences_list_chash', $cache );
279 - }
280 - }
241 + return $cache;
242 + }
281 243
282 - return $cache;
283 - }
244 + private function get_interest_categories( $list_id ) {
245 + $response = $this->mailchimp->get( "lists/{$list_id}/interest-categories" );
284 246
285 - private function get_interest_categories( $list_id ) {
286 - $response = $this->mailchimp->get( "lists/{$list_id}/interest-categories" );
247 + if ( $this->mailchimp->success() ) {
248 + if ( isset( $response['categories'] ) ) {
249 + $response = array_map(
250 + function ( $item ) {
251 + return array(
252 + 'id' => $item['id'],
253 + 'title' => $item['title'],
254 + );
255 + },
256 + $response['categories']
257 + );
258 + }
259 + } else {
260 + $error = $this->mailchimp->getLastError();
261 + wp_send_json_error( $error );
262 + }
287 263
288 - if ( $this->mailchimp->success() ) {
289 - if ( isset( $response[ 'categories' ] ) ) {
290 - $response = array_map( function ( $item ) {
291 - return array( 'id' => $item[ 'id' ], 'title' => $item[ 'title' ] );
292 - }, $response[ 'categories' ] );
293 - }
294 - } else {
295 - $error = $this->mailchimp->getLastError();
296 - wp_send_json_error( $error );
297 - }
264 + return $response;
265 + }
298 266
299 - return $response;
300 - }
267 + private function get_interests( $list_id, $category_id ) {
268 + $response = $this->mailchimp->get( "lists/{$list_id}/interest-categories/{$category_id}/interests" );
301 269
302 - private function get_interests( $list_id, $category_id ) {
303 - $response = $this->mailchimp->get( "lists/{$list_id}/interest-categories/{$category_id}/interests" );
270 + if ( $this->mailchimp->success() ) {
271 + if ( isset( $response['interests'] ) ) {
272 + $response = array_map(
273 + function ( $item ) {
274 + return array(
275 + 'id' => $item['id'],
276 + 'title' => $item['name'],
277 + );
278 + },
279 + $response['interests']
280 + );
281 + }
282 + } else {
283 + $error = $this->mailchimp->getLastError();
284 + wp_send_json_error( $error );
285 + }
304 286
305 - if ( $this->mailchimp->success() ) {
306 - if ( isset( $response[ 'interests' ] ) ) {
307 - $response = array_map( function ( $item ) {
308 - return array( 'id' => $item[ 'id' ], 'title' => $item[ 'name' ] );
309 - }, $response[ 'interests' ] );
310 - }
311 - } else {
312 - $error = $this->mailchimp->getLastError();
313 - wp_send_json_error( $error );
314 - }
287 + return $response;
288 + }
315 289
316 - return $response;
317 - }
290 + public function subscribe() {
318 291
319 - public function subscribe() {
292 + $email = ! empty( $_POST['data']['email'] ) ? sanitize_email( wp_unslash( $_POST['data']['email'] ) ) : '';
320 293
321 - $email = ! empty( $_POST[ 'data' ][ 'email' ] ) ? sanitize_email( wp_unslash( $_POST[ 'data' ][ 'email' ] ) ) : '';
294 + if ( empty( $email ) || ! is_email( $email ) ) {
295 + wp_send_json_error( __( 'Email is required.', 'getwid' ) );
296 + }
322 297
323 - if ( empty( $email ) || ! is_email( $email ) ) {
324 - wp_send_json_error( __('Email is required.', 'getwid') );
325 - }
298 + if ( empty( $_POST['data']['list_ids'] ) ) {
299 + wp_send_json_error( __( 'An invalid Mailchimp list was provided.', 'getwid' ) );
300 + }
326 301
327 - if ( empty( $_POST[ 'data' ][ 'list_ids' ] ) ) {
328 - wp_send_json_error( __('An invalid Mailchimp list was provided.', 'getwid') );
329 - }
302 + $interests_ids = json_decode( sanitize_text_field( wp_unslash( $_POST['data']['list_ids'] ) ), true );
330 303
331 - $interests_ids = json_decode( sanitize_text_field( wp_unslash( $_POST[ 'data' ][ 'list_ids' ] ) ), true );
304 + $merge_vars = array();
305 + $merge_vars['email_address'] = $email;
306 + $merge_vars['status'] = 'subscribed';
332 307
333 - $merge_vars = array();
334 - $merge_vars[ 'email_address' ] = $email;
335 - $merge_vars[ 'status' ] = 'subscribed';
308 + $merge_vars['merge_fields'] = array();
309 + if ( isset( $_POST['data']['first-name'] ) ) {
310 + $merge_vars['merge_fields']['FNAME'] = sanitize_text_field( wp_unslash( $_POST['data']['first-name'] ) );
311 + }
336 312
337 - $merge_vars[ 'merge_fields' ] = array();
338 - if ( isset( $_POST[ 'data' ][ 'first-name' ] ) ) {
339 - $merge_vars[ 'merge_fields' ][ 'FNAME' ] = sanitize_text_field( wp_unslash( $_POST[ 'data' ][ 'first-name' ] ) );
340 - }
313 + if ( isset( $_POST['data']['last-name'] ) ) {
314 + $merge_vars['merge_fields']['LNAME'] = sanitize_text_field( wp_unslash( $_POST['data']['last-name'] ) );
315 + }
341 316
342 - if ( isset( $_POST[ 'data' ][ 'last-name' ] ) ) {
343 - $merge_vars[ 'merge_fields' ][ 'LNAME' ] = sanitize_text_field( wp_unslash( $_POST[ 'data' ][ 'last-name' ] ) );
344 - }
317 + if ( empty( $merge_vars['merge_fields'] ) ) {
318 + unset( $merge_vars['merge_fields'] );
319 + }
345 320
346 - if ( empty( $merge_vars[ 'merge_fields' ] ) ) {
347 - unset( $merge_vars[ 'merge_fields' ] );
348 - }
321 + $merge_vars['interests'] = array();
322 + foreach ( $interests_ids as $list ) {
323 + $list = explode( '/', $list );
349 324
350 - $merge_vars[ 'interests' ] = array();
351 - foreach ( $interests_ids as $list ) {
352 - $list = explode( '/', $list );
325 + list( $first, $second ) = $list;
326 + if ( isset( $second ) ) {
327 + $merge_vars['interests'][ $second ] = true;
328 + }
329 + }
353 330
354 - list( $first, $second ) = $list;
355 - if ( isset( $second ) ) {
356 - $merge_vars[ 'interests' ][ $second ] = true;
357 - }
358 - }
331 + if ( empty( $merge_vars['interests'] ) ) {
332 + unset( $merge_vars['interests'] );
333 + }
359 334
360 - if ( empty( $merge_vars[ 'interests' ] ) ) {
361 - unset( $merge_vars[ 'interests' ] );
335 + if ( ! strpos( $interests_ids[0], '/' ) ) {
336 + list( $list_id ) = $interests_ids;
337 + } else {
338 + $interest = explode( '/', $interests_ids[0] );
339 + list( $list_id ) = $interest;
362 340 }
363 341
364 - if ( ! strpos( $interests_ids[ 0 ], '/' ) ) {
365 - list( $list_id, ) = $interests_ids;
366 - } else {
367 - $interest = explode( '/', $interests_ids[ 0 ] );
368 - list( $list_id, ) = $interest;
369 - }
342 + $api_key = get_option( 'getwid_mailchimp_api_key' );
370 343
371 - $api_key = get_option( 'getwid_mailchimp_api_key' );
344 + try {
345 + $this->mailchimp = new \DrewM\MailChimp\MailChimp( $api_key );
346 + } catch ( \Exception $exception ) {
347 + wp_send_json_error( $exception->getMessage() );
348 + }
372 349
373 - try {
374 - $this->mailchimp = new MC( $api_key );
375 - } catch ( \Exception $exception ) {
376 - wp_send_json_error( $exception->getMessage() );
377 - }
350 + $subscriber_hash = \DrewM\MailChimp\MailChimp::subscriberHash( $email );
351 + $response = $this->mailchimp->put( "lists/$list_id/members/$subscriber_hash", $merge_vars );
378 352
379 - $subscriber_hash = MC::subscriberHash( $email );
380 - $response = $this->mailchimp->put( "lists/$list_id/members/$subscriber_hash", $merge_vars );
381 -
382 - if ( $this->mailchimp->success() ) {
383 - wp_send_json_success(
384 - __( 'Thank you for joining our mailing list.',
385 - 'getwid'
386 - ) );
387 - } else {
388 - $error = $this->mailchimp->getLastError();
389 - wp_send_json_error( $error );
390 - }
391 - }
353 + if ( $this->mailchimp->success() ) {
354 + wp_send_json_success(
355 + __( 'Thank you for joining our mailing list.', 'getwid' )
356 + );
357 + } else {
358 + $error = $this->mailchimp->getLastError();
359 + wp_send_json_error( $error );
360 + }
361 + }
392 362 }
393 363
394 364 getwid()->blocksManager()->addBlock(
395 - new \Getwid\Blocks\MailChimp()
365 + new Mailchimp()
396 366 );