-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathQueryVars.php
More file actions
164 lines (137 loc) · 3.06 KB
/
QueryVars.php
File metadata and controls
164 lines (137 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace tcs\Theme\Supports;
/**
* Class QueryVars
*
* @package JCWP\Theme\Supports
*/
class QueryVars {
/**
* Query data
*
* @var array $query_data
*/
protected $query_data = array(
'get' => array(),
'post' => array(),
);
/**
* Query vars
*
* @var array $query_vars
*/
protected $query_vars = array();
/**
* Nonce name
*
* @var string $nonce_name
*/
private $nonce_name;
/**
* Nonce action
*
* @var string $nonce_action
*/
private $nonce_action;
/**
* Enable or disable sanitizing
*
* @var bool $sanitizing
*/
private $sanitizing = true;
/**
* QueryVars constructor.
*/
public function __construct() {
}
/**
* Configure
*
* @param $nonce_name
* @param $nonce_action
*/
public function configure( $nonce_name, $nonce_action ) {
if ( ! empty( $nonce_name ) && ! empty( $nonce_action ) ) {
$this->nonce_name = $nonce_name;
$this->nonce_action = $nonce_action;
}
}
/**
* Use_sanitize
*
* @return bool
*/
public function no_sanitize(): bool {
return $this->sanitizing = false;
}
/**
* Get query vars
*
* @param string $method Methods: post, get.
* @param string $var .
*
* @return mixed
*/
public function get_query_vars( $method = 'any', $var = '' ) {
$this->verify_query_vars();
if ( 'any' !== $method && empty( $var ) ) {
return $this->query_data[ $method ];
}
if ( 'any' !== $method && ! empty( $var ) ) {
if ( ! isset( $this->query_data[ $method ] ) || ! isset( $this->query_data[ $method ][ $var ] ) ) {
return null;
}
return $this->query_data[ $method ][ $var ];
}
return $this->query_data;
}
/**
* Add dynamic query var
*
* @param string|array $var .
*
* @return array
*/
public function add_query_var( $var ): array {
if ( ! empty( $var ) && ! is_array( $var ) && ! in_array( $var, $this->query_vars, true ) ) {
$this->query_vars[] = $var;
}
if ( ! empty( $var ) && is_array( $var ) ) {
foreach ( $var as $item ) {
if ( ! in_array( $item, $this->query_vars, true ) ) {
$this->query_vars[] = $item;
}
}
}
return $this->query_vars;
}
/**
* Verify vars
*
* @return array
*/
protected function verify_query_vars(): array {
$methods = array(
'post' => $_POST,
'get' => $_GET,
);
$_query_data = array();
foreach ( $methods as $method => $_vars ) {
$_query_data[ $method ] = array();
if ( isset( $_vars[ $this->nonce_name ] ) ) {
if ( ! wp_verify_nonce( $_vars[ $this->nonce_name ], $this->nonce_action ) ) {
$_query_data = array( $method => 'nonce error' );
continue;
}
}
foreach ( $this->query_vars as $query_var ) {
if ( empty( $_vars[ $query_var ] ) ) {
continue;
}
$_query_data[ $method ][ $query_var ] =
true === $this->sanitizing ? sanitize_text_field( $_vars[ $query_var ] ) : $_vars[ $query_var ];
}
}
return $this->query_data = $_query_data;
}
}