PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.6.4
Jetpack – WP Security, Backup, Speed, & Growth v12.6.4
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / json-endpoints / jetpack / class.jetpack-json-api-plugins-new-endpoint.php

class.jetpack-json-api-plugins-new-endpoint.php in Jetpack – WP Security, Backup, Speed, & Growth 12.6.4, at json-endpoints/jetpack/class.jetpack-json-api-plugins-new-endpoint.php

171 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
4 require_once ABSPATH . 'wp-admin/includes/file.php';
5
6 use Automattic\Jetpack\Automatic_Install_Skin;
7
8 // POST /sites/%s/plugins/new
9 new Jetpack_JSON_API_Plugins_New_Endpoint(
10 array(
11 'description' => 'Install a plugin to a Jetpack site by uploading a zip file',
12 'group' => '__do_not_document',
13 'stat' => 'plugins:new',
14 'min_version' => '1',
15 'max_version' => '1.1',
16 'method' => 'POST',
17 'path' => '/sites/%s/plugins/new',
18 'path_labels' => array(
19 '$site' => '(int|string) Site ID or domain',
20 ),
21 'request_format' => array(
22 'zip' => '(array) Reference to an uploaded plugin package zip file.',
23 ),
24 'response_format' => Jetpack_JSON_API_Plugins_Endpoint::$_response_format,
25 'allow_jetpack_site_auth' => true,
26 'example_request_data' => array(
27 'headers' => array(
28 'authorization' => 'Bearer YOUR_API_TOKEN',
29 ),
30 ),
31 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/example.wordpress.org/plugins/new',
32 )
33 );
34
35 new Jetpack_JSON_API_Plugins_New_Endpoint(
36 array(
37 'description' => 'Install a plugin to a Jetpack site by uploading a zip file',
38 'group' => '__do_not_document',
39 'stat' => 'plugins:new',
40 'min_version' => '1.2',
41 'method' => 'POST',
42 'path' => '/sites/%s/plugins/new',
43 'path_labels' => array(
44 '$site' => '(int|string) Site ID or domain',
45 ),
46 'request_format' => array(
47 'zip' => '(array) Reference to an uploaded plugin package zip file.',
48 ),
49 'response_format' => Jetpack_JSON_API_Plugins_Endpoint::$_response_format_v1_2,
50 'allow_jetpack_site_auth' => true,
51 'example_request_data' => array(
52 'headers' => array(
53 'authorization' => 'Bearer YOUR_API_TOKEN',
54 ),
55 ),
56 'example_request' => 'https://public-api.wordpress.com/rest/v1.2/sites/example.wordpress.org/plugins/new',
57 )
58 );
59
60 /**
61 * Plugins new endpoint class.
62 *
63 * POST /sites/%s/plugins/new
64 */
65 class Jetpack_JSON_API_Plugins_New_Endpoint extends Jetpack_JSON_API_Plugins_Endpoint {
66
67 /**
68 * Needed capabilities.
69 *
70 * @var string
71 */
72 protected $needed_capabilities = 'install_plugins';
73
74 /**
75 * The action.
76 *
77 * @var string
78 */
79 protected $action = 'install';
80
81 /**
82 * Validate call.
83 *
84 * @param int $_blog_id - the blog ID.
85 * @param string $capability - the capability.
86 * @param bool $check_manage_active - check if manage is active.
87 *
88 * @return bool|WP_Error a WP_Error object or true if things are good.
89 */
90 protected function validate_call( $_blog_id, $capability, $check_manage_active = true ) {
91 $validate = parent::validate_call( $_blog_id, $capability, $check_manage_active );
92 if ( is_wp_error( $validate ) ) {
93
94 // Lets delete the attachment... if the user doesn't have the right permissions to do things.
95 $args = $this->input();
96 if ( isset( $args['zip'][0]['id'] ) ) {
97 wp_delete_attachment( $args['zip'][0]['id'], true );
98 }
99 }
100
101 return $validate;
102 }
103
104 /**
105 * No need to try to validate the plugin since we didn't pass one in.
106 *
107 * @param string $plugin - the plugin we're validating.
108 */
109 protected function validate_input( $plugin ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
110 $this->bulk = false;
111 $this->plugins = array();
112 }
113
114 /**
115 * Install the plugin.
116 *
117 * @return bool|WP_Error
118 */
119 public function install() {
120 $args = $this->input();
121
122 if ( isset( $args['zip'][0]['id'] ) ) {
123 $plugin_attachment_id = $args['zip'][0]['id'];
124 $local_file = get_attached_file( $plugin_attachment_id );
125 if ( ! $local_file ) {
126 return new WP_Error( 'local-file-does-not-exist' );
127 }
128 $skin = new Automatic_Install_Skin();
129 $upgrader = new Plugin_Upgrader( $skin );
130
131 $pre_install_plugin_list = get_plugins();
132 $result = $upgrader->install( $local_file );
133
134 // clean up.
135 wp_delete_attachment( $plugin_attachment_id, true );
136
137 if ( is_wp_error( $result ) ) {
138 return $result;
139 }
140
141 $after_install_plugin_list = get_plugins();
142 $plugin = array_values( array_diff( array_keys( $after_install_plugin_list ), array_keys( $pre_install_plugin_list ) ) );
143
144 if ( ! $result ) {
145 $error_code = $upgrader->skin->get_main_error_code();
146 $message = $upgrader->skin->get_main_error_message();
147 if ( empty( $message ) ) {
148 $message = __( 'An unknown error occurred during installation', 'jetpack' );
149 }
150
151 if ( 'download_failed' === $error_code ) {
152 $error_code = 'no_package';
153 }
154
155 return new WP_Error( $error_code, $message, 400 );
156 }
157
158 if ( empty( $plugin ) ) {
159 return new WP_Error( 'plugin_already_installed' );
160 }
161
162 $this->plugins = $plugin;
163 $this->log[ $plugin[0] ] = $upgrader->skin->get_upgrade_messages();
164
165 return true;
166 }
167
168 return new WP_Error( 'no_plugin_installed' );
169 }
170 }
171