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

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

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