PluginProbe
Getwid – Gutenberg Blocks / 2.0.10
Getwid – Gutenberg Blocks v2.0.10
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
getwid / includes / blocks / mailchimp.php

mailchimp.php in Getwid – Gutenberg Blocks 2.0.10, at includes/blocks/mailchimp.php

391 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Getwid\Blocks;
4
5 use DrewM\MailChimp\MailChimp as MC;
6
7 class MailChimp extends \Getwid\Blocks\AbstractBlock {
8
9 private $mailchimp;
10
11 protected static $blockName = 'getwid/mailchimp';
12
13 public function __construct() {
14
15 parent::__construct( self::$blockName );
16
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');
27 }
28
29 private function register_mailchimp_blocks() {
30
31 /* #region register all blocks */
32 register_block_type(
33 'getwid/mailchimp',
34 array(
35 'render_callback' => [ $this, 'render_callback' ]
36 )
37 );
38
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 );
46
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 );
54
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 }
64
65 public function block_frontend_assets() {
66
67 if ( is_admin() ) {
68 return;
69 }
70
71 if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) {
72 return;
73 }
74
75 $rtl = is_rtl() ? '.rtl' : '';
76
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 wp_add_inline_script(
101 self::$blockName,
102 $inline_script,
103 'before'
104 );
105
106 }
107
108 public function render_callback( $attributes, $content ) {
109
110 $class = 'wp-block-getwid-mailchimp';
111 $block_name = $class;
112
113 if ( isset( $attributes[ 'className' ] ) ) {
114 $class .= ' ' . $attributes[ 'className' ];
115 }
116
117 if ( isset( $attributes[ 'align' ] ) ) {
118 $class .= ' align' . $attributes[ 'align' ];
119 }
120
121 $button_style = $button_class = '';
122
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' );
125
126 $extra_attr = array(
127 'class' => $class,
128 'block_name' => $block_name,
129 'content' => $content,
130
131 'button_style' => $button_style,
132 'button_class' => $button_class
133 );
134
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 $this->block_frontend_assets();
148
149 return $chash;
150 }
151
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
156
157 $chash = ob_get_clean();
158 return $chash;
159 }
160
161 public function render_mailchimp_field_first_name( $attributes ) {
162 $extra_attr = array( 'name' => 'first_name' );
163
164 if ( ! isset( $attributes[ 'label' ] ) ) {
165 $attributes[ 'label' ] = __( 'First name', 'getwid' );
166 }
167
168 ob_start();?>
169 <?php getwid_get_template_part( 'mailchimp/field-first-name', $attributes, false, $extra_attr ); ?><?php
170
171 $chash = ob_get_clean();
172 return $chash;
173 }
174
175 public function render_mailchimp_field_last_name( $attributes ) {
176 $extra_attr = array( 'name' => 'last_name' );
177
178 if ( ! isset( $attributes[ 'label' ] ) ) {
179 $attributes[ 'label' ] = __( 'Last name', 'getwid' );
180 }
181
182 ob_start();?>
183 <?php getwid_get_template_part( 'mailchimp/field-last-name', $attributes, false, $extra_attr ); ?><?php
184
185 $chash = ob_get_clean();
186 return $chash;
187 }
188 /* #endregion */
189
190 public function mailchimp_api_key_manage() {
191 $nonce = sanitize_key( $_POST[ 'nonce' ] );
192
193 if ( ! wp_verify_nonce( $nonce, 'getwid_nonce_mailchimp_api_key' ) ) {
194 wp_send_json_error();
195 }
196
197 $option = sanitize_text_field( wp_unslash( $_POST[ 'option' ] ) );
198
199 $api_key = sanitize_text_field( wp_unslash( $_POST[ 'data' ][ 'api_key' ] ) );
200
201 if ( $option == 'save' || $option == 'sync' ) {
202 if ( ! empty( $api_key ) ) {
203 update_option( 'getwid_mailchimp_api_key', $api_key );
204
205 try {
206 $this->mailchimp = new MC( $api_key );
207 } catch ( \Exception $exception ) {
208 wp_send_json_error( $exception->getMessage() );
209 }
210
211 $sync = false;
212 if ( $option == 'sync' ) {
213 $sync = true;
214
215 $this->get_lists();
216 }
217
218 $chash = $this->get_account_subscribe_lists( $sync );
219
220 wp_send_json_success( $chash );
221 }
222 } elseif ( $option == 'delete' ) {
223 delete_option( 'getwid_mailchimp_api_key' );
224 delete_option( 'audiences_list_chash' );
225 }
226 }
227
228 public function get_lists() {
229
230 $response = $this->mailchimp->get( 'lists' );
231
232 if ( $this->mailchimp->success() ) {
233 if ( isset( $response[ 'lists' ] ) ) {
234 $response = array_map( function ( $item ) {
235 return array( 'id' => $item[ 'id' ], 'title' => $item[ 'name' ] );
236 }, $response[ 'lists' ] );
237 }
238 } else {
239 $error = $this->mailchimp->getLastError();
240 wp_send_json_error( $error );
241 }
242
243 return $response;
244 }
245
246 public function get_account_subscribe_lists( $sync = false ) {
247
248 if ( ! $sync ) {
249 $cache = get_option( 'audiences_list_chash' );
250 }
251
252 if ( $sync || empty( $cache ) ) {
253 $cache = array();
254
255 $list = $this->get_lists();
256
257 if ( count( $list ) > 0 ) {
258 $cache = $list;
259
260 foreach ( $list as $key => $list_item ) {
261 $categories = $this->get_interest_categories( $list_item[ 'id' ] );
262
263 $cache[ $key ][ 'categories' ] = $categories;
264 foreach ( $cache[ $key ][ 'categories' ] as $k => $category_item ) {
265 $interests = $this->get_interests( $list_item[ 'id' ], $category_item[ 'id' ] );
266 $cache[ $key ][ 'categories' ][ $k ][ 'interests' ] = $interests;
267 }
268 }
269 }
270
271 if ( ! empty( $cache ) ) {
272 update_option( 'audiences_list_chash', $cache );
273 }
274 }
275
276 return $cache;
277 }
278
279 private function get_interest_categories( $list_id ) {
280 $response = $this->mailchimp->get( "lists/{$list_id}/interest-categories" );
281
282 if ( $this->mailchimp->success() ) {
283 if ( isset( $response[ 'categories' ] ) ) {
284 $response = array_map( function ( $item ) {
285 return array( 'id' => $item[ 'id' ], 'title' => $item[ 'title' ] );
286 }, $response[ 'categories' ] );
287 }
288 } else {
289 $error = $this->mailchimp->getLastError();
290 wp_send_json_error( $error );
291 }
292
293 return $response;
294 }
295
296 private function get_interests( $list_id, $category_id ) {
297 $response = $this->mailchimp->get( "lists/{$list_id}/interest-categories/{$category_id}/interests" );
298
299 if ( $this->mailchimp->success() ) {
300 if ( isset( $response[ 'interests' ] ) ) {
301 $response = array_map( function ( $item ) {
302 return array( 'id' => $item[ 'id' ], 'title' => $item[ 'name' ] );
303 }, $response[ 'interests' ] );
304 }
305 } else {
306 $error = $this->mailchimp->getLastError();
307 wp_send_json_error( $error );
308 }
309
310 return $response;
311 }
312
313 public function subscribe() {
314
315 $email = ! empty( $_POST[ 'data' ][ 'email' ] ) ? sanitize_email( wp_unslash( $_POST[ 'data' ][ 'email' ] ) ) : '';
316
317 if ( empty( $email ) || ! is_email( $email ) ) {
318 wp_send_json_error( __('Email is required.', 'getwid') );
319 }
320
321 if ( empty( $_POST[ 'data' ][ 'list_ids' ] ) ) {
322 wp_send_json_error( __('An invalid Mailchimp list was provided.', 'getwid') );
323 }
324
325 $interests_ids = json_decode( sanitize_text_field( wp_unslash( $_POST[ 'data' ][ 'list_ids' ] ) ), true );
326
327 $merge_vars = array();
328 $merge_vars[ 'email_address' ] = $email;
329 $merge_vars[ 'status' ] = 'subscribed';
330
331 $merge_vars[ 'merge_fields' ] = array();
332 if ( isset( $_POST[ 'data' ][ 'first-name' ] ) ) {
333 $merge_vars[ 'merge_fields' ][ 'FNAME' ] = sanitize_text_field( wp_unslash( $_POST[ 'data' ][ 'first-name' ] ) );
334 }
335
336 if ( isset( $_POST[ 'data' ][ 'last-name' ] ) ) {
337 $merge_vars[ 'merge_fields' ][ 'LNAME' ] = sanitize_text_field( wp_unslash( $_POST[ 'data' ][ 'last-name' ] ) );
338 }
339
340 if ( empty( $merge_vars[ 'merge_fields' ] ) ) {
341 unset( $merge_vars[ 'merge_fields' ] );
342 }
343
344 $merge_vars[ 'interests' ] = array();
345 foreach ( $interests_ids as $list ) {
346 $list = explode( '/', $list );
347
348 list( $first, $second ) = $list;
349 if ( isset( $second ) ) {
350 $merge_vars[ 'interests' ][ $second ] = true;
351 }
352 }
353
354 if ( empty( $merge_vars[ 'interests' ] ) ) {
355 unset( $merge_vars[ 'interests' ] );
356 }
357
358 if ( ! strpos( $interests_ids[ 0 ], '/' ) ) {
359 list( $list_id, ) = $interests_ids;
360 } else {
361 $interest = explode( '/', $interests_ids[ 0 ] );
362 list( $list_id, ) = $interest;
363 }
364
365 $api_key = get_option( 'getwid_mailchimp_api_key' );
366
367 try {
368 $this->mailchimp = new MC( $api_key );
369 } catch ( \Exception $exception ) {
370 wp_send_json_error( $exception->getMessage() );
371 }
372
373 $subscriber_hash = MC::subscriberHash( $email );
374 $response = $this->mailchimp->put( "lists/$list_id/members/$subscriber_hash", $merge_vars );
375
376 if ( $this->mailchimp->success() ) {
377 wp_send_json_success(
378 __( 'Thank you for joining our mailing list.',
379 'getwid'
380 ) );
381 } else {
382 $error = $this->mailchimp->getLastError();
383 wp_send_json_error( $error );
384 }
385 }
386 }
387
388 getwid()->blocksManager()->addBlock(
389 new \Getwid\Blocks\MailChimp()
390 );
391