|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class Jetpack_JSON_API_Plugins_Modify_Endpoint extends Jetpack_JSON_API_Plugins_Endpoint { |
|
4
|
|
|
// POST /sites/%s/plugins/%s |
|
5
|
|
|
// POST /sites/%s/plugins |
|
6
|
|
|
protected $slug = null; |
|
7
|
|
|
protected $needed_capabilities = 'activate_plugins'; |
|
8
|
|
|
protected $action = 'default_action'; |
|
9
|
|
|
protected $expected_actions = array( 'update', 'install', 'delete', 'update_translations' ); |
|
10
|
|
|
|
|
11
|
|
|
public function callback( $path = '', $blog_id = 0, $object = null ) { |
|
12
|
|
|
Jetpack_JSON_API_Endpoint::validate_input( $object ); |
|
13
|
|
|
switch ( $this->action ) { |
|
14
|
|
|
case 'delete': |
|
|
|
|
|
|
15
|
|
|
$this->needed_capabilities = 'delete_plugins'; |
|
16
|
|
|
case 'update_translations': |
|
17
|
|
|
case 'update' : |
|
18
|
|
|
$this->needed_capabilities = 'update_plugins'; |
|
19
|
|
|
break; |
|
20
|
|
|
case 'install' : |
|
21
|
|
|
$this->needed_capabilities = 'install_plugins'; |
|
22
|
|
|
break; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
if ( isset( $args['autoupdate'] ) || isset( $args['autoupdate_translations'] ) ) { |
|
|
|
|
|
|
26
|
|
|
$this->needed_capabilities = 'update_plugins'; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return parent::callback( $path, $blog_id, $object ); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function default_action() { |
|
33
|
|
|
$args = $this->input(); |
|
34
|
|
|
|
|
35
|
|
|
if ( isset( $args['autoupdate'] ) && is_bool( $args['autoupdate'] ) ) { |
|
36
|
|
|
if ( $args['autoupdate'] ) { |
|
37
|
|
|
$this->autoupdate_on(); |
|
38
|
|
|
} else { |
|
39
|
|
|
$this->autoupdate_off(); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
if ( isset( $args['active'] ) && is_bool( $args['active'] ) ) { |
|
44
|
|
|
if ( $args['active'] ) { |
|
45
|
|
|
return $this->activate(); |
|
46
|
|
|
} else { |
|
47
|
|
|
return $this->deactivate(); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
View Code Duplication |
if ( isset( $args['autoupdate_translations'] ) && is_bool( $args['autoupdate_translations'] ) ) { |
|
52
|
|
|
if ( $args['autoupdate_translations'] ) { |
|
53
|
|
|
$this->autoupdate_translations_on(); |
|
54
|
|
|
} else { |
|
55
|
|
|
$this->autoupdate_translations_off(); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function autoupdate_on() { |
|
63
|
|
|
$autoupdate_plugins = Jetpack_Options::get_option( 'autoupdate_plugins', array() ); |
|
64
|
|
|
$autoupdate_plugins = array_unique( array_merge( $autoupdate_plugins, $this->plugins ) ); |
|
65
|
|
|
Jetpack_Options::update_option( 'autoupdate_plugins', $autoupdate_plugins ); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function autoupdate_off() { |
|
69
|
|
|
$autoupdate_plugins = Jetpack_Options::get_option( 'autoupdate_plugins', array() ); |
|
70
|
|
|
$autoupdate_plugins = array_diff( $autoupdate_plugins, $this->plugins ); |
|
71
|
|
|
Jetpack_Options::update_option( 'autoupdate_plugins', $autoupdate_plugins ); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected function autoupdate_translations_on() { |
|
75
|
|
|
$autoupdate_plugins = Jetpack_Options::get_option( 'autoupdate_plugins_translations', array() ); |
|
76
|
|
|
$autoupdate_plugins = array_unique( array_merge( $autoupdate_plugins, $this->plugins ) ); |
|
77
|
|
|
Jetpack_Options::update_option( 'autoupdate_plugins_translations', $autoupdate_plugins ); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected function autoupdate_translations_off() { |
|
81
|
|
|
$autoupdate_plugins = Jetpack_Options::get_option( 'autoupdate_plugins_translations', array() ); |
|
82
|
|
|
$autoupdate_plugins = array_diff( $autoupdate_plugins, $this->plugins ); |
|
83
|
|
|
Jetpack_Options::update_option( 'autoupdate_plugins_translations', $autoupdate_plugins ); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function activate() { |
|
87
|
|
|
foreach ( $this->plugins as $plugin ) { |
|
88
|
|
View Code Duplication |
if ( ( ! $this->network_wide && Jetpack::is_plugin_active( $plugin ) ) || is_plugin_active_for_network( $plugin ) ) { |
|
89
|
|
|
$this->log[ $plugin ]['error'] = __( 'The Plugin is already active.', 'jetpack' ); |
|
90
|
|
|
$has_errors = true; |
|
91
|
|
|
continue; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
View Code Duplication |
if ( ! $this->network_wide && is_network_only_plugin( $plugin ) && is_multisite() ) { |
|
95
|
|
|
$this->log[ $plugin ]['error'] = __( 'Plugin can only be Network Activated', 'jetpack' ); |
|
96
|
|
|
$has_errors = true; |
|
97
|
|
|
continue; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$result = activate_plugin( $plugin, '', $this->network_wide ); |
|
101
|
|
|
|
|
102
|
|
View Code Duplication |
if ( is_wp_error( $result ) ) { |
|
103
|
|
|
$this->log[ $plugin ]['error'] = $result->get_error_messages(); |
|
104
|
|
|
$has_errors = true; |
|
105
|
|
|
continue; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$success = Jetpack::is_plugin_active( $plugin ); |
|
109
|
|
|
if ( $success && $this->network_wide ) { |
|
110
|
|
|
$success &= is_plugin_active_for_network( $plugin ); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
View Code Duplication |
if ( ! $success ) { |
|
114
|
|
|
$this->log[ $plugin ]['error'] = $result->get_error_messages; |
|
115
|
|
|
$has_errors = true; |
|
116
|
|
|
continue; |
|
117
|
|
|
} |
|
118
|
|
|
$this->log[ $plugin ][] = __( 'Plugin activated.', 'jetpack' ); |
|
119
|
|
|
} |
|
120
|
|
|
if ( ! $this->bulk && isset( $has_errors ) ) { |
|
121
|
|
|
$plugin = $this->plugins[0]; |
|
122
|
|
|
return new WP_Error( 'activation_error', $this->log[ $plugin ]['error'] ); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
protected function deactivate() { |
|
127
|
|
|
foreach ( $this->plugins as $plugin ) { |
|
128
|
|
View Code Duplication |
if ( ! Jetpack::is_plugin_active( $plugin ) ) { |
|
129
|
|
|
$error = $this->log[ $plugin ]['error'] = __( 'The Plugin is already deactivated.', 'jetpack' ); |
|
130
|
|
|
continue; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
deactivate_plugins( $plugin, false, $this->network_wide ); |
|
134
|
|
|
|
|
135
|
|
|
$success = ! Jetpack::is_plugin_active( $plugin ); |
|
136
|
|
|
if ( $success && $this->network_wide ) { |
|
137
|
|
|
$success &= ! is_plugin_active_for_network( $plugin ); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
View Code Duplication |
if ( ! $success ) { |
|
141
|
|
|
$error = $this->log[ $plugin ]['error'] = __( 'There was an error deactivating your plugin', 'jetpack' ); |
|
142
|
|
|
continue; |
|
143
|
|
|
} |
|
144
|
|
|
$this->log[ $plugin ][] = __( 'Plugin deactivated.', 'jetpack' ); |
|
145
|
|
|
} |
|
146
|
|
|
if ( ! $this->bulk && isset( $error ) ) { |
|
147
|
|
|
return new WP_Error( 'deactivation_error', $error ); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
protected function update() { |
|
152
|
|
|
|
|
153
|
|
|
wp_clean_plugins_cache(); |
|
154
|
|
|
ob_start(); |
|
155
|
|
|
wp_update_plugins(); // Check for Plugin updates |
|
156
|
|
|
ob_end_clean(); |
|
157
|
|
|
|
|
158
|
|
|
$update_plugins = get_site_transient( 'update_plugins' ); |
|
159
|
|
|
|
|
160
|
|
|
if ( isset( $update_plugins->response ) ) { |
|
161
|
|
|
$plugin_updates_needed = array_keys( $update_plugins->response ); |
|
162
|
|
|
} else { |
|
163
|
|
|
$plugin_updates_needed = array(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$update_attempted = false; |
|
167
|
|
|
|
|
168
|
|
|
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
|
169
|
|
|
|
|
170
|
|
|
// unhook this functions that output things before we send our response header. |
|
171
|
|
|
remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 ); |
|
172
|
|
|
remove_action( 'upgrader_process_complete', 'wp_version_check' ); |
|
173
|
|
|
remove_action( 'upgrader_process_complete', 'wp_update_themes' ); |
|
174
|
|
|
|
|
175
|
|
|
$result = false; |
|
176
|
|
|
|
|
177
|
|
|
foreach ( $this->plugins as $plugin ) { |
|
178
|
|
|
|
|
179
|
|
|
if ( ! in_array( $plugin, $plugin_updates_needed ) ) { |
|
180
|
|
|
$this->log[ $plugin ][] = __( 'No update needed', 'jetpack' ); |
|
181
|
|
|
continue; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Pre-upgrade action |
|
186
|
|
|
* |
|
187
|
|
|
* @since 3.9.3 |
|
188
|
|
|
* |
|
189
|
|
|
* @param array $plugin Plugin data |
|
190
|
|
|
* @param array $plugin Array of plugin objects |
|
191
|
|
|
* @param bool $updated_attempted false for the first update, true subsequently |
|
192
|
|
|
*/ |
|
193
|
|
|
do_action( 'jetpack_pre_plugin_upgrade', $plugin, $this->plugins, $update_attempted ); |
|
194
|
|
|
|
|
195
|
|
|
$update_attempted = true; |
|
196
|
|
|
|
|
197
|
|
|
// Object created inside the for loop to clean the messages for each plugin |
|
198
|
|
|
$skin = new Automatic_Upgrader_Skin(); |
|
199
|
|
|
// The Automatic_Upgrader_Skin skin shouldn't output anything. |
|
200
|
|
|
$upgrader = new Plugin_Upgrader( $skin ); |
|
201
|
|
|
$upgrader->init(); |
|
202
|
|
|
// This avoids the plugin to be deactivated. |
|
203
|
|
|
defined( 'DOING_CRON' ) or define( 'DOING_CRON', true ); |
|
|
|
|
|
|
204
|
|
|
$result = $upgrader->upgrade( $plugin ); |
|
205
|
|
|
|
|
206
|
|
|
$this->log[ $plugin ] = $upgrader->skin->get_upgrade_messages(); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
View Code Duplication |
if ( ! $this->bulk && ! $result && $update_attempted ) { |
|
210
|
|
|
return new WP_Error( 'update_fail', __( 'There was an error updating your plugin', 'jetpack' ), 400 ); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return $this->default_action(); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
function update_translations() { |
|
217
|
|
|
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
|
218
|
|
|
|
|
219
|
|
|
// Clear the cache. |
|
220
|
|
|
wp_clean_plugins_cache(); |
|
221
|
|
|
ob_start(); |
|
222
|
|
|
wp_update_plugins(); // Check for Plugin updates |
|
223
|
|
|
ob_end_clean(); |
|
224
|
|
|
|
|
225
|
|
|
$available_updates = get_site_transient( 'update_plugins' ); |
|
226
|
|
|
if ( ! isset( $available_updates->translations ) || empty( $available_updates->translations ) ) { |
|
227
|
|
|
return new WP_Error( 'nothing_to_translate' ); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
$update_attempted = false; |
|
231
|
|
|
$result = false; |
|
232
|
|
|
foreach( $this->plugins as $plugin ) { |
|
233
|
|
|
$this->slug = Jetpack_Autoupdate::get_plugin_slug( $plugin ); |
|
234
|
|
|
$translation = array_filter( $available_updates->translations, array( $this, 'get_translation' ) ); |
|
235
|
|
|
|
|
236
|
|
|
if ( empty( $translation ) ) { |
|
237
|
|
|
$this->log[ $plugin ][] = __( 'No update needed', 'jetpack' ); |
|
238
|
|
|
continue; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Pre-upgrade action |
|
243
|
|
|
* |
|
244
|
|
|
* @since 4.4 |
|
245
|
|
|
* |
|
246
|
|
|
* @param array $plugin Plugin data |
|
247
|
|
|
* @param array $plugin Array of plugin objects |
|
248
|
|
|
* @param bool $updated_attempted false for the first update, true subsequently |
|
249
|
|
|
*/ |
|
250
|
|
|
do_action( 'jetpack_pre_plugin_upgrade_translations', $plugin, $this->plugins, $update_attempted ); |
|
251
|
|
|
|
|
252
|
|
|
$update_attempted = true; |
|
253
|
|
|
|
|
254
|
|
|
$skin = new Automatic_Upgrader_Skin(); |
|
255
|
|
|
$upgrader = new Language_Pack_Upgrader( $skin ); |
|
256
|
|
|
$upgrader->init(); |
|
257
|
|
|
|
|
258
|
|
|
$result = $upgrader->upgrade( (object) $translation[0] ); |
|
|
|
|
|
|
259
|
|
|
|
|
260
|
|
|
$this->log[ $plugin ] = $upgrader->skin->get_upgrade_messages(); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
if ( ! $this->bulk && ! $result ) { |
|
264
|
|
|
return new WP_Error( 'update_fail', __( 'There was an error updating your plugin', 'jetpack' ), 400 ); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
return true; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
protected function get_translation( $translation ) { |
|
271
|
|
|
return ( $translation['slug'] === $this->slug ); |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
|