Forum Replies Created

Viewing 13 replies - 1 through 13 (of 13 total)
  • Thread Starter geochanto

    (@geochanto)

    @rockfire thank you for taking the time to respond. Yes I’m aware of the auto-regeneration feature. But as far as I can tell it’s based on a cron job.

    I’m trying to instead trigger regeneration only for specific endpoints as soon as they are cleared. This could be really powerful to avoid latency for first users who request the API endpoint right after cache has been cleared.

    However wp_remote_get seems like is not regenerating the cache after it’s cleared. There’s no X-Wp-Cached-Call when I visit the endpoint for the first time after cache clearing.

    Do you have any other pointers? Perhaps there’s a function similar to delete_cache_by_endpoint, like create_cache_by_endpoint or something.

    Thread Starter geochanto

    (@geochanto)

    Hey @quirksmode I found a workaround for this. Apparently you can call get_option('wpseo_titles'), which returns all default titles for your post and taxonomy types.

    Thread Starter geochanto

    (@geochanto)

    @sawasblog If I were you, I’d try to open a new ticket and reference any info in this one. I have limited knowledge of PHP so chances are I can’t provide you with the answers you need beyond what’s already described in this ticket.

    Thread Starter geochanto

    (@geochanto)

    after playing around for a while, I think I got it ?? cache seems to be clearing for the right post type, and caching seems to be working – can verify with the response header, and also cache hit incrementing in the admin area.

    Here’s my code:

    <?php
    
    require_once ABSPATH . 'wp-admin/includes/plugin.php';
    if ( is_plugin_active( 'wp-rest-cache/wp-rest-cache.php' ) ) {
        include_once WP_PLUGIN_DIR . '/wp-rest-cache/wp-rest-cache.php';
    
        $wp_rest_cache_api = new \WP_Rest_Cache_Plugin\Includes\API\Endpoint_Api();
        $wp_rest_cache_api->get_api_cache();
    
        function wprc_add_acf_posts_endpoint( $allowed_endpoints ) {
            if ( ! isset( $allowed_endpoints[ 'custom/v2' ] ) || ! in_array( 'all-products', $allowed_endpoints[ 'custom/v2' ] ) ) {
                $allowed_endpoints[ 'custom/v2' ][] = 'all-products';
            }
    
            if ( ! isset( $allowed_endpoints[ 'custom/v2' ] ) || ! in_array( 'all-posts', $allowed_endpoints[ 'custom/v2' ] ) ) {
                $allowed_endpoints[ 'custom/v2' ][] = 'all-posts';
            }
    
            return $allowed_endpoints;
        }
        add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_acf_posts_endpoint', 10, 1);
    
        function refreshCache($postId)
        {
            // If this is just a post revision, do nothing.
            if (wp_is_post_revision($postId) || wp_is_post_autosave($postId)) {
                return;
            }
            
            // Verify if this isn't an auto-save routine.
            // If it is that our form has not been submitted then we dont want to do anything.
            if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
                return;
            }
            
            // Return if it's a post revision.
            if (wp_is_post_revision($postId) !== false) {
                return;
            }
    
            $post_type = get_post_type($postId);
    
            $caching = \WP_Rest_Cache_Plugin\Includes\Caching\Caching::get_instance();
            if ($post_type === 'post') {
                $caching->delete_cache_by_endpoint("/wp-json/custom/v2/all-posts");
            }
            else if ($post_type === 'product') {
                $caching->delete_cache_by_endpoint("/wp-json/custom/v2/all-products");
            }
            
        }
        add_action('save_post', 'refreshCache', 1);
    }
    Thread Starter geochanto

    (@geochanto)

    @nurgiel FYI I did try to use your entire controller, but with that caching no longer works ??

    Thread Starter geochanto

    (@geochanto)

    @nurgiel thanks for taking the time. I see you have a lot of stuff going on in there. I think I probably don’t need to do all that. My need is simple, something like this pseudocode called from functions.php. I just don’t know the correct syntax for to call that method from another file, because delete_cache_by_endpoint is in /wp-content/plugins/wp-rest-cache/includes/class-caching.php:

    
    on_save_post {
      if(post->type === 'product) {
      delete_cache_by_endpoint('/custom/v2/all-products');
      }
    }
    

    Can I do this?

    Thread Starter geochanto

    (@geochanto)

    @rockfire with that, it did not update the object_type in the admin area, so I tried to delete that cache record, then hit the API again.

    I’m getting the x-wp-cached-call: served-cache header, and an updated response. But, the endpoint is not re-cached in the admin area. So, I suspect it may be a false positive and cache is actually not served.

    *EDIT:* confirmed cache does not work, after checking the wp_options table as suggested.

    I think calling delete_cache_by_endpoint may be a simpler approach.

    • This reply was modified 5 years ago by geochanto.
    Thread Starter geochanto

    (@geochanto)

    the x-wp-cached-call: served-cache header is there. I restarted the server and now I see caches in the admin area as well. I see that the object type is wrong – it says hardwood instead of product.

    So if I help it determine the right object type it would clear the cache?

    Below does not work. What’s the correct implementation of 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
        if ($object_type == 'hardwood') {
            $object_type = 'product';
        }
        // Do your magic here
        return $object_type;
    }
    Thread Starter geochanto

    (@geochanto)

    @rockfire thanks for the tips. However in my case I don’t even see any caches in the admin area, but cache does seem to be working. I’m not sure that “unkown” object type that others have complained about is my problem.

    I found in other posts that you have this method: \WP_Rest_Cache_Plugin\Includes\Caching\Caching::get_instance()->delete_cache_by_endpoint( '/api/endpoint/here' ); . My guess is that if I call this on save_post, then I could clear the cache manually. I’m just not sure about the PHP syntax how to do so, as I’m mostly a javascript guy. Any pointers would be appreciated.

    I’m also wondering how/where is the cache stored? I could not find any static files, so my guess is that this is a db-level cache?

    Thread Starter geochanto

    (@geochanto)

    Hi there. I know it’s been a while but I found why this issue occurs.

    It looks like when no changes are made in the Yoast SEO title, and left with just default snippet variables, the _yoast_wpseo_title field doesn’t get added to the wp_postmeta table. When the title is modified, it gets added. If the title is reverted back to default, it again gets removed.

    Unless there’s a specific reason it works this way, I would think it’s a bug.

    Thread Starter geochanto

    (@geochanto)

    Can you point to any documentation on how snippet variables are handled by Yoast? Like where the variables are stored in the DB etc?

    Thread Starter geochanto

    (@geochanto)

    sadly no, I only have it on my local now. I’ll post a link when I set up a staging site.

    Thread Starter geochanto

    (@geochanto)

    Front-end validation by contact form 7. When you submit the form with empty required field, and it gives you errors without redirecting to another page ??

Viewing 13 replies - 1 through 13 (of 13 total)