get_core_checksums()
云策文档标注
概述
get_core_checksums() 函数用于获取并缓存指定 WordPress 版本和区域设置的校验和。它通过 API 请求获取数据,并处理错误情况,返回校验和数组或失败时的 false。
关键要点
- 函数接受两个必需参数:$version(版本字符串)和 $locale(区域设置字符串)。
- 返回值为数组(成功时包含校验和)或 false(失败时)。
- 函数内部使用 wp_remote_get() 发起 HTTP 请求,支持 SSL 回退机制。
- 错误处理包括检查 wp_error 和响应码,并在 SSL 失败时触发警告。
- 相关函数包括 wp_trigger_error()、wp_doing_cron()、set_url_scheme() 等。
代码示例
function get_core_checksums( $version, $locale ) {
$http_url = 'https://api.wordpress.org/core/checksums/1.0/?' . http_build_query( compact( 'version', 'locale' ), '', '&' );
$url = $http_url;
$ssl = wp_http_supports( array( 'ssl' ) );
if ( $ssl ) {
$url = set_url_scheme( $url, 'https' );
}
$options = array(
'timeout' => wp_doing_cron() ? 30 : 3,
);
$response = wp_remote_get( $url, $options );
if ( $ssl && is_wp_error( $response ) ) {
wp_trigger_error(
__FUNCTION__,
sprintf(
/* translators: %s: Support forums URL. */
__( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the support forums.' ),
__( 'https://wordpress.org/support/forums/' )
) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
);
$response = wp_remote_get( $http_url, $options );
}
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
return false;
}
$body = trim( wp_remote_retrieve_body( $response ) );
$body = json_decode( $body, true );
if ( ! is_array( $body ) || ! isset( $body['checksums'] ) || ! is_array( $body['checksums'] ) ) {
return false;
}
return $body['checksums'];
}注意事项
- 函数在 WordPress 3.7.0 版本中引入。
- 用于核心文件校验,例如在 WP_Site_Health_Auto_Updates::test_all_files_writable()、Core_Upgrader::check_files() 和 update_core() 中。
- 请求超时设置根据是否在 cron 任务中调整(cron 时为 30 秒,否则为 3 秒)。
原文内容
Gets and caches the checksums for the given version of WordPress.
Parameters
$versionstringrequired-
Version string to query.
$localestringrequired-
Locale to query.
Source
function get_core_checksums( $version, $locale ) {
$http_url = 'https://api.wordpress.org/core/checksums/1.0/?' . http_build_query( compact( 'version', 'locale' ), '', '&' );
$url = $http_url;
$ssl = wp_http_supports( array( 'ssl' ) );
if ( $ssl ) {
$url = set_url_scheme( $url, 'https' );
}
$options = array(
'timeout' => wp_doing_cron() ? 30 : 3,
);
$response = wp_remote_get( $url, $options );
if ( $ssl && is_wp_error( $response ) ) {
wp_trigger_error(
__FUNCTION__,
sprintf(
/* translators: %s: Support forums URL. */
__( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the <a href="%s">support forums</a>.' ),
__( 'https://wordpress.org/support/forums/' )
) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ),
headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE
);
$response = wp_remote_get( $http_url, $options );
}
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
return false;
}
$body = trim( wp_remote_retrieve_body( $response ) );
$body = json_decode( $body, true );
if ( ! is_array( $body ) || ! isset( $body['checksums'] ) || ! is_array( $body['checksums'] ) ) {
return false;
}
return $body['checksums'];
}
Changelog
| Version | Description |
|---|---|
| 3.7.0 | Introduced. |