<?php

namespace Clonable\Services;

defined( 'ABSPATH' ) or die();

/**
 * Add caching support for caching plugins.
 *
 * Currently supported plugins:
 * - WP Super Cache
 */
class CacheService
{
    public function __construct() {
        add_filter('supercache_filename_str', [$this, 'wp_super_cache']);
        add_action('wp_headers', [$this, 'add_vary_header']);
    }

    public function add_vary_header() {
        header('Vary: Clonable-Clone-Domain, Clonable-Clone-Subfolder', false);
    }

    public function wp_super_cache($cache_key) {
        if(isset($_SERVER['HTTP_CLONABLE_CLONE_SUBFOLDER'])) {
            $normalized = mb_strtolower(str_replace('/', '', $_SERVER['HTTP_CLONABLE_CLONE_SUBFOLDER']));
            $cache_key = $cache_key . '-' . $normalized;
        }
        return $cache_key;
    }
}