From e9684e2ed2f059653052e71c2535f170e89a0a88 Mon Sep 17 00:00:00 2001 From: Stefan Kalscheuer Date: Sun, 6 Oct 2024 14:28:22 +0200 Subject: [PATCH] fix: test if gzip extension is available (#308) If zlib PHP extension is not loaded and there is no substitute in place, gzencode() does not exist and generation of compressed files will fail. Add a simple function_exists() test to prevent this. --- inc/class-cachify-hdd.php | 5 +++++ tests/test-cachify-hdd.php | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/inc/class-cachify-hdd.php b/inc/class-cachify-hdd.php index 103234e..a82c90e 100644 --- a/inc/class-cachify-hdd.php +++ b/inc/class-cachify-hdd.php @@ -33,6 +33,11 @@ public static function is_available() { * @since 2.4.0 */ public static function is_gzip_enabled() { + if ( ! function_exists( 'gzencode' ) ) { + // GZip is not available on the system. + return false; + } + /** * Filter that allows to enable/disable gzip file creation * diff --git a/tests/test-cachify-hdd.php b/tests/test-cachify-hdd.php index e5b7ab2..b573020 100644 --- a/tests/test-cachify-hdd.php +++ b/tests/test-cachify-hdd.php @@ -33,7 +33,16 @@ public function test_stringify_method() { * Test GZip availability. */ public function test_is_gzip_enabled() { - self::assertTrue( Cachify_HDD::is_gzip_enabled(), 'GZip should be enabled by default' ); + if ( ! function_exists( 'gzencode' ) ) { + self::assertFalse( Cachify_HDD::is_gzip_enabled(), 'GZip should be disabled, if not available' ); + + // Define gzencode function for testing the hook. + function gzencode( $data, $level = -1 ) { + return $data; + } + } + + self::assertTrue( Cachify_HDD::is_gzip_enabled(), 'GZip should be enabled if available' ); $capture = null; add_filter(