wp_dashboard_cached_rss_widget()
云策文档标注
概述
wp_dashboard_cached_rss_widget() 函数用于检查 RSS 源 URL 是否已缓存,并根据缓存状态输出仪表板小部件内容。它支持通过回调函数显示缓存内容,或在未缓存时输出加载提示,并通过 Ajax 更新。
关键要点
- 函数检查 $check_urls 数组中的 RSS 源 URL 是否已缓存,若为空则从仪表板小部件选项中获取 URL。
- 若缓存存在,则调用 $callback 回调函数输出内容;否则输出“Loading…”提示,后续由 Ajax 替换。
- 使用 Transient API 缓存输出,默认缓存时间为 12 小时,缓存键基于小部件 ID 和用户区域设置。
- 参数包括 $widget_id(字符串,必需)、$callback(可调用,必需)、$check_urls(数组,可选,默认空数组)和 ...$args(可变参数,可选)。
- 返回布尔值:成功时返回 true,失败时返回 false。
代码示例
function wp_dashboard_cached_rss_widget( $widget_id, $callback, $check_urls = array(), ...$args ) {
$doing_ajax = wp_doing_ajax();
$loading = '<div class="rss-widget">' . __( 'Loading…' ) . '</div>';
$loading .= wp_get_admin_notice(
__( 'This widget requires JavaScript.' ),
array(
'type' => 'error',
'additional_classes' => array( 'inline', 'hide-if-js' ),
)
);
if ( empty( $check_urls ) ) {
$widgets = get_option( 'dashboard_widget_options' );
if ( empty( $widgets[ $widget_id ]['url'] ) && ! $doing_ajax ) {
echo $loading;
return false;
}
$check_urls = array( $widgets[ $widget_id ]['url'] );
}
$locale = get_user_locale();
$cache_key = 'dash_v2_' . md5( $widget_id . '_' . $locale );
$output = get_transient( $cache_key );
if ( false !== $output ) {
echo $output;
return true;
}
if ( ! $doing_ajax ) {
echo $loading;
return false;
}
if ( $callback && is_callable( $callback ) ) {
array_unshift( $args, $widget_id, $check_urls );
ob_start();
call_user_func_array( $callback, $args );
set_transient( $cache_key, ob_get_flush(), 12 * HOUR_IN_SECONDS );
}
return true;
}注意事项
- 函数在非 Ajax 请求且无缓存时输出加载提示,确保前端用户体验。
- 缓存键包含用户区域设置,支持多语言环境下的内容缓存。
- 从 WordPress 5.3.0 开始,...$args 参数已正式添加到函数签名中。
原文内容
Checks to see if all of the feed url in $check_urls are cached.
Description
If $check_urls is empty, look for the rss feed url found in the dashboard widget options of $widget_id. If cached, call $callback, a function that echoes out output for this widget. If not cache, echo a “Loading…” stub which is later replaced by Ajax call (see top of /wp-admin/index.php)
Parameters
$widget_idstringrequired-
The widget ID.
$callbackcallablerequired-
The callback function used to display each feed.
$check_urlsarrayoptional-
RSS feeds.
Default:
array() $argsmixedoptional-
Optional additional parameters to pass to the callback function.
Source
function wp_dashboard_cached_rss_widget( $widget_id, $callback, $check_urls = array(), ...$args ) {
$doing_ajax = wp_doing_ajax();
$loading = '<p class="widget-loading hide-if-no-js">' . __( 'Loading…' ) . '</p>';
$loading .= wp_get_admin_notice(
__( 'This widget requires JavaScript.' ),
array(
'type' => 'error',
'additional_classes' => array( 'inline', 'hide-if-js' ),
)
);
if ( empty( $check_urls ) ) {
$widgets = get_option( 'dashboard_widget_options' );
if ( empty( $widgets[ $widget_id ]['url'] ) && ! $doing_ajax ) {
echo $loading;
return false;
}
$check_urls = array( $widgets[ $widget_id ]['url'] );
}
$locale = get_user_locale();
$cache_key = 'dash_v2_' . md5( $widget_id . '_' . $locale );
$output = get_transient( $cache_key );
if ( false !== $output ) {
echo $output;
return true;
}
if ( ! $doing_ajax ) {
echo $loading;
return false;
}
if ( $callback && is_callable( $callback ) ) {
array_unshift( $args, $widget_id, $check_urls );
ob_start();
call_user_func_array( $callback, $args );
// Default lifetime in cache of 12 hours (same as the feeds).
set_transient( $cache_key, ob_get_flush(), 12 * HOUR_IN_SECONDS );
}
return true;
}