get_core_updates()
云策文档标注
概述
get_core_updates() 函数用于获取可用的 WordPress 核心更新。它通过参数控制是否包含已忽略的更新,并返回更新对象数组或失败时返回 false。
关键要点
- 函数返回可用的核心更新信息,基于 update_core 瞬态和 dismissed_update_core 选项。
- 参数 $options 可选,可设置 'dismissed' 和 'available' 键来筛选更新。
- 默认情况下,仅返回未忽略的可用更新;若 $options['dismissed'] 为 true,则包含已忽略的更新。
- 函数内部跳过 'autoupdate' 类型的更新,并标记更新对象的 dismissed 属性。
代码示例
function get_core_updates( $options = array() ) {
$options = array_merge(
array(
'available' => true,
'dismissed' => false,
),
$options
);
$dismissed = get_site_option( 'dismissed_update_core' );
if ( ! is_array( $dismissed ) ) {
$dismissed = array();
}
$from_api = get_site_transient( 'update_core' );
if ( ! isset( $from_api->updates ) || ! is_array( $from_api->updates ) ) {
return false;
}
$updates = $from_api->updates;
$result = array();
foreach ( $updates as $update ) {
if ( 'autoupdate' === $update->response ) {
continue;
}
if ( array_key_exists( $update->current . '|' . $update->locale, $dismissed ) ) {
if ( $options['dismissed'] ) {
$update->dismissed = true;
$result[] = $update;
}
} else {
if ( $options['available'] ) {
$update->dismissed = false;
$result[] = $update;
}
}
}
return $result;
}注意事项
- 函数依赖于 get_site_transient('update_core') 和 get_site_option('dismissed_update_core'),确保这些数据已正确设置。
- 返回的更新对象包含 current、locale 和 response 等属性,可用于进一步处理。
- 在 WordPress 2.7.0 版本中引入,兼容后续版本。
原文内容
Gets available core updates.
Parameters
$optionsarrayoptional-
Set $options[
'dismissed'] to true to show dismissed upgrades too, set $options['available'] to false to skip not-dismissed updates.Default:
array()
Source
function get_core_updates( $options = array() ) {
$options = array_merge(
array(
'available' => true,
'dismissed' => false,
),
$options
);
$dismissed = get_site_option( 'dismissed_update_core' );
if ( ! is_array( $dismissed ) ) {
$dismissed = array();
}
$from_api = get_site_transient( 'update_core' );
if ( ! isset( $from_api->updates ) || ! is_array( $from_api->updates ) ) {
return false;
}
$updates = $from_api->updates;
$result = array();
foreach ( $updates as $update ) {
if ( 'autoupdate' === $update->response ) {
continue;
}
if ( array_key_exists( $update->current . '|' . $update->locale, $dismissed ) ) {
if ( $options['dismissed'] ) {
$update->dismissed = true;
$result[] = $update;
}
} else {
if ( $options['available'] ) {
$update->dismissed = false;
$result[] = $update;
}
}
}
return $result;
}
Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |