File: //wordpress/mu-plugins/edge-cache/class-edge-cache-atomic.php
<?php declare( strict_types = 1 );
/**
* Atomic platform specific integrations for shared Edge Cache code.
*/
class Edge_Cache_Atomic implements Edge_Cache_Platform {
/**
* Platform-specific code for purging a host and logging the result to logstash.
* {@inheritdoc}
*/
public function purge_domain( $domain, $tags ) {
$body = $this->prepare_purge_body( $tags );
$result = Edge_Cache_Plugin::get_instance()->query_ec_backend( 'purge', [ 'body' => $body ] );
return is_array( $result ) && ! empty( $result['success'] );
}
/**
* Platform-specific code for purging a host and logging the result to logstash.
* {@inheritdoc}
*/
public function purge_uris( $uris, $tags ) {
$body = $this->prepare_purge_body( $tags );
$body['purge_uris'] = $uris;
$result = Edge_Cache_Plugin::get_instance()->query_ec_backend( 'purge', [ 'body' => $body ] );
return is_array( $result) && ! empty( $result['success'] );
}
/**
* Platform-specific code for getting the domain name.
* {@inheritdoc}
*/
public function get_domain_name() {
static $domain_name = null;
if ( empty( $domain_name ) ) {
$site_url = get_site_url();
if ( is_string( $site_url ) ) {
$domain_name = parse_url( get_site_url(), PHP_URL_HOST );
} else {
$domain_name = '';
}
}
return $domain_name;
}
/**
* Platform-specific code for determining if a domain is able to purge. All domains on Atomic can purge.
* {@inheritdoc}
*/
public function can_purge_domain( $domain ) {
return true;
}
/**
* Return the current platform type.
* {@inheritdoc}
*/
public function get_platform_type() {
return 'atomic';
}
/**
* Prepare a post body for a purge, converting raw logging tags
* into the format expected by the Atomic API.
*/
private function prepare_purge_body( $tags ) {
$body = [
'purge_count' => (int)( $tags['purge_count'] ?? 1 ),
'wp_domain' => $this->get_domain_name(),
'at_host' => php_uname( 'n' ),
'wp_actions' => $tags['wp_actions'],
'wp_action' => $tags['wp_action'],
'batcache' => $tags['batcache'],
];
foreach ( [ 'new_status', 'old_status', 'purge_count', 'uri_limit', 'target' ] as $tag ) {
if ( ! empty( $tags[ $tag ] ) ) {
$body[ $tag ] = (string) $tags[ $tag ];
}
}
return $body;
}
}