Filter uncanny-automator

automator_outgoing_webhook_value

Filters the value of an outgoing webhook before it's sent, allowing customization of data.

add_filter( 'automator_outgoing_webhook_value', $callback, 10, 4 );

Description

Fires when preparing a single value for an outgoing webhook. Allows developers to modify webhook data before it's sent. You can change the value itself, or its key and type. This filter runs after default data processing.


Usage

add_filter( 'automator_outgoing_webhook_value', 'your_function_name', 10, 4 );

Parameters

$value (mixed)
This parameter contains the value of the webhook field being prepared for sending.
$key (mixed)
This parameter represents the actual value being prepared or modified for the outgoing webhook.
$type (mixed)
This parameter represents the key used to identify the specific value being processed in the outgoing webhook data.
$this (mixed)
This parameter indicates the data type of the value being prepared for the outgoing webhook.

Return Value

The filtered value.


Examples

/**
 * Example of how to filter the value sent in an outgoing webhook.
 *
 * This filter allows you to modify the value of a specific data field before it's sent
 * in an outgoing webhook. For instance, you might want to format a date,
 * anonymize sensitive information, or append a prefix to a string.
 *
 * @param mixed $value The current value of the webhook data field.
 * @param mixed $key   The key (name) of the webhook data field.
 * @param mixed $type  The type of data being processed (e.g., 'string', 'integer', 'array').
 * @param mixed $this  The instance of the Automator_Send_Webhook class.
 *
 * @return mixed The modified value to be sent in the webhook.
 */
add_filter( 'automator_outgoing_webhook_value', function ( $value, $key, $type, $send_webhook_instance ) {

	// Example: If the key is 'user_email', anonymize it by replacing with a placeholder.
	if ( 'user_email' === $key ) {
		// Ensure it's a string before performing string operations.
		if ( is_string( $value ) ) {
			// Replace the domain part with a placeholder.
			$parts = explode( '@', $value );
			if ( count( $parts ) === 2 ) {
				$value = $parts[0] . '@example.com';
			}
		}
	}

	// Example: If the value is a timestamp, format it into a readable date.
	if ( is_numeric( $value ) && $type === 'integer' && $key === 'created_at' ) {
		$value = date( 'Y-m-d H:i:s', (int) $value );
	}

	// Always return the value, whether modified or not.
	return $value;
}, 10, 4 );

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

src/core/lib/webhooks/class-automator-send-webhook.php:470

public function get_fields( $data, $legacy = false, $data_type = '', $parsing_args = array(), $is_check_sample = false ) {

		$prepared_data = array();

		if ( $legacy ) {
			return $this->prepare_legacy_fields( $data, $parsing_args );
		}

		if ( ! isset( $data['WEBHOOK_FIELDS'] ) ) {
			return $prepared_data;
		}

		$fields = ! is_array( $data['WEBHOOK_FIELDS'] ) ? json_decode( $data['WEBHOOK_FIELDS'], true ) : $data['WEBHOOK_FIELDS'];

		if ( empty( $fields ) ) {
			return $prepared_data;
		}

		foreach ( $fields as $field ) {

			$key   = isset( $field['KEY'] ) ? $this->maybe_parse_tokens( $field['KEY'], $parsing_args ) : null;
			$type  = isset( $field['VALUE_TYPE'] ) ? $this->maybe_parse_tokens( $field['VALUE_TYPE'], $parsing_args ) : 'text';
			$value = isset( $field['VALUE'] ) ? $this->maybe_parse_tokens( $field['VALUE'], $parsing_args ) : null;

			// Do not allow empty key.
			if ( '' !== $key && ! is_null( $key ) && ! is_null( $value ) ) {
				switch ( $type ) {
					case 'null':
					case 'undefined':
						$value = null;
						break;
					case 'int':
						$value = absint( $value );
						break;
					case 'float':
						$value = floatval( $value );
						break;
					case 'bool':
						$value = str_replace( array( '"', ''' ), '', html_entity_decode( $value ) );
						if ( 'true' === strtolower( $value ) || 'false' === strtolower( $value ) ) {
							$value = 'true' === strtolower( $value ) ? true : false;
						} elseif ( is_numeric( $value ) && ( 0 === absint( $value ) || 1 === absint( $value ) ) ) {
							$value = boolval( $value );
						} else {
							$value = (string) $value;
						}
						break;
					case 'text':
					default:
						/**
						 * Allows users to strip the quotes.
						 *
						 * @see <https://secure.helpscout.net/conversation/2067343003/45133?folderId=2122433>
						 */
						$should_strip_qoutes = apply_filters( 'automator_send_webhook_get_fields_should_strip_qoutes', false );

						if ( $should_strip_qoutes ) {
							// Decode HTML entities and replace " and '
							$value = str_replace( array( '"', ''' ), '', html_entity_decode( $value ) );
						}

						$value = apply_filters( 'automator_outgoing_webhook_default_data_value', (string) $value, $key, $type, $this );

						break;

				}

				$prepared_data[ $key ] = apply_filters( 'automator_outgoing_webhook_value', $value, $key, $type, $this );
			}
		}

		$prepared_data = $this->create_tree( $prepared_data, $data_type );

		return $this->format_outgoing_data( $prepared_data, $data_type, $is_check_sample );
	}


Scroll to Top