wp_check_php_version()
云策文档标注
概述
wp_check_php_version() 函数用于检查用户是否需要更新 PHP 版本,通过查询 WordPress.org 的 Serve Happy API 获取 PHP 版本状态信息,并返回包含推荐版本、最低要求等数据的数组。
关键要点
- 函数返回数组或 false:成功时返回 PHP 版本数据数组,失败时返回 false。
- 数据字段包括:recommended_version(WordPress 推荐的 PHP 版本)、minimum_version(最低要求 PHP 版本)、is_supported(是否受支持)、is_secure(是否接收安全更新)、is_acceptable(版本是否可接受)。
- 使用缓存机制:通过 site_transient 缓存 API 响应,有效期为一周,以提高性能。
- 包含过滤器:wp_is_php_version_acceptable 过滤器允许开发者自定义 PHP 版本的可接受性判断。
- 未来版本检查:函数会检查当前 PHP 版本是否低于未来最低要求(如 7.4),并设置 is_lower_than_future_minimum 标志。
代码示例
function wp_check_php_version() {
$version = PHP_VERSION;
$key = md5( $version );
$response = get_site_transient( 'php_check_' . $key );
if ( false === $response ) {
$url = 'https://api.wordpress.org/core/serve-happy/1.0/';
if ( wp_http_supports( array( 'ssl' ) ) ) {
$url = set_url_scheme( $url, 'https' );
}
$url = add_query_arg( 'php_version', $version, $url );
$response = wp_remote_get( $url );
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
return false;
}
$response = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! is_array( $response ) ) {
return false;
}
set_site_transient( 'php_check_' . $key, $response, WEEK_IN_SECONDS );
}
if ( isset( $response['is_acceptable'] ) && $response['is_acceptable'] ) {
$response['is_acceptable'] = (bool) apply_filters( 'wp_is_php_version_acceptable', true, $version );
}
$response['is_lower_than_future_minimum'] = false;
if ( version_compare( $version, '7.4', '<' ) ) {
$response['is_lower_than_future_minimum'] = true;
}
return $response;
}注意事项
- 函数依赖于外部 API 调用,网络问题或 API 不可用可能导致失败。
- 过滤器 wp_is_php_version_acceptable 仅在 API 认为版本可接受时运行,可用于加强检查但不能放宽标准。
- 缓存机制确保频繁调用时不会过度请求 API,但开发者需注意缓存更新时机。
原文内容
Checks if the user needs to update PHP.
Source
function wp_check_php_version() {
$version = PHP_VERSION;
$key = md5( $version );
$response = get_site_transient( 'php_check_' . $key );
if ( false === $response ) {
$url = 'https://api.wordpress.org/core/serve-happy/1.0/';
if ( wp_http_supports( array( 'ssl' ) ) ) {
$url = set_url_scheme( $url, 'https' );
}
$url = add_query_arg( 'php_version', $version, $url );
$response = wp_remote_get( $url );
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
return false;
}
$response = json_decode( wp_remote_retrieve_body( $response ), true );
if ( ! is_array( $response ) ) {
return false;
}
set_site_transient( 'php_check_' . $key, $response, WEEK_IN_SECONDS );
}
if ( isset( $response['is_acceptable'] ) && $response['is_acceptable'] ) {
/**
* Filters whether the active PHP version is considered acceptable by WordPress.
*
* Returning false will trigger a PHP version warning to show up in the admin dashboard to administrators.
*
* This filter is only run if the wordpress.org Serve Happy API considers the PHP version acceptable, ensuring
* that this filter can only make this check stricter, but not loosen it.
*
* @since 5.1.1
*
* @param bool $is_acceptable Whether the PHP version is considered acceptable. Default true.
* @param string $version PHP version checked.
*/
$response['is_acceptable'] = (bool) apply_filters( 'wp_is_php_version_acceptable', true, $version );
}
$response['is_lower_than_future_minimum'] = false;
// The minimum supported PHP version will be updated to 7.4 in the future. Check if the current version is lower.
if ( version_compare( $version, '7.4', '<' ) ) {
$response['is_lower_than_future_minimum'] = true;
// Force showing of warnings.
$response['is_acceptable'] = false;
}
return $response;
}
Hooks
- apply_filters( ‘wp_is_php_version_acceptable’, bool $is_acceptable, string $version )
-
Filters whether the active PHP version is considered acceptable by WordPress.
Changelog
| Version | Description |
|---|---|
| 5.1.1 | Added the ‘wp_is_php_version_acceptable’ filter. |
| 5.1.0 | Introduced. |