PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3 16.3-beta 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 All 508 releases
jetpack / json-endpoints / jetpack / class.jetpack-json-api-plugins-modify-v1-2-endpoint.php

class.jetpack-json-api-plugins-modify-v1-2-endpoint.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-beta, at json-endpoints/jetpack/class.jetpack-json-api-plugins-modify-v1-2-endpoint.php

228 lines 7.7 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 if ( ! defined( 'ABSPATH' ) ) {
4 exit( 0 );
5 }
6
7 new Jetpack_JSON_API_Plugins_Modify_v1_2_Endpoint(
8 array(
9 'description' => 'Activate/Deactivate a Plugin on your Jetpack Site, or set automatic updates',
10 'min_version' => '1.2',
11 'method' => 'POST',
12 'path' => '/sites/%s/plugins/%s',
13 'stat' => 'plugins:1:modify',
14 'path_labels' => array(
15 '$site' => '(int|string) The site ID, The site domain',
16 '$plugin' => '(string) The plugin ID',
17 ),
18 'request_format' => array(
19 'action' => '(string) Possible values are \'update\'',
20 'autoupdate' => '(bool) Whether or not to automatically update the plugin',
21 'active' => '(bool) Activate or deactivate the plugin',
22 'network_wide' => '(bool) Do action network wide (default value: false)',
23 ),
24 'query_parameters' => array(
25 'autoupdate' => '(bool=false) If the update is happening as a result of autoupdate event',
26 ),
27 'response_format' => Jetpack_JSON_API_Plugins_Endpoint::$_response_format_v1_2,
28 'allow_jetpack_site_auth' => true,
29 'example_request_data' => array(
30 'headers' => array(
31 'authorization' => 'Bearer YOUR_API_TOKEN',
32 ),
33 'body' => array(
34 'action' => 'update',
35 ),
36 ),
37 'example_request' => 'https://public-api.wordpress.com/rest/v1.2/sites/example.wordpress.org/plugins/hello-dolly%20hello',
38 )
39 );
40
41 new Jetpack_JSON_API_Plugins_Modify_v1_2_Endpoint(
42 array(
43 'description' => 'Activate/Deactivate a list of plugins on your Jetpack Site, or set automatic updates',
44 'min_version' => '1.2',
45 'method' => 'POST',
46 'path' => '/sites/%s/plugins',
47 'stat' => 'plugins:modify',
48 'path_labels' => array(
49 '$site' => '(int|string) The site ID, The site domain',
50 ),
51 'request_format' => array(
52 'action' => '(string) Possible values are \'update\'',
53 'autoupdate' => '(bool) Whether or not to automatically update the plugin',
54 'active' => '(bool) Activate or deactivate the plugin',
55 'network_wide' => '(bool) Do action network wide (default value: false)',
56 'plugins' => '(array) A list of plugin ids to modify',
57 ),
58 'query_parameters' => array(
59 'autoupdate' => '(bool=false) If the update is happening as a result of autoupdate event',
60 ),
61 'response_format' => array(
62 'plugins' => '(array:plugin_v1_2) An array of plugin objects.',
63 'updated' => '(array) A list of plugin ids that were updated. Only present if action is update.',
64 'not_updated' => '(array) A list of plugin ids that were not updated. Only present if action is update.',
65 'log' => '(array) Update log. Only present if action is update.',
66 ),
67 'allow_jetpack_site_auth' => true,
68 'example_request_data' => array(
69 'headers' => array(
70 'authorization' => 'Bearer YOUR_API_TOKEN',
71 ),
72 'body' => array(
73 'active' => true,
74 'plugins' => array(
75 'jetpack/jetpack',
76 'akismet/akismet',
77 ),
78 ),
79 ),
80 'example_request' => 'https://public-api.wordpress.com/rest/v1.2/sites/example.wordpress.org/plugins',
81 )
82 );
83
84 new Jetpack_JSON_API_Plugins_Modify_v1_2_Endpoint(
85 array(
86 'description' => 'Update a Plugin on your Jetpack Site',
87 'min_version' => '1.2',
88 'method' => 'POST',
89 'path' => '/sites/%s/plugins/%s/update/',
90 'stat' => 'plugins:1:update',
91 'path_labels' => array(
92 '$site' => '(int|string) The site ID, The site domain',
93 '$plugin' => '(string) The plugin ID',
94 ),
95 'query_parameters' => array(
96 'autoupdate' => '(bool=false) If the update is happening as a result of autoupdate event',
97 ),
98 'response_format' => Jetpack_JSON_API_Plugins_Endpoint::$_response_format_v1_2,
99 'allow_jetpack_site_auth' => true,
100 'example_request_data' => array(
101 'headers' => array(
102 'authorization' => 'Bearer YOUR_API_TOKEN',
103 ),
104 ),
105 'example_request' => 'https://public-api.wordpress.com/rest/v1.2/sites/example.wordpress.org/plugins/hello-dolly%20hello/update',
106 )
107 );
108
109 /**
110 * Plugins modify 1_2 Endpoint.
111 *
112 * @phan-constructor-used-for-side-effects
113 */
114 class Jetpack_JSON_API_Plugins_Modify_v1_2_Endpoint extends Jetpack_JSON_API_Plugins_Modify_Endpoint { // phpcs:ignore PEAR.NamingConventions.ValidClassName.Invalid, Generic.Classes.OpeningBraceSameLine.ContentAfterBrace
115
116 /**
117 * Activate plugins.
118 *
119 * @return null|WP_Error null on success, WP_Error otherwise.
120 */
121 protected function activate() {
122 $permission_error = false;
123 $has_errors = false;
124 foreach ( $this->plugins as $plugin ) {
125
126 // If this endpoint accepts site based authentication and a blog token is used, skip capabilities check.
127 if ( ! $this->accepts_site_based_authentication() ) {
128 if ( ! $this->current_user_can( 'activate_plugin', $plugin ) ) {
129 $this->log[ $plugin ]['error'] = __( 'Sorry, you are not allowed to activate this plugin.', 'jetpack' );
130
131 $has_errors = true;
132 $permission_error = true;
133 continue;
134 }
135 }
136
137 if ( ( ! $this->network_wide && Jetpack::is_plugin_active( $plugin ) ) || is_plugin_active_for_network( $plugin ) ) {
138 continue;
139 }
140
141 if ( ! $this->network_wide && is_network_only_plugin( $plugin ) && is_multisite() ) {
142 $this->log[ $plugin ]['error'] = __( 'Plugin can only be Network Activated', 'jetpack' );
143
144 $has_errors = true;
145 continue;
146 }
147
148 $result = activate_plugin( $plugin, '', $this->network_wide );
149
150 if ( is_wp_error( $result ) ) {
151 $this->log[ $plugin ]['error'] = $result->get_error_messages();
152
153 $has_errors = true;
154 continue;
155 }
156
157 $success = Jetpack::is_plugin_active( $plugin );
158 if ( $success && $this->network_wide ) {
159 $success &= is_plugin_active_for_network( $plugin );
160 }
161
162 if ( ! $success ) {
163 $this->log[ $plugin ]['error'] = $result->get_error_messages;
164
165 $has_errors = true;
166 continue;
167 }
168 $this->log[ $plugin ][] = __( 'Plugin activated.', 'jetpack' );
169 }
170
171 if ( ! $this->bulk && $has_errors ) {
172 $plugin = $this->plugins[0];
173 if ( $permission_error ) {
174 return new WP_Error( 'unauthorized_error', $this->log[ $plugin ]['error'], 403 );
175 }
176
177 return new WP_Error( 'activation_error', $this->log[ $plugin ]['error'] );
178 }
179 }
180
181 /**
182 * Deactivate plugins.
183 *
184 * @return null|WP_Error null on success, WP_Error otherwise.
185 */
186 protected function deactivate() {
187 $permission_error = false;
188 foreach ( $this->plugins as $plugin ) {
189 // If this endpoint accepts site based authentication and a blog token is used, skip capabilities check.
190 if ( ! $this->accepts_site_based_authentication() ) {
191 if ( ! $this->current_user_can( 'deactivate_plugin', $plugin ) ) {
192 $error = __( 'Sorry, you are not allowed to deactivate this plugin.', 'jetpack' );
193
194 $this->log[ $plugin ]['error'] = $error;
195 $permission_error = true;
196 continue;
197 }
198 }
199
200 if ( ! Jetpack::is_plugin_active( $plugin ) ) {
201 continue;
202 }
203
204 deactivate_plugins( $plugin, false, $this->network_wide );
205
206 $success = ! Jetpack::is_plugin_active( $plugin );
207 if ( $success && $this->network_wide ) {
208 $success &= ! is_plugin_active_for_network( $plugin );
209 }
210
211 if ( ! $success ) {
212 $error = __( 'There was an error deactivating your plugin', 'jetpack' );
213
214 $this->log[ $plugin ]['error'] = $error;
215 continue;
216 }
217 $this->log[ $plugin ][] = __( 'Plugin deactivated.', 'jetpack' );
218 }
219 if ( ! $this->bulk && isset( $error ) ) {
220 if ( $permission_error ) {
221 return new WP_Error( 'unauthorized_error', $error, 403 );
222 }
223
224 return new WP_Error( 'deactivation_error', $error );
225 }
226 }
227 }
228