Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
27.94% |
19 / 68 |
|
50.00% |
2 / 4 |
CRAP | |
0.00% |
0 / 1 |
| WP_REST_Help_Center_Persisted_Open_State | |
27.94% |
19 / 68 |
|
50.00% |
2 / 4 |
39.31 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| register_rest_route | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
1 | |||
| get_state | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
6 | |||
| set_state | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
30 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * WP_REST_Help_Center_Persisted_Open_State file. |
| 4 | * |
| 5 | * @package automattic/jetpack-help-center |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Help_Center; |
| 9 | |
| 10 | /** |
| 11 | * Class WP_REST_Help_Center_Persisted_Open_State. |
| 12 | */ |
| 13 | class WP_REST_Help_Center_Persisted_Open_State extends WP_REST_Help_Center_Controller { |
| 14 | |
| 15 | /** |
| 16 | * WP_REST_Help_Center_Persisted_Open_State constructor. |
| 17 | * |
| 18 | * @param Wpcom_Request_Client|null $wpcom_request_client WP.com request client. |
| 19 | */ |
| 20 | public function __construct( ?Wpcom_Request_Client $wpcom_request_client = null ) { |
| 21 | parent::__construct( $wpcom_request_client ); |
| 22 | $this->namespace = 'help-center'; |
| 23 | $this->rest_base = '/open-state'; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Register available routes. |
| 28 | */ |
| 29 | public function register_rest_route() { |
| 30 | register_rest_route( |
| 31 | $this->namespace, |
| 32 | $this->rest_base, |
| 33 | array( |
| 34 | // Get the open state. |
| 35 | array( |
| 36 | 'methods' => \WP_REST_Server::READABLE, |
| 37 | 'callback' => array( $this, 'get_state' ), |
| 38 | 'permission_callback' => '__return_true', |
| 39 | ), |
| 40 | // Set the open state |
| 41 | array( |
| 42 | 'methods' => \WP_REST_Server::EDITABLE, |
| 43 | 'callback' => array( $this, 'set_state' ), |
| 44 | 'permission_callback' => '__return_true', |
| 45 | ), |
| 46 | ) |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get chat_id and last_chat_id from user preferences. |
| 52 | */ |
| 53 | public function get_state() { |
| 54 | // Forward the request body to the support chat endpoint. |
| 55 | $body = $this->wpcom_request_client->request( |
| 56 | '/me/preferences', |
| 57 | '2', |
| 58 | array( 'method' => 'GET' ) |
| 59 | ); |
| 60 | |
| 61 | if ( is_wp_error( $body ) ) { |
| 62 | return $body; |
| 63 | } |
| 64 | |
| 65 | $response = json_decode( wp_remote_retrieve_body( $body ) ); |
| 66 | |
| 67 | $is_open = $response->calypso_preferences->help_center_open ?? false; |
| 68 | $is_minimized = $response->calypso_preferences->help_center_minimized ?? false; |
| 69 | $router_history = $response->calypso_preferences->help_center_router_history ?? null; |
| 70 | |
| 71 | $projected_response = array( |
| 72 | 'help_center_open' => (bool) $is_open, |
| 73 | 'help_center_minimized' => (bool) $is_minimized, |
| 74 | 'help_center_router_history' => $router_history, |
| 75 | ); |
| 76 | |
| 77 | return rest_ensure_response( $projected_response ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Set chat_id or last_chat_id from user preferences. |
| 82 | * |
| 83 | * @param \WP_REST_Request $request The request sent to the API. |
| 84 | */ |
| 85 | public function set_state( \WP_REST_Request $request ) { |
| 86 | $state = $request['help_center_open']; |
| 87 | $router_history = $request['help_center_router_history']; |
| 88 | $minimized = $request['help_center_minimized']; |
| 89 | |
| 90 | $data = array( |
| 91 | 'calypso_preferences' => array(), |
| 92 | ); |
| 93 | |
| 94 | if ( $request->has_param( 'help_center_open' ) ) { |
| 95 | $data['calypso_preferences']['help_center_open'] = $state; |
| 96 | } |
| 97 | |
| 98 | if ( $request->has_param( 'help_center_router_history' ) ) { |
| 99 | $data['calypso_preferences']['help_center_router_history'] = $router_history; |
| 100 | } |
| 101 | |
| 102 | if ( $request->has_param( 'help_center_minimized' ) ) { |
| 103 | $data['calypso_preferences']['help_center_minimized'] = $minimized; |
| 104 | } |
| 105 | |
| 106 | $body = $this->wpcom_request_client->request( |
| 107 | '/me/preferences', |
| 108 | '2', |
| 109 | array( 'method' => 'POST' ), |
| 110 | $data |
| 111 | ); |
| 112 | |
| 113 | if ( is_wp_error( $body ) ) { |
| 114 | return $body; |
| 115 | } |
| 116 | |
| 117 | $response = json_decode( wp_remote_retrieve_body( $body ) ); |
| 118 | |
| 119 | $is_open = $response->calypso_preferences->help_center_open ?? false; |
| 120 | $router_history = $response->calypso_preferences->help_center_router_history ?? null; |
| 121 | $is_minimized = $response->calypso_preferences->help_center_minimized ?? false; |
| 122 | |
| 123 | $projected_response = array( |
| 124 | 'calypso_preferences' => array( |
| 125 | 'help_center_open' => (bool) $is_open, |
| 126 | 'help_center_router_history' => $router_history, |
| 127 | 'help_center_minimized' => (bool) $is_minimized, |
| 128 | ), |
| 129 | ); |
| 130 | |
| 131 | return rest_ensure_response( $projected_response ); |
| 132 | } |
| 133 | } |