• Resolved Anonymous User 14009747

    (@anonymized-14009747)


    Hi,

    Using your plugin, a problem with a single/detail custom rest api item occured. It was not flushed after the content of that single item is changed.

    The index endpoint of that custom endpoint was however flushed successfully.

    Got some images to show what happens.

    We create a new item, and see it correctly in the endpoint index and detail:

    before-change-index.png

    before-change-detail.png

    before-change-settings.png

    Than we change that item:

    after-change-index.png

    after-change-detail.png

    after-change-settings.png

    And than after a reload of the index endpoint:

    after-change-after-refresh-settings.png

    And then the code to enable the custom endpoint:

    /**
     * Register the /wp-json/owc/openpub/v1/* endpoint so it will be cached.
     */
    add_filter( 'wp_rest_cache/allowed_endpoints',function( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'owc/openpub/v1' ] ) || ! in_array( 'items', $allowed_endpoints[ 'owc/openpub/v1' ] ) ) {
            $allowed_endpoints[ 'owc/openpub/v1' ][] = 'items';
        }
    
        return $allowed_endpoints;
    }, 10, 1);
    

    And the code to determine the entity

    
    /**
     * Add determination to entities.
     */
    add_filter( 'wp_rest_cache/determine_object_type',function( $object_type, $cache_key, $data, $uri ) {
        if ( 'unknown' !== $object_type || strpos( $uri, '/owc/openpub/v1/' ) === false ) {
            return $object_type;
        }
    
        if ( false !== strpos( $uri, '/wp-json/owc/openpub/v1/items' ) ) {
            $object_type = 'openpub-item';
        }
    
        return $object_type;
    }, 10, 4);
    

    Can you tell me what is going wrong here, so we can use your plugin to it’s full capacity?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Anonymous User 14009747

    (@anonymized-14009747)

    Okay, after a lot of debugging (and unfortunately, no support), I’ve found the culprit.

    Our custom rest-api is not providing the correct properties WP Rest Cache requires to cache and flush a detail item.

    The function process_recursive_cache_relations in file includes/caching/class-caching.php has some conditionals which it checks to see if it must insert or process the related cache.

    Our endpoint did not match it, so that’s what cased the above problem.

    My solution:

    
    /**
     * Process cache relations.
     *
     * Allows external processing of cache relations.
     *
     * @since 2018.4.2
     *
     * @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.
     */
    add_action('wp_rest_cache/process_cache_relations', function($cache_id, $data, $object_type, $uri) {
        global $wpdb;
    
        if ( false !== strpos( $uri, '/wp-json/owc/openpub/v1/items' ) ) {
            $wpdb->replace(
                $wpdb->prefix . \WP_Rest_Cache_Plugin\Includes\Caching\Caching::TABLE_RELATIONS,
            [
                    'cache_id'    => $cache_id,
                    'object_id'   => $data['data']['id'],
                    'object_type' => $object_type,
                ],
        [ '%d', '%s', '%s' ]
            );
        }
    }, 10, 4);
    

    This hooks into the available action, and inserts and thus caches the object.

    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @edwin-yard

    Thank you for using our plugin!

    Sorry I did not respond any sooner, but I was out of the office for six weeks (due to the birth of my son) and my colleague was too busy to provide free support in the meantime.

    I see you already found a solution to your problem, that is great to hear! Please let us know if we can be of any more help.

    I see it is for the OpenWebConcept, may I ask for what part you are using our plugin? (We also maintain a website that uses OWC)

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘detail item not flushed after mutation’ is closed to new replies.