-
Notifications
You must be signed in to change notification settings - Fork 798
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
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3228f04
Add API endpoints and Jetpack Backup package for managing Helper Scripts
8df73e4
Add new endpoint files to phpcs whitelist
seear e1b171d
[not verified] Call Automattic\Jetpack\Backup\Helper_Script_Manager::…
74675a1
[not verified] On uninstallation, delete all Helper Scripts
65b9532
Switch to using WP_Filesystem instead of file_get_contents and file_p…
24420ad
[not verified] Rename new api endpoint filenames in compliance with p…
72d367b
[not verified] Rename new api endpoint filenames in compliance with p…
1a43b0f
Document API endpoints as per linter requirements
e94fa84
Use use instead of bulky call syntax
0d28467
Change Jetpack Backups -> Jetpack Backup, also clean up formatting
15c5b72
Tidy up alignment of => markers
44db4a1
Use a more explicit header comment; making it clearer that this is re…
c6d322e
Use WP_CONTENT_DIR, WP_CONTENT_URL and wp_upload_dir to determine wp-…
e089413
Explicitly use constants and WordPress methods from the global namesp…
cf2b048
Escape the backslash in function_exists for WP_Filesystem to actually…
34b187e
Correct endpoint filenames in phpcs whitelist
seear 50c7922
Missing comma
seear File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
json-endpoints/jetpack/class-jetpack-json-api-delete-backup-helper-script-endpoint.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
json-endpoints/jetpack/class-jetpack-json-api-install-backup-helper-script-endpoint.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
/** | ||
* 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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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