PluginProbe
QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress / 4.0.0
QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress v4.0.0
4.1.2 4.1.1 4.1.0 4.0.1 4.0.0 3.3.1 3.3.0 trunk 1.0.0 2.0.0 2.1.0 3.0.0 3.1.0 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8
quickwebp / admin / rewrite-rules / class-rewrite-rules-abstract.php

class-rewrite-rules-abstract.php in QuickWebP – WebP & AVIF Image Optimizer, Compression & SEO for WordPress 4.0.0, at admin/rewrite-rules/class-rewrite-rules-abstract.php

207 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 abstract class Quickwebp_Rewrite_Rules_Abstract {
3
4 /**
5 * Name of the tag used as block delemiter.
6 */
7 protected $tag_name = 'Quickwebp: rewrite rules for webp';
8
9 /**
10 * Add new contents to the file.
11 */
12 public function add() {
13 $result = $this->insert_contents( $this->get_raw_new_contents() );
14
15 if ( is_wp_error( $result ) ) {
16 add_action( 'admin_notices', function() use ( $result ) {
17 ?>
18 <div class="notice notice-error">
19 <p><?php echo esc_html( $result->get_error_message() ); ?></p>
20 </div>
21 <?php
22 });
23 }
24
25 return true;
26 }
27
28 /**
29 * Remove contents from the file.
30 */
31 public function remove() {
32
33 $result = $this->insert_contents( '' );
34
35 if ( is_wp_error( $result ) ) {
36 add_action( 'admin_notices', function() use ( $result ) {
37 ?>
38 <div class="notice notice-error">
39 <p><?php echo esc_html( $result->get_error_message() ); ?></p>
40 </div>
41 <?php
42 });
43 }
44
45 return true;
46 }
47
48 /**
49 * Insert new contents into the directory conf file.
50 * Replaces existing marked info. Creates file if none exists.
51 */
52 protected function insert_contents( $new_contents ) {
53 $contents = $this->get_file_contents();
54
55 if ( is_wp_error( $contents ) ) {
56 return $contents;
57 }
58
59 $start_marker = '# BEGIN ' . $this->tag_name;
60 $end_marker = '# END ' . $this->tag_name;
61
62 // Remove previous rules.
63 $contents = preg_replace( '/\s*?' . preg_quote( $start_marker, '/' ) . '.*' . preg_quote( $end_marker, '/' ) . '\s*?/isU', "\n\n", $contents );
64 $contents = trim( $contents );
65
66 if ( $new_contents ) {
67 $contents = $new_contents . "\n\n" . $contents;
68 }
69
70 return $this->put_file_contents( $contents );
71 }
72
73 /**
74 * Get the path to the site's root.
75 * This is an improved version of get_home_path() that *should* work in almost every cases.
76 * Because creating a constant like ABSPATH was too simple.
77 */
78 protected function get_site_root() {
79
80 $home = set_url_scheme( untrailingslashit( get_option( 'home' ) ), 'http' );
81 $siteurl = set_url_scheme( untrailingslashit( get_option( 'siteurl' ) ), 'http' );
82
83 if ( ! empty( $home ) && 0 !== strcasecmp( $home, $siteurl ) ) {
84 $wp_path_rel_to_home = str_ireplace( $home, '', $siteurl ); /* $siteurl - $home */
85 $pos = strripos( str_replace( '\\', '/', ABSPATH ), trailingslashit( $wp_path_rel_to_home ) );
86 $root_path = substr( ABSPATH, 0, $pos );
87 $root_path = trailingslashit( wp_normalize_path( $root_path ) );
88 return $root_path;
89 }
90
91 if ( ! defined( 'PATH_CURRENT_SITE' ) || ! is_multisite() || is_main_site() ) {
92 $root_path = ABSPATH;
93 return $root_path;
94 }
95
96 /**
97 * For a multisite in its own directory, get_home_path() returns the expected path only for the main site.
98 *
99 * Friend, each time an attempt is made to improve this method, and especially this part, please increment the following counter.
100 * Improvement attempts: 3.
101 */
102 $document_root_raw = isset( $_SERVER['DOCUMENT_ROOT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['DOCUMENT_ROOT'] ) ) : '';
103 $document_root = realpath( $document_root_raw ); // `realpath()` is needed for those cases where $_SERVER['DOCUMENT_ROOT'] is totally different from ABSPATH.
104 $document_root = $document_root ? $document_root : ABSPATH;
105 $document_root = trailingslashit( str_replace( '\\', '/', $document_root ) );
106 $path_current_site = trim( str_replace( '\\', '/', PATH_CURRENT_SITE ), '/' );
107 $root_path = trailingslashit( wp_normalize_path( $document_root . $path_current_site ) );
108
109 return $root_path;
110 }
111
112 /**
113 * Get the file contents.
114 */
115 protected function get_file_contents() {
116
117 $file_path = $this->get_file_path();
118 $file_exists = file_exists( $file_path );
119 if ( ! $file_exists ) {
120 $dir_name = dirname( $file_path );
121 if ( ! file_exists( $dir_name ) ){
122 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir
123 mkdir( $dir_name, 0755, true );
124 }
125 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_touch
126 touch( $file_path );
127 }
128 $writable = wp_is_writable( $file_path );
129
130 if ( is_wp_error( $writable ) ) {
131 return $writable;
132 }
133
134 $contents = @file_get_contents( $file_path );
135
136 if ( false === $contents ) {
137 return new \WP_Error(
138 'not_read',
139 sprintf(
140 // translators: %s is a placeholder for the file path.
141 __( 'The %s file could not be read.', 'quickwebp' ),
142 '<code>' . esc_html( $file_path ) . '</code>'
143 )
144 );
145 }
146
147 return $contents;
148 }
149
150 /**
151 * Write contents to the file.
152 */
153 protected function put_file_contents( $contents ) {
154
155 $file_path = $this->get_file_path();
156 $result = $this->put_contents( $file_path, $contents );
157
158 if ( $result ) {
159 return true;
160 }
161
162 return new \WP_Error(
163 'edition_failed',
164 sprintf(
165 // translators: %s is a placeholder for the file path.
166 __( 'Could not write into the %s file.', 'quickwebp' ),
167 '<code>' . esc_html( $file_path ) . '</code>'
168 )
169 );
170 }
171
172 /**
173 * Write contents to the file.
174 */
175 protected function put_contents( $file, $contents, $mode = false ) {
176
177 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen
178 $fp = @fopen( $file, 'wb' );
179
180 if ( ! $fp ) {
181 return false;
182 }
183
184 mbstring_binary_safe_encoding();
185
186 $data_length = strlen( $contents );
187
188 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite
189 $bytes_written = fwrite( $fp, $contents );
190
191 reset_mbstring_encoding();
192
193 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose
194 fclose( $fp );
195
196 if ( $data_length !== $bytes_written ) {
197 return false;
198 }
199
200 $chmod_file = fileperms( ABSPATH . 'index.php' ) & 0777 | 0644;
201 // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_chmod
202 chmod( $file, $chmod_file );
203
204 return true;
205 }
206 }
207