License.php
8.56 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
declare( strict_types=1 );
namespace ACP\Admin\Page;
use AC\Admin\RenderableHead;
use AC\Asset;
use AC\Asset\Assets;
use AC\Asset\Location;
use AC\Asset\Style;
use AC\PluginInformation;
use AC\Renderable;
use AC\Type\Url;
use AC\View;
use ACP;
use ACP\Access\ActivationStorage;
use ACP\Access\PermissionsStorage;
use ACP\ActivationTokenFactory;
use ACP\LicenseKeyRepository;
use ACP\Type\LicenseKey;
use ACP\Type\SiteUrl;
use ACP\Type\Url\Changelog;
class License implements Asset\Enqueueables, Renderable, RenderableHead {
public const NAME = 'license';
/**
* @var Location\Absolute
*/
private $location;
/**
* @var Renderable
*/
private $head;
/**
* @var SiteUrl
*/
private $site_url;
/**
* @var ActivationTokenFactory
*/
private $activation_token_factory;
/**
* @var ActivationStorage
*/
private $activation_storage;
/**
* @var PermissionsStorage
*/
private $permission_storage;
/**
* @var LicenseKeyRepository
*/
private $license_key_repository;
/**
* @var bool
*/
private $network_active;
public function __construct(
Location\Absolute $location,
Renderable $head,
SiteUrl $site_url,
ActivationTokenFactory $activation_token_factory,
ActivationStorage $activation_storage,
PermissionsStorage $permission_storage,
LicenseKeyRepository $license_key_repository,
bool $network_active
) {
$this->location = $location;
$this->head = $head;
$this->site_url = $site_url;
$this->activation_token_factory = $activation_token_factory;
$this->activation_storage = $activation_storage;
$this->permission_storage = $permission_storage;
$this->license_key_repository = $license_key_repository;
$this->network_active = $network_active;
}
public function render_head() {
return $this->head;
}
public function get_assets() {
return new Assets( [
new Style( 'acp-license-manager', $this->location->with_suffix( 'assets/core/css/license-manager.css' ) ),
new ACP\Asset\Script\LicenseManager( $this->location->with_suffix( 'assets/core/js/license-manager.js' ) ),
] );
}
/**
* @param string $plugin_name
*
* @return Changelog
*/
private function get_changelog_url( $plugin_name ): Changelog {
return new Changelog( $this->network_active, $plugin_name );
}
private function show_render_section_updates(): bool {
// update section is hidden on subsites
if ( is_multisite() && ! is_network_admin() ) {
return false;
}
return true;
}
public function render() {
if ( $this->network_active && ! is_network_admin() ) {
return $this->render_network_message();
}
$view = new View( [
'section_license' => $this->render_license_section(),
'section_updates' => $this->show_render_section_updates() ? $this->render_section_updates() : '',
] );
return $view->set_template( 'admin/page/license' );
}
private function render_section_updates(): View {
$content = '';
$updates_available = false;
$updates_available_with_package = false;
$plugin = PluginInformation::create_by_file( ACP_FILE );
$plugin_update = $plugin->get_update();
$content .= $this->render_section_update( $plugin )->render();
if ( $plugin_update ) {
$updates_available = true;
if ( $plugin_update->has_package() ) {
$updates_available_with_package = true;
}
}
$has_token = null !== $this->activation_token_factory->create();
$has_update_permission = $this->permission_storage->retrieve()->has_updates_permission();
$show_update_now_button = $has_token && $updates_available_with_package && $has_update_permission;
$view = new View( [
'title' => __( 'Updates', 'codepress-admin-columns' ),
'content' => $content,
'button_update_now' => $show_update_now_button,
'button_update_now_disabled' => ! $show_update_now_button && $updates_available,
'button_check_for_updates' => ! $updates_available,
] );
return $view->set_template( 'admin/section-updates' );
}
private function render_section_update( PluginInformation $plugin ) {
$update_ready = $plugin->has_update() && $plugin->get_update()->has_package() && current_user_can( 'update_plugins' );
$view = new View( [
'plugin_update_basename' => $plugin->get_basename(),
'plugin_update_slug' => $plugin->get_dirname(),
'plugin_update_nonce' => wp_create_nonce( 'updates' ),
'plugin_update_ready' => $update_ready,
'plugin_label' => $plugin->get_name(),
'current_version' => $plugin->get_version()->get_value(),
'available_version' => $plugin->has_update() ? $plugin->get_update()->get_version()->get_value() : null,
'changelog_link' => $this->get_changelog_url( $plugin->get_dirname() )->get_url(),
] );
return $view->set_template( 'admin/section-update' );
}
private function get_inline_notice() {
$permissions = $this->permission_storage->retrieve();
$description = null;
if ( ! $permissions->has_usage_permission() ) {
$description = __( 'Enter your license code to receive automatic updates.', 'codepress-admin-columns' );
}
if ( ! $permissions->has_updates_permission() ) {
$description = sprintf(
__( 'Enter your license key to %s.', 'codepress-admin-columns' ),
sprintf(
'<strong>%s</strong>',
__( 'unlock all settings', 'codepress-admin-columns' )
)
);
}
return $description
? sprintf( '%s %s', ac_helper()->icon->dashicon( [ 'icon' => 'info-outline', 'class' => 'orange' ] ), $description )
: null;
}
private function render_network_message() {
$page = __( 'network settings page', 'codepress-admin-columns' );
if ( current_user_can( 'manage_network_options' ) ) {
$url = new Url\EditorNetwork( 'license' );
$page = sprintf( '<a href="%s">%s</a>', $url->get_url(), $page );
}
$content = sprintf(
__( 'The license can be managed on the %s.', 'codepress-admin-columns' ),
$page
);
$inline_notice = $this->get_inline_notice();
if ( $inline_notice ) {
$content = sprintf( '%s %s', $inline_notice, $content );
}
$view = new View( [
'title' => __( 'License', 'codepress-admin-columns' ),
'content' => sprintf( '<p>%s</p>', $content ),
'class' => '-license',
] );
return $view->set_template( 'admin/page/settings-section' );
}
private function render_license_section(): View {
$account_url = new Url\UtmTags( new Url\Site( Url\Site::PAGE_ACCOUNT_SUBSCRIPTIONS ), 'license-activation' );
$license_key = $this->license_key_repository->find();
if ( $license_key ) {
$account_url->add( [
'subscription_key' => $license_key->get_token(),
'site_url' => $this->site_url->get_url(),
] );
}
$activation_token = $this->activation_token_factory->create();
$activation = $activation_token
? $this->activation_storage->find( $activation_token )
: null;
$permissions = $this->permission_storage->retrieve();
$is_expired = $activation && $activation->is_expired();
// Give auto-renewal 2 extra days before marked as expired
if ( $is_expired && $activation->is_auto_renewal() && $activation->get_expiry_date()->get_expired_seconds() < ( 2 * DAY_IN_SECONDS ) ) {
$is_expired = false;
}
$updates_enabled = $activation && $activation->is_active() && ! $is_expired && $permissions->has_updates_permission();
$license_info = new View( [
'nonce_field' => ( new ACP\Nonce\LicenseNonce() )->create_field(),
'updates_disabled' => ! $updates_enabled,
'updates_enabled' => $updates_enabled,
'is_expired' => $is_expired,
'expiry_date' => $activation && $activation->get_expiry_date()->exists() ? ac_format_date( 'F j, Y', $activation->get_expiry_date()->get_value()->getTimestamp() ) : false,
'is_cancelled' => $activation && $activation->is_cancelled(),
'is_active' => $activation && $activation->is_active(),
'is_license_defined' => $license_key && LicenseKey::SOURCE_CODE === $license_key->get_source(),
'license_key' => $license_key ? $license_key->get_token() : false,
'has_activation' => null !== $activation,
'has_usage_permission' => $permissions->has_usage_permission(),
'my_account_link' => $account_url->get_url(),
'subscription_documentation_link' => ( new Url\Documentation( Url\Documentation::ARTICLE_SUBSCRIPTION_QUESTIONS ) ),
] );
$view = new View( [
'title' => __( 'License', 'codepress-admin-columns' ),
'content' => $license_info->set_template( 'admin/section-license' ),
'class' => '-license',
] );
return $view->set_template( 'admin/page/settings-section' );
}
}