Current Path : /storage/v11800/feel-restore/public_html/mu-plugins/cdn-cache-management/includes/ |
Linux v11800 5.3.0-1023-aws #25~18.04.1-Ubuntu SMP Fri Jun 5 15:19:18 UTC 2020 aarch64
|
Current File : /storage/v11800/feel-restore/public_html/mu-plugins/cdn-cache-management/includes/cdn-clear-cache-api.php |
<?php
/**
* CDN Clear Cache Api
*/
if (!class_exists('CDN_Clear_Cache_Api')) {
class CDN_Clear_Cache_Api {
/**
* clear site cache cdn api request
*
* @param array $files
* @param string $action
* @return array
*/
public static function cache_api_call($files, $action = 'purge') {
$data = [];
if ($action) {
$curl_url = 'https://my.nestify.io/cdn/purge/' . CDN_SITE_ID . '/' . $action;
$data['url'] = $_SERVER['HTTP_HOST'];
// set clear cache files
if (!empty($files))
{
$data['files'] = $files;
}
else
{
$data['files'] = array();
$pages = get_pages(array('number' => 30,'order' => 'DESC'));
foreach($pages as $page)
{
$data['files'][] = get_permalink($page->ID);
}
$posts = get_posts(array('number' => 30,'order' => 'DESC'));
foreach($posts as $post)
{
$data['posts'][] = get_permalink($post->ID);
}
}
// prepare request data
$data = json_encode($data);
if ($action == "purge_everything") {
$curl_url = 'https://my.nestify.io/cdn/purge/' . CDN_SITE_ID . '/purge';
//$data = "{\"tags\": [\"html\"]}";
}
// init curl request
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $curl_url,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Accept: application/json',
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true,
// Timeout the request after 5 seconds
CURLOPT_TIMEOUT => 5,
]);
try {
$result = curl_exec($ch);
curl_close($ch);
// // log request and response
// $message = 'Request: ' . $data . ' Response ' . $result;
// self::cdn_cache_log($message);
// return result
$result = json_decode($result);
return $result;
} catch (\Exception $e) {
// log request and exception
$message =' Exception: ' . $e->getMessage();
self::cdn_cache_log($message);
return [];
}
}
return $data;
}
/**
* wp cdn clear cache custom log
*
* @param string $message
*/
public static function cdn_cache_log($message) {
if (WP_DEBUG_LOG) {
try {
error_log($message);
} catch (\Exception $e) {
}
}
}
}
}