| 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 |
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 |
} |
| 202 |
|
| 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 |
} |
| 215 |
|
| 216 |
if ( ! isset( $api_key ) ) { |
| 217 |
$api_key = get_option( 'getwid_mailchimp_api_key', '' ); |
| 218 |
} |
| 219 |
|
| 220 |
if ( ! empty( $api_key ) ) { |
| 221 |
|
| 222 |
try { |
| 223 |
$this->mailchimp = new MC( $api_key ); |
| 224 |
} catch ( \Exception $exception ) { |
| 225 |
wp_send_json_error( $exception->getMessage() ); |
| 226 |
} |
| 227 |
|
| 228 |
$maybe_cached = $this->get_account_subscribe_lists( $option === 'sync' ); |
| 229 |
|
| 230 |
wp_send_json_success( $maybe_cached ); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
public function get_lists() { |
| 235 |
|
| 236 |
$response = $this->mailchimp->get( 'lists' ); |
| 237 |
|
| 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 |
} |
| 248 |
|
| 249 |
return $response; |
| 250 |
} |
| 251 |
|
| 252 |
public function get_account_subscribe_lists( $sync = false ) { |
| 253 |
|
| 254 |
if ( ! $sync ) { |
| 255 |
$cache = get_option( 'audiences_list_chash' ); |
| 256 |
} |
| 257 |
|
| 258 |
if ( $sync || empty( $cache ) ) { |
| 259 |
$cache = array(); |
| 260 |
|
| 261 |
$list = $this->get_lists(); |
| 262 |
|
| 263 |
if ( count( $list ) > 0 ) { |
| 264 |
$cache = $list; |
| 265 |
|
| 266 |
foreach ( $list as $key => $list_item ) { |
| 267 |
$categories = $this->get_interest_categories( $list_item[ 'id' ] ); |
| 268 |
|
| 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 |
} |
| 276 |
|
| 277 |
if ( ! empty( $cache ) ) { |
| 278 |
update_option( 'audiences_list_chash', $cache ); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
return $cache; |
| 283 |
} |
| 284 |
|
| 285 |
private function get_interest_categories( $list_id ) { |
| 286 |
$response = $this->mailchimp->get( "lists/{$list_id}/interest-categories" ); |
| 287 |
|
| 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 |
} |
| 298 |
|
| 299 |
return $response; |
| 300 |
} |
| 301 |
|
| 302 |
private function get_interests( $list_id, $category_id ) { |
| 303 |
$response = $this->mailchimp->get( "lists/{$list_id}/interest-categories/{$category_id}/interests" ); |
| 304 |
|
| 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 |
} |
| 315 |
|
| 316 |
return $response; |
| 317 |
} |
| 318 |
|
| 319 |
public function subscribe() { |
| 320 |
|
| 321 |
$email = ! empty( $_POST[ 'data' ][ 'email' ] ) ? sanitize_email( wp_unslash( $_POST[ 'data' ][ 'email' ] ) ) : ''; |
| 322 |
|
| 323 |
if ( empty( $email ) || ! is_email( $email ) ) { |
| 324 |
wp_send_json_error( __('Email is required.', 'getwid') ); |
| 325 |
} |
| 326 |
|
| 327 |
if ( empty( $_POST[ 'data' ][ 'list_ids' ] ) ) { |
| 328 |
wp_send_json_error( __('An invalid Mailchimp list was provided.', 'getwid') ); |
| 329 |
} |
| 330 |
|
| 331 |
$interests_ids = json_decode( sanitize_text_field( wp_unslash( $_POST[ 'data' ][ 'list_ids' ] ) ), true ); |
| 332 |
|
| 333 |
$merge_vars = array(); |
| 334 |
$merge_vars[ 'email_address' ] = $email; |
| 335 |
$merge_vars[ 'status' ] = 'subscribed'; |
| 336 |
|
| 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 |
} |
| 341 |
|
| 342 |
if ( isset( $_POST[ 'data' ][ 'last-name' ] ) ) { |
| 343 |
$merge_vars[ 'merge_fields' ][ 'LNAME' ] = sanitize_text_field( wp_unslash( $_POST[ 'data' ][ 'last-name' ] ) ); |
| 344 |
} |
| 345 |
|
| 346 |
if ( empty( $merge_vars[ 'merge_fields' ] ) ) { |
| 347 |
unset( $merge_vars[ 'merge_fields' ] ); |
| 348 |
} |
| 349 |
|
| 350 |
$merge_vars[ 'interests' ] = array(); |
| 351 |
foreach ( $interests_ids as $list ) { |
| 352 |
$list = explode( '/', $list ); |
| 353 |
|
| 354 |
list( $first, $second ) = $list; |
| 355 |
if ( isset( $second ) ) { |
| 356 |
$merge_vars[ 'interests' ][ $second ] = true; |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
if ( empty( $merge_vars[ 'interests' ] ) ) { |
| 361 |
unset( $merge_vars[ 'interests' ] ); |
| 362 |
} |
| 363 |
|
| 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 |
} |
| 370 |
|
| 371 |
$api_key = get_option( 'getwid_mailchimp_api_key' ); |
| 372 |
|
| 373 |
try { |
| 374 |
$this->mailchimp = new MC( $api_key ); |
| 375 |
} catch ( \Exception $exception ) { |
| 376 |
wp_send_json_error( $exception->getMessage() ); |
| 377 |
} |
| 378 |
|
| 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 |
} |
| 392 |
} |
| 393 |
|
| 394 |
getwid()->blocksManager()->addBlock( |
| 395 |
new \Getwid\Blocks\MailChimp() |
| 396 |
); |
| 397 |
|