| 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_tag_subscriber', array( $this, 'tag_subscriber' ) ); |
| 32 |
add_action( 'wp_ajax_convertkit_tag_subscriber', array( $this, 'tag_subscriber' ) ); |
| 33 |
|
| 34 |
add_action( 'wp_ajax_nopriv_convertkit_subscriber_authentication_send_code', array( $this, 'subscriber_authentication_send_code' ) ); |
| 35 |
add_action( 'wp_ajax_convertkit_subscriber_authentication_send_code', array( $this, 'subscriber_authentication_send_code' ) ); |
| 36 |
|
| 37 |
add_action( 'wp_ajax_nopriv_convertkit_subscriber_verification', array( $this, 'subscriber_verification' ) ); |
| 38 |
add_action( 'wp_ajax_convertkit_subscriber_verification', array( $this, 'subscriber_verification' ) ); |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Returns all ConvertKit registered blocks. |
| 44 |
* |
| 45 |
* Typically used when a refresh button in a block has been pressed when |
| 46 |
* convertKitGutenbergDisplayBlockNoticeWithLink() is called, because either |
| 47 |
* no API keys were specified, or no resources exist in ConvertKit. |
| 48 |
* |
| 49 |
* @since 2.2.6 |
| 50 |
*/ |
| 51 |
public function get_blocks() { |
| 52 |
|
| 53 |
// Check nonce. |
| 54 |
check_ajax_referer( 'convertkit_get_blocks', 'nonce' ); |
| 55 |
|
| 56 |
// Refresh resources from the API, to reflect any changes. |
| 57 |
$forms = new ConvertKit_Resource_Forms( 'block_edit' ); |
| 58 |
$forms->refresh(); |
| 59 |
|
| 60 |
$posts = new ConvertKit_Resource_Posts( 'block_edit' ); |
| 61 |
$posts->refresh(); |
| 62 |
|
| 63 |
$products = new ConvertKit_Resource_Products( 'block_edit' ); |
| 64 |
$products->refresh(); |
| 65 |
|
| 66 |
// Return blocks. |
| 67 |
wp_send_json_success( convertkit_get_blocks() ); |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Stores the ConvertKit Subscriber's ID in a cookie. |
| 73 |
* |
| 74 |
* Typically performed when the user subscribes via a ConvertKit Form on the web site |
| 75 |
* that is set to "Send subscriber to thank you page", and the Plugin's JavaScript is not |
| 76 |
* disabled, permitting convertkit.js to run. |
| 77 |
* |
| 78 |
* @since 1.9.6 |
| 79 |
*/ |
| 80 |
public function store_subscriber_id_in_cookie() { |
| 81 |
|
| 82 |
// Check nonce. |
| 83 |
check_ajax_referer( 'convertkit', 'convertkit_nonce' ); |
| 84 |
|
| 85 |
// Bail if required request parameters not submitted. |
| 86 |
if ( ! isset( $_REQUEST['subscriber_id'] ) ) { |
| 87 |
wp_send_json_error( __( 'ConvertKit: Required parameter `subscriber_id` not included in AJAX request.', 'convertkit' ) ); |
| 88 |
} |
| 89 |
|
| 90 |
// Bail if no subscriber ID provided. |
| 91 |
$id = absint( sanitize_text_field( $_REQUEST['subscriber_id'] ) ); |
| 92 |
if ( empty( $id ) ) { |
| 93 |
wp_send_json_error( __( 'ConvertKit: Required parameter `subscriber_id` empty in AJAX request.', 'convertkit' ) ); |
| 94 |
} |
| 95 |
|
| 96 |
// Get subscriber ID. |
| 97 |
$subscriber = new ConvertKit_Subscriber(); |
| 98 |
$subscriber_id = $subscriber->validate_and_store_subscriber_id( $id ); |
| 99 |
|
| 100 |
// Bail if an error occured i.e. API hasn't been configured, subscriber ID does not exist in ConvertKit etc. |
| 101 |
if ( is_wp_error( $subscriber_id ) ) { |
| 102 |
wp_send_json_error( $subscriber_id->get_error_message() ); |
| 103 |
} |
| 104 |
|
| 105 |
// Return the subscriber ID. |
| 106 |
wp_send_json_success( |
| 107 |
array( |
| 108 |
'id' => $subscriber_id, |
| 109 |
) |
| 110 |
); |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Stores the ConvertKit Subscriber Email's ID in a cookie. |
| 116 |
* |
| 117 |
* Typically performed when the user subscribes via a ConvertKit Form on the web site |
| 118 |
* and the Plugin's JavaScript is not disabled, permitting convertkit.js to run. |
| 119 |
* |
| 120 |
* @since 1.9.6 |
| 121 |
*/ |
| 122 |
public function store_subscriber_email_as_id_in_cookie() { |
| 123 |
|
| 124 |
// Check nonce. |
| 125 |
check_ajax_referer( 'convertkit', 'convertkit_nonce' ); |
| 126 |
|
| 127 |
// Bail if required request parameters not submitted. |
| 128 |
if ( ! isset( $_REQUEST['email'] ) ) { |
| 129 |
wp_send_json_error( __( 'ConvertKit: Required parameter `email` not included in AJAX request.', 'convertkit' ) ); |
| 130 |
} |
| 131 |
$email = sanitize_text_field( $_REQUEST['email'] ); |
| 132 |
|
| 133 |
// Bail if the email address is empty. |
| 134 |
if ( empty( $email ) ) { |
| 135 |
wp_send_json_error( __( 'ConvertKit: Required parameter `email` is empty.', 'convertkit' ) ); |
| 136 |
} |
| 137 |
|
| 138 |
// Bail if the email address isn't a valid email address. |
| 139 |
if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) { |
| 140 |
wp_send_json_error( __( 'ConvertKit: Required parameter `email` is not an email address.', 'convertkit' ) ); |
| 141 |
} |
| 142 |
|
| 143 |
// Get subscriber ID. |
| 144 |
$subscriber = new ConvertKit_Subscriber(); |
| 145 |
$subscriber_id = $subscriber->validate_and_store_subscriber_email( $email ); |
| 146 |
|
| 147 |
// Bail if an error occured i.e. API hasn't been configured, subscriber ID does not exist in ConvertKit etc. |
| 148 |
if ( is_wp_error( $subscriber_id ) ) { |
| 149 |
wp_send_json_error( $subscriber_id->get_error_message() ); |
| 150 |
} |
| 151 |
|
| 152 |
// Return the subscriber ID. |
| 153 |
wp_send_json_success( |
| 154 |
array( |
| 155 |
'id' => $subscriber_id, |
| 156 |
) |
| 157 |
); |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Tags a subscriber when their subscriber ID is present in the cookie or URL, |
| 163 |
* and the Page's ConvertKit Settings specify a Tag. |
| 164 |
* |
| 165 |
* @since 1.9.6 |
| 166 |
*/ |
| 167 |
public function tag_subscriber() { |
| 168 |
|
| 169 |
// Check nonce. |
| 170 |
check_ajax_referer( 'convertkit', 'convertkit_nonce' ); |
| 171 |
|
| 172 |
// Bail if required request parameters not submitted. |
| 173 |
if ( ! isset( $_REQUEST['subscriber_id'] ) ) { |
| 174 |
wp_send_json_error( __( 'ConvertKit: Required parameter `subscriber_id` not included in AJAX request.', 'convertkit' ) ); |
| 175 |
} |
| 176 |
if ( ! isset( $_REQUEST['tag'] ) ) { |
| 177 |
wp_send_json_error( __( 'ConvertKit: Required parameter `tag` not included in AJAX request.', 'convertkit' ) ); |
| 178 |
} |
| 179 |
$subscriber_id = absint( sanitize_text_field( $_REQUEST['subscriber_id'] ) ); |
| 180 |
$tag_id = absint( sanitize_text_field( $_REQUEST['tag'] ) ); |
| 181 |
|
| 182 |
// Bail if no subscriber ID or tag provided. |
| 183 |
if ( empty( $subscriber_id ) ) { |
| 184 |
wp_send_json_error( __( 'ConvertKit: Required parameter `subscriber_id` empty in AJAX request.', 'convertkit' ) ); |
| 185 |
} |
| 186 |
if ( empty( $tag_id ) ) { |
| 187 |
wp_send_json_error( __( 'ConvertKit: Required parameter `tag` empty in AJAX request.', 'convertkit' ) ); |
| 188 |
} |
| 189 |
|
| 190 |
// Bail if the API hasn't been configured. |
| 191 |
$settings = new ConvertKit_Settings(); |
| 192 |
if ( ! $settings->has_api_key_and_secret() ) { |
| 193 |
wp_send_json_error( __( 'ConvertKit: API Keys not defined in Plugin Settings.', 'convertkit' ) ); |
| 194 |
} |
| 195 |
|
| 196 |
// Initialize the API. |
| 197 |
$api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled() ); |
| 198 |
|
| 199 |
// Get subscriber's email address by subscriber ID. |
| 200 |
$subscriber = $api->get_subscriber_by_id( $subscriber_id ); |
| 201 |
|
| 202 |
// Bail if the subscriber could not be found. |
| 203 |
if ( is_wp_error( $subscriber ) ) { |
| 204 |
wp_send_json_error( $subscriber->get_error_message() ); |
| 205 |
} |
| 206 |
|
| 207 |
// Extract the subscriber's email. |
| 208 |
$email = $subscriber['email_address']; |
| 209 |
|
| 210 |
// Store the subscriber ID as a cookie. |
| 211 |
$subscriber = new ConvertKit_Subscriber(); |
| 212 |
$subscriber->set( $subscriber_id ); |
| 213 |
|
| 214 |
// Tag the subscriber with the Post's tag. |
| 215 |
$tag = $api->tag_subscribe( $tag_id, $email ); |
| 216 |
|
| 217 |
// Bail if an error occured tagging the subscriber. |
| 218 |
if ( is_wp_error( $tag ) ) { |
| 219 |
wp_send_json_error( $tag ); |
| 220 |
} |
| 221 |
|
| 222 |
wp_send_json_success( $tag ); |
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Calls the API to send the subscriber a magic link by email containing a code when |
| 228 |
* the modal version of Restrict Content is used, and the user has submitted their email address. |
| 229 |
* |
| 230 |
* Returns a view of either: |
| 231 |
* - an error message and email input i.e. the user entered an invalid email address, |
| 232 |
* - the code input, which is then displayed in the modal for the user to enter the code sent by email. |
| 233 |
* |
| 234 |
* See maybe_run_subscriber_verification() for logic once they enter the code on screen. |
| 235 |
* |
| 236 |
* @since 2.3.8 |
| 237 |
*/ |
| 238 |
public function subscriber_authentication_send_code() { |
| 239 |
|
| 240 |
// Load Restrict Content class. |
| 241 |
$output_restrict_content = WP_ConvertKit()->get_class( 'output_restrict_content' ); |
| 242 |
|
| 243 |
// Run subscriber authentication. |
| 244 |
$output_restrict_content->maybe_run_subscriber_authentication(); |
| 245 |
|
| 246 |
// If an error occured, build the email form view with the error message. |
| 247 |
if ( is_wp_error( $output_restrict_content->error ) ) { |
| 248 |
ob_start(); |
| 249 |
include CONVERTKIT_PLUGIN_PATH . '/views/frontend/restrict-content/product-modal-content-email.php'; |
| 250 |
$output = trim( ob_get_clean() ); |
| 251 |
wp_send_json_success( $output ); |
| 252 |
} |
| 253 |
|
| 254 |
// Build authentication code view to return for output. |
| 255 |
ob_start(); |
| 256 |
include CONVERTKIT_PLUGIN_PATH . '/views/frontend/restrict-content/product-modal-content-code.php'; |
| 257 |
$output = trim( ob_get_clean() ); |
| 258 |
wp_send_json_success( $output ); |
| 259 |
|
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Calls the API to verify the token and entered subscriber code, which tells us that the email |
| 264 |
* address supplied truly belongs to the user, and that we can safely trust their subscriber ID |
| 265 |
* to be valid. |
| 266 |
* |
| 267 |
* @since 2.3.8 |
| 268 |
*/ |
| 269 |
public function subscriber_verification() { |
| 270 |
|
| 271 |
// Load Restrict Content class. |
| 272 |
$output_restrict_content = WP_ConvertKit()->get_class( 'output_restrict_content' ); |
| 273 |
|
| 274 |
// Run subscriber authentication. |
| 275 |
$output_restrict_content->maybe_run_subscriber_verification(); |
| 276 |
|
| 277 |
// If an error occured, build the code form view with the error message. |
| 278 |
if ( is_wp_error( $output_restrict_content->error ) ) { |
| 279 |
ob_start(); |
| 280 |
include CONVERTKIT_PLUGIN_PATH . '/views/frontend/restrict-content/product-modal-content-code.php'; |
| 281 |
$output = trim( ob_get_clean() ); |
| 282 |
wp_send_json_error( $output ); |
| 283 |
} |
| 284 |
|
| 285 |
// Return success with the URL to the Post, including the `ck-cache-bust` parameter. |
| 286 |
// JS will load the given URL to show the restricted content. |
| 287 |
wp_send_json_success( $output_restrict_content->get_url( true ) ); |
| 288 |
|
| 289 |
} |
| 290 |
|
| 291 |
} |
| 292 |
|