Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Commit 9d19429

Browse files
prettyboympclaude
andcommitted
Add changelog entry for products last modified caching
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 10cbbde commit 9d19429

4 files changed

Lines changed: 59 additions & 32 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: performance
3+
4+
Cache Store API products last modified timestamp in the object cache to avoid a database query on every request.

plugins/woocommerce/includes/class-wc-post-data.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public static function delete_product_query_transients() {
163163
* @param int $post_id Post ID.
164164
* @param WP_Post $post Post object.
165165
*/
166-
public static function invalidate_products_last_modified( $post_id, $post ) {
166+
public static function invalidate_products_last_modified( $post_id, $post ): void {
167167
if ( $post instanceof WP_Post && in_array( $post->post_type, array( 'product', 'product_variation' ), true ) ) {
168168
wp_cache_delete( 'last_modified', 'wc_products' );
169169
}

plugins/woocommerce/phpstan-baseline.neon

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75816,12 +75816,6 @@ parameters:
7581675816
count: 1
7581775817
path: src/StoreApi/Routes/V1/Products.php
7581875818

75819-
-
75820-
message: '#^Parameter \#2 \$value of method WP_HTTP_Response\:\:header\(\) expects string, int\<min, \-1\>\|int\<1, max\> given\.$#'
75821-
identifier: argument.type
75822-
count: 1
75823-
path: src/StoreApi/Routes/V1/Products.php
75824-
7582575819
-
7582675820
message: '#^Method Automattic\\WooCommerce\\StoreApi\\Routes\\V1\\ProductsById\:\:get_route_response\(\) has parameter \$request with generic class WP_REST_Request but does not specify its types\: T$#'
7582775821
identifier: missingType.generics
@@ -77412,12 +77406,6 @@ parameters:
7741277406
count: 1
7741377407
path: src/StoreApi/Utilities/ProductQuery.php
7741477408

77415-
-
77416-
message: '#^Method Automattic\\WooCommerce\\StoreApi\\Utilities\\ProductQuery\:\:get_last_modified\(\) should return int but returns int\|false\|null\.$#'
77417-
identifier: return.type
77418-
count: 1
77419-
path: src/StoreApi/Utilities/ProductQuery.php
77420-
7742177409
-
7742277410
message: '#^Method Automattic\\WooCommerce\\StoreApi\\Utilities\\ProductQuery\:\:get_objects\(\) has parameter \$request with generic class WP_REST_Request but does not specify its types\: T$#'
7742377411
identifier: missingType.generics

plugins/woocommerce/tests/php/src/Blocks/StoreApi/Utilities/ProductQueryTest.php

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55

66
use Automattic\WooCommerce\StoreApi\Utilities\ProductQuery;
77
use Automattic\WooCommerce\Tests\Blocks\Helpers\FixtureData;
8-
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
98

109
/**
1110
* Unit tests for the ProductQuery::get_last_modified() caching behavior.
1211
*/
13-
class ProductQueryTest extends TestCase {
12+
class ProductQueryTest extends \WC_Unit_Test_Case {
1413

1514
/**
1615
* @var ProductQuery
@@ -20,7 +19,7 @@ class ProductQueryTest extends TestCase {
2019
/**
2120
* Setup test data. Called before every test.
2221
*/
23-
protected function setUp(): void {
22+
public function setUp(): void {
2423
parent::setUp();
2524
$this->product_query = new ProductQuery();
2625
wp_cache_delete( 'last_modified', 'wc_products' );
@@ -29,7 +28,7 @@ protected function setUp(): void {
2928
/**
3029
* @testdox get_last_modified returns null when no products exist.
3130
*/
32-
public function test_get_last_modified_returns_null_when_no_products() {
31+
public function test_get_last_modified_returns_null_when_no_products(): void {
3332
global $wpdb;
3433

3534
// Temporarily remove all product posts to test the null case.
@@ -58,9 +57,14 @@ public function test_get_last_modified_returns_null_when_no_products() {
5857
/**
5958
* @testdox get_last_modified returns an HTTP-date formatted string.
6059
*/
61-
public function test_get_last_modified_returns_http_date_format() {
60+
public function test_get_last_modified_returns_http_date_format(): void {
6261
$fixtures = new FixtureData();
63-
$fixtures->get_simple_product( array( 'name' => 'Test Product', 'regular_price' => 10 ) );
62+
$fixtures->get_simple_product(
63+
array(
64+
'name' => 'Test Product',
65+
'regular_price' => 10,
66+
)
67+
);
6468

6569
$result = $this->product_query->get_last_modified();
6670

@@ -73,9 +77,14 @@ public function test_get_last_modified_returns_http_date_format() {
7377
/**
7478
* @testdox get_last_modified caches the result in the object cache.
7579
*/
76-
public function test_get_last_modified_caches_result() {
80+
public function test_get_last_modified_caches_result(): void {
7781
$fixtures = new FixtureData();
78-
$fixtures->get_simple_product( array( 'name' => 'Test Product', 'regular_price' => 10 ) );
82+
$fixtures->get_simple_product(
83+
array(
84+
'name' => 'Test Product',
85+
'regular_price' => 10,
86+
)
87+
);
7988

8089
// First call seeds the cache.
8190
$result = $this->product_query->get_last_modified();
@@ -89,7 +98,7 @@ public function test_get_last_modified_caches_result() {
8998
/**
9099
* @testdox get_last_modified returns cached value without querying the database.
91100
*/
92-
public function test_get_last_modified_uses_cached_value() {
101+
public function test_get_last_modified_uses_cached_value(): void {
93102
$sentinel = 'Thu, 01 Jan 2099 00:00:00 GMT';
94103
wp_cache_set( 'last_modified', $sentinel, 'wc_products' );
95104

@@ -101,9 +110,14 @@ public function test_get_last_modified_uses_cached_value() {
101110
/**
102111
* @testdox Cache is invalidated when a product post cache is cleaned.
103112
*/
104-
public function test_cache_invalidated_on_product_change() {
113+
public function test_cache_invalidated_on_product_change(): void {
105114
$fixtures = new FixtureData();
106-
$product = $fixtures->get_simple_product( array( 'name' => 'Test Product', 'regular_price' => 10 ) );
115+
$product = $fixtures->get_simple_product(
116+
array(
117+
'name' => 'Test Product',
118+
'regular_price' => 10,
119+
)
120+
);
107121

108122
// Seed the cache.
109123
$this->product_query->get_last_modified();
@@ -118,9 +132,14 @@ public function test_cache_invalidated_on_product_change() {
118132
/**
119133
* @testdox Cache is invalidated when a product variation post cache is cleaned.
120134
*/
121-
public function test_cache_invalidated_on_variation_change() {
135+
public function test_cache_invalidated_on_variation_change(): void {
122136
$fixtures = new FixtureData();
123-
$product = $fixtures->get_simple_product( array( 'name' => 'Test Product', 'regular_price' => 10 ) );
137+
$product = $fixtures->get_simple_product(
138+
array(
139+
'name' => 'Test Product',
140+
'regular_price' => 10,
141+
)
142+
);
124143

125144
// Create a variation post directly to avoid complex variable product setup.
126145
$variation_id = wp_insert_post(
@@ -144,17 +163,28 @@ public function test_cache_invalidated_on_variation_change() {
144163
/**
145164
* @testdox Cache is NOT invalidated when a non-product post cache is cleaned.
146165
*/
147-
public function test_cache_not_invalidated_on_non_product_change() {
166+
public function test_cache_not_invalidated_on_non_product_change(): void {
148167
$fixtures = new FixtureData();
149-
$fixtures->get_simple_product( array( 'name' => 'Test Product', 'regular_price' => 10 ) );
168+
$fixtures->get_simple_product(
169+
array(
170+
'name' => 'Test Product',
171+
'regular_price' => 10,
172+
)
173+
);
150174

151175
// Seed the cache.
152176
$this->product_query->get_last_modified();
153177
$cached_value = wp_cache_get( 'last_modified', 'wc_products' );
154178
$this->assertNotFalse( $cached_value );
155179

156180
// Create and clean a regular post.
157-
$post_id = wp_insert_post( array( 'post_title' => 'Regular Post', 'post_type' => 'post', 'post_status' => 'publish' ) );
181+
$post_id = wp_insert_post(
182+
array(
183+
'post_title' => 'Regular Post',
184+
'post_type' => 'post',
185+
'post_status' => 'publish',
186+
)
187+
);
158188
clean_post_cache( $post_id );
159189

160190
$this->assertSame( $cached_value, wp_cache_get( 'last_modified', 'wc_products' ) );
@@ -163,9 +193,14 @@ public function test_cache_not_invalidated_on_non_product_change() {
163193
/**
164194
* @testdox get_last_modified re-seeds cache from DB after invalidation.
165195
*/
166-
public function test_get_last_modified_reseeds_after_invalidation() {
196+
public function test_get_last_modified_reseeds_after_invalidation(): void {
167197
$fixtures = new FixtureData();
168-
$product = $fixtures->get_simple_product( array( 'name' => 'Test Product', 'regular_price' => 10 ) );
198+
$product = $fixtures->get_simple_product(
199+
array(
200+
'name' => 'Test Product',
201+
'regular_price' => 10,
202+
)
203+
);
169204

170205
// Seed the cache.
171206
$first_result = $this->product_query->get_last_modified();
@@ -179,4 +214,4 @@ public function test_get_last_modified_reseeds_after_invalidation() {
179214
$this->assertNotNull( $second_result );
180215
$this->assertNotFalse( wp_cache_get( 'last_modified', 'wc_products' ) );
181216
}
182-
}
217+
}

0 commit comments

Comments
 (0)