| @@ -27,11 +27,14 @@ | ||
| 27 | 27 | |
| 28 | 28 | add_action( 'wp_ajax_nopriv_convertkit_store_subscriber_email_as_id_in_cookie', array( $this, 'store_subscriber_email_as_id_in_cookie' ) ); |
| 29 | 29 | add_action( 'wp_ajax_convertkit_store_subscriber_email_as_id_in_cookie', array( $this, 'store_subscriber_email_as_id_in_cookie' ) ); |
| 30 | 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' ) ); | |
| 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 | 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 | + | |
| 34 | 37 | } |
| 35 | 38 | |
| 36 | 39 | /** |
| 37 | 40 | * Returns all ConvertKit registered blocks. |
| @@ -37,9 +40,9 @@ | ||
| 37 | 40 | * Returns all ConvertKit registered blocks. |
| 38 | 41 | * |
| 39 | 42 | * Typically used when a refresh button in a block has been pressed when |
| 40 | 43 | * convertKitGutenbergDisplayBlockNoticeWithLink() is called, because either |
| 41 | - * no API keys were specified, or no resources exist in ConvertKit. | |
| 44 | + * no Access Token is specified, or no resources exist in ConvertKit. | |
| 42 | 45 | * |
| 43 | 46 | * @since 2.2.6 |
| 44 | 47 | */ |
| 45 | 48 | public function get_blocks() { |
| @@ -152,69 +155,69 @@ | ||
| 152 | 155 | |
| 153 | 156 | } |
| 154 | 157 | |
| 155 | 158 | /** |
| 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. | |
| 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. | |
| 158 | 161 | * |
| 159 | - * @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 | |
| 160 | 169 | */ |
| 161 | - public function tag_subscriber() { | |
| 170 | + public function subscriber_authentication_send_code() { | |
| 162 | 171 | |
| 163 | - // Check nonce. | |
| 164 | - check_ajax_referer( 'convertkit', 'convertkit_nonce' ); | |
| 172 | + // Load Restrict Content class. | |
| 173 | + $output_restrict_content = WP_ConvertKit()->get_class( 'output_restrict_content' ); | |
| 165 | 174 | |
| 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 | + // Run subscriber authentication. | |
| 176 | + $output_restrict_content->maybe_run_subscriber_authentication(); | |
| 175 | 177 | |
| 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' ) ); | |
| 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 ); | |
| 179 | 184 | } |
| 180 | - if ( empty( $tag_id ) ) { | |
| 181 | - wp_send_json_error( __( 'ConvertKit: Required parameter `tag` empty in AJAX request.', 'convertkit' ) ); | |
| 182 | - } | |
| 183 | 185 | |
| 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 | - } | |
| 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 ); | |
| 189 | 191 | |
| 190 | - // Initialize the API. | |
| 191 | - $api = new ConvertKit_API( $settings->get_api_key(), $settings->get_api_secret(), $settings->debug_enabled() ); | |
| 192 | + } | |
| 192 | 193 | |
| 193 | - // Get subscriber's email address by subscriber ID. | |
| 194 | - $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() { | |
| 195 | 202 | |
| 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 | - } | |
| 203 | + // Load Restrict Content class. | |
| 204 | + $output_restrict_content = WP_ConvertKit()->get_class( 'output_restrict_content' ); | |
| 200 | 205 | |
| 201 | - // Extract the subscriber's email. | |
| 202 | - $email = $subscriber['email_address']; | |
| 206 | + // Run subscriber authentication. | |
| 207 | + $output_restrict_content->maybe_run_subscriber_verification(); | |
| 203 | 208 | |
| 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 ); | |
| 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 ); | |
| 214 | 215 | } |
| 215 | 216 | |
| 216 | - 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 ) ); | |
| 217 | 220 | |
| 218 | 221 | } |
| 219 | 222 | |
| 220 | 223 | } |