PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.3.1
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.3.1
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / classes / WriteFile / AbstractApacheDirConfFile.php

AbstractApacheDirConfFile.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.3.1, at classes/WriteFile/AbstractApacheDirConfFile.php

78 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Imagify\WriteFile;
3
4 defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' );
5
6 /**
7 * Abstract class used to add and remove contents to the .htaccess file.
8 *
9 * @since 1.9
10 * @author Grégory Viguier
11 */
12 abstract class AbstractApacheDirConfFile extends AbstractWriteDirConfFile {
13
14 /**
15 * Insert new contents into the directory conf file.
16 * Replaces existing marked info. Creates file if none exists.
17 *
18 * @since 1.9
19 * @access protected
20 * @author Grégory Viguier
21 *
22 * @param string $new_contents Contents to insert.
23 * @return bool|\WP_Error True on write success, a \WP_Error object on failure.
24 */
25 protected function insert_contents( $new_contents ) {
26 $contents = $this->get_file_contents();
27
28 if ( is_wp_error( $contents ) ) {
29 return $contents;
30 }
31
32 $start_marker = '# BEGIN ' . static::TAG_NAME;
33 $end_marker = '# END ' . static::TAG_NAME;
34
35 // Remove previous rules.
36 $contents = preg_replace( '/\s*?' . preg_quote( $start_marker, '/' ) . '.*' . preg_quote( $end_marker, '/' ) . '\s*?/isU', "\n\n", $contents );
37 $contents = trim( $contents );
38
39 if ( $new_contents ) {
40 $contents = $new_contents . "\n\n" . $contents;
41 }
42
43 return $this->put_file_contents( $contents );
44 }
45
46 /**
47 * Get new contents to write into the file.
48 *
49 * @since 1.9
50 * @access public
51 * @author Grégory Viguier
52 *
53 * @return string
54 */
55 public function get_new_contents() {
56 $contents = parent::get_new_contents();
57
58 if ( ! $contents ) {
59 return '';
60 }
61
62 return '# BEGIN ' . static::TAG_NAME . "\n" . $contents . "\n# END " . static::TAG_NAME;
63 }
64
65 /**
66 * Get the unfiltered path to the file.
67 *
68 * @since 1.9
69 * @access protected
70 * @author Grégory Viguier
71 *
72 * @return string
73 */
74 protected function get_raw_file_path() {
75 return $this->filesystem->get_site_root() . '.htaccess';
76 }
77 }
78