PluginProbe
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript / 4.0.29
BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript v4.0.29
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 / api / store_cache_webhook.php

store_cache_webhook.php in BerqWP – All-In-One Optimization for Core Web Vitals, Cache, CDN, Images, CSS & JavaScript 4.0.29, at api/store_cache_webhook.php

97 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use BerqWP\Cache;
4
5 if (!defined('ABSPATH')) exit;
6
7 if ($_SERVER['REQUEST_METHOD'] === 'POST' && !empty($_GET['berqwp_webhook']) && $_GET['berqwp_webhook'] == 'store_cache') {
8 exit;
9
10 // // load WordPress
11 // require_once '../../../../wp-load.php';
12
13 // // Initialize BerqWP SDK
14 // require_once '../BerqWP/vendor/autoload.php';
15
16 if (defined('DOING_CRON') && DOING_CRON) {
17 return;
18 }
19
20 if (defined('DOING_AJAX') && DOING_AJAX) {
21 return;
22 }
23
24 if (defined('REST_REQUEST') && REST_REQUEST) {
25 return;
26 }
27
28 $jsonData = file_get_contents('php://input');
29 $data = json_decode($jsonData, true);
30
31 if ($data == null) {
32 return;
33 }
34
35 $status = sanitize_text_field($data['status']);
36 $html_url = sanitize_text_field($data['html_url']);
37 // $html = base64_decode($data['html']);
38 $page_slug = strtolower($data['page_slug']);
39 $license_key_hash = sanitize_text_field($data['license_key_hash']);
40 $page_url = strtolower($data['page_url']);
41 $license_key = berqwp_get_license_key();
42 global $berq_log;
43
44 if ($status == 'success' && !empty($html_url)) {
45
46 if (!bwp_can_optimize_page_url($page_url)) {
47 echo wp_json_encode(['status' => 'error', 'msg' => 'Invalid page url.']);
48 exit;
49 }
50
51 // Download the HTML
52 $response = wp_remote_get($html_url, ['timeout' => 60, 'redirection' => 0]);
53 $response_code = wp_remote_retrieve_response_code($response);
54
55 // Check for errors
56 if (is_wp_error($response) || $response_code !== 200) {
57 $berq_log->info("Failed to download cache: $page_url");
58 echo wp_json_encode(['status' => 'error', 'msg' => 'Could not download html cache']);
59 exit;
60 }
61
62 $html = wp_remote_retrieve_body($response);
63
64 // Allow other plugins to modify cache html
65 $html = apply_filters('berqwp_cache_buffer', $html);
66
67 if (empty($license_key_hash) || empty($license_key) || $license_key_hash !== md5($license_key)) {
68 echo wp_json_encode(['status' => 'error', 'msg' => 'Request authentication failed']);
69 http_response_code(403);
70 exit;
71 }
72
73 if (empty($html)) {
74 $berq_log->info("Empty html cache: $page_url");
75 echo wp_json_encode(['status' => 'error', 'msg' => 'Empty html cache']);
76 exit;
77 }
78
79 $cache = new Cache(null, bwp_get_cache_dir());
80 $cache->store_cache($page_url, $html);
81
82 do_action('berqwp_stored_page_cache', $page_slug);
83
84 $berq_log->info("Stored cache for $page_url");
85
86 // Cache is stored which means connection is stable
87 // Delete connection test error transient
88 $check_connection = bwp_check_connection();
89 if ($check_connection['status'] == 'error') {
90 delete_transient('berqwp_connection_status');
91 }
92
93 echo wp_json_encode(['status' => 'success']);
94 exit;
95 }
96 }
97