<?php
namespace AIOSEO\Plugin\Pro\Main;

use AIOSEO\Plugin\Common\Main as CommonMain;

/**
 * Outputs the Google Analytics to the head. (Also GTM if it is enabled).
 *
 * @since 4.0.0
 */
class GoogleAnalytics extends CommonMain\GoogleAnalytics {
	/**
	 * Class Constructor.
	 *
	 * @since 4.0.0
	 */
	public function __construct() {
		add_action( 'init', [ $this, 'addGtm' ] );
	}

	/**
	 * Adds GTM if needed.
	 *
	 * @since 4.0.0
	 *
	 * @return void
	 */
	public function addGtm() {
		if ( $this->canShowGtm() ) {
			add_action( 'wp_enqueue_scripts', [ $this, 'enqueueGtmAssets' ] );
		}
	}

	/**
	 * Get analytics options.
	 *
	 * @since 4.0.0
	 *
	 * @return array An array of options.
	 */
	public function getOptions() {
		$options = parent::getOptions();
		if ( aioseo()->options->webmasterTools->googleAnalytics->advanced && ! $this->canShowGtm() ) {
			if ( aioseo()->options->webmasterTools->googleAnalytics->trackOutboundForms ) {
				$options['options'][] = [
					'require',
					'outboundFormTracker'
				];
			}
			if ( aioseo()->options->webmasterTools->googleAnalytics->trackEvents ) {
				$options['options'][] = [
					'require',
					'eventTracker'
				];
			}
			if ( aioseo()->options->webmasterTools->googleAnalytics->trackUrlChanges ) {
				$options['options'][] = [
					'require',
					'urlChangeTracker'
				];
			}
			if ( aioseo()->options->webmasterTools->googleAnalytics->trackVisibility ) {
				$options['options'][] = [
					'require',
					'pageVisibilityTracker'
				];
			}
			if ( aioseo()->options->webmasterTools->googleAnalytics->trackMediaQueries ) {
				$options['options'][] = [
					'require',
					'mediaQueryTracker'
				];
			}
			if ( aioseo()->options->webmasterTools->googleAnalytics->trackImpressions ) {
				$options['options'][] = [
					'require',
					'impressionTracker'
				];
			}
			if ( aioseo()->options->webmasterTools->googleAnalytics->trackScrollbar ) {
				$options['options'][] = [
					'require',
					'maxScrollTracker'
				];
			}
			if ( aioseo()->options->webmasterTools->googleAnalytics->trackSocial ) {
				$options['options'][] = [
					'require',
					'socialWidgetTracker'
				];
			}
			if ( aioseo()->options->webmasterTools->googleAnalytics->trackCleanUrl ) {
				$options['options'][] = [
					'require',
					'cleanUrlTracker'
				];
			}
		}

		if ( $this->canShowGtm() ) {
			$optionsToRemove = [
				'ec',
				'outboundLinkTracker'
			];

			foreach ( $optionsToRemove as $option ) {
				foreach ( $options['options'] as $key => $opt ) {
					$index = array_search( $option, $opt, true );
					if ( false !== $index ) {
						unset( $options['options'][ $key ] );
						continue 2;
					}
				}
			}
		}

		return $options;
	}

	/**
	 * Check if autotrack JS should be included.
	 *
	 * @since 4.0.0
	 *
	 * @return boolean True if so, false if not.
	 */
	public function autoTrack() {
		if ( ! aioseo()->options->webmasterTools->googleAnalytics->advanced ) {
			return false;
		}

		if (
			aioseo()->options->webmasterTools->googleAnalytics->trackOutboundLinks ||
			aioseo()->options->webmasterTools->googleAnalytics->trackOutboundForms ||
			aioseo()->options->webmasterTools->googleAnalytics->trackEvents ||
			aioseo()->options->webmasterTools->googleAnalytics->trackUrlChanges ||
			aioseo()->options->webmasterTools->googleAnalytics->trackVisibility ||
			aioseo()->options->webmasterTools->googleAnalytics->trackMediaQueries ||
			aioseo()->options->webmasterTools->googleAnalytics->trackImpressions ||
			aioseo()->options->webmasterTools->googleAnalytics->trackScrollbar ||
			aioseo()->options->webmasterTools->googleAnalytics->trackSocial ||
			aioseo()->options->webmasterTools->googleAnalytics->trackCleanUrl
		) {
			return true;
		}
		return false;
	}

	/**
	 * Checks if we can show GTM on the site.
	 *
	 * @since 4.0.0
	 *
	 * @return bool Whether or not we can show GTM.
	 */
	public function canShowGtm() {
		$containerId = aioseo()->options->webmasterTools->googleAnalytics->gtmContainerId;

		$disable = apply_filters( 'aioseo_disable_google_tag_manager', false );

		if (
			$disable ||
			is_admin() ||
			empty( $containerId ) ||
			! preg_match( '/GTM-.{6}/', $containerId )
		) {
			return false;
		}

		return true;
	}

	/**
	 * Enqueues the GTM assets when needed.
	 *
	 * @since 4.0.0
	 *
	 * @return void
	 */
	public function enqueueGtmAssets() {
		aioseo()->helpers->enqueueScript(
			'aioseo-gtm',
			'js/aioseo-gtm.js',
			false
		);

		wp_localize_script(
			'aioseo-gtm',
			'aioseoGtm',
			[
				'containerId' => aioseo()->options->webmasterTools->googleAnalytics->gtmContainerId
			]
		);
	}
}