ACF\AI\Abilities

Abilities::parse_abilities_json_inputpublicACF 6.8.0

Parse JSON input from query parameters for Abilities API

WordPress 6.9's Abilities API REST controller doesn't parse JSON strings from query parameters in GET requests. This filter fixes that by detecting JSON strings in the 'input' parameter and parsing them into objects/arrays.

Method of the class: Abilities{}

No Hooks.

Returns

Mixed.

Usage

$Abilities = new Abilities();
$Abilities->parse_abilities_json_input( $response, $handler, $request );
$response(mixed) (required)
Response object.
$handler(array) (required)
Route handler info.
$request(WP_REST_Request) (required)
Request object.

Changelog

Since 6.8.0 Introduced.

Abilities::parse_abilities_json_input() code ACF 6.8.8

public function parse_abilities_json_input( $response, $handler, $request ) {
	// Only process ACF abilities.
	$route = $request->get_route();
	if ( strpos( $route, '/wp-abilities/v1/abilities/acf/' ) !== 0 ) {
		return $response;
	}

	// Only process GET and DELETE requests (POST uses JSON body which is already parsed).
	if ( ! in_array( $request->get_method(), array( 'GET', 'DELETE' ), true ) ) {
		return $response;
	}

	// Get the input query parameter.
	$input = $request->get_param( 'input' );

	// If input is a string that looks like JSON, try to parse it.
	if ( is_string( $input ) && ! empty( $input ) ) {
		$first_char = substr( trim( $input ), 0, 1 );
		// Check if it starts with { or [ (JSON object or array).
		if ( in_array( $first_char, array( '{', '[' ), true ) ) {
			$parsed = json_decode( $input, true );
			if ( json_last_error() === JSON_ERROR_NONE ) {
				// Successfully parsed JSON - update the request parameter.
				$request->set_param( 'input', $parsed );
			}
		}
	}

	return $response;
}