'GET', 'permission_callback' => array( new ConnectionController(), 'permission' ), 'callback' => array( $this, $action ), ) ); } } private function params( WP_REST_Request $request ): array { $params = $request->get_query_params(); unset( $params['rest_route'], $params['_locale'] ); return $params; } public function bookings( WP_REST_Request $request ): WP_REST_Response { return $this->response( ( new AdminBookings() )->catalog( $this->params( $request ) ) ); } public function overview( WP_REST_Request $request ): WP_REST_Response { if ( $this->params( $request ) ) { return $this->response( AdminBookings::error( 400 ) ); } $status = ( new ConnectionManager() )->status(); if ( is_wp_error( $status ) ) { return $this->response( $status ); } $result = array( 'connection' => $status, 'counts' => null, 'api' => 'unavailable', 'plan' => null, ); $account_id = (int) ( $status['account']['id'] ?? 0 ); $counts = array( 'single' => 0, 'group' => 0, ); $api = new Services(); for ( $page = 1; $page <= 20 && $account_id; ++$page ) { $catalog = $api->catalog( $account_id, $page, 100 ); if ( is_wp_error( $catalog ) ) { break; } foreach ( $catalog['data'] as $service ) { if ( isset( $counts[ $service['type'] ] ) ) { ++$counts[ $service['type'] ]; } } if ( $page >= $catalog['meta']['last_page'] ) { $result['counts'] = $counts; $result['api'] = 'available'; break; } } return $this->response( $result ); } private function response( array|WP_Error $data ): WP_REST_Response { $response = is_wp_error( $data ) ? rest_convert_error_to_response( $data ) : new WP_REST_Response( $data ); $response->header( 'Cache-Control', 'no-store, private' ); $response->header( 'Pragma', 'no-cache' ); return $response; } }