<?php
/**
 * AJAX Security Trait
 *
 * Provides DRY nonce verification and capability checking for AJAX handlers.
 *
 * @package Wisewand
 * @since   2.8.2
 * @license GPL-2.0-or-later
 */

namespace Wisewand\Admin\Traits;

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

/**
 * Trait AJAX_Security
 *
 * Consolidates the nonce + capability check pattern used by all AJAX handlers.
 * Sends wp_send_json_error and terminates if either check fails.
 *
 * @since 2.8.2
 */
trait AJAX_Security {

	/**
	 * Verify AJAX request with nonce and capability check
	 *
	 * @since 2.8.2
	 * @param string $nonce_action The nonce action name.
	 * @param string $capability   Required user capability. Default 'edit_posts'.
	 */
	protected function verify_ajax_request( $nonce_action, $capability = 'edit_posts' ) {
		check_ajax_referer( $nonce_action, 'nonce' );

		if ( ! current_user_can( $capability ) ) {
			wp_send_json_error( array(
				'message' => __( 'Insufficient permissions', 'wisewand-ai-content-writer-seo-content-generator' ),
			) );
		}
	}
}
