| @@ -67,8 +67,9 @@ | ||
| 67 | 67 | */ |
| 68 | 68 | public function __construct() { |
| 69 | 69 | |
| 70 | 70 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 71 | + add_action( 'init', array( $this, 'get_subscriber_id_from_request' ) ); | |
| 71 | 72 | add_action( 'wp', array( $this, 'maybe_tag_subscriber' ) ); |
| 72 | 73 | add_action( 'template_redirect', array( $this, 'output_form' ) ); |
| 73 | 74 | add_action( 'template_redirect', array( $this, 'page_takeover' ) ); |
| 74 | 75 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| @@ -114,9 +115,9 @@ | ||
| 114 | 115 | // Get subscriber ID. |
| 115 | 116 | $subscriber = new ConvertKit_Subscriber(); |
| 116 | 117 | $subscriber_id = $subscriber->validate_and_store_subscriber_email( $email ); |
| 117 | 118 | |
| 118 | - // Bail if an error occurred i.e. API hasn't been configured. | |
| 119 | + // Bail if an error occured i.e. API hasn't been configured. | |
| 119 | 120 | if ( is_wp_error( $subscriber_id ) ) { |
| 120 | 121 | return rest_ensure_response( $subscriber_id ); |
| 121 | 122 | } |
| 122 | 123 | |
| @@ -144,8 +145,13 @@ | ||
| 144 | 145 | * @since 2.4.9.1 |
| 145 | 146 | */ |
| 146 | 147 | public function maybe_tag_subscriber() { |
| 147 | 148 | |
| 149 | + // Bail if no subscriber ID detected. | |
| 150 | + if ( ! $this->subscriber_id ) { | |
| 151 | + return; | |
| 152 | + } | |
| 153 | + | |
| 148 | 154 | // Bail if not a singular Post Type supported by ConvertKit. |
| 149 | 155 | if ( ! is_singular( convertkit_get_supported_post_types() ) ) { |
| 150 | 156 | return; |
| 151 | 157 | } |
| @@ -177,16 +183,8 @@ | ||
| 177 | 183 | if ( ! $this->post_settings->has_tag() ) { |
| 178 | 184 | return; |
| 179 | 185 | } |
| 180 | 186 | |
| 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 | 187 | // Initialize the API. |
| 190 | 188 | $api = new ConvertKit_API_V4( |
| 191 | 189 | CONVERTKIT_OAUTH_CLIENT_ID, |
| 192 | 190 | CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI, |
| @@ -201,9 +199,9 @@ | ||
| 201 | 199 | // Fetch the underlying subscriber ID for the tag_subscriber() method. |
| 202 | 200 | if ( ! is_numeric( $this->subscriber_id ) ) { |
| 203 | 201 | $result = $api->profile( $this->subscriber_id ); |
| 204 | 202 | |
| 205 | - // If an error occurred, the subscriber ID is invalid. | |
| 203 | + // If an error occured, the subscriber ID is invalid. | |
| 206 | 204 | if ( is_wp_error( $result ) ) { |
| 207 | 205 | return; |
| 208 | 206 | } |
| 209 | 207 | |
| @@ -291,9 +289,9 @@ | ||
| 291 | 289 | |
| 292 | 290 | // Get Landing Page. |
| 293 | 291 | $landing_page = $this->landing_pages->get_html( $this->post_settings->get_landing_page() ); |
| 294 | 292 | |
| 295 | - // Bail if an error occurred. | |
| 293 | + // Bail if an error occured. | |
| 296 | 294 | if ( is_wp_error( $landing_page ) ) { |
| 297 | 295 | return; |
| 298 | 296 | } |
| 299 | 297 | |
| @@ -371,9 +369,9 @@ | ||
| 371 | 369 | |
| 372 | 370 | // Get Form HTML. |
| 373 | 371 | $form = $this->forms->get_html( $form_id, $post_id ); |
| 374 | 372 | |
| 375 | - // If an error occurred, it could be because the specified Form ID for the Post either: | |
| 373 | + // If an error occured, it could be because the specified Form ID for the Post either: | |
| 376 | 374 | // - 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 | 375 | // - the form was deleted from the ConvertKit account. |
| 378 | 376 | // Attempt to fallback to the default form for this Post Type. |
| 379 | 377 | if ( is_wp_error( $form ) ) { |
| @@ -395,9 +393,9 @@ | ||
| 395 | 393 | |
| 396 | 394 | // Get Form HTML. |
| 397 | 395 | $form = $this->forms->get_html( $form_id, $post_id ); |
| 398 | 396 | |
| 399 | - // If an error occurred again, the default form doesn't exist in this ConvertKit account. | |
| 397 | + // If an error occured again, the default form doesn't exist in this ConvertKit account. | |
| 400 | 398 | // Just return the Post Content, unedited. |
| 401 | 399 | if ( is_wp_error( $form ) ) { |
| 402 | 400 | if ( $this->settings->debug_enabled() ) { |
| 403 | 401 | $content .= '<!-- Kit append_form_to_content(): Default Form: ' . $form->get_error_message() . ' -->'; |
| @@ -505,25 +503,16 @@ | ||
| 505 | 503 | if ( is_null( $element_node ) ) { |
| 506 | 504 | return $content . $form; |
| 507 | 505 | } |
| 508 | 506 | |
| 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 ); | |
| 507 | + // Create new element for the Form. | |
| 508 | + $form_node = new DOMDocument(); | |
| 509 | + $form_node->loadHTML( $form, LIBXML_HTML_NODEFDTD ); | |
| 512 | 510 | |
| 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 | - } | |
| 511 | + // Append the form to the specific element. | |
| 512 | + $element_node->parentNode->insertBefore( $parser->html->importNode( $form_node->documentElement, true ), $element_node->nextSibling ); // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase | |
| 518 | 513 | |
| 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. | |
| 514 | + // Fetch HTML string. | |
| 526 | 515 | return $parser->get_body_html(); |
| 527 | 516 | |
| 528 | 517 | } |
| 529 | 518 | |
| @@ -811,12 +800,16 @@ | ||
| 811 | 800 | if ( $settings->scripts_disabled() ) { |
| 812 | 801 | return; |
| 813 | 802 | } |
| 814 | 803 | |
| 815 | - // Enqueue frontend JS. | |
| 816 | - convertkit_enqueue_frontend_js(); | |
| 817 | - | |
| 818 | - // Define variables. | |
| 804 | + // Register scripts that we might use. | |
| 805 | + wp_register_script( | |
| 806 | + 'convertkit-js', | |
| 807 | + CONVERTKIT_PLUGIN_URL . 'resources/frontend/js/convertkit.js', | |
| 808 | + array(), | |
| 809 | + CONVERTKIT_PLUGIN_VERSION, | |
| 810 | + true | |
| 811 | + ); | |
| 819 | 812 | wp_localize_script( |
| 820 | 813 | 'convertkit-js', |
| 821 | 814 | 'convertkit', |
| 822 | 815 | array( |
| @@ -826,8 +819,11 @@ | ||
| 826 | 819 | 'subscriber_id' => $this->subscriber_id, |
| 827 | 820 | ) |
| 828 | 821 | ); |
| 829 | 822 | |
| 823 | + // Enqueue. | |
| 824 | + wp_enqueue_script( 'convertkit-js' ); | |
| 825 | + | |
| 830 | 826 | } |
| 831 | 827 | |
| 832 | 828 | /** |
| 833 | 829 | * Gets the subscriber ID from the request (either the cookie or the URL). |
| @@ -839,9 +835,9 @@ | ||
| 839 | 835 | // Use ConvertKit_Subscriber class to fetch and validate the subscriber ID. |
| 840 | 836 | $subscriber = new ConvertKit_Subscriber(); |
| 841 | 837 | $subscriber_id = $subscriber->get_subscriber_id(); |
| 842 | 838 | |
| 843 | - // If an error occurred, the subscriber ID in the request/cookie is not a valid subscriber. | |
| 839 | + // If an error occured, the subscriber ID in the request/cookie is not a valid subscriber. | |
| 844 | 840 | if ( is_wp_error( $subscriber_id ) ) { |
| 845 | 841 | return; |
| 846 | 842 | } |
| 847 | 843 | |