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