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

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