PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / trunk
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution vtrunk
2.0.1 trunk 1.0.0 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.9.0 2.0.0
solid-performance / src / Performance / Cache_Delivery / Htaccess / Modifier.php

Modifier.php in Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution trunk, at src/Performance/Cache_Delivery/Htaccess/Modifier.php

154 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The htaccess modifier.
4 *
5 * @package SolidWP\Performance
6 */
7
8 declare( strict_types=1 );
9
10 namespace SolidWP\Performance\Cache_Delivery\Htaccess;
11
12 /**
13 * The htaccess modifier.
14 *
15 * @package SolidWP\Performance
16 */
17 final class Modifier {
18
19 public const START_TAG = '# BEGIN KadencePerformance';
20 public const END_TAG = '# END KadencePerformance';
21
22 /**
23 * @var array<string, string> Legacy tags to check for. The start_tag => end_tag pairs.
24 */
25 private array $legacy_tags = [
26 '# BEGIN SolidPerformance' => '# END SolidPerformance',
27 ];
28
29 /**
30 * Prepend our htaccess rules to the existing htaccess content.
31 *
32 * @param string $content The existing htaccess content.
33 * @param string $rules The rules to insert.
34 *
35 * @return string
36 */
37 public function prepend( string $content, string $rules ): string {
38 // Rules already exist, no replacement needed.
39 if ( $this->find_tags( $content ) !== null ) {
40 return $content;
41 }
42
43 return $this->wrap( $rules ) . PHP_EOL . $content;
44 }
45
46 /**
47 * Force prepend (replace) the existing rules to the existing htaccess content.
48 *
49 * @param string $content The htaccess content.
50 * @param string $rules The rules to insert.
51 *
52 * @return string
53 */
54 public function force_prepend( string $content, string $rules ): string {
55 $tags = $this->find_tags( $content );
56
57 if ( $tags === null ) {
58 return $this->wrap( $rules ) . PHP_EOL . $content;
59 }
60
61 [ $start_tag, $end_tag ] = $tags;
62
63 // Ensure we remove the line break after our end tag before replacing.
64 $regex = sprintf(
65 '/%s.*?\s*%s\R?/s',
66 preg_quote( $start_tag, '/' ),
67 preg_quote( $end_tag, '/' )
68 );
69
70 return preg_replace( $regex, $this->wrap( $rules ), $content );
71 }
72
73 /**
74 * Remove our rules from the htaccess content.
75 *
76 * @param string $content The content of the htaccess file.
77 *
78 * @return string
79 */
80 public function remove( string $content ): string {
81 $tags = $this->find_tags( $content );
82
83 if ( $tags === null ) {
84 return $content;
85 }
86
87 [ $start_tag, $end_tag ] = $tags;
88
89 return preg_replace(
90 sprintf(
91 '/%s.*?%s\s*/s',
92 preg_quote( $start_tag, '/' ),
93 preg_quote( $end_tag, '/' )
94 ),
95 '',
96 $content
97 ) ?? $content;
98 }
99
100 /**
101 * Determine if the current htaccess file has our rules in it.
102 *
103 * This also checks for legacy tags from previous brand names.
104 *
105 * @param string $content The htaccess content.
106 *
107 * @return bool
108 */
109 public function has_rules( string $content ): bool {
110 return $this->find_tags( $content ) !== null;
111 }
112
113 /**
114 * Find which start/end tags are present in the content.
115 *
116 * Checks the current tags first, then falls back to legacy tags.
117 *
118 * @param string $content The htaccess content.
119 *
120 * @return array{0: string, 1: string}|null The [start_tag, end_tag] pair, or null if none found.
121 */
122 private function find_tags( string $content ): ?array {
123 $tag_pairs = array_merge(
124 [ self::START_TAG => self::END_TAG ],
125 $this->legacy_tags,
126 );
127
128 foreach ( $tag_pairs as $start_tag => $end_tag ) {
129 $regex = sprintf(
130 '/%s.*?%s/s',
131 preg_quote( $start_tag, '/' ),
132 preg_quote( $end_tag, '/' )
133 );
134
135 if ( preg_match( $regex, $content ) ) {
136 return [ $start_tag, $end_tag ];
137 }
138 }
139
140 return null;
141 }
142
143 /**
144 * Wrap our htaccess rules in our start/end tags.
145 *
146 * @param string $rules The htaccess rules.
147 *
148 * @return string
149 */
150 private function wrap( string $rules ): string {
151 return self::START_TAG . PHP_EOL . trim( $rules ) . PHP_EOL . self::END_TAG . PHP_EOL;
152 }
153 }
154