gravityflowincomingwebhook_response_post_process

Description

The gravityflowincomingwebhook_response_post_process filter allows the WP_REST_Response object to be modified after processing.

Parameters

ParameterTypeDetails
$responseWP_REST_ResponseThe current response
$entryArrayThe current entry array.
$dataArrayThe request data.
$requestWP_REST_RequestThe entire request object.

Examples

Adding an entry ID to the response body

add_filter('gravityflowincomingwebhook_response_post_process', 'jo_incoming_webhook_response_example', 10, 4);
function jo_incoming_webhook_response_example( $response, $entry, $data, $request ) {
	if ( $entry['form_id'] == '96' ) {
		if ( ! is_wp_error( $response ) && isset( $response->data ) && is_array( $response->data ) ) {
			$response->data['entry_id'] = $entry['id'];
		}
	}

	return $response;
}

Use request data to define entry creator by user ID

This approach could also be used with small changes to lookup based on any data that get_user_by or the WP_User has as long as the $entry[‘created_by’] update is a user ID.

add_filter('gravityflowincomingwebhook_response_post_process', 'jo_incoming_webhook_response_example', 10, 4);

	function jo_incoming_webhook_response_example( $response, $entry, $data, $request ) {
		//Change to match your form ID
		if ( $entry['form_id'] == '1' ) {
			if ( ! is_wp_error( $response ) && isset( $response->data ) && is_array( $response->data ) ) {

				$content_type = $request->get_content_type();

				if ( rgar( $content_type, 'subtype' ) === 'json' ) {
					$request_data = $request->get_json_params();
				} else {
					$request_data = $request->get_body_params();
				}

				//Change to match the key of your request structure
				if ( isset( $request_data['creator'] ) ) {

					$potential = get_user_by( 'id', (int) $request_data['creator'] );
					if ( $potential ) {
						$entry['created_by'] = $request_data['creator'];
						$update = GFAPI::update_entry( $entry );

						if ( is_wp_error( $update ) ) {
							$response->data['error'] = $update->get_error_message();
						} else {
							$response->data['entry_id'] = $entry['id'];
						}
					}
				}
			}
		}
	
		return $response;
	}

Placement

This code can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.

See also the PHP section in this article: Where Do I Put This Code?