| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
|
| 3 |
// POST /sites/%s/plugins/%s/delete |
| 4 |
new Jetpack_JSON_API_Plugins_Delete_Endpoint( |
| 5 |
array( |
| 6 |
'description' => 'Delete/Uninstall a plugin from your jetpack blog', |
| 7 |
'group' => '__do_not_document', |
| 8 |
'stat' => 'plugins:1:delete', |
| 9 |
'min_version' => '1', |
| 10 |
'max_version' => '1.1', |
| 11 |
'method' => 'POST', |
| 12 |
'path' => '/sites/%s/plugins/%s/delete', |
| 13 |
'path_labels' => array( |
| 14 |
'$site' => '(int|string) The site ID, The site domain', |
| 15 |
'$plugin' => '(int|string) The plugin slug to delete', |
| 16 |
), |
| 17 |
'response_format' => Jetpack_JSON_API_Plugins_Endpoint::$_response_format, |
| 18 |
'allow_jetpack_site_auth' => true, |
| 19 |
'example_request_data' => array( |
| 20 |
'headers' => array( |
| 21 |
'authorization' => 'Bearer YOUR_API_TOKEN', |
| 22 |
), |
| 23 |
), |
| 24 |
'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/example.wordpress.org/plugins/akismet%2Fakismet/delete', |
| 25 |
) |
| 26 |
); |
| 27 |
// v1.2 |
| 28 |
new Jetpack_JSON_API_Plugins_Delete_Endpoint( |
| 29 |
array( |
| 30 |
'description' => 'Delete/Uninstall a plugin from your jetpack blog', |
| 31 |
'group' => '__do_not_document', |
| 32 |
'stat' => 'plugins:1:delete', |
| 33 |
'min_version' => '1.2', |
| 34 |
'method' => 'POST', |
| 35 |
'path' => '/sites/%s/plugins/%s/delete', |
| 36 |
'path_labels' => array( |
| 37 |
'$site' => '(int|string) The site ID, The site domain', |
| 38 |
'$plugin' => '(int|string) The plugin slug to delete', |
| 39 |
), |
| 40 |
'response_format' => Jetpack_JSON_API_Plugins_Endpoint::$_response_format_v1_2, |
| 41 |
'allow_jetpack_site_auth' => true, |
| 42 |
'example_request_data' => array( |
| 43 |
'headers' => array( |
| 44 |
'authorization' => 'Bearer YOUR_API_TOKEN', |
| 45 |
), |
| 46 |
), |
| 47 |
'example_request' => 'https://public-api.wordpress.com/rest/v1.2/sites/example.wordpress.org/plugins/akismet%2Fakismet/delete', |
| 48 |
) |
| 49 |
); |
| 50 |
|
| 51 |
/** |
| 52 |
* Plugins delete endpoint class. |
| 53 |
* |
| 54 |
* POST /sites/%s/plugins/%s/delete |
| 55 |
*/ |
| 56 |
class Jetpack_JSON_API_Plugins_Delete_Endpoint extends Jetpack_JSON_API_Plugins_Endpoint { |
| 57 |
|
| 58 |
/** |
| 59 |
* Needed capabilities. |
| 60 |
* |
| 61 |
* @var string |
| 62 |
*/ |
| 63 |
protected $needed_capabilities = 'delete_plugins'; |
| 64 |
|
| 65 |
/** |
| 66 |
* The action. |
| 67 |
* |
| 68 |
* @var string |
| 69 |
*/ |
| 70 |
protected $action = 'delete'; |
| 71 |
|
| 72 |
/** |
| 73 |
* The delete function. |
| 74 |
* |
| 75 |
* @return bool|WP_Error |
| 76 |
*/ |
| 77 |
protected function delete() { |
| 78 |
|
| 79 |
foreach ( $this->plugins as $plugin ) { |
| 80 |
|
| 81 |
if ( Jetpack::is_plugin_active( $plugin ) ) { |
| 82 |
$error = __( 'You cannot delete a plugin while it is active on the main site.', 'jetpack' ); |
| 83 |
$this->log[ $plugin ][] = $error; |
| 84 |
continue; |
| 85 |
} |
| 86 |
|
| 87 |
$result = delete_plugins( array( $plugin ) ); |
| 88 |
if ( is_wp_error( $result ) ) { |
| 89 |
$error = $result->get_error_message(); |
| 90 |
$this->log[ $plugin ][] = $error; |
| 91 |
} else { |
| 92 |
$this->log[ $plugin ][] = 'Plugin deleted'; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
if ( ! $this->bulk && isset( $error ) ) { |
| 97 |
return new WP_Error( 'delete_plugin_error', $error, 400 ); |
| 98 |
} |
| 99 |
|
| 100 |
return true; |
| 101 |
} |
| 102 |
} |
| 103 |
|