| @@ -1,98 +1,81 @@ | ||
| 1 | 1 | <?php |
| 2 | +declare(strict_types=1); | |
| 3 | + | |
| 2 | 4 | namespace Imagify\Webp\RewriteRules; |
| 3 | 5 | |
| 4 | -defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); | |
| 6 | +use Imagify\EventManagement\SubscriberInterface; | |
| 7 | +use Imagify\Notices\Notices; | |
| 8 | +use Imagify\WriteFile\WriteFileInterface; | |
| 5 | 9 | |
| 6 | 10 | /** |
| 7 | 11 | * Display WebP images on the site with rewrite rules. |
| 8 | 12 | * |
| 9 | - * @since 1.9 | |
| 10 | - * @author Grégory Viguier | |
| 13 | + * @since 1.9 | |
| 11 | 14 | */ |
| 12 | -class Display { | |
| 13 | - use \Imagify\Traits\InstanceGetterTrait; | |
| 15 | +class Display implements SubscriberInterface { | |
| 16 | + /** | |
| 17 | + * Configuration file writer. | |
| 18 | + * | |
| 19 | + * @var WriteFileInterface|null | |
| 20 | + */ | |
| 21 | + protected $server_conf = null; | |
| 14 | 22 | |
| 15 | 23 | /** |
| 16 | 24 | * Option value. |
| 17 | 25 | * |
| 18 | - * @var string | |
| 19 | - * @since 1.9 | |
| 20 | - * @author Grégory Viguier | |
| 26 | + * @var string | |
| 27 | + * @since 1.9 | |
| 21 | 28 | */ |
| 22 | 29 | const OPTION_VALUE = 'rewrite'; |
| 23 | 30 | |
| 24 | 31 | /** |
| 25 | - * Init. | |
| 32 | + * Returns an array of events this subscriber listens to | |
| 26 | 33 | * |
| 27 | - * @since 1.9 | |
| 28 | - * @access public | |
| 29 | - * @author Grégory Viguier | |
| 34 | + * @return array | |
| 30 | 35 | */ |
| 31 | - public function init() { | |
| 32 | - add_filter( 'imagify_settings_on_save', [ $this, 'maybe_add_rewrite_rules' ] ); | |
| 33 | - add_action( 'imagify_settings_webp_info', [ $this, 'maybe_add_webp_info' ] ); | |
| 34 | - add_action( 'imagify_activation', [ $this, 'activate' ] ); | |
| 35 | - add_action( 'imagify_deactivation', [ $this, 'deactivate' ] ); | |
| 36 | + public static function get_subscribed_events() { | |
| 37 | + return [ | |
| 38 | + 'imagify_settings_on_save' => [ 'maybe_add_rewrite_rules', 10 ], | |
| 39 | + 'imagify_settings_webp_info' => 'maybe_add_webp_info', | |
| 40 | + 'imagify_activation' => 'activate', | |
| 41 | + 'imagify_deactivation' => 'deactivate', | |
| 42 | + ]; | |
| 36 | 43 | } |
| 37 | 44 | |
| 38 | - /** ----------------------------------------------------------------------------------------- */ | |
| 39 | - /** HOOKS =================================================================================== */ | |
| 40 | - /** ----------------------------------------------------------------------------------------- */ | |
| 41 | - | |
| 42 | 45 | /** |
| 43 | 46 | * If display WebP images via rewrite rules, add the rules to the .htaccess/etc file. |
| 44 | 47 | * |
| 45 | - * @since 1.9 | |
| 46 | - * @access public | |
| 47 | - * @author Grégory Viguier | |
| 48 | + * @since 1.9 | |
| 48 | 49 | * |
| 49 | - * @param array $values The option values. | |
| 50 | + * @param array $values The option values. | |
| 51 | + * | |
| 50 | 52 | * @return array |
| 51 | 53 | */ |
| 52 | 54 | public function maybe_add_rewrite_rules( $values ) { |
| 53 | - global $is_apache, $is_iis7, $is_nginx; | |
| 55 | + $was_enabled = (bool) get_imagify_option( 'display_nextgen' ); | |
| 56 | + $is_enabled = ! empty( $values['display_nextgen'] ); | |
| 54 | 57 | |
| 55 | - // Display WebP? | |
| 56 | - $was_enabled = (bool) get_imagify_option( 'display_webp' ); | |
| 57 | - // See \Imagify_Options->validate_values_on_update() for why we use 'convert_to_webp' here. | |
| 58 | - $is_enabled = ! empty( $values['display_webp'] ) && ! empty( $values['convert_to_webp'] ); | |
| 59 | - | |
| 60 | 58 | // Which method? |
| 61 | - $old_value = get_imagify_option( 'display_webp_method' ); | |
| 62 | - $new_value = ! empty( $values['display_webp_method'] ) ? $values['display_webp_method'] : ''; | |
| 59 | + $old_value = get_imagify_option( 'display_nextgen_method' ); | |
| 60 | + $new_value = ! empty( $values['display_nextgen_method'] ) ? $values['display_nextgen_method'] : ''; | |
| 63 | 61 | |
| 64 | 62 | // Decide when to add or remove rules. |
| 65 | - $is_rewrite = self::OPTION_VALUE === $new_value; | |
| 66 | - $was_rewrite = self::OPTION_VALUE === $old_value; | |
| 67 | - $add_or_remove = false; | |
| 63 | + $is_rewrite = self::OPTION_VALUE === $new_value; | |
| 64 | + $was_rewrite = self::OPTION_VALUE === $old_value; | |
| 68 | 65 | |
| 69 | - if ( $is_enabled && $is_rewrite && ( ! $was_enabled || ! $was_rewrite ) ) { | |
| 70 | - // Display WebP & use rewrite method, but only if one of the values changed: add rules. | |
| 71 | - $add_or_remove = 'add'; | |
| 72 | - } elseif ( $was_enabled && $was_rewrite && ( ! $is_enabled || ! $is_rewrite ) ) { | |
| 73 | - // Was displaying WebP & was using rewrite method, but only if one of the values changed: remove rules. | |
| 74 | - $add_or_remove = 'remove'; | |
| 75 | - } else { | |
| 66 | + if ( ! $this->get_server_conf() ) { | |
| 76 | 67 | return $values; |
| 77 | 68 | } |
| 78 | 69 | |
| 79 | - if ( $is_apache ) { | |
| 80 | - $rules = new Apache(); | |
| 81 | - } elseif ( $is_iis7 ) { | |
| 82 | - $rules = new IIS(); | |
| 83 | - } elseif ( $is_nginx ) { | |
| 84 | - $rules = new Nginx(); | |
| 85 | - } else { | |
| 86 | - return $values; | |
| 87 | - } | |
| 70 | + $result = false; | |
| 88 | 71 | |
| 89 | - if ( 'add' === $add_or_remove ) { | |
| 72 | + if ( $is_enabled && $is_rewrite && ( ! $was_enabled || ! $was_rewrite ) ) { | |
| 90 | 73 | // Add the rewrite rules. |
| 91 | - $result = $rules->add(); | |
| 92 | - } else { | |
| 74 | + $result = $this->get_server_conf()->add(); | |
| 75 | + } elseif ( $was_enabled && $was_rewrite && ( ! $is_enabled || ! $is_rewrite ) ) { | |
| 93 | 76 | // Remove the rewrite rules. |
| 94 | - $result = $rules->remove(); | |
| 77 | + $result = $this->get_server_conf()->remove(); | |
| 95 | 78 | } |
| 96 | 79 | |
| 97 | 80 | if ( ! is_wp_error( $result ) ) { |
| 98 | 81 | return $values; |
| @@ -99,13 +82,15 @@ | ||
| 99 | 82 | } |
| 100 | 83 | |
| 101 | 84 | // Display an error message. |
| 102 | 85 | if ( is_multisite() && strpos( wp_get_referer(), network_admin_url( '/' ) ) === 0 ) { |
| 103 | - \Imagify_Notices::get_instance()->add_network_temporary_notice( $result->get_error_message() ); | |
| 104 | - } else { | |
| 105 | - \Imagify_Notices::get_instance()->add_site_temporary_notice( $result->get_error_message() ); | |
| 86 | + Notices::get_instance()->add_network_temporary_notice( $result->get_error_message() ); | |
| 87 | + | |
| 88 | + return $values; | |
| 106 | 89 | } |
| 107 | 90 | |
| 91 | + Notices::get_instance()->add_site_temporary_notice( $result->get_error_message() ); | |
| 92 | + | |
| 108 | 93 | return $values; |
| 109 | 94 | } |
| 110 | 95 | |
| 111 | 96 | /** |
| @@ -110,11 +95,9 @@ | ||
| 110 | 95 | |
| 111 | 96 | /** |
| 112 | 97 | * If the conf file is not writable, add a warning. |
| 113 | 98 | * |
| 114 | - * @since 1.9 | |
| 115 | - * @access public | |
| 116 | - * @author Grégory Viguier | |
| 99 | + * @since 1.9 | |
| 117 | 100 | */ |
| 118 | 101 | public function maybe_add_webp_info() { |
| 119 | 102 | global $is_nginx; |
| 120 | 103 | |
| @@ -136,9 +119,9 @@ | ||
| 136 | 119 | |
| 137 | 120 | printf( |
| 138 | 121 | /* translators: %s is a file name. */ |
| 139 | 122 | esc_html__( 'If you choose to use rewrite rules, you will have to add the following lines manually to the %s file:', 'imagify' ), |
| 140 | - '<code>' . $this->get_file_path( true ) . '</code>' | |
| 123 | + '<code>' . $this->get_file_path( true ) . '</code>' // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 141 | 124 | ); |
| 142 | 125 | |
| 143 | 126 | echo '<pre class="code">' . esc_html( $rules ) . '</pre>'; |
| 144 | 127 | } elseif ( $is_nginx ) { |
| @@ -144,9 +127,9 @@ | ||
| 144 | 127 | } elseif ( $is_nginx ) { |
| 145 | 128 | printf( |
| 146 | 129 | /* translators: %s is a file name. */ |
| 147 | 130 | esc_html__( 'If you choose to use rewrite rules, the file %s will be created and must be included into the server’s configuration file (then restart the server).', 'imagify' ), |
| 148 | - '<code>' . $this->get_file_path( true ) . '</code>' | |
| 131 | + '<code>' . $this->get_file_path( true ) . '</code>' // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 149 | 132 | ); |
| 150 | 133 | } |
| 151 | 134 | } |
| 152 | 135 | |
| @@ -152,11 +135,9 @@ | ||
| 152 | 135 | |
| 153 | 136 | /** |
| 154 | 137 | * Add rules on plugin activation. |
| 155 | 138 | * |
| 156 | - * @since 1.9 | |
| 157 | - * @access public | |
| 158 | - * @author Grégory Viguier | |
| 139 | + * @since 1.9 | |
| 159 | 140 | */ |
| 160 | 141 | public function activate() { |
| 161 | 142 | $conf = $this->get_server_conf(); |
| 162 | 143 | |
| @@ -162,12 +143,13 @@ | ||
| 162 | 143 | |
| 163 | 144 | if ( ! $conf ) { |
| 164 | 145 | return; |
| 165 | 146 | } |
| 166 | - if ( ! get_imagify_option( 'display_webp' ) ) { | |
| 147 | + | |
| 148 | + if ( ! get_imagify_option( 'display_nextgen' ) ) { | |
| 167 | 149 | return; |
| 168 | 150 | } |
| 169 | - if ( self::OPTION_VALUE !== get_imagify_option( 'display_webp_method' ) ) { | |
| 151 | + if ( self::OPTION_VALUE !== get_imagify_option( 'display_nextgen_method' ) ) { | |
| 170 | 152 | return; |
| 171 | 153 | } |
| 172 | 154 | if ( is_wp_error( $conf->is_file_writable() ) ) { |
| 173 | 155 | return; |
| @@ -178,11 +160,9 @@ | ||
| 178 | 160 | |
| 179 | 161 | /** |
| 180 | 162 | * Remove rules on plugin deactivation. |
| 181 | 163 | * |
| 182 | - * @since 1.9 | |
| 183 | - * @access public | |
| 184 | - * @author Grégory Viguier | |
| 164 | + * @since 1.9 | |
| 185 | 165 | */ |
| 186 | 166 | public function deactivate() { |
| 187 | 167 | $conf = $this->get_server_conf(); |
| 188 | 168 | |
| @@ -188,12 +168,12 @@ | ||
| 188 | 168 | |
| 189 | 169 | if ( ! $conf ) { |
| 190 | 170 | return; |
| 191 | 171 | } |
| 192 | - if ( ! get_imagify_option( 'display_webp' ) ) { | |
| 172 | + if ( ! get_imagify_option( 'display_nextgen' ) ) { | |
| 193 | 173 | return; |
| 194 | 174 | } |
| 195 | - if ( self::OPTION_VALUE !== get_imagify_option( 'display_webp_method' ) ) { | |
| 175 | + if ( self::OPTION_VALUE !== get_imagify_option( 'display_nextgen_method' ) ) { | |
| 196 | 176 | return; |
| 197 | 177 | } |
| 198 | 178 | |
| 199 | 179 | $file_path = $conf->get_file_path(); |
| @@ -208,21 +188,16 @@ | ||
| 208 | 188 | |
| 209 | 189 | $conf->remove(); |
| 210 | 190 | } |
| 211 | 191 | |
| 212 | - /** ----------------------------------------------------------------------------------------- */ | |
| 213 | - /** TOOLS =================================================================================== */ | |
| 214 | - /** ----------------------------------------------------------------------------------------- */ | |
| 215 | - | |
| 216 | 192 | /** |
| 217 | 193 | * Get the path to the directory conf file. |
| 218 | 194 | * |
| 219 | - * @since 1.9 | |
| 220 | - * @access public | |
| 221 | - * @author Grégory Viguier | |
| 195 | + * @since 1.9 | |
| 222 | 196 | * |
| 223 | 197 | * @param bool $relative True to get a path relative to the site’s root. |
| 224 | - * @return string|bool The file path. False on failure. | |
| 198 | + * | |
| 199 | + * @return string|bool The file path. False on failure. | |
| 225 | 200 | */ |
| 226 | 201 | public function get_file_path( $relative = false ) { |
| 227 | 202 | if ( ! $this->get_server_conf() ) { |
| 228 | 203 | return false; |
| @@ -239,13 +214,11 @@ | ||
| 239 | 214 | |
| 240 | 215 | /** |
| 241 | 216 | * Get the server conf instance. |
| 242 | 217 | * |
| 243 | - * @since 1.9 | |
| 244 | - * @access public | |
| 245 | - * @author Grégory Viguier | |
| 218 | + * @since 1.9 | |
| 246 | 219 | * |
| 247 | - * @return \Imagify\WriteFile\WriteFileInterface | |
| 220 | + * @return WriteFileInterface | |
| 248 | 221 | */ |
| 249 | 222 | protected function get_server_conf() { |
| 250 | 223 | global $is_apache, $is_iis7, $is_nginx; |
| 251 | 224 | |
| @@ -258,10 +231,8 @@ | ||
| 258 | 231 | } elseif ( $is_iis7 ) { |
| 259 | 232 | $this->server_conf = new IIS(); |
| 260 | 233 | } elseif ( $is_nginx ) { |
| 261 | 234 | $this->server_conf = new Nginx(); |
| 262 | - } else { | |
| 263 | - $this->server_conf = false; | |
| 264 | 235 | } |
| 265 | 236 | |
| 266 | 237 | return $this->server_conf; |
| 267 | 238 | } |