@automattic/newspack-blocks 4.12.3 → 4.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/dist/carousel/view.asset.php +1 -1
  3. package/dist/carousel/view.js +3 -3
  4. package/dist/editor.asset.php +1 -1
  5. package/dist/editor.js +3 -3
  6. package/dist/modal.asset.php +1 -1
  7. package/dist/modal.js +1 -1
  8. package/dist/modalCheckout-rtl.css +1 -1
  9. package/dist/modalCheckout.asset.php +1 -1
  10. package/dist/modalCheckout.css +1 -1
  11. package/dist/modalCheckout.js +1 -1
  12. package/includes/class-modal-checkout.php +32 -106
  13. package/includes/class-newspack-blocks.php +4 -0
  14. package/includes/modal-checkout/class-checkout-data.php +287 -0
  15. package/includes/tracking/class-data-events.php +5 -140
  16. package/newspack-blocks.php +3 -2
  17. package/package.json +5 -5
  18. package/src/blocks/checkout-button/view.php +8 -42
  19. package/src/blocks/donate/frontend/class-newspack-blocks-donate-renderer-base.php +2 -2
  20. package/src/blocks/donate/frontend/class-newspack-blocks-donate-renderer-frequency-based.php +4 -28
  21. package/src/blocks/donate/frontend/class-newspack-blocks-donate-renderer-tiers-based.php +5 -17
  22. package/src/modal-checkout/analytics/ga4/checkout-attempt.js +3 -2
  23. package/src/modal-checkout/analytics/ga4/checkout-success.js +3 -3
  24. package/src/modal-checkout/analytics/ga4/dismissed.js +3 -3
  25. package/src/modal-checkout/analytics/ga4/loaded.js +3 -3
  26. package/src/modal-checkout/analytics/ga4/pagination.js +3 -2
  27. package/src/modal-checkout/analytics/ga4/utils/index.js +31 -17
  28. package/src/modal-checkout/analytics/index.js +1 -1
  29. package/src/modal-checkout/checkout.scss +2 -1
  30. package/src/modal-checkout/index.js +1 -1
  31. package/src/modal-checkout/modal.js +232 -253
  32. package/src/modal-checkout/templates/thankyou.php +3 -6
  33. package/src/modal-checkout/utils.js +126 -0
  34. package/vendor/autoload.php +1 -1
  35. package/vendor/composer/autoload_real.php +4 -4
  36. package/vendor/composer/autoload_static.php +2 -2
  37. package/vendor/composer/installed.php +2 -2
@@ -11,7 +11,7 @@
11
11
 
12
12
  namespace Newspack_Blocks;
13
13
 
14
- use Newspack_Blocks\Tracking\Data_Events;
14
+ use Newspack_Blocks\Modal_Checkout\Checkout_Data;
15
15
 
