Untitled

mail@pastecode.io avatar
unknown
plain_text
25 days ago
1.3 kB
12
Indexable
Never
use function get_class_methods;


class AdminAjax {
	
	public function __construct() {
		$this->register_actions();
	}

	
	public function start_parsing() {

		if ( ! $this->verify( 'start_parsing' ) ) {
			echo wp_json_encode(
				array(
					'error' => 'Error request.',
				)
			);
			wp_die();
		}

		$categories = $this->get_post_field( 'category_fans', '' );
		
	}

	
	public static function get_nonce_field( string $action_name ) {
		wp_nonce_field( "of_{$action_name}_action", "of_{$action_name}_nonce" );
	}

	
	private function register_actions() {
		$methods = get_class_methods( $this );

		foreach ( $methods as $method ) {
			if ( '_' === substr( $method, 0, 1 ) ) {
				continue;
			}

			if ( 'register_actions' === $method ) {
				continue;
			}

			add_action( 'wp_ajax_' . $method, array( $this, $method ) );
			add_action( 'wp_ajax_nopriv_' . $method, array( $this, $method ) );
		}
	}


	private function verify( string $action_name ): bool {
		$nonce  = "of_{$action_name}_nonce";
		$action = "of_{$action_name}_action";

		return isset( $_POST[ $nonce ] ) && wp_verify_nonce( $_POST[ $nonce ], $action );
	}

	
	private function get_post_field( string $key, $default = '' ) {
		return isset( $_POST[ $key ] ) ? wp_unslash( $_POST[ $key ] ) : $default;
	}
}
Leave a Comment