Filter uncanny-automator

automator_send_webhook_binary_to_string_format

Filters the binary data before sending it as a string in a webhook to modify its format.

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

Description

Allows modification of the format string used by the pack function when converting binary webhook data to a string. Developers can alter the `pack` format to control how binary data is interpreted and encoded. The default format is 'H*'.


Usage

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

Parameters

$string (mixed)
This parameter specifies the format string for the `pack()` function, which is used to format the binary data into a string.

Return Value

The filtered value.


Examples

/**
 * Modify the format string for converting binary data to text.
 *
 * This example demonstrates how to change the format string from 'H*' to 'a*'
 * to interpret the binary data as ASCII characters instead of hexadecimal pairs.
 *
 * @param string $format The current format string passed to pack(). Defaults to 'H*'.
 * @param string|null $current_string The string being built so far.
 * @return string The modified format string.
 */
add_filter( 'automator_send_webhook_binary_to_string_format', function( $format, $current_string ) {
    // Check if we are dealing with binary strings that should be interpreted as ASCII.
    // In a real-world scenario, you might check a specific condition or a global setting.
    // For this example, we'll assume if the $current_string is null (meaning it's the first iteration),
    // we want to switch to ASCII interpretation.
    if ( $current_string === null ) {
        return 'a*'; // Change format to interpret as ASCII characters.
    }
    return $format; // Otherwise, keep the default format.
}, 10, 2 ); // Priority 10, accepts 2 arguments.

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

private function binary_to_text( $binary ) {
		$binaries = explode( ' ', $binary );

		$string = null;
		foreach ( $binaries as $binary ) {
			$string .= pack( apply_filters( 'automator_send_webhook_binary_to_string_format', 'H*', $string ), dechex( bindec( $binary ) ) ); // phpcs:ignore PHPCompatibility.ParameterValues.NewPackFormat.NewFormatFound
		}

		return $string;
	}


Scroll to Top