<?php
/**
 * EverestForms Pro EVF_PRO_AJAX. AJAX Event Handlers.
 *
 * @class   EVF_PRO_AJAX
 * @package EverestFormsPro/Classes
 */

defined( 'ABSPATH' ) || exit;

/**
 * EVF_PRO_AJAX class.
 */
class EVF_PRO_AJAX {

	/**
	 * Hook in ajax handlers.
	 */
	public static function init() {
		add_action( 'init', array( __CLASS__, 'define_ajax' ), 0 );
		add_action( 'template_redirect', array( __CLASS__, 'do_evf_ajax' ), 0 );
		self::add_ajax_events();
	}



	/**
	 * Set EVF Pro AJAX constant and headers.
	 */
	public static function define_ajax() {
		// @codingStandardsIgnoreStart
		if ( ! empty( $_GET['evf-pro-ajax'] ) ) {
			evf_maybe_define_constant( 'DOING_AJAX', true );
			evf_maybe_define_constant( 'EVFP_DOING_AJAX', true );
			if ( ! WP_DEBUG || ( WP_DEBUG && ! WP_DEBUG_DISPLAY ) ) {
				@ini_set( 'display_errors', 0 ); // Turn off display_errors during AJAX events to prevent malformed JSON.
				}
			$GLOBALS['wpdb']->hide_errors();
			}
		// @codingStandardsIgnoreEnd
	}

	/**
	 * Send headers for EVFP Ajax Requests.
	 *
	 * @since 1.0.0
	 */
	private static function evfp_ajax_headers() {
		if ( ! headers_sent() ) {
			send_origin_headers();
			send_nosniff_header();
			evf_nocache_headers();
			header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
			header( 'X-Robots-Tag: noindex' );
			status_header( 200 );
		} elseif ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
			headers_sent( $file, $line );
			trigger_error( "evf_ajax_headers cannot set headers - headers already sent by {$file} on line {$line}", E_USER_NOTICE ); // @codingStandardsIgnoreLine
		}
	}

	/**
	 * Check for EVFP Ajax request and fire action.
	 */
	public static function do_evf_ajax() {
		global $wp_query;

		if ( ! empty( $_GET['evf-pro-ajax'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
			$wp_query->set( 'evf-pro-ajax', sanitize_text_field( wp_unslash( $_GET['evf-pro-ajax'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification
		}

		$action = $wp_query->get( 'evf-pro-ajax' );

		if ( $action ) {
			self::evfp_ajax_headers();
			$action = sanitize_text_field( $action );
			do_action( 'evf_ajax_' . $action );
			wp_die();
		}
	}

	/**
	 * Hook in methods - uses WordPress ajax handlers (admin-ajax).
	 */
	public static function add_ajax_events() {
		$ajax_events = array(
			'add_webhooks' => false
		);

		foreach ( $ajax_events as $ajax_event => $nopriv ) {
			add_action( 'wp_ajax_everest_forms_pro_' . $ajax_event, array( __CLASS__, $ajax_event ) );

			if ( $nopriv ) {
				add_action( 'wp_ajax_nopriv_everest_forms_pro_' . $ajax_event, array( __CLASS__, $ajax_event ) );

				// EVFP AJAX can be used for frontend ajax requests.
				add_action( 'evfp_ajax_' . $ajax_event, array( __CLASS__, $ajax_event ) );
			}
		}
	}

	/**
	 * Add the webhook.
	 *
	 * @since 1.7.8
	 */
	public static function add_webhooks() {
		check_ajax_referer( 'evf_webhook_nonce', 'security' );
		$next_id = isset( $_POST['next_id'] ) ? sanitize_text_field( wp_unslash( $_POST['next_id'] ) ) : 'unknown';

		$pro_instance = EverestForms_Pro::get_instance();
		$form_data    = array();

		if ( ! empty( $_GET['form_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
			$form_data = evf()->form->get( absint( $_GET['form_id'] ), array( 'content_only' => true ) ); // phpcs:ignore WordPress.Security.NonceVerification
		}
		$next_web_hook_settings = $pro_instance->output_single_webhook_settings( (object) array( 'form_data' => $form_data ), $next_id, array() );

		wp_send_json_success( array( 'content' => $next_web_hook_settings ) );
	}
}

EVF_PRO_AJAX::init();
