| 1 |
<?php |
| 2 |
|
| 3 |
namespace Hostinger\Cdn; |
| 4 |
|
| 5 |
use Hostinger\WpHelper\Utils; |
| 6 |
use WP_Http; |
| 7 |
use WP_Error; |
| 8 |
|
| 9 |
defined( 'ABSPATH' ) || exit; |
| 10 |
|
| 11 |
class CachePurger { |
| 12 |
private const INSTALLATIONS_BASE = '/api/v1/installations/'; |
| 13 |
private const ALL_CACHE_PATH = '/cache'; |
| 14 |
private const CDN_CACHE_PATH = '/cache/cdn'; |
| 15 |
|
| 16 |
private CacheClient $client; |
| 17 |
private SoftwareIdResolver $resolver; |
| 18 |
private Utils $utils; |
| 19 |
|
| 20 |
public function __construct( CacheClient $client, SoftwareIdResolver $resolver, Utils $utils ) { |
| 21 |
$this->client = $client; |
| 22 |
$this->resolver = $resolver; |
| 23 |
$this->utils = $utils; |
| 24 |
} |
| 25 |
|
| 26 |
public function purge_all(): array|WP_Error { |
| 27 |
return $this->purge( self::ALL_CACHE_PATH ); |
| 28 |
} |
| 29 |
|
| 30 |
public function purge_cdn(): array|WP_Error { |
| 31 |
return $this->purge( self::CDN_CACHE_PATH ); |
| 32 |
} |
| 33 |
|
| 34 |
private function purge( string $path ): array|WP_Error { |
| 35 |
$domain = $this->resolver->site_domain(); |
| 36 |
|
| 37 |
if ( ! $this->is_eligible( $domain ) ) { |
| 38 |
return array( |
| 39 |
'purged' => false, |
| 40 |
'skipped' => true, |
| 41 |
'message' => __( 'Cache clearing is not available for this site.', 'hostinger' ), |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
$software_id = $this->resolver->resolve(); |
| 46 |
|
| 47 |
if ( $software_id === null ) { |
| 48 |
return new WP_Error( |
| 49 |
'hostinger_cdn_no_software_id', |
| 50 |
__( 'Could not determine the Hostinger installation for this site.', 'hostinger' ) |
| 51 |
); |
| 52 |
} |
| 53 |
|
| 54 |
$endpoint = self::INSTALLATIONS_BASE . rawurlencode( $software_id ) . $path; |
| 55 |
$response = $this->client->delete( $endpoint, $domain ); |
| 56 |
|
| 57 |
if ( is_wp_error( $response ) ) { |
| 58 |
$this->log_failure( $endpoint, $response->get_error_message() ); |
| 59 |
|
| 60 |
return $this->purge_failed(); |
| 61 |
} |
| 62 |
|
| 63 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 64 |
|
| 65 |
if ( WP_Http::NOT_FOUND === $response_code ) { |
| 66 |
$retry = $this->retry_with_fresh_software_id( $software_id, $path, $domain ); |
| 67 |
|
| 68 |
if ( $retry !== null ) { |
| 69 |
$response = $retry; |
| 70 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
if ( $response_code >= 300 ) { |
| 75 |
$this->log_failure( $endpoint, $response_code . ' ' . sanitize_text_field( wp_remote_retrieve_response_message( $response ) ) ); |
| 76 |
|
| 77 |
return $this->purge_failed(); |
| 78 |
} |
| 79 |
|
| 80 |
return array( |
| 81 |
'purged' => true, |
| 82 |
'skipped' => false, |
| 83 |
'message' => __( 'Cache has been cleared', 'hostinger' ), |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
private function is_eligible( ?string $domain ): bool { |
| 88 |
if ( empty( $_SERVER['H_PLATFORM'] ) ) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
return $domain !== null; |
| 93 |
} |
| 94 |
|
| 95 |
private function retry_with_fresh_software_id( string $stale_id, string $path, string $domain ): array|null { |
| 96 |
$this->log_failure( self::INSTALLATIONS_BASE . $stale_id . $path, '404 - cached software_id looks stale, re-resolving' ); |
| 97 |
|
| 98 |
$this->resolver->forget(); |
| 99 |
|
| 100 |
$fresh_id = $this->resolver->resolve(); |
| 101 |
|
| 102 |
if ( $fresh_id === null || $fresh_id === $stale_id ) { |
| 103 |
return null; |
| 104 |
} |
| 105 |
|
| 106 |
$response = $this->client->delete( |
| 107 |
self::INSTALLATIONS_BASE . rawurlencode( $fresh_id ) . $path, |
| 108 |
$domain |
| 109 |
); |
| 110 |
|
| 111 |
return is_wp_error( $response ) ? null : $response; |
| 112 |
} |
| 113 |
|
| 114 |
private function purge_failed(): WP_Error { |
| 115 |
return new WP_Error( |
| 116 |
'hostinger_cdn_purge_failed', |
| 117 |
__( 'We could not clear the cache. Please try again later.', 'hostinger' ) |
| 118 |
); |
| 119 |
} |
| 120 |
|
| 121 |
private function log_failure( string $endpoint, string $reason ): void { |
| 122 |
$this->utils->errorLog( 'Hostinger Tools hCDN purge failed: DELETE ' . $endpoint . ' -- ' . $reason ); |
| 123 |
} |
| 124 |
} |
| 125 |
|