函数文档

wp_is_ini_value_changeable()

💡 云策文档标注

概述

wp_is_ini_value_changeable() 函数用于检查 PHP ini 设置是否可在运行时更改。它通过分析 ini_get_all() 返回的访问权限信息来判断,并优雅处理函数禁用情况。

关键要点

  • 函数接受一个字符串参数 $setting,表示要检查的 ini 设置名称。
  • 返回布尔值:如果设置可在运行时更改(访问权限为 INI_ALL 或 INI_USER),则返回 true;否则返回 false。
  • 内部使用静态变量缓存 ini_get_all() 结果以提高性能。
  • 如果 ini_get_all() 被禁用,函数会优雅地假设设置可更改,返回 true。

代码示例

function wp_is_ini_value_changeable( $setting ) {
    static $ini_all;

    if ( ! isset( $ini_all ) ) {
        $ini_all = false;
        // Sometimes `ini_get_all()` is disabled via the `disable_functions` option for "security purposes".
        if ( function_exists( 'ini_get_all' ) ) {
            $ini_all = ini_get_all();
        }
    }

    if ( isset( $ini_all[ $setting ]['access'] )
        && ( INI_ALL === $ini_all[ $setting ]['access'] || INI_USER === $ini_all[ $setting ]['access'] )
    ) {
        return true;
    }

    // If we were unable to retrieve the details, fail gracefully to assume it's changeable.
    if ( ! is_array( $ini_all ) ) {
        return true;
    }

    return false;
}

注意事项

  • 该函数从 WordPress 4.6.0 版本开始引入。
  • 相关函数包括 wp_raise_memory_limit() 和 wp_initial_constants(),用于内存限制提升和常量定义。
  • 在安全配置下,ini_get_all() 可能被禁用,函数会默认返回 true 以避免错误。

📄 原文内容

Determines whether a PHP ini value is changeable at runtime.

Parameters

$settingstringrequired
The name of the ini setting to check.

Return

bool True if the value is changeable at runtime. False otherwise.

Source

function wp_is_ini_value_changeable( $setting ) {
	static $ini_all;

	if ( ! isset( $ini_all ) ) {
		$ini_all = false;
		// Sometimes `ini_get_all()` is disabled via the `disable_functions` option for "security purposes".
		if ( function_exists( 'ini_get_all' ) ) {
			$ini_all = ini_get_all();
		}
	}

	if ( isset( $ini_all[ $setting ]['access'] )
		&& ( INI_ALL === $ini_all[ $setting ]['access'] || INI_USER === $ini_all[ $setting ]['access'] )
	) {
		return true;
	}

	// If we were unable to retrieve the details, fail gracefully to assume it's changeable.
	if ( ! is_array( $ini_all ) ) {
		return true;
	}

	return false;
}

Changelog

Version Description
4.6.0 Introduced.