dispatch() public méthode

Matches the request to a callback and call it.
Since: 4.4.0
public dispatch ( WP_REST_Request $request ) : WP_REST_Response
$request WP_REST_Request Request to attempt dispatching.
Résultat WP_REST_Response Response returned by the callback.
 /**
  * Creates a WP_REST_Request and returns it.
  *
  * @since 4.4.0
  *
  * @param string $route       REST API path to be append to /jetpack/v4/
  * @param array  $json_params When present, parameters are added to request in JSON format
  * @param string $method      Request method to use, GET or POST
  * @param array  $params      Parameters to add to endpoint
  *
  * @return WP_REST_Response
  */
 protected function create_and_get_request($route = '', $json_params = array(), $method = 'GET', $params = array())
 {
     $request = new WP_REST_Request($method, "/jetpack/v4/{$route}");
     $request->set_header('content-type', 'application/json');
     if (!empty($json_params)) {
         $request->set_body(json_encode($json_params));
     }
     if (!empty($params) && is_array($params)) {
         foreach ($params as $key => $value) {
             $request->set_param($key, $value);
         }
     }
     return $this->server->dispatch($request);
 }
 /**
  * Test update item.
  */
 function test_delete_item()
 {
     wp_set_current_user($this->factory()->user->create(array('role' => 'administrator')));
     $post = get_post($this->snapshot_by_status['publish']);
     $request = new \WP_REST_Request('DELETE', '/wp/v2/customize_snapshots/' . $post->ID);
     $response = $this->server->dispatch($request);
     $this->assertErrorResponse('invalid-method', $response);
 }
 /**
  * Test the rest_pre_serve_request method.
  */
 function test_rest_pre_serve_request_wrong_method()
 {
     $post = $this->factory->post->create_and_get();
     $request = new WP_REST_Request('HEAD', '/wp/v2/oembed');
     $request->set_param('url', get_permalink($post->ID));
     $request->set_param('format', 'xml');
     $response = $this->server->dispatch($request);
     $this->assertTrue(_oembed_rest_pre_serve_request(true, $response, $request, $this->server));
 }
 /**
  * Call dispatch() with the rest_post_dispatch filter
  */
 public function dispatch($request)
 {
     $result = parent::dispatch($request);
     $result = rest_ensure_response($result);
     if (is_wp_error($result)) {
         $result = $this->error_to_response($result);
     }
     return apply_filters('rest_post_dispatch', rest_ensure_response($result), $this, $request);
 }
 function test_rest_pre_serve_request_headers()
 {
     if (!function_exists('xdebug_get_headers')) {
         $this->markTestSkipped('xdebug is required for this test');
     }
     $post = $this->factory()->post->create_and_get(array('post_title' => 'Hello World'));
     $request = new WP_REST_Request('GET', '/oembed/1.0/embed');
     $request->set_param('url', get_permalink($post->ID));
     $request->set_param('format', 'xml');
     $server = new WP_REST_Server();
     $response = $server->dispatch($request);
     $output = get_echo('_oembed_rest_pre_serve_request', array(true, $response, $request, $server));
     $this->assertNotEmpty($output);
     $headers = xdebug_get_headers();
     $this->assertTrue(in_array('Content-Type: text/xml; charset=' . get_option('blog_charset'), $headers));
 }
