Filter uncanny-automator

automator_send_webhook_string_to_binary_format

Filters the webhook string before it's converted to a binary format for sending.

add_filter( 'automator_send_webhook_string_to_binary_format', $callback, 10, 1 );

Description

Filters the format string used for converting characters to binary representation during webhook data processing. Developers can modify the format string ('H*' by default) to alter how individual characters are unpacked and subsequently converted to binary, allowing for custom character encoding handling in outgoing webhooks.


Usage

add_filter( 'automator_send_webhook_string_to_binary_format', 'your_function_name', 10, 1 );

Parameters

$string (mixed)
This parameter specifies the format string used by PHP's `unpack()` function to interpret the binary data of each character from the input string.

Return Value

The filtered value.


Examples

add_filter( 'automator_send_webhook_string_to_binary_format', 'my_custom_automator_webhook_binary_format', 10, 2 );

/**
 * Example of how to modify the format used for converting strings to binary for webhooks.
 *
 * This function changes the unpack format from 'H*' (which expects hex representation)
 * to 'a*' (which expects a string). This might be useful if the original string
 * is not guaranteed to be a valid hex string, and you want to directly convert
 * character by character.
 *
 * @param string $format The current format string for unpack.
 * @param string $original_string The original string being converted.
 * @return string The modified format string.
 */
function my_custom_automator_webhook_binary_format( $format, $original_string ) {
    // If the original string is empty, there's no point in changing the format.
    if ( empty( $original_string ) ) {
        return $format;
    }

    // Let's assume we want to treat each character as its ASCII value and convert that.
    // The 'H*' format in the original code is meant to unpack hex.
    // If we want to ensure each character is processed individually, we might use 'a'
    // and let unpack treat it as a string for a single character.
    // However, the current loop iterates through `str_split()`, meaning $character is already one character.
    // The 'H*' format expects the *entire* string to be a hex representation.
    // This means if the original_string was 'AB', 'H*' would try to unpack it as 'A' and 'B' hex.
    //
    // A more realistic scenario might be to ensure that the unpack format is always safe for single characters,
    // or to handle specific encoding issues.
    //
    // For this example, let's say we want to be absolutely sure we're treating each character's
    // ASCII representation and converting it. The original 'H*' is problematic if $string
    // itself isn't hex.
    //
    // A more robust approach for character-by-character binary conversion would be to
    // directly get the ASCII value and convert. However, this filter is designed to
    // change the *unpack format*.
    //
    // If the intention is to ensure each *character* is treated as a single unit for unpacking,
    // and the original string might not be hex, we could modify the format to expect
    // a single character. 'c' unpacks a signed char, 'C' unpacks an unsigned char.
    // Let's use 'C' to get the ASCII value of each character.
    //
    // NOTE: The original code uses 'H*', which implies it expects the *entire* string passed
    // to unpack to be a hex string. Since the loop does `str_split($string)` and then
    // `unpack(..., $character)`, this means `unpack('H*', $character)` is actually
    // unpacking a single character *as if it were a hex string*. This is likely the
    // intended behavior of the original code, assuming each character *is* meant to be
    // interpreted as a hex representation of something.
    //
    // If we want to change the *interpretation* of what's being unpacked from hex to ASCII,
    // we would change the format specifier. For example, 'C' unpacks an unsigned char.
    //
    // Let's demonstrate changing it to 'C' to get the ASCII value of the character.
    // This would mean $data[1] will be the ASCII decimal value, which is then converted to binary.
    return 'C';
}

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

private function string_to_binary_conversion( $string ) {
		$characters = str_split( $string );

		$binary = array();
		foreach ( $characters as $character ) {
			$data     = unpack( apply_filters( 'automator_send_webhook_string_to_binary_format', 'H*', $string ), $character );
			$binary[] = base_convert( $data[1], 16, 2 );
		}

		return implode( ' ', $binary );
	}


Scroll to Top