1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
4
|
|
|
include_once ABSPATH . 'wp-admin/includes/file.php'; |
5
|
|
|
|
6
|
|
|
class Jetpack_JSON_API_Plugins_Install_Endpoint extends Jetpack_JSON_API_Plugins_Endpoint { |
7
|
|
|
|
8
|
|
|
// POST /sites/%s/plugins/%s/install |
9
|
|
|
protected $needed_capabilities = 'install_plugins'; |
10
|
|
|
protected $action = 'install'; |
11
|
|
|
protected $download_links = array(); |
12
|
|
|
|
13
|
|
|
protected function install() { |
14
|
|
|
foreach ( $this->plugins as $index => $slug ) { |
15
|
|
|
|
16
|
|
|
$skin = new Jetpack_Automatic_Plugin_Install_Skin(); |
17
|
|
|
$upgrader = new Plugin_Upgrader( $skin ); |
18
|
|
|
|
19
|
|
|
$result = $upgrader->install( $this->download_links[ $slug ] ); |
20
|
|
|
|
21
|
|
|
if ( ! $this->bulk && is_wp_error( $result ) ) { |
22
|
|
|
return $result; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$plugin = self::get_plugin_id_by_slug( $slug ); |
26
|
|
|
$error_code = 'install_error'; |
27
|
|
View Code Duplication |
if ( ! $plugin ) { |
28
|
|
|
$error = $this->log[ $slug ]['error'] = __( 'There was an error installing your plugin', 'jetpack' ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
if ( ! $this->bulk && ! $result ) { |
32
|
|
|
$error_code = $upgrader->skin->get_main_error_code(); |
33
|
|
|
$message = $upgrader->skin->get_main_error_message(); |
34
|
|
|
$error = $this->log[ $slug ]['error'] = $message ? $message : __( 'An unknown error occurred during installation' , 'jetpack' ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$this->log[ $plugin ][] = $upgrader->skin->get_upgrade_messages(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ( ! $this->bulk && isset( $error ) ) { |
41
|
|
|
return new WP_Error( $error_code, $this->log[ $slug ]['error'], 400 ); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// replace the slug with the actual plugin id |
45
|
|
|
$this->plugins[ $index ] = $plugin; |
|
|
|
|
46
|
|
|
|
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function validate_plugins() { |
51
|
|
View Code Duplication |
if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) { |
52
|
|
|
return new WP_Error( 'missing_plugins', __( 'No plugins found.', 'jetpack' ) ); |
53
|
|
|
} |
54
|
|
|
foreach( $this->plugins as $index => $slug ) { |
55
|
|
|
|
56
|
|
|
// make sure it is not already installed |
57
|
|
|
if ( self::get_plugin_id_by_slug( $slug ) ) { |
58
|
|
|
return new WP_Error( 'plugin_already_installed', __( 'The plugin is already installed', 'jetpack' ) ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$response = wp_remote_get( "http://api.wordpress.org/plugins/info/1.0/$slug" ); |
62
|
|
|
$plugin_data = unserialize( $response['body'] ); |
63
|
|
|
if ( is_wp_error( $plugin_data ) ) { |
64
|
|
|
return $plugin_data; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->download_links[ $slug ] = $plugin_data->download_link; |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected static function get_plugin_id_by_slug( $slug ) { |
74
|
|
|
$plugins = get_plugins(); |
75
|
|
|
if ( ! is_array( $plugins ) ) { |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
foreach( $plugins as $id => $plugin_data ) { |
79
|
|
|
if ( strpos( $id, $slug ) !== false ) { |
80
|
|
|
return $id; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
/** |
87
|
|
|
* Allows us to capture that the site doesn't have proper file system access. |
88
|
|
|
* In order to update the plugin. |
89
|
|
|
*/ |
90
|
|
|
class Jetpack_Automatic_Plugin_Install_Skin extends Automatic_Upgrader_Skin { |
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Stores the last error key; |
93
|
|
|
**/ |
94
|
|
|
protected $main_error_code = 'install_error'; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Stores the last error message. |
98
|
|
|
**/ |
99
|
|
|
protected $main_error_message = 'An unknown error occurred during installation'; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Overwrites the set_upgrader to be able to tell if we e ven have the ability to write to the files. |
103
|
|
|
* |
104
|
|
|
* @param WP_Upgrader $upgrader |
105
|
|
|
* |
106
|
|
|
*/ |
107
|
|
|
public function set_upgrader( &$upgrader ) { |
108
|
|
|
parent::set_upgrader( $upgrader ); |
109
|
|
|
|
110
|
|
|
// Check if we even have permission to. |
111
|
|
|
$result = $upgrader->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) ); |
112
|
|
|
if ( ! $result ) { |
113
|
|
|
// set the string here since they are not available just yet |
114
|
|
|
$upgrader->generic_strings(); |
115
|
|
|
$this->feedback( 'fs_unavailable' ); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Overwrites the error function |
121
|
|
|
*/ |
122
|
|
|
public function error( $error ) { |
123
|
|
|
if ( is_wp_error( $error ) ) { |
124
|
|
|
$this->feedback( $error ); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function set_main_error_code( $code ) { |
129
|
|
|
// Don't set the process_failed as code since it is not that helpful unless we don't have one already set. |
130
|
|
|
$this->main_error_code = ( $code === 'process_failed' && $this->main_error_code ? $this->main_error_code : $code ); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private function set_main_error_message( $message, $code ) { |
134
|
|
|
// Don't set the process_failed as message since it is not that helpful unless we don't have one already set. |
135
|
|
|
$this->main_error_message = ( $code === 'process_failed' && $this->main_error_code ? $this->main_error_code : $message ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function get_main_error_code() { |
139
|
|
|
return $this->main_error_code; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function get_main_error_message() { |
143
|
|
|
return $this->main_error_message; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Overwrites the feedback function |
148
|
|
|
*/ |
149
|
|
|
public function feedback( $data ) { |
150
|
|
|
|
151
|
|
|
$current_error = null; |
152
|
|
|
if ( is_wp_error( $data ) ) { |
153
|
|
|
$this->set_main_error_code( $data->get_error_code() ); |
154
|
|
|
$string = $data->get_error_message(); |
155
|
|
|
} elseif ( is_array( $data ) ) { |
156
|
|
|
return; |
157
|
|
|
} else { |
158
|
|
|
$string = $data; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
if ( ! empty( $this->upgrader->strings[ $string ] ) ) { |
162
|
|
|
$this->set_main_error_code( $string ); |
163
|
|
|
|
164
|
|
|
$current_error = $string; |
165
|
|
|
$string = $this->upgrader->strings[ $string ]; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
if ( strpos( $string, '%' ) !== false ) { |
169
|
|
|
$args = func_get_args(); |
170
|
|
|
$args = array_splice( $args, 1 ); |
171
|
|
|
if ( ! empty( $args ) ) |
172
|
|
|
$string = vsprintf( $string, $args ); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$string = trim( $string ); |
176
|
|
|
$string = wp_kses( $string, array( |
177
|
|
|
'a' => array( |
178
|
|
|
'href' => true |
179
|
|
|
), |
180
|
|
|
'br' => true, |
181
|
|
|
'em' => true, |
182
|
|
|
'strong' => true, |
183
|
|
|
) ); |
184
|
|
|
|
185
|
|
|
$this->set_main_error_message( $string, $current_error ); |
186
|
|
|
$this->messages[] = $string; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
It seems like you are relying on a variable being defined by an iteration: