Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| Sitelock | |
0.00% |
0 / 38 |
|
0.00% |
0 / 3 |
110 | |
0.00% |
0 / 1 |
| should_enable | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| purge_all | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
30 | |||
| purge_url | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Performance\Cache\Types; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Performance\Cache\Purgeable; |
| 6 | use NewfoldLabs\WP\ModuleLoader\Container; |
| 7 | |
| 8 | /** |
| 9 | * Sitelock cache type. |
| 10 | */ |
| 11 | class Sitelock extends CacheBase implements Purgeable { |
| 12 | |
| 13 | /** |
| 14 | * Whether the code for this cache type should be loaded. |
| 15 | * |
| 16 | * @param Container $container The dependency injection container. |
| 17 | * |
| 18 | * @return bool True if the code should be loaded, false otherwise. |
| 19 | */ |
| 20 | public static function should_enable( Container $container ) { |
| 21 | return (bool) \get_option( 'endurance_sitelock_enabled', false ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Purge all content from the Sitelock CDN cache. |
| 26 | */ |
| 27 | public function purge_all() { |
| 28 | |
| 29 | $refresh_token = \get_option( '_mm_refresh_token' ); |
| 30 | |
| 31 | if ( false === $refresh_token ) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | $endpoint = 'https://my.bluehost.com/cgi/wpapi/cdn_purge'; |
| 36 | $domain = wp_parse_url( \home_url(), PHP_URL_HOST ); |
| 37 | $query = add_query_arg( array( 'domain' => $domain ), $endpoint ); |
| 38 | |
| 39 | $path = ABSPATH; |
| 40 | $path = explode( 'public_html/', $path ); |
| 41 | if ( 2 === count( $path ) ) { |
| 42 | $path = '/public_html/' . $path[1]; |
| 43 | } else { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | $args = array( |
| 48 | 'headers' => array( |
| 49 | 'x-api-refresh-token' => $refresh_token, |
| 50 | 'x-api-path' => bin2hex( $path ), |
| 51 | ), |
| 52 | ); |
| 53 | |
| 54 | // If WP_DEBUG is enabled, we want to wait for a response. |
| 55 | if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) { |
| 56 | $args['blocking'] = false; |
| 57 | $args['timeout'] = 0.01; |
| 58 | } |
| 59 | |
| 60 | wp_remote_get( $query, $args ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Purge a specific URL from the Sitelock CDN cache. |
| 65 | * |
| 66 | * @param string $url The URL to purge. |
| 67 | */ |
| 68 | public function purge_url( $url ) { |
| 69 | |
| 70 | $refreshToken = \get_option( '_mm_refresh_token' ); |
| 71 | |
| 72 | if ( false === $refreshToken ) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | $path = wp_parse_url( $url, PHP_URL_PATH ); |
| 77 | $pattern = rawurlencode( $path . '$' ); |
| 78 | $domain = wp_parse_url( \home_url(), PHP_URL_HOST ); |
| 79 | |
| 80 | $args = array( |
| 81 | 'method' => 'PUT', |
| 82 | 'headers' => array( |
| 83 | 'X-MOJO-TOKEN' => $refreshToken, |
| 84 | ), |
| 85 | ); |
| 86 | |
| 87 | // If WP_DEBUG is enabled, we want to wait for a response. |
| 88 | if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG ) { |
| 89 | $args['blocking'] = false; |
| 90 | $args['timeout'] = 0.01; |
| 91 | } |
| 92 | |
| 93 | wp_remote_post( "https://my.bluehost.com/api/domains/{$domain}/caches/sitelock/{$pattern}", $args ); |
| 94 | } |
| 95 | } |