-
Notifications
You must be signed in to change notification settings - Fork 1
/
Authorize_Button_Controller.php
219 lines (190 loc) · 5.64 KB
/
Authorize_Button_Controller.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
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
<?php declare( strict_types=1 );
namespace StellarWP\Uplink\Components\Admin;
use InvalidArgumentException;
use StellarWP\Uplink\Auth\Admin\Disconnect_Controller;
use StellarWP\Uplink\Auth\Auth_Url_Builder;
use StellarWP\Uplink\Auth\Authorizer;
use StellarWP\Uplink\Auth\Token\Contracts\Token_Manager;
use StellarWP\Uplink\Components\Controller;
use StellarWP\Uplink\Config;
use StellarWP\Uplink\Resources\Collection;
use StellarWP\Uplink\View\Contracts\View;
final class Authorize_Button_Controller extends Controller {
/**
* The view file, without ext, relative to the root views directory.
*/
public const VIEW = 'admin/authorize-button';
/**
* @var Authorizer
*/
private $authorizer;
/**
* @var Token_Manager
*/
private $token_manager;
/**
* @var Auth_Url_Builder
*/
private $url_builder;
/**
* @var Collection
*/
private $resources;
/**
* @var Disconnect_Controller
*/
private $disconnect_controller;
/**
* @param View $view The View Engine to render views.
* @param Authorizer $authorizer Determines if the current user can perform actions.
* @param Token_Manager $token_manager The Token Manager.
* @param Auth_Url_Builder $url_builder The Auth URL Builder.
*/
public function __construct(
View $view,
Authorizer $authorizer,
Token_Manager $token_manager,
Auth_Url_Builder $url_builder,
Collection $resources,
Disconnect_Controller $disconnect_controller
) {
parent::__construct( $view );
$this->authorizer = $authorizer;
$this->token_manager = $token_manager;
$this->url_builder = $url_builder;
$this->resources = $resources;
$this->disconnect_controller = $disconnect_controller;
}
/**
* Renders the authorize-button view.
*
* @param array{slug?: string, domain?: string, license?: string} $args The Product slug and license domain.
*
* @see src/views/admin/authorize-button.php
*
* @throws InvalidArgumentException
*/
public function render( array $args = [] ): void {
global $pagenow;
$slug = $args['slug'] ?? '';
$domain = $args['domain'] ?? '';
$license = $args['license'] ?? '';
if ( empty ( $slug ) ) {
throw new InvalidArgumentException( __( 'The Product slug cannot be empty', '%TEXTDOMAIN%' ) );
}
$url = $this->url_builder->set_license( $license )->build( $slug, $domain );
if ( ! $url ) {
return;
}
$plugin = $this->resources->offsetGet( $slug );
if ( ! $plugin ) {
return;
}
$authenticated = false;
$target = '_blank';
$link_text = __( 'Connect', '%TEXTDOMAIN%' );
$classes = [
'button',
'uplink-authorize',
'not-authorized',
];
if ( ! $this->authorizer->can_auth() ) {
$target = '_self';
$link_text = __( 'Contact your network administrator to connect', '%TEXTDOMAIN%' );
$url = get_admin_url( get_current_blog_id(), 'network/' );
} elseif ( $this->token_manager->get( $plugin ) ) {
$authenticated = true;
$target = '_self';
$link_text = __( 'Disconnect', '%TEXTDOMAIN%' );
$url = $this->disconnect_controller->get_url( $plugin );
$classes[2] = 'authorized';
}
$hook_prefix = Config::get_hook_prefix();
/**
* Filter the link text.
*
* @param string $link_text The current link text.
* @param bool $authenticated Whether they are authenticated.
* @param string|null $pagenow The value of WordPress's pagenow.
*/
$link_text = apply_filters(
"stellarwp/uplink/$hook_prefix/view/authorize_button/link_text",
$link_text,
$authenticated,
$pagenow
);
/**
* Filter the link text for the slug.
*
* @param string $link_text The current link text.
* @param bool $authenticated Whether they are authenticated.
* @param string|null $pagenow The value of WordPress's pagenow.
*/
$link_text = apply_filters(
"stellarwp/uplink/$hook_prefix/$slug/view/authorize_button/link_text",
$link_text,
$authenticated,
$pagenow
);
/**
* Filter the hyperlink url.
*
* @param string $url The current hyperlink url.
* @param bool $authenticated Whether they are authenticated.
* @param string|null $pagenow The value of WordPress's pagenow.
*/
$url = apply_filters(
"stellarwp/uplink/$hook_prefix/view/authorize_button/url",
$url,
$authenticated,
$pagenow
);
/**
* Filter the link target.
*
* @param string $target The current link target.
* @param bool $authenticated Whether they are authenticated.
* @param string|null $pagenow The value of WordPress's pagenow.
*/
$target = apply_filters(
"stellarwp/uplink/$hook_prefix/view/authorize_button/target",
$target,
$authenticated,
$pagenow
);
/**
* Filter the HTML wrapper tag.
*
* @param string $tag The HTML tag to use for the wrapper.
* @param bool $authenticated Whether they are authenticated.
* @param string|null $pagenow The value of WordPress's pagenow.
*/
$tag = apply_filters(
"stellarwp/uplink/$hook_prefix/view/authorize_button/tag",
'div',
$authenticated,
$pagenow
);
/**
* Filter the CSS classes
*
* @param array $classes An array of CSS classes.
* @param bool $authenticated Whether they are authenticated.
* @param string|null $pagenow The value of WordPress's pagenow.
*/
$classes = (array) apply_filters(
"stellarwp/uplink/$hook_prefix/view/authorize_button/classes",
$classes,
$authenticated,
$pagenow
);
echo $this->view->render( self::VIEW, [
'link_text' => $link_text,
'url' => $url,
'target' => $target,
'tag' => $tag,
'classes' => $this->classes( $classes ),
'slug' => $slug,
] );
}
}