PluginProbe
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts / 2.7.6
Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts v2.7.6
2.7.6 2.7.5 2.7.4 trunk 1.3 2.0.4 2.0.6 2.1.91 2.2.4 2.2.7 2.2.9 2.3.1 2.3.10 2.4.10 2.4.2 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.6.0 2.6.1 2.7.0 2.7.1 All 27 releases
insert-php / includes / class.http.php

class.http.php in Woody Code Snippets – Insert PHP, CSS, JS, and Header/Footer Scripts 2.7.6, at includes/class.http.php

132 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Request handler class
4 *
5 * @package Woody_Code_Snippets
6 */
7
8 // Exit if accessed directly.
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 /**
14 * Class WINP_HTTP
15 *
16 * Handles HTTP request parameter retrieval and sanitization
17 */
18 class WINP_HTTP {
19
20 /**
21 * Get request parameter value
22 *
23 * @param string $param Parameter name.
24 * @param mixed $default Default value if parameter not found.
25 * @param bool|string $sanitize Sanitize function name or true for default sanitization.
26 * @param string $method Request method: 'REQUEST', 'GET', or 'POST'.
27 *
28 * @return mixed
29 */
30 private static function get_body( $param, $sanitize = false, $default = false, $method = 'REQUEST' ) {
31 if ( empty( $param ) ) {
32 return null;
33 }
34
35 $sanitize_function_name = 'sanitize_text_field';
36
37 switch ( strtoupper( $method ) ) {
38 case 'GET':
39 $source = $_GET;
40 break;
41 case 'POST':
42 $source = $_POST;
43 break;
44 case 'REQUEST':
45 default:
46 $source = $_REQUEST;
47 break;
48 }
49
50 if ( is_string( $sanitize ) && $sanitize !== $sanitize_function_name ) {
51 $sanitize_function_name = $sanitize;
52 }
53
54 if ( isset( $source[ $param ] ) ) {
55 if ( is_array( $source[ $param ] ) ) {
56 return ! empty( $sanitize ) ? self::recursive_array_map( $sanitize_function_name, $source[ $param ] ) : $source[ $param ];
57 } else {
58 if ( ! empty( $sanitize ) && is_callable( $sanitize_function_name ) ) {
59 return call_user_func( $sanitize_function_name, $source[ $param ] );
60 }
61 return $source[ $param ];
62 }
63 }
64
65 return $default;
66 }
67
68 /**
69 * Recursive sanitation for an array
70 *
71 * @param string $function_name Sanitization function name.
72 * @param array<string, mixed> $array Array to sanitize.
73 *
74 * @return array<string, mixed>
75 * @throws Exception If function is not defined.
76 */
77 public static function recursive_array_map( $function_name, $array ) {
78 foreach ( $array as $key => &$value ) {
79 if ( is_array( $value ) ) {
80 $value = self::recursive_array_map( $function_name, $value );
81 } else {
82 if ( ! function_exists( $function_name ) ) {
83 throw new Exception( 'Function ' . $function_name . ' is undefined.' );
84 }
85
86 $value = $function_name( $value );
87 }
88 }
89
90 return $array;
91 }
92
93 /**
94 * Get value from REQUEST superglobal
95 *
96 * @param string $param Parameter name.
97 * @param mixed $default Default value if parameter not found.
98 * @param bool|string $sanitize Sanitize function name or true for default sanitization.
99 *
100 * @return mixed
101 */
102 public static function request( $param, $default = false, $sanitize = false ) {
103 return self::get_body( $param, $sanitize, $default, 'REQUEST' );
104 }
105
106 /**
107 * Get value from GET superglobal
108 *
109 * @param string $param Parameter name.
110 * @param mixed $default Default value if parameter not found.
111 * @param bool|string $sanitize Sanitize function name or true for default sanitization.
112 *
113 * @return mixed
114 */
115 public static function get( $param, $default = false, $sanitize = false ) {
116 return self::get_body( $param, $sanitize, $default, 'GET' );
117 }
118
119 /**
120 * Get value from POST superglobal
121 *
122 * @param string $param Parameter name.
123 * @param mixed $default Default value if parameter not found.
124 * @param bool|string $sanitize Sanitize function name or true for default sanitization.
125 *
126 * @return mixed
127 */
128 public static function post( $param, $default = false, $sanitize = false ) {
129 return self::get_body( $param, $sanitize, $default, 'POST' );
130 }
131 }
132