| @@ -45,11 +45,22 @@ | ||
| 45 | 45 | } |
| 46 | 46 | return self::$instance; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | + /** | |
| 50 | + * Newsletter list endpoint the welcome step's opt-in posts to. | |
| 51 | + */ | |
| 52 | + const SUBSCRIBE_ENDPOINT = 'https://marketing.sigmative.com/newsletter/rui/lists/6a9943aacbe70/embedded-form-subscribe'; | |
| 53 | + | |
| 54 | + /** | |
| 55 | + * Customer identifier required by the newsletter endpoint. | |
| 56 | + */ | |
| 57 | + const SUBSCRIBE_CUSTOMER_UID = '6a93d39ce0ebd'; | |
| 58 | + | |
| 49 | 59 | // Initialize hooks |
| 50 | 60 | private function init_hooks() { |
| 51 | 61 | add_action( 'wp_ajax_ultimate_post_kit_setup_wizard_install_plugins', array( $this, 'install_plugins' ) ); |
| 62 | + add_action( 'wp_ajax_ultimate_post_kit_setup_wizard_subscribe', array( $this, 'ajax_subscribe' ) ); | |
| 52 | 63 | add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 53 | 64 | add_action( 'admin_init', array( $this, 'activate_default_widgets' ) ); |
| 54 | 65 | add_action( 'admin_init', array( $this, 'maybe_display_setup_wizard' ) ); |
| 55 | 66 | add_action( 'admin_init', array( $this, 'check_manual_wizard_request' ) ); |
| @@ -249,9 +260,9 @@ | ||
| 249 | 260 | wp_enqueue_style( 'upk-setup-wizard' ); |
| 250 | 261 | |
| 251 | 262 | wp_localize_script( |
| 252 | 263 | 'upk-setup-wizard', |
| 253 | - 'BDT_SetupWizard', | |
| 264 | + 'UPK_SetupWizard', | |
| 254 | 265 | array( |
| 255 | 266 | 'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 256 | 267 | 'nonce' => wp_create_nonce( 'ultimate_post_kit_setup_wizard_nonce' ), |
| 257 | 268 | 'is_fullscreen' => true |
| @@ -266,8 +277,105 @@ | ||
| 266 | 277 | return $core_widgets; |
| 267 | 278 | } |
| 268 | 279 | ); |
| 269 | 280 | return $arr_obj; |
| 281 | + } | |
| 282 | + | |
| 283 | + /** | |
| 284 | + * Handle the newsletter opt-in on the welcome step. | |
| 285 | + * | |
| 286 | + * Opt-in only: nothing is sent unless the administrator ticked the box, | |
| 287 | + * which is unticked by default. The choice is recorded either way so the | |
| 288 | + * wizard can show it again on a re-run. Runs server side so the | |
| 289 | + * cross-origin POST is not subject to CORS. | |
| 290 | + */ | |
| 291 | + public function ajax_subscribe() { | |
| 292 | + check_ajax_referer( 'ultimate_post_kit_setup_wizard_nonce', 'nonce' ); | |
| 293 | + | |
| 294 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 295 | + wp_send_json_error( array( 'message' => esc_html__( 'Unauthorized', 'ultimate-post-kit' ) ) ); | |
| 296 | + } | |
| 297 | + | |
| 298 | + // Record the choice first, whichever way it went. | |
| 299 | + $consent = isset( $_POST['consent'] ) && 'yes' === sanitize_text_field( wp_unslash( $_POST['consent'] ) ); | |
| 300 | + | |
| 301 | + update_option( 'bdtupk_subscribe_optin', $consent ? 'yes' : 'no' ); | |
| 302 | + | |
| 303 | + if ( ! $consent ) { | |
| 304 | + // No opt-in: the choice is stored and nothing leaves the site. | |
| 305 | + wp_send_json_success( | |
| 306 | + array( | |
| 307 | + 'subscribed' => false, | |
| 308 | + 'message' => esc_html__( 'Preferences saved.', 'ultimate-post-kit' ), | |
| 309 | + ) | |
| 310 | + ); | |
| 311 | + } | |
| 312 | + | |
| 313 | + // Keep the raw value: sanitize_email() flattens anything malformed to an | |
| 314 | + // empty string, which would otherwise be indistinguishable from "left blank". | |
| 315 | + $raw_email = isset( $_POST['email'] ) ? sanitize_text_field( wp_unslash( $_POST['email'] ) ) : ''; | |
| 316 | + $email = sanitize_email( $raw_email ); | |
| 317 | + | |
| 318 | + if ( '' === trim( $raw_email ) ) { | |
| 319 | + wp_send_json_success( | |
| 320 | + array( | |
| 321 | + 'subscribed' => false, | |
| 322 | + 'message' => esc_html__( 'Preferences saved.', 'ultimate-post-kit' ), | |
| 323 | + ) | |
| 324 | + ); | |
| 325 | + } | |
| 326 | + | |
| 327 | + if ( ! is_email( $email ) ) { | |
| 328 | + wp_send_json_error( array( 'message' => esc_html__( 'Please enter a valid email address.', 'ultimate-post-kit' ) ) ); | |
| 329 | + } | |
| 330 | + | |
| 331 | + // Never subscribe the same address twice from this site. | |
| 332 | + if ( get_option( 'bdtupk_subscribed_email' ) === $email ) { | |
| 333 | + wp_send_json_success( | |
| 334 | + array( | |
| 335 | + 'subscribed' => true, | |
| 336 | + 'message' => esc_html__( 'You are already subscribed.', 'ultimate-post-kit' ), | |
| 337 | + ) | |
| 338 | + ); | |
| 339 | + } | |
| 340 | + | |
| 341 | + $current_user = wp_get_current_user(); | |
| 342 | + | |
| 343 | + $body = array( | |
| 344 | + 'customer_uid' => apply_filters( 'bdtupk/setup_wizard/subscribe_customer_uid', self::SUBSCRIBE_CUSTOMER_UID ), | |
| 345 | + 'EMAIL' => $email, | |
| 346 | + 'FIRST_NAME' => $current_user ? $current_user->first_name : '', | |
| 347 | + 'LAST_NAME' => $current_user ? $current_user->last_name : '', | |
| 348 | + ); | |
| 349 | + | |
| 350 | + $response = wp_safe_remote_post( | |
| 351 | + apply_filters( 'bdtupk/setup_wizard/subscribe_url', self::SUBSCRIBE_ENDPOINT ), | |
| 352 | + array( | |
| 353 | + 'timeout' => 15, | |
| 354 | + 'body' => apply_filters( 'bdtupk/setup_wizard/subscribe_body', $body, $email ), | |
| 355 | + 'headers' => array( 'Accept' => '*/*' ), | |
| 356 | + 'sslverify' => true, | |
| 357 | + ) | |
| 358 | + ); | |
| 359 | + | |
| 360 | + if ( is_wp_error( $response ) ) { | |
| 361 | + wp_send_json_error( array( 'message' => esc_html__( 'Could not reach the subscription service. Please try again later.', 'ultimate-post-kit' ) ) ); | |
| 362 | + } | |
| 363 | + | |
| 364 | + $code = wp_remote_retrieve_response_code( $response ); | |
| 365 | + | |
| 366 | + if ( $code < 200 || $code >= 400 ) { | |
| 367 | + wp_send_json_error( array( 'message' => esc_html__( 'The subscription service rejected the request.', 'ultimate-post-kit' ) ) ); | |
| 368 | + } | |
| 369 | + | |
| 370 | + update_option( 'bdtupk_subscribed_email', $email ); | |
| 371 | + | |
| 372 | + wp_send_json_success( | |
| 373 | + array( | |
| 374 | + 'subscribed' => true, | |
| 375 | + 'message' => esc_html__( 'Thanks for subscribing!', 'ultimate-post-kit' ), | |
| 376 | + ) | |
| 377 | + ); | |
| 270 | 378 | } |
| 271 | 379 | |
| 272 | 380 | // Install plugins |
| 273 | 381 | public function install_plugins() { |