PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.0.2
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.0.2
3.4.3 3.4.2 3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 All 196 releases
← All changes | includes/class-convertkit-output.php +53 -625 3.4.22.0.2 View file →
@@ -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,101 +57,16 @@
66 57 * @since 1.9.6
67 58 */
68 59 public function __construct() {
69 60
70 - add_action( 'wp', array( $this, 'maybe_tag_subscriber' ) );
71 61 add_action( 'template_redirect', array( $this, 'output_form' ) );
72 62 add_action( 'template_redirect', array( $this, 'page_takeover' ) );
73 63 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
74 64 add_filter( 'the_content', array( $this, 'append_form_to_content' ) );
75 - add_filter( 'hooked_block_types', array( $this, 'maybe_register_form_block_on_category_archive' ), 10, 4 );
76 - add_filter( 'hooked_block_convertkit/form', array( $this, 'append_form_block_to_category_archive' ), 10, 1 );
77 - add_action( 'wp_footer', array( $this, 'output_global_non_inline_form' ), 1 );
78 - add_action( 'wp_footer', array( $this, 'output_scripts_footer' ) );
79 65
80 66 }
81 67
82 68 /**
83 - * Tags the subscriber, if:
84 - * - a subscriber ID exists in the cookie or URL,
85 - * - the WordPress Page has the "Add a Tag" setting specified
86 - *
87 - * @since 2.4.9.1
88 - */
89 - public function maybe_tag_subscriber() {
90 -
91 - // Bail if not a singular Post Type supported by ConvertKit.
92 - if ( ! is_singular( convertkit_get_supported_post_types() ) ) {
93 - return;
94 - }
95 -
96 - // Get Post ID.
97 - $post_id = get_the_ID();
98 -
99 - // Bail if a Post ID couldn't be identified.
100 - if ( ! $post_id ) {
101 - return;
102 - }
103 -
104 - // Get Settings, if they have not yet been loaded.
105 - if ( ! $this->settings ) {
106 - $this->settings = new ConvertKit_Settings();
107 - }
108 -
109 - // Bail if the API hasn't been configured.
110 - if ( ! $this->settings->has_access_and_refresh_token() ) {
111 - return;
112 - }
113 -
114 - // Get ConvertKit Post's Settings, if they have not yet been loaded.
115 - if ( ! $this->post_settings ) {
116 - $this->post_settings = new ConvertKit_Post( $post_id );
117 - }
118 -
119 - // Bail if no "Add a Tag" setting specified for this Page.
120 - if ( ! $this->post_settings->has_tag() ) {
121 - return;
122 - }
123 -
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 - // Initialize the API.
133 - $api = new ConvertKit_API_V4(
134 - CONVERTKIT_OAUTH_CLIENT_ID,
135 - CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI,
136 - $this->settings->get_access_token(),
137 - $this->settings->get_refresh_token(),
138 - $this->settings->debug_enabled(),
139 - 'output'
140 - );
141 -
142 - // If the subscriber ID is not numeric, it's a cryptographically-signed subscriber ID, set
143 - // when using the Member Content functionality by Kit's API.
144 - // Fetch the underlying subscriber ID for the tag_subscriber() method.
145 - if ( ! is_numeric( $this->subscriber_id ) ) {
146 - $result = $api->profile( $this->subscriber_id );
147 -
148 - // If an error occurred, the subscriber ID is invalid.
149 - if ( is_wp_error( $result ) ) {
150 - return;
151 - }
152 -
153 - // Set the subscriber ID.
154 - $this->subscriber_id = $result['id'];
155 - }
156 -
157 - // Tag subscriber.
158 - $api->tag_subscriber( $this->post_settings->get_tag(), $this->subscriber_id );
159 -
160 - }
161 -
162 - /**
163 69 * Runs the `convertkit_output_output_form` action for singular Post Types that don't use the_content()
164 70 * or apply_filters( 'the_content' ) to output a ConvertKit Form.
165 71 *
166 72 * @since 1.9.6
@@ -234,31 +140,13 @@
234 140
235 141 // Get Landing Page.
236 142 $landing_page = $this->landing_pages->get_html( $this->post_settings->get_landing_page() );
237 143
238 - // Bail if an error occurred.
144 + // Bail if an error occured.
239 145 if ( is_wp_error( $landing_page ) ) {
240 146 return;
241 147 }
242 148
243 - // Replace the favicon with the WordPress site's favicon, if specified.
244 - $landing_page = $this->landing_pages->replace_favicon( $landing_page );
245 -
246 - /**
247 - * Perform any actions immediately prior to outputting the Landing Page.
248 - *
249 - * Caching and minification Plugins may need to hook here to prevent
250 - * CSS / JS minification and lazy loading images, which can interfere
251 - * with Landing Pages.
252 - *
253 - * @since 2.4.4
254 - *
255 - * @param string $landing_page ConvertKit Landing Page HTML.
256 - * @param int $landing_page_id ConvertKit Landing Page ID.
257 - * @param int $post_id WordPress Page ID.
258 - */
259 - do_action( 'convertkit_output_landing_page_before', $landing_page, $landing_page_id, $post_id );
260 -
261 149 // Output Landing Page.
262 150 // Output is supplied from ConvertKit's API, which is already sanitized.
263 151 echo $landing_page; // phpcs:ignore WordPress.Security.EscapeOutput
264 152 exit;
@@ -265,9 +153,9 @@
265 153
266 154 }
267 155
268 156 /**
269 - * 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.
270 158 *
271 159 * @param string $content Post Content.
272 160 * @return string Post Content with Form Appended, if applicable
273 161 */
@@ -272,22 +160,15 @@
272 160 * @return string Post Content with Form Appended, if applicable
273 161 */
274 162 public function append_form_to_content( $content ) {
275 163
276 - // Bail if not a singular Post Type supported by ConvertKit.
277 - if ( ! is_singular( convertkit_get_supported_post_types() ) ) {
164 + // Bail if not a singular Post Type.
165 + if ( ! is_singular() ) {
278 166 return $content;
279 167 }
280 168
281 - // Get Post ID.
169 + // Get Post ID and ConvertKit Form ID for the Post.
282 170 $post_id = get_the_ID();
283 -
284 - // Bail if Post Type is not supported by Kit.
285 - if ( ! in_array( get_post_type( $post_id ), convertkit_get_supported_post_types(), true ) ) {
286 - return $content;
287 - }
288 -
289 - // Get Form ID for the Post.
290 171 $form_id = $this->get_post_form_id( $post_id );
291 172
292 173 /**
293 174 * Define the ConvertKit Form ID to display for the given Post ID,
@@ -312,17 +193,17 @@
312 193 $this->forms = new ConvertKit_Resource_Forms( 'output_form' );
313 194 }
314 195
315 196 // Get Form HTML.
316 - $form = $this->forms->get_html( $form_id, $post_id );
197 + $form = $this->forms->get_html( $form_id );
317 198
318 - // 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:
319 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
320 201 // - the form was deleted from the ConvertKit account.
321 202 // Attempt to fallback to the default form for this Post Type.
322 203 if ( is_wp_error( $form ) ) {
323 204 if ( $this->settings->debug_enabled() ) {
324 - $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. -->';
325 206 }
326 207
327 208 // Get Default Form ID for this Post's Type.
328 209 $form_id = $this->settings->get_default_form( get_post_type( $post_id ) );
@@ -329,9 +210,9 @@
329 210
330 211 // If no Default Form is specified, just return the Post Content, unedited.
331 212 if ( ! $form_id ) {
332 213 if ( $this->settings->debug_enabled() ) {
333 - $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. -->';
334 215 }
335 216
336 217 return $content;
337 218 }
@@ -336,15 +217,15 @@
336 217 return $content;
337 218 }
338 219
339 220 // Get Form HTML.
340 - $form = $this->forms->get_html( $form_id, $post_id );
221 + $form = $this->forms->get_html( $form_id );
341 222
342 - // 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.
343 224 // Just return the Post Content, unedited.
344 225 if ( is_wp_error( $form ) ) {
345 226 if ( $this->settings->debug_enabled() ) {
346 - $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() . ' -->';
347 228 }
348 229
349 230 return $content;
350 231 }
@@ -349,70 +230,23 @@
349 230 return $content;
350 231 }
351 232 }
352 233
353 - // If the Form HTML is empty, it's a modal form that has been set to load in the footer of the site.
354 - // We don't need to append anything to the content.
355 - if ( empty( $form ) ) {
356 - if ( $this->settings->debug_enabled() ) {
357 - $content .= '<!-- Kit append_form_to_content(): Form is non-inline, appended to footer. -->';
358 - }
359 -
360 - return $content;
361 - }
362 -
363 234 // If here, we have a ConvertKit Form.
364 - // Append form to Post's Content, based on the position setting.
365 - $form_position = $this->settings->get_default_form_position( get_post_type( $post_id ) );
235 + // Append form to Post's Content.
236 + $content = $content .= $form;
366 237
367 - if ( $this->settings->debug_enabled() ) {
368 - $content .= '<!-- Kit append_form_to_content(): Form Position: ' . esc_html( $form_position ) . ' -->';
369 - }
370 -
371 - switch ( $form_position ) {
372 - case 'before_after_content':
373 - $content = $form . $content . $form;
374 - break;
375 -
376 - case 'before_content':
377 - $content = $form . $content;
378 - break;
379 -
380 - case 'after_element':
381 - $element = $this->settings->get_default_form_position_element( get_post_type( $post_id ) );
382 - $index = $this->settings->get_default_form_position_element_index( get_post_type( $post_id ) );
383 -
384 - // Check if DOMDocument is installed.
385 - // It should be installed as mosts hosts include php-dom and php-xml modules.
386 - // If not, fallback to using preg_match_all(), which is less reliable.
387 - if ( ! class_exists( 'DOMDocument' ) ) {
388 - $content = $this->inject_form_after_element_fallback( $content, $element, $index, $form );
389 - break;
390 - }
391 -
392 - // Use DOMDocument.
393 - $content = $this->inject_form_after_element( $content, $element, $index, $form );
394 - break;
395 -
396 - case 'after_content':
397 - default:
398 - // Default behaviour < 2.5.8 was to append the Form after the content.
399 - $content .= $form;
400 - break;
401 - }
402 -
403 238 /**
404 239 * Filter the Post's Content, which includes a ConvertKit Form, immediately before it is output.
405 240 *
406 241 * @since 1.9.6
407 242 *
408 - * @param string $content Post Content
409 - * @param string $form ConvertKit Form HTML
410 - * @param int $post_id Post ID
411 - * @param int $form_id ConvertKit Form ID
412 - * @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
413 247 */
414 - $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 );
415 249
416 250 return $content;
417 251
418 252 }
@@ -417,219 +251,8 @@
417 251
418 252 }
419 253
420 254 /**
421 - * Injects the form after the given element and index, using DOMDocument.
422 - *
423 - * @since 2.6.2
424 - *
425 - * @param string $content Page / Post Content.
426 - * @param string $tag HTML tag to insert form after.
427 - * @param int $index Number of $tag elements to find before inserting form.
428 - * @param string $form Form HTML to inject.
429 - * @return string
430 - */
431 - private function inject_form_after_element( $content, $tag, $index, $form ) {
432 -
433 - // If the form is empty, don't inject anything.
434 - if ( empty( $form ) ) {
435 - return $content;
436 - }
437 -
438 - // Load the content into the parser.
439 - $parser = new ConvertKit_HTML_Parser( $content, LIBXML_HTML_NODEFDTD );
440 -
441 - // Find the element to append the form to.
442 - // item() is a zero based index.
443 - $element_node = $parser->html->getElementsByTagName( $tag )->item( $index - 1 );
444 -
445 - // If the element could not be found, either the number of elements by tag name is less
446 - // than the requested position the form be inserted in, or no element exists.
447 - // Append the form to the original content and return.
448 - if ( is_null( $element_node ) ) {
449 - return $content . $form;
450 - }
451 -
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 );
455 -
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 - }
461 -
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.
469 - return $parser->get_body_html();
470 -
471 - }
472 -
473 - /**
474 - * Injects the form after the given element and index, using preg_match_all().
475 - * This is less reliable than DOMDocument, and is called if DOMDocument is
476 - * not installed on the server.
477 - *
478 - * @since 2.6.2
479 - *
480 - * @param string $content Page / Post Content.
481 - * @param string $tag HTML tag to insert form after.
482 - * @param int $index Number of $tag elements to find before inserting form.
483 - * @param string $form Form HTML to inject.
484 - * @return string
485 - */
486 - private function inject_form_after_element_fallback( $content, $tag, $index, $form ) {
487 -
488 - // If the form is empty, don't inject anything.
489 - if ( empty( $form ) ) {
490 - return $content;
491 - }
492 -
493 - // Calculate tag length.
494 - $tag_length = ( strlen( $tag ) + 3 );
495 -
496 - // Find all closing elements.
497 - preg_match_all( '/<\/' . $tag . '>/', $content, $matches );
498 -
499 - // If no elements exist, just append the form.
500 - if ( count( $matches[0] ) === 0 ) {
501 - $content = $content . $form;
502 - return $content;
503 - }
504 -
505 - // If the number of elements is less than the index, we don't have enough elements to add the form to.
506 - // Just add the form after the content.
507 - if ( count( $matches[0] ) <= $index ) {
508 - $content = $content . $form;
509 - return $content;
510 - }
511 -
512 - // Iterate through the content to find the element at the configured index e.g. find the 4th closing paragraph.
513 - $offset = 0;
514 - foreach ( $matches[0] as $element_index => $element ) {
515 - $position = strpos( $content, $element, $offset );
516 - if ( ( $element_index + 1 ) === $index ) {
517 - return substr( $content, 0, $position + 4 ) . $form . substr( $content, $position + 4 );
518 - }
519 -
520 - // Increment offset.
521 - $offset = $position + 1;
522 - }
523 -
524 - // If here, something went wrong.
525 - // Just add the form after the content.
526 - $content = $content . $form;
527 - return $content;
528 -
529 - }
530 -
531 - /**
532 - * Registers the ConvertKit Form block to before or after the Query Loop block, when viewing a Category archive.
533 - *
534 - * See append_form_block_on_category_archive() configures the block to display the applicable category's Form.
535 - *
536 - * @since 2.4.9.1
537 - *
538 - * @param array $hooked_blocks The list of hooked block types.
539 - * @param string $position The relative position of the hooked blocks.
540 - * @param string $anchor_block The anchor block type.
541 - * @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.
542 - * @return array
543 - */
544 - public function maybe_register_form_block_on_category_archive( $hooked_blocks, $position, $anchor_block, $context ) {
545 -
546 - // Don't append if we're not viewing a category archive.
547 - if ( ! is_category() ) {
548 - return $hooked_blocks;
549 - }
550 -
551 - if ( $context instanceof WP_Block_Template && $context->slug !== 'archive' ) {
552 - return $hooked_blocks;
553 - }
554 -
555 - // Don't append if the anchor block isn't the Query Loop block.
556 - if ( $anchor_block !== 'core/query' ) {
557 - return $hooked_blocks;
558 - }
559 -
560 - // Don't append if the Category's form position setting is not defined.
561 - $form_position = $this->get_term_form_position();
562 - if ( ! $form_position ) {
563 - // Unhook this function as we don't need to check again in this request, as we'll
564 - // never output a form on the Category archive.
565 - remove_filter( 'hooked_block_types', array( $this, 'maybe_register_form_block_on_category_archive' ), 10 );
566 -
567 - return $hooked_blocks;
568 - }
569 -
570 - // Don't append if the position doesn't match.
571 - if ( $form_position !== $position ) {
572 - return $hooked_blocks;
573 - }
574 -
575 - // Hook the ConvertKit Form block.
576 - $hooked_blocks[] = 'convertkit/form';
577 -
578 - // Unhook this function as we don't need to check again in this request, as
579 - // we have now appended the form.
580 - remove_filter( 'hooked_block_types', array( $this, 'maybe_register_form_block_on_category_archive' ), 10 );
581 -
582 - return $hooked_blocks;
583 -
584 - }
585 -
586 - /**
587 - * Configures the ConvertKit Form block that was hooked below the Query Loop block by maybe_register_form_block_on_category_archive,
588 - * defining the Form ID based on the current Category's Form ID.
589 - *
590 - * @since 2.4.9.1
591 - *
592 - * @param array $parsed_hooked_block The parsed block array for the given hooked block type, or null to suppress the block.
593 - * @return null|array
594 - */
595 - public function append_form_block_to_category_archive( $parsed_hooked_block ) {
596 -
597 - // Sanity check that we're still viewing a Category archive.
598 - if ( ! is_category() ) {
599 - // Returning null will unregister the Form block from displaying.
600 - return null;
601 - }
602 -
603 - // Get Category archive being viewed.
604 - $category = get_category( get_query_var( 'cat' ) );
605 -
606 - // Bail if the Category could be found.
607 - if ( is_wp_error( $category ) || is_null( $category ) ) {
608 - // Returning null will unregister the Form block from displaying.
609 - return null;
610 - }
611 -
612 - // Load Term Settings.
613 - $term_settings = new ConvertKit_Term( $category->term_id );
614 -
615 - // Bail if no Form specified for the Category.
616 - if ( ! $term_settings->has_form() ) {
617 - // Returning null will unregister the Form block from displaying.
618 - return null;
619 - }
620 -
621 - // Define the form block attributes to display the given Form ID.
622 - $parsed_hooked_block['attrs'] = array(
623 - 'id' => absint( $term_settings->get_form() ),
624 - );
625 -
626 - // Return the Form block with its attributes.
627 - return $parsed_hooked_block;
628 -
629 - }
630 -
631 - /**
632 255 * Returns the Post, Category or Plugin ConvertKit Form ID for the given Post.
633 256 *
634 257 * If the Post specifies a form to use, returns that Form ID.
635 258 * If the Post uses the 'Default' setting, and an assigned Category has a Form ID, uses the Category's Form ID.
@@ -695,53 +318,16 @@
695 318 // If a Form ID exists, return it now.
696 319 if ( $term_settings->has_form() ) {
697 320 return $term_settings->get_form();
698 321 }
699 -
700 - // If the Term specifies that no Form should be used, return false.
701 - if ( $term_settings->uses_no_form() ) {
702 - return false;
703 - }
704 322 }
705 323
706 - // If here, all Terms were set to display the Default Form.
707 - // Therefore use the Plugin's Default Form.
324 + // If here, use the Plugin's Default Form.
708 325 return $this->settings->get_default_form( get_post_type( $post_id ) );
709 326
710 327 }
711 328
712 329 /**
713 - * Returns the Form Position setting for the currently viewed Category.
714 - *
715 - * @since 2.4.9.1
716 - *
717 - * @return bool|string
718 - */
719 - private function get_term_form_position() {
720 -
721 - // Get Category archive being viewed.
722 - $category = get_category( get_query_var( 'cat' ) );
723 -
724 - // Bail if the Category could be found.
725 - if ( is_wp_error( $category ) || is_null( $category ) ) {
726 - return false;
727 - }
728 -
729 - // Load Term Settings.
730 - $term_settings = new ConvertKit_Term( $category->term_id );
731 -
732 - // Return false if no form position is defined i.e. we don't want to display
733 - // it on the Category archive.
734 - if ( ! $term_settings->has_form_position() ) {
735 - return false;
736 - }
737 -
738 - // Return form position.
739 - return $term_settings->get_form_position();
740 -
741 - }
742 -
743 - /**
744 330 * Enqueue scripts.
745 331 *
746 332 * @since 1.9.6
747 333 */
@@ -746,29 +332,49 @@
746 332 * @since 1.9.6
747 333 */
748 334 public function enqueue_scripts() {
749 335
750 - // Get ConvertKit Settings and Post's Settings.
751 - $settings = new ConvertKit_Settings();
336 + // Get Post.
337 + $post = get_post();
752 338
753 - // Bail if the no scripts setting is enabled.
754 - if ( $settings->scripts_disabled() ) {
339 + // Bail if no Post could be fetched.
340 + if ( ! $post ) {
755 341 return;
756 342 }
757 343
758 - // Enqueue frontend JS.
759 - 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 );
760 347
761 - // 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 + );
762 356 wp_localize_script(
763 357 'convertkit-js',
764 358 'convertkit',
765 359 array(
360 + 'ajaxurl' => admin_url( 'admin-ajax.php' ),
766 361 'debug' => $settings->debug_enabled(),
767 - '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,
768 366 )
769 367 );
770 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 +
771 377 }
772 378
773 379 /**
774 380 * Gets the subscriber ID from the request (either the cookie or the URL).
@@ -773,8 +379,10 @@
773 379 /**
774 380 * Gets the subscriber ID from the request (either the cookie or the URL).
775 381 *
776 382 * @since 1.9.6
383 + *
384 + * @return int Subscriber ID
777 385 */
778 386 public function get_subscriber_id_from_request() {
779 387
780 388 // Use ConvertKit_Subscriber class to fetch and validate the subscriber ID.
@@ -780,194 +388,14 @@
780 388 // Use ConvertKit_Subscriber class to fetch and validate the subscriber ID.
781 389 $subscriber = new ConvertKit_Subscriber();
782 390 $subscriber_id = $subscriber->get_subscriber_id();
783 391
784 - // 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.
785 393 if ( is_wp_error( $subscriber_id ) ) {
786 - return;
394 + return 0;
787 395 }
788 396
789 - $this->subscriber_id = $subscriber_id;
790 -
791 - }
792 -
793 - /**
794 - * Outputs a non-inline forms if defined in the Plugin's settings >
795 - * Default Forms (Site Wide) setting.
796 - *
797 - * @since 2.3.3
798 - */
799 - public function output_global_non_inline_form() {
800 -
801 - // Get Settings, if they have not yet been loaded.
802 - if ( ! $this->settings ) {
803 - $this->settings = new ConvertKit_Settings();
804 - }
805 -
806 - // Bail if no non-inline form setting is specified.
807 - if ( ! $this->settings->has_non_inline_form() ) {
808 - return;
809 - }
810 -
811 - // Bail if the Page, Post or Custom Post Type's Form setting is set to 'None'
812 - // and the Plugin is set to honor this setting.
813 - if ( $this->post_settings !== false && $this->post_settings->uses_no_form() && $this->settings->non_inline_form_honor_none_setting() ) {
814 - return;
815 - }
816 -
817 - // Determine if the Non-inline Form Limit per Session setting is enabled.
818 - $limit_per_session = $this->settings->non_inline_form_limit_per_session();
819 -
820 - // Get form.
821 - $convertkit_forms = new ConvertKit_Resource_Forms();
822 -
823 - // Iterate through forms.
824 - foreach ( $this->settings->get_non_inline_form() as $form_id ) {
825 - // Get Form.
826 - $form = $convertkit_forms->get_by_id( (int) $form_id );
827 -
828 - // Bail if the Form doesn't exist (this shouldn't happen, but you never know).
829 - if ( ! $form ) {
830 - continue;
831 - }
832 -
833 - // Add the form to the scripts array so it is included in the output.
834 - add_filter(
835 - 'convertkit_output_scripts_footer',
836 - function ( $scripts ) use ( $form, $limit_per_session ) {
837 -
838 - $scripts[] = array(
839 - 'async' => true,
840 - 'data-uid' => $form['uid'],
841 - 'src' => $form['embed_js'],
842 - 'data-kit-limit-per-session' => $limit_per_session ? '1' : '0',
843 - );
844 -
845 - return $scripts;
846 -
847 - }
848 - );
849 - }
850 -
851 - }
852 -
853 - /**
854 - * Outputs any JS <script> tags registered with the convertkit_output_scripts_footer
855 - * filter
856 - *
857 - * @since 2.1.4
858 - */
859 - public function output_scripts_footer() {
860 -
861 - // Don't output scripts if the request is for a search page or 404.
862 - if ( is_search() || is_404() ) {
863 - return;
864 - }
865 -
866 - // Define array of scripts.
867 - $scripts = array();
868 -
869 - /**
870 - * Define an array of scripts to output in the footer of the WordPress site.
871 - *
872 - * @since 2.1.4
873 - *
874 - * @param array $scripts Scripts.
875 - */
876 - $scripts = apply_filters( 'convertkit_output_scripts_footer', $scripts );
877 -
878 - // Bail if no scripts exist.
879 - if ( ! count( $scripts ) ) {
880 - return;
881 - }
882 -
883 - // Define array to store <script> outputs.
884 - $output_scripts = array();
885 -
886 - // Iterate through scripts, building the <script> tag for each.
887 - foreach ( $scripts as $script ) {
888 - /**
889 - * Filter the form <script> key/value pairs immediately before the script is output.
890 - *
891 - * @since 2.4.5
892 - *
893 - * @param array $script Form script key/value pairs to output as <script> tag.
894 - */
895 - $script = apply_filters( 'convertkit_output_script_footer', $script );
896 -
897 - // Skip script if it is limited by the Non-inline Form Limit per Session setting.
898 - if ( $this->is_script_output_limited_by_session( $script ) ) {
899 - continue;
900 - }
901 -
902 - // Build output.
903 - $output = '<script';
904 - foreach ( $script as $attribute => $value ) {
905 - // If the value is true, just output the attribute.
906 - if ( $value === true ) {
907 - $output .= ' ' . esc_attr( $attribute );
908 - continue;
909 - }
910 -
911 - // Sanitize attribute and value.
912 - $attribute = esc_attr( $attribute );
913 - $value = ( $attribute === 'src' ? esc_url( $value ) : esc_attr( $value ) );
914 -
915 - // Output the attribute and value.
916 - $output .= ' ' . $attribute;
917 -
918 - // Output the value, if it's not a blank string.
919 - if ( strlen( $value ) > 0 ) {
920 - $output .= '="' . $value . '"';
921 - }
922 - }
923 -
924 - $output .= '></script>';
925 -
926 - // Add to array.
927 - $output_scripts[] = $output;
928 - }
929 -
930 - // Remove duplicate scripts.
931 - // This prevents the same non-inline form displaying twice. For example, if a modal form is specified both
932 - // in the Page's settings and the Form block, the user would see the same modal form displayed twice
933 - // because the script would be output twice.
934 - $output_scripts = array_unique( $output_scripts );
935 -
936 - // Output scripts.
937 - foreach ( $output_scripts as $output_script ) {
938 - echo $output_script . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
939 - }
940 -
941 - }
942 -
943 - /**
944 - * Checks if a script is limited by the Non-inline Form Limit per Session setting.
945 - *
946 - * @since 3.0.0
947 - *
948 - * @param array $script Script.
949 - * @return bool
950 - */
951 - private function is_script_output_limited_by_session( $script ) {
952 -
953 - // Get Settings, if they have not yet been loaded.
954 - if ( ! $this->settings ) {
955 - $this->settings = new ConvertKit_Settings();
956 - }
957 -
958 - // Display script if the "Display Limit" setting isn't enabled.
959 - if ( ! $this->settings->non_inline_form_limit_per_session() ) {
960 - return false;
961 - }
962 -
963 - // Display script if the "Display Limit" setting should not be applied to this script.
964 - if ( ! isset( $script['data-kit-limit-per-session'] ) ) {
965 - return false;
966 - }
967 -
968 - // Display script if this is the first time the visitor has seen any non-inline form.
969 - return isset( $_COOKIE['ck_non_inline_form_displayed'] );
397 + return $subscriber_id;
970 398
971 399 }
972 400
973 401 }