| 1 |
<?php |
| 2 |
/** |
| 3 |
* Disco Plugin - ActiveCampaign Email Collector |
| 4 |
* |
| 5 |
* The React app submits the email straight to the ActiveCampaign v1 API |
| 6 |
* from the browser (form-encoded POST, no preflight). This file only |
| 7 |
* handles the local side: an AJAX endpoint that stores a notice-style |
| 8 |
* flag in the options table once the email has been collected, and the |
| 9 |
* localized data that tells the frontend whether to show the opt-in field. |
| 10 |
* The email address itself is never stored locally. |
| 11 |
* |
| 12 |
* @since 1.3.49 |
| 13 |
* @version 1.2.0 |
| 14 |
*/ |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Option flag set once the user has submitted their email. |
| 22 |
* Works like a notice-dismiss flag: only `true` is stored, never the email. |
| 23 |
*/ |
| 24 |
define( 'DISCO_AC_OPTION_KEY', 'disco_active_campaign_subscribe' ); |
| 25 |
|
| 26 |
/** |
| 27 |
* AJAX action name. |
| 28 |
*/ |
| 29 |
define( 'DISCO_AC_AJAX_ACTION', 'disco_activecampaign_subscribed' ); |
| 30 |
|
| 31 |
/** |
| 32 |
* Nonce action name. |
| 33 |
*/ |
| 34 |
define( 'DISCO_AC_NONCE_ACTION', 'disco_activecampaign_nonce' ); |
| 35 |
|
| 36 |
/** |
| 37 |
* Whether an email has already been collected. |
| 38 |
* |
| 39 |
* @return bool |
| 40 |
*/ |
| 41 |
function disco_activecampaign_has_subscriber() { |
| 42 |
return (bool) get_option( DISCO_AC_OPTION_KEY, false ); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Data localized for the React app. |
| 47 |
* |
| 48 |
* `show_email_field` is true until an email has been collected, so the |
| 49 |
* frontend can show the opt-in field once and hide it afterwards. |
| 50 |
* |
| 51 |
* @return array |
| 52 |
*/ |
| 53 |
function disco_activecampaign_get_localize_data() { |
| 54 |
return array( |
| 55 |
'ajax_url' => admin_url( 'admin-ajax.php' ), |
| 56 |
'action' => DISCO_AC_AJAX_ACTION, |
| 57 |
'nonce' => wp_create_nonce( DISCO_AC_NONCE_ACTION ), |
| 58 |
'show_email_field' => ! disco_activecampaign_has_subscriber(), |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* AJAX handler: mark the email as collected. |
| 64 |
* |
| 65 |
* Called by the React app after it has submitted the email to |
| 66 |
* ActiveCampaign. Stores the flag so the opt-in field stays hidden. |
| 67 |
* |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
function disco_activecampaign_handle_subscribed() { |
| 71 |
check_ajax_referer( DISCO_AC_NONCE_ACTION, 'nonce' ); |
| 72 |
|
| 73 |
if ( ! current_user_can( 'manage_options' ) && ! current_user_can( 'manage_woocommerce' ) ) { |
| 74 |
wp_send_json_error( array( 'message' => __( 'You are not allowed to do this.', 'disco' ) ), 403 ); |
| 75 |
} |
| 76 |
|
| 77 |
update_option( DISCO_AC_OPTION_KEY, true, false ); |
| 78 |
|
| 79 |
wp_send_json_success( |
| 80 |
array( |
| 81 |
'message' => __( 'Thanks! You are subscribed.', 'disco' ), |
| 82 |
) |
| 83 |
); |
| 84 |
} |
| 85 |
|
| 86 |
add_action( 'wp_ajax_' . DISCO_AC_AJAX_ACTION, 'disco_activecampaign_handle_subscribed' ); |
| 87 |
|