<?php

/**
 * Fontend Ajax
 * Handles all ajax requests for frontend area
 *
 * @since 1.7.6
 * @package EasyVideoReviews
 */

// Namespace.
namespace RVIOR;

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


if ( ! class_exists( __NAMESPACE__ . '\Ajax' ) ) {
	/**
	 * Frontend Ajax
	 *
	 * @since 1.7.6
	 */
	class Ajax extends \RVIOR\Base\Controller {

		// Use utilities trait.
		use \RVIOR\Traits\Utilities;

		/**
		 * Prefix for all ajax actions
		 *
		 * @var string
		 */
		protected $prefix = 'rvior_';

		/**
		 * Registers all ajax hooks
		 *
		 * @return void
		 */
		public function register_hooks() {
			add_action( 'wp_ajax_' . $this->prefix . 'check_existing_gallery', [ $this, 'check_existing_gallery' ]);
			add_action( 'wp_ajax_nopriv' . $this->prefix . 'check_existing_gallery', [ $this, 'check_existing_gallery' ]);
		}

		/**
		 * Check existing gallery with folder embed
		 *
		 * @return void
		 */
        public function check_existing_gallery() {

			// Check nonce.
			check_ajax_referer( 'rvior_nonce', 'nonce');

            $gallery_id = absint($_REQUEST['gid']); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated

			// get created gallery from database
			$galleries = $this->option()->get( 'gallaries' );

			// get selected gallery by gallery id
			$get_selected_gallery = array_filter($galleries, function ( $gallery ) use ( $gallery_id ) {
				return (int) $gallery['id'] === (int) $gallery_id['gid'];
			});

			if ( ! empty($get_selected_gallery) ) {
				$this->io()->send_json( true,'gallery exist');
			} else {
				$this->io()->send_json( false,'gallery not exist');
			}

			//$this->io()->send_json( true,'',$check_iputs);
		}
    }

    // Instantiate.
	Ajax::init();
}
