| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit AJAX class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Registers AJAX actions for the Plugin. |
| 11 |
* |
| 12 |
* @since 1.9.6 |
| 13 |
*/ |
| 14 |
class ConvertKit_AJAX { |
| 15 |
|
| 16 |
/** |
| 17 |
* Constructor. |
| 18 |
* |
| 19 |
* @since 1.9.6 |
| 20 |
*/ |
| 21 |
public function __construct() { |
| 22 |
|
| 23 |
add_action( 'wp_ajax_convertkit_get_blocks', array( $this, 'get_blocks' ) ); |
| 24 |
|
| 25 |
add_action( 'wp_ajax_nopriv_convertkit_store_subscriber_id_in_cookie', array( $this, 'store_subscriber_id_in_cookie' ) ); |
| 26 |
add_action( 'wp_ajax_convertkit_store_subscriber_id_in_cookie', array( $this, 'store_subscriber_id_in_cookie' ) ); |
| 27 |
|
| 28 |
add_action( 'wp_ajax_nopriv_convertkit_store_subscriber_email_as_id_in_cookie', array( $this, 'store_subscriber_email_as_id_in_cookie' ) ); |
| 29 |
add_action( 'wp_ajax_convertkit_store_subscriber_email_as_id_in_cookie', array( $this, 'store_subscriber_email_as_id_in_cookie' ) ); |
| 30 |
|
| 31 |
add_action( 'wp_ajax_nopriv_convertkit_subscriber_authentication_send_code', array( $this, 'subscriber_authentication_send_code' ) ); |
| 32 |
add_action( 'wp_ajax_convertkit_subscriber_authentication_send_code', array( $this, 'subscriber_authentication_send_code' ) ); |
| 33 |
|
| 34 |
add_action( 'wp_ajax_nopriv_convertkit_subscriber_verification', array( $this, 'subscriber_verification' ) ); |
| 35 |
add_action( 'wp_ajax_convertkit_subscriber_verification', array( $this, 'subscriber_verification' ) ); |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Returns all ConvertKit registered blocks. |
| 41 |
* |
| 42 |
* Typically used when a refresh button in a block has been pressed when |
| 43 |
* convertKitGutenbergDisplayBlockNoticeWithLink() is called, because either |
| 44 |
* no Access Token is specified, or no resources exist in ConvertKit. |
| 45 |
* |
| 46 |
* @since 2.2.6 |
| 47 |
*/ |
| 48 |
public function get_blocks() { |
| 49 |
|
| 50 |
// Check nonce. |
| 51 |
check_ajax_referer( 'convertkit_get_blocks', 'nonce' ); |
| 52 |
|
| 53 |
// Refresh resources from the API, to reflect any changes. |
| 54 |
$forms = new ConvertKit_Resource_Forms( 'block_edit' ); |
| 55 |
$forms->refresh(); |
| 56 |
|
| 57 |
$posts = new ConvertKit_Resource_Posts( 'block_edit' ); |
| 58 |
$posts->refresh(); |
| 59 |
|
| 60 |
$products = new ConvertKit_Resource_Products( 'block_edit' ); |
| 61 |
$products->refresh(); |
| 62 |
|
| 63 |
// Return blocks. |
| 64 |
wp_send_json_success( convertkit_get_blocks() ); |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Stores the ConvertKit Subscriber's ID in a cookie. |
| 70 |
* |
| 71 |
* Typically performed when the user subscribes via a ConvertKit Form on the web site |
| 72 |
* that is set to "Send subscriber to thank you page", and the Plugin's JavaScript is not |
| 73 |
* disabled, permitting convertkit.js to run. |
| 74 |
* |
| 75 |
* @since 1.9.6 |
| 76 |
*/ |
| 77 |
public function store_subscriber_id_in_cookie() { |
| 78 |
|
| 79 |
// Check nonce. |
| 80 |
check_ajax_referer( 'convertkit', 'convertkit_nonce' ); |
| 81 |
|
| 82 |
// Bail if required request parameters not submitted. |
| 83 |
if ( ! isset( $_REQUEST['subscriber_id'] ) ) { |
| 84 |
wp_send_json_error( __( 'ConvertKit: Required parameter `subscriber_id` not included in AJAX request.', 'convertkit' ) ); |
| 85 |
} |
| 86 |
|
| 87 |
// Bail if no subscriber ID provided. |
| 88 |
$id = absint( sanitize_text_field( $_REQUEST['subscriber_id'] ) ); |
| 89 |
if ( empty( $id ) ) { |
| 90 |
wp_send_json_error( __( 'ConvertKit: Required parameter `subscriber_id` empty in AJAX request.', 'convertkit' ) ); |
| 91 |
} |
| 92 |
|
| 93 |
// Get subscriber ID. |
| 94 |
$subscriber = new ConvertKit_Subscriber(); |
| 95 |
$subscriber_id = $subscriber->validate_and_store_subscriber_id( $id ); |
| 96 |
|
| 97 |
// Bail if an error occured i.e. API hasn't been configured, subscriber ID does not exist in ConvertKit etc. |
| 98 |
if ( is_wp_error( $subscriber_id ) ) { |
| 99 |
wp_send_json_error( $subscriber_id->get_error_message() ); |
| 100 |
} |
| 101 |
|
| 102 |
// Return the subscriber ID. |
| 103 |
wp_send_json_success( |
| 104 |
array( |
| 105 |
'id' => $subscriber_id, |
| 106 |
) |
| 107 |
); |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Stores the ConvertKit Subscriber Email's ID in a cookie. |
| 113 |
* |
| 114 |
* Typically performed when the user subscribes via a ConvertKit Form on the web site |
| 115 |
* and the Plugin's JavaScript is not disabled, permitting convertkit.js to run. |
| 116 |
* |
| 117 |
* @since 1.9.6 |
| 118 |
*/ |
| 119 |
public function store_subscriber_email_as_id_in_cookie() { |
| 120 |
|
| 121 |
// Check nonce. |
| 122 |
check_ajax_referer( 'convertkit', 'convertkit_nonce' ); |
| 123 |
|
| 124 |
// Bail if required request parameters not submitted. |
| 125 |
if ( ! isset( $_REQUEST['email'] ) ) { |
| 126 |
wp_send_json_error( __( 'ConvertKit: Required parameter `email` not included in AJAX request.', 'convertkit' ) ); |
| 127 |
} |
| 128 |
$email = sanitize_text_field( $_REQUEST['email'] ); |
| 129 |
|
| 130 |
// Bail if the email address is empty. |
| 131 |
if ( empty( $email ) ) { |
| 132 |
wp_send_json_error( __( 'ConvertKit: Required parameter `email` is empty.', 'convertkit' ) ); |
| 133 |
} |
| 134 |
|
| 135 |
// Bail if the email address isn't a valid email address. |
| 136 |
if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) { |
| 137 |
wp_send_json_error( __( 'ConvertKit: Required parameter `email` is not an email address.', 'convertkit' ) ); |
| 138 |
} |
| 139 |
|
| 140 |
// Get subscriber ID. |
| 141 |
$subscriber = new ConvertKit_Subscriber(); |
| 142 |
$subscriber_id = $subscriber->validate_and_store_subscriber_email( $email ); |
| 143 |
|
| 144 |
// Bail if an error occured i.e. API hasn't been configured, subscriber ID does not exist in ConvertKit etc. |
| 145 |
if ( is_wp_error( $subscriber_id ) ) { |
| 146 |
wp_send_json_error( $subscriber_id->get_error_message() ); |
| 147 |
} |
| 148 |
|
| 149 |
// Return the subscriber ID. |
| 150 |
wp_send_json_success( |
| 151 |
array( |
| 152 |
'id' => $subscriber_id, |
| 153 |
) |
| 154 |
); |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Calls the API to send the subscriber a magic link by email containing a code when |
| 160 |
* the modal version of Restrict Content is used, and the user has submitted their email address. |
| 161 |
* |
| 162 |
* Returns a view of either: |
| 163 |
* - an error message and email input i.e. the user entered an invalid email address, |
| 164 |
* - the code input, which is then displayed in the modal for the user to enter the code sent by email. |
| 165 |
* |
| 166 |
* See maybe_run_subscriber_verification() for logic once they enter the code on screen. |
| 167 |
* |
| 168 |
* @since 2.3.8 |
| 169 |
*/ |
| 170 |
public function subscriber_authentication_send_code() { |
| 171 |
|
| 172 |
// Load Restrict Content class. |
| 173 |
$output_restrict_content = WP_ConvertKit()->get_class( 'output_restrict_content' ); |
| 174 |
|
| 175 |
// Run subscriber authentication. |
| 176 |
$output_restrict_content->maybe_run_subscriber_authentication(); |
| 177 |
|
| 178 |
// If an error occured, build the email form view with the error message. |
| 179 |
if ( is_wp_error( $output_restrict_content->error ) ) { |
| 180 |
ob_start(); |
| 181 |
include CONVERTKIT_PLUGIN_PATH . '/views/frontend/restrict-content/product-modal-content-email.php'; |
| 182 |
$output = trim( ob_get_clean() ); |
| 183 |
wp_send_json_success( $output ); |
| 184 |
} |
| 185 |
|
| 186 |
// Build authentication code view to return for output. |
| 187 |
ob_start(); |
| 188 |
include CONVERTKIT_PLUGIN_PATH . '/views/frontend/restrict-content/product-modal-content-code.php'; |
| 189 |
$output = trim( ob_get_clean() ); |
| 190 |
wp_send_json_success( $output ); |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Calls the API to verify the token and entered subscriber code, which tells us that the email |
| 196 |
* address supplied truly belongs to the user, and that we can safely trust their subscriber ID |
| 197 |
* to be valid. |
| 198 |
* |
| 199 |
* @since 2.3.8 |
| 200 |
*/ |
| 201 |
public function subscriber_verification() { |
| 202 |
|
| 203 |
// Load Restrict Content class. |
| 204 |
$output_restrict_content = WP_ConvertKit()->get_class( 'output_restrict_content' ); |
| 205 |
|
| 206 |
// Run subscriber authentication. |
| 207 |
$output_restrict_content->maybe_run_subscriber_verification(); |
| 208 |
|
| 209 |
// If an error occured, build the code form view with the error message. |
| 210 |
if ( is_wp_error( $output_restrict_content->error ) ) { |
| 211 |
ob_start(); |
| 212 |
include CONVERTKIT_PLUGIN_PATH . '/views/frontend/restrict-content/product-modal-content-code.php'; |
| 213 |
$output = trim( ob_get_clean() ); |
| 214 |
wp_send_json_error( $output ); |
| 215 |
} |
| 216 |
|
| 217 |
// Return success with the URL to the Post, including the `ck-cache-bust` parameter. |
| 218 |
// JS will load the given URL to show the restricted content. |
| 219 |
wp_send_json_success( $output_restrict_content->get_url( true ) ); |
| 220 |
|
| 221 |
} |
| 222 |
|
| 223 |
} |
| 224 |
|