<?php

namespace GemBoards\Utils;

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

class Caching {

	/**
	 * Get prefix for use with wp_cache_set. Allows all cache in a group to be invalidated at once.
	 *
	 * @param string $group Group of cache to get.
	 *
	 * @return string Prefix.
	 */
	public static function get_cache_prefix( string $group ): string {
		$prefix = wp_cache_get( 'gemboards_' . $group . '_cache_prefix', $group );

		if ( false === $prefix ) {
			$prefix = microtime();
			wp_cache_set( 'gemboards_' . $group . '_cache_prefix', $prefix, $group );
		}

		return 'gemboards_cache_' . $prefix . '_';
	}

	/**
	 * Set constants to prevent caching by some plugins.
	 *
	 * @param mixed $return Value to return. Previously hooked into a filter.
	 *
	 * @return mixed
	 */
	public static function set_nocache_constants( $return = true ) {
		if ( ! defined( 'DONOTCACHEPAGE' ) ) {
			define( 'DONOTCACHEPAGE', true ); // Play nice with WP-Super-Cache.
		}
		if ( ! defined( 'DONOTCACHEOBJECT' ) ) {
			define( 'DONOTCACHEOBJECT', true );
		}
		if ( ! defined( 'DONOTCACHEDB' ) ) {
			define( 'DONOTCACHEDB', true );
		}

		return $return;
	}

	/**
	 * Wrapper for nocache_headers which also disables page caching.
	 */
	public static function nocache_headers() {
		self::set_nocache_constants();
		nocache_headers();
	}

}
