Custom End Point Flushing Automatically
-
Hi,
Is there any hook where I can force the custom endpoint created to be flushed automatically upon any post edit/publish?
Thanks,
-
Hi @mustafamsy
Thank you for using our plugin!
Since I don’t know much about your custom endpoint I will do my best to help you, but I might be wrong:
I assume you have already made sure the custom endpoint is being cached by our plugin? If not you can find out how to do this in our FAQ here.
Now if you have done this our plugin will try to determine the object type in your endpoint. Chances are it is unable to do so, in this case on the cache overview page (Settings > WP REST Cache > tab: Endpoint API Caches) you will see the object type for your endpoint asunknown
. Please read our FAQ here, to see how you can make sure it is detected correctly.
Once the object type is detected correctly our plugin will automatically flush the correct caches upon post edit/publish.I hope this will help you.
I apologize for my ignorance. This plugin looks great to me. But I’m having trouble because it doesn’t auto flush.I too have unknown in my EndPoint but I cannot understand from the faq how to proceed. There is a file that I need to edit. If so where do I find this file. Excuse me but I’m at the beginning.
Where I can do this?
Yes you can! Use the hook wp_rest_cache/determine_object_type like this:
function wprc_determine_object_type( $object_type, $cache_key, $data, $uri ) {
if ( $object_type !== ‘unknown’ || strpos( $uri, $this->namespace . ‘/’ . $this->rest_base ) === 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 );Thanks in advance
- This reply was modified 4 years, 1 month ago by csacchetti.
Hi @rockfire
Yes, I created a custom endpoint for one post type and other settings data that I am getting from another post using:
function get_last_posts_with_thumbnail( $data ) { $query_ads = array( 'post_type' => 'ads', 'post_status' => 'publish', ); $ads_post = get_posts($query_ads); for($v = 0; $v < count($ads_post); ++$v) { $ads_post[$v] = $ads_post[$v]; $perm = get_permalink($ads_post[$v]->ID); $thumbnail_id = get_post_thumbnail_id( $ads_post[$v]->ID ); $thumbnail = wp_get_attachment_image_src( $thumbnail_id, 'full'); $ads_post[$v]->link = $perm; $ads_post[$v]->thumbnail = $thumbnail[0]; } $settings_page = get_post('90201'); $acf = get_fields($settings_page->ID); $settings_page->acf = $acf; return array ( 'ads' => $ads_post, 'settings' => $settings_page, ); }
Above code is working. Then I registered this endpoint using:
add_action( 'rest_api_init', function () { register_rest_route( 'app_data/v1', '/data/', array( 'methods' => 'GET', 'callback' => 'get_last_posts_with_thumbnail', ) ); } );
I enabled cache for this endpoint using:
/** * Register the /wp-json/app_data/v1/posts endpoint so it will be cached. */ function wprc_add_get_last_posts_endpoint( $allowed_endpoints ) { if ( ! isset( $allowed_endpoints[ 'app_data/v1' ] ) || ! in_array( 'data', $allowed_endpoints[ 'app_data/v1' ] ) ) { $allowed_endpoints[ 'app_data/v1' ][] = 'data'; } return $allowed_endpoints; } add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_get_last_posts_endpoint', 10, 1);
The cache is working for this endpoint but no auto flush upon post edit/publish.
My request is that I want to be able to enable the auto flushing for this endpoint cache when I edit or publish a post.
- This reply was modified 4 years, 1 month ago by mustafamsy.
Hi @csacchetti
Could you please open your own topic, so we can help you?
Hi @mustafamsy
Looking at your code I would think it should work fine. If you visit Settings > WP REST Cache > tab: Endpoint API Caches, what do you see in the column
Object Type
for your custom endpoint?@rockfire It is working fine except for auto flushing. The Object Type for my endpoint is shown as unknown.
@mustafamsy Strange… Could you post the output of your custom endpoint so I can have a look at that? If you don’t want to share it on a public forum like this, you could email it to plugins [at] acato [dot] nl
Sure, I have shared it to your the email.
Hi @mustafamsy
Thank you for your email. I have had a look at the output of your endpoint and I see what the issue is:
Default WordPress endpoints don’t have the field
post_type
, in stead they have the fieldtype
. Our plugin uses that field to determine the correct object type. Since you are already looping through all the posts in your endpoint, I guess the easiest solution would be to add the fieldtype
(which would be the same value as thepost_type
field), so:
$ads_post[$v]->type = $ads_post[$v]->post_type;
Which makes me think that we might have to improve our plugin and add the field
post_type
as a way to automatically determine the correct object type. I will put it on our todo list.Hi @rockfire
I have added
type
as you suggested but I am still see object type as unknown.Hi @mustafamsy
Ah I responded too quickly without looking closely enough to the output of your endpoint. Adding the
type
isn’t the solution and isn’t necessary. Most probably all the different posts within your endpoint are detected correctly. I have tested this locally by recreating your endpoint and indeed all seperate posts are detected and when I edit one of those posts the endpoint is in fact cleared. So eventhough the object type statesunknown
the automatic flushing is working correctly.A little explanation: When caching an endpoint the plugin tries to determine the object type of the endpoint, but it also loops through all the records within the endpoint to determine the relations within the endpoint. Now if one of those relations is edited, the endpoint is flushed.
So for your endpoint: all the posts of the post typeads
are recognized and saved as relations, but also the settings page of post typepage
is recognized and saved as relations. It even recognizes more, like the categories you have selected within ACF within your settings page. Now if one of the ads, or the settings page, or any of the other relations is edited, the cache of the endpoint will be flushed.So now my question for you is: Are you sure automatic flushing isn’t working correctly, because (as I stated above) it is working on my local test environment?
@rockfire I checked but the content is not flushing automatically. You can check from the URL I sent to your email that in recent_post: the last entry date is 9 Sept. while there are many new posts added after that one.
Hi @mustafamsy
Ok, so it is not about existing items that are updated, but it is about new items not added. That is in fact caused by the
unknown
object type. Usually I would point you to our FAQ about helping our plugin determine the correct object type. You could do something like this:function wprc_determine_object_type( $object_type, $cache_key, $data, $uri ) { if ( $object_type !== 'unknown' || strpos( $uri, 'app_data/v1/data' ) === false ) { return $object_type; } return 'ads'; } add_filter( 'wp_rest_cache/determine_object_type', 'wprc_determine_object_type', 10, 4 );
However now we have a small problem and that is that your endpoint doesn’t contain one single object type (like for instance the
wp-json/wp/v2/posts
endpoint), but it contains multiple object types (ads
,post
,page
, …). So what is the correct object type? If you only want the endpoint to be flushed when newads
are added, the above code will work fine. But if you want it to be flushed when newads
and new posts are added, unfortunately our plugin can not do this for you.What you could do is use the above code for
ads
and then add some custom code for other post types. I haven’t tested it, but something like this should work:function flush_cache_upon_new_post( $post_id, $post, $update ) { if( !in_array( $post->post_type, [ 'post', 'page' ] ) || !$update ) { return; } \WP_Rest_Cache_Plugin\Includes\Caching\Caching::get_instance()->delete_cache_by_endpoint( '/wp-json/app_data/v1/data' ); } add_filter('save_post', 'flush_cache_upon_new_post', 10, 3);
@rockfire That’s what I actually was looking for, thanks a lot!
This code did the trick for me
function wprc_determine_object_type( $object_type, $cache_key, $data, $uri ) { if ( $object_type !== 'unknown' || strpos( $uri, 'app_data/v1/data' ) === false ) { return $object_type; } return 'ads'; } add_filter( 'wp_rest_cache/determine_object_type', 'wprc_determine_object_type', 10, 4 );
I just want that endpoint to be flushed when any post is edited/added, so I just changed it to
return 'post';
instead ofreturn 'ads';
- This reply was modified 4 years, 1 month ago by mustafamsy.
Shoutout to @mustafamsy and @rockfire, THANKS! It was exactly my problem. The condition strpos did not really work for me to differentiate between the different object types and between my endpoints. So I decided to share the code I used:
function wprc_determine_object_type( $object_type, $cache_key, $data, $uri) { if ( $object_type !== 'unknown' || strpos( $uri, 'api/v2' ) === false ) { return $object_type; } elseif ( $object_type == 'unknown' && $uri === 'MATCH_EXACT_uri/posts' ) { return 'post'; } elseif ( $object_type == 'unknown' && $uri === 'MATCH_EXACT_uri/pages' ) { return 'page'; } return 'unknown'; } add_filter( 'wp_rest_cache/determine_object_type', 'wprc_determine_object_type', 10, 4 );
- The topic ‘Custom End Point Flushing Automatically’ is closed to new replies.