<?php
/**
 * Visitor cookie template file.
 * Brief description of the styles in this file
 *
 * @since   3.0.8
 * @package AdvancedAds\Pro
 * @author  Advanced Ads <info@wpadvancedads.com>
 */

namespace AdvancedAds\Pro;

use AdvancedAds\Framework\Utilities\Str;
use AdvancedAds\Framework\Utilities\Params;
use AdvancedAds\Framework\Interfaces\Integration_Interface;

defined( 'ABSPATH' ) || exit;

/**
 * Visitor_Cookie class.
 */
class Visitor_Cookie implements Integration_Interface {

	/**
	 * Hook into WordPress.
	 *
	 * @return void
	 */
	public function hooks(): void {
		$this->maybe_set_visitor_cookie();
	}

	/**
	 * Set visitor cookie to true (allowed to be defined by JS) if explicitly allowed using filter or Cookiebot statistics consent
	 *
	 * @return void
	 */
	public function maybe_set_visitor_cookie(): void {
		if ( wp_doing_cron() ) {
			return;
		}

		$cookie_consent     = Params::cookie( 'CookieConsent' ) ?? '';
		$statistics_consent = Str::contains( 'statistics:true', $cookie_consent );

		$is_consent = $statistics_consent || apply_filters( 'advanced_ads_allow_visitor_cookie', false );

		if( $is_consent ) {
			wp_advads_json_add( 'cookieConsent', '1' );
		}
	}
}
