| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('WPO_VERSION')) die('No direct access allowed'); |
| 4 |
|
| 5 |
if (!class_exists('WP_Optimize_WebP')) : |
| 6 |
|
| 7 |
class WP_Optimize_WebP { |
| 8 |
|
| 9 |
private $_htaccess = null; |
| 10 |
|
| 11 |
private $_rewrite_status = ''; |
| 12 |
|
| 13 |
private $_should_use_webp = false; |
| 14 |
|
| 15 |
/** |
| 16 |
* Constructor |
| 17 |
*/ |
| 18 |
private function __construct() { |
| 19 |
$this->maybe_set_converter_status(); |
| 20 |
$this->maybe_set_rewrite_status(); |
| 21 |
if (!is_admin()) { |
| 22 |
$this->maybe_decide_webp_serve_method(); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Returns singleton instance |
| 28 |
* |
| 29 |
* @return WP_Optimize_WebP |
| 30 |
*/ |
| 31 |
public static function get_instance() { |
| 32 |
static $instance = null; |
| 33 |
if (null === $instance) { |
| 34 |
$instance = new WP_Optimize_WebP(); |
| 35 |
} |
| 36 |
return $instance; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Detect working webp converters and set converter status, if needed |
| 41 |
*/ |
| 42 |
private function maybe_set_converter_status() { |
| 43 |
$converters = WP_Optimize()->get_options()->get_option('webp_converters', false); |
| 44 |
if (empty($converters)) { |
| 45 |
$this->set_converter_status(); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Test Run and find converter status |
| 51 |
*/ |
| 52 |
private function set_converter_status() { |
| 53 |
if (!class_exists('WPO_WebP_Test_Run')) { |
| 54 |
require_once WPO_PLUGIN_MAIN_PATH . 'webp/class-wpo-webp-test-run.php'; |
| 55 |
} |
| 56 |
$converter_status = WPO_WebP_Test_Run::get_converter_status(); |
| 57 |
WP_Optimize()->get_options()->update_option('webp_converters', $converter_status['working_converters']); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* May be set server's rewrite status |
| 62 |
*/ |
| 63 |
private function maybe_set_rewrite_status() { |
| 64 |
$this->_should_use_webp = WP_Optimize()->get_options()->get_option('webp_conversion'); |
| 65 |
if ($this->_should_use_webp) { |
| 66 |
$this->set_rewrite_status(); |
| 67 |
} |
| 68 |
} |
| 69 |
/** |
| 70 |
* Sets server's rewrite status |
| 71 |
*/ |
| 72 |
private function set_rewrite_status() { |
| 73 |
$this->_rewrite_status = WP_Optimize()->get_options()->get_option('rewrite_status', false); |
| 74 |
if ('true' === $this->_rewrite_status) { |
| 75 |
$this->setup_htaccess_file(); |
| 76 |
return; |
| 77 |
} elseif ('false' === $this->_rewrite_status) return; |
| 78 |
|
| 79 |
if (!class_exists('WPO_Htaccess_Capabilities')) { |
| 80 |
require_once WPO_PLUGIN_MAIN_PATH . 'webp/class-wpo-htaccess-capabilities.php'; |
| 81 |
} |
| 82 |
$htc = WPO_Htaccess_Capabilities::get_instance(); |
| 83 |
|
| 84 |
if ($htc->htaccess_enabled && $htc->mod_rewrite && $htc->mod_headers && $htc->mod_mime) { |
| 85 |
$this->_rewrite_status = 'true'; |
| 86 |
$this->setup_htaccess_file(); |
| 87 |
} else { |
| 88 |
$this->_rewrite_status = 'false'; |
| 89 |
} |
| 90 |
WP_Optimize()->get_options()->update_option('rewrite_status', $this->_rewrite_status); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* If webp images should be used, then decide whether it is possible to server webp |
| 95 |
* using rewrite rules or using altered html method |
| 96 |
*/ |
| 97 |
private function maybe_decide_webp_serve_method() { |
| 98 |
if ('true' === $this->_rewrite_status) { |
| 99 |
if (!$this->_should_use_webp) { |
| 100 |
$this->save_htaccess_rules(false); |
| 101 |
return; |
| 102 |
} else { |
| 103 |
if (!$this->is_webp_redirection_possible()) { |
| 104 |
$this->maybe_use_alter_html(); |
| 105 |
} |
| 106 |
} |
| 107 |
} else { |
| 108 |
if ($this->_should_use_webp) { |
| 109 |
$this->maybe_use_alter_html(); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* If alter html method is possible, then use it |
| 116 |
*/ |
| 117 |
private function maybe_use_alter_html() { |
| 118 |
if ($this->is_alter_html_possible()) { |
| 119 |
$this->use_alter_html(); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Even if server support .htaccess rewrite, sometimes it is not possible |
| 125 |
* to serve webp images. So, find it webp redirection is possible or not |
| 126 |
* |
| 127 |
* @return bool |
| 128 |
*/ |
| 129 |
private function is_webp_redirection_possible() { |
| 130 |
if (!class_exists('WPO_WebP_Self_Test')) { |
| 131 |
require_once WPO_PLUGIN_MAIN_PATH . 'webp/class-wpo-webp-self-test.php'; |
| 132 |
} |
| 133 |
$self_test = WPO_WebP_Self_Test::get_instance(); |
| 134 |
|
| 135 |
if ($self_test->get_webp_image()) { |
| 136 |
$this->save_htaccess_rules($this->_should_use_webp); |
| 137 |
if ($self_test->is_webp_served()) return true; |
| 138 |
} |
| 139 |
$this->save_htaccess_rules(); |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Decide whether the browser requesting the URL can accept webp images or not |
| 145 |
* |
| 146 |
* @return bool |
| 147 |
*/ |
| 148 |
private function is_browser_accepting_webp() { |
| 149 |
return (isset($_SERVER['HTTP_ACCEPT']) && false !== strpos($_SERVER['HTTP_ACCEPT'], 'image/webp')); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Detect whether using alter HTML method is possible or not |
| 154 |
* |
| 155 |
* @return bool |
| 156 |
*/ |
| 157 |
private function is_alter_html_possible() { |
| 158 |
if ($this->is_browser_accepting_webp()) { |
| 159 |
return true; |
| 160 |
} |
| 161 |
return false; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Setup alter html method |
| 166 |
*/ |
| 167 |
private function use_alter_html() { |
| 168 |
if (!class_exists('WPO_WebP_Alter_HTML')) { |
| 169 |
require_once WPO_PLUGIN_MAIN_PATH . 'webp/class-wpo-webp-alter-html.php'; |
| 170 |
} |
| 171 |
WPO_WebP_Alter_HTML::get_instance(); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Initialize .htaccess |
| 176 |
*/ |
| 177 |
private function setup_htaccess_file() { |
| 178 |
if (null !== $this->_htaccess) return; |
| 179 |
$wp_uploads = wp_get_upload_dir(); |
| 180 |
$htaccess_file = $wp_uploads['basedir'] . '/.htaccess'; |
| 181 |
if (!file_exists($htaccess_file)) { |
| 182 |
file_put_contents($htaccess_file, ''); |
| 183 |
} |
| 184 |
if (!class_exists('WP_Optimize_Htaccess')) { |
| 185 |
require_once WPO_PLUGIN_MAIN_PATH . 'includes/class-wp-optimize-htaccess.php'; |
| 186 |
} |
| 187 |
$this->_htaccess = new WP_Optimize_Htaccess($htaccess_file); |
| 188 |
$this->add_webp_mime_type(); |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Save .htaccess rules |
| 193 |
* |
| 194 |
* @return bool |
| 195 |
*/ |
| 196 |
private function save_htaccess_rules($webp_conversion_enabled = false) { |
| 197 |
$htaccess_comment_section = 'WP-Optimize WebP Rules'; |
| 198 |
|
| 199 |
if ($this->_htaccess->is_exists() && !$webp_conversion_enabled) { |
| 200 |
$this->_htaccess->remove_commented_section($htaccess_comment_section); |
| 201 |
$this->_htaccess->write_file(); |
| 202 |
return true; |
| 203 |
} |
| 204 |
if ($this->_htaccess->is_commented_section_exists($htaccess_comment_section)) return false; |
| 205 |
$this->_htaccess->update_commented_section($this->prepare_webp_htaccess_rules(), $htaccess_comment_section); |
| 206 |
$this->_htaccess->write_file(); |
| 207 |
return true; |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Prepare array of htaccess rules to use webp images. |
| 212 |
* |
| 213 |
* @return array |
| 214 |
*/ |
| 215 |
private function prepare_webp_htaccess_rules() { |
| 216 |
return array( |
| 217 |
array( |
| 218 |
'<IfModule mod_rewrite.c>', |
| 219 |
'RewriteEngine On', |
| 220 |
'', |
| 221 |
'# Redirect to existing converted image in same dir (if browser supports webp)', |
| 222 |
'RewriteCond %{HTTP_ACCEPT} image/webp', |
| 223 |
'RewriteCond %{REQUEST_FILENAME} (?i)(.*)(\.jpe?g|\.png)$', |
| 224 |
'RewriteCond %1%2\.webp -f', |
| 225 |
'RewriteRule (?i)(.*)(\.jpe?g|\.png)$ %1%2\.webp [T=image/webp,E=EXISTING:1,E=ADDVARY:1,L]', |
| 226 |
'', |
| 227 |
'# Make sure that browsers which does not support webp also gets the Vary:Accept header', |
| 228 |
'# when requesting images that would be redirected to webp on browsers that does.', |
| 229 |
array( |
| 230 |
'<IfModule mod_headers.c>', |
| 231 |
array( |
| 232 |
'<FilesMatch "(?i)\.(jpe?g|png)$">', |
| 233 |
'Header append "Vary" "Accept"', |
| 234 |
'</FilesMatch>', |
| 235 |
), |
| 236 |
'</IfModule>', |
| 237 |
), |
| 238 |
'', |
| 239 |
'</IfModule>', |
| 240 |
'', |
| 241 |
), |
| 242 |
array( |
| 243 |
'# Rules for handling requests for webp images', |
| 244 |
'# ---------------------------------------------', |
| 245 |
'', |
| 246 |
'# Set Vary:Accept header if we came here by way of our redirect, which set the ADDVARY environment variable', |
| 247 |
'# The purpose is to make proxies and CDNs aware that the response varies with the Accept header', |
| 248 |
'<IfModule mod_headers.c>', |
| 249 |
array( |
| 250 |
'<IfModule mod_setenvif.c>', |
| 251 |
'# Apache appends "REDIRECT_" in front of the environment variables defined in mod_rewrite, but LiteSpeed does not', |
| 252 |
'# So, the next lines are for Apache, in order to set environment variables without "REDIRECT_"', |
| 253 |
'SetEnvIf REDIRECT_EXISTING 1 EXISTING=1', |
| 254 |
'SetEnvIf REDIRECT_ADDVARY 1 ADDVARY=1', |
| 255 |
'', |
| 256 |
'Header append "Vary" "Accept" env=ADDVARY', |
| 257 |
'', |
| 258 |
'# Set X-WPO-WebP header for diagnose purposes', |
| 259 |
'Header set "X-WPO-WebP" "Redirected directly to existing webp" env=EXISTING', |
| 260 |
'</IfModule>', |
| 261 |
), |
| 262 |
'</IfModule>', |
| 263 |
), |
| 264 |
); |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Prepare array of htaccess rules to use webp images. |
| 269 |
* |
| 270 |
* @return array |
| 271 |
*/ |
| 272 |
private function add_webp_mime_type() { |
| 273 |
$htaccess_comment_section = 'Register webp mime type'; |
| 274 |
if ($this->_htaccess->is_exists() && !$this->_htaccess->is_commented_section_exists($htaccess_comment_section)) { |
| 275 |
$webp_mime_type = array( |
| 276 |
array( |
| 277 |
'<IfModule mod_mime.c>', |
| 278 |
'AddType image/webp .webp', |
| 279 |
'</IfModule>', |
| 280 |
), |
| 281 |
); |
| 282 |
$this->_htaccess->update_commented_section($webp_mime_type, $htaccess_comment_section); |
| 283 |
$this->_htaccess->write_file(); |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
endif; |
| 289 |
|