<?php
/**
 * Global helper functions for PrimeLink.
 *
 * @package PrimeLink
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( ! function_exists( 'mt_primelink_get' ) ) {
    /**
     * Safe access to request values with sanitization.
     *
     * @param string $key Request key.
     * @param mixed  $default Default value.
     * @param string $sanitize Sanitization type: 'text', 'url', 'email', 'int', 'float', 'array', or 'raw' (no sanitization).
     * @return mixed Sanitized value or default.
     */
    function mt_primelink_get( $key, $default = null, $sanitize = 'text' ) {
        if ( ! isset( $_REQUEST[ $key ] ) ) {
            return $default;
        }

        $value = wp_unslash( $_REQUEST[ $key ] );

        // Apply sanitization based on type.
        switch ( $sanitize ) {
            case 'text':
                if ( is_string( $value ) ) {
                    return sanitize_text_field( $value );
                }
                if ( is_array( $value ) ) {
                    return array_map( 'sanitize_text_field', $value );
                }
                return $value;

            case 'url':
                return esc_url_raw( $value );

            case 'email':
                return sanitize_email( $value );

            case 'int':
                return absint( $value );

            case 'float':
                return floatval( $value );

            case 'array':
                if ( ! is_array( $value ) ) {
                    return $default;
                }
                return array_map( 'sanitize_text_field', $value );

            case 'raw':
            default:
                return $value;
        }
    }
}