Edit File by line

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