From 1ce04d73bfec967f4ca8b461c31a72c3f21d3471 Mon Sep 17 00:00:00 2001 From: Caroline Moore Date: Tue, 17 Sep 2024 09:04:10 -0400 Subject: [PATCH] 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. --- .../jetpack-mu-wpcom/.phan/baseline.php | 1 + .../changelog/add-wpcom-key-endpoint | 4 + ...int-site-migration-wpcom-migration-key.php | 109 ++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 projects/packages/jetpack-mu-wpcom/changelog/add-wpcom-key-endpoint create mode 100644 projects/packages/jetpack-mu-wpcom/src/features/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-site-migration-wpcom-migration-key.php diff --git a/projects/packages/jetpack-mu-wpcom/.phan/baseline.php b/projects/packages/jetpack-mu-wpcom/.phan/baseline.php index bb58e6850074b..f8dea226fcd09 100644 --- a/projects/packages/jetpack-mu-wpcom/.phan/baseline.php +++ b/projects/packages/jetpack-mu-wpcom/.phan/baseline.php @@ -79,6 +79,7 @@ 'src/features/verbum-comments/class-verbum-comments.php' => ['PhanImpossibleTypeComparison', 'PhanNoopNew', 'PhanParamTooMany', 'PhanTypeMismatchArgumentProbablyReal', 'PhanUndeclaredFunction'], 'src/features/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-launchpad.php' => ['PhanPluginDuplicateConditionalNullCoalescing'], 'src/features/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-site-migration-migrate-guru-key.php' => ['PhanUndeclaredClassMethod'], + 'src/features/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-site-migration-wpcom-migration-key.php' => ['PhanUndeclaredClassMethod'], 'tests/lib/functions-wordpress.php' => ['PhanRedefineFunction'], 'tests/php/features/block-patterns/class-wpcom-block-patterns-from-api-test.php' => ['PhanDeprecatedFunction'], 'tests/php/features/coming-soon/class-coming-soon-test.php' => ['PhanTypeMismatchArgument', 'PhanTypeMismatchArgumentProbablyReal'], diff --git a/projects/packages/jetpack-mu-wpcom/changelog/add-wpcom-key-endpoint b/projects/packages/jetpack-mu-wpcom/changelog/add-wpcom-key-endpoint new file mode 100644 index 0000000000000..e92c25acdebd4 --- /dev/null +++ b/projects/packages/jetpack-mu-wpcom/changelog/add-wpcom-key-endpoint @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Add new wpcom-migration-key endpoint. diff --git a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-site-migration-wpcom-migration-key.php b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-site-migration-wpcom-migration-key.php new file mode 100644 index 0000000000000..955e839d2da66 --- /dev/null +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-site-migration-wpcom-migration-key.php @@ -0,0 +1,109 @@ +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' );