<?php
/**
 * Compatibility with UK Cookie Consent.
 *
 * @since  3.2
 * @author TheZoker
 * @link   https://wordpress.org/plugins/uk-cookie-consent/
 */

defined( 'ABSPATH' ) || exit;

if ( class_exists( 'CTCC_Public' ) ) {
	add_filter( 'science_htaccess_mod_rewrite', '__return_false', 62 );
	// Create cache version based on value set in cookie_consent_accepted cookie.
	add_filter( 'science_cache_dynamic_cookies', 'science_get_cookie_uk_consent_cookie' );
}

/**
 * Return the cookie name set by UK Cookie Consent plugin.
 *
 * @since  3.2
 * @author TheZoker
 *
 * @param  array $cookies List of dynamic cookies.
 * @return array          List of dynamic cookies with the UK Cookie Consent cookie appended.
 */
function science_get_cookie_uk_consent_cookie( $cookies ) {
	$cookies[] = 'catAccCookies';
	return $cookies;
}

/**
 * Add dynamic cookie and mandatory cookie when UK Cookie Consent plugin is activated.
 *
 * @since  3.2
 * @author TheZoker
 */
function science_add_uk_cookie_consent_dynamic_cookie() {
	add_filter( 'science_htaccess_mod_rewrite', '__return_false', 62 );
	// Create cache version based on value set in cookie_consent_accepted cookie.
	add_filter( 'science_cache_dynamic_cookies', 'science_get_cookie_uk_consent_cookie' );

	// Update the WP science rules on the .htaccess file.
	flush_science_htaccess();

	// Regenerate the config file.
	science_generate_config_file();

	// Clear WP science cache.
	science_clean_domain();
}
add_action( 'activate_uk-cookie-consent/uk-cookie-consent.php', 'science_add_uk_cookie_consent_dynamic_cookie', 11 );

/**
 * Remove dynamic cookie when Cookie Consent plugin is deactivated.
 *
 * @since  3.2
 * @author TheZoker
 */
function science_remove_uk_cookie_consent_dynamic_cookie() {
	remove_filter( 'science_htaccess_mod_rewrite', '__return_false', 62 );
	remove_filter( 'science_cache_dynamic_cookies', 'science_get_cookie_uk_consent_cookie' );

	// Update the WP science rules on the .htaccess file.
	flush_science_htaccess();

	// Regenerate the config file.
	science_generate_config_file();

	// Clear WP science cache.
	science_clean_domain();
}
add_action( 'deactivate_uk-cookie-consent/uk-cookie-consent.php', 'science_remove_uk_cookie_consent_dynamic_cookie', 11 );
