wp/wp-includes/rest-api/endpoints/class-wp-rest-controller.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    48 	public function register_routes() {
    48 	public function register_routes() {
    49 		_doing_it_wrong(
    49 		_doing_it_wrong(
    50 			'WP_REST_Controller::register_routes',
    50 			'WP_REST_Controller::register_routes',
    51 			/* translators: %s: register_routes() */
    51 			/* translators: %s: register_routes() */
    52 			sprintf( __( "Method '%s' must be overridden." ), __METHOD__ ),
    52 			sprintf( __( "Method '%s' must be overridden." ), __METHOD__ ),
    53 			'4.7'
    53 			'4.7.0'
    54 		);
    54 		);
    55 	}
    55 	}
    56 
    56 
    57 	/**
    57 	/**
    58 	 * Checks if a given request has access to get items.
    58 	 * Checks if a given request has access to get items.
   441 	 *
   441 	 *
   442 	 * @since 4.7.0
   442 	 * @since 4.7.0
   443 	 *
   443 	 *
   444 	 * @param object          $object  Data model like WP_Term or WP_Post.
   444 	 * @param object          $object  Data model like WP_Term or WP_Post.
   445 	 * @param WP_REST_Request $request Full details about the request.
   445 	 * @param WP_REST_Request $request Full details about the request.
   446 	 * @return bool|WP_Error True on success, WP_Error object if a field cannot be updated.
   446 	 * @return true|WP_Error True on success, WP_Error object if a field cannot be updated.
   447 	 */
   447 	 */
   448 	protected function update_additional_fields_for_object( $object, $request ) {
   448 	protected function update_additional_fields_for_object( $object, $request ) {
   449 		$additional_fields = $this->get_additional_fields();
   449 		$additional_fields = $this->get_additional_fields();
   450 
   450 
   451 		foreach ( $additional_fields as $field_name => $field_options ) {
   451 		foreach ( $additional_fields as $field_name => $field_options ) {
   550 	 * Included fields are based on item schema and `_fields=` request argument.
   550 	 * Included fields are based on item schema and `_fields=` request argument.
   551 	 *
   551 	 *
   552 	 * @since 4.9.6
   552 	 * @since 4.9.6
   553 	 *
   553 	 *
   554 	 * @param WP_REST_Request $request Full details about the request.
   554 	 * @param WP_REST_Request $request Full details about the request.
   555 	 * @return array Fields to be included in the response.
   555 	 * @return string[] Fields to be included in the response.
   556 	 */
   556 	 */
   557 	public function get_fields_for_response( $request ) {
   557 	public function get_fields_for_response( $request ) {
   558 		$schema     = $this->get_item_schema();
   558 		$schema     = $this->get_item_schema();
   559 		$properties = isset( $schema['properties'] ) ? $schema['properties'] : array();
   559 		$properties = isset( $schema['properties'] ) ? $schema['properties'] : array();
   560 
   560 
   623 	 *                       checked for required values and may fall-back to a given default, this is not done
   623 	 *                       checked for required values and may fall-back to a given default, this is not done
   624 	 *                       on `EDITABLE` requests. Default WP_REST_Server::CREATABLE.
   624 	 *                       on `EDITABLE` requests. Default WP_REST_Server::CREATABLE.
   625 	 * @return array Endpoint arguments.
   625 	 * @return array Endpoint arguments.
   626 	 */
   626 	 */
   627 	public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CREATABLE ) {
   627 	public function get_endpoint_args_for_item_schema( $method = WP_REST_Server::CREATABLE ) {
   628 
   628 		return rest_get_endpoint_args_for_schema( $this->get_item_schema(), $method );
   629 		$schema                  = $this->get_item_schema();
       
   630 		$schema_properties       = ! empty( $schema['properties'] ) ? $schema['properties'] : array();
       
   631 		$endpoint_args           = array();
       
   632 		$valid_schema_properties = array(
       
   633 			'type',
       
   634 			'format',
       
   635 			'enum',
       
   636 			'items',
       
   637 			'properties',
       
   638 			'additionalProperties',
       
   639 			'minimum',
       
   640 			'maximum',
       
   641 			'exclusiveMinimum',
       
   642 			'exclusiveMaximum',
       
   643 			'minLength',
       
   644 			'maxLength',
       
   645 			'pattern',
       
   646 			'minItems',
       
   647 			'maxItems',
       
   648 			'uniqueItems',
       
   649 		);
       
   650 
       
   651 		foreach ( $schema_properties as $field_id => $params ) {
       
   652 
       
   653 			// Arguments specified as `readonly` are not allowed to be set.
       
   654 			if ( ! empty( $params['readonly'] ) ) {
       
   655 				continue;
       
   656 			}
       
   657 
       
   658 			$endpoint_args[ $field_id ] = array(
       
   659 				'validate_callback' => 'rest_validate_request_arg',
       
   660 				'sanitize_callback' => 'rest_sanitize_request_arg',
       
   661 			);
       
   662 
       
   663 			if ( isset( $params['description'] ) ) {
       
   664 				$endpoint_args[ $field_id ]['description'] = $params['description'];
       
   665 			}
       
   666 
       
   667 			if ( WP_REST_Server::CREATABLE === $method && isset( $params['default'] ) ) {
       
   668 				$endpoint_args[ $field_id ]['default'] = $params['default'];
       
   669 			}
       
   670 
       
   671 			if ( WP_REST_Server::CREATABLE === $method && ! empty( $params['required'] ) ) {
       
   672 				$endpoint_args[ $field_id ]['required'] = true;
       
   673 			}
       
   674 
       
   675 			foreach ( $valid_schema_properties as $schema_prop ) {
       
   676 				if ( isset( $params[ $schema_prop ] ) ) {
       
   677 					$endpoint_args[ $field_id ][ $schema_prop ] = $params[ $schema_prop ];
       
   678 				}
       
   679 			}
       
   680 
       
   681 			// Merge in any options provided by the schema property.
       
   682 			if ( isset( $params['arg_options'] ) ) {
       
   683 
       
   684 				// Only use required / default from arg_options on CREATABLE endpoints.
       
   685 				if ( WP_REST_Server::CREATABLE !== $method ) {
       
   686 					$params['arg_options'] = array_diff_key(
       
   687 						$params['arg_options'],
       
   688 						array(
       
   689 							'required' => '',
       
   690 							'default'  => '',
       
   691 						)
       
   692 					);
       
   693 				}
       
   694 
       
   695 				$endpoint_args[ $field_id ] = array_merge( $endpoint_args[ $field_id ], $params['arg_options'] );
       
   696 			}
       
   697 		}
       
   698 
       
   699 		return $endpoint_args;
       
   700 	}
   629 	}
   701 
   630 
   702 	/**
   631 	/**
   703 	 * Sanitizes the slug value.
   632 	 * Sanitizes the slug value.
   704 	 *
   633 	 *