Exemple #6
0
 /**
  * Test HTTP headers set by the rest_pre_serve_request method.
  */
 function test_rest_pre_serve_request_headers()
 {
     if (!function_exists('xdebug_get_headers')) {
         $this->markTestSkipped('xdebug is required for this test');
     }
     require_once dirname(__FILE__) . '/../vendor/json-rest-api/plugin.php';
     require_once dirname(__FILE__) . '/../includes/class-wp-rest-oembed-controller.php';
     $user = $this->factory->user->create_and_get(array('display_name' => 'John Doe'));
     $post = $this->factory->post->create_and_get(array('post_author' => $user->ID, 'post_title' => 'Hello World'));
     $request = new WP_REST_Request('GET', '/wp/v2/oembed');
     $request->set_param('url', get_permalink($post->ID));
     $request->set_param('format', 'xml');
     $server = new WP_REST_Server();
     $response = $server->dispatch($request);
     ob_start();
     _oembed_rest_pre_serve_request(true, $response, $request, $server);
     $output = ob_get_clean();
     $this->assertNotEmpty($output);
     $headers = xdebug_get_headers();
     $this->assertTrue(in_array('Content-Type: text/xml; charset=' . get_option('blog_charset'), $headers));
 }
 /**
  * Test request for a child blog post embed in root blog.
  *
  * @group multisite
  */
 function test_request_ms_child_in_root_blog()
 {
     if (!is_multisite()) {
         $this->markTestSkipped(__METHOD__ . ' is a multisite-only test.');
     }
     $child = $this->factory->blog->create();
     switch_to_blog($child);
     $post = $this->factory->post->create_and_get(array('post_title' => 'Hello Child Blog'));
     $user = $this->factory->user->create_and_get(array('display_name' => 'John Doe'));
     $post = $this->factory->post->create_and_get(array('post_author' => $user->ID, 'post_title' => 'Hello World'));
     $request = new WP_REST_Request('GET', '/wp/v2/oembed');
     $request->set_param('url', get_permalink($post->ID));
     $response = $this->server->dispatch($request);
     $data = $response->get_data();
     $this->assertTrue(is_array($data));
     $this->assertArrayHasKey('version', $data);
     $this->assertArrayHasKey('provider_name', $data);
     $this->assertArrayHasKey('provider_url', $data);
     $this->assertArrayHasKey('author_name', $data);
     $this->assertArrayHasKey('author_url', $data);
     $this->assertArrayHasKey('title', $data);
     $this->assertArrayHasKey('type', $data);
     restore_current_blog();
 }
 public function test_get_index()
 {
     $server = new WP_REST_Server();
     $server->register_route('test/example', '/test/example/some-route', array(array('methods' => WP_REST_Server::READABLE, 'callback' => '__return_true'), array('methods' => WP_REST_Server::DELETABLE, 'callback' => '__return_true')));
     $request = new WP_REST_Request('GET', '/');
     $index = $server->dispatch($request);
     $data = $index->get_data();
     $this->assertArrayHasKey('name', $data);
     $this->assertArrayHasKey('description', $data);
     $this->assertArrayHasKey('url', $data);
     $this->assertArrayHasKey('home', $data);
     $this->assertArrayHasKey('namespaces', $data);
     $this->assertArrayHasKey('authentication', $data);
     $this->assertArrayHasKey('routes', $data);
     // Check namespace data.
     $this->assertContains('test/example', $data['namespaces']);
     // Check the route.
     $this->assertArrayHasKey('/test/example/some-route', $data['routes']);
     $route = $data['routes']['/test/example/some-route'];
     $this->assertEquals('test/example', $route['namespace']);
     $this->assertArrayHasKey('methods', $route);
     $this->assertContains('GET', $route['methods']);
     $this->assertContains('DELETE', $route['methods']);
     $this->assertArrayHasKey('_links', $route);
 }
 /**
  * Override the dispatch method so we can get a handle on the request object.
  *
  * @param  WP_REST_Request $request
  * @return WP_REST_Response Response returned by the callback.
  */
 public function dispatch($request)
 {
     $this->last_request = $request;
     return parent::dispatch($request);
 }
 /**
  * Attempt to upgrade all Customizer REST API requests to use the edit context.
  *
  * @param null|\WP_REST_Response $result  Response to replace the requested version with. Can be anything
  *                                        a normal endpoint can return, or null to not hijack the request.
  * @param \WP_REST_Server        $server  Server instance.
  * @param \WP_REST_Request       $request Original request used to generate the response.
  * @return null|\WP_REST_Response Dispatch result if successful, or null if the upgrade was not possible.
  */
 public function use_edit_context_for_requests($result, \WP_REST_Server $server, \WP_REST_Request $request)
 {
     if (null !== $result || 'edit' === $request['context'] || !is_customize_preview()) {
         return $result;
     }
     $edit_request = clone $request;
     $edit_request['context'] = 'edit';
     $edit_result = $server->dispatch($edit_request);
     if ($edit_result->is_error()) {
         /*
          * Return the original $result to prevent the short-circuiting of the
          * request dispatching since it is found to result in an error, likely
          * a rest_forbidden_context one.
          */
         return $result;
     }
     /*
      * Now set the context on the original request object to be edit so that
      * it will match the context that was actually used, and so that the
      * context will be available in the rest_post_dispatch filter.
      */
     $request['context'] = 'edit';
     return $edit_result;
 }