Skip to content

Commit

Permalink
Merge pull request #78 from amimoto-ami/feat/hooks/c3_invalidation_items
Browse files Browse the repository at this point in the history
feat: re-apply 'c3_invalidation_items' hook
  • Loading branch information
wokamoto authored Nov 2, 2021
2 parents 2af5ca2 + 6b54033 commit 6ef006e
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 12 deletions.
12 changes: 0 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,10 @@ jobs:
strategy:
matrix:
include:
#- php: 8.0
# wp: trunk
# experimental: true
- php: 7.4
wp: trunk
experimental: true
- php: 7.4
wp: latest
- php: 7.3
wp: trunk
experimental: true
- php: 7.3
wp: latest
- php: 7.2
wp: trunk
experimental: true
- php: 7.2
wp: latest
steps:
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ add_filter( 'c3_invalidation_item_limits', function( $limits ) {
} );
```

### Customize/Overwrite the invalidation path

Using the `c3_invalidation_items` filter, we can update the invalidation path.

```php
add_filter( 'c3_invalidation_items', function($items){
return array('/*');
});
```

```php
add_filter( 'c3_invalidation_items', function( $items, $post ) {
if ( 'should-overwritten' === $post->post_name) {
return ['/slug-overwritten'];
}
return $items;
}, 10, 2 );
```

### Change or Stop loading bundled AWS SDK (Since v6.0.0)

Use `c3_aws_sdk_path` filter, to relace the AWS SDK library path.
Expand Down
10 changes: 10 additions & 0 deletions classes/AWS/Invalidation_Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ public function put_invalidation_path( string $path ) {
$this->items = array_unique( $this->items );
}

/**
* Apply WordPress filter hook.
* We can overwrite the invalidation item by manually
*
* @param \WP_Post $post WordPress Post object.
*/
public function apply_invalidation_item_filter( \WP_Post $post = null ) {
$this->items = apply_filters( 'c3_invalidation_items', $this->items, $post );
}

/**
* Get the invalidation target items.
* If over the defined limit, should return '/*' to remove all cache.
Expand Down
1 change: 1 addition & 0 deletions classes/AWS/Invalidation_Batch_Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public function put_post_invalidation_batch( Invalidation_Batch $invalidation_ba
foreach ( $term_links as $key => $url ) {
$invalidation_batch->put_invalidation_path( $url );
}
$invalidation_batch->apply_invalidation_item_filter( $post );
return $invalidation_batch;
}

Expand Down
45 changes: 45 additions & 0 deletions tests/AWS/Invalidation_Batch_Service_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,51 @@ public function test_get_the_published_post_invalidation_paths() {
'Quantity' => 2
), $result[ 'InvalidationBatch' ][ 'Paths' ] );
}

/**
* @dataProvider provide_overwrite_invalidation_item_by_post_name_test_case
*/
public function test_overwrite_invalidation_item_by_post_name( $post, $expected ) {
add_filter( 'c3_invalidation_items', function( $items, $post ) {
if ( 'should-overwritten' === $post->post_name) {
return ['/slug-overwritten'];
}
return $items;
}, 10, 2 );
$target = new AWS\Invalidation_Batch_Service();
$result = $target->create_batch_by_post( 'localhost', 'EXXX', $post );
$this->assertEquals( $expected, $result[ 'InvalidationBatch' ][ 'Paths' ] );
}
public function provide_overwrite_invalidation_item_by_post_name_test_case() {
return [
[
$this->factory->post->create_and_get( array(
'post_status' => 'publish',
'post_name' => 'should-overwritten',
) ),
[
'Items' => array(
'/slug-overwritten',
),
'Quantity' => 1
]
],
[
$this->factory->post->create_and_get( array(
'post_status' => 'publish',
'post_name' => 'should-not-overwritten',
) ),
[
'Items' => array(
'localhost',
'/should-not-overwritten/*',
),
'Quantity' => 2
]
]
];
}

public function test_get_the_un_published_post_invalidation_paths() {
$post = $this->factory->post->create_and_get( array(
'post_status' => 'trash',
Expand Down

0 comments on commit 6ef006e

Please sign in to comment.