| 1 |
<?php |
| 2 |
|
| 3 |
class berqCriticalCSS { |
| 4 |
function __construct() { |
| 5 |
add_action('berqwp_get_critical_css', [$this, 'make_request'], 10, 1); |
| 6 |
add_action('berqwp_flush_page_cache', [$this, 'delete_css_file']); |
| 7 |
add_action('berqwp_flush_all_cache', [$this, 'delete_all_css_files']); |
| 8 |
} |
| 9 |
|
| 10 |
function get_css($page_slug) { |
| 11 |
|
| 12 |
if ($this->can_make_request($page_slug)) { |
| 13 |
as_enqueue_async_action('berqwp_get_critical_css', [$page_slug]); |
| 14 |
|
| 15 |
return false; |
| 16 |
} |
| 17 |
|
| 18 |
$css_file = $this->get_file_path($page_slug); |
| 19 |
return file_get_contents($css_file); |
| 20 |
|
| 21 |
} |
| 22 |
|
| 23 |
function make_request($page_slug, $page_url = null) { |
| 24 |
|
| 25 |
if (!empty($page_url)) { |
| 26 |
$page_url = $page_url . "?generating_critical_css"; |
| 27 |
|
| 28 |
} else { |
| 29 |
$page_url = home_url() . $page_slug . "?generating_critical_css"; |
| 30 |
|
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
// $page_url = "https://wordpress-1288044-4671067.cloudwaysapps.com/?generating_critical_css"; |
| 35 |
$url = 'https://api-critical.hamzamairaj.dev/'; |
| 36 |
|
| 37 |
$params = [ |
| 38 |
'url' => rawurlencode($page_url), |
| 39 |
'w' => '1440', |
| 40 |
'h' => '100000', |
| 41 |
]; |
| 42 |
|
| 43 |
$api_url_with_params = add_query_arg($params, $url); |
| 44 |
$response = wp_remote_get($api_url_with_params, array('timeout' => 100, 'sslverify' => true)); |
| 45 |
|
| 46 |
// Check for errors |
| 47 |
if (is_wp_error($response) && wp_remote_retrieve_response_code($response) !== 200) { |
| 48 |
$error_message = $response->get_error_message(); |
| 49 |
return false; |
| 50 |
} else { |
| 51 |
// Retrieve the response body |
| 52 |
$response_body = wp_remote_retrieve_body($response); |
| 53 |
|
| 54 |
if (isset (json_decode($response_body, true)['status']) && json_decode($response_body, true)['status'] == 'error') { |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
if (!empty (trim($response_body)) && !str_contains($response_body, '</title>')) { |
| 60 |
$this->store_css($page_slug, $response_body); |
| 61 |
return $response_body; |
| 62 |
} else { |
| 63 |
as_enqueue_async_action('berqwp_get_critical_css', [$page_slug]); |
| 64 |
} |
| 65 |
|
| 66 |
return false; |
| 67 |
|
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
function delete_css_file($page_slug) { |
| 72 |
$file_path = $this->get_file_path($page_slug); |
| 73 |
if (file_exists($file_path)) { |
| 74 |
unlink($file_path); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
function delete_all_css_files() { |
| 79 |
$css_directory = optifer_cache . '/critical-css/'; |
| 80 |
berqwp_unlink_recursive($css_directory); |
| 81 |
} |
| 82 |
|
| 83 |
function store_css($page_slug, $critical_css) { |
| 84 |
$css_file = $this->get_file_path($page_slug); |
| 85 |
file_put_contents($css_file, $critical_css); |
| 86 |
|
| 87 |
$this->update_cache($page_slug); |
| 88 |
} |
| 89 |
|
| 90 |
function get_file_path($page_slug) { |
| 91 |
$css_directory = optifer_cache . '/critical-css/'; |
| 92 |
|
| 93 |
if (!file_exists($css_directory)) { |
| 94 |
mkdir($css_directory, 0755, true); |
| 95 |
} |
| 96 |
|
| 97 |
$css_file = $css_directory . md5($page_slug) . '.css'; |
| 98 |
|
| 99 |
return $css_file; |
| 100 |
} |
| 101 |
|
| 102 |
function can_make_request($page_slug) { |
| 103 |
$file_path = $this->get_file_path($page_slug); |
| 104 |
|
| 105 |
if (!file_exists($file_path)) { |
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
$file_mod_time = filemtime($file_path); |
| 110 |
$current_time = time(); |
| 111 |
$file_age = $current_time - $file_mod_time; |
| 112 |
$seven_days_in_seconds = 7 * 24 * 60 * 60; |
| 113 |
|
| 114 |
// Check if the file is older than 7 days |
| 115 |
if ($file_age > $seven_days_in_seconds) { |
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
function add_css_to_buffer($buffer, $css) { |
| 123 |
$criticalcss = sprintf('<style data-berqwp id="berqwp-critical-css">%s</style>', $css); |
| 124 |
$buffer = preg_replace('/<head(\s[^>]*)?>/i', '<head$1>' . $criticalcss, $buffer); |
| 125 |
return $buffer; |
| 126 |
} |
| 127 |
|
| 128 |
function update_cache($page_slug) { |
| 129 |
if (!function_exists('str_get_html')) { |
| 130 |
require_once optifer_PATH . '/simplehtmldom/simple_html_dom.php'; |
| 131 |
} |
| 132 |
|
| 133 |
$cache_directory = optifer_cache . '/html/'; |
| 134 |
|
| 135 |
// Create the cache directory if it doesn't exist |
| 136 |
if (!file_exists($cache_directory)) { |
| 137 |
mkdir($cache_directory, 0755, true); |
| 138 |
} |
| 139 |
|
| 140 |
$cache_file = $cache_directory . md5($page_slug) . '.html'; |
| 141 |
$css_file = $this->get_file_path($page_slug); |
| 142 |
|
| 143 |
if (file_exists($cache_file) && file_exists($css_file)) { |
| 144 |
$buffer = file_get_contents($cache_file); |
| 145 |
|
| 146 |
// Load the HTML buffer into Simple HTML DOM |
| 147 |
$html = str_get_html($buffer); |
| 148 |
|
| 149 |
// Check if there's a <style> tag with id="berqwp-critical-css" |
| 150 |
$style_tag = $html->find('style#berqwp-critical-css', 0); |
| 151 |
|
| 152 |
if ($style_tag === null) { |
| 153 |
$criticalcss = file_get_contents($css_file); |
| 154 |
$buffer = $this->add_css_to_buffer($buffer, $criticalcss); |
| 155 |
|
| 156 |
// update cache |
| 157 |
$berqPageOptimizer = new berqPageOptimizer(); |
| 158 |
$berqBufferOptimize = new berqBufferOptimize(); |
| 159 |
|
| 160 |
$buffer = $berqBufferOptimize->css_optimize($buffer, true, false); |
| 161 |
$berqPageOptimizer->set_slug($page_slug); |
| 162 |
|
| 163 |
$beforeBodyClose = apply_filters( 'berqwp_buffer_before_closing_body', '' ); |
| 164 |
$buffer = str_replace('</body>', $beforeBodyClose . '</body>', $buffer); |
| 165 |
|
| 166 |
$berqPageOptimizer->store_cache($buffer); |
| 167 |
} |
| 168 |
|
| 169 |
unset($html); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
// new berqCriticalCSS(); |