PluginProbe
Depicter — Popup & Slider Builder / 1.5.2
Depicter — Popup & Slider Builder v1.5.2
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 1.5.2, at app/src/Middleware/CacheMiddleware.php

95 lines 2.8 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 // To flush cache we dont set cache-control and let WordPress send nocache headers
50 if( $this->hasForceFlush( $request ) ){
51 return $next( $request );
52 }
53
54 // disable WordPress nocache header for this request
55 add_filter( 'nocache_headers', 'depicter_disable_nocache_headers' );
56
57 return $next( $request )->withHeader( 'Cache-Control', 'max-age=' . $expiration )
58 ->withHeader( 'Access-Control-Allow-Origin' , '*' );
59 }
60
61 $cache = \Depicter::cache( $module );
62 $key = $cache->hashRequest( $request );
63
64 // flush cache
65 if( $this->hasForceFlush( $request ) ){
66 $cache->delete( $key );
67
68 // read and return cache if exists
69 } elseif ( $cache->has( $key ) ) {
70
71 // disable WordPress nocache header for this request
72 add_filter( 'nocache_headers', 'depicter_disable_nocache_headers' );
73
74 $result = $cache->get( $key );
75 $response = ( is_array( $result ) || JSON::isJson( $result ) ) ? $this->responseService->json( $result ) : $this->responseService->output( $result );
76
77 return $response->withHeader( 'Cache-Control', 'max-age=' . $expiration )
78 ->withHeader( 'Access-Control-Allow-Origin' , '*' );
79 }
80
81 return $next( $request );
82 }
83
84 /**
85 * Whether to flush cache or not
86 *
87 * @param RequestInterface $request
88 *
89 * @return bool
90 */
91 protected function hasForceFlush( RequestInterface $request ){
92 return $request->query('flush') == 'true';
93 }
94 }
95