| 1 |
<?php |
| 2 |
namespace Imagify\Webp\RewriteRules; |
| 3 |
|
| 4 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 5 |
|
| 6 |
/** |
| 7 |
* Display webp images on the site with rewrite rules. |
| 8 |
* |
| 9 |
* @since 1.9 |
| 10 |
* @author Grégory Viguier |
| 11 |
*/ |
| 12 |
class Display { |
| 13 |
use \Imagify\Traits\InstanceGetterTrait; |
| 14 |
|
| 15 |
/** |
| 16 |
* Option value. |
| 17 |
* |
| 18 |
* @var string |
| 19 |
* @since 1.9 |
| 20 |
* @author Grégory Viguier |
| 21 |
*/ |
| 22 |
const OPTION_VALUE = 'rewrite'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Init. |
| 26 |
* |
| 27 |
* @since 1.9 |
| 28 |
* @access public |
| 29 |
* @author Grégory Viguier |
| 30 |
*/ |
| 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 |
} |
| 37 |
|
| 38 |
/** ----------------------------------------------------------------------------------------- */ |
| 39 |
/** HOOKS =================================================================================== */ |
| 40 |
/** ----------------------------------------------------------------------------------------- */ |
| 41 |
|
| 42 |
/** |
| 43 |
* If display webp images via rewrite rules, add the rules to the .htaccess/etc file. |
| 44 |
* |
| 45 |
* @since 1.9 |
| 46 |
* @access public |
| 47 |
* @author Grégory Viguier |
| 48 |
* |
| 49 |
* @param array $values The option values. |
| 50 |
* @return array |
| 51 |
*/ |
| 52 |
public function maybe_add_rewrite_rules( $values ) { |
| 53 |
global $is_apache, $is_iis7, $is_nginx; |
| 54 |
|
| 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 |
// Which method? |
| 61 |
$old_value = get_imagify_option( 'display_webp_method' ); |
| 62 |
$new_value = ! empty( $values['display_webp_method'] ) ? $values['display_webp_method'] : ''; |
| 63 |
|
| 64 |
// 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; |
| 68 |
|
| 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 { |
| 76 |
return $values; |
| 77 |
} |
| 78 |
|
| 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 |
} |
| 88 |
|
| 89 |
if ( 'add' === $add_or_remove ) { |
| 90 |
// Add the rewrite rules. |
| 91 |
$result = $rules->add(); |
| 92 |
} else { |
| 93 |
// Remove the rewrite rules. |
| 94 |
$result = $rules->remove(); |
| 95 |
} |
| 96 |
|
| 97 |
if ( ! is_wp_error( $result ) ) { |
| 98 |
return $values; |
| 99 |
} |
| 100 |
|
| 101 |
// Display an error message. |
| 102 |
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() ); |
| 106 |
} |
| 107 |
|
| 108 |
return $values; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* If the conf file is not writable, add a warning. |
| 113 |
* |
| 114 |
* @since 1.9 |
| 115 |
* @access public |
| 116 |
* @author Grégory Viguier |
| 117 |
*/ |
| 118 |
public function maybe_add_webp_info() { |
| 119 |
global $is_nginx; |
| 120 |
|
| 121 |
$conf = $this->get_server_conf(); |
| 122 |
|
| 123 |
if ( ! $conf ) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
$writable = $conf->is_file_writable(); |
| 128 |
|
| 129 |
if ( is_wp_error( $writable ) ) { |
| 130 |
$rules = $conf->get_new_contents(); |
| 131 |
|
| 132 |
if ( ! $rules ) { |
| 133 |
// Uh? |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
printf( |
| 138 |
/* translators: %s is a file name. */ |
| 139 |
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>' |
| 141 |
); |
| 142 |
|
| 143 |
echo '<pre class="code">' . esc_html( $rules ) . '</pre>'; |
| 144 |
} elseif ( $is_nginx ) { |
| 145 |
printf( |
| 146 |
/* translators: %s is a file name. */ |
| 147 |
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>' |
| 149 |
); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Add rules on plugin activation. |
| 155 |
* |
| 156 |
* @since 1.9 |
| 157 |
* @access public |
| 158 |
* @author Grégory Viguier |
| 159 |
*/ |
| 160 |
public function activate() { |
| 161 |
$conf = $this->get_server_conf(); |
| 162 |
|
| 163 |
if ( ! $conf ) { |
| 164 |
return; |
| 165 |
} |
| 166 |
if ( ! get_imagify_option( 'display_webp' ) ) { |
| 167 |
return; |
| 168 |
} |
| 169 |
if ( self::OPTION_VALUE !== get_imagify_option( 'display_webp_method' ) ) { |
| 170 |
return; |
| 171 |
} |
| 172 |
if ( is_wp_error( $conf->is_file_writable() ) ) { |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
$conf->add(); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Remove rules on plugin deactivation. |
| 181 |
* |
| 182 |
* @since 1.9 |
| 183 |
* @access public |
| 184 |
* @author Grégory Viguier |
| 185 |
*/ |
| 186 |
public function deactivate() { |
| 187 |
$conf = $this->get_server_conf(); |
| 188 |
|
| 189 |
if ( ! $conf ) { |
| 190 |
return; |
| 191 |
} |
| 192 |
if ( ! get_imagify_option( 'display_webp' ) ) { |
| 193 |
return; |
| 194 |
} |
| 195 |
if ( self::OPTION_VALUE !== get_imagify_option( 'display_webp_method' ) ) { |
| 196 |
return; |
| 197 |
} |
| 198 |
|
| 199 |
$file_path = $conf->get_file_path(); |
| 200 |
$filesystem = \Imagify_Filesystem::get_instance(); |
| 201 |
|
| 202 |
if ( ! $filesystem->exists( $file_path ) ) { |
| 203 |
return; |
| 204 |
} |
| 205 |
if ( ! $filesystem->is_writable( $file_path ) ) { |
| 206 |
return; |
| 207 |
} |
| 208 |
|
| 209 |
$conf->remove(); |
| 210 |
} |
| 211 |
|
| 212 |
/** ----------------------------------------------------------------------------------------- */ |
| 213 |
/** TOOLS =================================================================================== */ |
| 214 |
/** ----------------------------------------------------------------------------------------- */ |
| 215 |
|
| 216 |
/** |
| 217 |
* Get the path to the directory conf file. |
| 218 |
* |
| 219 |
* @since 1.9 |
| 220 |
* @access public |
| 221 |
* @author Grégory Viguier |
| 222 |
* |
| 223 |
* @param bool $relative True to get a path relative to the site’s root. |
| 224 |
* @return string|bool The file path. False on failure. |
| 225 |
*/ |
| 226 |
public function get_file_path( $relative = false ) { |
| 227 |
if ( ! $this->get_server_conf() ) { |
| 228 |
return false; |
| 229 |
} |
| 230 |
|
| 231 |
$file_path = $this->get_server_conf()->get_file_path(); |
| 232 |
|
| 233 |
if ( $relative ) { |
| 234 |
return \Imagify_Filesystem::get_instance()->make_path_relative( $file_path ); |
| 235 |
} |
| 236 |
|
| 237 |
return $file_path; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Get the server conf instance. |
| 242 |
* |
| 243 |
* @since 1.9 |
| 244 |
* @access public |
| 245 |
* @author Grégory Viguier |
| 246 |
* |
| 247 |
* @return \Imagify\WriteFile\WriteFileInterface |
| 248 |
*/ |
| 249 |
protected function get_server_conf() { |
| 250 |
global $is_apache, $is_iis7, $is_nginx; |
| 251 |
|
| 252 |
if ( isset( $this->server_conf ) ) { |
| 253 |
return $this->server_conf; |
| 254 |
} |
| 255 |
|
| 256 |
if ( $is_apache ) { |
| 257 |
$this->server_conf = new Apache(); |
| 258 |
} elseif ( $is_iis7 ) { |
| 259 |
$this->server_conf = new IIS(); |
| 260 |
} elseif ( $is_nginx ) { |
| 261 |
$this->server_conf = new Nginx(); |
| 262 |
} else { |
| 263 |
$this->server_conf = false; |
| 264 |
} |
| 265 |
|
| 266 |
return $this->server_conf; |
| 267 |
} |
| 268 |
} |
| 269 |
|