<?php

namespace CodeClouds\Unify\Service;

/**
 * Server request handler.
 * @package CodeClouds\Unify
 */
class Request
{
    /**
     * Get GET variable by $key or get all.
     * @param String $key
     * @return String
     */
    public static function get($key = null)
    {
        // phpcs:disable WordPress.Security.NonceVerification.Recommended -- This is a utility class for accessing request data
        if($key == null)
        {
            return self::sanitize_array_field(wp_unslash($_GET));
        }
        
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        if(isset($_GET[$key]))
        {
            return self::sanitize_array_field(wp_unslash($_GET[$key])); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized via sanitize_array_field method
        }
        // phpcs:enable WordPress.Security.NonceVerification.Recommended
        
        return '';
    }
    
    /**
     * Get POST variable by $key or get all.
     * @param String $key
     * @return String/array
     */
    public static function post($key = null)
    {
        // phpcs:disable WordPress.Security.NonceVerification.Missing -- This is a utility class for accessing request data
        if($key == null)
        {
            return self::sanitize_array_field(wp_unslash($_POST));
        }
        
        // phpcs:ignore WordPress.Security.NonceVerification.Missing
        if(isset($_POST[$key]))
        {
            return self::sanitize_array_field(wp_unslash($_POST[$key])); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized via sanitize_array_field method
        }
        // phpcs:enable WordPress.Security.NonceVerification.Missing
    }
    
    /**
     * Get REQUEST variable by $key or get all.
     * @param String $key
     * @return String/array
     */
    public static function any($key = null)
    {
        // phpcs:disable WordPress.Security.NonceVerification.Recommended -- This is a utility class for accessing request data
        if($key == null)
        {
            return self::sanitize_array_field(wp_unslash($_REQUEST));
        }
        
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        if(isset($_REQUEST[$key]))
        {
            return self::sanitize_array_field(wp_unslash($_REQUEST[$key])); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized via sanitize_array_field method
        }
        // phpcs:enable WordPress.Security.NonceVerification.Recommended
    }
    
    

    /**
     * Get POST variable by $key.
     * @param String $key
     * @return String
     */
    public static function getPost($key)
    {
        // phpcs:disable WordPress.Security.NonceVerification.Missing -- This is a utility class for accessing request data
        if(isset($_POST[$key]))
        {
            return self::sanitize_array_field(wp_unslash($_POST[$key])); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized via sanitize_array_field method
        }
        // phpcs:enable WordPress.Security.NonceVerification.Missing
    }
    
    /**
     * Get all POST variables as an array.
     * @return array
     */
    public static function getPostArray()
    {
        // phpcs:disable WordPress.Security.NonceVerification.Missing -- This is a utility class for accessing request data
        return self::sanitize_array_field(wp_unslash($_POST));
        // phpcs:enable WordPress.Security.NonceVerification.Missing
    }
    
    /**
	 * Set POST variable.
	 * @param String $key
	 * @param String/array $value
	 */
	public static function setPost($key, $value)
	{
		$_POST[$key] = $value;
	}

    /**
     * sanitize array/string
     */
    public static function sanitize_array_field($array)
    {
        if (is_array($array)) {
            foreach ($array as $key => &$value) {
                if (is_array($value)) {
                    $array[$key] = self::sanitize_array_field($value);
                } else {
                    // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
                    $array[$key] = sanitize_text_field(wp_unslash($value));
                }
            }
        } else {
            // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
            return sanitize_text_field(wp_unslash($array));
        }

        return $array;
    } 
}
