PluginProbe
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution / 1.9.0
Solid Performance – Your No-Code Caching, Performance, & Page Speed Solution v1.9.0
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 1.9.0, at src/Performance/Cache_Delivery/Htaccess/Modifier.php

113 lines 2.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 SolidPerformance';
20 public const END_TAG = '# END SolidPerformance';
21
22 /**
23 * Prepend our htaccess rules to the existing htaccess content.
24 *
25 * @param string $content The existing htaccess content.
26 * @param string $rules The rules to insert.
27 *
28 * @return string
29 */
30 public function prepend( string $content, string $rules ): string {
31 // Rules already exist, no replacement needed.
32 if ( $this->has_rules( $content ) ) {
33 return $content;
34 }
35
36 return $this->wrap( $rules ) . PHP_EOL . $content;
37 }
38
39 /**
40 * Force prepend (replace) the existing rules to the existing htaccess content.
41 *
42 * @param string $content The htaccess content.
43 * @param string $rules The rules to insert.
44 *
45 * @return string
46 */
47 public function force_prepend( string $content, string $rules ): string {
48 if ( ! $this->has_rules( $content ) ) {
49 return $this->prepend( $content, $rules );
50 }
51
52 // Ensure we remove the line break after our end tag before replacing.
53 $regex = sprintf(
54 '/%s.*?\s*%s\R?/s',
55 preg_quote( self::START_TAG, '/' ),
56 preg_quote( self::END_TAG, '/' )
57 );
58
59 return preg_replace( $regex, $this->wrap( $rules ), $content );
60 }
61
62 /**
63 * Remove our rules from the htaccess content.
64 *
65 * @param string $content The content of the htaccess file.
66 *
67 * @return string
68 */
69 public function remove( string $content ): string {
70 if ( ! $this->has_rules( $content ) ) {
71 return $content;
72 }
73
74 return preg_replace(
75 sprintf(
76 '/%s.*?%s\s*/s',
77 preg_quote( self::START_TAG, '/' ),
78 preg_quote( self::END_TAG, '/' )
79 ),
80 '',
81 $content
82 ) ?? $content;
83 }
84
85 /**
86 * Determine if the current htaccess file has our rules in it.
87 *
88 * @param string $content The htaccess content.
89 *
90 * @return bool
91 */
92 public function has_rules( string $content ): bool {
93 $regex = sprintf(
94 '/%s.*?%s/s',
95 preg_quote( self::START_TAG, '/' ),
96 preg_quote( self::END_TAG, '/' )
97 );
98
99 return (bool) preg_match( $regex, $content );
100 }
101
102 /**
103 * Wrap our htaccess rules in our start/end tags.
104 *
105 * @param string $rules The htaccess rules.
106 *
107 * @return string
108 */
109 private function wrap( string $rules ): string {
110 return self::START_TAG . PHP_EOL . trim( $rules ) . PHP_EOL . self::END_TAG . PHP_EOL;
111 }
112 }
113