forked from Pollora/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordPressHeaders.php
More file actions
125 lines (108 loc) · 3.58 KB
/
WordPressHeaders.php
File metadata and controls
125 lines (108 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
declare(strict_types=1);
namespace Pollora\Route\Infrastructure\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
/**
* Middleware to manage HTTP headers for WordPress responses.
*
* Handles framework-specific headers, WordPress header cleanup,
* and cache control directives.
*/
class WordPressHeaders
{
/**
* Framework name constant for headers.
*/
private const FRAMEWORK_NAME = 'Pollora';
/**
* Framework header name constant.
*/
private const FRAMEWORK_HEADER = 'X-Powered-By';
/**
* Handle the incoming request.
*
* Manages response headers including:
* - Adding framework identification
* - Cleaning up WordPress headers for non-authenticated requests
* - Setting appropriate cache control directives
*
* @param Request $request Incoming HTTP request
* @param Closure $next Next middleware handler
* @return SymfonyResponse Modified response instance
*/
public function handle(Request $request, Closure $next): SymfonyResponse
{
$response = $next($request);
if (! $response instanceof SymfonyResponse) {
return $response;
}
$this->addFrameworkHeader($response);
if ($this->shouldCleanupHeaders($request)) {
$this->removeWordPressHeaders($response);
}
if ($this->shouldSetPublicCache()) {
header_remove('Cache-Control');
$response->headers->remove('Cache-Control');
$response->setPublic();
$response->headers->addCacheControlDirective('must-revalidate', true);
$response->headers->addCacheControlDirective('max-age', '3600');
}
return $response;
}
/**
* Add the framework identification header.
*
* @param SymfonyResponse $response Response being modified
*/
private function addFrameworkHeader(SymfonyResponse $response): void
{
$response->headers->set(self::FRAMEWORK_HEADER, self::FRAMEWORK_NAME);
}
/**
* Determine if WordPress headers should be cleaned up.
*
* @param Request $request Incoming request instance
* @return bool True when headers should be removed
*/
private function shouldCleanupHeaders(Request $request): bool
{
$route = $request->route();
return $route &&
method_exists($route, 'hasCondition') &&
! $route->hasCondition() &&
$this->isWordPressFunctionAvailable('is_user_logged_in') &&
! is_user_logged_in();
}
/**
* Remove WordPress-specific headers from the response.
*
* @param SymfonyResponse $response Response to clean up
*/
private function removeWordPressHeaders(SymfonyResponse $response): void
{
$response->headers->remove('Cache-Control');
$response->headers->remove('Expires');
$response->headers->remove('Content-Type');
}
/**
* Determine if public cache headers should be set.
*
* @return bool True when public caching is allowed
*/
private function shouldSetPublicCache(): bool
{
return $this->isWordPressFunctionAvailable('is_user_logged_in') && ! is_user_logged_in();
}
/**
* Check if a WordPress function is available.
*
* @param string $function Function name to check
* @return bool True if the function exists
*/
private function isWordPressFunctionAvailable(string $function): bool
{
return function_exists($function);
}
}