<?php
namespace FluentC\Compat;

if (!defined('ABSPATH')) {
    exit;
}

class WP_Rocket_Compatibility {

    public function __construct() {
        add_action('fluentc_after_translation', [$this, 'clear_wp_rocket_cache_per_language']);
    }

    public function clear_wp_rocket_cache_per_language() {
        if (!function_exists('rocket_clean_home') || !function_exists('pll_get_languages')) {
            return;
        }

        $languages = pll_get_languages();
        if (!$languages || !is_array($languages)) {
            return;
        }

        foreach ($languages as $lang_code => $lang_data) {
            rocket_clean_home($lang_code);
        }
    }

    public static function maybe_exclude_homepage() {
        // Developers can call this manually or tie into settings.
        if (function_exists('get_rocket_option')) {
            $excluded_urls = get_rocket_option('never_cache_url', []);
            if (!in_array('/', $excluded_urls)) {
                $excluded_urls[] = '/';
                update_option('wp_rocket_settings', array_merge(get_option('wp_rocket_settings', []), [
                    'never_cache_url' => $excluded_urls
                ]));
            }
        }
    }
}
