ma['type'] = $matching_schema['type'];
}
$response_data = rest_filter_response_by_context( $response_data, $matching_schema, $context );
}
}
if ( ! is_array( $response_data ) && ! is_object( $response_data ) ) {
return $response_data;
}
if ( isset( $schema['type'] ) ) {
$type = $schema['type'];
} elseif ( isset( $schema['properties'] ) ) {
$type = 'object'; // Back compat if a developer accidentally omitted the type.
} else {
return $response_data;
}
$is_array_type = 'array' === $type || ( is_array( $type ) && in_array( 'array', $type, true ) );
$is_object_type = 'object' === $type || ( is_array( $type ) && in_array( 'object', $type, true ) );
if ( $is_array_type && $is_object_type ) {
if ( rest_is_array( $response_data ) ) {
$is_object_type = false;
} else {
$is_array_type = false;
}
}
$has_additional_properties = $is_object_type && isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] );
foreach ( $response_data as $key => $value ) {
$check = array();
if ( $is_array_type ) {
$check = isset( $schema['items'] ) ? $schema['items'] : array();
} elseif ( $is_object_type ) {
if ( isset( $schema['properties'][ $key ] ) ) {
$check = $schema['properties'][ $key ];
} else {
$pattern_property_schema = rest_find_matching_pattern_property_schema( $key, $schema );
if ( null !== $pattern_property_schema ) {
$check = $pattern_property_schema;
} elseif ( $has_additional_properties ) {
$check = $schema['additionalProperties'];
}
}
}
if ( ! isset( $check['context'] ) ) {
continue;
}
if ( ! in_array( $context, $check['context'], true ) ) {
if ( $is_array_type ) {
// All array items share schema, so there's no need to check each one.
$response_data = array();
break;
}
if ( is_object( $response_data ) ) {
unset( $response_data->$key );
} else {
unset( $response_data[ $key ] );
}
} elseif ( is_array( $value ) || is_object( $value ) ) {
$new_value = rest_filter_response_by_context( $value, $check, $context );
if ( is_object( $response_data ) ) {
$response_data->$key = $new_value;
} else {
$response_data[ $key ] = $new_value;
}
}
}
return $response_data;
}
/**
* Sets the "additionalProperties" to false by default for all object definitions in the schema.
*
* @since 5.5.0
* @since 5.6.0 Support the "patternProperties" keyword.
*
* @param array $schema The schema to modify.
* @return array The modified schema.
*/
function rest_default_additional_properties_to_false( $schema ) {
$type = (array) $schema['type'];
if ( in_array( 'object', $type, true ) ) {
if ( isset( $schema['properties'] ) ) {
foreach ( $schema['properties'] as $key => $child_schema ) {
$schema['properties'][ $key ] = rest_default_additional_properties_to_false( $child_schema );
}
}
if ( isset( $schema['patternProperties'] ) ) {
foreach ( $schema['patternProperties'] as $key => $child_schema ) {
$schema['patternProperties'][ $key ] = rest_default_additional_properties_to_false( $child_schema );
}
}
if ( ! isset( $schema['additionalProperties'] ) ) {
$schema['additionalProperties'] = false;
}
}
if ( in_array( 'array', $type, true ) ) {
if ( isset( $schema['items'] ) ) {
$schema['items'] = rest_default_additional_properties_to_false( $schema['items'] );
}
}
return $schema;
}
/**
* Gets the REST API route for a post.
*
* @since 5.5.0
*
* @param int|WP_Post $post Post ID or post object.
* @return string The route path with a leading slash for the given post,
* or an empty string if there is not a route.
*/
function rest_get_route_for_post( $post ) {
$post = get_post( $post );
if ( ! $post instanceof WP_Post ) {
return '';
}
$post_type_route = rest_get_route_for_post_type_items( $post->post_type );
if ( ! $post_type_route ) {
return '';
}
$route = sprintf( '%s/%d', $post_type_route, $post->ID );
/**
* Filters the REST API route for a post.
*
* @since 5.5.0
*
* @param string $route The route path.
* @param WP_Post $post The post object.
*/
return apply_filters( 'rest_route_for_post', $route, $post );
}
/**
* Gets the REST API route for a post type.
*
* @since 5.9.0
*
* @param string $post_type The name of a registered post type.
* @return string The route path with a leading slash for the given post type,
* or an empty string if there is not a route.
*/
function rest_get_route_for_post_type_items( $post_type ) {
$post_type = get_post_type_object( $post_type );
if ( ! $post_type ) {
return '';
}
if ( ! $post_type->show_in_rest ) {
return '';
}
$namespace = ! empty( $post_type->rest_namespace ) ? $post_type->rest_namespace : 'wp/v2';
$rest_base = ! empty( $post_type->rest_base ) ? $post_type->rest_base : $post_type->name;
$route = sprintf( '/%s/%s', $namespace, $rest_base );
/**
* Filters the REST API route for a post type.
*
* @since 5.9.0
*
* @param string $route The route path.
* @param WP_Post_Type $post_type The post type object.
*/
return apply_filters( 'rest_route_for_post_type_items', $route, $post_type );
}
/**
* Gets the REST API route for a term.
*
* @since 5.5.0
*
* @param int|WP_Term $term Term ID or term object.
* @return string The route path with a leading slash for the given term,
* or an empty string if there is not a route.
*/
function rest_get_route_for_term( $term ) {
$term = get_term( $term );
if ( ! $term instanceof WP_Term ) {
return '';
}
$taxonomy_route = rest_get_route_for_taxonomy_items( $term->taxonomy );
if ( ! $taxonomy_route ) {
return '';
}
$route = sprintf( '%s/%d', $taxonomy_route, $term->term_id );
/**
* Filters the REST API route for a term.
*
* @since 5.5.0
*
* @param string $route The route path.
* @param WP_Term $term The term object.
*/
return apply_filters( 'rest_route_for_term', $route, $term );
}
/**
* Gets the REST API route for a taxonomy.
*
* @since 5.9.0
*
* @param string $taxonomy Name of taxonomy.
* @return string The route path with a leading slash for the given taxonomy.
*/
function rest_get_route_for_taxonomy_items( $taxonomy ) {
$taxonomy = get_taxonomy( $taxonomy );
if ( ! $taxonomy ) {
return '';
}
if ( ! $taxonomy->show_in_rest ) {
return '';
}
$namespace = ! empty( $taxonomy->rest_namespace ) ? $taxonomy->rest_namespace : 'wp/v2';
$rest_base = ! empty( $taxonomy->rest_base ) ? $taxonomy->rest_base : $taxonomy->name;
$route = sprintf( '/%s/%s', $namespace, $rest_base );
/**
* Filters the REST API route for a taxonomy.
*
* @since 5.9.0
*
* @param string $route The route path.
* @param WP_Taxonomy $taxonomy The taxonomy object.
*/
return apply_filters( 'rest_route_for_taxonomy_items', $route, $taxonomy );
}
/**
* Gets the REST route for the currently queried object.
*
* @since 5.5.0
*
* @return string The REST route of the resource, or an empty string if no resource identified.
*/
function rest_get_queried_resource_route() {
if ( is_singular() ) {
$route = rest_get_route_for_post( get_queried_object() );
} elseif ( is_category() || is_tag() || is_tax() ) {
$route = rest_get_route_for_term( get_queried_object() );
} elseif ( is_author() ) {
$route = '/wp/v2/users/' . get_queried_object_id();
} else {
$route = '';
}
/**
* Filters the REST route for the currently queried object.
*
* @since 5.5.0
*
* @param string $link The route with a leading slash, or an empty string.
*/
return apply_filters( 'rest_queried_resource_route', $route );
}
/**
* Retrieves an array of endpoint arguments from the item schema and endpoint method.
*
* @since 5.6.0
*
* @param array $schema The full JSON schema for the endpoint.
* @param string $method Optional. HTTP method of the endpoint. The arguments for `CREATABLE` endpoints are
* checked for required values and may fall-back to a given default, this is not done
* on `EDITABLE` endpoints. Default WP_REST_Server::CREATABLE.
* @return array The endpoint arguments.
*/
function rest_get_endpoint_args_for_schema( $schema, $method = WP_REST_Server::CREATABLE ) {
$schema_properties = ! empty( $schema['properties'] ) ? $schema['properties'] : array();
$endpoint_args = array();
$valid_schema_properties = rest_get_allowed_schema_keywords();
$valid_schema_properties = array_diff( $valid_schema_properties, array( 'default', 'required' ) );
foreach ( $schema_properties as $field_id => $params ) {
// Arguments specified as `readonly` are not allowed to be set.
if ( ! empty( $params['readonly'] ) ) {
continue;
}
$endpoint_args[ $field_id ] = array(
'validate_callback' => 'rest_validate_request_arg',
'sanitize_callback' => 'rest_sanitize_request_arg',
);
if ( WP_REST_Server::CREATABLE === $method && isset( $params['default'] ) ) {
$endpoint_args[ $field_id ]['default'] = $params['default'];
}
if ( WP_REST_Server::CREATABLE === $method && ! empty( $params['required'] ) ) {
$endpoint_args[ $field_id ]['required'] = true;
}
foreach ( $valid_schema_properties as $schema_prop ) {
if ( isset( $params[ $schema_prop ] ) ) {
$endpoint_args[ $field_id ][ $schema_prop ] = $params[ $schema_prop ];
}
}
// Merge in any options provided by the schema property.
if ( isset( $params['arg_options'] ) ) {
// Only use required / default from arg_options on CREATABLE endpoints.
if ( WP_REST_Server::CREATABLE !== $method ) {
$params['arg_options'] = array_diff_key(
$params['arg_options'],
array(
'required' => '',
'default' => '',
)
);
}
$endpoint_args[ $field_id ] = array_merge( $endpoint_args[ $field_id ], $params['arg_options'] );
}
}
return $endpoint_args;
}
/**
* Converts an error to a response object.
*
* This iterates over all error codes and messages to change it into a flat
* array. This enables simpler client behavior, as it is represented as a
* list in JSON rather than an object/map.
*
* @since 5.7.0
*
* @param WP_Error $error WP_Error instance.
*
* @return WP_REST_Response List of associative arrays with code and message keys.
*/
function rest_convert_error_to_response( $error ) {
$status = array_reduce(
$error->get_all_error_data(),
static function ( $status, $error_data ) {
return is_array( $error_data ) && isset( $error_data['status'] ) ? $error_data['status'] : $status;
},
500
);
$errors = array();
foreach ( (array) $error->errors as $code => $messages ) {
$all_data = $error->get_all_error_data( $code );
$last_data = array_pop( $all_data );
foreach ( (array) $messages as $message ) {
$formatted = array(
'code' => $code,
'message' => $message,
'data' => $last_data,
);
if ( $all_data ) {
$formatted['additional_data'] = $all_data;
}
$errors[] = $formatted;
}
}
$data = $errors[0];
if ( count( $errors ) > 1 ) {
// Remove the primary error.
array_shift( $errors );
$data['additional_errors'] = $errors;
}
return new WP_REST_Response( $data, $status );
}
/**
* Checks whether a REST API endpoint request is currently being handled.
*
* This may be a standalone REST API request, or an internal request dispatched from within a regular page load.
*
* @since 6.5.0
*
* @global WP_REST_Server $wp_rest_server REST server instance.
*
* @return bool True if a REST endpoint request is currently being handled, false otherwise.
*/
function wp_is_rest_endpoint() {
/* @var WP_REST_Server $wp_rest_server */
global $wp_rest_server;
// Check whether this is a standalone REST request.
$is_rest_endpoint = wp_is_serving_rest_request();
if ( ! $is_rest_endpoint ) {
// Otherwise, check whether an internal REST request is currently being handled.
$is_rest_endpoint = isset( $wp_rest_server )
&& $wp_rest_server->is_dispatching();
}
/**
* Filters whether a REST endpoint request is currently being handled.
*
* This may be a standalone REST API request, or an internal request dispatched from within a regular page load.
*
* @since 6.5.0
*
* @param bool $is_request_endpoint Whether a REST endpoint request is currently being handled.
*/
return (bool) apply_filters( 'wp_is_rest_endpoint', $is_rest_endpoint );
}