• Resolved seanpaulfx

    (@seanpaulfx)


    Hi

    Our Endpoint: /wp-json/example1/v2/PostID/(page)/

    function wprc_add_acf_posts_endpoint( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'example1' ] ) || ! in_array( 'v2', $allowed_endpoints[ 'example1' ] ) ) {
            $allowed_endpoints[ 'example1' ][] = 'v2';
        }
        return $allowed_endpoints;
    }
    add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_acf_posts_endpoint', 10, 1);

    I want to know if I’m doing it right.
    In the admin page appears the list as follows.

    Endpoint API Caches

    Cache Key                           Request URI             Request Headers	    Request Method  Object Type Expiration          # Cache Hits    Active
    87a4f82e53a5af1b6010d39dbee6fa3b    /wp-json/example1/v2/9	[]	                GET	            unknown     2023-11-01 21:08:13 13	            Cache is ready to be served.
    210e9e3a3b9a798c9b55fdea65278f7d    /wp-json/example1/v2/3	[]	                GET	            unknown	    2023-11-01 21:07:18 4	            Cache is ready to be served.
    deccffd614cbb39559f9863cd84ca790    /wp-json/example1/v2/4	[]	                GET	            unknown	    2023-11-01 21:07:19 5	            Cache is ready to be served.

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Richard Korthuis

    (@rockfire)

    Hi @seanpaulfx

    Thank you for using our plugin!

    Well you are seeing caches, so it is working. But having said that I do have a remark about your code:
    You are mixing namespace and endpoint, or actually you only have a namespace ( example1/v2 ). When registering your endpoint using register_rest_route (https://developer.www.ads-software.com/reference/functions/register_rest_route/) example1/v2 would go into the first parameter (namespace) and you would need to provide an endpoint. Since I am not sure what is in your endpoint, let’s assume you have products in it, then the second parameter (endpoint) would be something like /products/(?P<id>[\d]+). Which would make your call to the endpoint not /wp-json/example1/v2/9 but /wp-json/example1/v2/products/9.

    Now the filter for the allowed endpoints would become:

    function wprc_add_acf_posts_endpoint( $allowed_endpoints ) {
        if ( ! isset( $allowed_endpoints[ 'example1/v2' ] ) || ! in_array( 'products', $allowed_endpoints[ 'example1/v2' ] ) ) {
            $allowed_endpoints[ 'example1/v2' ][] = 'products';
        }
        return $allowed_endpoints;
    }

    In your Endpoint API Caches I can see the Object Type is still unknown which means our plugin can not detect the object type (i.e. post type or taxonomy type) for your endpoints. So in order for the automatic clearing of the caches to work correctly you would have to add an extra filter, see: https://www.ads-software.com/plugins/wp-rest-cache/#on%20the%20cache%20overview%20page%20i%20see%20the%20object%20type%20is%20%27unknown%27.%20can%20i%20help%20the%20wp%20rest%20cache%20plugin%20to%20detect%20the%20object%20type%20correctly%3F

    Thread Starter seanpaulfx

    (@seanpaulfx)

    Here is my code

    	public function api_route_example1(){
    		register_rest_route('example1/v2','/spost/(?P<id>\d+)/(?P<page>\d+)',array(
    			'methods'  			  => WP_REST_Server::READABLE,
    			'callback'            => array($this,'api_action_example1'),
    			'permission_callback' => '__return_true',
    		));
    	}

    here is my code, added ‘spost’
    Do I need to edit anything further?

    And what do I need to do with this object type filter.

    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

    Thread Starter seanpaulfx

    (@seanpaulfx)

    The code seems to work, but how do I add two versions of api.
    I add this code but it doesn’t work.
    If I remove this fragment the code works.

    strpos( $uri, 'example1/v2/stype' ) === false ) {
    		return $object_type;
    	}
    function wprc_determine_object_type( $object_type, $cache_key, $data, $uri ) {
    	if ( $object_type !== 'unknown' || strpos( $uri, 'example1/v2/spost' ) === false || strpos( $uri, 'example1/v2/stype' ) === false ) {
    		return $object_type;
    	}
    
    	return 'products';
    }
    add_filter( 'wp_rest_cache/determine_object_type', 'wprc_determine_object_type', 10, 4 );

    Thanks

    Plugin Author Richard Korthuis

    (@rockfire)

    Hi @seanpaulfx

    Use this:

    function wprc_determine_object_type( $object_type, $cache_key, $data, $uri ) {
    	if ( $object_type !== 'unknown' || ( strpos( $uri, 'example1/v2/spost' ) === false && strpos( $uri, 'example1/v2/stype' ) === false ) ) {
    		return $object_type;
    	}
    
    	return 'products';
    }
    add_filter( 'wp_rest_cache/determine_object_type', 'wprc_determine_object_type', 10, 4 );
    
    Thread Starter seanpaulfx

    (@seanpaulfx)

    Thanks it worked

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom endpoint’ is closed to new replies.