switched = switch_to_blog($blog_id); } } /** * Function that gets called after every action * * Link to udrpc_action main function in class UpdraftCentral_Listener */ public function _post_action() { // Here, we're restoring to the current (default) blog before we switched if ($this->switched) restore_current_blog(); } /** * Relays the REST API request. * * @param array $params The parameters for the request. * * @return array The response from the REST API, wrapped in udrpc response structure. */ public function handle_request($params) { $route = untrailingslashit(!empty($params['route']) ? $params['route'] : ''); $method = !empty($params['method']) ? $params['method'] : 'GET'; $body = !empty($params['body']) ? $params['body'] : null; // Return early if the route is empty. if (empty($route)) { return $this->_generic_error_response('route_empty', array( 'prefix' => 'updraftcentral', 'command' => 'handle_request', 'class' => 'UpdraftCentral_REST_API_Access_Commands' )); } if (!class_exists('WP_REST_Request')) { return $this->_generic_error_response('rest_api_not_available_on_this_wordpress_version', array( 'prefix' => 'updraftcentral', 'command' => 'handle_request', 'class' => 'UpdraftCentral_REST_API_Access_Commands' )); } $request = new WP_REST_Request($method, '/' . $route); if (!empty($body)) { $request->set_body(json_encode($body)); $request->set_header('Content-Type', 'application/json'); } // Do the request. $response = rest_do_request($request); // Return if error. if (true === $response->is_error()) { return $this->_generic_error_response('rest_request_failed', $response->as_error()); } // `get_data` should always return JSON-serializable data. return $this->_response(array('rest_data' => $response->get_data(), 'headers' => $response->headers)); } }