@automattic/newspack-blocks 3.6.0 → 3.6.1

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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [3.6.1](https://github.com/Automattic/newspack-blocks/compare/v3.6.0...v3.6.1) (2024-08-12)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * **wc-subs-limiting:** handle not-logged-in user ([#1816](https://github.com/Automattic/newspack-blocks/issues/1816)) ([d85a818](https://github.com/Automattic/newspack-blocks/commit/d85a8188515cf1e90256b7ecb2b3dbdb5e83ec10))
7
+
1
8
  # [3.6.0](https://github.com/Automattic/newspack-blocks/compare/v3.5.0...v3.6.0) (2024-07-30)
2
9
 
3
10
 
@@ -34,6 +34,7 @@ final class Modal_Checkout {
34
34
  add_action( 'wp_loaded', [ __CLASS__, 'process_checkout_request' ], 5 );
35
35
  add_filter( 'wp_redirect', [ __CLASS__, 'pass_url_param_on_redirect' ] );
36
36
  add_filter( 'woocommerce_cart_product_cannot_be_purchased_message', [ __CLASS__, 'woocommerce_cart_product_cannot_be_purchased_message' ], 10, 2 );
37
+ add_filter( 'woocommerce_add_error', [ __CLASS__, 'hide_expiry_message_shop_link' ] );
37
38
  add_action( 'wp_footer', [ __CLASS__, 'render_modal_markup' ], 100 );
38
39
  add_action( 'wp_footer', [ __CLASS__, 'render_variation_selection' ], 100 );
39
40
  add_action( 'wp_enqueue_scripts', [ __CLASS__, 'enqueue_scripts' ] );
@@ -89,6 +90,7 @@ final class Modal_Checkout {
89
90
  add_filter( 'woocommerce_subscription_variation_is_purchasable', [ 'WCS_Limiter', 'is_purchasable_renewal' ], 12, 2 );
90
91
  add_filter( 'woocommerce_valid_order_statuses_for_order_again', [ 'WCS_Limiter', 'filter_order_again_statuses_for_limited_subscriptions' ] );
91
92
  }
93
+ add_filter( 'woocommerce_subscriptions_product_limited_for_user', [ __CLASS__, 'subscriptions_product_limited_for_user' ], 10, 3 );
92
94
  }
93
95
 
94
96
  /**
@@ -1026,6 +1028,15 @@ final class Modal_Checkout {
1026
1028
  return $location;
1027
1029
  }
1028
1030
 
1031
+ /**
1032
+ * Alternative error message to show when limiting a subscription product's purchase.
1033
+ *
1034
+ * @return string
1035
+ */
1036
+ public static function get_subscription_limited_message() {
1037
+ return __( 'You may only have one subscription of this product at a time.', 'newspack-blocks' );
1038
+ }
1039
+
1029
1040
  /**
1030
1041
  * Filters the error message shown when a product can't be added to the cart.
1031
1042
  *
@@ -1038,13 +1049,28 @@ final class Modal_Checkout {
1038
1049
  if ( method_exists( 'WCS_Limiter', 'is_purchasable' ) ) {
1039
1050
  $product = \wc_get_product( $product_data->get_id() );
1040
1051
  if ( ! \WCS_Limiter::is_purchasable( false, $product ) ) {
1041
- $message .= ' ' . __( 'You may only have one subscription of this product at a time.', 'newspack-blocks' );
1052
+ $message .= ' ' . self::get_subscription_limited_message();
1042
1053
  }
1043
1054
  }
1044
1055
 
1045
1056
  return $message;
1046
1057
  }
1047
1058
 
1059
+ /**
1060
+ * We don't want to show the Shop page link in modal checkout because the Shop page doesn't work well inside.
1061
+ * Unfortunately Woo doesn't provide a filter for this message, so we need to detect it by string matching.
1062
+ * May not work if the message has been translated or modified elsewhere.
1063
+ *
1064
+ * @param string $message The message.
1065
+ * @return string
1066
+ */
1067
+ public static function hide_expiry_message_shop_link( $message ) {
1068
+ if ( self::is_modal_checkout() && strpos( $message, 'Sorry, your session has expired' ) !== false ) {
1069
+ return __( 'Could not complete this transaction. Please contact us for assistance.', 'newspack-blocks' );
1070
+ }
1071
+ return $message;
1072
+ }
1073
+
1048
1074
  /**
1049
1075
  * Is this request using the modal checkout?
1050
1076
  */
@@ -1124,6 +1150,22 @@ final class Modal_Checkout {
1124
1150
  return $option_value;
1125
1151
  }
1126
1152
 
1153
+ /**
1154
+ * Get user from email.
1155
+ *
1156
+ * @return false|int User ID if found by email address, false otherwise.
1157
+ */
1158
+ private static function get_user_id_from_email() {
1159
+ $billing_email = filter_input( INPUT_POST, 'billing_email', FILTER_SANITIZE_EMAIL );
1160
+ if ( $billing_email ) {
1161
+ $customer = \get_user_by( 'email', $billing_email );
1162
+ if ( $customer ) {
1163
+ return $customer->ID;
1164
+ }
1165
+ }
1166
+ return false;
1167
+ }
1168
+
1127
1169
  /**
1128
1170
  * If a reader tries to make a purchase with an email address that
1129
1171
  * has been previously registered, automatically associate the transaction
@@ -1137,12 +1179,9 @@ final class Modal_Checkout {
1137
1179
  if ( ! self::is_modal_checkout() ) {
1138
1180
  return $customer_id;
1139
1181
  }
1140
- $billing_email = filter_input( INPUT_POST, 'billing_email', FILTER_SANITIZE_EMAIL );
1141
- if ( $billing_email ) {
1142
- $customer = \get_user_by( 'email', $billing_email );
1143
- if ( $customer ) {
1144
- $customer_id = $customer->ID;
1145
- }
1182
+ $id_from_email = self::get_user_id_from_email();
1183
+ if ( $id_from_email ) {
1184
+ return $id_from_email;
1146
1185
  }
1147
1186
  return $customer_id;
1148
1187
  }
@@ -1191,5 +1230,26 @@ final class Modal_Checkout {
1191
1230
  }
1192
1231
  return $modules;
1193
1232
  }
1233
+
1234
+ /**
1235
+ * Trigger the subscriptions-limiting logic, using the user gleaned from the email address.
1236
+ *
1237
+ * @param bool $is_limited_for_user If the subscription should be limited.
1238
+ * @param int|WC_Product $product A WC_Product object or the ID of a product.
1239
+ * @param int $user_id The user ID.
1240
+ */
1241
+ public static function subscriptions_product_limited_for_user( $is_limited_for_user, $product, $user_id ) {
1242
+ if ( $user_id !== 0 ) {
1243
+ return $is_limited_for_user;
1244
+ }
1245
+ $id_from_email = self::get_user_id_from_email();
1246
+ if ( $id_from_email ) {
1247
+ $is_limited_for_user = wcs_is_product_limited_for_user( $product, $id_from_email );
1248
+ if ( $is_limited_for_user ) {
1249
+ add_filter( 'woocommerce_cart_item_removed_message', [ __CLASS__, 'get_subscription_limited_message' ], 10, 2 );
1250
+ }
1251
+ }
1252
+ return $is_limited_for_user;
1253
+ }
1194
1254
  }
