Filter
uncanny-automator-pro
automator_pro_webhook_field_key_not_found
Filters when a webhook field key is not found in the provided data, allowing for custom handling.
add_filter( 'automator_pro_webhook_field_key_not_found', $callback, 10, 4 );
Description
Fires when a webhook field key is not found within the received data. Developers can use this filter to modify the "Key not found" message, alter the field being processed, or adjust the parsed data. This hook provides fine-grained control over how missing webhook data is handled by Uncanny Automator Pro.
Usage
add_filter( 'automator_pro_webhook_field_key_not_found', 'your_function_name', 10, 4 );
Parameters
-
$_field(mixed) - This parameter represents the default error message displayed when a webhook field key is not found in the provided data.
-
$parsed_fields(mixed) - This parameter represents the specific field that was not found during the webhook data parsing.
-
$_fields(mixed) - This parameter contains an array of all parsed webhook fields, allowing for iteration and checking for the existence of specific keys.
-
$data(mixed) - This parameter is a mix of all the fields that were passed into the webhook.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to use the automator_pro_webhook_field_key_not_found filter.
*
* This filter allows you to customize the value returned when a specific field key
* is not found in the parsed webhook data. In this example, we'll append a
* custom string to the default "Key not found in data" message to provide more context.
*/
add_filter( 'automator_pro_webhook_field_key_not_found', 'my_custom_key_not_found_value', 10, 5 );
/**
* Custom function to modify the value when a webhook field key is not found.
*
* @param mixed $_default_message The default message, e.g., 'Key not found in data'.
* @param object $_field The current field object being processed. It is expected to have a 'KEY' property.
* @param array $parsed_fields An array of the fields parsed from the webhook data.
* @param array $_fields An array of all field objects expected for the webhook.
* @param array $data The raw incoming webhook data.
*
* @return string The modified value to be used when the field key is not found.
*/
function my_custom_key_not_found_value( $_default_message, $_field, $parsed_fields, $_fields, $data ) {
// Check if $_field and its KEY property are valid before proceeding.
if ( ! is_object( $_field ) || ! property_exists( $_field, 'KEY' ) ) {
return $_default_message; // Return default if $_field is not as expected.
}
// Get the specific key that was not found.
$missing_key = $_field->KEY;
// You could potentially inspect the $data or $parsed_fields here to provide more specific context.
// For example, if you know the expected structure of $data, you could check if a particular
// top-level key is missing.
// Let's assume we want to add the missing key to our message.
$custom_message = sprintf(
'%s - Missing Key: %s',
$_default_message,
esc_html( $missing_key ) // Sanitize the key before outputting.
);
// You could also choose to return a specific default value like null or an empty string,
// depending on your needs.
// For this example, we are returning a more descriptive string.
return $custom_message;
}
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
uncanny-automator-pro/src/core/webhook/webhook-rest-handler.php:409
}
$params = apply_filters( 'automator_pro_webhook_params_pre_handle_params', $params, $trigger['meta'], $data );
$parsed_fields = self::handle_params( $params );
foreach ( $_fields as $_field ) {
if ( isset( $parsed_fields[ $_field->KEY ] ) ) { //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
$value = $parsed_fields[ $_field->KEY ]; //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
} else {
$value = apply_filters( 'automator_pro_webhook_field_key_not_found', __( 'Key not found in data', 'uncanny-automator-pro' ), $_field, $parsed_fields, $_fields, $data );
}
if ( is_array( $value ) ) {
$value = serialize( $value ); //phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_serialize
}
$_hooks['params'][] = array(
'key' => $_field->KEY, //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
'type' => $_field->VALUE_TYPE, //phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase