| @@ -66,9 +66,9 @@ | ||
| 66 | 66 | * @since 1.9.6 |
| 67 | 67 | */ |
| 68 | 68 | public function __construct() { |
| 69 | 69 | |
| 70 | - add_action( 'rest_api_init', array( $this, 'register_routes' ) ); | |
| 70 | + add_action( 'init', array( $this, 'get_subscriber_id_from_request' ) ); | |
| 71 | 71 | add_action( 'wp', array( $this, 'maybe_tag_subscriber' ) ); |
| 72 | 72 | add_action( 'template_redirect', array( $this, 'output_form' ) ); |
| 73 | 73 | add_action( 'template_redirect', array( $this, 'page_takeover' ) ); |
| 74 | 74 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| @@ -80,64 +80,8 @@ | ||
| 80 | 80 | |
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | /** |
| 84 | - * Register REST API routes. | |
| 85 | - * | |
| 86 | - * @since 3.1.7 | |
| 87 | - */ | |
| 88 | - public function register_routes() { | |
| 89 | - | |
| 90 | - // Register route to store the Kit subscriber's email's ID in a cookie. | |
| 91 | - register_rest_route( | |
| 92 | - 'kit/v1', | |
| 93 | - '/subscriber/store-email-as-id-in-cookie', | |
| 94 | - array( | |
| 95 | - 'methods' => WP_REST_Server::CREATABLE, | |
| 96 | - 'args' => array( | |
| 97 | - // Email: Validate email is included in the request, a valid email address | |
| 98 | - // and sanitize the email address. | |
| 99 | - 'email' => array( | |
| 100 | - 'required' => true, | |
| 101 | - 'validate_callback' => function ( $param ) { | |
| 102 | - | |
| 103 | - return is_string( $param ) && is_email( $param ); | |
| 104 | - | |
| 105 | - }, | |
| 106 | - 'sanitize_callback' => 'sanitize_email', | |
| 107 | - ), | |
| 108 | - ), | |
| 109 | - 'callback' => function ( $request ) { | |
| 110 | - | |
| 111 | - // Get email address. | |
| 112 | - $email = $request->get_param( 'email' ); | |
| 113 | - | |
| 114 | - // Get subscriber ID. | |
| 115 | - $subscriber = new ConvertKit_Subscriber(); | |
| 116 | - $subscriber_id = $subscriber->validate_and_store_subscriber_email( $email ); | |
| 117 | - | |
| 118 | - // Bail if an error occurred i.e. API hasn't been configured. | |
| 119 | - if ( is_wp_error( $subscriber_id ) ) { | |
| 120 | - return rest_ensure_response( $subscriber_id ); | |
| 121 | - } | |
| 122 | - | |
| 123 | - // Return the subscriber ID. | |
| 124 | - return rest_ensure_response( | |
| 125 | - array( | |
| 126 | - 'id' => $subscriber_id, | |
| 127 | - ) | |
| 128 | - ); | |
| 129 | - | |
| 130 | - }, | |
| 131 | - | |
| 132 | - // No authentication required, as this is on the frontend site. | |
| 133 | - 'permission_callback' => '__return_true', | |
| 134 | - ) | |
| 135 | - ); | |
| 136 | - | |
| 137 | - } | |
| 138 | - | |
| 139 | - /** | |
| 140 | 84 | * Tags the subscriber, if: |
| 141 | 85 | * - a subscriber ID exists in the cookie or URL, |
| 142 | 86 | * - the WordPress Page has the "Add a Tag" setting specified |
| 143 | 87 | * |
| @@ -144,8 +88,13 @@ | ||
| 144 | 88 | * @since 2.4.9.1 |
| 145 | 89 | */ |
| 146 | 90 | public function maybe_tag_subscriber() { |
| 147 | 91 | |
| 92 | + // Bail if no subscriber ID detected. | |
| 93 | + if ( ! $this->subscriber_id ) { | |
| 94 | + return; | |
| 95 | + } | |
| 96 | + | |
| 148 | 97 | // Bail if not a singular Post Type supported by ConvertKit. |
| 149 | 98 | if ( ! is_singular( convertkit_get_supported_post_types() ) ) { |
| 150 | 99 | return; |
| 151 | 100 | } |
| @@ -177,16 +126,8 @@ | ||
| 177 | 126 | if ( ! $this->post_settings->has_tag() ) { |
| 178 | 127 | return; |
| 179 | 128 | } |
| 180 | 129 | |
| 181 | - // Get subscriber ID from URL or cookie. | |
| 182 | - $this->get_subscriber_id_from_request(); | |
| 183 | - | |
| 184 | - // Bail if no subscriber ID detected. | |
| 185 | - if ( ! $this->subscriber_id ) { | |
| 186 | - return; | |
| 187 | - } | |
| 188 | - | |
| 189 | 130 | // Initialize the API. |
| 190 | 131 | $api = new ConvertKit_API_V4( |
| 191 | 132 | CONVERTKIT_OAUTH_CLIENT_ID, |
| 192 | 133 | CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI, |
| @@ -201,9 +142,9 @@ | ||
| 201 | 142 | // Fetch the underlying subscriber ID for the tag_subscriber() method. |
| 202 | 143 | if ( ! is_numeric( $this->subscriber_id ) ) { |
| 203 | 144 | $result = $api->profile( $this->subscriber_id ); |
| 204 | 145 | |
| 205 | - // If an error occurred, the subscriber ID is invalid. | |
| 146 | + // If an error occured, the subscriber ID is invalid. | |
| 206 | 147 | if ( is_wp_error( $result ) ) { |
| 207 | 148 | return; |
| 208 | 149 | } |
| 209 | 150 | |
| @@ -291,9 +232,9 @@ | ||
| 291 | 232 | |
| 292 | 233 | // Get Landing Page. |
| 293 | 234 | $landing_page = $this->landing_pages->get_html( $this->post_settings->get_landing_page() ); |
| 294 | 235 | |
| 295 | - // Bail if an error occurred. | |
| 236 | + // Bail if an error occured. | |
| 296 | 237 | if ( is_wp_error( $landing_page ) ) { |
| 297 | 238 | return; |
| 298 | 239 | } |
| 299 | 240 | |
| @@ -371,9 +312,9 @@ | ||
| 371 | 312 | |
| 372 | 313 | // Get Form HTML. |
| 373 | 314 | $form = $this->forms->get_html( $form_id, $post_id ); |
| 374 | 315 | |
| 375 | - // If an error occurred, it could be because the specified Form ID for the Post either: | |
| 316 | + // If an error occured, it could be because the specified Form ID for the Post either: | |
| 376 | 317 | // - belongs to another ConvertKit account (i.e. API credentials were changed in the Plugin, but this Post's specified Form was not changed), or |
| 377 | 318 | // - the form was deleted from the ConvertKit account. |
| 378 | 319 | // Attempt to fallback to the default form for this Post Type. |
| 379 | 320 | if ( is_wp_error( $form ) ) { |
| @@ -395,9 +336,9 @@ | ||
| 395 | 336 | |
| 396 | 337 | // Get Form HTML. |
| 397 | 338 | $form = $this->forms->get_html( $form_id, $post_id ); |
| 398 | 339 | |
| 399 | - // If an error occurred again, the default form doesn't exist in this ConvertKit account. | |
| 340 | + // If an error occured again, the default form doesn't exist in this ConvertKit account. | |
| 400 | 341 | // Just return the Post Content, unedited. |
| 401 | 342 | if ( is_wp_error( $form ) ) { |
| 402 | 343 | if ( $this->settings->debug_enabled() ) { |
| 403 | 344 | $content .= '<!-- Kit append_form_to_content(): Default Form: ' . $form->get_error_message() . ' -->'; |
| @@ -505,25 +446,16 @@ | ||
| 505 | 446 | if ( is_null( $element_node ) ) { |
| 506 | 447 | return $content . $form; |
| 507 | 448 | } |
| 508 | 449 | |
| 509 | - // Load the form into the parser. | |
| 510 | - $form_parser = new ConvertKit_HTML_Parser( $form, LIBXML_HTML_NODEFDTD ); | |
| 511 | - $form_body = $form_parser->html->getElementsByTagName( 'body' )->item( 0 ); | |
| 450 | + // Create new element for the Form. | |
| 451 | + $form_node = new DOMDocument(); | |
| 452 | + $form_node->loadHTML( $form, LIBXML_HTML_NODEFDTD ); | |
| 512 | 453 | |
| 513 | - // Collect nodes first to avoid live NodeList mutation issues. | |
| 514 | - $nodes_to_insert = array(); | |
| 515 | - foreach ( $form_body->childNodes as $child ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase | |
| 516 | - $nodes_to_insert[] = $parser->html->importNode( $child, true ); | |
| 517 | - } | |
| 454 | + // Append the form to the specific element. | |
| 455 | + $element_node->parentNode->insertBefore( $parser->html->importNode( $form_node->documentElement, true ), $element_node->nextSibling ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase | |
| 518 | 456 | |
| 519 | - // Inject the form node(s) after the element node e.g. after the paragraph, heading etc. | |
| 520 | - $next_sibling = $element_node->nextSibling; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase | |
| 521 | - foreach ( $nodes_to_insert as $node ) { | |
| 522 | - $element_node->parentNode->insertBefore( $node, $element_node->nextSibling ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase | |
| 523 | - } | |
| 524 | - | |
| 525 | - // Return modified HTML string. | |
| 457 | + // Fetch HTML string. | |
| 526 | 458 | return $parser->get_body_html(); |
| 527 | 459 | |
| 528 | 460 | } |
| 529 | 461 | |
| @@ -811,23 +743,30 @@ | ||
| 811 | 743 | if ( $settings->scripts_disabled() ) { |
| 812 | 744 | return; |
| 813 | 745 | } |
| 814 | 746 | |
| 815 | - // Enqueue frontend JS. | |
| 816 | - convertkit_enqueue_frontend_js(); | |
| 817 | - | |
| 818 | - // Define variables. | |
| 747 | + // Register scripts that we might use. | |
| 748 | + wp_register_script( | |
| 749 | + 'convertkit-js', | |
| 750 | + CONVERTKIT_PLUGIN_URL . 'resources/frontend/js/convertkit.js', | |
| 751 | + array(), | |
| 752 | + CONVERTKIT_PLUGIN_VERSION, | |
| 753 | + true | |
| 754 | + ); | |
| 819 | 755 | wp_localize_script( |
| 820 | 756 | 'convertkit-js', |
| 821 | 757 | 'convertkit', |
| 822 | 758 | array( |
| 823 | - 'ajaxurl' => rest_url( 'kit/v1/subscriber/store-email-as-id-in-cookie' ), | |
| 759 | + 'ajaxurl' => admin_url( 'admin-ajax.php' ), | |
| 824 | 760 | 'debug' => $settings->debug_enabled(), |
| 825 | - 'nonce' => wp_create_nonce( 'wp_rest' ), | |
| 761 | + 'nonce' => wp_create_nonce( 'convertkit' ), | |
| 826 | 762 | 'subscriber_id' => $this->subscriber_id, |
| 827 | 763 | ) |
| 828 | 764 | ); |
| 829 | 765 | |
| 766 | + // Enqueue. | |
| 767 | + wp_enqueue_script( 'convertkit-js' ); | |
| 768 | + | |
| 830 | 769 | } |
| 831 | 770 | |
| 832 | 771 | /** |
| 833 | 772 | * Gets the subscriber ID from the request (either the cookie or the URL). |
| @@ -839,9 +778,9 @@ | ||
| 839 | 778 | // Use ConvertKit_Subscriber class to fetch and validate the subscriber ID. |
| 840 | 779 | $subscriber = new ConvertKit_Subscriber(); |
| 841 | 780 | $subscriber_id = $subscriber->get_subscriber_id(); |
| 842 | 781 | |
| 843 | - // If an error occurred, the subscriber ID in the request/cookie is not a valid subscriber. | |
| 782 | + // If an error occured, the subscriber ID in the request/cookie is not a valid subscriber. | |
| 844 | 783 | if ( is_wp_error( $subscriber_id ) ) { |
| 845 | 784 | return; |
| 846 | 785 | } |
| 847 | 786 | |