Commit d3df51df669 for woocommerce
commit d3df51df669d7e8adc2a2d9e041451b5e1df53d0
Author: Hannah Tinkler <hannah.tinkler@gmail.com>
Date: Wed Sep 30 15:39:18 2026 +0100
Treat a per page of zero as one when counting push token pages (#69162)
Requesting a page past the last one with a per page of zero threw a division by zero error instead of returning an empty page with the real totals, as WP_Query treats zero as one.
diff --git a/plugins/woocommerce/src/Internal/PushNotifications/DataStores/PushTokensDataStore.php b/plugins/woocommerce/src/Internal/PushNotifications/DataStores/PushTokensDataStore.php
index b5a85da1503..a411f8c3aed 100644
--- a/plugins/woocommerce/src/Internal/PushNotifications/DataStores/PushTokensDataStore.php
+++ b/plugins/woocommerce/src/Internal/PushNotifications/DataStores/PushTokensDataStore.php
@@ -556,7 +556,7 @@ class PushTokensDataStore {
);
$result['total'] = (int) $count_query->found_posts;
- $result['total_pages'] = (int) ceil( $result['total'] / $per_page );
+ $result['total_pages'] = (int) ceil( $result['total'] / max( 1, $per_page ) );
}
$this->tokens_by_roles_cache[ $cache_key ] = $result;
diff --git a/plugins/woocommerce/tests/php/src/Internal/PushNotifications/DataStores/PushTokensDataStoreTest.php b/plugins/woocommerce/tests/php/src/Internal/PushNotifications/DataStores/PushTokensDataStoreTest.php
index 459fe71b927..159617105d9 100644
--- a/plugins/woocommerce/tests/php/src/Internal/PushNotifications/DataStores/PushTokensDataStoreTest.php
+++ b/plugins/woocommerce/tests/php/src/Internal/PushNotifications/DataStores/PushTokensDataStoreTest.php
@@ -1025,6 +1025,22 @@ class PushTokensDataStoreTest extends WC_Unit_Test_Case {
$this->assertSame( 1, $result['total_pages'] );
}
+ /**
+ * @testdox Should treat a per page of zero as one when the requested page is past the last one, as WP_Query does.
+ */
+ public function test_get_tokens_for_roles_reports_totals_for_a_page_past_the_end_with_zero_per_page(): void {
+ $admin_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
+ $data_store = new PushTokensDataStore();
+
+ $this->create_push_token_for_user( $data_store, $admin_id );
+
+ $result = $data_store->get_tokens_for_roles( array( 'administrator' ), 2, 0 );
+
+ $this->assertSame( array(), $result['tokens'] );
+ $this->assertSame( 1, $result['total'] );
+ $this->assertSame( 1, $result['total_pages'] );
+ }
+
/**
* @testdox Should return only the given user's tokens when filtered by user ID.
*/