<?php
/**
 * Admin Action plugin file.
 *
 * @package AMVE\Admin\Actions
 */

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

/**
 * Create category in Ajax.
 *
 * @since 1.0.0
 *
 * @return void
 */
function amve_create_category() {
	check_ajax_referer( 'ajax-nonce', 'nonce' );

	if ( ! isset( $_POST['category_name'] ) ) {
		wp_die( '"category_name" parameter is missing!' );
	}

	$category_name = sanitize_text_field( wp_unslash( $_POST['category_name'] ) );
	$taxonomy      = xbox_get_field_value( 'amve-options', 'custom-video-categories', 'category' );

	$params = array(
		'cat_name' => $category_name,
		'taxonomy' => $taxonomy,
	);

	$new_cat_id = wp_insert_category( $params );

	$output = array(
		'new_cat_id' => $new_cat_id,
		'wp_cats'    => AMVE()->get_wp_cats(),
	);
	wp_send_json( $output );

	wp_die();
}
add_action( 'wp_ajax_amve_create_category', 'amve_create_category' );
