On this page
WP_REST_Controller::get_context_param( array $args = array() ): array
Retrieves the magical context param.
Description
Ensures consistent descriptions between endpoints, and populates enum from schema.
Parameters
$argsarray Optional-
Additional arguments for context parameter.
Default:
array()
Return
array Context parameter details.
Source
File: wp-includes/rest-api/endpoints/class-wp-rest-controller.php. View all references
public function get_context_param( $args = array() ) {
$param_details = array(
'description' => __( 'Scope under which the request is made; determines fields present in response.' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_key',
'validate_callback' => 'rest_validate_request_arg',
);
$schema = $this->get_item_schema();
if ( empty( $schema['properties'] ) ) {
return array_merge( $param_details, $args );
}
$contexts = array();
foreach ( $schema['properties'] as $attributes ) {
if ( ! empty( $attributes['context'] ) ) {
$contexts = array_merge( $contexts, $attributes['context'] );
}
}
if ( ! empty( $contexts ) ) {
$param_details['enum'] = array_unique( $contexts );
rsort( $param_details['enum'] );
}
return array_merge( $param_details, $args );
}
Related
Uses
| Uses | Description |
|---|---|
| WP_REST_Controller::get_item_schema() wp-includes/rest-api/endpoints/class-wp-rest-controller.php | Retrieves the item’s schema, conforming to JSON Schema. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
Used By
| Used By | Description |
|---|---|
| WP_REST_Controller::get_collection_params() wp-includes/rest-api/endpoints/class-wp-rest-controller.php | Retrieves the query params for the collections. |
Changelog
| Version | Description |
|---|---|
| 4.7.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_rest_controller/get_context_param