|
4 | 4 |
|
5 | 5 | function purge_cache_for_url(string $url) |
6 | 6 | { |
7 | | - $url = urlencode($url); |
8 | | - $curl = curl_init(); |
9 | | - curl_setopt_array($curl, [ |
10 | | - CURLOPT_URL => "https://api.bunny.net/purge?url=$url", |
11 | | - CURLOPT_RETURNTRANSFER => true, |
12 | | - CURLOPT_POST => true, |
13 | | - CURLOPT_MAXREDIRS => 5, |
14 | | - CURLOPT_TIMEOUT => 10, |
15 | | - CURLOPT_HTTPHEADER => [ |
16 | | - "AccessKey: " . constant('BUNNY_API_KEY'), |
| 7 | + $request_url = 'https://api.bunny.net/purge?url=' . urlencode($url); |
| 8 | + |
| 9 | + $response = wp_remote_post($request_url, [ |
| 10 | + 'timeout' => 10, |
| 11 | + 'redirection' => 5, |
| 12 | + 'headers' => [ |
| 13 | + 'AccessKey' => constant('BUNNY_API_KEY'), |
17 | 14 | ], |
18 | 15 | ]); |
19 | 16 |
|
20 | | - curl_exec($curl); |
21 | | - $error = curl_error($curl); |
22 | | - if ($error) { |
23 | | - error_log("Error purging Bunny CDN cache: $error"); |
| 17 | + if (is_wp_error($response)) { |
| 18 | + error_log('Error purging Bunny CDN cache: ' . $response->get_error_message()); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + $status_code = wp_remote_retrieve_response_code($response); |
| 23 | + if ($status_code < 200 || $status_code >= 300) { |
| 24 | + error_log('Error purging Bunny CDN cache: HTTP ' . $status_code); |
24 | 25 | } |
25 | 26 | } |
26 | 27 |
|
27 | 28 | add_action('save_post', function ($post_id) { |
| 29 | + // No-op if BUNNY_API_KEY constant is not set |
| 30 | + if (! defined('BUNNY_API_KEY')) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
28 | 34 | // Check if it's not an autosave |
29 | | - if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
| 35 | + if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || wp_is_post_autosave($post_id) || wp_is_post_revision($post_id)) { |
30 | 36 | return; |
31 | 37 | } |
32 | 38 |
|
33 | | - // No-op if BUNNY_API_KEY constant is not set |
34 | | - if (! defined('BUNNY_API_KEY')) { |
| 39 | + |
| 40 | + $post_type = get_post_type($post_id); |
| 41 | + $post_type_object = $post_type ? get_post_type_object($post_type) : null; |
| 42 | + if (! $post_type_object || ! $post_type_object->public) { |
35 | 43 | return; |
36 | 44 | } |
37 | 45 |
|
|
0 commit comments