Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_REST_API_V2_Endpoint_Site_Migration_Migrate_Guru_Key
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 5
156
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 / 13
0.00% covered (danger)
0.00%
0 / 1
72
 get_migration_key
0.00% covered (danger)
0.00%
0 / 4
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_Migrate_Guru_Key extends WP_REST_Controller {
15    /**
16     * Option name that tracks wether the key has been read or not.
17     * The only possible value for the option is 'read'.
18     *
19     * @var string
20     */
21    protected $key_is_read_option_name = 'wpcom_site_migration_migrate_guru_key_read';
22
23    /**
24     * Class constructor
25     */
26    public function __construct() {
27        $this->namespace = 'wpcom/v2';
28        $this->rest_base = 'atomic-migration-status/migrate-guru-key';
29
30        add_action( 'rest_api_init', array( $this, 'register_routes' ) );
31    }
32
33    /**
34     * Register our routes.
35     */
36    public function register_routes() {
37        register_rest_route(
38            $this->namespace,
39            $this->rest_base,
40            array(
41                array(
42                    'methods'             => WP_REST_Server::READABLE,
43                    'callback'            => array( $this, 'get_data' ),
44                    'permission_callback' => array( $this, 'can_access' ),
45                ),
46            )
47        );
48    }
49
50    /**
51     * Permission callback for the REST route.
52     *
53     * @return boolean
54     */
55    public function can_access() {
56        if ( ! class_exists( 'Automattic\Jetpack\Status\Host' ) ) {
57            return false;
58        }
59
60        if ( ! ( new Automattic\Jetpack\Status\Host() )->is_woa_site() ) {
61            return false;
62        }
63
64        if ( ! current_user_can( 'manage_options' ) ) {
65            return false;
66        }
67
68        if ( ! is_plugin_active( 'migrate-guru/migrateguru.php' ) ) {
69            return false;
70        }
71
72        if ( ! class_exists( 'MGWPSettings' ) || ! class_exists( 'MGInfo' ) ) {
73            return false;
74        }
75
76        if ( 'read' === get_option( $this->key_is_read_option_name, false ) ) {
77            return false;
78        }
79
80        return true;
81    }
82
83    /**
84     * Returns the migration key.
85     *
86     * @return string
87     */
88    private function get_migration_key() {
89        $migrate_guru_settings = new MGWPSettings();
90        $migrate_guru_info     = new MGInfo( $migrate_guru_settings );
91
92        update_option( $this->key_is_read_option_name, 'read' );
93
94        return $migrate_guru_info->getConnectionKey();
95    }
96
97    /**
98     * Returns Launchpad-related options.
99     *
100     * @return array Associative array with `migration_key`.
101     */
102    public function get_data() {
103        return array(
104            'migration_key' => $this->get_migration_key(),
105        );
106    }
107}
108
109wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Site_Migration_Migrate_Guru_Key' );