LicenseUpdate.php
1.68 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
<?php
namespace ACP\RequestHandler;
use AC\Capabilities;
use AC\Message;
use AC\Message\Notice;
use AC\Request;
use ACP\Access\ActivationUpdater;
use ACP\ActivationTokenFactory;
use ACP\Nonce\LicenseNonce;
use ACP\RequestHandler;
class LicenseUpdate implements RequestHandler
{
/**
* @var ActivationTokenFactory
*/
private $activation_token_factory;
/**
* @var ActivationUpdater
*/
private $activation_updater;
public function __construct(ActivationTokenFactory $activation_token_factory, ActivationUpdater $activation_updater)
{
$this->activation_token_factory = $activation_token_factory;
$this->activation_updater = $activation_updater;
}
public function handle(Request $request): void
{
if ( ! current_user_can(Capabilities::MANAGE)) {
return;
}
if ( ! (new LicenseNonce())->verify($request)) {
return;
}
$token = $this->activation_token_factory->create();
if ( ! $token) {
$this->error_notice('Missing activation token.');
return;
}
$api_response = $this->activation_updater->update($token);
if ($api_response->has_error()) {
$this->error_notice($api_response->get_error()->get_error_message());
return;
}
$this->success_notice(__('License information has been updated.', 'codepress-admin-columns'));
}
private function error_notice(string $message): void
{
(new Notice($message))->set_type(Message::ERROR)->register();
}
private function success_notice(string $message): void
{
(new Notice($message))->register();
}
}