| @@ -1,390 +1,366 @@ | ||
| 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 | -); | |
| 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 | +); | |