| @@ -131,12 +131,47 @@ | ||
| 131 | 131 | /** |
| 132 | 132 | * Write current $_file_tree content into .htaccess file. |
| 133 | 133 | */ |
| 134 | 134 | public function write_file() { |
| 135 | - $content = implode(PHP_EOL, $this->get_flat_array($this->_file_tree)); | |
| 136 | 135 | if ($this->is_writable()) { |
| 137 | - @file_put_contents($this->_htaccess_file, $content); // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged -- Hosts like WP Engine restricts use of `.htaccess` file | |
| 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 | |
| 138 | 137 | } |
| 138 | + } | |
| 139 | + | |
| 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)); | |
| 139 | 174 | } |
| 140 | 175 | |
| 141 | 176 | /** |
| 142 | 177 | * Recursive function used to prepare data for output - build flat array from $_file_tree. |