Filter uncanny-automator

automator_outgoing_webhook_default_data_value

Filters the default value for outgoing webhook data before it's sent.

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

Description

Allows modification of default webhook data values before they are sent. Developers can alter the value, key, or type of data being sent in an outgoing webhook. This filter is applied after any quote stripping logic.


Usage

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

Parameters

$value (mixed)
This parameter contains the default value that will be used for the outgoing webhook if no specific value is provided.
$key (mixed)
This parameter holds the default value for a webhook field.
$type (mixed)
This parameter represents the unique identifier or key associated with a particular data field within the outgoing webhook.
$this (mixed)
The `$type` parameter specifies the data type of the webhook field being processed.

Return Value

The filtered value.


Examples

/**
 * Example of how to use the 'automator_outgoing_webhook_default_data_value' filter.
 * This example will prepend a prefix to specific webhook data values based on their key.
 */
add_filter(
	'automator_outgoing_webhook_default_data_value',
	function( $value, $key, $type, $webhook_instance ) {
		// Only modify the value if the key is 'user_id' or 'order_id'
		if ( in_array( $key, array( 'user_id', 'order_id' ), true ) ) {
			// Prepend a custom prefix
			return 'automation_prefix_' . $value;
		}

		// Return the original value if the conditions are not met
		return $value;
	},
	10, // Priority: default is 10
	4  // Accepted args: $value, $key, $type, $this
);

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:464

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