Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_REST_API_V2_Endpoint_Site_Migration_WPCOM_Migration_Key
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 5
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 register_routes
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 can_access
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
56
 get_migration_key
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 get_data
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Allow us to access the Migrate Guru site migration key via API.
4 *
5 * @package automattic/jetpack
6 */
7
8/**
9 * WARNING: This file is distributed verbatim in Jetpack.
10 * There should be nothing WordPress.com specific in this file.
11 *
12 * @hide-in-jetpack
13 */
14class WPCOM_REST_API_V2_Endpoint_Site_Migration_WPCOM_Migration_Key extends WP_REST_Controller {
15
16    /**
17     * Class constructor
18     */
19    public function __construct() {
20        $this->namespace = 'wpcom/v2';
21        $this->rest_base = 'atomic-migration-status/wpcom-migration-key';
22
23        add_action( 'rest_api_init', array( $this, 'register_routes' ) );
24    }
25
26    /**
27     * Register our routes.
28     */
29    public function register_routes() {
30        register_rest_route(
31            $this->namespace,
32            $this->rest_base,
33            array(
34                array(
35                    'methods'             => WP_REST_Server::READABLE,
36                    'callback'            => array( $this, 'get_data' ),
37                    'permission_callback' => array( $this, 'can_access' ),
38                ),
39            )
40        );
41    }
42
43    /**
44     * Permission callback for the REST route.
45     *
46     * @return boolean
47     */
48    public function can_access() {
49        if ( ! class_exists( 'Automattic\Jetpack\Status\Host' ) ) {
50            return false;
51        }
52
53        if ( ! ( new Automattic\Jetpack\Status\Host() )->is_woa_site() ) {
54            return false;
55        }
56
57        if ( ! current_user_can( 'manage_options' ) ) {
58            return false;
59        }
60
61        if ( ! is_plugin_active( 'wpcom-migration/wpcom_migration.php' ) ) {
62            return false;
63        }
64
65        if ( ! class_exists( 'WPCOMWPSettings' ) || ! class_exists( 'WPCOMInfo' ) ) {
66            return false;
67        }
68
69        return true;
70    }
71
72    /**
73     * Returns the migration key.
74     *
75     * @return string
76     */
77    private function get_migration_key() {
78        $wpcom_migration_settings = new WPCOMWPSettings();
79        $wpcom_migration_info     = new WPCOMInfo( $wpcom_migration_settings );
80
81        return $wpcom_migration_info->getConnectionKey();
82    }
83
84    /**
85     * Returns migration key.
86     *
87     * @return array Associative array with `migration_key`.
88     */
89    public function get_data() {
90        return array(
91            'migration_key' => $this->get_migration_key(),
92        );
93    }
94}
95
96wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Site_Migration_WPCOM_Migration_Key' );