<?php
/**
 * TrackingCodes.
 *
 * @package MadeByAura\Prime\Hooks
 * @author  MadeByAura.com
 */

namespace MadeByAura\Prime\Hooks;

use MadeByAura\Prime\Base;
use MadeByAura\Prime\Helpers\SingletonTrait;

defined( 'ABSPATH' ) || die();

/**
 * TrackingCodes.
 */
class TrackingCodes {
	use SingletonTrait;

	/**
	 * Constructor.
	 */
	protected function __construct() {
		add_action( 'wp_head', [ __CLASS__, 'add_code_before_closing_head_tag' ], 999 );
		add_action( 'wp_footer', [ __CLASS__, 'add_code_before_closing_body_tag' ], 999 );
	}

	/**
	 * Add code in the <head> tag.
	 */
	public static function add_code_before_closing_head_tag() {
		// Do not proceed if the current user is an administrator.
		if ( current_user_can( 'manage_options' ) ) {
			return;
		}

		$code = Base::get_acf( 'option', 'tracking_code_before_closing_head_tag' );

		// Do not proceed if the there is no code.
		if ( ! $code ) {
			return;
		}

		echo $code; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	}

	/**
	 * Add code in the in the footer above the closing <body> tag.
	 */
	public static function add_code_before_closing_body_tag() {
		// Do not proceed if the current user is an administrator.
		if ( current_user_can( 'manage_options' ) ) {
			return;
		}

		$code = Base::get_acf( 'option', 'tracking_code_before_closing_body_tag' );

		// Do not proceed if the there is no code.
		if ( ! $code ) {
			return;
		}

		echo $code; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	}
}
