spy-rest-server.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. class Spy_REST_Server extends WP_REST_Server {
  3. public $sent_headers = array();
  4. public $sent_body = '';
  5. public $last_request = null;
  6. public $override_by_default = false;
  7. /**
  8. * Gets the raw $endpoints data from the server.
  9. *
  10. * @return array
  11. */
  12. public function get_raw_endpoint_data() {
  13. return $this->endpoints;
  14. }
  15. /**
  16. * Allow calling protected methods from tests.
  17. *
  18. * @param string $method Method to call.
  19. * @param array $args Arguments to pass to the method.
  20. * @return mixed
  21. */
  22. public function __call( $method, $args ) {
  23. return call_user_func_array( array( $this, $method ), $args );
  24. }
  25. /**
  26. * Adds a header to the list of sent headers.
  27. *
  28. * @param string $header Header name.
  29. * @param string $value Header value.
  30. */
  31. public function send_header( $header, $value ) {
  32. $this->sent_headers[ $header ] = $value;
  33. }
  34. /**
  35. * Removes a header from the list of sent headers.
  36. *
  37. * @param string $header Header name.
  38. */
  39. public function remove_header( $header ) {
  40. unset( $this->sent_headers[ $header ] );
  41. }
  42. /**
  43. * Overrides the dispatch method so we can get a handle on the request object.
  44. *
  45. * @param WP_REST_Request $request Request to attempt dispatching.
  46. * @return WP_REST_Response Response returned by the callback.
  47. */
  48. public function dispatch( $request ) {
  49. $this->last_request = $request;
  50. return parent::dispatch( $request );
  51. }
  52. /**
  53. * Overrides the register_route method so we can re-register routes internally if needed.
  54. *
  55. * @param string $namespace Namespace.
  56. * @param string $route The REST route.
  57. * @param array $route_args Route arguments.
  58. * @param bool $override Optional. Whether the route should be overridden if it already exists.
  59. * Default false. Also set `$GLOBALS['wp_rest_server']->override_by_default = true`
  60. * to set overrides when you don't have access to the caller context.
  61. */
  62. public function register_route( $namespace, $route, $route_args, $override = false ) {
  63. parent::register_route( $namespace, $route, $route_args, $override || $this->override_by_default );
  64. }
  65. /**
  66. * Serves the request and returns the result.
  67. *
  68. * @param string $path Optional. The request route. If not set, `$_SERVER['PATH_INFO']` will be used.
  69. * Default null.
  70. * @return null|false Null if not served and a HEAD request, false otherwise.
  71. */
  72. public function serve_request( $path = null ) {
  73. ob_start();
  74. $result = parent::serve_request( $path );
  75. $this->sent_body = ob_get_clean();
  76. return $result;
  77. }
  78. }