Plugin Directory


Ignore:
Timestamp:
11/08/2026 04:28:35 PM (5 hours ago)
Author:
degird
Message:

Release 1.2.4

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cliedesk/trunk/src/Rest/RestServer.php

    r3790823 r3791202  
    1919use Cliedesk\Infrastructure\ChangeLog;
    2020use Cliedesk\Infrastructure\Ephemeral;
     21use Cliedesk\Infrastructure\Http\NoCache;
    2122use Cliedesk\Infrastructure\Jobs\Kick;
    2223use Cliedesk\Infrastructure\RequestId;
    … …  
    7879                add_filter( 'rest_pre_dispatch', array( $this, 'complete_routes' ), 0, 3 );
    7980                add_filter( 'rest_pre_dispatch', array( $this, 'guard' ), 10, 3 );
     81                add_filter( 'rest_pre_dispatch', array( $this, 'never_cache' ), 0, 3 );
    8082                add_filter( 'rest_post_dispatch', array( $this, 'headers' ), 10, 3 );
    8183                add_filter( 'rest_index', array( $this, 'trim_index' ) );
    … …  
    273275
    274276        /**
    275          * Add X-Request-Id and no-store headers to cliedesk/v1 responses.
     277         * `rest_pre_dispatch`: a page cache never stores a Cliedesk answer, streams and early exits included. Only for a
     278         * real REST request: a route called from inside a normal page does not make that page uncacheable.
     279         *
     280         * @param mixed            $result  Result so far.
     281         * @param \WP_REST_Server  $server  Server.
     282         * @param \WP_REST_Request $request Request.
     283         * @return mixed
     284         */
     285        public function never_cache( $result, $server, $request ) {
     286                if ( defined( 'REST_REQUEST' ) && REST_REQUEST && NoCache::is_own_route( (string) $request->get_route() ) ) {
     287                        NoCache::refuse();
     288                }
     289                return $result;
     290        }
     291
     292        /**
     293         * Add the no-cache headers to Cliedesk's responses, and X-Request-Id and no-store to cliedesk/v1 responses.
    276294         *
    277295         * @param \WP_HTTP_Response $response Response.
    … …  
    281299         */
    282300        public function headers( $response, $server, $request ) {
    283                 if ( str_starts_with( (string) $request->get_route(), '/' . RouteRegistry::NAMESPACE ) ) {
     301                $route = (string) $request->get_route();
     302                if ( ! NoCache::is_own_route( $route ) ) {
     303                        return $response;
     304                }
     305                foreach ( NoCache::headers() as $name => $value ) {
     306                        $response->header( $name, $value );
     307                }
     308                if ( 0 === stripos( $route, '/' . RouteRegistry::NAMESPACE ) ) {
    284309                        $response->header( 'X-Request-Id', RequestId::get() );
    285310                        $response->header( 'Cache-Control', 'no-store, private' );
Note: See TracChangeset for help on using the changeset viewer.