PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/rest-api/class-wp-rest-response.php

https://github.com/markjaquith/WordPress
PHP | 293 lines | 98 code | 29 blank | 166 comment | 12 complexity | a5b938d1b8577fbda4423b84eba1fbca MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * REST API: WP_REST_Response class
  4. *
  5. * @package WordPress
  6. * @subpackage REST_API
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core class used to implement a REST response object.
  11. *
  12. * @since 4.4.0
  13. *
  14. * @see WP_HTTP_Response
  15. */
  16. class WP_REST_Response extends WP_HTTP_Response {
  17. /**
  18. * Links related to the response.
  19. *
  20. * @since 4.4.0
  21. * @var array
  22. */
  23. protected $links = array();
  24. /**
  25. * The route that was to create the response.
  26. *
  27. * @since 4.4.0
  28. * @var string
  29. */
  30. protected $matched_route = '';
  31. /**
  32. * The handler that was used to create the response.
  33. *
  34. * @since 4.4.0
  35. * @var null|array
  36. */
  37. protected $matched_handler = null;
  38. /**
  39. * Adds a link to the response.
  40. *
  41. * @internal The $rel parameter is first, as this looks nicer when sending multiple.
  42. *
  43. * @since 4.4.0
  44. *
  45. * @link https://tools.ietf.org/html/rfc5988
  46. * @link https://www.iana.org/assignments/link-relations/link-relations.xml
  47. *
  48. * @param string $rel Link relation. Either an IANA registered type,
  49. * or an absolute URL.
  50. * @param string $href Target URI for the link.
  51. * @param array $attributes Optional. Link parameters to send along with the URL. Default empty array.
  52. */
  53. public function add_link( $rel, $href, $attributes = array() ) {
  54. if ( empty( $this->links[ $rel ] ) ) {
  55. $this->links[ $rel ] = array();
  56. }
  57. if ( isset( $attributes['href'] ) ) {
  58. // Remove the href attribute, as it's used for the main URL.
  59. unset( $attributes['href'] );
  60. }
  61. $this->links[ $rel ][] = array(
  62. 'href' => $href,
  63. 'attributes' => $attributes,
  64. );
  65. }
  66. /**
  67. * Removes a link from the response.
  68. *
  69. * @since 4.4.0
  70. *
  71. * @param string $rel Link relation. Either an IANA registered type, or an absolute URL.
  72. * @param string $href Optional. Only remove links for the relation matching the given href.
  73. * Default null.
  74. */
  75. public function remove_link( $rel, $href = null ) {
  76. if ( ! isset( $this->links[ $rel ] ) ) {
  77. return;
  78. }
  79. if ( $href ) {
  80. $this->links[ $rel ] = wp_list_filter( $this->links[ $rel ], array( 'href' => $href ), 'NOT' );
  81. } else {
  82. $this->links[ $rel ] = array();
  83. }
  84. if ( ! $this->links[ $rel ] ) {
  85. unset( $this->links[ $rel ] );
  86. }
  87. }
  88. /**
  89. * Adds multiple links to the response.
  90. *
  91. * Link data should be an associative array with link relation as the key.
  92. * The value can either be an associative array of link attributes
  93. * (including `href` with the URL for the response), or a list of these
  94. * associative arrays.
  95. *
  96. * @since 4.4.0
  97. *
  98. * @param array $links Map of link relation to list of links.
  99. */
  100. public function add_links( $links ) {
  101. foreach ( $links as $rel => $set ) {
  102. // If it's a single link, wrap with an array for consistent handling.
  103. if ( isset( $set['href'] ) ) {
  104. $set = array( $set );
  105. }
  106. foreach ( $set as $attributes ) {
  107. $this->add_link( $rel, $attributes['href'], $attributes );
  108. }
  109. }
  110. }
  111. /**
  112. * Retrieves links for the response.
  113. *
  114. * @since 4.4.0
  115. *
  116. * @return array List of links.
  117. */
  118. public function get_links() {
  119. return $this->links;
  120. }
  121. /**
  122. * Sets a single link header.
  123. *
  124. * @internal The $rel parameter is first, as this looks nicer when sending multiple.
  125. *
  126. * @since 4.4.0
  127. *
  128. * @link https://tools.ietf.org/html/rfc5988
  129. * @link https://www.iana.org/assignments/link-relations/link-relations.xml
  130. *
  131. * @param string $rel Link relation. Either an IANA registered type, or an absolute URL.
  132. * @param string $link Target IRI for the link.
  133. * @param array $other Optional. Other parameters to send, as an associative array.
  134. * Default empty array.
  135. */
  136. public function link_header( $rel, $link, $other = array() ) {
  137. $header = '<' . $link . '>; rel="' . $rel . '"';
  138. foreach ( $other as $key => $value ) {
  139. if ( 'title' === $key ) {
  140. $value = '"' . $value . '"';
  141. }
  142. $header .= '; ' . $key . '=' . $value;
  143. }
  144. $this->header( 'Link', $header, false );
  145. }
  146. /**
  147. * Retrieves the route that was used.
  148. *
  149. * @since 4.4.0
  150. *
  151. * @return string The matched route.
  152. */
  153. public function get_matched_route() {
  154. return $this->matched_route;
  155. }
  156. /**
  157. * Sets the route (regex for path) that caused the response.
  158. *
  159. * @since 4.4.0
  160. *
  161. * @param string $route Route name.
  162. */
  163. public function set_matched_route( $route ) {
  164. $this->matched_route = $route;
  165. }
  166. /**
  167. * Retrieves the handler that was used to generate the response.
  168. *
  169. * @since 4.4.0
  170. *
  171. * @return null|array The handler that was used to create the response.
  172. */
  173. public function get_matched_handler() {
  174. return $this->matched_handler;
  175. }
  176. /**
  177. * Sets the handler that was responsible for generating the response.
  178. *
  179. * @since 4.4.0
  180. *
  181. * @param array $handler The matched handler.
  182. */
  183. public function set_matched_handler( $handler ) {
  184. $this->matched_handler = $handler;
  185. }
  186. /**
  187. * Checks if the response is an error, i.e. >= 400 response code.
  188. *
  189. * @since 4.4.0
  190. *
  191. * @return bool Whether the response is an error.
  192. */
  193. public function is_error() {
  194. return $this->get_status() >= 400;
  195. }
  196. /**
  197. * Retrieves a WP_Error object from the response.
  198. *
  199. * @since 4.4.0
  200. *
  201. * @return WP_Error|null WP_Error or null on not an errored response.
  202. */
  203. public function as_error() {
  204. if ( ! $this->is_error() ) {
  205. return null;
  206. }
  207. $error = new WP_Error;
  208. if ( is_array( $this->get_data() ) ) {
  209. $data = $this->get_data();
  210. $error->add( $data['code'], $data['message'], $data['data'] );
  211. if ( ! empty( $data['additional_errors'] ) ) {
  212. foreach ( $data['additional_errors'] as $err ) {
  213. $error->add( $err['code'], $err['message'], $err['data'] );
  214. }
  215. }
  216. } else {
  217. $error->add( $this->get_status(), '', array( 'status' => $this->get_status() ) );
  218. }
  219. return $error;
  220. }
  221. /**
  222. * Retrieves the CURIEs (compact URIs) used for relations.
  223. *
  224. * @since 4.5.0
  225. *
  226. * @return array Compact URIs.
  227. */
  228. public function get_curies() {
  229. $curies = array(
  230. array(
  231. 'name' => 'wp',
  232. 'href' => 'https://api.w.org/{rel}',
  233. 'templated' => true,
  234. ),
  235. );
  236. /**
  237. * Filters extra CURIEs available on REST API responses.
  238. *
  239. * CURIEs allow a shortened version of URI relations. This allows a more
  240. * usable form for custom relations than using the full URI. These work
  241. * similarly to how XML namespaces work.
  242. *
  243. * Registered CURIES need to specify a name and URI template. This will
  244. * automatically transform URI relations into their shortened version.
  245. * The shortened relation follows the format `{name}:{rel}`. `{rel}` in
  246. * the URI template will be replaced with the `{rel}` part of the
  247. * shortened relation.
  248. *
  249. * For example, a CURIE with name `example` and URI template
  250. * `http://w.org/{rel}` would transform a `http://w.org/term` relation
  251. * into `example:term`.
  252. *
  253. * Well-behaved clients should expand and normalize these back to their
  254. * full URI relation, however some naive clients may not resolve these
  255. * correctly, so adding new CURIEs may break backward compatibility.
  256. *
  257. * @since 4.5.0
  258. *
  259. * @param array $additional Additional CURIEs to register with the REST API.
  260. */
  261. $additional = apply_filters( 'rest_response_link_curies', array() );
  262. return array_merge( $curies, $additional );
  263. }
  264. }