-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWpNonce.php
192 lines (168 loc) · 4.29 KB
/
WpNonce.php
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
namespace mndev\WpNonce;
/**
* Class WpNonce
* Ein Objektorientierter Ansatz um Wordpress Nonces zu nutzen.
* Inspiriert von https://github.com/christophwolff/WPnonce
*
* @package mndev\WpNonce
* @version 0.0.1
**/
class WpNonce
{
protected static $_instance = null;
private $action;
private function __construct() {}
/**
* __clone verboten
*/
private function __clone() {}
/**
* Wakeup unterbinden
*
* @throws Exception
*/
public function __wakeup()
{
throw new Exception("Cannot unserialize singleton");
}
/**
* Singleton
*
* Einzige Instanz die einen Zugriff auf die Methoden ermöglicht.
*
* @return WpNonce|null
*/
public static function getInstance($action)
{
self::setAction($action);
if (self::$_instance === null) {
self::$_instance = new self;
}
return self::$_instance;
}
/**
* @return string
*/
public function setAction($action)
{
return $this->action = $action;
}
/**
* @return string
*/
public function getAction()
{
return $this->action;
}
/**
* Einen Nonce für die weitere Verwendung erzeugen
*
* Codex-Dokumentation:
* @see https://codex.wordpress.org/Function_Reference/wp_create_nonce
*
* @return bool|string Nonce
*/
public function createNonce()
{
if (!function_exists('wp_create_nonce')) {
return false;
}
return wp_create_nonce($this->action);
}
/**
* Überprüfung eines Nonce
*
* Codex-Dokumentation:
* @see https://codex.wordpress.org/Function_Reference/wp_verify_nonce
*
* @param $nonce
* @return bool
*/
public function verifyNonce($nonce)
{
if (!function_exists('wp_verify_nonce')
|| empty($nonce)
|| !is_string($nonce)
) {
return false;
}
return wp_verify_nonce($nonce, $this->action);
}
/**
* Erzeugt ein Nonce HTML Feld für die Nutzung im Formular
*
* Codex-Dokumentation:
* @see https://codex.wordpress.org/Function_Reference/wp_nonce_field
*
* @param $name
* @param bool $referer
*
* @return bool | html
*/
public function createNonceField($name, $referer = true)
{
if (!function_exists('wp_nonce_field')
|| empty($name)
|| !is_string($name)
) {
return false;
}
return wp_nonce_field($this->action, $name, $referer, false);
}
/**
* Erzeugt aus einer "normalen" URL eine Nonce enthaltende URL
*
* Codex-Dokumentation:
* @see https://codex.wordpress.org/Function_Reference/wp_nonce_url
*
* @param $actionUrl
*
* @param string $name
* @return bool
*/
public function createNonceUrl($actionUrl, $name = '_wpnonce')
{
if (!function_exists('wp_nonce_url')
|| empty($actionUrl)
|| !is_string($actionUrl)
) {
return false;
}
return wp_nonce_url($actionUrl, $this->action, $name);
}
/**
* Überprüft ob die übergebene URL eine valide URL mit Nounce ist (Admin)
*
* Codex-Dokumentation:
* @see https://codex.wordpress.org/Function_Reference/check_admin_referer
*
* @param string $query_arg
* @return bool
*/
public function checkAdminReferer($query_arg = '_wpnonce')
{
if (!function_exists('check_admin_referer')) {
return false;
}
return check_admin_referer($this->action, $query_arg);
}
/**
* Überprüft ob die übergebene URL eine valide URL mit Nounce ist (Ajax)
*
* Codex-Dokumentation:
* @see https://codex.wordpress.org/Function_Reference/check_ajax_referer
*
* @param string $query_arg
* @param $die (whether to die if the nonce is invalid)
*
* @return bool
*/
public function checkAjaxReferer($query_arg = '_wpnonce', $die)
{
if (!function_exists('check_ajax_referer')) {
return false;
}
return check_ajax_referer($this->action, $query_arg, $die);
}
}