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
getwid / includes / blocks / mailchimp.php

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

367 lines 9.4 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 class Mailchimp extends AbstractBlock {
6
7 private $mailchimp;
8
9 public function __construct() {
10
11 parent::__construct( 'getwid/mailchimp' );
12
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' ) );
16
17 $this->register_mailchimp_blocks();
18 }
19
20 public function get_label() {
21 return __( 'Mailchimp', 'getwid' );
22 }
23
24 private function register_mailchimp_blocks() {
25
26 register_block_type(
27 getwid_get_plugin_path( 'assets/blocks/mailchimp/mailchimp' ),
28 array(
29 'render_callback' => array( $this, 'render_callback' ),
30 )
31 );
32
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 );
39
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 );
46
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 }
54
55 public function block_frontend_assets() {
56
57 if ( is_admin() ) {
58 return;
59 }
60
61 $inline_script = 'var Getwid = Getwid || {};';
62 $inline_script .= 'Getwid["ajax_url"] = ' . wp_json_encode( admin_url( 'admin-ajax.php' ) ) . ';';
63
64 wp_add_inline_script(
65 'getwid-mailchimp-view-script',
66 $inline_script,
67 'before'
68 );
69 }
70
71 public function render_callback( $attributes, $content ) {
72
73 $class = 'wp-block-getwid-mailchimp';
74 $block_name = 'wp-block-getwid-mailchimp';
75
76 if ( isset( $attributes['className'] ) ) {
77 $class .= ' ' . $attributes['className'];
78 }
79
80 if ( isset( $attributes['align'] ) ) {
81 $class .= ' align' . $attributes['align'];
82 }
83
84 $button_style = '';
85 $button_class = '';
86
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' );
89
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 );
97
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
104
105 $result = ob_get_clean();
106
107 $this->block_frontend_assets();
108
109 return $result;
110 }
111
112 public function render_mailchimp_field_email( $attributes ) {
113 ob_start();
114 getwid_get_template_part( 'mailchimp/field-email', $attributes, false );
115
116 return ob_get_clean();
117 }
118
119 public function render_mailchimp_field_first_name( $attributes ) {
120 $extra_attr = array( 'name' => 'first_name' );
121
122 if ( ! isset( $attributes['label'] ) ) {
123 $attributes['label'] = __( 'First name', 'getwid' );
124 }
125
126 ob_start();
127 getwid_get_template_part( 'mailchimp/field-first-name', $attributes, false, $extra_attr );
128
129 return ob_get_clean();
130 }
131
132 public function render_mailchimp_field_last_name( $attributes ) {
133 $extra_attr = array( 'name' => 'last_name' );
134
135 if ( ! isset( $attributes['label'] ) ) {
136 $attributes['label'] = __( 'Last name', 'getwid' );
137 }
138
139 ob_start();
140 getwid_get_template_part( 'mailchimp/field-last-name', $attributes, false, $extra_attr );
141
142 return ob_get_clean();
143 }
144
145 public function mailchimp_api_key_manage() {
146 $nonce = sanitize_key( $_POST['nonce'] );
147
148 if ( ! wp_verify_nonce( $nonce, 'getwid_nonce_mailchimp_api_key' ) ) {
149 wp_send_json_error();
150 }
151
152 $option = sanitize_text_field( wp_unslash( $_POST['option'] ) );
153
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 }
157
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 }
169
170 if ( ! isset( $api_key ) ) {
171 $api_key = get_option( 'getwid_mailchimp_api_key', '' );
172 }
173
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 }
180
181 $maybe_cached = $this->get_account_subscribe_lists( 'sync' === $option );
182
183 wp_send_json_success( $maybe_cached );
184 }
185 }
186
187 public function get_lists() {
188
189 $response = $this->mailchimp->get( 'lists' );
190
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 }
207
208 return $response;
209 }
210
211 public function get_account_subscribe_lists( $sync = false ) {
212
213 if ( ! $sync ) {
214 $cache = get_option( 'audiences_list_chash' );
215 }
216
217 if ( $sync || empty( $cache ) ) {
218 $cache = array();
219
220 $list = $this->get_lists();
221
222 if ( count( $list ) > 0 ) {
223 $cache = $list;
224
225 foreach ( $list as $key => $list_item ) {
226 $categories = $this->get_interest_categories( $list_item['id'] );
227
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 }
235
236 if ( ! empty( $cache ) ) {
237 update_option( 'audiences_list_chash', $cache );
238 }
239 }
240
241 return $cache;
242 }
243
244 private function get_interest_categories( $list_id ) {
245 $response = $this->mailchimp->get( "lists/{$list_id}/interest-categories" );
246
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 }
263
264 return $response;
265 }
266
267 private function get_interests( $list_id, $category_id ) {
268 $response = $this->mailchimp->get( "lists/{$list_id}/interest-categories/{$category_id}/interests" );
269
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 }
286
287 return $response;
288 }
289
290 public function subscribe() {
291
292 $email = ! empty( $_POST['data']['email'] ) ? sanitize_email( wp_unslash( $_POST['data']['email'] ) ) : '';
293
294 if ( empty( $email ) || ! is_email( $email ) ) {
295 wp_send_json_error( __( 'Email is required.', 'getwid' ) );
296 }
297
298 if ( empty( $_POST['data']['list_ids'] ) ) {
299 wp_send_json_error( __( 'An invalid Mailchimp list was provided.', 'getwid' ) );
300 }
301
302 $interests_ids = json_decode( sanitize_text_field( wp_unslash( $_POST['data']['list_ids'] ) ), true );
303
304 $merge_vars = array();
305 $merge_vars['email_address'] = $email;
306 $merge_vars['status'] = 'subscribed';
307
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 }
312
313 if ( isset( $_POST['data']['last-name'] ) ) {
314 $merge_vars['merge_fields']['LNAME'] = sanitize_text_field( wp_unslash( $_POST['data']['last-name'] ) );
315 }
316
317 if ( empty( $merge_vars['merge_fields'] ) ) {
318 unset( $merge_vars['merge_fields'] );
319 }
320
321 $merge_vars['interests'] = array();
322 foreach ( $interests_ids as $list ) {
323 $list = explode( '/', $list );
324
325 list( $first, $second ) = $list;
326 if ( isset( $second ) ) {
327 $merge_vars['interests'][ $second ] = true;
328 }
329 }
330
331 if ( empty( $merge_vars['interests'] ) ) {
332 unset( $merge_vars['interests'] );
333 }
334
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;
340 }
341
342 $api_key = get_option( 'getwid_mailchimp_api_key' );
343
344 try {
345 $this->mailchimp = new \DrewM\MailChimp\MailChimp( $api_key );
346 } catch ( \Exception $exception ) {
347 wp_send_json_error( $exception->getMessage() );
348 }
349
350 $subscriber_hash = \DrewM\MailChimp\MailChimp::subscriberHash( $email );
351 $response = $this->mailchimp->put( "lists/$list_id/members/$subscriber_hash", $merge_vars );
352
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 }
362 }
363
364 getwid()->blocksManager()->addBlock(
365 new Mailchimp()
366 );
367