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
|
|
View Code Duplication |
class Jetpack_JSON_API_Plugins_New_Endpoint extends Jetpack_JSON_API_Plugins_Endpoint { |
|
|
|
|
7
|
|
|
|
8
|
|
|
// POST /sites/%s/plugins/new |
9
|
|
|
protected $needed_capabilities = 'install_plugins'; |
10
|
|
|
protected $action = 'install'; |
11
|
|
|
|
12
|
|
|
protected function validate_call( $_blog_id, $capability, $check_manage_active = true ) { |
13
|
|
|
$validate = parent::validate_call( $_blog_id, $capability, $check_manage_active ); |
14
|
|
|
if ( is_wp_error( $validate ) ) { |
15
|
|
|
|
16
|
|
|
// Lets delete the attachment... if the user doesn't have the right permissions to do things. |
17
|
|
|
$args = $this->input(); |
18
|
|
|
if ( isset( $args['zip'][0]['id'] ) ) { |
19
|
|
|
wp_delete_attachment( $args['zip'][0]['id'], true ); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
return $validate; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
// no need to try to validate the plugin since we didn't pass one in. |
27
|
|
|
protected function validate_input( $plugin ) { |
28
|
|
|
$this->bulk = false; |
29
|
|
|
$this->plugins = array(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
function install() { |
33
|
|
|
$args = $this->input(); |
34
|
|
|
|
35
|
|
|
if ( isset( $args['zip'][0]['id'] ) ) { |
36
|
|
|
$plugin_attachment_id = $args['zip'][0]['id']; |
37
|
|
|
$local_file = get_attached_file( $plugin_attachment_id ); |
38
|
|
|
if ( ! $local_file ) { |
39
|
|
|
return new WP_Error( 'local-file-does-not-exist' ); |
40
|
|
|
} |
41
|
|
|
$skin = new Jetpack_Automatic_Install_Skin(); |
42
|
|
|
$upgrader = new Plugin_Upgrader( $skin ); |
43
|
|
|
|
44
|
|
|
$pre_install_plugin_list = get_plugins(); |
45
|
|
|
$result = $upgrader->install( $local_file ); |
46
|
|
|
|
47
|
|
|
// clean up. |
48
|
|
|
wp_delete_attachment( $plugin_attachment_id, true ); |
49
|
|
|
|
50
|
|
|
if ( is_wp_error( $result ) ) { |
51
|
|
|
return $result; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$after_install_plugin_list = get_plugins(); |
55
|
|
|
$plugin = array_values( array_diff( array_keys( $after_install_plugin_list ), array_keys( $pre_install_plugin_list ) ) ); |
56
|
|
|
|
57
|
|
|
if ( ! $result ) { |
58
|
|
|
$error_code = $upgrader->skin->get_main_error_code(); |
59
|
|
|
$message = $upgrader->skin->get_main_error_message(); |
60
|
|
|
if ( empty( $message ) ) { |
61
|
|
|
$message = __( 'An unknown error occurred during installation', 'jetpack' ); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ( 'download_failed' === $error_code ) { |
65
|
|
|
$error_code = 'no_package'; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return new WP_Error( $error_code, $message, 400 ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ( empty( $plugin ) ) { |
72
|
|
|
return new WP_Error( 'plugin_already_installed' ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$this->plugins = $plugin; |
76
|
|
|
$this->log[ $plugin[0] ] = $upgrader->skin->get_upgrade_messages(); |
77
|
|
|
|
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return new WP_Error( 'no_plugin_installed' ); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.