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