Hi @papazetis
Thank you for using our plugin!
That is correct, our plugin needs at least the fields id
and type
(or id
and taxonomy
in case of a taxonomy endpoint) to determine the correct object type. If it can’t determine the object type correctly it cannot flush it when that object type is saved/updated.
To determine the relations within a cache it would need even more fields to work correctly. It would need a combination of fields (which exactly depends on the endpoint and the object type within it):
id
and post_type
id
, type
and ( slug
or status
)
id
, slug
and _links
taxonomy
, id
, name
and slug
taxonomy
and term_id
If you want to keep the number of fields in the output limited to less fields than the ones mentioned above, you would have to help the plugin determine the correct object type (using the wp_rest_cache/determine_object_type
filter) and the correct relations (using the wp_rest_cache/process_cache_relations
)
Some small examples of how to use the filters:
/**
* Help determine the correct object type for our custom endpoint.
*
* @param string $object_type The object type as determined by the plugin.
* @param string $cache_key The cache key for the current cache record.
* @param mixed $data The data that is to be cached.
* @param string $uri The requested URI.
*
* @return string
*/
function wprc_determine_object_type( $object_type, $cache_key, $data, $uri ) {
if ( $object_type !== 'unknown' || strpos( $uri, 'MY_NAMESPACE/v1/MY_ENDPOINT' ) === false ) {
return $object_type;
}
// Do your magic here.
$object_type = 'website';
// Do your magic here.
return $object_type;
}
add_filter( 'wp_rest_cache/determine_object_type', 'wprc_determine_object_type', 10, 4 );
and:
/**
* Process cache relations.
*
* @param int $cache_id The row id of the current cache.
* @param mixed $data The data that is to be cached.
* @param string $object_type Object type.
* @param string $uri The requested URI.
*/
function wprc_process_cache_relations( $cache_id, $data, $object_type, $uri ) {
if ( ! isset( $data['data'] ) || false === strpos( $uri, '/namespace/v2/custom_endpoint' ) ) {
return;
}
$caching = \WP_Rest_Cache_Plugin\Includes\Caching\Caching::get_instance();
if ( isset( $data['data']['id'] ) ) {
// Single item.
$caching->insert_cache_relation( $cache_id, $data['data']['id'], 'my-post-type' );
return;
}
foreach ( $data['data'] as $record ) {
if ( ! is_array( $record ) || ! isset( $record['id'] ) ) {
continue;
}
$caching->insert_cache_relation( $cache_id, $record['id'], 'my-post-type' );
}
}
add_action( 'wp_rest_cache/process_cache_relations', 'wprc_process_cache_relations', 10, 4 );