gravityflowincomingwebhook_response_pre_process

Description

The gravityflowincomingwebhook_response_pre_process filters the processing of the webhook to be able to override the response entirely. The filter can be very helpful if you are looking for a way to modify the $response and $request in the Incoming webhook.

Parameters

ParameterTypeDetails
$responsenullA WP_REST_Response object to override
$requestWP_REST_RequestThe entire request object.

Examples

Send the request name as an entire response

add_filter( 'gravityflowincomingwebhook_response_pre_process', 'sh_gravityflowincomingwebhook_response_pre_process', 10, 2 );
function sh_gravityflowincomingwebhook_response_pre_process( $response, $request ) {
	//Send a 'name' as response
	$response = $request['name'];
	return $response;
}

Verify request data and potential duplicate entries

add_filter( 'gravityflowincomingwebhook_response_pre_process', 'jo_gravityflowincomingwebhook_response_pre_process', 10, 2 );
        function jo_gravityflowincomingwebhook_response_pre_process( $response, $request ) {
 
            //Only process the request if it is the specific webhook route.
            if ( $request->get_route() != '/gf/v2/workflow/webhooks/26/kypvSfeJxa5ABzHIZt5Arqs8o' ) {
                return $response;
            }
     
            //Ensure we have meaningful data to compare to a GFAPI request for existing entries.
            $content_type = $request->get_content_type();
            if ( rgar( $content_type, 'subtype' ) === 'json' ) {
                $data = $request->get_json_params();
            } else {
                $data = $request->get_body_params();
            }
     
            $valid = true;
            //Verify that necessary data exists in the request for validation or entry creation.
            $required_keys = array( 'program_id', 'student_id', 'review_id' );
            foreach( $required_keys as $key ) {
                if ( ! in_array( $key, array_keys( $data ) ) ) {
                    $valid = false;
                    break;
                }
            }
             
            if ( ! $valid ) {
                $message = __( 'The request is missing required data.', 'gravityflow' );
                return new WP_REST_Response( $message, 400 );
            }
     
            //Check if the entry already exists with the same data.
            $search_criteria = array();
            $search_criteria['status'] = 'active';
            $search_criteria['field_filters'] = array(
                'mode' => 'all',
                array( 'key'   => '3', 'value' => $data['program_id'] ),
                array( 'key'   => '1', 'value' => $data['student_id'] ),
                array( 'key'   => '5', 'value' => $data['review_id'] ),
            );
     
            $entry_count = GFAPI::count_entries( 57, $search_criteria);
     
            if ( $entry_count > 0 ) {
                $message = __( 'An entry with the provided data already exists.', 'gravityflow' );
                return new WP_REST_Response( $message, 400 );
            }
     
            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?