<?php
/**
 * Post/Page/CPT visiting assigned Scoring Rules
 *
 * @link       https://personalizewp.com/
 * @since      3.2.0 Merged into PWP
 * @since      1.0.0
 *
 * @package    PersonalizeWP
 * @subpackage PersonalizeWP/Scoring_Rules
 */

namespace PersonalizeWP\Scoring_Rules;

use PersonalizeWP\Scoring_Rules\Base;

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

/**
 * Page/Post/URL Segments
 *
 * Handles Activities with type `home`, `post`, `tax`
 */
class Page_URL extends Base {

	/**
	 * Types of Activity to process
	 *
	 * @var array
	 */
	protected $activity_types = array( 'home', 'post', 'tax' );

	/**
	 * Types of Scoring Rule to process
	 *
	 * @var array
	 */
	protected $score_types = array( 'page' );

	/**
	 * Filter a `page` type Activity for Scoring rules
	 *
	 * @since 1.1.0
	 *
	 * @param array  $activity_args Activity arguments.
	 * @param string $type          Activity Type.
	 *
	 * @return array
	 */
	public function add_activity_score( $activity_args, $type ) {

		// Double check
		if ( ! in_array( $type, $this->activity_types, true ) || empty( $activity_args ) || ! isset( $activity_args['lead_score'] ) ) {
			return $activity_args;
		}

		if ( ! isset( $this->scoring_rules ) ) {
			$this->scoring_rules = $this->get_scoring_rules();
		}

		if ( empty( $this->scoring_rules ) ) {
			return $activity_args;
		}

		// Clean Up URL, removing domain & query parameters
		$url = wp_parse_url( $activity_args['url'], PHP_URL_PATH );
		foreach ( $this->scoring_rules as $scoring_rule ) {
			// Not tracking any page/URL, or somehow no score to apply.
			if ( empty( $scoring_rule['conditions'] ) || empty( $scoring_rule['lead_score'] ) ) {
				continue;
			}

			$args = [
				'activity'   => (object) $activity_args, // Need to convert to object for inherited functions
				'conditions' => json_decode( $scoring_rule['conditions'], true ),
				'operator'   => $scoring_rule['operator'],
				'url'        => $url,
			];
			if ( $this->activity_has_matched_conditions( $args ) ) {
				// Add the Scoring Rule score back for the Activity to store.
				$activity_args['lead_score'] += $scoring_rule['lead_score'];
			}
		}

		// Always return activity args for Activity creation.
		return $activity_args;
	}

	/**
	 * Test condition against Page URL criteria, full URL, URL regex, or CPT ID match.
	 *
	 * @since 2.3.0
	 *
	 * @param array $args {
	 *     Array of args to use for matching the condition.
	 *
	 *     @type Activity $activity  Activity object
	 *     @type mixed    $condition Index of all conditions, or URL/regex to compare
	 *     @type mixed    $options   CPT ID, (legacy) URL condition to match, or URL options/qualifier
	 *     @type string   $url       URL Path of Activity
	 * }
	 *
	 * @return boolean
	 */
	protected function condition_has_matched_criteria( $args ) {

		// Note: Condition is stored as options as the 'condition' is just an array index.
		list( 'activity' => $activity, 'condition' => $condition, 'options' => $options, 'url' => $url ) = $args;

		$options = trim( $options );

		// In legacy format 'options' mapped to 'condition', so 'options' will contain numeric ID or string URL, and 'condition' is always a numeric index.
		// In updated format 'options' can contain either numeric ID, or string of 'url' or 'regex'. 'condition' is numeric for ID, string for URL.

		if (
			( is_numeric( $condition ) || 0 === (int) $condition ) &&
			is_numeric( $options )
		) {
			// Supporting direct object ID condition.
			$condition = (int) $options;
			$options   = null;
		} elseif (
			( is_numeric( $condition ) || 0 === (int) $condition ) &&
			! in_array( $options, array( 'regex', 'url' ), true )
		) {
			// Supporting legacy URL based condition, without newer url/regex qualifier.
			$condition = $options;
			$options   = null;
		}

		// Matching CPT ID, when it's a CPT type activity
		if ( 'post' === $activity->activity_type && ! empty( $condition ) &&
			isset( $activity->object_id ) && (int) $activity->object_id === (int) $condition ) {

			return true;
		}

		$is_url   = empty( $options ) || 'url' === $options;
		$is_regex = 'regex' === $options;

		// Check for simplier string match. Must fully match the URL. Note: case in-sensitive matching.
		if ( $is_url && strtolower( $url ) === strtolower( $condition ) ) {
			return true;
		}
		// With sites having many subdomains or TLD, we need to use regex.
		if ( $is_regex && 1 === preg_match( '/' . $condition . '/i', $url ) ) {
			return true;
		}
		return false;
	}
}
