<?php

namespace WC_Affiliate\Traits;

defined( 'ABSPATH' ) || exit;

/**
 * Trait Hook
 *
 * This trait provides methods to register action hooks, filter hooks, shortcodes, and AJAX actions in the WordPress plugin.
 *
 * @package WC_Affiliate
 */
trait Hook {

	/**
	 * Registers an action hook.
	 *
	 * @param string $tag           The name of the action.
	 * @param mixed  $callback      The callback function.
	 * @param int    $priority      The order of execution. Default is 10.
	 * @param int    $accepted_args The number of accepted arguments. Default is 1.
	 */
	public function add_action( string $tag, $callback, int $priority = 10, int $accepted_args = 1 ) {
		if ( is_callable( $callback ) ) {
			add_action( $tag, $callback, $priority, $accepted_args );
		}
	}

	/**
	 * Convenience wrapper for add_action.
	 *
	 * @param string $tag           The name of the action.
	 * @param mixed  $callback      The callback function.
	 * @param int    $priority      The order of execution. Default is 10.
	 * @param int    $accepted_args The number of accepted arguments. Default is 1.
	 */
	public function action( string $tag, $callback, int $priority = 10, int $accepted_args = 1 ) {
		$this->add_action( $tag, $callback, $priority, $accepted_args );
	}

	/**
	 * Registers a filter hook.
	 *
	 * @param string $tag           The name of the filter.
	 * @param mixed  $callback      The callback function.
	 * @param int    $priority      The order of execution. Default is 10.
	 * @param int    $accepted_args The number of accepted arguments. Default is 1.
	 */
	public function add_filter( string $tag, $callback, int $priority = 10, int $accepted_args = 1 ) {
		if ( is_callable( $callback ) ) {
			add_filter( $tag, $callback, $priority, $accepted_args );
		}
	}

	/**
	 * Convenience wrapper for add_filter.
	 *
	 * @param string $tag           The name of the filter.
	 * @param mixed  $callback      The callback function.
	 * @param int    $priority      The order of execution. Default is 10.
	 * @param int    $accepted_args The number of accepted arguments. Default is 1.
	 */
	public function filter( string $tag, $callback, int $priority = 10, int $accepted_args = 1 ) {
		$this->add_filter( $tag, $callback, $priority, $accepted_args );
	}

	/**
	 * Registers a shortcode.
	 *
	 * @param string $tag      The shortcode tag.
	 * @param mixed  $callback The callback function.
	 */
	public function add_shortcode( string $tag, $callback ) {
		if ( is_callable( $callback ) ) {
			add_shortcode( $tag, $callback );
		}
	}

	/**
	 * Convenience wrapper for add_shortcode.
	 *
	 * @param string $tag      The shortcode tag.
	 * @param mixed  $callback The callback function.
	 */
	public function shortcode( string $tag, $callback ) {
		$this->add_shortcode( $tag, $callback );
	}

	/**
	 * Registers an AJAX action for logged-in users.
	 *
	 * @param string $action   The AJAX action.
	 * @param mixed  $callback The callback function.
	 */
	public function ajax_priv( string $action, $callback ) {
		if ( is_callable( $callback ) ) {
			add_action( 'wp_ajax_' . $action, $callback );
		}
	}

	/**
	 * Registers an AJAX action for non-logged-in users.
	 *
	 * @param string $action   The AJAX action.
	 * @param mixed  $callback The callback function.
	 */
	public function ajax_nopriv( string $action, $callback ) {
		if ( is_callable( $callback ) ) {
			add_action( 'wp_ajax_nopriv_' . $action, $callback );
		}
	}

	/**
	 * Registers both logged-in and non-logged-in AJAX actions.
	 *
	 * @param string $action   The AJAX action.
	 * @param mixed  $callback The callback function.
	 */
	public function ajax( string $action, $callback ) {
		if ( is_callable( $callback ) ) {
			$this->ajax_priv( $action, $callback );
			$this->ajax_nopriv( $action, $callback );
		}
	}
}
