Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
ResponseHeaderManager | |
0.00% |
0 / 31 |
|
0.00% |
0 / 7 |
156 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
parse_headers | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
add_header | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
add_headers | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
remove_header | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
remove_all_headers | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
set_headers | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace NewfoldLabs\WP\Module\Performance\Cache; |
4 | |
5 | use WP_Forge\WP_Htaccess_Manager\htaccess; |
6 | |
7 | use function WP_Forge\WP_Htaccess_Manager\convertContentToLines; |
8 | |
9 | /** |
10 | * Manage response headers. |
11 | */ |
12 | class ResponseHeaderManager { |
13 | |
14 | /** |
15 | * The file marker name. |
16 | * |
17 | * @var string |
18 | */ |
19 | const MARKER = 'Newfold Headers'; |
20 | |
21 | /** |
22 | * The htaccess manager. |
23 | * |
24 | * @var htaccess |
25 | */ |
26 | public $htaccess; |
27 | |
28 | /** |
29 | * Constructor. |
30 | */ |
31 | public function __construct() { |
32 | $this->htaccess = new htaccess( self::MARKER ); |
33 | } |
34 | |
35 | /** |
36 | * Parse existing headers. |
37 | * |
38 | * @return array |
39 | */ |
40 | public function parse_headers() { |
41 | |
42 | $headers = array(); |
43 | |
44 | $content = $this->htaccess->readContent(); |
45 | $lines = array_map( 'trim', convertContentToLines( $content ) ); |
46 | |
47 | array_shift( $lines ); // Remove opening IfModule |
48 | array_pop( $lines ); // Remove closing IfModule |
49 | |
50 | $pattern = '/^Header set (.*) "(.*)"$/'; |
51 | |
52 | foreach ( $lines as $line ) { |
53 | if ( preg_match( $pattern, trim( $line ), $matches ) && isset( $matches[1], $matches[2] ) ) { |
54 | $headers[ $matches[1] ] = $matches[2]; |
55 | } |
56 | } |
57 | |
58 | return $headers; |
59 | } |
60 | |
61 | /** |
62 | * Add a header. |
63 | * |
64 | * @param string $name Header name |
65 | * @param string $value Header value |
66 | */ |
67 | public function add_header( string $name, string $value ) { |
68 | $this->set_headers( |
69 | array_merge( |
70 | $this->parse_headers(), |
71 | array( $name => $value ) |
72 | ) |
73 | ); |
74 | } |
75 | |
76 | /** |
77 | * Add multiple headers at once. |
78 | * |
79 | * @param string[] $headers Headers to add. |
80 | */ |
81 | public function add_headers( array $headers ) { |
82 | $headers = array_merge( $this->parse_headers(), $headers ); |
83 | $this->set_headers( $headers ); |
84 | } |
85 | |
86 | /** |
87 | * Remove a header. |
88 | * |
89 | * @param string $name Header name |
90 | */ |
91 | public function remove_header( $name ) { |
92 | $headers = $this->parse_headers(); |
93 | unset( $headers[ $name ] ); |
94 | $this->set_headers( $headers ); |
95 | } |
96 | |
97 | /** |
98 | * Remove all headers. |
99 | */ |
100 | public function remove_all_headers() { |
101 | $this->set_headers( array() ); |
102 | } |
103 | |
104 | /** |
105 | * Set headers. |
106 | * |
107 | * @param array $headers Headers to set. |
108 | */ |
109 | public function set_headers( array $headers ) { |
110 | |
111 | if ( empty( $headers ) ) { |
112 | $this->htaccess->removeContent(); |
113 | |
114 | return; |
115 | } |
116 | |
117 | $content = '<IfModule mod_headers.c>' . PHP_EOL; |
118 | foreach ( $headers as $key => $value ) { |
119 | $content .= "\tHeader set {$key} \"{$value}\"" . PHP_EOL; |
120 | } |
121 | $content .= '</IfModule>'; |
122 | |
123 | $this->htaccess->addContent( $content ); |
124 | } |
125 | } |