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

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

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