WordPressMVC

  1. Get Started
  2. Documentation
  3. Tutorials
  4. Blog
  1. Documentation
  2. Tutorials
v1.0.8 Login
  • Blog
  • Download
  • Get Started
  • The Basics
    • Ayuco
    • Main Class
    • Configuration
  • Hooks
    • Actions
    • Filters
    • Widgets
    • Shortcodes
  • MVC
    • Models
    • Post Models
    • Option Models
    • Term Models
    • User Models
    • Views
    • Controllers
  • Resources
    • Assets
    • Styles
    • Scripts
  • Advanced
    • Request
    • Response
    • Localization
    • Cache
    • Logger
    • Database
    • Testing
    • Deployment
    • Functions
    • Add-ons

Request

Last updated: May 1, 2020

Request class simplifies the retrieval and sanitization of values from $_GET, $_POST and WordPress’s $query_vars.

Usage

Add the use statement at the beginning of your PHP files to specify which Request class you’ll use in your code.

use WPMVC\Request;

Use the static method input() to get the wanted value:

/*
 * "$default_value" value will be returned if not input under
 * "name" is found.
 */
$name = Request::input( 'name', $default_value );

All

Use the static method all() to get all the values sanitized:

$request = Request::all();

// Then search by key
echo $request[$key];

Nonce

Add a third boolean parameter in order to prevent loops caused when calling to certain WordPress hooks. This has a critical use when calling for a nonce value:

/**
 * "$clear_source" will clear the input retrieved from source
 * ($_GET, $_POST or $query_vars).
 */
$nonce = Request::input( 'my_nonce', $default_value, $clear_source = true );

Sanitization

By default, the request class will apply sanitation to the values:

Data typeSanitize callableDescription
intintvalTrims the incoming value and forces casting to aninteger value.
floatfloatvalTrims the incoming value and forces casting to afloat value.
stringsanitize_email
sanitize_text_field
Cast to bool
Trims the string. If the string is true or false then the request class returns a boolean value. If the string passes PHP email filter then the request class applies sanitize_email. For Any other string will the request class will apply sanitize_text_field.
arrayAll the above.Each of the values inside of an incoming array will pass for the sanitization described for the data types above.

Custom sanitize callable

The default sanitize callable can be changed for a custom one, see samples:

/**
 * Sample callable as a global function.
 */
function custom_sanitizer( $value ) {
    // Custom code here...
    return $value
}

/**
 * 4th parameter should be the sanitize callable.
 * @param $key => 'key'
 * @param $default_value => null
 * @param $clear_source  => false
 * @param $sanitize => 'custom_sanitizer'
 */
$value = Request::input( 'key', null, false, 'custom_sanitizer' );

/**
 * @param $sanitize => 'custom_sanitizer'
 */
$request = Request::all( 'custom_sanitizer' );

Stop sanatization

Pass a `false` value instead of a callable to prevent and stop sanitization from happening, this will return the original/raw values, see samples:

/**
 * @param $key => 'key'
 * @param $default_value => null
 * @param $clear_source  => false
 * @param $sanitize => 'custom_sanitizer'
 */
$value = Request::input( 'key', null, false, false );

/**
 * @param $sanitize => 'custom_sanitizer'
 */
$request = Request::all( false );
© 2023 10 Quality Studio . All rights reserved.
Search in: