函数文档

wp_ajax_wp_compression_test()

💡 云策文档标注

概述

wp_ajax_wp_compression_test() 是一个用于通过 AJAX 处理压缩测试的 WordPress 函数。它检查服务器压缩配置,并根据测试参数更新 can_compress_scripts 选项,以决定是否启用脚本压缩。

关键要点

  • 函数通过 AJAX 处理压缩测试,仅允许具有 manage_options 权限的用户访问。
  • 检查服务器是否已启用 zlib.output_compression 或使用 ob_gzhandler,如果是则禁用脚本压缩。
  • 根据 GET 参数 test 的值执行不同操作:输出测试字符串、测试压缩支持或更新 can_compress_scripts 选项。
  • 支持 deflate 和 gzip 压缩,优先使用 deflate(如果可用且未强制 gzip)。
  • 在单站点和多站点环境中分别使用 update_option() 和 update_site_option() 更新选项。

代码示例

if ( isset( $_GET['test'] ) ) {
    header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
    header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
    header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
    header( 'Content-Type: application/javascript; charset=UTF-8' );
    $force_gzip = ( defined( 'ENFORCE_GZIP' ) && ENFORCE_GZIP );
    $test_str   = '...'; // 长测试字符串

    if ( '1' === $_GET['test'] ) {
        echo $test_str;
        wp_die();
    } elseif ( '2' === $_GET['test'] ) {
        if ( ! isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
            wp_die( -1 );
        }

        if ( false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate' ) && function_exists( 'gzdeflate' ) && ! $force_gzip ) {
            header( 'Content-Encoding: deflate' );
            $out = gzdeflate( $test_str, 1 );
        } elseif ( false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) && function_exists( 'gzencode' ) ) {
            header( 'Content-Encoding: gzip' );
            $out = gzencode( $test_str, 1 );
        } else {
            wp_die( -1 );
        }

        echo $out;
        wp_die();
    } elseif ( 'no' === $_GET['test'] ) {
        check_ajax_referer( 'update_can_compress_scripts' );
        if ( is_multisite() ) {
            update_site_option( 'can_compress_scripts', 0 );
        } else {
            update_option( 'can_compress_scripts', 0, true );
        }
    } elseif ( 'yes' === $_GET['test'] ) {
        check_ajax_referer( 'update_can_compress_scripts' );
        if ( is_multisite() ) {
            update_site_option( 'can_compress_scripts', 1 );
        } else {
            update_option( 'can_compress_scripts', 1, true );
        }
    }
}

注意事项

  • 函数使用 wp_die() 终止执行,并可能返回 -1 或 0 作为错误代码。
  • 在更新 can_compress_scripts 选项时,需要验证 AJAX nonce 以确保安全性。
  • 测试字符串为固定内容,用于压缩测试,开发者不应修改。

📄 原文内容

Handles compression testing via AJAX.

Source

function wp_ajax_wp_compression_test() {
	if ( ! current_user_can( 'manage_options' ) ) {
		wp_die( -1 );
	}

	if ( ini_get( 'zlib.output_compression' ) || 'ob_gzhandler' === ini_get( 'output_handler' ) ) {
		// Use `update_option()` on single site to mark the option for autoloading.
		if ( is_multisite() ) {
			update_site_option( 'can_compress_scripts', 0 );
		} else {
			update_option( 'can_compress_scripts', 0, true );
		}
		wp_die( 0 );
	}

	if ( isset( $_GET['test'] ) ) {
		header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
		header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
		header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
		header( 'Content-Type: application/javascript; charset=UTF-8' );
		$force_gzip = ( defined( 'ENFORCE_GZIP' ) && ENFORCE_GZIP );
		$test_str   = '"wpCompressionTest Lorem ipsum dolor sit amet consectetuer mollis sapien urna ut a. Eu nonummy condimentum fringilla tempor pretium platea vel nibh netus Maecenas. Hac molestie amet justo quis pellentesque est ultrices interdum nibh Morbi. Cras mattis pretium Phasellus ante ipsum ipsum ut sociis Suspendisse Lorem. Ante et non molestie. Porta urna Vestibulum egestas id congue nibh eu risus gravida sit. Ac augue auctor Ut et non a elit massa id sodales. Elit eu Nulla at nibh adipiscing mattis lacus mauris at tempus. Netus nibh quis suscipit nec feugiat eget sed lorem et urna. Pellentesque lacus at ut massa consectetuer ligula ut auctor semper Pellentesque. Ut metus massa nibh quam Curabitur molestie nec mauris congue. Volutpat molestie elit justo facilisis neque ac risus Ut nascetur tristique. Vitae sit lorem tellus et quis Phasellus lacus tincidunt nunc Fusce. Pharetra wisi Suspendisse mus sagittis libero lacinia Integer consequat ac Phasellus. Et urna ac cursus tortor aliquam Aliquam amet tellus volutpat Vestibulum. Justo interdum condimentum In augue congue tellus sollicitudin Quisque quis nibh."';

		if ( '1' === $_GET['test'] ) {
			echo $test_str;
			wp_die();
		} elseif ( '2' === $_GET['test'] ) {
			if ( ! isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
				wp_die( -1 );
			}

			if ( false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'deflate' ) && function_exists( 'gzdeflate' ) && ! $force_gzip ) {
				header( 'Content-Encoding: deflate' );
				$out = gzdeflate( $test_str, 1 );
			} elseif ( false !== stripos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) && function_exists( 'gzencode' ) ) {
				header( 'Content-Encoding: gzip' );
				$out = gzencode( $test_str, 1 );
			} else {
				wp_die( -1 );
			}

			echo $out;
			wp_die();
		} elseif ( 'no' === $_GET['test'] ) {
			check_ajax_referer( 'update_can_compress_scripts' );
			// Use `update_option()` on single site to mark the option for autoloading.
			if ( is_multisite() ) {
				update_site_option( 'can_compress_scripts', 0 );
			} else {
				update_option( 'can_compress_scripts', 0, true );
			}
		} elseif ( 'yes' === $_GET['test'] ) {
			check_ajax_referer( 'update_can_compress_scripts' );
			// Use `update_option()` on single site to mark the option for autoloading.
			if ( is_multisite() ) {
				update_site_option( 'can_compress_scripts', 1 );
			} else {
				update_option( 'can_compress_scripts', 1, true );
			}
		}
	}

	wp_die( 0 );
}

Changelog

Version Description
3.1.0 Introduced.