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 / Display.php

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

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