Filter uncanny-automator

automator_compression_min_bytes

Filters the minimum number of bytes required for compression.

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

Description

Filters the minimum byte size required for a string to be considered for compression. This allows developers to adjust the compression threshold. Default is 2048 bytes. A value of 0 or less will revert to the default.


Usage

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

Return Value

The filtered value.


Examples

// Increase the minimum byte threshold for compression to 4096 bytes.
// This means strings smaller than 4096 bytes will not be compressed.
add_filter( 'automator_compression_min_bytes', function( $min_bytes ) {
    return 4096;
}, 10, 1 );

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/utilities/class-automator-compression.php:39

public static function maybe_compress_string( $payload ) {

		if ( ! is_string( $payload ) ) {
			return $payload;
		}

		// Allow hosts to tune thresholds without code changes.
		$min_bytes = absint( apply_filters( 'automator_compression_min_bytes', 2048 ) );
		$level     = absint( apply_filters( 'automator_compression_level', 4 ) );

		if ( $min_bytes <= 0 ) {
			$min_bytes = 2048;
		}

		// Do nothing if zlib is unavailable or payload is too small.
		if ( ! function_exists( 'gzcompress' ) || strlen( $payload ) < $min_bytes ) {
			return $payload;
		}

		// Compress with a moderate level to reduce CPU on shared hosting.
		$compressed = @gzcompress( $payload, $level ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged

		if ( false === $compressed ) {
			return $payload;
		}

		$wrapped = self::PREFIX . base64_encode( $compressed );

		// Avoid expanding small payloads due to base64 overhead.
		if ( strlen( $wrapped ) >= strlen( $payload ) ) {
			return $payload;
		}

		return $wrapped;
	}


Scroll to Top