| @@ -19,8 +19,10 @@ | ||
| 19 | 19 | * @since 1.9.6 |
| 20 | 20 | */ |
| 21 | 21 | public function __construct() { |
| 22 | 22 | |
| 23 | + add_action( 'wp_ajax_convertkit_get_blocks', array( $this, 'get_blocks' ) ); | |
| 24 | + | |
| 23 | 25 | add_action( 'wp_ajax_nopriv_convertkit_store_subscriber_id_in_cookie', array( $this, 'store_subscriber_id_in_cookie' ) ); |
| 24 | 26 | add_action( 'wp_ajax_convertkit_store_subscriber_id_in_cookie', array( $this, 'store_subscriber_id_in_cookie' ) ); |
| 25 | 27 | |
| 26 | 28 | add_action( 'wp_ajax_nopriv_convertkit_store_subscriber_email_as_id_in_cookie', array( $this, 'store_subscriber_email_as_id_in_cookie' ) ); |
| @@ -25,14 +27,46 @@ | ||
| 25 | 27 | |
| 26 | 28 | add_action( 'wp_ajax_nopriv_convertkit_store_subscriber_email_as_id_in_cookie', array( $this, 'store_subscriber_email_as_id_in_cookie' ) ); |
| 27 | 29 | add_action( 'wp_ajax_convertkit_store_subscriber_email_as_id_in_cookie', array( $this, 'store_subscriber_email_as_id_in_cookie' ) ); |
| 28 | 30 | |
| 29 | - add_action( 'wp_ajax_nopriv_convertkit_tag_subscriber', array( $this, 'tag_subscriber' ) ); | |
| 30 | - add_action( 'wp_ajax_convertkit_tag_subscriber', array( $this, 'tag_subscriber' ) ); | |
| 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' ) ); | |
| 31 | 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 | + | |
| 32 | 37 | } |
| 33 | 38 | |
| 34 | 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 | + /** | |
| 35 | 69 | * Stores the ConvertKit Subscriber's ID in a cookie. |
| 36 | 70 | * |
| 37 | 71 | * Typically performed when the user subscribes via a ConvertKit Form on the web site |
| 38 | 72 | * that is set to "Send subscriber to thank you page", and the Plugin's JavaScript is not |
| @@ -121,69 +155,69 @@ | ||
| 121 | 155 | |
| 122 | 156 | } |
| 123 | 157 | |
| 124 | 158 | /** |
| 125 | - * Tags a subscriber when their subscriber ID is present in the cookie or URL, | |
| 126 | - * and the Page's ConvertKit Settings specify a Tag. | |
| 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. | |
| 127 | 161 | * |
| 128 | - * @since 1.9.6 | |
| 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 | |
| 129 | 169 | */ |
| 130 | - public function tag_subscriber() { | |
| 170 | + public function subscriber_authentication_send_code() { | |
| 131 | 171 | |
| 132 | - // Check nonce. | |
| 133 | - check_ajax_referer( 'convertkit', 'convertkit_nonce' ); | |
| 172 | + // Load Restrict Content class. | |
| 173 | + $output_restrict_content = WP_ConvertKit()->get_class( 'output_restrict_content' ); | |
| 134 | 174 | |
| 135 | - // Bail if required request parameters not submitted. | |
| 136 | - if ( ! isset( $_REQUEST['subscriber_id'] ) ) { | |
| 137 | - wp_send_json_error( __( 'ConvertKit: Required parameter `subscriber_id` not included in AJAX request.', 'convertkit' ) ); | |
| 138 | - } | |
| 139 | - if ( ! isset( $_REQUEST['tag'] ) ) { | |
| 140 | - wp_send_json_error( __( 'ConvertKit: Required parameter `tag` not included in AJAX request.', 'convertkit' ) ); | |
| 141 | - } | |
| 142 | - $subscriber_id = absint( sanitize_text_field( $_REQUEST['subscriber_id'] ) ); | |
| 143 | - $tag_id = absint( sanitize_text_field( $_REQUEST['tag'] ) ); | |
| 175 | + // Run subscriber authentication. | |
| 176 | + $output_restrict_content->maybe_run_subscriber_authentication(); | |
| 144 | 177 | |
| 145 | - // Bail if no subscriber ID or tag provided. | |
| 146 | - if ( empty( $subscriber_id ) ) { | |
| 147 | - wp_send_json_error( __( 'ConvertKit: Required parameter `subscriber_id` empty in AJAX request.', 'convertkit' ) ); | |
| 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 ); | |
| 148 | 184 | } |
| 149 | - if ( empty( $tag_id ) ) { | |
| 150 | - wp_send_json_error( __( 'ConvertKit: Required parameter `tag` empty in AJAX request.', 'convertkit' ) ); | |
| 151 | - } | |
| 152 | 185 | |
| 153 | - // Bail if the API hasn't been configured. | |
| 154 | - $settings = new ConvertKit_Settings(); | |
| 155 | - if ( ! $settings->has_api_key_and_secret() ) { | |
| 156 | - wp_send_json_error( __( 'ConvertKit: API Keys not defined in Plugin Settings.', 'convertkit' ) ); | |
| 157 | - } | |
| 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 ); | |
| 158 | 191 | |
| 159 | - // Initialize the API. | |
| 160 | - $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled() ); | |
| 192 | + } | |
| 161 | 193 | |
| 162 | - // Get subscriber's email address by subscriber ID. | |
| 163 | - $subscriber = $api->get_subscriber_by_id( $subscriber_id ); | |
| 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() { | |
| 164 | 202 | |
| 165 | - // Bail if the subscriber could not be found. | |
| 166 | - if ( is_wp_error( $subscriber ) ) { | |
| 167 | - wp_send_json_error( $subscriber->get_error_message() ); | |
| 168 | - } | |
| 203 | + // Load Restrict Content class. | |
| 204 | + $output_restrict_content = WP_ConvertKit()->get_class( 'output_restrict_content' ); | |
| 169 | 205 | |
| 170 | - // Extract the subscriber's email. | |
| 171 | - $email = $subscriber['email_address']; | |
| 206 | + // Run subscriber authentication. | |
| 207 | + $output_restrict_content->maybe_run_subscriber_verification(); | |
| 172 | 208 | |
| 173 | - // Store the subscriber ID as a cookie. | |
| 174 | - $subscriber = new ConvertKit_Subscriber(); | |
| 175 | - $subscriber->set( $subscriber_id ); | |
| 176 | - | |
| 177 | - // Tag the subscriber with the Post's tag. | |
| 178 | - $tag = $api->tag_subscribe( $tag_id, $email ); | |
| 179 | - | |
| 180 | - // Bail if an error occured tagging the subscriber. | |
| 181 | - if ( is_wp_error( $tag ) ) { | |
| 182 | - wp_send_json_error( $tag ); | |
| 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 ); | |
| 183 | 215 | } |
| 184 | 216 | |
| 185 | - wp_send_json_success( $tag ); | |
| 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 ) ); | |
| 186 | 220 | |
| 187 | 221 | } |
| 188 | 222 | |
| 189 | 223 | } |