LockedSettings.php
3.81 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
<?php
namespace ACP\Check;
use AC;
use AC\Capabilities;
use AC\Message;
use AC\Message\Notice;
use AC\Registerable;
use AC\Screen;
use AC\Type\Url;
use ACP\Access\PermissionsStorage;
use ACP\Admin\Page;
class LockedSettings implements Registerable {
/**
* @var string
*/
private $plugin_basename;
/**
* @var PermissionsStorage
*/
private $permission_storage;
/**
* @var bool
*/
private $is_network_active;
public function __construct( $plugin_basename, PermissionsStorage $permission_storage, $is_network_active ) {
$this->plugin_basename = (string) $plugin_basename;
$this->permission_storage = $permission_storage;
$this->is_network_active = (bool) $is_network_active;
}
public function register() {
add_action( 'ac/screen', [ $this, 'register_notice' ] );
}
/**
* @return Url
*/
private function get_license_page_url() {
return $this->is_network_active
? new Url\EditorNetwork( 'license' )
: new Url\Editor( 'license' );
}
private function get_message() {
return sprintf(
'%s %s',
sprintf( '%s is not yet activated.', 'Admin Columns Pro' ),
sprintf(
__( "Go to the %s and activate Admin Columns Pro to start using the plugin.", 'codepress_admin_columns' ),
sprintf(
'<a href="%s">%s</a>',
esc_url( $this->get_license_page_url()->get_url() ),
__( 'license page', 'codepress_admin_columns' )
)
)
);
}
private function get_inline_plugin_message() {
return sprintf( '%s %s',
sprintf(
__( '%s is not yet activated, please %s.', 'codepress_admin_columns' ),
'Admin Columns Pro',
sprintf(
'<a href="%s">%s</a>',
esc_url( $this->get_license_page_url()->get_url() ),
__( 'enter your license key', 'codepress_admin_columns' )
)
),
$this->get_message_account_page()
);
}
private function missing_usage_permission() {
return ! $this->permission_storage->retrieve()->has_usage_permission();
}
private function get_account_url() {
return new Url\UtmTags( new Url\Site( Url\Site::PAGE_ACCOUNT_SUBSCRIPTIONS ), 'license-activation' );
}
private function get_message_account_page() {
return 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' )
)
);
}
private function get_license_page_message() {
$documentation = sprintf(
'<a href="%s" target="_blank">%s</a>',
( new Url\Documentation( Url\Documentation::ARTICLE_SUBSCRIPTION_QUESTIONS ) )->get_url(),
sprintf( __( 'activating %s', 'codepress-admin-columns' ), 'Admin Columns Pro' )
);
$parts = [
__( 'To start using Admin Columns Pro, fill in your license key below.', 'codepress-admin-columns' ),
sprintf( __( 'Read more about %s.' ), $documentation ),
];
return implode( ' ', $parts );
}
public function register_notice( Screen $screen ) {
if ( ! current_user_can( Capabilities::MANAGE ) || ! $screen->has_screen() ) {
return;
}
switch ( true ) {
case $screen->is_plugin_screen() && $this->missing_usage_permission() :
$notice = new Message\Plugin(
$this->get_inline_plugin_message(),
$this->plugin_basename,
Message::WARNING
);
$notice->register();
break;
case $screen->is_admin_screen( Page\License::NAME ) && $this->missing_usage_permission() :
$notice = new Notice(
$this->get_license_page_message(),
Message::ERROR
);
$notice->register();
break;
case ( $screen->is_admin_screen( AC\Admin\Page\Columns::NAME ) || $screen->is_admin_screen( Page\Tools::NAME ) || $screen->is_admin_screen( AC\Admin\Page\Settings::NAME ) ) && $this->missing_usage_permission() :
$notice = new Notice(
$this->get_message(),
Message::ERROR
);
$notice->register();
break;
}
}
}