<?php
if ( ! defined( 'ABSPATH' ) ) {
	// Exit if accessed directly.
	exit;
}

if ( ! function_exists( 'qode_badges_for_woocommerce_add_rest_api_preview_global_variables' ) ) {
	/**
	 * Extend main rest api variables with new case
	 *
	 * @param array $global - list of variables
	 * @param string $namespace - rest namespace url
	 *
	 * @return array
	 */
	function qode_badges_for_woocommerce_add_rest_api_preview_global_variables( $global, $namespace ) {
		$global['previewRestRoute'] = $namespace . '/preview';

		return $global;
	}

	add_filter( 'qode_badges_for_woocommerce_filter_preview_rest_api_global_variables', 'qode_badges_for_woocommerce_add_rest_api_preview_global_variables', 10, 2 );
}

if ( ! function_exists( 'qode_badges_for_woocommerce_add_rest_api_preview_route' ) ) {
	/**
	 * Extend main rest api routes with new case
	 *
	 * @param array $routes - list of rest routes
	 *
	 * @return array
	 */
	function qode_badges_for_woocommerce_add_rest_api_preview_route( $routes ) {
		$routes['preview'] = array(
			'route'               => 'preview',
			'methods'             => WP_REST_Server::READABLE,
			'callback'            => 'qode_badges_for_woocommerce_get_preview_template_type',
			'permission_callback' => function () {
				return is_user_logged_in();
			},
			'args'                => array(
				'options'        => array(
					'required'          => true,
					'validate_callback' => function ( $param ) {
						// Simple solution for validation can be 'is_array' value instead of callback function.
						return is_array( $param ) ? $param : (array) $param;
					},
					'description'       => esc_html__( 'Options data is array with reaction and id values', 'qode-badges-for-woocommerce' ),
				),
				'security_token' => array(
					'required'          => true,
					'validate_callback' => function ( $param ) {
						return esc_attr( $param );
					},
				),
			),
		);

		return $routes;
	}

	add_filter( 'qode_badges_for_woocommerce_filter_preview_rest_api_routes', 'qode_badges_for_woocommerce_add_rest_api_preview_route' );
}

if ( ! function_exists( 'qode_badges_for_woocommerce_get_preview_template_type' ) ) {
	function qode_badges_for_woocommerce_get_preview_template_type() {
		if ( ! isset( $_GET ) || empty( $_GET ) || ! isset( $_GET['security_token'] ) ) {
			qode_badges_for_woocommerce_get_ajax_status( 'error', esc_html__( 'You are not authorized', 'qode-badges-for-woocommerce' ) );
		} else {
			// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
			if ( ! wp_verify_nonce( wp_unslash( $_GET['security_token'] ), 'wp_rest' ) ) {
				qode_badges_for_woocommerce_get_ajax_status( 'error', esc_html__( 'You are not authorized.', 'qode-badges-for-woocommerce' ) );
			}
			$options = isset( $_GET['options'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_GET['options'] ) ) : array();

			if ( ! empty( $options ) ) {
				$options['classes'] = qode_badges_for_woocommerce_badge_classes( $options );

				ob_start();

				$options = apply_filters( 'qode_badges_for_woocommerce_filter_preview_params', $options );

				qode_badges_for_woocommerce_badge_html( $options['badge_id'], $options );

				$html = ob_get_contents();

				ob_end_clean();

				$data = array(
					'html' => $html,
				);

				qode_badges_for_woocommerce_get_ajax_status( 'success', esc_html__( 'Items are loaded', 'qode-badges-for-woocommerce' ), $data );
			} else {
				qode_badges_for_woocommerce_get_ajax_status( 'error', esc_html__( 'Options are invalid', 'qode-badges-for-woocommerce' ) );
			}
		}
	}
}
