| @@ -1,77 +1,67 @@ | ||
| 1 | 1 | <?php |
| 2 | +declare(strict_types=1); | |
| 3 | + | |
| 2 | 4 | namespace Imagify\Webp; |
| 3 | 5 | |
| 4 | -defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); | |
| 6 | +use Imagify\EventManagement\SubscriberInterface; | |
| 7 | +use Imagify\Notices\Notices; | |
| 8 | +use Imagify\Traits\InstanceGetterTrait; | |
| 9 | +use Imagify\WriteFile\WriteFileInterface; | |
| 5 | 10 | |
| 6 | 11 | /** |
| 7 | - * Display WebP images on the site. | |
| 12 | + * Display WebP images on the site using picture tag. | |
| 8 | 13 | * |
| 9 | - * @since 1.9 | |
| 10 | - * @author Grégory Viguier | |
| 14 | + * @since 1.9 | |
| 11 | 15 | */ |
| 12 | -class Display { | |
| 13 | - use \Imagify\Traits\InstanceGetterTrait; | |
| 16 | +class Display implements SubscriberInterface { | |
| 17 | + use InstanceGetterTrait; | |
| 14 | 18 | |
| 15 | 19 | /** |
| 16 | 20 | * Server conf object. |
| 17 | 21 | * |
| 18 | - * @var \Imagify\WriteFile\WriteFileInterface | |
| 19 | - * @since 1.9 | |
| 20 | - * @access protected | |
| 21 | - * @author Grégory Viguier | |
| 22 | + * @var WriteFileInterface|null | |
| 23 | + * @since 1.9 | |
| 22 | 24 | */ |
| 23 | - protected $server_conf; | |
| 25 | + protected $server_conf = null; | |
| 24 | 26 | |
| 25 | 27 | /** |
| 26 | - * Init. | |
| 28 | + * Returns an array of events this subscriber listens to | |
| 27 | 29 | * |
| 28 | - * @since 1.9 | |
| 29 | - * @access public | |
| 30 | - * @author Grégory Viguier | |
| 30 | + * @return array | |
| 31 | 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(); | |
| 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 | + ]; | |
| 40 | 39 | } |
| 41 | 40 | |
| 42 | - /** ----------------------------------------------------------------------------------------- */ | |
| 43 | - /** HOOKS =================================================================================== */ | |
| 44 | - /** ----------------------------------------------------------------------------------------- */ | |
| 45 | - | |
| 46 | 41 | /** |
| 47 | 42 | * If display WebP images, add the WebP type to the .htaccess/etc file. |
| 48 | 43 | * |
| 49 | - * @since 1.9 | |
| 50 | - * @access public | |
| 51 | - * @author Grégory Viguier | |
| 44 | + * @since 1.9 | |
| 52 | 45 | * |
| 53 | - * @param array $values The option values. | |
| 46 | + * @param array $values The option values. | |
| 47 | + * | |
| 54 | 48 | * @return array |
| 55 | 49 | */ |
| 56 | 50 | 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. | |
| 51 | + if ( ! $this->get_server_conf() ) { | |
| 63 | 52 | return $values; |
| 64 | 53 | } |
| 65 | 54 | |
| 66 | - if ( ! $this->get_server_conf() ) { | |
| 67 | - return $values; | |
| 68 | - } | |
| 55 | + $enabled = isset( $values['display_nextgen'] ) ? true : false; | |
| 56 | + $was_enabled = (bool) get_imagify_option( 'display_nextgen' ); | |
| 69 | 57 | |
| 70 | - if ( $new_value ) { | |
| 58 | + $result = false; | |
| 59 | + | |
| 60 | + if ( $enabled && ! $was_enabled ) { | |
| 71 | 61 | // Add the WebP file type. |
| 72 | 62 | $result = $this->get_server_conf()->add(); |
| 73 | - } else { | |
| 63 | + } elseif ( ! $enabled && $was_enabled ) { | |
| 74 | 64 | // Remove the WebP file type. |
| 75 | 65 | $result = $this->get_server_conf()->remove(); |
| 76 | 66 | } |
| 77 | 67 | |
| @@ -80,13 +70,15 @@ | ||
| 80 | 70 | } |
| 81 | 71 | |
| 82 | 72 | // Display an error message. |
| 83 | 73 | 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() ); | |
| 74 | + Notices::get_instance()->add_network_temporary_notice( $result->get_error_message() ); | |
| 75 | + | |
| 76 | + return $values; | |
| 87 | 77 | } |
| 88 | 78 | |
| 79 | + Notices::get_instance()->add_site_temporary_notice( $result->get_error_message() ); | |
| 80 | + | |
| 89 | 81 | return $values; |
| 90 | 82 | } |
| 91 | 83 | |
| 92 | 84 | /** |
| @@ -91,11 +83,9 @@ | ||
| 91 | 83 | |
| 92 | 84 | /** |
| 93 | 85 | * If the conf file is not writable, add a warning. |
| 94 | 86 | * |
| 95 | - * @since 1.9 | |
| 96 | - * @access public | |
| 97 | - * @author Grégory Viguier | |
| 87 | + * @since 1.9 | |
| 98 | 88 | */ |
| 99 | 89 | public function maybe_add_webp_info() { |
| 100 | 90 | $conf = $this->get_server_conf(); |
| 101 | 91 | |
| @@ -120,9 +110,9 @@ | ||
| 120 | 110 | |
| 121 | 111 | printf( |
| 122 | 112 | /* translators: %s is a file name. */ |
| 123 | 113 | 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>' | |
| 114 | + '<code>' . $this->get_file_path( true ) . '</code>' // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 125 | 115 | ); |
| 126 | 116 | |
| 127 | 117 | echo '<pre class="code">' . esc_html( $rules ) . '</pre>'; |
| 128 | 118 | } |
| @@ -129,11 +119,9 @@ | ||
| 129 | 119 | |
| 130 | 120 | /** |
| 131 | 121 | * Add rules on plugin activation. |
| 132 | 122 | * |
| 133 | - * @since 1.9 | |
| 134 | - * @access public | |
| 135 | - * @author Grégory Viguier | |
| 123 | + * @since 1.9 | |
| 136 | 124 | */ |
| 137 | 125 | public function activate() { |
| 138 | 126 | $conf = $this->get_server_conf(); |
| 139 | 127 | |
| @@ -139,11 +127,13 @@ | ||
| 139 | 127 | |
| 140 | 128 | if ( ! $conf ) { |
| 141 | 129 | return; |
| 142 | 130 | } |
| 143 | - if ( ! get_imagify_option( 'display_webp' ) ) { | |
| 131 | + | |
| 132 | + if ( ! get_imagify_option( 'display_nextgen' ) ) { | |
| 144 | 133 | return; |
| 145 | 134 | } |
| 135 | + | |
| 146 | 136 | if ( is_wp_error( $conf->is_file_writable() ) ) { |
| 147 | 137 | return; |
| 148 | 138 | } |
| 149 | 139 | |
| @@ -152,11 +142,9 @@ | ||
| 152 | 142 | |
| 153 | 143 | /** |
| 154 | 144 | * Remove rules on plugin deactivation. |
| 155 | 145 | * |
| 156 | - * @since 1.9 | |
| 157 | - * @access public | |
| 158 | - * @author Grégory Viguier | |
| 146 | + * @since 1.9 | |
| 159 | 147 | */ |
| 160 | 148 | public function deactivate() { |
| 161 | 149 | $conf = $this->get_server_conf(); |
| 162 | 150 | |
| @@ -162,11 +150,8 @@ | ||
| 162 | 150 | |
| 163 | 151 | if ( ! $conf ) { |
| 164 | 152 | return; |
| 165 | 153 | } |
| 166 | - if ( ! get_imagify_option( 'display_webp' ) ) { | |
| 167 | - return; | |
| 168 | - } | |
| 169 | 154 | |
| 170 | 155 | $file_path = $conf->get_file_path(); |
| 171 | 156 | $filesystem = \Imagify_Filesystem::get_instance(); |
| 172 | 157 | |
| @@ -179,18 +164,12 @@ | ||
| 179 | 164 | |
| 180 | 165 | $conf->remove(); |
| 181 | 166 | } |
| 182 | 167 | |
| 183 | - /** ----------------------------------------------------------------------------------------- */ | |
| 184 | - /** TOOLS =================================================================================== */ | |
| 185 | - /** ----------------------------------------------------------------------------------------- */ | |
| 186 | - | |
| 187 | 168 | /** |
| 188 | 169 | * Get the path to the directory conf file. |
| 189 | 170 | * |
| 190 | - * @since 1.9 | |
| 191 | - * @access public | |
| 192 | - * @author Grégory Viguier | |
| 171 | + * @since 1.9 | |
| 193 | 172 | * |
| 194 | 173 | * @param bool $relative True to get a path relative to the site’s root. |
| 195 | 174 | * @return string|bool The file path. False on failure. |
| 196 | 175 | */ |
| @@ -208,35 +187,14 @@ | ||
| 208 | 187 | return $file_path; |
| 209 | 188 | } |
| 210 | 189 | |
| 211 | 190 | /** |
| 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 | 191 | * Get the server conf instance. |
| 232 | 192 | * Note: nothing needed for nginx. |
| 233 | 193 | * |
| 234 | - * @since 1.9 | |
| 235 | - * @access public | |
| 236 | - * @author Grégory Viguier | |
| 194 | + * @since 1.9 | |
| 237 | 195 | * |
| 238 | - * @return \Imagify\WriteFile\WriteFileInterface | |
| 196 | + * @return WriteFileInterface | |
| 239 | 197 | */ |
| 240 | 198 | protected function get_server_conf() { |
| 241 | 199 | global $is_apache, $is_iis7; |
| 242 | 200 | |
| @@ -247,10 +205,8 @@ | ||
| 247 | 205 | if ( $is_apache ) { |
| 248 | 206 | $this->server_conf = new Apache(); |
| 249 | 207 | } elseif ( $is_iis7 ) { |
| 250 | 208 | $this->server_conf = new IIS(); |
| 251 | - } else { | |
| 252 | - $this->server_conf = false; | |
| 253 | 209 | } |
| 254 | 210 | |
| 255 | 211 | return $this->server_conf; |
| 256 | 212 | } |