PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
← All changes | includes/class-wp-optimize-htaccess.php +57 -18 3.2.5 → 4.7.0 View file →
@@ -1,7 +1,7 @@
1 1 <?php
2 2
3 -if (!defined('WPO_VERSION')) die('No direct access allowed');
3 +if (!defined('ABSPATH')) die('No direct access allowed');
4 4
5 5 class WP_Optimize_Htaccess {
6 6
7 7 /**
@@ -8,9 +8,9 @@
8 8 * Full path to .htaccess file.
9 9 *
10 10 * @var string
11 11 */
12 - private $_htaccess_file = '';
12 + private $_htaccess_file;
13 13
14 14 /**
15 15 * Structured content of .htaccess file.
16 16 *
@@ -47,9 +47,9 @@
47 47 return is_file($this->_htaccess_file);
48 48 }
49 49
50 50 /**
51 - * Checks if .htaccess file is readaable.
51 + * Checks if .htaccess file is readable.
52 52 *
53 53 * @return bool
54 54 */
55 55 public function is_readable() {
@@ -61,9 +61,9 @@
61 61 *
62 62 * @return bool
63 63 */
64 64 public function is_writable() {
65 - return is_writable($this->_htaccess_file);
65 + return wp_is_writable($this->_htaccess_file);
66 66 }
67 67
68 68 /**
69 69 * Read content of .htaccess file and store it as a tree in $_file_tree variable.
@@ -77,14 +77,15 @@
77 77 * [4] => 'RewriteCond %{REQUEST_FILENAME} !-f',
78 78 * [5] => 'RewriteCond %{REQUEST_FILENAME} !-d',
79 79 * [6] => 'RewriteRule . /index.php [L]',
80 80 * [7] => '</IfModule>',
81 + * ]
81 82 * [2] => '# END WordPress'
82 83 *
83 84 * @return void
84 85 */
85 86 public function read_file() {
86 - if (false == $this->is_exists() || false == $this->is_readable()) return;
87 + if (!$this->is_exists() || !$this->is_readable()) return;
87 88
88 89 $content = file_get_contents($this->_htaccess_file);
89 90
90 91 $content = explode("\n", $content);
@@ -130,13 +131,50 @@
130 131 /**
131 132 * Write current $_file_tree content into .htaccess file.
132 133 */
133 134 public function write_file() {
134 - $content = implode(PHP_EOL, $this->get_flat_array($this->_file_tree));
135 - file_put_contents($this->_htaccess_file, $content);
135 + if ($this->is_writable()) {
136 + @file_put_contents($this->_htaccess_file, $this->get_content()); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- Hosts like WP Engine restricts use of `.htaccess` file
137 + }
136 138 }
137 139
138 140 /**
141 + * Get .htaccess content as a string.
142 + *
143 + * @return string
144 + */
145 + public function get_content() {
146 + return implode(PHP_EOL, $this->get_flat_array($this->_file_tree));
147 + }
148 +
149 + /**
150 + * Returns section content as a string
151 + *
152 + * @param string $section
153 + * @return string
154 + */
155 + public function get_section_content($section = 'WP-Optimize Browser Cache') {
156 + $section_index = $this->search_commented_section($section);
157 +
158 + if (false === $section_index) return '';
159 +
160 + $section_length = (false === $section_index['end']) ? null : ($section_index['end'] - $section_index['begin'] + 1);
161 + $section_array = array_slice($this->_file_tree, $section_index['begin'], $section_length);
162 +
163 + return implode(PHP_EOL, $this->get_flat_array($section_array));
164 + }
165 +
166 + /**
167 + * Recursive function that builds a flat array from a tree-structured .htaccess file array.
168 + * Get .htaccess hash
169 + *
170 + * @return string
171 + */
172 + public function get_hash() {
173 + return hash('sha256', serialize($this->_file_tree));
174 + }
175 +
176 + /**
139 177 * Recursive function used to prepare data for output - build flat array from $_file_tree.
140 178 *
141 179 * @param array $array
142 180 * @param string $prefix
@@ -162,13 +200,13 @@
162 200 end($flat_array);
163 201 $last = key($flat_array);
164 202
165 203 foreach ($flat_array as $key => $value) {
166 - if ('' != $value && '#' == $value[0]) {
204 + if ('' !== $value && '#' === $value[0]) {
167 205 // never add prefix for comment lines.
168 206 $flat_array[$key] = $value;
169 207 } else {
170 - $flat_array[$key] = ($key == $first || $key == $last) ? $value : $prefix . $value;
208 + $flat_array[$key] = ($key === $first || $key === $last) ? $value : $prefix . $value;
171 209 }
172 210 }
173 211
174 212 return $flat_array;
@@ -181,10 +219,11 @@
181 219 * # END WP-Optimize Browser Cache
182 220 *
183 221 * @param array $content
184 222 * @param string $section
223 + * @param bool $prepend
185 224 */
186 - public function update_commented_section($content, $section = 'WP-Optimize Browser Cache') {
225 + public function update_commented_section($content, $section = 'WP-Optimize Browser Cache', $prepend = false) {
187 226 $section_begin = $this->get_section_begin_comment($section);
188 227 $section_end = $this->get_section_end_comment($section);
189 228
190 229 // add begin-end section comments.
@@ -195,9 +234,9 @@
195 234
196 235 // check if section with cache settings already in the file.
197 236 if (false === $section_index) {
198 237 // no section in file then add it to the end of file.
199 - $this->_file_tree = array_merge($this->_file_tree, $content);
238 + $this->_file_tree = $prepend ? array_merge($content, $this->_file_tree) : array_merge($this->_file_tree, $content);
200 239 } else {
201 240 $remove_length = (false === $section_index['end']) ? null : ($section_index['end'] - $section_index['begin'] + 1);
202 241 array_splice($this->_file_tree, $section_index['begin'], $remove_length, $content);
203 242 }
@@ -214,9 +253,8 @@
214 253 * @return bool
215 254 */
216 255 public function remove_commented_section($comment = 'WP-Optimize Browser Cache') {
217 256 $section_index = $this->search_commented_section($comment);
218 - WP_Optimize()->log(print_r($section_index, true));
219 257 if (false === $section_index) return false;
220 258
221 259 $remove_length = (false === $section_index['end']) ? null : ($section_index['end'] - $section_index['begin'] + 1);
222 260 array_splice($this->_file_tree, $section_index['begin'], $remove_length);
@@ -238,9 +276,9 @@
238 276 */
239 277 public function is_commented_section_exists($section = 'WP-Optimize Browser Cache') {
240 278 $search = $this->search_commented_section($section);
241 279
242 - return (false === $search) ? false : true;
280 + return false !== $search;
243 281 }
244 282
245 283 /**
246 284 * Search section in $_file_tree array wrapped by begin and end comments.
@@ -262,13 +300,13 @@
262 300 if (is_array($value)) continue;
263 301
264 302 $value = $this->normalize_string($value);
265 303
266 - if ($value == $section_begin_normalized) $section_begin_index = $i;
267 - if ($value == $section_end_normalized) $section_end_index = $i;
304 + if ($value === $section_begin_normalized) $section_begin_index = $i;
305 + if ($value === $section_end_normalized) $section_end_index = $i;
268 306 }
269 307
270 - if (false == $section_begin_index) {
308 + if (false === $section_begin_index) {
271 309 return false;
272 310 } else {
273 311 return array(
274 312 'begin' => $section_begin_index,
@@ -322,11 +360,12 @@
322 360
323 361 $home = set_url_scheme(get_option('home'), 'http');
324 362 $siteurl = set_url_scheme(get_option('siteurl'), 'http');
325 363 if (!empty($home) && 0 !== strcasecmp($home, $siteurl)) {
364 + $script_filename = isset($_SERVER['SCRIPT_FILENAME']) ? sanitize_text_field(wp_unslash($_SERVER['SCRIPT_FILENAME'])) : '';
326 365 $wp_path_rel_to_home = str_ireplace($home, '', $siteurl); /* $siteurl - $home */
327 - $pos = strripos(str_replace('\\', '/', $_SERVER['SCRIPT_FILENAME']), trailingslashit($wp_path_rel_to_home));
328 - $home_path = substr($_SERVER['SCRIPT_FILENAME'], 0, $pos);
366 + $pos = strripos(str_replace('\\', '/', $script_filename), trailingslashit($wp_path_rel_to_home));
367 + $home_path = substr($script_filename, 0, $pos);
329 368 $home_path = trailingslashit($home_path);
330 369 } else {
331 370 $home_path = ABSPATH;
332 371 }