| @@ -66,8 +66,9 @@ | ||
| 66 | 66 | * @since 1.9.6 |
| 67 | 67 | */ |
| 68 | 68 | public function __construct() { |
| 69 | 69 | |
| 70 | + add_action( 'init', array( $this, 'get_subscriber_id_from_request' ) ); | |
| 70 | 71 | add_action( 'wp', array( $this, 'maybe_tag_subscriber' ) ); |
| 71 | 72 | add_action( 'template_redirect', array( $this, 'output_form' ) ); |
| 72 | 73 | add_action( 'template_redirect', array( $this, 'page_takeover' ) ); |
| 73 | 74 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| @@ -87,8 +88,13 @@ | ||
| 87 | 88 | * @since 2.4.9.1 |
| 88 | 89 | */ |
| 89 | 90 | public function maybe_tag_subscriber() { |
| 90 | 91 | |
| 92 | + // Bail if no subscriber ID detected. | |
| 93 | + if ( ! $this->subscriber_id ) { | |
| 94 | + return; | |
| 95 | + } | |
| 96 | + | |
| 91 | 97 | // Bail if not a singular Post Type supported by ConvertKit. |
| 92 | 98 | if ( ! is_singular( convertkit_get_supported_post_types() ) ) { |
| 93 | 99 | return; |
| 94 | 100 | } |
| @@ -120,16 +126,8 @@ | ||
| 120 | 126 | if ( ! $this->post_settings->has_tag() ) { |
| 121 | 127 | return; |
| 122 | 128 | } |
| 123 | 129 | |
| 124 | - // Get subscriber ID from URL or cookie. | |
| 125 | - $this->get_subscriber_id_from_request(); | |
| 126 | - | |
| 127 | - // Bail if no subscriber ID detected. | |
| 128 | - if ( ! $this->subscriber_id ) { | |
| 129 | - return; | |
| 130 | - } | |
| 131 | - | |
| 132 | 130 | // Initialize the API. |
| 133 | 131 | $api = new ConvertKit_API_V4( |
| 134 | 132 | CONVERTKIT_OAUTH_CLIENT_ID, |
| 135 | 133 | CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI, |
| @@ -144,9 +142,9 @@ | ||
| 144 | 142 | // Fetch the underlying subscriber ID for the tag_subscriber() method. |
| 145 | 143 | if ( ! is_numeric( $this->subscriber_id ) ) { |
| 146 | 144 | $result = $api->profile( $this->subscriber_id ); |
| 147 | 145 | |
| 148 | - // If an error occurred, the subscriber ID is invalid. | |
| 146 | + // If an error occured, the subscriber ID is invalid. | |
| 149 | 147 | if ( is_wp_error( $result ) ) { |
| 150 | 148 | return; |
| 151 | 149 | } |
| 152 | 150 | |
| @@ -234,9 +232,9 @@ | ||
| 234 | 232 | |
| 235 | 233 | // Get Landing Page. |
| 236 | 234 | $landing_page = $this->landing_pages->get_html( $this->post_settings->get_landing_page() ); |
| 237 | 235 | |
| 238 | - // Bail if an error occurred. | |
| 236 | + // Bail if an error occured. | |
| 239 | 237 | if ( is_wp_error( $landing_page ) ) { |
| 240 | 238 | return; |
| 241 | 239 | } |
| 242 | 240 | |
| @@ -314,9 +312,9 @@ | ||
| 314 | 312 | |
| 315 | 313 | // Get Form HTML. |
| 316 | 314 | $form = $this->forms->get_html( $form_id, $post_id ); |
| 317 | 315 | |
| 318 | - // 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: | |
| 319 | 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 |
| 320 | 318 | // - the form was deleted from the ConvertKit account. |
| 321 | 319 | // Attempt to fallback to the default form for this Post Type. |
| 322 | 320 | if ( is_wp_error( $form ) ) { |
| @@ -338,9 +336,9 @@ | ||
| 338 | 336 | |
| 339 | 337 | // Get Form HTML. |
| 340 | 338 | $form = $this->forms->get_html( $form_id, $post_id ); |
| 341 | 339 | |
| 342 | - // 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. | |
| 343 | 341 | // Just return the Post Content, unedited. |
| 344 | 342 | if ( is_wp_error( $form ) ) { |
| 345 | 343 | if ( $this->settings->debug_enabled() ) { |
| 346 | 344 | $content .= '<!-- Kit append_form_to_content(): Default Form: ' . $form->get_error_message() . ' -->'; |
| @@ -448,25 +446,16 @@ | ||
| 448 | 446 | if ( is_null( $element_node ) ) { |
| 449 | 447 | return $content . $form; |
| 450 | 448 | } |
| 451 | 449 | |
| 452 | - // Load the form into the parser. | |
| 453 | - $form_parser = new ConvertKit_HTML_Parser( $form, LIBXML_HTML_NODEFDTD ); | |
| 454 | - $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 ); | |
| 455 | 453 | |
| 456 | - // Collect nodes first to avoid live NodeList mutation issues. | |
| 457 | - $nodes_to_insert = array(); | |
| 458 | - foreach ( $form_body->childNodes as $child ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase | |
| 459 | - $nodes_to_insert[] = $parser->html->importNode( $child, true ); | |
| 460 | - } | |
| 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 | |
| 461 | 456 | |
| 462 | - // Inject the form node(s) after the element node e.g. after the paragraph, heading etc. | |
| 463 | - $next_sibling = $element_node->nextSibling; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase | |
| 464 | - foreach ( $nodes_to_insert as $node ) { | |
| 465 | - $element_node->parentNode->insertBefore( $node, $element_node->nextSibling ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase | |
| 466 | - } | |
| 467 | - | |
| 468 | - // Return modified HTML string. | |
| 457 | + // Fetch HTML string. | |
| 469 | 458 | return $parser->get_body_html(); |
| 470 | 459 | |
| 471 | 460 | } |
| 472 | 461 | |
| @@ -754,21 +743,30 @@ | ||
| 754 | 743 | if ( $settings->scripts_disabled() ) { |
| 755 | 744 | return; |
| 756 | 745 | } |
| 757 | 746 | |
| 758 | - // Enqueue frontend JS. | |
| 759 | - convertkit_enqueue_frontend_js(); | |
| 760 | - | |
| 761 | - // 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 | + ); | |
| 762 | 755 | wp_localize_script( |
| 763 | 756 | 'convertkit-js', |
| 764 | 757 | 'convertkit', |
| 765 | 758 | array( |
| 759 | + 'ajaxurl' => admin_url( 'admin-ajax.php' ), | |
| 766 | 760 | 'debug' => $settings->debug_enabled(), |
| 761 | + 'nonce' => wp_create_nonce( 'convertkit' ), | |
| 767 | 762 | 'subscriber_id' => $this->subscriber_id, |
| 768 | 763 | ) |
| 769 | 764 | ); |
| 770 | 765 | |
| 766 | + // Enqueue. | |
| 767 | + wp_enqueue_script( 'convertkit-js' ); | |
| 768 | + | |
| 771 | 769 | } |
| 772 | 770 | |
| 773 | 771 | /** |
| 774 | 772 | * Gets the subscriber ID from the request (either the cookie or the URL). |
| @@ -780,9 +778,9 @@ | ||
| 780 | 778 | // Use ConvertKit_Subscriber class to fetch and validate the subscriber ID. |
| 781 | 779 | $subscriber = new ConvertKit_Subscriber(); |
| 782 | 780 | $subscriber_id = $subscriber->get_subscriber_id(); |
| 783 | 781 | |
| 784 | - // 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. | |
| 785 | 783 | if ( is_wp_error( $subscriber_id ) ) { |
| 786 | 784 | return; |
| 787 | 785 | } |
| 788 | 786 | |