<?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);
		    $redis = new Redis();
		    $redis->connect('127.0.0.1', 6379);
		    $keys = $redis->keys(CDN_SITE_ID.'*');

                    if(!empty($keys))
                    {
                            foreach($keys as $key)
                            {
                                    $redis->del($key);
                            }
                    }
                    $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) {

                }
            }
        }

    }

}