1195
1255
  Modal_Checkout::init();
@@ -7,7 +7,7 @@
7
7
  * Author URI: https://newspack.com/
8
8
  * Text Domain: newspack-blocks
9
9
  * Domain Path: /languages
10
- * Version: 3.6.0
10
+ * Version: 3.6.1
11
11
  *
12
12
  * @package Newspack_Blocks
13
13
  */
@@ -15,7 +15,7 @@
15
15
  define( 'NEWSPACK_BLOCKS__PLUGIN_FILE', __FILE__ );
16
16
  define( 'NEWSPACK_BLOCKS__BLOCKS_DIRECTORY', 'dist/' );
17
17
  define( 'NEWSPACK_BLOCKS__PLUGIN_DIR', plugin_dir_path( NEWSPACK_BLOCKS__PLUGIN_FILE ) );
18
- define( 'NEWSPACK_BLOCKS__VERSION', '3.6.0' );
18
+ define( 'NEWSPACK_BLOCKS__VERSION', '3.6.1' );
19
19
 
20
20
  require_once NEWSPACK_BLOCKS__PLUGIN_DIR . 'includes/class-newspack-blocks.php';
21
21
  require_once NEWSPACK_BLOCKS__PLUGIN_DIR . 'includes/class-newspack-blocks-api.php';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automattic/newspack-blocks",
