Caching private wordpress rest endpoints

22 Views Asked by At

I am trying to cache some private custom rest endpoints using WP Rest Cache and whenever i try it makes them public? Not sure if anyone has come across this but i need it to remain private in the cache to authenticated users.

register_rest_route( 'v1', 'get-report-summary/',array(
      'methods'  => 'GET',
      'callback' => 'get_report_summary',
      'permission_callback' => function () { 
          return is_user_logged_in();
      },
));

function get_report_summary(){

   $data = "Some code";

   $response = new WP_REST_Response($data);
   $response->set_status(200);

   return $response;
}

Adding the endpoint to the cache

function wprc_add_endpoint( $allowed_endpoints ) {
    if ( ! isset( $allowed_endpoints[ 'v1' ] ) || ! in_array( 'get-report-summary', $allowed_endpoints[ 'v1' ] ) ) {
        $allowed_endpoints[ 'v1' ][] = 'get-report-summary';
    }
    return $allowed_endpoints;
}
add_filter( 'wp_rest_cache/allowed_endpoints', 'wprc_add_endpoint', 10, 1);
0

There are 0 best solutions below