'error', 'msg' => 'Request authentication failed']); http_response_code(403); exit; } if ($status == 'success' && !empty($page_url) && $event == 'store_cache') { if (!bwp_can_optimize_page_url($page_url)) { echo wp_json_encode(['status' => 'error', 'msg' => 'Invalid page url.']); exit; } if (empty($html_url)) { $berq_log->info("Empty html url: $page_url"); echo wp_json_encode(['status' => 'error', 'msg' => 'Empty html url']); exit; } // Download the HTML $response = wp_remote_get($html_url, ['timeout' => 60, 'redirection' => 0]); $response_code = wp_remote_retrieve_response_code($response); // Check for errors if (is_wp_error($response) || $response_code !== 200) { $berq_log->info("Failed to download cache: $page_url"); echo wp_json_encode(['status' => 'error', 'msg' => "Could not download html cache, $html_url"]); exit; } $html = wp_remote_retrieve_body($response); // Allow other plugins to modify cache html $html = apply_filters('berqwp_cache_buffer', $html); $cache = new Cache(null, bwp_get_cache_dir()); $cache->store_cache($page_url, $html); do_action('berqwp_stored_page_cache', $page_slug); $berq_log->info("Stored cache for $page_url"); // Cache is stored which means connection is stable // Delete connection test error transient $check_connection = bwp_check_connection(); if ($check_connection['status'] == 'error') { delete_transient('berqwp_connection_status'); } echo wp_json_encode(['status' => 'success']); exit; } }