Skip to content

Commit 9003531

Browse files
committed
Clear post type cache after each request
1 parent e5588b7 commit 9003531

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

includes/rest-api/class-acf-rest-types-endpoint.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public function __construct() {
4444

4545
// Clean up null entries from the response
4646
add_filter( 'rest_pre_echo_response', array( $this, 'clean_types_response' ), 10, 3 );
47+
48+
// Clear cache after each request to make the endpoint stateless
49+
add_action( 'rest_request_after_callbacks', array( $this, 'clear_cache_for_types_request' ), 10, 1 );
4750
}
4851

4952
/**
@@ -74,7 +77,7 @@ public function filter_types_request( $response, $handler, $request ) {
7477
return $response;
7578
}
7679

77-
// Get post types, calculating once and reusing for the entire request
80+
// Get post types with caching within this single request
7881
if ( null === $this->cached_post_types ) {
7982
$this->cached_post_types = $this->get_source_post_types( $source );
8083
}
@@ -118,6 +121,7 @@ public function filter_post_type( $response, $post_type, $request ) {
118121
return $response;
119122
}
120123

124+
// Get post types with caching within this single request
121125
if ( null === $this->cached_post_types ) {
122126
$this->cached_post_types = $this->get_source_post_types( $source );
123127
}
@@ -391,4 +395,17 @@ function ( $entry ) {
391395

392396
return $response;
393397
}
398+
399+
/**
400+
* Clear cache for types endpoint requests to prevent cross-contamination
401+
*
402+
* @since SCF 6.5.0
403+
*
404+
* @param mixed $response The current response.
405+
*/
406+
public function clear_cache_for_types_request( $response ) {
407+
// Clear cache to prevent cross-contamination and make the endpoint stateless
408+
$this->cached_post_types = null;
409+
return $response;
410+
}
394411
}

0 commit comments

Comments
 (0)