PluginProbe
Depicter — Popup & Slider Builder / trunk
Depicter — Popup & Slider Builder vtrunk
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / app / src / Middleware / CacheMiddleware.php

CacheMiddleware.php in Depicter — Popup & Slider Builder trunk, at app/src/Middleware/CacheMiddleware.php

105 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Depicter\Middleware;
3
4
5 use Averta\WordPress\Utility\JSON;
6 use Psr\Http\Message\ResponseInterface;
7 use WPEmerge\Requests\RequestInterface;
8 use WPEmerge\Responses\ResponseService;
9
10 class CacheMiddleware
11 {
12
13 /**
14 * Response service.
15 *
16 * @var ResponseService
17 */
18 protected $responseService = null;
19
20 /**
21 * Constructor.
22 *
23 * @codeCoverageIgnore
24 * @param ResponseService $responseService
25 */
26 public function __construct( ResponseService $responseService ) {
27 $this->responseService = $responseService;
28 }
29
30 /**
31 * @param RequestInterface $request
32 * @param $next
33 * @param int $expiration Expiration time in seconds
34 * @param string $module
35 * @param string $allowedMethod pass 'ANY or ALL or *' for all methods
36 *
37 * @return ResponseInterface
38 * @throws \Psr\SimpleCache\InvalidArgumentException
39 */
40 public function handle( RequestInterface $request, $next, $expiration = 60, $module = 'api', $allowedMethod = '*' ) {
41
42 // only continue for allowed methods
43 if( ! in_array( $allowedMethod, [ $request->getMethod(), 'ANY', 'ALL', '*' ] ) ){
44 return $next( $request );
45 }
46
47 // Append Cache_control and Access-Control to response ( $next( $request ) )
48 if( $module === 'browser' ){
49 $response = $next( $request );
50
51 // To flush cache we dont set cache-control and let WordPress send nocache headers
52 if( $this->hasForceFlush( $request ) ){
53 return $response;
54 }
55
56 $responseBody = $response->getBody()->getContents();
57 if ( JSON::isJson( $responseBody ) ) {
58 $responseArray = JSON::decode( $responseBody, true );
59 if ( isset( $responseArray['errors'] ) ) {
60 return $response;
61 }
62 }
63
64 // disable WordPress nocache header for this request
65 add_filter( 'nocache_headers', 'depicter_disable_nocache_headers' );
66
67 return $response->withHeader( 'Cache-Control', 'max-age=' . $expiration )
68 ->withHeader( 'Access-Control-Allow-Origin' , '*' );
69 }
70
71 $cache = \Depicter::cache( $module );
72 $key = $cache->hashRequest( $request );
73
74 // flush cache
75 if( $this->hasForceFlush( $request ) ){
76 $cache->delete( $key );
77
78 // read and return cache if exists
79 } elseif ( $cache->has( $key ) ) {
80
81 // disable WordPress nocache header for this request
82 add_filter( 'nocache_headers', 'depicter_disable_nocache_headers' );
83
84 $result = $cache->get( $key );
85 $response = ( is_array( $result ) || JSON::isJson( $result ) ) ? $this->responseService->json( $result ) : $this->responseService->output( $result );
86
87 return $response->withHeader( 'Cache-Control', 'max-age=' . $expiration )
88 ->withHeader( 'Access-Control-Allow-Origin' , '*' );
89 }
90
91 return $next( $request );
92 }
93
94 /**
95 * Whether to flush cache or not
96 *
97 * @param RequestInterface $request
98 *
99 * @return bool
100 */
101 protected function hasForceFlush( RequestInterface $request ){
102 return $request->query('flush') == 'true';
103 }
104 }
105