PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 1.9.7
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v1.9.7
4.1.16 4.1.15 4.1.14 4.1.13 4.1.12 4.1.11 4.1.10 4.0.30 4.0.29 4.0.28 4.0.27 4.0.26 4.0.24 4.0.25 4.0.23 4.0.22 4.0.21 4.0.19 4.0.18 4.0.17 4.0.16 1.9.3 1.9.4 1.9.5 1.9.6 All 170 releases
searchpro / inc / photon / class-berqCriticalCSS.php

class-berqCriticalCSS.php in BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript 1.9.7, at inc/photon/class-berqCriticalCSS.php

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