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-berqCDN.php

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

103 lines 2.9 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 berqCDN {
5 public $queue = [];
6
7 function __construct() {
8 add_action('berqwp_update_cdn_urls', [$this, 'update_urls'], 10, 2);
9 }
10
11 function add_file_in_queue($file_url) {
12
13 if (empty($file_url)) {
14 return false;
15 }
16
17 // Parse the URL to extract the domain
18 $parsed_url = wp_parse_url(home_url());
19
20 // Get the domain
21 $domain = $parsed_url['host'];
22
23 $cdnUrl = 'https://boost.berqwp.com/photon/cdn/';
24 $cdnUrl .= '?url=' . urlencode($file_url);
25 $cdnUrl .= '&domain=' . $domain;
26 $cdnUrl .= '&license_key=' . get_option('berqwp_license_key');
27
28 $this->queue[] = [$file_url, $cdnUrl];
29
30 }
31
32 function finish_queue($buffer, $page_slug) {
33
34 // Generate a unique key for the queue data
35 $queue_key = 'berqwp_queue_' . md5($page_slug) . '_' . uniqid();
36
37 // Store the queue data in a transient
38 set_transient($queue_key, $this->queue, 24 * HOUR_IN_SECONDS);
39
40 // Schedule the async action with the key instead of the full queue
41 as_enqueue_async_action('berqwp_update_cdn_urls', [$queue_key, $page_slug]);
42
43 // as_enqueue_async_action('berqwp_update_cdn_urls', [$this->queue, $page_slug]);
44
45 $this->queue = [];
46
47 return $buffer;
48
49 }
50
51 function update_urls($queue_key, $page_slug) {
52 // Retrieve the queue data using the key
53 $urls = get_transient($queue_key);
54
55 if ($urls === false) {
56 // Handle error: queue data not found
57 return;
58 }
59
60 $cache_directory = optifer_cache . '/html/';
61
62 // Create the cache directory if it doesn't exist
63 if (!file_exists($cache_directory)) {
64 mkdir($cache_directory, 0755, true);
65 }
66
67 $cache_file = $cache_directory . md5($page_slug) . '.html';
68 $buffer = file_get_contents($cache_file);
69
70 $chunks = array_chunk($urls, 50);
71
72 foreach ($chunks as $chunk) {
73 $cdn_file_urls = [];
74
75 foreach ($chunk as $arr) {
76 $cdn_file_urls[] = $arr[1];
77 }
78
79 $cdn_file_responses = berqBufferOptimize::parallelCurlRequests($cdn_file_urls, [], 'GET');
80
81 for ($i = 0; $i < count($cdn_file_responses); $i++) {
82 $original_file = $chunk[$i][0];
83 $cdnData = json_decode($cdn_file_responses[$i]);
84
85 if (!empty ($cdnData->filePath)) {
86 $webp_url = $cdnData->filePath;
87 $buffer = str_replace($original_file, $webp_url, $buffer);
88 }
89 }
90 }
91
92 // update cache
93 $berqPageOptimizer = new berqPageOptimizer();
94 $berqPageOptimizer->set_slug($page_slug);
95 $berqPageOptimizer->store_cache($buffer);
96
97 delete_transient( $queue_key );
98
99 }
100 }
101
102 global $berqCDN;
103 $berqCDN = new berqCDN();