-
Notifications
You must be signed in to change notification settings - Fork 798
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
White-labeled plugin: Add wpcom-migration key endpoint (#39377)
* Add new wpcom-migration-key endpoint * changelog * Add new endpoint to exception list for phan * Fix incorrect class name. * Fix comment.
- Loading branch information
Showing
3 changed files
with
114 additions
and
0 deletions.
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
4 changes: 4 additions & 0 deletions
4
projects/packages/jetpack-mu-wpcom/changelog/add-wpcom-key-endpoint
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,4 @@ | ||
Significance: minor | ||
Type: added | ||
|
||
Add new wpcom-migration-key endpoint. |
109 changes: 109 additions & 0 deletions
109
...s/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-site-migration-wpcom-migration-key.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,109 @@ | ||
<?php | ||
/** | ||
* Allow us to access the Migrate Guru site migration key via API. | ||
* | ||
* @package automattic/jetpack | ||
*/ | ||
|
||
/** | ||
* WARNING: This file is distributed verbatim in Jetpack. | ||
* There should be nothing WordPress.com specific in this file. | ||
* | ||
* @hide-in-jetpack | ||
*/ | ||
class WPCOM_REST_API_V2_Endpoint_Site_Migration_WPCOM_Migration_Key extends WP_REST_Controller { | ||
/** | ||
* Option name that tracks wether the key has been read or not. | ||
* The only possible value for the option is 'read'. | ||
* | ||
* @var string | ||
*/ | ||
protected $key_is_read_option_name = 'wpcom_site_migration_wpcom_migration_key_read'; | ||
|
||
/** | ||
* Class constructor | ||
*/ | ||
public function __construct() { | ||
$this->namespace = 'wpcom/v2'; | ||
$this->rest_base = 'atomic-migration-status/wpcom-migration-key'; | ||
|
||
add_action( 'rest_api_init', array( $this, 'register_routes' ) ); | ||
} | ||
|
||
/** | ||
* Register our routes. | ||
*/ | ||
public function register_routes() { | ||
register_rest_route( | ||
$this->namespace, | ||
$this->rest_base, | ||
array( | ||
array( | ||
'methods' => WP_REST_Server::READABLE, | ||
'callback' => array( $this, 'get_data' ), | ||
'permission_callback' => array( $this, 'can_access' ), | ||
), | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Permission callback for the REST route. | ||
* | ||
* @return boolean | ||
*/ | ||
public function can_access() { | ||
if ( ! class_exists( 'Automattic\Jetpack\Status\Host' ) ) { | ||
return false; | ||
} | ||
|
||
if ( ! ( new Automattic\Jetpack\Status\Host() )->is_woa_site() ) { | ||
return false; | ||
} | ||
|
||
if ( ! current_user_can( 'manage_options' ) ) { | ||
return false; | ||
} | ||
|
||
if ( ! is_plugin_active( 'wpcom-migration/wpcom_migration.php' ) ) { | ||
return false; | ||
} | ||
|
||
if ( ! class_exists( 'WPCOMWPSettings' ) || ! class_exists( 'WPCOMInfo' ) ) { | ||
return false; | ||
} | ||
|
||
if ( 'read' === get_option( $this->key_is_read_option_name, false ) ) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Returns the migration key. | ||
* | ||
* @return string | ||
*/ | ||
private function get_migration_key() { | ||
$wpcom_migration_settings = new WPCOMWPSettings(); | ||
$wpcom_migration_info = new WPCOMInfo( $wpcom_migration_settings ); | ||
|
||
update_option( $this->key_is_read_option_name, 'read' ); | ||
|
||
return $wpcom_migration_info->getConnectionKey(); | ||
} | ||
|
||
/** | ||
* Returns migration key. | ||
* | ||
* @return array Associative array with `migration_key`. | ||
*/ | ||
public function get_data() { | ||
return array( | ||
'migration_key' => $this->get_migration_key(), | ||
); | ||
} | ||
} | ||
|
||
wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Site_Migration_WPCOM_Migration_Key' ); |