Skip to content

Commit f8bd205

Browse files
committed
chore: Bump version to 7.3.1 and update dependencies
- Updated version numbers in plugin files and package manifests to 7.3.1. - Enhanced Invalidation_Batch_Service to handle permalink retrieval for drafts and trashed posts more effectively. - Added a new method `get_invalidation_permalink()` to improve permalink handling for CloudFront invalidation. - Updated tests to cover new functionality and ensure proper invalidation paths are generated for posts transitioning between statuses.
1 parent 51aab73 commit f8bd205

File tree

6 files changed

+117
-38
lines changed

6 files changed

+117
-38
lines changed

c3-cloudfront-clear-cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* Plugin Name: C3 Cloudfront Cache Controller
4-
* Version: 7.3.0
4+
* Version: 7.3.1
55
* Plugin URI:https://github.com/amimoto-ami/c3-cloudfront-clear-cache
66
* Description: Manage CloudFront Cache and provide some fixtures.
77
* Author: hideokamoto

classes/AWS/Invalidation_Batch_Service.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,22 @@ function __construct( ...$args ) {
6262
*
6363
* @param \WP_Post $post WP post object.
6464
*/
65-
public function set_post( $post ) {
66-
/**
67-
* To get the trashed post's permalink.
68-
*
69-
* @see https://github.com/amimoto-ami/c3-cloudfront-clear-cache/pull/54/files
70-
*/
71-
if ( 'trash' === $post->post_status ) {
72-
// For trashed post, get the permalink when it was published.
73-
$post->post_status = 'publish';
65+
public function set_post( \WP_Post $post ) {
66+
// Clone and normalize so permalink can be derived without mutating the original.
67+
$normalized_post = clone $post;
68+
$original_post_name = $normalized_post->post_name;
69+
70+
if ( 'publish' !== $normalized_post->post_status ) {
71+
$normalized_post->post_status = 'publish';
72+
if ( preg_match( '/__trashed(-\d+)?$/', (string) $normalized_post->post_name ) ) {
73+
$normalized_post->post_name = preg_replace( '/__trashed(-\d+)?$/', '', (string) $normalized_post->post_name );
74+
}
75+
if ( empty( $normalized_post->post_name ) ) {
76+
$normalized_post->post_name = $original_post_name ?: sanitize_title( (string) $normalized_post->post_title );
77+
}
7478
}
75-
$this->post->set_post( $post );
79+
80+
$this->post->set_post( $normalized_post );
7681
}
7782

7883
/**
@@ -85,7 +90,10 @@ public function put_post_invalidation_batch( Invalidation_Batch $invalidation_ba
8590
if ( $post ) {
8691
$this->set_post( $post );
8792
}
88-
$invalidation_batch->put_invalidation_path( $this->post->get_permalink() . '*' );
93+
$permalink = $this->post->get_invalidation_permalink();
94+
if ( ! is_wp_error( $permalink ) && $permalink ) {
95+
$invalidation_batch->put_invalidation_path( $permalink . '*' );
96+
}
8997
$term_links = $this->post->get_the_post_term_links();
9098
foreach ( $term_links as $key => $url ) {
9199
$invalidation_batch->put_invalidation_path( $url );

classes/WP/Post.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,46 @@ public function get_permalink() {
4747
return get_permalink( $this->post );
4848
}
4949

50+
/**
51+
* Get a permalink suitable for CloudFront invalidation.
52+
*
53+
* When a post is not published (e.g. draft), WordPress may return a preview/plain permalink
54+
* like `/?p=123`. In that case the invalidation path becomes `/` and the single permalink
55+
* cache is not cleared. This method attempts to retrieve a "sample" (pretty) permalink.
56+
*
57+
* @return \WP_Error|string|false
58+
*/
59+
public function get_invalidation_permalink() {
60+
if ( ! $this->post ) {
61+
return new \WP_Error( 'Post is required' );
62+
}
63+
64+
$post = $this->post;
65+
$permalink = get_permalink( $post );
66+
67+
// If the permalink looks like a plain/preview URL, try to retrieve a sample (pretty) permalink.
68+
if ( is_string( $permalink ) ) {
69+
$path = parse_url( $permalink, PHP_URL_PATH );
70+
$looks_plain = ( ! $path || '/' === $path || false !== strpos( $permalink, '?p=' ) );
71+
72+
if ( $looks_plain && function_exists( 'get_post_permalink' ) ) {
73+
$sample_permalink = get_post_permalink( $post, false, true );
74+
if ( is_string( $sample_permalink ) && '' !== $sample_permalink ) {
75+
$permalink = $sample_permalink;
76+
}
77+
}
78+
79+
if ( $looks_plain && function_exists( 'get_sample_permalink' ) && isset( $post->ID ) ) {
80+
$sample = get_sample_permalink( (int) $post->ID );
81+
if ( is_array( $sample ) && isset( $sample[0] ) && is_string( $sample[0] ) && '' !== $sample[0] ) {
82+
$permalink = $sample[0];
83+
}
84+
}
85+
}
86+
87+
return $permalink;
88+
}
89+
5090
/**
5191
* Parse the url
5292
*

package-lock.json

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
}
1111
},
1212
"name": "trunk",
13-
"version": "7.3.0",
13+
"version": "7.3.1",
1414
"main": "index.js",
1515
"directories": {
1616
"test": "tests"

tests/AWS/Invalidation_Batch_Service_Test.php

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public function test_overwrite_invalidation_item_by_post_name() {
5454
}
5555
return $items;
5656
}, 10, 2 );
57-
58-
// ケース1: 上書きされるべきケース
57+
58+
// Case 1: Item should be overwritten by filter.
5959
$post1 = $this->factory->post->create_and_get( array(
6060
'post_status' => 'publish',
6161
'post_name' => 'should-overwritten',
@@ -68,8 +68,8 @@ public function test_overwrite_invalidation_item_by_post_name() {
6868
),
6969
'Quantity' => 1
7070
], $result[ 'InvalidationBatch' ][ 'Paths' ]);
71-
72-
// ケース2: 上書きされないケース
71+
72+
// Case 2: Item should not be overwritten.
7373
$post2 = $this->factory->post->create_and_get( array(
7474
'post_status' => 'publish',
7575
'post_name' => 'should-not-overwritten',
@@ -101,6 +101,31 @@ public function test_get_the_un_published_post_invalidation_paths() {
101101
) , $result[ 'InvalidationBatch' ][ 'Paths' ] );
102102
}
103103

104+
public function test_get_the_published_to_draft_post_invalidation_paths() {
105+
$post_id = $this->factory->post->create( array(
106+
'post_status' => 'publish',
107+
'post_name' => 'published-to-draft',
108+
'post_type' => 'post',
109+
) );
110+
111+
wp_update_post( array(
112+
'ID' => $post_id,
113+
'post_status' => 'draft',
114+
) );
115+
116+
$post = get_post( $post_id );
117+
118+
$target = new AWS\Invalidation_Batch_Service();
119+
$result = $target->create_batch_by_post( 'localhost', 'EXXX', $post );
120+
$this->assertEquals( array(
121+
'Items' => array(
122+
'localhost',
123+
'/published-to-draft/*',
124+
),
125+
'Quantity' => 2
126+
), $result[ 'InvalidationBatch' ][ 'Paths' ] );
127+
}
128+
104129
public function test_get_invalidation_path_for_all() {
105130
$target = new AWS\Invalidation_Batch_Service();
106131
$result = $target->create_batch_for_all( 'EXXXX' );
@@ -113,7 +138,7 @@ public function test_get_invalidation_path_for_all() {
113138
}
114139

115140
public function test_create_batch_by_posts() {
116-
// ケース1: 1つの投稿
141+
// Case 1: Single post.
117142
$post1 = $this->factory->post->create_and_get( array(
118143
'post_status' => 'publish',
119144
'post_name' => 'hello-world',
@@ -128,8 +153,8 @@ public function test_create_batch_by_posts() {
128153
],
129154
"Quantity" => 2
130155
], $result[ 'InvalidationBatch' ][ 'Paths' ]);
131-
132-
// ケース2: 複数の投稿
156+
157+
// Case 2: Multiple posts.
133158
$post2 = $this->factory->post->create_and_get( array(
134159
'post_status' => 'publish',
135160
'post_name' => 'see-you',
@@ -138,7 +163,7 @@ public function test_create_batch_by_posts() {
138163
'post_status' => 'trash',
139164
'post_name' => 'good-bye',
140165
) );
141-
166+
142167
$result = $target->create_batch_by_posts( 'localhost', 'EXXXX', [$post2, $post3] );
143168
$this->assertEquals([
144169
"Items" => [
@@ -152,17 +177,17 @@ public function test_create_batch_by_posts() {
152177

153178
/**
154179
* Test Case: c3_invalidation_post_batch_home_path filter hook functionality
155-
*
180+
*
156181
* Overview:
157182
* This test verifies that the c3_invalidation_post_batch_home_path filter hook
158183
* correctly allows customization of the home path during single post invalidation.
159-
*
184+
*
160185
* Expected Behavior:
161186
* - The filter should receive the original home path and post object as parameters
162187
* - When the post slug matches 'custom-home', the filter should return '/custom-homepage/'
163188
* - The invalidation batch should contain both the custom home path and the post-specific path
164189
* - Other posts should not be affected by this filter condition
165-
*
190+
*
166191
* Test Method:
167192
* 1. Register a filter that modifies home path for posts with slug 'custom-home'
168193
* 2. Create a test post with the matching slug 'custom-home'
@@ -197,17 +222,17 @@ public function test_c3_invalidation_post_batch_home_path_filter() {
197222

198223
/**
199224
* Test Case: c3_invalidation_posts_batch_home_path filter hook functionality
200-
*
225+
*
201226
* Overview:
202227
* This test verifies that the c3_invalidation_posts_batch_home_path filter hook
203228
* correctly allows customization of the home path during multiple posts invalidation.
204-
*
229+
*
205230
* Expected Behavior:
206231
* - The filter should receive the original home path and array of post objects as parameters
207232
* - When more than 1 post is being processed, the filter should return '/bulk-update-homepage/'
208233
* - The invalidation batch should contain the custom home path plus individual post paths
209234
* - Single post batches should not trigger this filter condition
210-
*
235+
*
211236
* Test Method:
212237
* 1. Register a filter that modifies home path when processing multiple posts (count > 1)
213238
* 2. Create two test posts with different slugs ('post-one' and 'post-two')
@@ -247,17 +272,17 @@ public function test_c3_invalidation_posts_batch_home_path_filter() {
247272

248273
/**
249274
* Test Case: c3_invalidation_manual_batch_all_path filter hook functionality
250-
*
275+
*
251276
* Overview:
252277
* This test verifies that the c3_invalidation_manual_batch_all_path filter hook
253278
* correctly allows customization of the path pattern used for manual "clear all cache" operations.
254-
*
279+
*
255280
* Expected Behavior:
256281
* - The filter should receive the default all-clear path pattern ('/*') as a parameter
257282
* - The filter should be able to return a custom path pattern for clearing all cache
258283
* - The invalidation batch should contain only the custom path pattern
259284
* - This provides more granular control over manual cache clearing operations
260-
*
285+
*
261286
* Test Method:
262287
* 1. Register a filter that replaces the default '/*' pattern with '/custom-all-path/*'
263288
* 2. Call create_batch_for_all() method to trigger manual all-cache clearing
@@ -283,18 +308,18 @@ public function test_c3_invalidation_manual_batch_all_path_filter() {
283308

284309
/**
285310
* Test Case: WordPress subdirectory installation support
286-
*
311+
*
287312
* Overview:
288313
* This test verifies that the new path adjustment hooks correctly handle WordPress
289314
* installations in subdirectories by automatically including subdirectory paths
290315
* in invalidation requests through WordPress's standard home_url() function.
291-
*
316+
*
292317
* Expected Behavior:
293318
* - When WordPress is installed in a subdirectory (e.g., /blog/), home_url() should return the subdirectory path
294319
* - The c3_invalidation_post_batch_home_path filter should receive the subdirectory path automatically
295320
* - Invalidation batches should include both the subdirectory home path and post-specific paths
296321
* - The plugin should work seamlessly without manual configuration for subdirectory installations
297-
*
322+
*
298323
* Test Method:
299324
* 1. Mock WordPress home_url() to simulate a subdirectory installation (/blog/)
300325
* 2. Register a filter to verify the subdirectory path is correctly passed to the hook
@@ -335,17 +360,17 @@ public function test_subdirectory_installation_support() {
335360

336361
/**
337362
* Test Case: Subdirectory-specific path customization
338-
*
363+
*
339364
* Overview:
340365
* This test verifies that developers can customize invalidation paths specifically
341366
* for subdirectory installations using the new hooks, allowing for environment-specific
342367
* or subdirectory-specific cache invalidation strategies.
343-
*
368+
*
344369
* Expected Behavior:
345370
* - Developers should be able to detect subdirectory installations in their filters
346371
* - Custom paths can be returned that are specific to the subdirectory environment
347372
* - The hooks should work correctly with both subdirectory and root installations
348-
*
373+
*
349374
* Test Method:
350375
* 1. Create a filter that detects subdirectory installations and returns custom paths
351376
* 2. Test with both subdirectory and root installation scenarios

0 commit comments

Comments
 (0)