| 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 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Returns all ConvertKit registered blocks. |
| 38 |
* |
| 39 |
* Typically used when a refresh button in a block has been pressed when |
| 40 |
* convertKitGutenbergDisplayBlockNoticeWithLink() is called, because either |
| 41 |
* no API keys were specified, or no resources exist in ConvertKit. |
| 42 |
* |
| 43 |
* @since 2.2.6 |
| 44 |
*/ |
| 45 |
public function get_blocks() { |
| 46 |
|
| 47 |
// Check nonce. |
| 48 |
check_ajax_referer( 'convertkit_get_blocks', 'nonce' ); |
| 49 |
|
| 50 |
// Refresh resources from the API, to reflect any changes. |
| 51 |
$forms = new ConvertKit_Resource_Forms( 'block_edit' ); |
| 52 |
$forms->refresh(); |
| 53 |
|
| 54 |
$posts = new ConvertKit_Resource_Posts( 'block_edit' ); |
| 55 |
$posts->refresh(); |
| 56 |
|
| 57 |
$products = new ConvertKit_Resource_Products( 'block_edit' ); |
| 58 |
$products->refresh(); |
| 59 |
|
| 60 |
// Return blocks. |
| 61 |
wp_send_json_success( convertkit_get_blocks() ); |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Stores the ConvertKit Subscriber's ID in a cookie. |
| 67 |
* |
| 68 |
* Typically performed when the user subscribes via a ConvertKit Form on the web site |
| 69 |
* that is set to "Send subscriber to thank you page", and the Plugin's JavaScript is not |
| 70 |
* disabled, permitting convertkit.js to run. |
| 71 |
* |
| 72 |
* @since 1.9.6 |
| 73 |
*/ |
| 74 |
public function store_subscriber_id_in_cookie() { |
| 75 |
|
| 76 |
// Check nonce. |
| 77 |
check_ajax_referer( 'convertkit', 'convertkit_nonce' ); |
| 78 |
|
| 79 |
// Bail if required request parameters not submitted. |
| 80 |
if ( ! isset( $_REQUEST['subscriber_id'] ) ) { |
| 81 |
wp_send_json_error( __( 'ConvertKit: Required parameter `subscriber_id` not included in AJAX request.', 'convertkit' ) ); |
| 82 |
} |
| 83 |
|
| 84 |
// Bail if no subscriber ID provided. |
| 85 |
$id = absint( sanitize_text_field( $_REQUEST['subscriber_id'] ) ); |
| 86 |
if ( empty( $id ) ) { |
| 87 |
wp_send_json_error( __( 'ConvertKit: Required parameter `subscriber_id` empty in AJAX request.', 'convertkit' ) ); |
| 88 |
} |
| 89 |
|
| 90 |
// Get subscriber ID. |
| 91 |
$subscriber = new ConvertKit_Subscriber(); |
| 92 |
$subscriber_id = $subscriber->validate_and_store_subscriber_id( $id ); |
| 93 |
|
| 94 |
// Bail if an error occured i.e. API hasn't been configured, subscriber ID does not exist in ConvertKit etc. |
| 95 |
if ( is_wp_error( $subscriber_id ) ) { |
| 96 |
wp_send_json_error( $subscriber_id->get_error_message() ); |
| 97 |
} |
| 98 |
|
| 99 |
// Return the subscriber ID. |
| 100 |
wp_send_json_success( |
| 101 |
array( |
| 102 |
'id' => $subscriber_id, |
| 103 |
) |
| 104 |
); |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Stores the ConvertKit Subscriber Email's ID in a cookie. |
| 110 |
* |
| 111 |
* Typically performed when the user subscribes via a ConvertKit Form on the web site |
| 112 |
* and the Plugin's JavaScript is not disabled, permitting convertkit.js to run. |
| 113 |
* |
| 114 |
* @since 1.9.6 |
| 115 |
*/ |
| 116 |
public function store_subscriber_email_as_id_in_cookie() { |
| 117 |
|
| 118 |
// Check nonce. |
| 119 |
check_ajax_referer( 'convertkit', 'convertkit_nonce' ); |
| 120 |
|
| 121 |
// Bail if required request parameters not submitted. |
| 122 |
if ( ! isset( $_REQUEST['email'] ) ) { |
| 123 |
wp_send_json_error( __( 'ConvertKit: Required parameter `email` not included in AJAX request.', 'convertkit' ) ); |
| 124 |
} |
| 125 |
$email = sanitize_text_field( $_REQUEST['email'] ); |
| 126 |
|
| 127 |
// Bail if the email address is empty. |
| 128 |
if ( empty( $email ) ) { |
| 129 |
wp_send_json_error( __( 'ConvertKit: Required parameter `email` is empty.', 'convertkit' ) ); |
| 130 |
} |
| 131 |
|
| 132 |
// Bail if the email address isn't a valid email address. |
| 133 |
if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) { |
| 134 |
wp_send_json_error( __( 'ConvertKit: Required parameter `email` is not an email address.', 'convertkit' ) ); |
| 135 |
} |
| 136 |
|
| 137 |
// Get subscriber ID. |
| 138 |
$subscriber = new ConvertKit_Subscriber(); |
| 139 |
$subscriber_id = $subscriber->validate_and_store_subscriber_email( $email ); |
| 140 |
|
| 141 |
// Bail if an error occured i.e. API hasn't been configured, subscriber ID does not exist in ConvertKit etc. |
| 142 |
if ( is_wp_error( $subscriber_id ) ) { |
| 143 |
wp_send_json_error( $subscriber_id->get_error_message() ); |
| 144 |
} |
| 145 |
|
| 146 |
// Return the subscriber ID. |
| 147 |
wp_send_json_success( |
| 148 |
array( |
| 149 |
'id' => $subscriber_id, |
| 150 |
) |
| 151 |
); |
| 152 |
|
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Tags a subscriber when their subscriber ID is present in the cookie or URL, |
| 157 |
* and the Page's ConvertKit Settings specify a Tag. |
| 158 |
* |
| 159 |
* @since 1.9.6 |
| 160 |
*/ |
| 161 |
public function tag_subscriber() { |
| 162 |
|
| 163 |
// Check nonce. |
| 164 |
check_ajax_referer( 'convertkit', 'convertkit_nonce' ); |
| 165 |
|
| 166 |
// Bail if required request parameters not submitted. |
| 167 |
if ( ! isset( $_REQUEST['subscriber_id'] ) ) { |
| 168 |
wp_send_json_error( __( 'ConvertKit: Required parameter `subscriber_id` not included in AJAX request.', 'convertkit' ) ); |
| 169 |
} |
| 170 |
if ( ! isset( $_REQUEST['tag'] ) ) { |
| 171 |
wp_send_json_error( __( 'ConvertKit: Required parameter `tag` not included in AJAX request.', 'convertkit' ) ); |
| 172 |
} |
| 173 |
$subscriber_id = absint( sanitize_text_field( $_REQUEST['subscriber_id'] ) ); |
| 174 |
$tag_id = absint( sanitize_text_field( $_REQUEST['tag'] ) ); |
| 175 |
|
| 176 |
// Bail if no subscriber ID or tag provided. |
| 177 |
if ( empty( $subscriber_id ) ) { |
| 178 |
wp_send_json_error( __( 'ConvertKit: Required parameter `subscriber_id` empty in AJAX request.', 'convertkit' ) ); |
| 179 |
} |
| 180 |
if ( empty( $tag_id ) ) { |
| 181 |
wp_send_json_error( __( 'ConvertKit: Required parameter `tag` empty in AJAX request.', 'convertkit' ) ); |
| 182 |
} |
| 183 |
|
| 184 |
// Bail if the API hasn't been configured. |
| 185 |
$settings = new ConvertKit_Settings(); |
| 186 |
if ( ! $settings->has_api_key_and_secret() ) { |
| 187 |
wp_send_json_error( __( 'ConvertKit: API Keys not defined in Plugin Settings.', 'convertkit' ) ); |
| 188 |
} |
| 189 |
|
| 190 |
// Initialize the API. |
| 191 |
$api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled() ); |
| 192 |
|
| 193 |
// Get subscriber's email address by subscriber ID. |
| 194 |
$subscriber = $api->get_subscriber_by_id( $subscriber_id ); |
| 195 |
|
| 196 |
// Bail if the subscriber could not be found. |
| 197 |
if ( is_wp_error( $subscriber ) ) { |
| 198 |
wp_send_json_error( $subscriber->get_error_message() ); |
| 199 |
} |
| 200 |
|
| 201 |
// Extract the subscriber's email. |
| 202 |
$email = $subscriber['email_address']; |
| 203 |
|
| 204 |
// Store the subscriber ID as a cookie. |
| 205 |
$subscriber = new ConvertKit_Subscriber(); |
| 206 |
$subscriber->set( $subscriber_id ); |
| 207 |
|
| 208 |
// Tag the subscriber with the Post's tag. |
| 209 |
$tag = $api->tag_subscribe( $tag_id, $email ); |
| 210 |
|
| 211 |
// Bail if an error occured tagging the subscriber. |
| 212 |
if ( is_wp_error( $tag ) ) { |
| 213 |
wp_send_json_error( $tag ); |
| 214 |
} |
| 215 |
|
| 216 |
wp_send_json_success( $tag ); |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
} |
| 221 |
|