wp_get_update_data()
云策文档标注
概述
wp_get_update_data() 函数用于收集 WordPress 中可用更新的计数和 UI 字符串,返回一个包含更新数据和标题的数组。它检查插件、主题、WordPress 核心和翻译的更新状态,并基于用户权限进行过滤。
关键要点
- 函数返回一个数组,包含 'counts'(更新计数)和 'title'(更新标题)两个键。
- 更新计数包括插件、主题、WordPress 核心和翻译的数量,以及总计。
- 函数使用 current_user_can() 检查用户权限,确保只有有权限的用户才能看到相关更新。
- 通过 get_site_transient() 和 get_core_updates() 获取更新数据。
- 支持 apply_filters('wp_get_update_data', $update_data, $titles) Hook,允许开发者过滤返回的数据。
代码示例
print_r( wp_get_update_data() );
// Return of wp_get_update_data()
Array
(
[counts] => Array
(
[plugins] => 17
[themes] => 5
[wordpress] => 1
[translations] => 0
[total] => 23
)
[title] => 1 WordPress Update, 17 Plugin Updates, 5 Theme Updates
)
原文内容
Collects counts and UI strings for available updates.
Source
function wp_get_update_data() {
$counts = array(
'plugins' => 0,
'themes' => 0,
'wordpress' => 0,
'translations' => 0,
);
$plugins = current_user_can( 'update_plugins' );
if ( $plugins ) {
$update_plugins = get_site_transient( 'update_plugins' );
if ( ! empty( $update_plugins->response ) ) {
$counts['plugins'] = count( $update_plugins->response );
}
}
$themes = current_user_can( 'update_themes' );
if ( $themes ) {
$update_themes = get_site_transient( 'update_themes' );
if ( ! empty( $update_themes->response ) ) {
$counts['themes'] = count( $update_themes->response );
}
}
$core = current_user_can( 'update_core' );
if ( $core && function_exists( 'get_core_updates' ) ) {
$update_wordpress = get_core_updates( array( 'dismissed' => false ) );
if ( ! empty( $update_wordpress )
&& ! in_array( $update_wordpress[0]->response, array( 'development', 'latest' ), true )
&& current_user_can( 'update_core' )
) {
$counts['wordpress'] = 1;
}
}
if ( ( $core || $plugins || $themes ) && wp_get_translation_updates() ) {
$counts['translations'] = 1;
}
$counts['total'] = $counts['plugins'] + $counts['themes'] + $counts['wordpress'] + $counts['translations'];
$titles = array();
if ( $counts['wordpress'] ) {
/* translators: %d: Number of available WordPress updates. */
$titles['wordpress'] = sprintf( __( '%d WordPress Update' ), $counts['wordpress'] );
}
if ( $counts['plugins'] ) {
/* translators: %d: Number of available plugin updates. */
$titles['plugins'] = sprintf( _n( '%d Plugin Update', '%d Plugin Updates', $counts['plugins'] ), $counts['plugins'] );
}
if ( $counts['themes'] ) {
/* translators: %d: Number of available theme updates. */
$titles['themes'] = sprintf( _n( '%d Theme Update', '%d Theme Updates', $counts['themes'] ), $counts['themes'] );
}
if ( $counts['translations'] ) {
$titles['translations'] = __( 'Translation Updates' );
}
$update_title = $titles ? esc_attr( implode( ', ', $titles ) ) : '';
$update_data = array(
'counts' => $counts,
'title' => $update_title,
);
/**
* Filters the returned array of update data for plugins, themes, and WordPress core.
*
* @since 3.5.0
*
* @param array $update_data {
* Fetched update data.
*
* @type int[] $counts An array of counts for available plugin, theme, and WordPress updates.
* @type string $update_title Titles of available updates.
* }
* @param array $titles An array of update counts and UI strings for available updates.
*/
return apply_filters( 'wp_get_update_data', $update_data, $titles );
}
Hooks
- apply_filters( ‘wp_get_update_data’, array $update_data, array $titles )
-
Filters the returned array of update data for plugins, themes, and WordPress core.
Changelog
| Version | Description |
|---|---|
| 3.3.0 | Introduced. |
Skip to note 2 content
Matheus Martins
The following piece of code is just an example of what is returned by wp_get_update_data() .
print_r( wp_get_update_data() ); // Return of wp_get_update_data() Array ( [counts] => Array ( [plugins] => 17 [themes] => 5 [wordpress] => 1 [translations] => 0 [total] => 23 ) [title] => 1 WordPress Update, 17 Plugin Updates, 5 Theme Updates )