Commit 88173458f23 for woocommerce
commit 88173458f23a656bf2085c6812dbce0a859fff02
Author: Hicham Boushaba <hicham.boushaba@gmail.com>
Date: Mon Nov 30 18:30:31 2026 +0000
Return the WordPress.com blog ID when registering a push token (#71162)
* Return the WordPress.com blog ID when registering a push token
Push notifications sent through WordPress.com carry the store's
WordPress.com blog ID, but the mobile apps can't look that ID up for
stores they connect to with site credentials. The registration response
now includes it as blog_id, so the apps can ignore notifications from
another store that still holds the device's token.
WpcomNotificationDispatcher::get_site_id() reads the ID for both the
response and dispatch(), so the two can't drift apart.
* Add changelog entry for the push token registration blog ID
* Remove a redundant line from the push token create() docblock
* Read the push notification site ID through Jetpack's accessor
Manager::get_site_id() is the method Jetpack provides for this ID.
Keep requiring a positive integer on top of it, since Jetpack doesn't
promise one.
* Drop the unreachable null case from the blog_id schema description
* Update the get_site_id() @since tag to 11.5.0
diff --git a/plugins/woocommerce/changelog/woomob-4436-push-token-registration-blog-id b/plugins/woocommerce/changelog/woomob-4436-push-token-registration-blog-id
new file mode 100644
index 00000000000..e0b4050a2bf
--- /dev/null
+++ b/plugins/woocommerce/changelog/woomob-4436-push-token-registration-blog-id
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Return the WordPress.com blog ID from push token registration for the mobile apps; no merchant change.
diff --git a/plugins/woocommerce/src/Internal/PushNotifications/Controllers/PushTokenRestController.php b/plugins/woocommerce/src/Internal/PushNotifications/Controllers/PushTokenRestController.php
index 27f0c6f5e6b..d9a78899945 100644
--- a/plugins/woocommerce/src/Internal/PushNotifications/Controllers/PushTokenRestController.php
+++ b/plugins/woocommerce/src/Internal/PushNotifications/Controllers/PushTokenRestController.php
@@ -7,6 +7,7 @@ namespace Automattic\WooCommerce\Internal\PushNotifications\Controllers;
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Internal\PushNotifications\DataStores\PushTokensDataStore;
+use Automattic\WooCommerce\Internal\PushNotifications\Dispatchers\WpcomNotificationDispatcher;
use Automattic\WooCommerce\Internal\PushNotifications\Entities\PushToken;
use Automattic\WooCommerce\Internal\PushNotifications\Exceptions\PushTokenNotFoundException;
use Automattic\WooCommerce\Internal\PushNotifications\PushNotifications;
@@ -126,7 +127,19 @@ class PushTokenRestController extends RestApiControllerBase {
array(
'title' => 'push_tokens',
'properties' => array(
- 'tokens' => array(
+ 'id' => array(
+ 'description' => __( 'The ID of the registered token. Only returned when registering a token.', 'woocommerce' ),
+ 'type' => 'integer',
+ 'context' => array( 'view' ),
+ 'readonly' => true,
+ ),
+ 'blog_id' => array(
+ 'description' => __( "The store's WordPress.com blog ID, which its push notifications carry. Not the multisite blog ID from get_current_blog_id(). Only returned when registering a token.", 'woocommerce' ),
+ 'type' => array( 'integer', 'null' ),
+ 'context' => array( 'view' ),
+ 'readonly' => true,
+ ),
+ 'tokens' => array(
'description' => __( 'The push tokens registered on this store.', 'woocommerce' ),
'type' => 'array',
'context' => array( 'view' ),
@@ -274,7 +287,7 @@ class PushTokenRestController extends RestApiControllerBase {
}
/**
- * Creates a push token record.
+ * Creates a push token record and returns its ID and the store's WordPress.com blog ID.
*
* @since 10.6.0
*
@@ -311,7 +324,10 @@ class PushTokenRestController extends RestApiControllerBase {
}
return new WP_REST_Response(
- array( 'id' => $push_token->get_id() ),
+ array(
+ 'id' => $push_token->get_id(),
+ 'blog_id' => wc_get_container()->get( WpcomNotificationDispatcher::class )->get_site_id(),
+ ),
WP_Http::CREATED
);
}
diff --git a/plugins/woocommerce/src/Internal/PushNotifications/Dispatchers/WpcomNotificationDispatcher.php b/plugins/woocommerce/src/Internal/PushNotifications/Dispatchers/WpcomNotificationDispatcher.php
index 10506d3330b..456ee24ffa1 100644
--- a/plugins/woocommerce/src/Internal/PushNotifications/Dispatchers/WpcomNotificationDispatcher.php
+++ b/plugins/woocommerce/src/Internal/PushNotifications/Dispatchers/WpcomNotificationDispatcher.php
@@ -7,11 +7,11 @@ namespace Automattic\WooCommerce\Internal\PushNotifications\Dispatchers;
defined( 'ABSPATH' ) || exit;
use Automattic\Jetpack\Connection\Client as Jetpack_Connection_Client;
+use Automattic\Jetpack\Connection\Manager as Jetpack_Connection_Manager;
use Automattic\WooCommerce\Internal\PushNotifications\Entities\PushToken;
use Automattic\WooCommerce\Internal\PushNotifications\Enums\SendOutcome;
use Automattic\WooCommerce\Internal\PushNotifications\Notifications\Notification;
use Automattic\WooCommerce\Internal\PushNotifications\Services\NotificationStepLogger;
-use Jetpack_Options;
use WP_Error;
use WP_Http;
@@ -73,6 +73,19 @@ class WpcomNotificationDispatcher {
$this->step_logger = $step_logger;
}
+ /**
+ * Returns the store's WordPress.com blog ID used for push notifications, or null when Jetpack has no ID.
+ *
+ * @since 11.5.0
+ *
+ * @return int|null
+ */
+ public function get_site_id(): ?int {
+ $site_id = Jetpack_Connection_Manager::get_site_id( true );
+
+ return is_int( $site_id ) && $site_id > 0 ? $site_id : null;
+ }
+
/**
* Dispatches a notification with push tokens to WPCOM.
*
@@ -86,9 +99,9 @@ class WpcomNotificationDispatcher {
* @since 10.7.0
*/
public function dispatch( Notification $notification, array $tokens ): array {
- $site_id = class_exists( Jetpack_Options::class ) ? Jetpack_Options::get_option( 'id' ) : null;
+ $site_id = $this->get_site_id();
- if ( empty( $site_id ) ) {
+ if ( null === $site_id ) {
$this->step_logger->log_failure(
$notification,
'dispatched',
diff --git a/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Controllers/PushTokenRestControllerTest.php b/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Controllers/PushTokenRestControllerTest.php
index 8c9b82069cf..2ea0ecb690d 100644
--- a/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Controllers/PushTokenRestControllerTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Controllers/PushTokenRestControllerTest.php
@@ -231,6 +231,26 @@ class PushTokenRestControllerTest extends WC_Unit_Test_Case {
);
}
+ /**
+ * @testdox Test it returns the WordPress.com blog ID when creating a push token.
+ */
+ public function test_create_returns_the_blog_id(): void {
+ wp_set_current_user( $this->user_id );
+ add_filter( 'pre_option_jetpack_options', static fn () => array( 'id' => 12345 ) );
+
+ $request = new WP_REST_Request( 'POST', '/wc-push-notifications/push-tokens' );
+ $request->set_param( 'token', 'test_android_token_123' );
+ $request->set_param( 'platform', PushToken::PLATFORM_ANDROID );
+ $request->set_param( 'device_uuid', 'test-device-uuid-789' );
+ $request->set_param( 'origin', PushToken::ORIGIN_WOOCOMMERCE_ANDROID );
+ $request->set_param( 'device_locale', 'en_US' );
+
+ $response = $this->server->dispatch( $request );
+
+ $this->assertSame( WP_Http::CREATED, $response->get_status() );
+ $this->assertSame( 12345, $response->get_data()['blog_id'] );
+ }
+
/**
* @testdox Test it updates an existing push token by token value.
*/
diff --git a/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Dispatchers/WpcomNotificationDispatcherTest.php b/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Dispatchers/WpcomNotificationDispatcherTest.php
index 91cc8288d74..c95a3d93b8d 100644
--- a/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Dispatchers/WpcomNotificationDispatcherTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/PushNotifications/Dispatchers/WpcomNotificationDispatcherTest.php
@@ -205,7 +205,7 @@ class WpcomNotificationDispatcherTest extends WC_Unit_Test_Case {
$this->sut->dispatch( $notification, $tokens );
$this->assertStringContainsString(
- WpcomNotificationDispatcher::SEND_ENDPOINT,
+ '/sites/12345/' . WpcomNotificationDispatcher::SEND_ENDPOINT,
$this->captured_url
);