Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API endpoints and Jetpack Backup package for managing Helper Scripts #13830

Merged
merged 17 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin/phpcs-whitelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ module.exports = [
'_inc/lib/core-api/wpcom-endpoints/memberships.php',
'_inc/lib/debugger/',
'_inc/lib/plans.php',
'json-endpoints/jetpack/class-jetpack-json-api-delete-backup-helper-script-endpoint.php',
'json-endpoints/jetpack/class-jetpack-json-api-install-backup-helper-script-endpoint.php',
'load-jetpack.php',
'modules/masterbar/',
'modules/memberships/',
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"automattic/jetpack-tracking": "@dev",
"automattic/jetpack-autoloader": "@dev",
"automattic/jetpack-compat": "@dev",
"automattic/jetpack-terms-of-service": "@dev"
"automattic/jetpack-terms-of-service": "@dev",
"automattic/jetpack-backup": "@dev"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "0.5.0",
Expand Down
22 changes: 22 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* API endpoint /sites/%s/delete-backup-helper-script
* This API endpoint deletes a Jetpack Backup Helper Script
*
* @package Jetpack
*/

use Automattic\Jetpack\Backup\Helper_Script_Manager;

class Jetpack_JSON_API_Delete_Backup_Helper_Script_Endpoint extends Jetpack_JSON_API_Endpoint {
/**
* This endpoint is only accessible from Jetpack Backup; it requires no further capabilities.
*
* @var array
*/
protected $needed_capabilities = array();

/**
* Method to call when running this endpoint (delete)
*
* @var string
*/
protected $action = 'delete';

/**
* Local path to the Helper Script to delete.
*
* @var string|null
*/
protected $script_path = null;

/**
* True if the specified file has been successfully deleted.
*
* @var boolean
*/
protected $result = false;

/**
* Checks that the input args look like a valid Helper Script path.
*
* @param null $object Unused.
* @return bool|WP_Error a WP_Error object or true if the input seems ok.
*/
protected function validate_input( $object ) {
$args = $this->input();

if ( ! isset( $args['path'] ) ) {
return new WP_Error( 'invalid_args', __( 'You must specify a helper script path', 'jetpack' ), 400 );
}

$this->script_path = $args['path'];
return true;
}

/**
* Deletes the specified Helper Script.
*/
protected function delete() {
$this->result = Helper_Script_Manager::delete_helper_script( $this->script_path );
Helper_Script_Manager::cleanup_expired_helper_scripts();
}

/**
* Returns the success or failure of the deletion operation
*
* @return array An array containing one key; 'success', which specifies whether the operation was successful.
*/
protected function result() {
return array(
'success' => $this->result,
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* API endpoint /sites/%s/install-backup-helper-script
* This API endpoint installs a Helper Script to assist Jetpack Backup fetch data
*
* @package Jetpack
*/

use Automattic\Jetpack\Backup\Helper_Script_Manager;

class Jetpack_JSON_API_Install_Backup_Helper_Script_Endpoint extends Jetpack_JSON_API_Endpoint {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered creating those endpoints as v2 endpoints (like this one)? I thought we try to create new endpoints with this mechanism, rather than the legacy v1 one.

Just in case it's helpful, this post is a good place to read more about the different types of endpoints: PCYsg-aqU-p2

/**
* This endpoint is only accessible from Jetpack Backup; it requires no further capabilities.
*
* @var array
*/
protected $needed_capabilities = array();

/**
* Method to call when running this endpoint (install)
*
* @var string
*/
protected $action = 'install';

/**
* Contents of the Helper Script to install
*
* @var string|null
*/
protected $helper_script = null;

/**
* Contains the result of installing the Helper Script.
*
* @var null|WP_Error|array
*/
protected $result = null;

/**
* Checks that the input args look like a valid Helper Script.
*
* @param null $object Unused.
* @return bool|WP_Error a WP_Error object or true if the input seems ok.
*/
protected function validate_input( $object ) {
$args = $this->input();

if ( ! isset( $args['helper'] ) ) {
return new WP_Error( 'invalid_args', __( 'You must specify a helper script body', 'jetpack' ), 400 );
}

$this->helper_script = base64_decode( $args['helper'] );
if ( ! $this->helper_script ) {
return new WP_Error( 'invalid_args', __( 'Helper script body must be base64 encoded', 'jetpack' ), 400 );
}

return true;
}

/**
* Installs the uploaded Helper Script.
*/
protected function install() {
$this->result = Helper_Script_Manager::install_helper_script( $this->helper_script );
Helper_Script_Manager::cleanup_expired_helper_scripts();
}

/**
* Returns the result of Helper Script installation. Returns one of:
* - WP_Error on failure, or
* - An array containing the access url ('url') and installation path ('path') on success.
*
* @return array|WP_Error Success or failure information.
*/
protected function result() {
// Include ABSPATH with successful result.
if ( ! is_wp_error( $this->result ) ) {
$this->result['abspath'] = ABSPATH;
}

return $this->result;
}

}
52 changes: 52 additions & 0 deletions json-endpoints/jetpack/json-api-jetpack-endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -1264,3 +1264,55 @@
'example_response' => '{ "success": true }',
'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/example.wordpress.org/jps/woo-connect'
) );

// POST /sites/%s/install-backup-helper-script
require_once( $json_jetpack_endpoints_dir . 'class-jetpack-json-api-install-backup-helper-script-endpoint.php' );
new Jetpack_JSON_API_Install_Backup_Helper_Script_Endpoint( array(
'description' => 'Setup a Helper Script, to allow Jetpack Backup to connect to this site',
'group' => '__do_not_document',
'method' => 'POST',
'path' => '/sites/%s/install-backup-helper-script',
'allow_jetpack_site_auth' => true,
'path_labels' => array(
'$site' => '(int|string) The site ID, The site domain',
),
'request_format' => array(
'helper' => '(string) Base64-encoded Helper Script contents',
),
'response_format' => array(
'abspath' => '(string) WordPress install path',
'path' => '(string) Path of the helper script',
'url' => '(string) URL to access the helper script',
),
'example_request_data' => array(
'headers' => array(
'authorization' => 'Bearer YOUR_API_TOKEN',
),
),
'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/example.wordpress.org/install-backup-helper-script'
) );

// POST /sites/%s/delete-backup-helper-script
require_once( $json_jetpack_endpoints_dir . 'class-jetpack-json-api-delete-backup-helper-script-endpoint.php' );
new Jetpack_JSON_API_Delete_Backup_Helper_Script_Endpoint( array(
'description' => 'Delete a Helper Script',
'group' => '__do_not_document',
'method' => 'POST',
'path' => '/sites/%s/delete-backup-helper-script',
'allow_jetpack_site_auth' => true,
'path_labels' => array(
'$site' => '(int|string) The site ID, The site domain',
),
'response_format' => array(
'success' => '(bool) Deleted the Helper Script successfully?'
),
'request_format' => array(
'path' => '(string) Path to Helper Script to delete',
),
'example_request_data' => array(
'headers' => array(
'authorization' => 'Bearer YOUR_API_TOKEN',
),
),
'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/example.wordpress.org/delete-backup-helper-script'
) );
13 changes: 13 additions & 0 deletions packages/backup/actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Action Hooks for Jetpack Backup module.
*
* @package automattic/jetpack-backup
*/

if ( ! defined( 'ABSPATH' ) ) {
return;
}

// Clean up expired Helper Scripts from a scheduled event.
add_action( 'jetpack_backup_cleanup_helper_scripts', array( 'Automattic\\Jetpack\\Backup\\Helper_Script_Manager', 'cleanup_expired_helper_scripts' ) );
17 changes: 17 additions & 0 deletions packages/backup/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
thingalon marked this conversation as resolved.
Show resolved Hide resolved
"name": "automattic/jetpack-backup",
"description": "Tools to assist with backing up Jetpack sites.",
"type": "library",
"license": "GPL-2.0-or-later",
"require": {},
"autoload": {
"files": [
"actions.php"
thingalon marked this conversation as resolved.
Show resolved Hide resolved
],
"psr-4": {
"Automattic\\Jetpack\\Backup\\": "src/"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
Loading