<?php
/**
 * Elite Notification Module Lite
 *
 * @package Elite_Notification_Lite\Core
 */

namespace Elite_Notification_Lite\Core;

use Appsbd_Lite\V4\Core\BaseModule;
use Appsbd_Lite\V4\libs\Ajax_Confirm_Response;

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

/**
 * Class Elite_Notification_Module_Lite
 *
 * @package Elite_Notification_Lite\Core
 */
abstract class Elite_Notification_Module_Lite extends BaseModule {
	/**
	 * The on init is generated by appsbd
	 */
	public function on_init() {
		$this->add_admin_ajax_action( 'option', array( $this, 'ajax_request_callback' ) );
		$this->add_admin_ajax_action( 'get-option', array( $this, 'get_admin_options' ) );
		$this->add_admin_ajax_action( 'data', array( $this, 'data' ) );
		$this->add_admin_ajax_action( 'confirm', array( $this, 'confirm' ) );
	}
	/**
	 * The check user action access is generated by appsbd
	 *
	 * @param mixed $action_name Its action_name param.
	 *
	 * @return bool
	 */
	public function check_user_action_access( $action_name ) {
		return $this->check_user_access();
	}
	/**
	 * The check ajax referer is generated by appsbd
	 *
	 * @param bool $is_return Its checking security.
	 *
	 * @return bool
	 */
	public function app_check_ajax_referer( $is_return = false ) {

		if ( $this->kernel_object->is_develop_mode() ) {
			$headers = getallheaders();
			if ( ! empty( $headers['appsbd_dev'] ) ) {
				return true;
			}
		}

		return true;
	}

	/**
	 * The AddAjaxAction is generated by appsbd
	 *
	 * @param any      $action_name Its action_name param.
	 * @param callable $function_to_add Its function_to_add param.
	 */
	public function add_admin_ajax_action( $action_name, $function_to_add ) {
		if ( ! $this->check_user_action_access( $action_name ) ) {
			$action_name = $this->get_action_name( $action_name );
			add_action(
				'wp_ajax_' . $action_name,
				function () {
					$main_response = new Ajax_Confirm_Response();
					$this->add_error( 'User privilege error' );
					$main_response->display_with_response( false, null, 403 );
				}
			);
			return;
		}
		$action_name = $this->get_action_name( $action_name );
		if ( $this->app_check_ajax_referer( true ) ) {
			add_action( 'wp_ajax_' . $action_name, $function_to_add );
		} else {
			add_action(
				'wp_ajax_' . $action_name,
				function () {
					$main_response = new Ajax_Confirm_Response();
					$this->add_error( 'Nonce error' );
					$main_response->display_with_response( false, null, 403 );
				}
			);
		}
	}
}
