/
WPNonce.php
64 lines (50 loc) · 1.62 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
<?php
declare(strict_types=1);
namespace Snicco\Middleware\WPNonce;
use Snicco\Component\BetterWPAPI\BetterWPAPI;
use Snicco\Component\HttpRouting\Routing\Exception\RouteNotFound;
use Snicco\Component\HttpRouting\Routing\UrlGenerator\UrlGenerator;
use function htmlentities;
use function ltrim;
use const ENT_QUOTES;
final class WPNonce
{
private UrlGenerator $generator;
private string $current_path;
private BetterWPAPI $wp;
public function __construct(UrlGenerator $generator, BetterWPAPI $wp, string $current_path)
{
$this->generator = $generator;
$this->current_path = $current_path;
$this->wp = $wp;
}
/**
* @param array<string,int|string> $args
*/
public function __invoke(string $route_name = null, array $args = []): string
{
if (null === $route_name) {
$nonce_action = $this->current_path;
} else {
try {
$nonce_action = $this->generator->toRoute($route_name, $args);
} catch (RouteNotFound $e) {
$nonce_action = '/' . ltrim($route_name, '/');
}
}
return $this->createNonce($nonce_action);
}
/**
* @psalm-suppress DeprecatedClass
*/
private function createNonce(string $nonce_action): string
{
$nonce = $this->noHtml($this->wp->createNonce($nonce_action));
$name = VerifyWPNonce::inputKey();
return sprintf("<input type='hidden' name='%s' value='%s'>", $name, $nonce);
}
private function noHtml(string $nonce): string
{
return htmlentities($nonce, ENT_QUOTES, 'UTF-8');
}
}