| @@ -14,17 +14,8 @@ | ||
| 14 | 14 | */ |
| 15 | 15 | class ConvertKit_Output { |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | - * Holds the ConvertKit Subscriber ID. | |
| 19 | - * | |
| 20 | - * @since 2.0.6 | |
| 21 | - * | |
| 22 | - * @var int|string | |
| 23 | - */ | |
| 24 | - private $subscriber_id = 0; | |
| 25 | - | |
| 26 | - /** | |
| 27 | 18 | * Holds the ConvertKit Plugin Settings class |
| 28 | 19 | * |
| 29 | 20 | * @since 1.9.6 |
| 30 | 21 | * |
| @@ -66,158 +57,16 @@ | ||
| 66 | 57 | * @since 1.9.6 |
| 67 | 58 | */ |
| 68 | 59 | public function __construct() { |
| 69 | 60 | |
| 70 | - add_action( 'rest_api_init', array( $this, 'register_routes' ) ); | |
| 71 | - add_action( 'wp', array( $this, 'maybe_tag_subscriber' ) ); | |
| 72 | 61 | add_action( 'template_redirect', array( $this, 'output_form' ) ); |
| 73 | 62 | add_action( 'template_redirect', array( $this, 'page_takeover' ) ); |
| 74 | 63 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 75 | 64 | add_filter( 'the_content', array( $this, 'append_form_to_content' ) ); |
| 76 | - add_filter( 'hooked_block_types', array( $this, 'maybe_register_form_block_on_category_archive' ), 10, 4 ); | |
| 77 | - add_filter( 'hooked_block_convertkit/form', array( $this, 'append_form_block_to_category_archive' ), 10, 1 ); | |
| 78 | - add_action( 'wp_footer', array( $this, 'output_global_non_inline_form' ), 1 ); | |
| 79 | - add_action( 'wp_footer', array( $this, 'output_scripts_footer' ) ); | |
| 80 | 65 | |
| 81 | 66 | } |
| 82 | 67 | |
| 83 | 68 | /** |
| 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 | - * Tags the subscriber, if: | |
| 141 | - * - a subscriber ID exists in the cookie or URL, | |
| 142 | - * - the WordPress Page has the "Add a Tag" setting specified | |
| 143 | - * | |
| 144 | - * @since 2.4.9.1 | |
| 145 | - */ | |
| 146 | - public function maybe_tag_subscriber() { | |
| 147 | - | |
| 148 | - // Bail if not a singular Post Type supported by ConvertKit. | |
| 149 | - if ( ! is_singular( convertkit_get_supported_post_types() ) ) { | |
| 150 | - return; | |
| 151 | - } | |
| 152 | - | |
| 153 | - // Get Post ID. | |
| 154 | - $post_id = get_the_ID(); | |
| 155 | - | |
| 156 | - // Bail if a Post ID couldn't be identified. | |
| 157 | - if ( ! $post_id ) { | |
| 158 | - return; | |
| 159 | - } | |
| 160 | - | |
| 161 | - // Get Settings, if they have not yet been loaded. | |
| 162 | - if ( ! $this->settings ) { | |
| 163 | - $this->settings = new ConvertKit_Settings(); | |
| 164 | - } | |
| 165 | - | |
| 166 | - // Bail if the API hasn't been configured. | |
| 167 | - if ( ! $this->settings->has_access_and_refresh_token() ) { | |
| 168 | - return; | |
| 169 | - } | |
| 170 | - | |
| 171 | - // Get ConvertKit Post's Settings, if they have not yet been loaded. | |
| 172 | - if ( ! $this->post_settings ) { | |
| 173 | - $this->post_settings = new ConvertKit_Post( $post_id ); | |
| 174 | - } | |
| 175 | - | |
| 176 | - // Bail if no "Add a Tag" setting specified for this Page. | |
| 177 | - if ( ! $this->post_settings->has_tag() ) { | |
| 178 | - return; | |
| 179 | - } | |
| 180 | - | |
| 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 | - // Initialize the API. | |
| 190 | - $api = new ConvertKit_API_V4( | |
| 191 | - CONVERTKIT_OAUTH_CLIENT_ID, | |
| 192 | - CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI, | |
| 193 | - $this->settings->get_access_token(), | |
| 194 | - $this->settings->get_refresh_token(), | |
| 195 | - $this->settings->debug_enabled(), | |
| 196 | - 'output' | |
| 197 | - ); | |
| 198 | - | |
| 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 occurred, 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 | - // Tag subscriber. | |
| 215 | - $api->tag_subscriber( $this->post_settings->get_tag(), $this->subscriber_id ); | |
| 216 | - | |
| 217 | - } | |
| 218 | - | |
| 219 | - /** | |
| 220 | 69 | * Runs the `convertkit_output_output_form` action for singular Post Types that don't use the_content() |
| 221 | 70 | * or apply_filters( 'the_content' ) to output a ConvertKit Form. |
| 222 | 71 | * |
| 223 | 72 | * @since 1.9.6 |
| @@ -291,31 +140,13 @@ | ||
| 291 | 140 | |
| 292 | 141 | // Get Landing Page. |
| 293 | 142 | $landing_page = $this->landing_pages->get_html( $this->post_settings->get_landing_page() ); |
| 294 | 143 | |
| 295 | - // Bail if an error occurred. | |
| 144 | + // Bail if an error occured. | |
| 296 | 145 | if ( is_wp_error( $landing_page ) ) { |
| 297 | 146 | return; |
| 298 | 147 | } |
| 299 | 148 | |
| 300 | - // Replace the favicon with the WordPress site's favicon, if specified. | |
| 301 | - $landing_page = $this->landing_pages->replace_favicon( $landing_page ); | |
| 302 | - | |
| 303 | - /** | |
| 304 | - * Perform any actions immediately prior to outputting the Landing Page. | |
| 305 | - * | |
| 306 | - * Caching and minification Plugins may need to hook here to prevent | |
| 307 | - * CSS / JS minification and lazy loading images, which can interfere | |
| 308 | - * with Landing Pages. | |
| 309 | - * | |
| 310 | - * @since 2.4.4 | |
| 311 | - * | |
| 312 | - * @param string $landing_page ConvertKit Landing Page HTML. | |
| 313 | - * @param int $landing_page_id ConvertKit Landing Page ID. | |
| 314 | - * @param int $post_id WordPress Page ID. | |
| 315 | - */ | |
| 316 | - do_action( 'convertkit_output_landing_page_before', $landing_page, $landing_page_id, $post_id ); | |
| 317 | - | |
| 318 | 149 | // Output Landing Page. |
| 319 | 150 | // Output is supplied from ConvertKit's API, which is already sanitized. |
| 320 | 151 | echo $landing_page; // phpcs:ignore WordPress.Security.EscapeOutput |
| 321 | 152 | exit; |
| @@ -322,9 +153,9 @@ | ||
| 322 | 153 | |
| 323 | 154 | } |
| 324 | 155 | |
| 325 | 156 | /** |
| 326 | - * Inserts a form to the singular Page, Post or Custom Post Type's Content. | |
| 157 | + * Appends a form to the singular Page, Post or Custom Post Type's Content. | |
| 327 | 158 | * |
| 328 | 159 | * @param string $content Post Content. |
| 329 | 160 | * @return string Post Content with Form Appended, if applicable |
| 330 | 161 | */ |
| @@ -329,22 +160,15 @@ | ||
| 329 | 160 | * @return string Post Content with Form Appended, if applicable |
| 330 | 161 | */ |
| 331 | 162 | public function append_form_to_content( $content ) { |
| 332 | 163 | |
| 333 | - // Bail if not a singular Post Type supported by ConvertKit. | |
| 334 | - if ( ! is_singular( convertkit_get_supported_post_types() ) ) { | |
| 164 | + // Bail if not a singular Post Type. | |
| 165 | + if ( ! is_singular() ) { | |
| 335 | 166 | return $content; |
| 336 | 167 | } |
| 337 | 168 | |
| 338 | - // Get Post ID. | |
| 169 | + // Get Post ID and ConvertKit Form ID for the Post. | |
| 339 | 170 | $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 | 171 | $form_id = $this->get_post_form_id( $post_id ); |
| 348 | 172 | |
| 349 | 173 | /** |
| 350 | 174 | * Define the ConvertKit Form ID to display for the given Post ID, |
| @@ -369,17 +193,17 @@ | ||
| 369 | 193 | $this->forms = new ConvertKit_Resource_Forms( 'output_form' ); |
| 370 | 194 | } |
| 371 | 195 | |
| 372 | 196 | // Get Form HTML. |
| 373 | - $form = $this->forms->get_html( $form_id, $post_id ); | |
| 197 | + $form = $this->forms->get_html( $form_id ); | |
| 374 | 198 | |
| 375 | - // If an error occurred, it could be because the specified Form ID for the Post either: | |
| 199 | + // If an error occured, it could be because the specified Form ID for the Post either: | |
| 376 | 200 | // - 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 | 201 | // - the form was deleted from the ConvertKit account. |
| 378 | 202 | // Attempt to fallback to the default form for this Post Type. |
| 379 | 203 | if ( is_wp_error( $form ) ) { |
| 380 | 204 | if ( $this->settings->debug_enabled() ) { |
| 381 | - $content .= '<!-- Kit append_form_to_content(): ' . $form->get_error_message() . ' Attempting fallback to Default Form. -->'; | |
| 205 | + $content .= '<!-- ConvertKit append_form_to_content(): ' . $form->get_error_message() . ' Attempting fallback to Default Form. -->'; | |
| 382 | 206 | } |
| 383 | 207 | |
| 384 | 208 | // Get Default Form ID for this Post's Type. |
| 385 | 209 | $form_id = $this->settings->get_default_form( get_post_type( $post_id ) ); |
| @@ -386,9 +210,9 @@ | ||
| 386 | 210 | |
| 387 | 211 | // If no Default Form is specified, just return the Post Content, unedited. |
| 388 | 212 | if ( ! $form_id ) { |
| 389 | 213 | if ( $this->settings->debug_enabled() ) { |
| 390 | - $content .= '<!-- Kit append_form_to_content(): No Default Form exists as a fallback. -->'; | |
| 214 | + $content .= '<!-- ConvertKit append_form_to_content(): No Default Form exists as a fallback. -->'; | |
| 391 | 215 | } |
| 392 | 216 | |
| 393 | 217 | return $content; |
| 394 | 218 | } |
| @@ -393,15 +217,15 @@ | ||
| 393 | 217 | return $content; |
| 394 | 218 | } |
| 395 | 219 | |
| 396 | 220 | // Get Form HTML. |
| 397 | - $form = $this->forms->get_html( $form_id, $post_id ); | |
| 221 | + $form = $this->forms->get_html( $form_id ); | |
| 398 | 222 | |
| 399 | - // If an error occurred again, the default form doesn't exist in this ConvertKit account. | |
| 223 | + // If an error occured again, the default form doesn't exist in this ConvertKit account. | |
| 400 | 224 | // Just return the Post Content, unedited. |
| 401 | 225 | if ( is_wp_error( $form ) ) { |
| 402 | 226 | if ( $this->settings->debug_enabled() ) { |
| 403 | - $content .= '<!-- Kit append_form_to_content(): Default Form: ' . $form->get_error_message() . ' -->'; | |
| 227 | + $content .= '<!-- ConvertKit append_form_to_content(): Default Form: ' . $form->get_error_message() . ' -->'; | |
| 404 | 228 | } |
| 405 | 229 | |
| 406 | 230 | return $content; |
| 407 | 231 | } |
| @@ -406,70 +230,23 @@ | ||
| 406 | 230 | return $content; |
| 407 | 231 | } |
| 408 | 232 | } |
| 409 | 233 | |
| 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 | 234 | // 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 ) ); | |
| 235 | + // Append form to Post's Content. | |
| 236 | + $content = $content .= $form; | |
| 423 | 237 | |
| 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 | 238 | /** |
| 461 | 239 | * Filter the Post's Content, which includes a ConvertKit Form, immediately before it is output. |
| 462 | 240 | * |
| 463 | 241 | * @since 1.9.6 |
| 464 | 242 | * |
| 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. | |
| 243 | + * @param string $content Post Content | |
| 244 | + * @param string $form ConvertKit Form HTML | |
| 245 | + * @param int $post_id Post ID | |
| 246 | + * @param int $form_id ConvertKit Form ID | |
| 470 | 247 | */ |
| 471 | - $content = apply_filters( 'convertkit_frontend_append_form', $content, $form, $post_id, $form_id, $form_position ); | |
| 248 | + $content = apply_filters( 'convertkit_frontend_append_form', $content, $form, $post_id, $form_id ); | |
| 472 | 249 | |
| 473 | 250 | return $content; |
| 474 | 251 | |
| 475 | 252 | } |
| @@ -474,219 +251,8 @@ | ||
| 474 | 251 | |
| 475 | 252 | } |
| 476 | 253 | |
| 477 | 254 | /** |
| 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 | - * Registers the ConvertKit Form block to before or after the Query Loop block, when viewing a Category archive. | |
| 590 | - * | |
| 591 | - * See append_form_block_on_category_archive() configures the block to display the applicable category's Form. | |
| 592 | - * | |
| 593 | - * @since 2.4.9.1 | |
| 594 | - * | |
| 595 | - * @param array $hooked_blocks The list of hooked block types. | |
| 596 | - * @param string $position The relative position of the hooked blocks. | |
| 597 | - * @param string $anchor_block The anchor block type. | |
| 598 | - * @param WP_Block_Template|WP_Post|array $context The block template, template part, wp_navigation post type, or pattern that the anchor block belongs to. | |
| 599 | - * @return array | |
| 600 | - */ | |
| 601 | - public function maybe_register_form_block_on_category_archive( $hooked_blocks, $position, $anchor_block, $context ) { | |
| 602 | - | |
| 603 | - // Don't append if we're not viewing a category archive. | |
| 604 | - if ( ! is_category() ) { | |
| 605 | - return $hooked_blocks; | |
| 606 | - } | |
| 607 | - | |
| 608 | - if ( $context instanceof WP_Block_Template && $context->slug !== 'archive' ) { | |
| 609 | - return $hooked_blocks; | |
| 610 | - } | |
| 611 | - | |
| 612 | - // Don't append if the anchor block isn't the Query Loop block. | |
| 613 | - if ( $anchor_block !== 'core/query' ) { | |
| 614 | - return $hooked_blocks; | |
| 615 | - } | |
| 616 | - | |
| 617 | - // Don't append if the Category's form position setting is not defined. | |
| 618 | - $form_position = $this->get_term_form_position(); | |
| 619 | - if ( ! $form_position ) { | |
| 620 | - // Unhook this function as we don't need to check again in this request, as we'll | |
| 621 | - // never output a form on the Category archive. | |
| 622 | - remove_filter( 'hooked_block_types', array( $this, 'maybe_register_form_block_on_category_archive' ), 10 ); | |
| 623 | - | |
| 624 | - return $hooked_blocks; | |
| 625 | - } | |
| 626 | - | |
| 627 | - // Don't append if the position doesn't match. | |
| 628 | - if ( $form_position !== $position ) { | |
| 629 | - return $hooked_blocks; | |
| 630 | - } | |
| 631 | - | |
| 632 | - // Hook the ConvertKit Form block. | |
| 633 | - $hooked_blocks[] = 'convertkit/form'; | |
| 634 | - | |
| 635 | - // Unhook this function as we don't need to check again in this request, as | |
| 636 | - // we have now appended the form. | |
| 637 | - remove_filter( 'hooked_block_types', array( $this, 'maybe_register_form_block_on_category_archive' ), 10 ); | |
| 638 | - | |
| 639 | - return $hooked_blocks; | |
| 640 | - | |
| 641 | - } | |
| 642 | - | |
| 643 | - /** | |
| 644 | - * Configures the ConvertKit Form block that was hooked below the Query Loop block by maybe_register_form_block_on_category_archive, | |
| 645 | - * defining the Form ID based on the current Category's Form ID. | |
| 646 | - * | |
| 647 | - * @since 2.4.9.1 | |
| 648 | - * | |
| 649 | - * @param array $parsed_hooked_block The parsed block array for the given hooked block type, or null to suppress the block. | |
| 650 | - * @return null|array | |
| 651 | - */ | |
| 652 | - public function append_form_block_to_category_archive( $parsed_hooked_block ) { | |
| 653 | - | |
| 654 | - // Sanity check that we're still viewing a Category archive. | |
| 655 | - if ( ! is_category() ) { | |
| 656 | - // Returning null will unregister the Form block from displaying. | |
| 657 | - return null; | |
| 658 | - } | |
| 659 | - | |
| 660 | - // Get Category archive being viewed. | |
| 661 | - $category = get_category( get_query_var( 'cat' ) ); | |
| 662 | - | |
| 663 | - // Bail if the Category could be found. | |
| 664 | - if ( is_wp_error( $category ) || is_null( $category ) ) { | |
| 665 | - // Returning null will unregister the Form block from displaying. | |
| 666 | - return null; | |
| 667 | - } | |
| 668 | - | |
| 669 | - // Load Term Settings. | |
| 670 | - $term_settings = new ConvertKit_Term( $category->term_id ); | |
| 671 | - | |
| 672 | - // Bail if no Form specified for the Category. | |
| 673 | - if ( ! $term_settings->has_form() ) { | |
| 674 | - // Returning null will unregister the Form block from displaying. | |
| 675 | - return null; | |
| 676 | - } | |
| 677 | - | |
| 678 | - // Define the form block attributes to display the given Form ID. | |
| 679 | - $parsed_hooked_block['attrs'] = array( | |
| 680 | - 'id' => absint( $term_settings->get_form() ), | |
| 681 | - ); | |
| 682 | - | |
| 683 | - // Return the Form block with its attributes. | |
| 684 | - return $parsed_hooked_block; | |
| 685 | - | |
| 686 | - } | |
| 687 | - | |
| 688 | - /** | |
| 689 | 255 | * Returns the Post, Category or Plugin ConvertKit Form ID for the given Post. |
| 690 | 256 | * |
| 691 | 257 | * If the Post specifies a form to use, returns that Form ID. |
| 692 | 258 | * If the Post uses the 'Default' setting, and an assigned Category has a Form ID, uses the Category's Form ID. |
| @@ -752,53 +318,16 @@ | ||
| 752 | 318 | // If a Form ID exists, return it now. |
| 753 | 319 | if ( $term_settings->has_form() ) { |
| 754 | 320 | return $term_settings->get_form(); |
| 755 | 321 | } |
| 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 | 322 | } |
| 762 | 323 | |
| 763 | - // If here, all Terms were set to display the Default Form. | |
| 764 | - // Therefore use the Plugin's Default Form. | |
| 324 | + // If here, use the Plugin's Default Form. | |
| 765 | 325 | return $this->settings->get_default_form( get_post_type( $post_id ) ); |
| 766 | 326 | |
| 767 | 327 | } |
| 768 | 328 | |
| 769 | 329 | /** |
| 770 | - * Returns the Form Position setting for the currently viewed Category. | |
| 771 | - * | |
| 772 | - * @since 2.4.9.1 | |
| 773 | - * | |
| 774 | - * @return bool|string | |
| 775 | - */ | |
| 776 | - private function get_term_form_position() { | |
| 777 | - | |
| 778 | - // Get Category archive being viewed. | |
| 779 | - $category = get_category( get_query_var( 'cat' ) ); | |
| 780 | - | |
| 781 | - // Bail if the Category could be found. | |
| 782 | - if ( is_wp_error( $category ) || is_null( $category ) ) { | |
| 783 | - return false; | |
| 784 | - } | |
| 785 | - | |
| 786 | - // Load Term Settings. | |
| 787 | - $term_settings = new ConvertKit_Term( $category->term_id ); | |
| 788 | - | |
| 789 | - // Return false if no form position is defined i.e. we don't want to display | |
| 790 | - // it on the Category archive. | |
| 791 | - if ( ! $term_settings->has_form_position() ) { | |
| 792 | - return false; | |
| 793 | - } | |
| 794 | - | |
| 795 | - // Return form position. | |
| 796 | - return $term_settings->get_form_position(); | |
| 797 | - | |
| 798 | - } | |
| 799 | - | |
| 800 | - /** | |
| 801 | 330 | * Enqueue scripts. |
| 802 | 331 | * |
| 803 | 332 | * @since 1.9.6 |
| 804 | 333 | */ |
| @@ -803,31 +332,49 @@ | ||
| 803 | 332 | * @since 1.9.6 |
| 804 | 333 | */ |
| 805 | 334 | public function enqueue_scripts() { |
| 806 | 335 | |
| 807 | - // Get ConvertKit Settings and Post's Settings. | |
| 808 | - $settings = new ConvertKit_Settings(); | |
| 336 | + // Get Post. | |
| 337 | + $post = get_post(); | |
| 809 | 338 | |
| 810 | - // Bail if the no scripts setting is enabled. | |
| 811 | - if ( $settings->scripts_disabled() ) { | |
| 339 | + // Bail if no Post could be fetched. | |
| 340 | + if ( ! $post ) { | |
| 812 | 341 | return; |
| 813 | 342 | } |
| 814 | 343 | |
| 815 | - // Enqueue frontend JS. | |
| 816 | - convertkit_enqueue_frontend_js(); | |
| 344 | + // Get ConvertKit Settings and Post's Settings. | |
| 345 | + $settings = new ConvertKit_Settings(); | |
| 346 | + $convertkit_post = new ConvertKit_Post( $post->ID ); | |
| 817 | 347 | |
| 818 | - // Define variables. | |
| 348 | + // Register scripts that we might use. | |
| 349 | + wp_register_script( | |
| 350 | + 'convertkit-js', | |
| 351 | + CONVERTKIT_PLUGIN_URL . 'resources/frontend/js/convertkit.js', | |
| 352 | + array( 'jquery' ), | |
| 353 | + CONVERTKIT_PLUGIN_VERSION, | |
| 354 | + true | |
| 355 | + ); | |
| 819 | 356 | wp_localize_script( |
| 820 | 357 | 'convertkit-js', |
| 821 | 358 | 'convertkit', |
| 822 | 359 | array( |
| 823 | - 'ajaxurl' => rest_url( 'kit/v1/subscriber/store-email-as-id-in-cookie' ), | |
| 360 | + 'ajaxurl' => admin_url( 'admin-ajax.php' ), | |
| 824 | 361 | 'debug' => $settings->debug_enabled(), |
| 825 | - 'nonce' => wp_create_nonce( 'wp_rest' ), | |
| 826 | - 'subscriber_id' => $this->subscriber_id, | |
| 362 | + 'nonce' => wp_create_nonce( 'convertkit' ), | |
| 363 | + 'subscriber_id' => $this->get_subscriber_id_from_request(), | |
| 364 | + 'tag' => ( ( is_singular() && $convertkit_post->has_tag() ) ? $convertkit_post->get_tag() : false ), | |
| 365 | + 'post_id' => $post->ID, | |
| 827 | 366 | ) |
| 828 | 367 | ); |
| 829 | 368 | |
| 369 | + // Bail if the no scripts setting is enabled. | |
| 370 | + if ( $settings->scripts_disabled() ) { | |
| 371 | + return; | |
| 372 | + } | |
| 373 | + | |
| 374 | + // Enqueue. | |
| 375 | + wp_enqueue_script( 'convertkit-js' ); | |
| 376 | + | |
| 830 | 377 | } |
| 831 | 378 | |
| 832 | 379 | /** |
| 833 | 380 | * Gets the subscriber ID from the request (either the cookie or the URL). |
| @@ -832,8 +379,10 @@ | ||
| 832 | 379 | /** |
| 833 | 380 | * Gets the subscriber ID from the request (either the cookie or the URL). |
| 834 | 381 | * |
| 835 | 382 | * @since 1.9.6 |
| 383 | + * | |
| 384 | + * @return int Subscriber ID | |
| 836 | 385 | */ |
| 837 | 386 | public function get_subscriber_id_from_request() { |
| 838 | 387 | |
| 839 | 388 | // Use ConvertKit_Subscriber class to fetch and validate the subscriber ID. |
| @@ -839,194 +388,14 @@ | ||
| 839 | 388 | // Use ConvertKit_Subscriber class to fetch and validate the subscriber ID. |
| 840 | 389 | $subscriber = new ConvertKit_Subscriber(); |
| 841 | 390 | $subscriber_id = $subscriber->get_subscriber_id(); |
| 842 | 391 | |
| 843 | - // If an error occurred, the subscriber ID in the request/cookie is not a valid subscriber. | |
| 392 | + // If an error occured, the subscriber ID in the request/cookie is not a valid subscriber. | |
| 844 | 393 | if ( is_wp_error( $subscriber_id ) ) { |
| 845 | - return; | |
| 394 | + return 0; | |
| 846 | 395 | } |
| 847 | 396 | |
| 848 | - $this->subscriber_id = $subscriber_id; | |
| 849 | - | |
| 850 | - } | |
| 851 | - | |
| 852 | - /** | |
| 853 | - * Outputs a non-inline forms if defined in the Plugin's settings > | |
| 854 | - * Default Forms (Site Wide) setting. | |
| 855 | - * | |
| 856 | - * @since 2.3.3 | |
| 857 | - */ | |
| 858 | - public function output_global_non_inline_form() { | |
| 859 | - | |
| 860 | - // Get Settings, if they have not yet been loaded. | |
| 861 | - if ( ! $this->settings ) { | |
| 862 | - $this->settings = new ConvertKit_Settings(); | |
| 863 | - } | |
| 864 | - | |
| 865 | - // Bail if no non-inline form setting is specified. | |
| 866 | - if ( ! $this->settings->has_non_inline_form() ) { | |
| 867 | - return; | |
| 868 | - } | |
| 869 | - | |
| 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() ) { | |
| 873 | - return; | |
| 874 | - } | |
| 875 | - | |
| 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(); | |
| 878 | - | |
| 879 | - // Get form. | |
| 880 | - $convertkit_forms = new ConvertKit_Resource_Forms(); | |
| 881 | - | |
| 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 ); | |
| 886 | - | |
| 887 | - // Bail if the Form doesn't exist (this shouldn't happen, but you never know). | |
| 888 | - if ( ! $form ) { | |
| 889 | - continue; | |
| 890 | - } | |
| 891 | - | |
| 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 | - } | |
| 911 | - | |
| 912 | - /** | |
| 913 | - * Outputs any JS <script> tags registered with the convertkit_output_scripts_footer | |
| 914 | - * filter | |
| 915 | - * | |
| 916 | - * @since 2.1.4 | |
| 917 | - */ | |
| 918 | - public function output_scripts_footer() { | |
| 919 | - | |
| 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 | - // Define array of scripts. | |
| 926 | - $scripts = array(); | |
| 927 | - | |
| 928 | - /** | |
| 929 | - * Define an array of scripts to output in the footer of the WordPress site. | |
| 930 | - * | |
| 931 | - * @since 2.1.4 | |
| 932 | - * | |
| 933 | - * @param array $scripts Scripts. | |
| 934 | - */ | |
| 935 | - $scripts = apply_filters( 'convertkit_output_scripts_footer', $scripts ); | |
| 936 | - | |
| 937 | - // Bail if no scripts exist. | |
| 938 | - if ( ! count( $scripts ) ) { | |
| 939 | - return; | |
| 940 | - } | |
| 941 | - | |
| 942 | - // Define array to store <script> outputs. | |
| 943 | - $output_scripts = array(); | |
| 944 | - | |
| 945 | - // Iterate through scripts, building the <script> tag for each. | |
| 946 | - foreach ( $scripts as $script ) { | |
| 947 | - /** | |
| 948 | - * Filter the form <script> key/value pairs immediately before the script is output. | |
| 949 | - * | |
| 950 | - * @since 2.4.5 | |
| 951 | - * | |
| 952 | - * @param array $script Form script key/value pairs to output as <script> tag. | |
| 953 | - */ | |
| 954 | - $script = apply_filters( 'convertkit_output_script_footer', $script ); | |
| 955 | - | |
| 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 | - // Build output. | |
| 962 | - $output = '<script'; | |
| 963 | - foreach ( $script as $attribute => $value ) { | |
| 964 | - // If the value is true, just output the attribute. | |
| 965 | - if ( $value === true ) { | |
| 966 | - $output .= ' ' . esc_attr( $attribute ); | |
| 967 | - continue; | |
| 968 | - } | |
| 969 | - | |
| 970 | - // Sanitize attribute and value. | |
| 971 | - $attribute = esc_attr( $attribute ); | |
| 972 | - $value = ( $attribute === 'src' ? esc_url( $value ) : esc_attr( $value ) ); | |
| 973 | - | |
| 974 | - // Output the attribute and value. | |
| 975 | - $output .= ' ' . $attribute; | |
| 976 | - | |
| 977 | - // Output the value, if it's not a blank string. | |
| 978 | - if ( strlen( $value ) > 0 ) { | |
| 979 | - $output .= '="' . $value . '"'; | |
| 980 | - } | |
| 981 | - } | |
| 982 | - | |
| 983 | - $output .= '></script>'; | |
| 984 | - | |
| 985 | - // Add to array. | |
| 986 | - $output_scripts[] = $output; | |
| 987 | - } | |
| 988 | - | |
| 989 | - // Remove duplicate scripts. | |
| 990 | - // This prevents the same non-inline form displaying twice. For example, if a modal form is specified both | |
| 991 | - // in the Page's settings and the Form block, the user would see the same modal form displayed twice | |
| 992 | - // because the script would be output twice. | |
| 993 | - $output_scripts = array_unique( $output_scripts ); | |
| 994 | - | |
| 995 | - // Output scripts. | |
| 996 | - foreach ( $output_scripts as $output_script ) { | |
| 997 | - echo $output_script . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 998 | - } | |
| 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'] ); | |
| 397 | + return $subscriber_id; | |
| 1029 | 398 | |
| 1030 | 399 | } |
| 1031 | 400 | |
| 1032 | 401 | } |