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