<?php
/**
 * Cache-Plugin Compatibility Layer.
 *
 * Adds the SimplyBuy cart cookie identifier to the cookie-bypass / vary list
 * of common page-cache plugins so that cart and checkout pages are never
 * served from a stale cache when the visitor has an active cart cookie.
 *
 * Supported out of the box:
 *   - WP Rocket          (filter: rocket_cache_reject_cookies)
 *   - WP Super Cache     (filter: wpsc_cache_dont_cache_with_cookie)
 *   - LiteSpeed Cache    (filter: litespeed_vary_cookies)
 *
 * Other cache layers (W3 Total Cache, WP Fastest Cache, Cloudflare page cache,
 * Varnish / Nginx FastCGI / hosting-level caches) cannot be reached via PHP
 * filters and need to be configured manually. Add the cookie name
 * `simply-buy-products` to the cache plugin's bypass / vary list.
 *
 * Site owners can disable this entire layer with:
 *   add_filter( 'sbuy_cache_compat_enabled', '__return_false' );
 *
 * The hooks are additive — they only append the SimplyBuy cookie to the
 * existing list and never overwrite or remove anything. If none of the
 * supported cache plugins is active, the filters are simply never fired
 * and this class is effectively a no-op.
 *
 * @package SimplyBuy
 * @since   1.3.3
 */

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

if ( ! class_exists( 'SBUY_Cache_Compat' ) ) {

	class SBUY_Cache_Compat {

		/**
		 * Cookie identifier used by SimplyBuy to store cart contents.
		 *
		 * The actual cookie name on disk is either `simply-buy-products`
		 * (for logged-out visitors) or `simply-buy-products-{user_id}`
		 * (for logged-in users). All three supported cache plugins do
		 * substring / prefix matching on cookie names, so registering
		 * the prefix `simply-buy-products` covers both variants.
		 *
		 * Page caches typically only serve cached HTML to logged-out
		 * visitors anyway, so the prefix is sufficient in practice.
		 */
		const COOKIE_IDENTIFIER = 'simply-buy-products';

		/**
		 * Constructor — registers the cache-bypass filters.
		 */
		public function __construct() {

			/**
			 * Master switch — allows site owners or other plugins to disable
			 * the entire cache-compat layer with a single filter.
			 *
			 * @since 1.3.3
			 *
			 * @param bool $enabled Whether the cache-compat hooks should run.
			 */
			if ( ! apply_filters( 'sbuy_cache_compat_enabled', true ) ) {
				return;
			}

			// WP Rocket — adds the cookie to the do-not-cache list.
			add_filter( 'rocket_cache_reject_cookies', array( $this, 'add_cookie_identifier' ) );

			// WP Super Cache — same purpose, different filter name.
			add_filter( 'wpsc_cache_dont_cache_with_cookie', array( $this, 'add_cookie_identifier' ) );

			// LiteSpeed Cache — adds the cookie to the vary list (cache key segmentation).
			add_filter( 'litespeed_vary_cookies', array( $this, 'add_cookie_identifier' ) );
		}

		/**
		 * Append the SimplyBuy cookie identifier to a cache plugin's cookie list.
		 *
		 * Defensive against unexpected input: if the cache plugin passes a
		 * non-array (e.g. due to a third-party filter that returns something
		 * else), the input is reset to an empty array before appending.
		 *
		 * @since 1.3.3
		 *
		 * @param mixed $cookies Existing list of cookie names from the cache plugin.
		 *                       Expected to be an array of strings.
		 *
		 * @return array Modified list with the SimplyBuy cookie identifier appended.
		 */
		public function add_cookie_identifier( $cookies ) {

			if ( ! is_array( $cookies ) ) {
				$cookies = array();
			}

			if ( ! in_array( self::COOKIE_IDENTIFIER, $cookies, true ) ) {
				$cookies[] = self::COOKIE_IDENTIFIER;
			}

			return $cookies;
		}
	}
}