3
- "version": "3.6.0",
3
+ "version": "3.6.1",
4
4
  "author": "Automattic",
5
5
  "devDependencies": {
6
6
  "@rushstack/eslint-patch": "^1.10.3",
@@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
22
22
 
23
23
  require_once __DIR__ . '/composer/autoload_real.php';
24
24
 
25
- return ComposerAutoloaderInit561a3c3ccf297ba8c6b0b3c8c8ae1a81::getLoader();
25
+ return ComposerAutoloaderInit5a66c0d203d139c8911f8f5d70bdba40::getLoader();
@@ -2,7 +2,7 @@
2
2
 
3
3
  // autoload_real.php @generated by Composer
4
4
 
5
- class ComposerAutoloaderInit561a3c3ccf297ba8c6b0b3c8c8ae1a81
5
+ class ComposerAutoloaderInit5a66c0d203d139c8911f8f5d70bdba40
6
6
  {
7
7
  private static $loader;
8
8
 
@@ -22,12 +22,12 @@ class ComposerAutoloaderInit561a3c3ccf297ba8c6b0b3c8c8ae1a81
22
22
  return self::$loader;
23
23
  }
24
24
 
25
- spl_autoload_register(array('ComposerAutoloaderInit561a3c3ccf297ba8c6b0b3c8c8ae1a81', 'loadClassLoader'), true, true);
25
+ spl_autoload_register(array('ComposerAutoloaderInit5a66c0d203d139c8911f8f5d70bdba40', 'loadClassLoader'), true, true);
26
26
  self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
27
- spl_autoload_unregister(array('ComposerAutoloaderInit561a3c3ccf297ba8c6b0b3c8c8ae1a81', 'loadClassLoader'));
27
+ spl_autoload_unregister(array('ComposerAutoloaderInit5a66c0d203d139c8911f8f5d70bdba40', 'loadClassLoader'));
28
28
 
29
29
  require __DIR__ . '/autoload_static.php';
30
- call_user_func(\Composer\Autoload\ComposerStaticInit561a3c3ccf297ba8c6b0b3c8c8ae1a81::getInitializer($loader));
30
+ call_user_func(\Composer\Autoload\ComposerStaticInit5a66c0d203d139c8911f8f5d70bdba40::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 ComposerStaticInit561a3c3ccf297ba8c6b0b3c8c8ae1a81
7
+ class ComposerStaticInit5a66c0d203d139c8911f8f5d70bdba40
8
8
  {
9
9
  public static $classMap = array (
10
10
  'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php',
@@ -13,7 +13,7 @@ class ComposerStaticInit561a3c3ccf297ba8c6b0b3c8c8ae1a81
13
13
  public static function getInitializer(ClassLoader $loader)
14
14
  {
15
15
  return \Closure::bind(function () use ($loader) {
16
- $loader->classMap = ComposerStaticInit561a3c3ccf297ba8c6b0b3c8c8ae1a81::$classMap;
16
+ $loader->classMap = ComposerStaticInit5a66c0d203d139c8911f8f5d70bdba40::$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' => 'e3f1a1bfc530a1e0280124c6cb5c22475cc4dd01',
6
+ 'reference' => 'd85a8188515cf1e90256b7ecb2b3dbdb5e83ec10',
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' => 'e3f1a1bfc530a1e0280124c6cb5c22475cc4dd01',
16
+ 'reference' => 'd85a8188515cf1e90256b7ecb2b3dbdb5e83ec10',
17
17
  'type' => 'wordpress-plugin',
18
18
  'install_path' => __DIR__ . '/../../',
19
19
  'aliases' => array(),