| @@ -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 occured 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 | } |
| @@ -162,10 +111,10 @@ | ||
| 162 | 111 | if ( ! $this->settings ) { |
| 163 | 112 | $this->settings = new ConvertKit_Settings(); |
| 164 | 113 | } |
| 165 | 114 | |
| 166 | - // Bail if the API hasn't been configured. | |
| 167 | - if ( ! $this->settings->has_access_and_refresh_token() ) { | |
| 115 | + // Bail if the API if an API Key and Secret is not defined. | |
| 116 | + if ( ! $this->settings->has_api_key_and_secret() ) { | |
| 168 | 117 | return; |
| 169 | 118 | } |
| 170 | 119 | |
| 171 | 120 | // Get ConvertKit Post's Settings, if they have not yet been loaded. |
| @@ -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, |
| @@ -195,23 +136,8 @@ | ||
| 195 | 136 | $this->settings->debug_enabled(), |
| 196 | 137 | 'output' |
| 197 | 138 | ); |
| 198 | 139 | |
| 199 | - // If the subscriber ID is not numeric, it's a cryptographically-signed subscriber ID, set | |
| 200 | - // when using the Member Content functionality by Kit's API. | |
| 201 | - // Fetch the underlying subscriber ID for the tag_subscriber() method. | |
| 202 | - if ( ! is_numeric( $this->subscriber_id ) ) { | |
| 203 | - $result = $api->profile( $this->subscriber_id ); | |
| 204 | - | |
| 205 | - // If an error occured, the subscriber ID is invalid. | |
| 206 | - if ( is_wp_error( $result ) ) { | |
| 207 | - return; | |
| 208 | - } | |
| 209 | - | |
| 210 | - // Set the subscriber ID. | |
| 211 | - $this->subscriber_id = $result['id']; | |
| 212 | - } | |
| 213 | - | |
| 214 | 140 | // Tag subscriber. |
| 215 | 141 | $api->tag_subscriber( $this->post_settings->get_tag(), $this->subscriber_id ); |
| 216 | 142 | |
| 217 | 143 | } |
| @@ -322,9 +248,9 @@ | ||
| 322 | 248 | |
| 323 | 249 | } |
| 324 | 250 | |
| 325 | 251 | /** |
| 326 | - * Inserts a form to the singular Page, Post or Custom Post Type's Content. | |
| 252 | + * Appends a form to the singular Page, Post or Custom Post Type's Content. | |
| 327 | 253 | * |
| 328 | 254 | * @param string $content Post Content. |
| 329 | 255 | * @return string Post Content with Form Appended, if applicable |
| 330 | 256 | */ |
| @@ -334,17 +260,10 @@ | ||
| 334 | 260 | if ( ! is_singular( convertkit_get_supported_post_types() ) ) { |
| 335 | 261 | return $content; |
| 336 | 262 | } |
| 337 | 263 | |
| 338 | - // Get Post ID. | |
| 264 | + // Get Post ID and ConvertKit Form ID for the Post. | |
| 339 | 265 | $post_id = get_the_ID(); |
| 340 | - | |
| 341 | - // Bail if Post Type is not supported by Kit. | |
| 342 | - if ( ! in_array( get_post_type( $post_id ), convertkit_get_supported_post_types(), true ) ) { | |
| 343 | - return $content; | |
| 344 | - } | |
| 345 | - | |
| 346 | - // Get Form ID for the Post. | |
| 347 | 266 | $form_id = $this->get_post_form_id( $post_id ); |
| 348 | 267 | |
| 349 | 268 | /** |
| 350 | 269 | * Define the ConvertKit Form ID to display for the given Post ID, |
| @@ -369,9 +288,9 @@ | ||
| 369 | 288 | $this->forms = new ConvertKit_Resource_Forms( 'output_form' ); |
| 370 | 289 | } |
| 371 | 290 | |
| 372 | 291 | // Get Form HTML. |
| 373 | - $form = $this->forms->get_html( $form_id, $post_id ); | |
| 292 | + $form = $this->forms->get_html( $form_id ); | |
| 374 | 293 | |
| 375 | 294 | // If an error occured, it could be because the specified Form ID for the Post either: |
| 376 | 295 | // - 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 | 296 | // - the form was deleted from the ConvertKit account. |
| @@ -377,9 +296,9 @@ | ||
| 377 | 296 | // - the form was deleted from the ConvertKit account. |
| 378 | 297 | // Attempt to fallback to the default form for this Post Type. |
| 379 | 298 | if ( is_wp_error( $form ) ) { |
| 380 | 299 | if ( $this->settings->debug_enabled() ) { |
| 381 | - $content .= '<!-- Kit append_form_to_content(): ' . $form->get_error_message() . ' Attempting fallback to Default Form. -->'; | |
| 300 | + $content .= '<!-- ConvertKit append_form_to_content(): ' . $form->get_error_message() . ' Attempting fallback to Default Form. -->'; | |
| 382 | 301 | } |
| 383 | 302 | |
| 384 | 303 | // Get Default Form ID for this Post's Type. |
| 385 | 304 | $form_id = $this->settings->get_default_form( get_post_type( $post_id ) ); |
| @@ -386,9 +305,9 @@ | ||
| 386 | 305 | |
| 387 | 306 | // If no Default Form is specified, just return the Post Content, unedited. |
| 388 | 307 | if ( ! $form_id ) { |
| 389 | 308 | if ( $this->settings->debug_enabled() ) { |
| 390 | - $content .= '<!-- Kit append_form_to_content(): No Default Form exists as a fallback. -->'; | |
| 309 | + $content .= '<!-- ConvertKit append_form_to_content(): No Default Form exists as a fallback. -->'; | |
| 391 | 310 | } |
| 392 | 311 | |
| 393 | 312 | return $content; |
| 394 | 313 | } |
| @@ -393,15 +312,15 @@ | ||
| 393 | 312 | return $content; |
| 394 | 313 | } |
| 395 | 314 | |
| 396 | 315 | // Get Form HTML. |
| 397 | - $form = $this->forms->get_html( $form_id, $post_id ); | |
| 316 | + $form = $this->forms->get_html( $form_id ); | |
| 398 | 317 | |
| 399 | 318 | // If an error occured again, the default form doesn't exist in this ConvertKit account. |
| 400 | 319 | // Just return the Post Content, unedited. |
| 401 | 320 | if ( is_wp_error( $form ) ) { |
| 402 | 321 | if ( $this->settings->debug_enabled() ) { |
| 403 | - $content .= '<!-- Kit append_form_to_content(): Default Form: ' . $form->get_error_message() . ' -->'; | |
| 322 | + $content .= '<!-- ConvertKit append_form_to_content(): Default Form: ' . $form->get_error_message() . ' -->'; | |
| 404 | 323 | } |
| 405 | 324 | |
| 406 | 325 | return $content; |
| 407 | 326 | } |
| @@ -406,70 +325,23 @@ | ||
| 406 | 325 | return $content; |
| 407 | 326 | } |
| 408 | 327 | } |
| 409 | 328 | |
| 410 | - // If the Form HTML is empty, it's a modal form that has been set to load in the footer of the site. | |
| 411 | - // We don't need to append anything to the content. | |
| 412 | - if ( empty( $form ) ) { | |
| 413 | - if ( $this->settings->debug_enabled() ) { | |
| 414 | - $content .= '<!-- Kit append_form_to_content(): Form is non-inline, appended to footer. -->'; | |
| 415 | - } | |
| 416 | - | |
| 417 | - return $content; | |
| 418 | - } | |
| 419 | - | |
| 420 | 329 | // If here, we have a ConvertKit Form. |
| 421 | - // Append form to Post's Content, based on the position setting. | |
| 422 | - $form_position = $this->settings->get_default_form_position( get_post_type( $post_id ) ); | |
| 330 | + // Append form to Post's Content. | |
| 331 | + $content = $content .= $form; | |
| 423 | 332 | |
| 424 | - if ( $this->settings->debug_enabled() ) { | |
| 425 | - $content .= '<!-- Kit append_form_to_content(): Form Position: ' . esc_html( $form_position ) . ' -->'; | |
| 426 | - } | |
| 427 | - | |
| 428 | - switch ( $form_position ) { | |
| 429 | - case 'before_after_content': | |
| 430 | - $content = $form . $content . $form; | |
| 431 | - break; | |
| 432 | - | |
| 433 | - case 'before_content': | |
| 434 | - $content = $form . $content; | |
| 435 | - break; | |
| 436 | - | |
| 437 | - case 'after_element': | |
| 438 | - $element = $this->settings->get_default_form_position_element( get_post_type( $post_id ) ); | |
| 439 | - $index = $this->settings->get_default_form_position_element_index( get_post_type( $post_id ) ); | |
| 440 | - | |
| 441 | - // Check if DOMDocument is installed. | |
| 442 | - // It should be installed as mosts hosts include php-dom and php-xml modules. | |
| 443 | - // If not, fallback to using preg_match_all(), which is less reliable. | |
| 444 | - if ( ! class_exists( 'DOMDocument' ) ) { | |
| 445 | - $content = $this->inject_form_after_element_fallback( $content, $element, $index, $form ); | |
| 446 | - break; | |
| 447 | - } | |
| 448 | - | |
| 449 | - // Use DOMDocument. | |
| 450 | - $content = $this->inject_form_after_element( $content, $element, $index, $form ); | |
| 451 | - break; | |
| 452 | - | |
| 453 | - case 'after_content': | |
| 454 | - default: | |
| 455 | - // Default behaviour < 2.5.8 was to append the Form after the content. | |
| 456 | - $content .= $form; | |
| 457 | - break; | |
| 458 | - } | |
| 459 | - | |
| 460 | 333 | /** |
| 461 | 334 | * Filter the Post's Content, which includes a ConvertKit Form, immediately before it is output. |
| 462 | 335 | * |
| 463 | 336 | * @since 1.9.6 |
| 464 | 337 | * |
| 465 | - * @param string $content Post Content | |
| 466 | - * @param string $form ConvertKit Form HTML | |
| 467 | - * @param int $post_id Post ID | |
| 468 | - * @param int $form_id ConvertKit Form ID | |
| 469 | - * @param string $form_position Form Position setting for the Post's Type. | |
| 338 | + * @param string $content Post Content | |
| 339 | + * @param string $form ConvertKit Form HTML | |
| 340 | + * @param int $post_id Post ID | |
| 341 | + * @param int $form_id ConvertKit Form ID | |
| 470 | 342 | */ |
| 471 | - $content = apply_filters( 'convertkit_frontend_append_form', $content, $form, $post_id, $form_id, $form_position ); | |
| 343 | + $content = apply_filters( 'convertkit_frontend_append_form', $content, $form, $post_id, $form_id ); | |
| 472 | 344 | |
| 473 | 345 | return $content; |
| 474 | 346 | |
| 475 | 347 | } |
| @@ -474,119 +346,8 @@ | ||
| 474 | 346 | |
| 475 | 347 | } |
| 476 | 348 | |
| 477 | 349 | /** |
| 478 | - * Injects the form after the given element and index, using DOMDocument. | |
| 479 | - * | |
| 480 | - * @since 2.6.2 | |
| 481 | - * | |
| 482 | - * @param string $content Page / Post Content. | |
| 483 | - * @param string $tag HTML tag to insert form after. | |
| 484 | - * @param int $index Number of $tag elements to find before inserting form. | |
| 485 | - * @param string $form Form HTML to inject. | |
| 486 | - * @return string | |
| 487 | - */ | |
| 488 | - private function inject_form_after_element( $content, $tag, $index, $form ) { | |
| 489 | - | |
| 490 | - // If the form is empty, don't inject anything. | |
| 491 | - if ( empty( $form ) ) { | |
| 492 | - return $content; | |
| 493 | - } | |
| 494 | - | |
| 495 | - // Load the content into the parser. | |
| 496 | - $parser = new ConvertKit_HTML_Parser( $content, LIBXML_HTML_NODEFDTD ); | |
| 497 | - | |
| 498 | - // Find the element to append the form to. | |
| 499 | - // item() is a zero based index. | |
| 500 | - $element_node = $parser->html->getElementsByTagName( $tag )->item( $index - 1 ); | |
| 501 | - | |
| 502 | - // If the element could not be found, either the number of elements by tag name is less | |
| 503 | - // than the requested position the form be inserted in, or no element exists. | |
| 504 | - // Append the form to the original content and return. | |
| 505 | - if ( is_null( $element_node ) ) { | |
| 506 | - return $content . $form; | |
| 507 | - } | |
| 508 | - | |
| 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 ); | |
| 512 | - | |
| 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 | - } | |
| 518 | - | |
| 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. | |
| 526 | - return $parser->get_body_html(); | |
| 527 | - | |
| 528 | - } | |
| 529 | - | |
| 530 | - /** | |
| 531 | - * Injects the form after the given element and index, using preg_match_all(). | |
| 532 | - * This is less reliable than DOMDocument, and is called if DOMDocument is | |
| 533 | - * not installed on the server. | |
| 534 | - * | |
| 535 | - * @since 2.6.2 | |
| 536 | - * | |
| 537 | - * @param string $content Page / Post Content. | |
| 538 | - * @param string $tag HTML tag to insert form after. | |
| 539 | - * @param int $index Number of $tag elements to find before inserting form. | |
| 540 | - * @param string $form Form HTML to inject. | |
| 541 | - * @return string | |
| 542 | - */ | |
| 543 | - private function inject_form_after_element_fallback( $content, $tag, $index, $form ) { | |
| 544 | - | |
| 545 | - // If the form is empty, don't inject anything. | |
| 546 | - if ( empty( $form ) ) { | |
| 547 | - return $content; | |
| 548 | - } | |
| 549 | - | |
| 550 | - // Calculate tag length. | |
| 551 | - $tag_length = ( strlen( $tag ) + 3 ); | |
| 552 | - | |
| 553 | - // Find all closing elements. | |
| 554 | - preg_match_all( '/<\/' . $tag . '>/', $content, $matches ); | |
| 555 | - | |
| 556 | - // If no elements exist, just append the form. | |
| 557 | - if ( count( $matches[0] ) === 0 ) { | |
| 558 | - $content = $content . $form; | |
| 559 | - return $content; | |
| 560 | - } | |
| 561 | - | |
| 562 | - // If the number of elements is less than the index, we don't have enough elements to add the form to. | |
| 563 | - // Just add the form after the content. | |
| 564 | - if ( count( $matches[0] ) <= $index ) { | |
| 565 | - $content = $content . $form; | |
| 566 | - return $content; | |
| 567 | - } | |
| 568 | - | |
| 569 | - // Iterate through the content to find the element at the configured index e.g. find the 4th closing paragraph. | |
| 570 | - $offset = 0; | |
| 571 | - foreach ( $matches[0] as $element_index => $element ) { | |
| 572 | - $position = strpos( $content, $element, $offset ); | |
| 573 | - if ( ( $element_index + 1 ) === $index ) { | |
| 574 | - return substr( $content, 0, $position + 4 ) . $form . substr( $content, $position + 4 ); | |
| 575 | - } | |
| 576 | - | |
| 577 | - // Increment offset. | |
| 578 | - $offset = $position + 1; | |
| 579 | - } | |
| 580 | - | |
| 581 | - // If here, something went wrong. | |
| 582 | - // Just add the form after the content. | |
| 583 | - $content = $content . $form; | |
| 584 | - return $content; | |
| 585 | - | |
| 586 | - } | |
| 587 | - | |
| 588 | - /** | |
| 589 | 350 | * Registers the ConvertKit Form block to before or after the Query Loop block, when viewing a Category archive. |
| 590 | 351 | * |
| 591 | 352 | * See append_form_block_on_category_archive() configures the block to display the applicable category's Form. |
| 592 | 353 | * |
| @@ -752,17 +513,11 @@ | ||
| 752 | 513 | // If a Form ID exists, return it now. |
| 753 | 514 | if ( $term_settings->has_form() ) { |
| 754 | 515 | return $term_settings->get_form(); |
| 755 | 516 | } |
| 756 | - | |
| 757 | - // If the Term specifies that no Form should be used, return false. | |
| 758 | - if ( $term_settings->uses_no_form() ) { | |
| 759 | - return false; | |
| 760 | - } | |
| 761 | 517 | } |
| 762 | 518 | |
| 763 | - // If here, all Terms were set to display the Default Form. | |
| 764 | - // Therefore use the Plugin's Default Form. | |
| 519 | + // If here, use the Plugin's Default Form. | |
| 765 | 520 | return $this->settings->get_default_form( get_post_type( $post_id ) ); |
| 766 | 521 | |
| 767 | 522 | } |
| 768 | 523 | |
| @@ -803,31 +558,47 @@ | ||
| 803 | 558 | * @since 1.9.6 |
| 804 | 559 | */ |
| 805 | 560 | public function enqueue_scripts() { |
| 806 | 561 | |
| 807 | - // Get ConvertKit Settings and Post's Settings. | |
| 808 | - $settings = new ConvertKit_Settings(); | |
| 562 | + // Get Post. | |
| 563 | + $post = get_post(); | |
| 809 | 564 | |
| 810 | - // Bail if the no scripts setting is enabled. | |
| 811 | - if ( $settings->scripts_disabled() ) { | |
| 565 | + // Bail if no Post could be fetched. | |
| 566 | + if ( ! $post ) { | |
| 812 | 567 | return; |
| 813 | 568 | } |
| 814 | 569 | |
| 815 | - // Enqueue frontend JS. | |
| 816 | - convertkit_enqueue_frontend_js(); | |
| 570 | + // Get ConvertKit Settings and Post's Settings. | |
| 571 | + $settings = new ConvertKit_Settings(); | |
| 572 | + $convertkit_post = new ConvertKit_Post( $post->ID ); | |
| 817 | 573 | |
| 818 | - // Define variables. | |
| 574 | + // Register scripts that we might use. | |
| 575 | + wp_register_script( | |
| 576 | + 'convertkit-js', | |
| 577 | + CONVERTKIT_PLUGIN_URL . 'resources/frontend/js/convertkit.js', | |
| 578 | + array(), | |
| 579 | + CONVERTKIT_PLUGIN_VERSION, | |
| 580 | + true | |
| 581 | + ); | |
| 819 | 582 | wp_localize_script( |
| 820 | 583 | 'convertkit-js', |
| 821 | 584 | 'convertkit', |
| 822 | 585 | array( |
| 823 | - 'ajaxurl' => rest_url( 'kit/v1/subscriber/store-email-as-id-in-cookie' ), | |
| 586 | + 'ajaxurl' => admin_url( 'admin-ajax.php' ), | |
| 824 | 587 | 'debug' => $settings->debug_enabled(), |
| 825 | - 'nonce' => wp_create_nonce( 'wp_rest' ), | |
| 588 | + 'nonce' => wp_create_nonce( 'convertkit' ), | |
| 826 | 589 | 'subscriber_id' => $this->subscriber_id, |
| 827 | 590 | ) |
| 828 | 591 | ); |
| 829 | 592 | |
| 593 | + // Bail if the no scripts setting is enabled. | |
| 594 | + if ( $settings->scripts_disabled() ) { | |
| 595 | + return; | |
| 596 | + } | |
| 597 | + | |
| 598 | + // Enqueue. | |
| 599 | + wp_enqueue_script( 'convertkit-js' ); | |
| 600 | + | |
| 830 | 601 | } |
| 831 | 602 | |
| 832 | 603 | /** |
| 833 | 604 | * Gets the subscriber ID from the request (either the cookie or the URL). |
| @@ -849,10 +620,10 @@ | ||
| 849 | 620 | |
| 850 | 621 | } |
| 851 | 622 | |
| 852 | 623 | /** |
| 853 | - * Outputs a non-inline forms if defined in the Plugin's settings > | |
| 854 | - * Default Forms (Site Wide) setting. | |
| 624 | + * Outputs a non-inline form if defined in the Plugin's settings > | |
| 625 | + * Default Non-Inline Form (Global) setting. | |
| 855 | 626 | * |
| 856 | 627 | * @since 2.3.3 |
| 857 | 628 | */ |
| 858 | 629 | public function output_global_non_inline_form() { |
| @@ -866,48 +637,33 @@ | ||
| 866 | 637 | if ( ! $this->settings->has_non_inline_form() ) { |
| 867 | 638 | return; |
| 868 | 639 | } |
| 869 | 640 | |
| 870 | - // Bail if the Page, Post or Custom Post Type's Form setting is set to 'None' | |
| 871 | - // and the Plugin is set to honor this setting. | |
| 872 | - if ( $this->post_settings !== false && $this->post_settings->uses_no_form() && $this->settings->non_inline_form_honor_none_setting() ) { | |
| 641 | + // Get form. | |
| 642 | + $convertkit_forms = new ConvertKit_Resource_Forms(); | |
| 643 | + $form = $convertkit_forms->get_by_id( (int) $this->settings->get_non_inline_form() ); | |
| 644 | + | |
| 645 | + // Bail if the Form doesn't exist (this shouldn't happen, but you never know). | |
| 646 | + if ( ! $form ) { | |
| 873 | 647 | return; |
| 874 | 648 | } |
| 875 | 649 | |
| 876 | - // Determine if the Non-inline Form Limit per Session setting is enabled. | |
| 877 | - $limit_per_session = $this->settings->non_inline_form_limit_per_session(); | |
| 650 | + // Add the form to the scripts array so it is included in the output. | |
| 651 | + add_filter( | |
| 652 | + 'convertkit_output_scripts_footer', | |
| 653 | + function ( $scripts ) use ( $form ) { | |
| 878 | 654 | |
| 879 | - // Get form. | |
| 880 | - $convertkit_forms = new ConvertKit_Resource_Forms(); | |
| 655 | + $scripts[] = array( | |
| 656 | + 'async' => true, | |
| 657 | + 'data-uid' => $form['uid'], | |
| 658 | + 'src' => $form['embed_js'], | |
| 659 | + ); | |
| 881 | 660 | |
| 882 | - // Iterate through forms. | |
| 883 | - foreach ( $this->settings->get_non_inline_form() as $form_id ) { | |
| 884 | - // Get Form. | |
| 885 | - $form = $convertkit_forms->get_by_id( (int) $form_id ); | |
| 661 | + return $scripts; | |
| 886 | 662 | |
| 887 | - // Bail if the Form doesn't exist (this shouldn't happen, but you never know). | |
| 888 | - if ( ! $form ) { | |
| 889 | - continue; | |
| 890 | 663 | } |
| 664 | + ); | |
| 891 | 665 | |
| 892 | - // Add the form to the scripts array so it is included in the output. | |
| 893 | - add_filter( | |
| 894 | - 'convertkit_output_scripts_footer', | |
| 895 | - function ( $scripts ) use ( $form, $limit_per_session ) { | |
| 896 | - | |
| 897 | - $scripts[] = array( | |
| 898 | - 'async' => true, | |
| 899 | - 'data-uid' => $form['uid'], | |
| 900 | - 'src' => $form['embed_js'], | |
| 901 | - 'data-kit-limit-per-session' => $limit_per_session ? '1' : '0', | |
| 902 | - ); | |
| 903 | - | |
| 904 | - return $scripts; | |
| 905 | - | |
| 906 | - } | |
| 907 | - ); | |
| 908 | - } | |
| 909 | - | |
| 910 | 666 | } |
| 911 | 667 | |
| 912 | 668 | /** |
| 913 | 669 | * Outputs any JS <script> tags registered with the convertkit_output_scripts_footer |
| @@ -916,13 +672,8 @@ | ||
| 916 | 672 | * @since 2.1.4 |
| 917 | 673 | */ |
| 918 | 674 | public function output_scripts_footer() { |
| 919 | 675 | |
| 920 | - // Don't output scripts if the request is for a search page or 404. | |
| 921 | - if ( is_search() || is_404() ) { | |
| 922 | - return; | |
| 923 | - } | |
| 924 | - | |
| 925 | 676 | // Define array of scripts. |
| 926 | 677 | $scripts = array(); |
| 927 | 678 | |
| 928 | 679 | /** |
| @@ -952,13 +703,8 @@ | ||
| 952 | 703 | * @param array $script Form script key/value pairs to output as <script> tag. |
| 953 | 704 | */ |
| 954 | 705 | $script = apply_filters( 'convertkit_output_script_footer', $script ); |
| 955 | 706 | |
| 956 | - // Skip script if it is limited by the Non-inline Form Limit per Session setting. | |
| 957 | - if ( $this->is_script_output_limited_by_session( $script ) ) { | |
| 958 | - continue; | |
| 959 | - } | |
| 960 | - | |
| 961 | 707 | // Build output. |
| 962 | 708 | $output = '<script'; |
| 963 | 709 | foreach ( $script as $attribute => $value ) { |
| 964 | 710 | // If the value is true, just output the attribute. |
| @@ -995,38 +741,8 @@ | ||
| 995 | 741 | // Output scripts. |
| 996 | 742 | foreach ( $output_scripts as $output_script ) { |
| 997 | 743 | echo $output_script . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 998 | 744 | } |
| 999 | - | |
| 1000 | - } | |
| 1001 | - | |
| 1002 | - /** | |
| 1003 | - * Checks if a script is limited by the Non-inline Form Limit per Session setting. | |
| 1004 | - * | |
| 1005 | - * @since 3.0.0 | |
| 1006 | - * | |
| 1007 | - * @param array $script Script. | |
| 1008 | - * @return bool | |
| 1009 | - */ | |
| 1010 | - private function is_script_output_limited_by_session( $script ) { | |
| 1011 | - | |
| 1012 | - // Get Settings, if they have not yet been loaded. | |
| 1013 | - if ( ! $this->settings ) { | |
| 1014 | - $this->settings = new ConvertKit_Settings(); | |
| 1015 | - } | |
| 1016 | - | |
| 1017 | - // Display script if the "Display Limit" setting isn't enabled. | |
| 1018 | - if ( ! $this->settings->non_inline_form_limit_per_session() ) { | |
| 1019 | - return false; | |
| 1020 | - } | |
| 1021 | - | |
| 1022 | - // Display script if the "Display Limit" setting should not be applied to this script. | |
| 1023 | - if ( ! isset( $script['data-kit-limit-per-session'] ) ) { | |
| 1024 | - return false; | |
| 1025 | - } | |
| 1026 | - | |
| 1027 | - // Display script if this is the first time the visitor has seen any non-inline form. | |
| 1028 | - return isset( $_COOKIE['ck_non_inline_form_displayed'] ); | |
| 1029 | 745 | |
| 1030 | 746 | } |
| 1031 | 747 | |
| 1032 | 748 | } |