AddonInstaller.php
4.39 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
<?php
namespace ACP\RequestHandler\Ajax;
use AC\Capabilities;
use AC\IntegrationRepository;
use AC\Nonce;
use AC\PluginInformation;
use AC\Request;
use AC\Type\Url;
use ACP\Access\ActivationStorage;
use ACP\ActivationTokenFactory;
use ACP\API;
use ACP\RequestAjaxHandler;
use ACP\RequestDispatcher;
use ACP\Type\SiteUrl;
use Plugin_Upgrader;
use WP_Ajax_Upgrader_Skin;
use WP_Error;
class AddonInstaller implements RequestAjaxHandler {
/**
* @var RequestDispatcher
*/
private $api;
/**
* @var SiteUrl
*/
private $site_url;
/**
* @var ActivationStorage
*/
private $activation_storage;
/**
* @var ActivationTokenFactory
*/
private $activation_token_factory;
/**
* @var IntegrationRepository
*/
private $integrations;
/**
* @var bool
*/
private $is_network_active;
public function __construct( RequestDispatcher $api, SiteUrl $site_url, ActivationStorage $activation_storage, ActivationTokenFactory $activation_token_factory, IntegrationRepository $integrations, $is_network_active ) {
$this->api = $api;
$this->site_url = $site_url;
$this->activation_storage = $activation_storage;
$this->activation_token_factory = $activation_token_factory;
$this->integrations = $integrations;
$this->is_network_active = (bool) $is_network_active;
}
public function handle() {
$request = new Request();
if ( ! current_user_can( Capabilities::MANAGE ) ) {
return;
}
if ( ! ( new Nonce\Ajax() )->verify( $request ) ) {
wp_send_json_error();
}
$plugin_slug = $request->get( 'plugin_name' );
$network_wide = '1' === $request->get( 'network_wide' );
$integration = $this->integrations->find_by_slug( $plugin_slug );
if ( ! $integration ) {
wp_send_json_error( 'Invalid plugin.' );
}
$token = $this->activation_token_factory->create();
$activation = $token
? $this->activation_storage->find( $token )
: null;
$plugin = new PluginInformation( $integration->get_basename() );
// Install
if ( ! $plugin->is_installed() ) {
if ( ! current_user_can( 'install_plugins' ) ) {
wp_send_json_error( 'User does not have the permission to install plugin.' );
}
if ( ! $activation || ! $activation->is_active() ) {
$message = sprintf(
'%s %s',
__( 'License is not active.', 'codepress-admin-columns' ),
sprintf(
__( 'Enter your license key on <a href="%s">the settings page</a>.', 'codepress-admin-columns' ),
esc_url( $this->get_license_page_url()->get_url() )
)
);
wp_send_json_error( $message );
}
$response = $this->api->dispatch( new API\Request\DownloadInformation( $plugin_slug, $token, $this->site_url ) );
if ( $response->has_error() ) {
wp_send_json_error( $response->get_error()->get_error_message() );
}
$result = $this->install_plugin( $response->get( 'download_link' ) );
if ( is_wp_error( $result ) ) {
wp_send_json_error( $result->get_error_message() );
}
if ( ! $result ) {
wp_send_json_error( __( 'Install failed.', 'codepress-admin-columns' ) );
}
}
if ( ! current_user_can( 'activate_plugins' ) ) {
wp_send_json_error( 'User does not have permission to activate plugin.' );
}
// Activate
$is_active = null === activate_plugin( $integration->get_basename(), '', $network_wide );
$status = __( 'Installed', 'codepress-admin-columns' );
if ( $is_active ) {
$status = $network_wide
? __( 'Network Active', 'codepress-admin-columns' )
: __( 'Active', 'codepress-admin-columns' );
}
wp_send_json_success( [
'activated' => $is_active,
'status' => $status,
] );
}
/**
* @return Url
*/
private function get_license_page_url() {
return $this->is_network_active
? new Url\EditorNetwork( 'license' )
: new Url\Editor( 'license' );
}
/**
* @param string $package_url zip file
*
* @return string|WP_Error|false Plugin basename on success. False or WP_Error when failed.
*/
private function install_plugin( $package_url ) {
include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' );
$skin = new WP_Ajax_Upgrader_Skin();
$upgrader = new Plugin_Upgrader( $skin );
$result = $upgrader->install( $package_url );
if ( is_wp_error( $result ) ) {
return $result;
}
if ( $skin->get_errors()->get_error_codes() ) {
return $skin->get_errors();
}
if ( true !== $result ) {
return false;
}
return $upgrader->plugin_info();
}
}