| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
|
| 3 |
use Automattic\Jetpack\Plugins_Installer; |
| 4 |
|
| 5 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 6 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 7 |
// POST /sites/%s/plugins/%s/install |
| 8 |
new Jetpack_JSON_API_Plugins_Install_Endpoint( |
| 9 |
array( |
| 10 |
'description' => 'Install a plugin to your jetpack blog', |
| 11 |
'group' => '__do_not_document', |
| 12 |
'stat' => 'plugins:1:install', |
| 13 |
'min_version' => '1', |
| 14 |
'max_version' => '1.1', |
| 15 |
'method' => 'POST', |
| 16 |
'path' => '/sites/%s/plugins/%s/install', |
| 17 |
'path_labels' => array( |
| 18 |
'$site' => '(int|string) The site ID, The site domain', |
| 19 |
'$plugin' => '(int|string) The plugin slug to install', |
| 20 |
), |
| 21 |
'response_format' => Jetpack_JSON_API_Plugins_Endpoint::$_response_format, |
| 22 |
'allow_jetpack_site_auth' => true, |
| 23 |
'example_request_data' => array( |
| 24 |
'headers' => array( |
| 25 |
'authorization' => 'Bearer YOUR_API_TOKEN', |
| 26 |
), |
| 27 |
), |
| 28 |
'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/example.wordpress.org/plugins/akismet/install', |
| 29 |
) |
| 30 |
); |
| 31 |
|
| 32 |
new Jetpack_JSON_API_Plugins_Install_Endpoint( |
| 33 |
array( |
| 34 |
'description' => 'Install a plugin to your jetpack blog', |
| 35 |
'group' => '__do_not_document', |
| 36 |
'stat' => 'plugins:1:install', |
| 37 |
'min_version' => '1.2', |
| 38 |
'method' => 'POST', |
| 39 |
'path' => '/sites/%s/plugins/%s/install', |
| 40 |
'path_labels' => array( |
| 41 |
'$site' => '(int|string) The site ID, The site domain', |
| 42 |
'$plugin' => '(int|string) The plugin slug to install', |
| 43 |
), |
| 44 |
'response_format' => Jetpack_JSON_API_Plugins_Endpoint::$_response_format_v1_2, |
| 45 |
'allow_jetpack_site_auth' => true, |
| 46 |
'example_request_data' => array( |
| 47 |
'headers' => array( |
| 48 |
'authorization' => 'Bearer YOUR_API_TOKEN', |
| 49 |
), |
| 50 |
), |
| 51 |
'example_request' => 'https://public-api.wordpress.com/rest/v1.2/sites/example.wordpress.org/plugins/akismet/install', |
| 52 |
) |
| 53 |
); |
| 54 |
|
| 55 |
/** |
| 56 |
* Plugins install enedpoint class. |
| 57 |
* |
| 58 |
* POST /sites/%s/plugins/%s/install |
| 59 |
*/ |
| 60 |
class Jetpack_JSON_API_Plugins_Install_Endpoint extends Jetpack_JSON_API_Plugins_Endpoint { |
| 61 |
|
| 62 |
/** |
| 63 |
* Needed capabilities. |
| 64 |
* |
| 65 |
* @var string |
| 66 |
*/ |
| 67 |
protected $needed_capabilities = 'install_plugins'; |
| 68 |
|
| 69 |
/** |
| 70 |
* The action. |
| 71 |
* |
| 72 |
* @var string |
| 73 |
*/ |
| 74 |
protected $action = 'install'; |
| 75 |
|
| 76 |
/** |
| 77 |
* Installation. |
| 78 |
* |
| 79 |
* @return bool|WP_Error |
| 80 |
*/ |
| 81 |
protected function install() { |
| 82 |
$result = ''; |
| 83 |
foreach ( $this->plugins as $index => $slug ) { |
| 84 |
$result = Plugins_Installer::install_plugin( $slug ); |
| 85 |
if ( is_wp_error( $result ) ) { |
| 86 |
$this->log[ $slug ][] = $result->get_error_message(); |
| 87 |
if ( ! $this->bulk ) { |
| 88 |
return $result; |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if ( ! $result ) { |
| 94 |
return new WP_Error( 'plugin_install_failed', __( 'Plugin install failed because the result was invalid.', 'jetpack' ) ); |
| 95 |
} |
| 96 |
|
| 97 |
if ( is_wp_error( $result ) ) { |
| 98 |
return $result; |
| 99 |
} |
| 100 |
|
| 101 |
// No errors, install worked. Now replace the slug with the actual plugin id |
| 102 |
$this->plugins[ $index ] = Plugins_Installer::get_plugin_id_by_slug( $slug ); |
| 103 |
|
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Validate the plugins. |
| 109 |
* |
| 110 |
* @return bool|WP_Error |
| 111 |
*/ |
| 112 |
protected function validate_plugins() { |
| 113 |
if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) { |
| 114 |
return new WP_Error( 'missing_plugins', __( 'No plugins found.', 'jetpack' ) ); |
| 115 |
} |
| 116 |
|
| 117 |
foreach ( $this->plugins as $slug ) { |
| 118 |
// make sure it is not already installed |
| 119 |
if ( Plugins_Installer::get_plugin_id_by_slug( $slug ) ) { |
| 120 |
return new WP_Error( 'plugin_already_installed', __( 'The plugin is already installed', 'jetpack' ) ); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
return true; |
| 125 |
} |
| 126 |
} |
| 127 |
|