Activation.php
5.3 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
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
193
194
195
196
<?php
namespace ACP\Check;
use AC\Admin\Page\Addons;
use AC\Admin\Page\Columns;
use AC\Admin\Page\Settings;
use AC\Ajax;
use AC\Capabilities;
use AC\Entity\Plugin;
use AC\Message;
use AC\Registerable;
use AC\Screen;
use AC\Storage;
use AC\Type\Url;
use ACP\Access\ActivationStorage;
use ACP\Access\Permissions;
use ACP\Access\PermissionsStorage;
use ACP\ActivationTokenFactory;
use ACP\Admin\Page\License;
use ACP\Admin\Page\Tools;
class Activation
implements Registerable
{
private $plugin;
private $activation_token_factory;
private $activation_storage;
private $permission_storage;
public function __construct(
Plugin $plugin,
ActivationTokenFactory $activation_token_factory,
ActivationStorage $activation_storage,
PermissionsStorage $permission_storage
) {
$this->plugin = $plugin;
$this->activation_token_factory = $activation_token_factory;
$this->activation_storage = $activation_storage;
$this->permission_storage = $permission_storage;
}
public function register(): void
{
add_action('ac/screen', [$this, 'register_notice']);
$this->get_ajax_handler()->register();
}
/**
* @return Ajax\Handler
*/
private function get_ajax_handler()
{
$handler = new Ajax\Handler();
$handler
->set_action('ac_notice_dismiss_activation')
->set_callback([$this, 'ajax_dismiss_notice']);
return $handler;
}
/**
* @param Screen $screen
*/
public function register_notice(Screen $screen)
{
if ( ! $screen->has_screen() || ! current_user_can(Capabilities::MANAGE)) {
return;
}
switch (true) {
case $screen->is_plugin_screen() && $this->show_message() :
$notice = new Message\Plugin(
$this->get_message(),
$this->plugin->get_basename(),
Message::INFO
);
$notice->register();
break;
case (
$screen->is_admin_screen(Settings::NAME) ||
$screen->is_admin_screen(Columns::NAME) ||
$screen->is_admin_screen(Tools::NAME) ||
$screen->is_admin_screen(Addons::NAME) ||
$screen->is_admin_screen(License::NAME)) && $this->show_message() :
$notice = new Message\Notice($this->get_message());
$notice
->set_type(Message::INFO)
->register();
break;
case $screen->is_list_screen() && $this->get_dismiss_option()->is_expired() && $this->show_message() :
// Dismissible message on list table
$notice = new Message\Notice\Dismissible($this->get_message(), $this->get_ajax_handler());
$notice
->set_type(Message::INFO)
->register();
break;
}
}
/**
* @return bool
*/
private function show_message()
{
// We send a different (locked) message when a use has no usage permissions
$has_usage = $this->permission_storage->retrieve()->has_permission(Permissions::USAGE);
if ( ! $has_usage) {
return false;
}
$token = $this->activation_token_factory->create();
$activation = $token ? $this->activation_storage->find($token) : null;
if ( ! $activation) {
return true;
}
// An expired license has its own message
if ($activation->is_expired()) {
return false;
}
return ! $activation->is_active();
}
/**
* @return Url
*/
private function get_license_page_url()
{
return $this->plugin->is_network_active()
? new Url\EditorNetwork('license')
: new Url\Editor('license');
}
/**
* @return Url
*/
private function get_account_url()
{
return new Url\UtmTags(new Url\Site(Url\Site::PAGE_ACCOUNT_SUBSCRIPTIONS), 'license-activation');
}
/**
* @return string
*/
private function get_message()
{
return sprintf(
'%s %s',
sprintf(
__(
"To enable automatic updates for %s, <a href='%s'>enter your license key</a>.",
'codepress_admin_columns'
),
'Admin Columns Pro',
esc_url($this->get_license_page_url()->get_url())
),
sprintf(
__('You can find your license key on your %s.', 'codepress-admin-columns'),
sprintf(
'<a href="%s" target="_blank">%s</a>',
esc_url($this->get_account_url()->get_url()),
__('account page', 'codepress-admin-columns')
)
)
);
}
/**
* @return Storage\Timestamp
*/
private function get_dismiss_option()
{
return new Storage\Timestamp(
new Storage\UserMeta('ac_notice_dismiss_activation')
);
}
public function ajax_dismiss_notice()
{
$this->get_ajax_handler()->verify_request();
$this->get_dismiss_option()->save(time() + (MONTH_IN_SECONDS * 2));
exit;
}
}