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 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_REST_API_V2_Endpoint_Jetpack_Launch_Site
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 4
30
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 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 launch_site
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Launch Site REST API endpoint.
4 *
5 * Used by Atomic sites (where WPCOM APIs are not directly accessible) to launch
6 * the site by proxying the request to WPCOM via the Jetpack Connection.
7 *
8 * @package automattic/jetpack-mu-wpcom
9 */
10
11use Automattic\Jetpack\Connection\Client;
12
13/**
14 * Launches the site by proxying to the WPCOM REST API.
15 */
16class WPCOM_REST_API_V2_Endpoint_Jetpack_Launch_Site extends WP_REST_Controller {
17
18    /**
19     * Class constructor.
20     */
21    public function __construct() {
22        $this->namespace = 'wpcom/v2';
23        $this->rest_base = 'launch-site';
24
25        add_action( 'rest_api_init', array( $this, 'register_routes' ) );
26    }
27
28    /**
29     * Register our routes.
30     */
31    public function register_routes() {
32        register_rest_route(
33            $this->namespace,
34            $this->rest_base,
35            array(
36                array(
37                    'methods'             => WP_REST_Server::CREATABLE,
38                    'callback'            => array( $this, 'launch_site' ),
39                    'permission_callback' => array( $this, 'can_access' ),
40                ),
41            )
42        );
43    }
44
45    /**
46     * Permission callback: only admins may launch the site.
47     *
48     * @return bool
49     */
50    public function can_access() {
51        return current_user_can( 'manage_options' );
52    }
53
54    /**
55     * Proxies a launch request to the WPCOM REST API.
56     *
57     * @return WP_REST_Response|WP_Error
58     */
59    public function launch_site() {
60        $blog_id  = \Jetpack_Options::get_option( 'id' );
61        $response = Client::wpcom_json_api_request_as_user(
62            '/sites/' . rawurlencode( $blog_id ) . '/launch',
63            'v2',
64            array(
65                'method'  => 'POST',
66                'headers' => array(
67                    'content-type' => 'application/json',
68                ),
69            ),
70            null,
71            'wpcom'
72        );
73
74        if ( is_wp_error( $response ) ) {
75            return $response;
76        }
77
78        $body        = wp_remote_retrieve_body( $response );
79        $status_code = wp_remote_retrieve_response_code( $response );
80        $data        = json_decode( $body );
81
82        return new WP_REST_Response( $data, $status_code );
83    }
84}
85
86wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Jetpack_Launch_Site' );