Make WordPress Core

Opened 9 years ago

Closed 7 years ago

#35312 closed enhancement (wontfix)

Support for transforming REST API request arguments

Reported by: danielbachhuber's profile danielbachhuber Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: REST API Keywords: needs-patch
Focuses: Cc:

Description

The REST API controllers have a fair amount of procedural transformation code like this:

/**
 * Prepend internal property prefix to query parameters to match our response fields.
 *
 * @param  string $query_param
 * @return string $normalized
 */
protected function normalize_query_param( $query_param ) {
	$prefix = 'comment_';

	switch ( $query_param ) {
		case 'id':
			$normalized = $prefix . 'ID';
			break;
		case 'post':
			$normalized = $prefix . 'post_ID';
			break;
		case 'parent':
			$normalized = $prefix . 'parent';
			break;
		default:
			$normalized = $prefix . $query_param;
			break;
	}

	return $normalized;
}

What if I could just define some transform_to argument when registering my request arguments, and have the request data transformed before it gets to my callback?

Originally https://github.com/WP-API/WP-API/issues/1236

Change History (2)

This ticket was mentioned in Slack in #core-restapi by kadamwhite. View the logs.


8 years ago

#2 @rmccue
7 years ago

  • Milestone Future Release deleted
  • Resolution set to wontfix
  • Status changed from new to closed

I'm going to close this out as a wontfix. While there are a bunch of spots which have code repetition, they usually turn out to be full of special cases that make a generic transform_to useless. Where this isn't the case, the code is usually pretty clean anyway, or could simply use a foreach loop.

I don't see the need for this as a general-purpose argument in parameter registration. This could always be implemented as a plugin if you really want it.

Note: See TracTickets for help on using tickets.