PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 1.9.3
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v1.9.3
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.3, at inc/photon/class-berqCriticalCSS.php

175 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 require_once optifer_PATH . '/simplehtmldom/simple_html_dom.php';
130
131 $cache_directory = optifer_cache . '/html/';
132
133 // Create the cache directory if it doesn't exist
134 if (!file_exists($cache_directory)) {
135 mkdir($cache_directory, 0755, true);
136 }
137
138 $cache_file = $cache_directory . md5($page_slug) . '.html';
139 $css_file = $this->get_file_path($page_slug);
140
141 if (file_exists($cache_file) && file_exists($css_file)) {
142 $buffer = file_get_contents($cache_file);
143
144 // Load the HTML buffer into Simple HTML DOM
145 $html = str_get_html($buffer);
146
147 // Check if there's a <style> tag with id="berqwp-critical-css"
148 $style_tag = $html->find('style#berqwp-critical-css', 0);
149
150 if ($style_tag === null) {
151 $criticalcss = file_get_contents($css_file);
152 $buffer = $this->add_css_to_buffer($buffer, $criticalcss);
153
154 // update cache
155 $berqPageOptimizer = new berqPageOptimizer();
156 $berqBufferOptimize = new berqBufferOptimize();
157
158 $buffer = $berqBufferOptimize->css_optimize($buffer, true, false);
159 $berqPageOptimizer->set_slug($page_slug);
160
161 $beforeBodyClose = apply_filters( 'berqwp_buffer_before_closing_body', '' );
162 $buffer = str_replace('</body>', $beforeBodyClose . '</body>', $buffer);
163
164 $berqPageOptimizer->store_cache($buffer);
165 }
166
167 unset($html);
168 }
169
170
171 }
172
173 }
174
175 // new berqCriticalCSS();