PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / 1.9.13
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF v1.9.13
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 / Display.php

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

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