<?php
/**
 * Tracking Exclusions
 *
 * @link       https://personalizewp.com/
 * @since      3.2.0 Merged into PWP
 * @since      2.1.0
 *
 * @package    PersonalizeWP
 * @subpackage PersonalizeWP/Tracking
 */

namespace PersonalizeWP\Tracking;

defined( 'ABSPATH' ) || exit; // Exit if accessed directly.

/**
 * Exclusions
 */
class Exclusions {

	/**
	 * Execute hooks
	 */
	public function setup() {
		add_filter( 'personalizewp_pre_record_activity', [ $this, 'exclude_activity' ], 10, 3 );
	}

	/**
	 * Exclude Activity recording under specific conditions
	 *
	 * @since 2.1.0
	 *
	 * @param null|bool $check         Whether to allow recording the Activity entry.
	 * @param string    $unique_id     Browser unique ID
	 * @param array     $activity_args {
	 *     @type string $url           URL of the page the activity took place on
	 *     @type string $activity_type Type of activity to create.
	 *     @type int    $object_id     Object ID for the activity, e.g. Post ID, Term ID. Optional.
	 * }
	 */
	public function exclude_activity( $check, $unique_id, $activity_args ) {

		// Suppress the activity recording of WPEngine Autoupdater checks.
		if ( ! empty( $activity_args['url'] ) ) {
			$url = wp_parse_url( $activity_args['url'], PHP_URL_QUERY );
			if ( ! empty( $url ) && str_contains( $url, 'autoupdater_nonce' ) ) {
				$check = false;
			}
		}

		return $check;
	}
}
