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

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

170 lines 3.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\Avif;
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 using picture tag.
12 */
13 class Display implements SubscriberInterface {
14 /**
15 * Server conf object.
16 *
17 * @var WriteFileInterface|null
18 * @since 1.9
19 */
20 protected $server_conf = null;
21
22 /**
23 * Returns an array of events this subscriber listens to
24 *
25 * @return array
26 */
27 public static function get_subscribed_events() {
28 return [
29 'imagify_settings_on_save' => [ 'maybe_add_rewrite_rules', 12 ],
30 'imagify_activation' => 'activate',
31 'imagify_deactivation' => 'deactivate',
32 ];
33 }
34
35 /**
36 * If display Next-Gen images, add the AVIF type to the .htaccess/etc file.
37 *
38 * @since 1.9
39 *
40 * @param array $values The option values.
41 *
42 * @return array
43 */
44 public function maybe_add_rewrite_rules( $values ) {
45 if ( ! $this->get_server_conf() ) {
46 return $values;
47 }
48
49 $enabled = isset( $values['display_nextgen'] ) ? true : false;
50 $result = false;
51
52 if ( $enabled ) {
53 // Add the AVIF file type.
54 $result = $this->get_server_conf()->add();
55 } elseif ( ! $enabled ) {
56 // Remove the AVIF file type.
57 $result = $this->get_server_conf()->remove();
58 }
59
60 if ( ! is_wp_error( $result ) ) {
61 return $values;
62 }
63
64 // Display an error message.
65 if ( is_multisite() && strpos( wp_get_referer(), network_admin_url( '/' ) ) === 0 ) {
66 Notices::get_instance()->add_network_temporary_notice( $result->get_error_message() );
67
68 return $values;
69 }
70
71 Notices::get_instance()->add_site_temporary_notice( $result->get_error_message() );
72
73 return $values;
74 }
75
76 /**
77 * Add rules on plugin activation.
78 *
79 * @since 1.9
80 */
81 public function activate() {
82 $conf = $this->get_server_conf();
83
84 if ( ! $conf ) {
85 return;
86 }
87
88 if ( ! get_imagify_option( 'display_nextgen' ) ) {
89 return;
90 }
91
92 if ( is_wp_error( $conf->is_file_writable() ) ) {
93 return;
94 }
95
96 $conf->add();
97 }
98
99 /**
100 * Remove rules on plugin deactivation.
101 *
102 * @since 1.9
103 */
104 public function deactivate() {
105 $conf = $this->get_server_conf();
106
107 if ( ! $conf ) {
108 return;
109 }
110
111 $file_path = $conf->get_file_path();
112 $filesystem = \Imagify_Filesystem::get_instance();
113
114 if ( ! $filesystem->exists( $file_path ) ) {
115 return;
116 }
117 if ( ! $filesystem->is_writable( $file_path ) ) {
118 return;
119 }
120
121 $conf->remove();
122 }
123
124 /**
125 * Get the path to the directory conf file.
126 *
127 * @since 1.9
128 *
129 * @param bool $relative True to get a path relative to the site’s root.
130 * @return string|bool The file path. False on failure.
131 */
132 public function get_file_path( $relative = false ) {
133 if ( ! $this->get_server_conf() ) {
134 return false;
135 }
136
137 $file_path = $this->get_server_conf()->get_file_path();
138
139 if ( $relative ) {
140 return \Imagify_Filesystem::get_instance()->make_path_relative( $file_path );
141 }
142
143 return $file_path;
144 }
145
146 /**
147 * Get the server conf instance.
148 * Note: nothing needed for nginx.
149 *
150 * @since 1.9
151 *
152 * @return WriteFileInterface
153 */
154 protected function get_server_conf() {
155 global $is_apache, $is_iis7;
156
157 if ( isset( $this->server_conf ) ) {
158 return $this->server_conf;
159 }
160
161 if ( $is_apache ) {
162 $this->server_conf = new Apache();
163 } elseif ( $is_iis7 ) {
164 $this->server_conf = new IIS();
165 }
166
167 return $this->server_conf;
168 }
169 }
170