Filter uncanny-automator

automator_outgoing_webhook_value_of_type_float

Filters the value of a float type before it's sent in an outgoing webhook.

add_filter( 'automator_outgoing_webhook_value_of_type_float', $callback, 10, 3 );

Description

Fires when a webhook value is determined to be a float. Developers can modify the $type to 'float', the $key, or the $value. This hook allows for custom handling or transformation of float webhook data before it's sent.


Usage

add_filter( 'automator_outgoing_webhook_value_of_type_float', 'your_function_name', 10, 3 );

Parameters

$type (mixed)
This parameter specifies the intended data type for the outgoing webhook value, defaulting to 'text'.
$key (mixed)
This parameter represents the determined data type for the webhook value, defaulting to 'text' if not otherwise specified.
$value (mixed)
This parameter represents the key associated with the webhook value.

Return Value

The filtered value.


Examples

/**
 * Example filter for automator_outgoing_webhook_value_of_type_float.
 *
 * This filter allows modifying the type and value of a float when sending an outgoing webhook.
 * For instance, you might want to round the float to a specific number of decimal places
 * or format it as a string before it's sent.
 *
 * @param string $type  The current type, which will be 'float'.
 * @param string $key   The key associated with the value in the webhook payload.
 * @param float  $value The float value to be sent in the webhook.
 *
 * @return array An array containing the potentially modified type and value.
 */
add_filter(
	'automator_outgoing_webhook_value_of_type_float',
	function ( $type, $key, $value ) {
		// Example: Round the float to 2 decimal places if the key is 'price'.
		if ( 'price' === $key ) {
			$value = round( $value, 2 );
			// Optionally, you could also change the type if needed, e.g., to 'string'
			// $type = 'string';
		}

		// Example: Add a prefix to the float value if the key is 'measurement'.
		if ( 'measurement' === $key ) {
			$value = 'ml_' . $value;
			$type  = 'string'; // Changing type to string as we prepended text
		}

		// Always return the modified (or original) type and value.
		// The filter expects to return the same parameters it received,
		// potentially modified. In this case, we're returning the type
		// and the modified value.
		return array( $type, $value );
	},
	10,
	3 // Accepts $type, $key, and $value
);

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

public static function value_maybe_of_type( $key, $value ) {
		$type = 'text';

		if ( is_array( $value ) || is_object( $value ) ) {
			return apply_filters( 'automator_outgoing_webhook_value_of_type_array', $type, $key, $value );
		}

		if ( is_email( $value ) ) {
			$type = 'email';

			return apply_filters( 'automator_outgoing_webhook_value_of_type_email', $type, $key, $value );
		}

		if ( is_float( $value ) ) {
			$type = 'float';

			return apply_filters( 'automator_outgoing_webhook_value_of_type_float', $type, $key, $value );
		}

		if ( is_numeric( $value ) ) {
			$type = 'int';

			return apply_filters( 'automator_outgoing_webhook_value_of_type_int', $type, $key, $value );
		}

		if ( wp_http_validate_url( $value ) ) {
			$type = 'url';

			return apply_filters( 'automator_outgoing_webhook_value_of_type_url', $type, $key, $value );
		}

		return apply_filters( 'automator_outgoing_webhook_value_of_type_text', $type, $key, $value );
	}


Scroll to Top