<?php
/**
 * class AjaxBase
 *
 * @since 4.2.7.6
 * @version 1.0.6
 */

namespace LearnPress\Ajax;

use LP_Request;

/**
 * @use LoadContentViaAjax::load_content_via_ajax
 *
 * $action must unique name on all Ajax classes.
 * Because not specify a specific class.
 */
abstract class AbstractAjax {
	public static function catch_lp_ajax() {
		if ( ! empty( $_REQUEST['lp-load-ajax'] ) ) {
			$action = LP_Request::get_param( 'lp-load-ajax' );
			$nonce  = LP_Request::get_param( 'nonce' );
			$class  = new static();

			if ( ! method_exists( $class, $action ) ) {
				return;
			}

			// For case cache HTML, so cache nonce is not required.
			$class_no_nonce = [
				LoadContentViaAjax::class,
			];

			// Todo: should write separation check none login and none not login
			if ( ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
				if ( ! in_array( get_class( $class ), $class_no_nonce ) ) {
					wp_die( 'Invalid request!', 400 );
				}
			}

			if ( is_callable( [ $class, $action ] ) ) {
				call_user_func( [ $class, $action ] );
			}
		}
	}
}
