<?php
/**
 * WordPress Triggers Trait.
 *
 * @package automation-web-platform
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

trait WAWP_WordPress_Triggers {
	/**
	 * Handle user login trigger.
	 *
	 * @param string  $user_login Username.
	 * @param WP_User $user       User object.
	 */
	public function handle_user_login_trigger( $user_login, $user ) {
		if ( empty( $this->wawp_db_manager ) ) {
			return;
		}

		$settings = $this->get_settings();
		$locale   = get_user_locale( $user->ID );
		$rules    = $this->get_matching_rules( $settings, $locale, 'user_login' );
		if ( empty( $rules ) ) {
			return;
		}

		$phone             = WAWP()->user_manager->get_user_phone( $user->ID );
		$first_name        = get_user_meta( $user->ID, 'first_name', true );
		$last_name         = get_user_meta( $user->ID, 'last_name', true );
		$replacements_user = array(
			'{{wp-display-name}}'   => $user->display_name,
			'{{wp-first-name}}'     => $first_name ? $first_name : $user->display_name,
			'{{wp-last-name}}'      => $last_name,
			'{{wp-username}}'       => $user->user_login,
			'{{wp-email}}'          => $user->user_email,
			'{{wp-nickname}}'       => get_user_meta( $user->ID, 'nickname', true ) ? get_user_meta( $user->ID, 'nickname', true ) : $user->user_login,
			'{{wp-user-website}}'   => $user->user_url,
			'{{wp-user-bio}}'       => get_user_meta( $user->ID, 'description', true ),
			'{{wawp_user_phone}}'   => $phone,
			'{{user_name}}'         => $user->user_login,
			'{{display_name}}'      => $user->display_name,
			'{{user_email}}'        => $user->user_email,
			'{{site_title}}'        => get_bloginfo( 'name' ),
			'{{sitename}}'          => get_bloginfo( 'name' ),
			'{{siteurl}}'           => home_url(),
			'{{tagline}}'           => get_bloginfo( 'description' ),
			'{{privacy-policy}}'    => function_exists( 'get_privacy_policy_url' ) ? get_privacy_policy_url() : '',
			'{{current_date_time}}' => wp_date( 'Y-m-d H:i:s' ),
		);
		$recipient_user    = array(
			'user_id'      => $user->ID,
			'email'        => $user->user_email,
			'phone'        => $phone,
			'display_name' => $user->display_name,
		);
		foreach ( $rules as $rule ) {
			$this->schedule_or_send( $rule, $replacements_user, $recipient_user, 'User Login', $user->ID, 'user_login' );
		}
	}

	/**
	 * Handle user signup trigger.
	 *
	 * @param int $user_id User ID.
	 */
	public function handle_user_signup_trigger( $user_id ) {
		if ( empty( $this->wawp_db_manager ) ) {
			return;
		}

		$user = get_userdata( $user_id );
		if ( ! $user ) {
			return;
		}

		$settings = $this->get_settings();
		$locale   = get_user_locale( $user_id );
		$rules    = $this->get_matching_rules( $settings, $locale, 'user_signup' );
		if ( empty( $rules ) ) {
			return;
		}

		$phone = WAWP()->user_manager->get_user_phone( $user_id );
		if ( empty( $phone ) ) {
			$nonce = isset( $_POST['_wpnonce'] ) ? sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ) : '';
			if ( $nonce ) {
				wp_verify_nonce( $nonce );
			}

			if ( ! empty( $_POST['phone'] ) ) {
				$phone = sanitize_text_field( wp_unslash( $_POST['phone'] ) );
			} elseif ( ! empty( $_POST['wawp_phone'] ) ) {
				$phone = sanitize_text_field( wp_unslash( $_POST['wawp_phone'] ) );
			} elseif ( ! empty( $_POST['billing_phone'] ) ) {
				$phone = sanitize_text_field( wp_unslash( $_POST['billing_phone'] ) );
			}
		}
		$first_name        = get_user_meta( $user_id, 'first_name', true );
		$last_name         = get_user_meta( $user_id, 'last_name', true );
		$replacements_user = array(
			'{{wp-display-name}}'   => $user->display_name,
			'{{wp-first-name}}'     => $first_name ? $first_name : $user->display_name,
			'{{wp-last-name}}'      => $last_name,
			'{{wp-username}}'       => $user->user_login,
			'{{wp-email}}'          => $user->user_email,
			'{{wp-nickname}}'       => get_user_meta( $user_id, 'nickname', true ) ? get_user_meta( $user_id, 'nickname', true ) : $user->user_login,
			'{{wp-user-website}}'   => $user->user_url,
			'{{wp-user-bio}}'       => get_user_meta( $user_id, 'description', true ),
			'{{wawp_user_phone}}'   => $phone,
			'{{user_name}}'         => $user->user_login,
			'{{display_name}}'      => $user->display_name,
			'{{user_email}}'        => $user->user_email,
			'{{site_title}}'        => get_bloginfo( 'name' ),
			'{{sitename}}'          => get_bloginfo( 'name' ),
			'{{siteurl}}'           => home_url(),
			'{{tagline}}'           => get_bloginfo( 'description' ),
			'{{privacy-policy}}'    => function_exists( 'get_privacy_policy_url' ) ? get_privacy_policy_url() : '',
			'{{current_date_time}}' => wp_date( 'Y-m-d H:i:s' ),
		);
		$recipient_user    = array(
			'user_id'      => $user_id,
			'email'        => $user->user_email,
			'phone'        => $phone,
			'display_name' => $user->display_name,
		);
		foreach ( $rules as $rule ) {
			$this->schedule_or_send( $rule, $replacements_user, $recipient_user, 'User Signup', $user_id, 'user_signup' );
		}
	}
}
