PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 2.1
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v2.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 / Webp / RewriteRules / Display.php

Display.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.1, at classes/Webp/RewriteRules/Display.php

254 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Imagify\Webp\RewriteRules;
3
4 use Imagify\Notices\Notices;
5 use Imagify\Traits\InstanceGetterTrait;
6
7 /**
8 * Display WebP images on the site with rewrite rules.
9 *
10 * @since 1.9
11 */
12 class Display {
13 use InstanceGetterTrait;
14
15 /**
16 * Option value.
17 *
18 * @var string
19 * @since 1.9
20 */
21 const OPTION_VALUE = 'rewrite';
22
23 /**
24 * Init.
25 *
26 * @since 1.9
27 */
28 public function init() {
29 add_filter( 'imagify_settings_on_save', [ $this, 'maybe_add_rewrite_rules' ] );
30 add_action( 'imagify_settings_webp_info', [ $this, 'maybe_add_webp_info' ] );
31 add_action( 'imagify_activation', [ $this, 'activate' ] );
32 add_action( 'imagify_deactivation', [ $this, 'deactivate' ] );
33 }
34
35 /** ----------------------------------------------------------------------------------------- */
36 /** HOOKS =================================================================================== */
37 /** ----------------------------------------------------------------------------------------- */
38
39 /**
40 * If display WebP images via rewrite rules, add the rules to the .htaccess/etc file.
41 *
42 * @since 1.9
43 *
44 * @param array $values The option values.
45 * @return array
46 */
47 public function maybe_add_rewrite_rules( $values ) {
48 global $is_apache, $is_iis7, $is_nginx;
49
50 // Display WebP?
51 $was_enabled = (bool) get_imagify_option( 'display_webp' );
52 // See \Imagify_Options->validate_values_on_update() for why we use 'convert_to_webp' here.
53 $is_enabled = ! empty( $values['display_webp'] ) && ! empty( $values['convert_to_webp'] );
54
55 // Which method?
56 $old_value = get_imagify_option( 'display_webp_method' );
57 $new_value = ! empty( $values['display_webp_method'] ) ? $values['display_webp_method'] : '';
58
59 // Decide when to add or remove rules.
60 $is_rewrite = self::OPTION_VALUE === $new_value;
61 $was_rewrite = self::OPTION_VALUE === $old_value;
62 $add_or_remove = false;
63
64 if ( $is_enabled && $is_rewrite && ( ! $was_enabled || ! $was_rewrite ) ) {
65 // Display WebP & use rewrite method, but only if one of the values changed: add rules.
66 $add_or_remove = 'add';
67 } elseif ( $was_enabled && $was_rewrite && ( ! $is_enabled || ! $is_rewrite ) ) {
68 // Was displaying WebP & was using rewrite method, but only if one of the values changed: remove rules.
69 $add_or_remove = 'remove';
70 } else {
71 return $values;
72 }
73
74 if ( $is_apache ) {
75 $rules = new Apache();
76 } elseif ( $is_iis7 ) {
77 $rules = new IIS();
78 } elseif ( $is_nginx ) {
79 $rules = new Nginx();
80 } else {
81 return $values;
82 }
83
84 if ( 'add' === $add_or_remove ) {
85 // Add the rewrite rules.
86 $result = $rules->add();
87 } else {
88 // Remove the rewrite rules.
89 $result = $rules->remove();
90 }
91
92 if ( ! is_wp_error( $result ) ) {
93 return $values;
94 }
95
96 // Display an error message.
97 if ( is_multisite() && strpos( wp_get_referer(), network_admin_url( '/' ) ) === 0 ) {
98 Notices::get_instance()->add_network_temporary_notice( $result->get_error_message() );
99 } else {
100 Notices::get_instance()->add_site_temporary_notice( $result->get_error_message() );
101 }
102
103 return $values;
104 }
105
106 /**
107 * If the conf file is not writable, add a warning.
108 *
109 * @since 1.9
110 */
111 public function maybe_add_webp_info() {
112 global $is_nginx;
113
114 $conf = $this->get_server_conf();
115
116 if ( ! $conf ) {
117 return;
118 }
119
120 $writable = $conf->is_file_writable();
121
122 if ( is_wp_error( $writable ) ) {
123 $rules = $conf->get_new_contents();
124
125 if ( ! $rules ) {
126 // Uh?
127 return;
128 }
129
130 printf(
131 /* translators: %s is a file name. */
132 esc_html__( 'If you choose to use rewrite rules, you will have to add the following lines manually to the %s file:', 'imagify' ),
133 '<code>' . $this->get_file_path( true ) . '</code>'
134 );
135
136 echo '<pre class="code">' . esc_html( $rules ) . '</pre>';
137 } elseif ( $is_nginx ) {
138 printf(
139 /* translators: %s is a file name. */
140 esc_html__( 'If you choose to use rewrite rules, the file %s will be created and must be included into the server’s configuration file (then restart the server).', 'imagify' ),
141 '<code>' . $this->get_file_path( true ) . '</code>'
142 );
143 }
144 }
145
146 /**
147 * Add rules on plugin activation.
148 *
149 * @since 1.9
150 */
151 public function activate() {
152 $conf = $this->get_server_conf();
153
154 if ( ! $conf ) {
155 return;
156 }
157 if ( ! get_imagify_option( 'display_webp' ) ) {
158 return;
159 }
160 if ( self::OPTION_VALUE !== get_imagify_option( 'display_webp_method' ) ) {
161 return;
162 }
163 if ( is_wp_error( $conf->is_file_writable() ) ) {
164 return;
165 }
166
167 $conf->add();
168 }
169
170 /**
171 * Remove rules on plugin deactivation.
172 *
173 * @since 1.9
174 */
175 public function deactivate() {
176 $conf = $this->get_server_conf();
177
178 if ( ! $conf ) {
179 return;
180 }
181 if ( ! get_imagify_option( 'display_webp' ) ) {
182 return;
183 }
184 if ( self::OPTION_VALUE !== get_imagify_option( 'display_webp_method' ) ) {
185 return;
186 }
187
188 $file_path = $conf->get_file_path();
189 $filesystem = \Imagify_Filesystem::get_instance();
190
191 if ( ! $filesystem->exists( $file_path ) ) {
192 return;
193 }
194 if ( ! $filesystem->is_writable( $file_path ) ) {
195 return;
196 }
197
198 $conf->remove();
199 }
200
201 /** ----------------------------------------------------------------------------------------- */
202 /** TOOLS =================================================================================== */
203 /** ----------------------------------------------------------------------------------------- */
204
205 /**
206 * Get the path to the directory conf file.
207 *
208 * @since 1.9
209 *
210 * @param bool $relative True to get a path relative to the site’s root.
211 * @return string|bool The file path. False on failure.
212 */
213 public function get_file_path( $relative = false ) {
214 if ( ! $this->get_server_conf() ) {
215 return false;
216 }
217
218 $file_path = $this->get_server_conf()->get_file_path();
219
220 if ( $relative ) {
221 return \Imagify_Filesystem::get_instance()->make_path_relative( $file_path );
222 }
223
224 return $file_path;
225 }
226
227 /**
228 * Get the server conf instance.
229 *
230 * @since 1.9
231 *
232 * @return \Imagify\WriteFile\WriteFileInterface
233 */
234 protected function get_server_conf() {
235 global $is_apache, $is_iis7, $is_nginx;
236
237 if ( isset( $this->server_conf ) ) {
238 return $this->server_conf;
239 }
240
241 if ( $is_apache ) {
242 $this->server_conf = new Apache();
243 } elseif ( $is_iis7 ) {
244 $this->server_conf = new IIS();
245 } elseif ( $is_nginx ) {
246 $this->server_conf = new Nginx();
247 } else {
248 $this->server_conf = false;
249 }
250
251 return $this->server_conf;
252 }
253 }
254