| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('WPO_VERSION')) die('No direct access allowed'); |
| 4 |
|
| 5 |
require_once WPO_PLUGIN_MAIN_PATH . 'includes/class-wp-optimize-htaccess.php'; |
| 6 |
|
| 7 |
if (!class_exists('WP_Optimize_WebP')) : |
| 8 |
|
| 9 |
class WP_Optimize_WebP { |
| 10 |
|
| 11 |
private $_htaccess = null; |
| 12 |
|
| 13 |
private $_server_info_instance; |
| 14 |
|
| 15 |
private $_rewrite_status = false; |
| 16 |
|
| 17 |
/** |
| 18 |
* Constructor |
| 19 |
*/ |
| 20 |
public function __construct() { |
| 21 |
$this->set_converter_status(); |
| 22 |
$this->set_server_info_instance(); |
| 23 |
$this->set_rewrite_status(); |
| 24 |
$this->set_webp_serve_method(); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Returns singleton instance |
| 29 |
* |
| 30 |
* @return WP_Optimize_WebP |
| 31 |
*/ |
| 32 |
public static function get_instance() { |
| 33 |
static $instance = null; |
| 34 |
if (null === $instance) { |
| 35 |
$instance = new WP_Optimize_WebP(); |
| 36 |
} |
| 37 |
return $instance; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Test Run and find converter status |
| 42 |
*/ |
| 43 |
public function set_converter_status() { |
| 44 |
include_once WPO_PLUGIN_MAIN_PATH . 'webp/class-wpo-webp-test-run.php'; |
| 45 |
$converters = WP_Optimize()->get_options()->get_option('webp_converters', false); |
| 46 |
|
| 47 |
if (empty($converters)) { |
| 48 |
$converter_status = WPO_WebP_Test_Run::get_converter_status(); |
| 49 |
WP_Optimize()->get_options()->update_option('webp_converters', $converter_status['working_converters']); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Sets server info instance variable |
| 55 |
*/ |
| 56 |
private function set_server_info_instance() { |
| 57 |
if (!class_exists('WPO_Server_Info')) { |
| 58 |
require_once(WPO_PLUGIN_MAIN_PATH . 'webp/class-wpo-server-info.php'); |
| 59 |
} |
| 60 |
$this->_server_info_instance = WPO_Server_Info::get_instance(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Sets server's rewrite status |
| 65 |
*/ |
| 66 |
private function set_rewrite_status() { |
| 67 |
$this->_rewrite_status = $this->_server_info_instance->get_rewrite_status(); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Sets how webp images are served |
| 72 |
*/ |
| 73 |
private function set_webp_serve_method() { |
| 74 |
if (true === $this->_rewrite_status) { |
| 75 |
$this->use_htaccess(); |
| 76 |
} else { |
| 77 |
$this->use_alter_html(); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Setup .htaccess method |
| 83 |
*/ |
| 84 |
private function use_htaccess() { |
| 85 |
$options = Updraft_Smush_Manager()->get_smush_options(); |
| 86 |
$webp_conversion_enabled = isset($options['webp_conversion']) ? $options['webp_conversion'] : false; |
| 87 |
$this->save_htaccess_rules($webp_conversion_enabled); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Setup alter html method |
| 92 |
*/ |
| 93 |
private function use_alter_html() { |
| 94 |
// To be continued in another part |
| 95 |
// add_action('template_direct', array('')); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Save .htaccess rules |
| 100 |
* |
| 101 |
* @return bool |
| 102 |
*/ |
| 103 |
public function save_htaccess_rules($webp_conversion_enabled = false) { |
| 104 |
$wp_uploads = wp_get_upload_dir(); |
| 105 |
$htaccess_file = $wp_uploads['basedir'] . '/.htaccess'; |
| 106 |
if (!file_exists($htaccess_file)) { |
| 107 |
file_put_contents($htaccess_file, ''); |
| 108 |
} |
| 109 |
$htaccess_comment_section = 'WP-Optimize WebP Rules'; |
| 110 |
$this->_htaccess = new WP_Optimize_Htaccess($htaccess_file); |
| 111 |
|
| 112 |
if ($this->_htaccess->is_exists() && !$webp_conversion_enabled) { |
| 113 |
$this->_htaccess->remove_commented_section($htaccess_comment_section); |
| 114 |
$this->_htaccess->write_file(); |
| 115 |
return true; |
| 116 |
} |
| 117 |
if ($this->_htaccess->is_commented_section_exists($htaccess_comment_section)) return false; |
| 118 |
$this->_htaccess->update_commented_section($this->prepare_webp_htaccess_rules(), $htaccess_comment_section); |
| 119 |
$this->_htaccess->write_file(); |
| 120 |
return true; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Prepare array of htaccess rules to use webp images. |
| 125 |
* |
| 126 |
* @return array |
| 127 |
*/ |
| 128 |
private function prepare_webp_htaccess_rules() { |
| 129 |
return array( |
| 130 |
array( |
| 131 |
'<IfModule mod_rewrite.c>', |
| 132 |
'RewriteEngine On', |
| 133 |
'', |
| 134 |
'# Redirect to existing converted image in same dir (if browser supports webp)', |
| 135 |
'RewriteCond %{HTTP_ACCEPT} image/webp', |
| 136 |
'RewriteCond %{REQUEST_FILENAME} (?i)(.*)(\.jpe?g|\.png)$', |
| 137 |
'RewriteCond %1%2\.webp -f', |
| 138 |
'RewriteRule (?i)(.*)(\.jpe?g|\.png)$ %1%2\.webp [T=image/webp,E=EXISTING:1,E=ADDVARY:1,L]', |
| 139 |
'', |
| 140 |
'# Make sure that browsers which does not support webp also gets the Vary:Accept header', |
| 141 |
'# when requesting images that would be redirected to webp on browsers that does.', |
| 142 |
array( |
| 143 |
'<IfModule mod_headers.c>', |
| 144 |
array( |
| 145 |
'<FilesMatch "(?i)\.(jpe?g|png)$">', |
| 146 |
'Header append "Vary" "Accept"', |
| 147 |
'</FilesMatch>', |
| 148 |
), |
| 149 |
'</IfModule>', |
| 150 |
), |
| 151 |
'', |
| 152 |
'</IfModule>', |
| 153 |
'', |
| 154 |
), |
| 155 |
array( |
| 156 |
'# Rules for handling requests for webp images', |
| 157 |
'# ---------------------------------------------', |
| 158 |
'', |
| 159 |
'# Set Vary:Accept header if we came here by way of our redirect, which set the ADDVARY environment variable', |
| 160 |
'# The purpose is to make proxies and CDNs aware that the response varies with the Accept header', |
| 161 |
'<IfModule mod_headers.c>', |
| 162 |
array( |
| 163 |
'<IfModule mod_setenvif.c>', |
| 164 |
'# Apache appends "REDIRECT_" in front of the environment variables defined in mod_rewrite, but LiteSpeed does not', |
| 165 |
'# So, the next lines are for Apache, in order to set environment variables without "REDIRECT_"', |
| 166 |
'SetEnvIf REDIRECT_EXISTING 1 EXISTING=1', |
| 167 |
'SetEnvIf REDIRECT_ADDVARY 1 ADDVARY=1', |
| 168 |
'', |
| 169 |
'Header append "Vary" "Accept" env=ADDVARY', |
| 170 |
'', |
| 171 |
'# Set X-WPO-WebP header for diagnose purposes', |
| 172 |
'Header set "X-WPO-WebP" "Redirected directly to existing webp" env=EXISTING', |
| 173 |
'</IfModule>', |
| 174 |
), |
| 175 |
'</IfModule>', |
| 176 |
), |
| 177 |
array( |
| 178 |
'# Register webp mime type', |
| 179 |
'<IfModule mod_mime.c>', |
| 180 |
'AddType image/webp .webp', |
| 181 |
'</IfModule>', |
| 182 |
), |
| 183 |
); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
endif; |
| 188 |
|