16
16
  if ( ! defined( 'ABSPATH' ) ) {
17
17
  exit;
@@ -43,10 +43,7 @@ function newspack_blocks_replace_login_with_order_summary() {
43
43
  $after_success_behavior = isset( $_GET['after_success_behavior'] ) ? \sanitize_text_field( \wp_unslash( $_GET['after_success_behavior'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
44
44
  $after_success_url = isset( $_GET['after_success_url'] ) ? esc_url( \sanitize_url( \wp_unslash( $_GET['after_success_url'] ) ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
45
45
  $after_success_label = isset( $_GET['after_success_button_label'] ) ? \sanitize_text_field( \wp_unslash( $_GET['after_success_button_label'] ) ) : \Newspack_Blocks\Modal_Checkout::get_modal_checkout_labels( 'after_success' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended
46
- $order_items = $order->get_items();
47
- $item = reset( $order_items );
48
- $product_id = $item ? $item->get_product_id() : null;
49
- $data_order_details = Data_Events::build_js_data_events( $product_id, null, $order );
46
+ $checkout_data = Checkout_Data::get_checkout_data( $order );
50
47
  ?>
51
48
  <div class="woocommerce-order">
52
49
  <?php if ( $is_success ) : ?>
@@ -57,7 +54,7 @@ function newspack_blocks_replace_login_with_order_summary() {
57
54
  <path d="M16.7 7.1l-6.3 8.5-3.3-2.5-.9 1.2 4.5 3.4L17.9 8z"></path>
58
55
  </svg>
59
56
  </span>
60
- <p id="modal-checkout-product-details" data-order-details='<?php echo wp_json_encode( $data_order_details ); ?>'>
57
+ <p id="modal-checkout-product-details" data-checkout='<?php echo wp_json_encode( $checkout_data ); ?>'>
61
58
  <strong>
62
59
  <?php
63
60
  echo esc_html( Modal_Checkout::get_post_checkout_success_text() );
@@ -40,3 +40,129 @@ export function createHiddenInput( name, value = null ) {
40
40
 
41
41
  return input;
42
42
  }
43
+
44
+ /**
45
+ * Run a callback when an iframe is ready.
46
+ *
47
+ * @param {HTMLIFrameElement} iframe The iframe element.
48
+ * @param {Function} cb The callback to execute when the iframe is ready.
49
+ *
50
+ * @return {void}
51
+ */
52
+ export function iframeReady( iframe, cb ) {
53
+ if ( iframe._readyTimer ) {
54
+ clearTimeout( iframe._readyTimer );
55
+ }
56
+ let fired = false;
57
+
58
+ function ready() {
59
+ if ( ! fired ) {
60
+ fired = true;
61
+ clearTimeout( iframe._readyTimer );
62
+ cb.call( this );
63
+ }
64
+ }
65
+ function readyState() {
66
+ if ( this.readyState === "complete" ) {
67
+ ready.call( this );
68
+ }
69
+ }
70
+ function checkLoaded() {
71
+ if ( iframe._ready ) {
72
+ clearTimeout( iframe._readyTimer );
73
+ return;
74
+ }
75
+ const doc = iframe.contentDocument || iframe.contentWindow?.document;
76
+ if ( doc && doc.URL.indexOf('about:') !== 0 ) {
77
+ if ( doc?.readyState === 'complete' ) {
78
+ ready.call( doc );
79
+ } else {
80
+ doc.addEventListener( 'DOMContentLoaded', ready );
81
+ doc.addEventListener( 'readystatechange', readyState );
82
+ }
83
+ } else {
84
+ iframe._readyTimer = setTimeout( checkLoaded, 10 );
85
+ }
86
+ }
87
+ checkLoaded();
88
+ }
89
+
90
+ /**
91
+ * Trigger a form submit.
92
+ *
93
+ * @param {HTMLFormElement} form The form element.
94
+ *
95
+ * @return {void}
96
+ */
97
+ export function triggerFormSubmit( form ) {
98
+ // form.submit does not trigger submit event listener, so we use requestSubmit.
99
+ form.requestSubmit( form.querySelector( 'button[type="submit"]' ) );
100
+ }
101
+
102
+ /**
103
+ * Get checkout data from an element or its ID.
104
+ *
105
+ * @param {HTMLElement|string} element HTML element or its ID to get checkout data from.
106
+ *
107
+ * @return {Object} Checkout data.
108
+ */
109
+ export function getCheckoutData( element ) {
110
+ const container = typeof element === 'string' ? document.getElementById( element ) : element;
111
+ if ( ! container ) {
112
+ console.warn( 'No container found for checkout data' ); // eslint-disable-line no-console
113
+ return {};
114
+ }
115
+
116
+ let data = {};
117
+ // If the element is a form, get the form data.
118
+ if ( container.tagName === 'FORM' ) {
119
+ const formData = new FormData( container );
120
+ data = Object.fromEntries( formData );
121
+ }
122
+
123
+ const json = container.dataset.checkout;
124
+ if ( ! json ) {
125
+ console.warn( 'No checkout data found' ); // eslint-disable-line no-console
126
+ return data;
127
+ }
128
+
129
+ try {
130
+ data = {
131
+ ...data,
132
+ ...JSON.parse( json ),
133
+ };
134
+ } catch ( error ) {
135
+ console.warn( 'Error parsing checkout data' ); // eslint-disable-line no-console
136
+ }
137
+
138
+ // Overwrite the action type with the value from the URL.
139
+ const url = new URL( container.ownerDocument.defaultView.location.href );
140
+ if ( url.searchParams.get( 'action_type' ) ) {
141
+ data.action_type = url.searchParams.get( 'action_type' );
142
+ }
143
+
144
+ // Replace 'memberships_content_gate' with 'gate_post_id'.
145
+ if ( data.memberships_content_gate ) {
146
+ data.gate_post_id = data.memberships_content_gate;
147
+ delete data.memberships_content_gate;
148
+ }
149
+
150
+ return data;
151
+ }
152
+
153
+
154
+ /**
155
+ * Get formatted amount for price summary display.
156
+ *
157
+ * @param {number} amount The amount to format.
158
+ * @param {string} currency The currency to format the amount in.
159
+ *
160
+ * @return {string} The formatted amount.
161
+ */
162
+ export function getFormattedAmount( amount, currency = 'USD' ) {
163
+ return parseFloat( amount ).toLocaleString( document.documentElement.lang, {
164
+ style: 'currency',
165
+ currency,
166
+ currencyDisplay: 'narrowSymbol',
167
+ } );
168
+ }
@@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
22
22
 
23
23
  require_once __DIR__ . '/composer/autoload_real.php';
24
24
 
25
- return ComposerAutoloaderInit9e2e8e314628d5030f6030b973fa296b::getLoader();
25
+ return ComposerAutoloaderInit15a9301f3620998e3c1693d6c9682cba::getLoader();
@@ -2,7 +2,7 @@
2
2
 
3
3
  // autoload_real.php @generated by Composer
4
4
 
5
- class ComposerAutoloaderInit9e2e8e314628d5030f6030b973fa296b
5
+ class ComposerAutoloaderInit15a9301f3620998e3c1693d6c9682cba
6
6
  {
7
7
  private static $loader;
8
8
 
@@ -22,12 +22,12 @@ class ComposerAutoloaderInit9e2e8e314628d5030f6030b973fa296b
22
22
  return self::$loader;
23
23
  }
24
24
 
25
- spl_autoload_register(array('ComposerAutoloaderInit9e2e8e314628d5030f6030b973fa296b', 'loadClassLoader'), true, true);
25
+ spl_autoload_register(array('ComposerAutoloaderInit15a9301f3620998e3c1693d6c9682cba', 'loadClassLoader'), true, true);
26
26
  self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
27
- spl_autoload_unregister(array('ComposerAutoloaderInit9e2e8e314628d5030f6030b973fa296b', 'loadClassLoader'));
27
+ spl_autoload_unregister(array('ComposerAutoloaderInit15a9301f3620998e3c1693d6c9682cba', 'loadClassLoader'));
28
28
 
29
29
  require __DIR__ . '/autoload_static.php';
30
- call_user_func(\Composer\Autoload\ComposerStaticInit9e2e8e314628d5030f6030b973fa296b::getInitializer($loader));
30
+ call_user_func(\Composer\Autoload\ComposerStaticInit15a9301f3620998e3c1693d6c9682cba::getInitializer($loader));
31
31
 
32
32
  $loader->register(true);
33
33
 
@@ -4,7 +4,7 @@
4
4
 
5
5
  namespace Composer\Autoload;
6
6
 
7
- class ComposerStaticInit9e2e8e314628d5030f6030b973fa296b
7
+ class ComposerStaticInit15a9301f3620998e3c1693d6c9682cba
8
8
  {
9
9
  public static $classMap = array (
10
10
  'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php',
@@ -13,7 +13,7 @@ class ComposerStaticInit9e2e8e314628d5030f6030b973fa296b
13
13
  public static function getInitializer(ClassLoader $loader)
14
14
  {
15
15
  return \Closure::bind(function () use ($loader) {
16
- $loader->classMap = ComposerStaticInit9e2e8e314628d5030f6030b973fa296b::$classMap;
16
+ $loader->classMap = ComposerStaticInit15a9301f3620998e3c1693d6c9682cba::$classMap;
17
17
 
18
18
  }, null, ClassLoader::class);
19
19
  }
@@ -3,7 +3,7 @@
3
3
  'name' => 'automattic/newspack-blocks',
4
4
  'pretty_version' => 'dev-trunk',
5
5
  'version' => 'dev-trunk',
6
- 'reference' => '290edd51f246a797ec47a035fbad0aa5ae086d31',
6
+ 'reference' => '9fa339167934e8287b2ba0a9bad159c6dbdf642a',
7
7
  'type' => 'wordpress-plugin',
8
8
  'install_path' => __DIR__ . '/../../',
9
9
  'aliases' => array(),
@@ -13,7 +13,7 @@
13
13
  'automattic/newspack-blocks' => array(
14
14
  'pretty_version' => 'dev-trunk',
15
15
  'version' => 'dev-trunk',
16
- 'reference' => '290edd51f246a797ec47a035fbad0aa5ae086d31',
16
+ 'reference' => '9fa339167934e8287b2ba0a9bad159c6dbdf642a',
17
17
  'type' => 'wordpress-plugin',
18
18
  'install_path' => __DIR__ . '/../../',
19
19
  'aliases' => array(),