| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('Access denied.'); |
| 4 |
|
| 5 |
if (!class_exists('WPO_Page_Optimizer')) : |
| 6 |
class WPO_Page_Optimizer { |
| 7 |
|
| 8 |
/** |
| 9 |
* Instance of this class |
| 10 |
* |
| 11 |
* @var null|WPO_Page_Optimizer |
| 12 |
*/ |
| 13 |
private static $instance = null; |
| 14 |
|
| 15 |
/** |
| 16 |
* Whether the page should be cached |
| 17 |
* |
| 18 |
* @var bool|array |
| 19 |
*/ |
| 20 |
private $should_cache_page; |
| 21 |
|
| 22 |
/** |
| 23 |
* Constructor |
| 24 |
*/ |
| 25 |
private function __construct() { |
| 26 |
// Initialize WP_Optimize_Minify_Config to make available wp_optimize_minify_config() |
| 27 |
WP_Optimize_Minify_Config::get_instance(); |
| 28 |
// Include page cache function to make available wpo_is_cacheable_sitemap_request() function |
| 29 |
$this->include_page_cache_functions(); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Get the buffer and perform tasks related to page optimization |
| 34 |
* |
| 35 |
* @param string $buffer Page HTML. |
| 36 |
* @param int $flags OB flags to be passed through. |
| 37 |
* |
| 38 |
* @return string |
| 39 |
*/ |
| 40 |
private function optimize($buffer, $flags): string { |
| 41 |
$buffer = apply_filters('wp_optimize_buffer', $buffer, $flags); |
| 42 |
|
| 43 |
$this->should_cache_page = WP_Optimize()->get_page_cache()->should_cache_page(); |
| 44 |
|
| 45 |
if (WP_Optimize_Utils::is_valid_html($buffer)) { |
| 46 |
$buffer = $this->maybe_host_google_fonts_locally($buffer); |
| 47 |
$buffer = $this->maybe_remove_unused_css($buffer); |
| 48 |
$buffer = $this->maybe_apply_capojs_rules($buffer); |
| 49 |
$buffer = $this->maybe_add_missing_image_dimensions($buffer); |
| 50 |
$buffer = $this->maybe_delay_js($buffer); |
| 51 |
$buffer = $this->maybe_alter_html_for_webp($buffer); |
| 52 |
} |
| 53 |
$buffer = $this->maybe_cache_page($buffer, $flags); |
| 54 |
return $buffer; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Cache the page if the page cache enabled |
| 59 |
* |
| 60 |
* @param string $buffer Page HTML. |
| 61 |
* @param int $flags OB flags to be passed through. |
| 62 |
* |
| 63 |
* @return string |
| 64 |
*/ |
| 65 |
private function maybe_cache_page($buffer, $flags) { |
| 66 |
|
| 67 |
if (!$this->is_wp_cli()) { |
| 68 |
$wpo_page_cache = WP_Optimize()->get_page_cache(); |
| 69 |
|
| 70 |
// If an array with the reasons why the page isn't cached is returned |
| 71 |
if (is_array($this->should_cache_page)) { |
| 72 |
do_action('wpo_page_not_cached', $this->should_cache_page); |
| 73 |
|
| 74 |
$wpo_page_cache->maybe_add_no_cache_because_info($this->should_cache_page); |
| 75 |
} |
| 76 |
|
| 77 |
if (true === $this->should_cache_page) return wpo_cache($buffer, $flags); |
| 78 |
} |
| 79 |
|
| 80 |
return $buffer; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Optimize head tags sequence to make a web page load optimally |
| 85 |
* |
| 86 |
* @param string $buffer source HTML page |
| 87 |
* @return string |
| 88 |
*/ |
| 89 |
private function maybe_apply_capojs_rules($buffer) { |
| 90 |
|
| 91 |
if (WP_Optimize::is_premium() && WP_Optimize_CapoJS_Rules::get_instance()->should_apply_capojs_rules($buffer)) { |
| 92 |
$buffer = WP_Optimize_CapoJS_Rules::get_instance()->optimize($buffer); |
| 93 |
} |
| 94 |
return $buffer; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Hosts Google Fonts locally from the provided HTML and the CSS files included in it when the option is enabled. |
| 99 |
* |
| 100 |
* @param string $buffer |
| 101 |
* @return string |
| 102 |
*/ |
| 103 |
private function maybe_host_google_fonts_locally(string $buffer): string { |
| 104 |
if (WP_Optimize::is_premium() && wp_optimize_minify_config()->get('host_local_google_fonts')) { |
| 105 |
$buffer = WP_Optimize_Host_Google_Fonts::instance()->update_gfont_urls_in_html($buffer); |
| 106 |
} |
| 107 |
|
| 108 |
return $buffer; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Remove unused css |
| 113 |
* |
| 114 |
* @param String $buffer Page HTML. |
| 115 |
* |
| 116 |
* @return String |
| 117 |
*/ |
| 118 |
private function maybe_remove_unused_css($buffer) { |
| 119 |
|
| 120 |
if (is_user_logged_in()) return $buffer; |
| 121 |
|
| 122 |
// Remove unused CSS available in Premium and only when the page cache is enabled |
| 123 |
if (WP_Optimize::is_premium() && wp_optimize_minify_config()->get('enable_unused_css') && true === $this->should_cache_page) { |
| 124 |
$unused_css_class = WP_Optimize_Minify_Unused_Css::get_instance(); |
| 125 |
return $unused_css_class->remove_unused_css($buffer); |
| 126 |
} |
| 127 |
return $buffer; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Check if we should initialise page optimizer. |
| 132 |
* |
| 133 |
* @return boolean |
| 134 |
*/ |
| 135 |
private function should_initialise() { |
| 136 |
|
| 137 |
// Skip admin, AJAX, WP-CLI, cron, static assets and page builder edit modes. |
| 138 |
if (is_admin() || $this->is_ajax() || $this->is_wp_cli() || $this->is_cron_job() || $this->is_static_asset_request() || WPO_Page_Builder_Compatibility::is_edit_mode()) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Initialise the output buffer handler. |
| 147 |
* |
| 148 |
* @return void |
| 149 |
*/ |
| 150 |
private function initialise() { |
| 151 |
ob_start(array(self::$instance, 'optimize')); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Maybe initialise page optimizer. |
| 156 |
* |
| 157 |
* @return void |
| 158 |
*/ |
| 159 |
public function maybe_initialise() { |
| 160 |
if ($this->should_initialise()) { |
| 161 |
$this->initialise(); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Checks if the current execution context is WP-CLI. |
| 167 |
* |
| 168 |
* @return bool |
| 169 |
*/ |
| 170 |
private function is_wp_cli() { |
| 171 |
return defined('WP_CLI') && WP_CLI; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Checks if the current execution context is ajax request. |
| 176 |
* |
| 177 |
* @return boolean |
| 178 |
*/ |
| 179 |
private function is_ajax() { |
| 180 |
return defined('DOING_AJAX') && DOING_AJAX; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Checks if the current execution context is a cron job request. |
| 185 |
* |
| 186 |
* @return boolean |
| 187 |
*/ |
| 188 |
private function is_cron_job() { |
| 189 |
return defined('DOING_CRON') && DOING_CRON; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Checks if the current request is for a static asset. |
| 194 |
* |
| 195 |
* @return boolean |
| 196 |
*/ |
| 197 |
private function is_static_asset_request() { |
| 198 |
if (wpo_is_cacheable_sitemap_request()) return false; |
| 199 |
|
| 200 |
// Skip non-HTML requests (like .css, .js, .map, .ico, images, fonts, .htaccess, etc.) |
| 201 |
$path = isset($_SERVER['REQUEST_URI']) ? wp_parse_url(esc_url_raw(wp_unslash($_SERVER['REQUEST_URI'])), PHP_URL_PATH) : false; |
| 202 |
if ($path) { |
| 203 |
$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION)); |
| 204 |
$skip_exts = array( |
| 205 |
'css', |
| 206 |
'js', |
| 207 |
'json', |
| 208 |
'map', |
| 209 |
'ico', |
| 210 |
'png', |
| 211 |
'jpg', |
| 212 |
'jpeg', |
| 213 |
'gif', |
| 214 |
'webp', |
| 215 |
'svg', |
| 216 |
'woff', |
| 217 |
'woff2', |
| 218 |
'ttf', |
| 219 |
'otf', |
| 220 |
'eot', |
| 221 |
'xml', |
| 222 |
'txt', |
| 223 |
'pdf', |
| 224 |
'zip', |
| 225 |
'rar', |
| 226 |
'htaccess', |
| 227 |
); |
| 228 |
|
| 229 |
if (in_array($ext, $skip_exts, true)) { |
| 230 |
return true; |
| 231 |
} |
| 232 |
} |
| 233 |
return false; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Returns a singleton instance of WPO_Page_Optimizer |
| 238 |
* |
| 239 |
* @return WPO_Page_Optimizer |
| 240 |
*/ |
| 241 |
public static function instance() { |
| 242 |
if (null === self::$instance) { |
| 243 |
self::$instance = new self(); |
| 244 |
} |
| 245 |
return self::$instance; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Add missing image dimensions if enabled |
| 250 |
* |
| 251 |
* @param string $buffer |
| 252 |
* |
| 253 |
* @return string |
| 254 |
*/ |
| 255 |
private function maybe_add_missing_image_dimensions(string $buffer): string { |
| 256 |
if (WP_Optimize::is_premium() && WP_Optimize()->get_options()->get_option('image_dimensions')) { |
| 257 |
$buffer = WP_Optimize_Image_Dimensions::instance()->add_missing_image_dimensions($buffer); |
| 258 |
} |
| 259 |
return $buffer; |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Delay JavaScript execution if enabled |
| 264 |
* |
| 265 |
* @param string $buffer |
| 266 |
* @return string |
| 267 |
*/ |
| 268 |
private function maybe_delay_js(string $buffer): string { |
| 269 |
$delay_js = WP_Optimize_Delay_JS::instance(); |
| 270 |
if ($delay_js->should_process()) { |
| 271 |
$buffer = $delay_js->process($buffer); |
| 272 |
} |
| 273 |
return $buffer; |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Alter HTML for WebP if redirection is not possible |
| 278 |
* |
| 279 |
* @param string $buffer |
| 280 |
* |
| 281 |
* @return string |
| 282 |
*/ |
| 283 |
private function maybe_alter_html_for_webp(string $buffer): string { |
| 284 |
$webp = WP_Optimize_WebP::get_instance(); |
| 285 |
if ($webp->is_webp_conversion_enabled() && $webp->get_webp_conversion_test_result()) { |
| 286 |
$buffer = $webp->maybe_decide_webp_serve_method($buffer); |
| 287 |
} |
| 288 |
return $buffer; |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Includes file-based-page-cache-functions.php |
| 293 |
* |
| 294 |
* @return void |
| 295 |
*/ |
| 296 |
private function include_page_cache_functions() { |
| 297 |
require_once WPO_PLUGIN_MAIN_PATH . '/cache/file-based-page-cache-functions.php'; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
endif; |
| 302 |
|