wp_dashboard_browser_nag()
云策文档标注
概述
wp_dashboard_browser_nag() 函数用于在 WordPress 仪表板中显示浏览器更新提示,根据浏览器版本和安全性生成相应通知消息。
关键要点
- 函数调用 wp_check_browser_version() 检查浏览器版本,返回数组或 false。
- 针对 Internet Explorer 用户显示特殊消息,建议切换到现代浏览器。
- 根据浏览器是否不安全或过时,生成不同的本地化提示消息。
- 支持通过 'browse-happy-notice' 过滤器钩子自定义通知输出。
- 函数内部处理了浏览器图标、本地化链接和用户交互(如“Dismiss”按钮)。
代码示例
function wp_dashboard_browser_nag() {
global $is_IE;
$notice = '';
$response = wp_check_browser_version();
if ( $response ) {
if ( $is_IE ) {
$msg = __( 'Internet Explorer does not give you the best WordPress experience. Switch to Microsoft Edge, or another more modern browser to get the most from your site.' );
} elseif ( $response['insecure'] ) {
$msg = sprintf(
/* translators: %s: Browser name and link. */
__( "It looks like you're using an insecure version of %s. Using an outdated browser makes your computer unsafe. For the best WordPress experience, please update your browser." ),
sprintf( '%s', esc_url( $response['update_url'] ), esc_html( $response['name'] ) )
);
} else {
$msg = sprintf(
/* translators: %s: Browser name and link. */
__( "It looks like you're using an old version of %s. For the best WordPress experience, please update your browser." ),
sprintf( '%s', esc_url( $response['update_url'] ), esc_html( $response['name'] ) )
);
}
$browser_nag_class = '';
if ( ! empty( $response['img_src'] ) ) {
$img_src = ( is_ssl() && ! empty( $response['img_src_ssl'] ) ) ? $response['img_src_ssl'] : $response['img_src'];
$notice .= '';
$browser_nag_class = ' has-browser-icon';
}
$notice .= "{$msg}";
$browsehappy = 'https://browsehappy.com/';
$locale = get_user_locale();
if ( 'en_US' !== $locale ) {
$browsehappy = add_query_arg( 'locale', $locale, $browsehappy );
}
if ( $is_IE ) {
$msg_browsehappy = sprintf(
/* translators: %s: Browse Happy URL. */
__( 'Learn how to browse happy' ),
esc_url( $browsehappy )
);
} else {
$msg_browsehappy = sprintf(
/* translators: 1: Browser update URL, 2: Browser name, 3: Browse Happy URL. */
__( 'Update %2$s or learn how to browse happy' ),
esc_attr( $response['update_url'] ),
esc_html( $response['name'] ),
esc_url( $browsehappy )
);
}
$notice .= '' . $msg_browsehappy . '';
$notice .= '' . __( 'Dismiss' ) . '';
$notice .= '';
}
/**
* Filters the notice output for the 'Browse Happy' nag meta box.
*
* @since 3.2.0
*
* @param string $notice The notice content.
* @param array|false $response An array containing web browser information, or
* false on failure. See wp_check_browser_version().
*/
echo apply_filters( 'browse-happy-notice', $notice, $response ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
}注意事项
- 函数依赖于 wp_check_browser_version() 返回的数据,需确保该函数正常工作。
- 通知内容可通过 'browse-happy-notice' 过滤器进行自定义,参数为 $notice 和 $response。
- 在 WordPress 5.8.0 版本中,为 Internet Explorer 用户添加了特殊消息。
- 代码中使用了多个转义函数(如 esc_url、esc_html)以确保安全性。
原文内容
Displays the browser update nag.
Source
function wp_dashboard_browser_nag() {
global $is_IE;
$notice = '';
$response = wp_check_browser_version();
if ( $response ) {
if ( $is_IE ) {
$msg = __( 'Internet Explorer does not give you the best WordPress experience. Switch to Microsoft Edge, or another more modern browser to get the most from your site.' );
} elseif ( $response['insecure'] ) {
$msg = sprintf(
/* translators: %s: Browser name and link. */
__( "It looks like you're using an insecure version of %s. Using an outdated browser makes your computer unsafe. For the best WordPress experience, please update your browser." ),
sprintf( '<a href="%s">%s</a>', esc_url( $response['update_url'] ), esc_html( $response['name'] ) )
);
} else {
$msg = sprintf(
/* translators: %s: Browser name and link. */
__( "It looks like you're using an old version of %s. For the best WordPress experience, please update your browser." ),
sprintf( '<a href="%s">%s</a>', esc_url( $response['update_url'] ), esc_html( $response['name'] ) )
);
}
$browser_nag_class = '';
if ( ! empty( $response['img_src'] ) ) {
$img_src = ( is_ssl() && ! empty( $response['img_src_ssl'] ) ) ? $response['img_src_ssl'] : $response['img_src'];
$notice .= '<div class="alignright browser-icon"><img src="' . esc_url( $img_src ) . '" alt="" /></div>';
$browser_nag_class = ' has-browser-icon';
}
$notice .= "<p class='browser-update-nag{$browser_nag_class}'>{$msg}</p>";
$browsehappy = 'https://browsehappy.com/';
$locale = get_user_locale();
if ( 'en_US' !== $locale ) {
$browsehappy = add_query_arg( 'locale', $locale, $browsehappy );
}
if ( $is_IE ) {
$msg_browsehappy = sprintf(
/* translators: %s: Browse Happy URL. */
__( 'Learn how to <a href="%s" class="update-browser-link">browse happy</a>' ),
esc_url( $browsehappy )
);
} else {
$msg_browsehappy = sprintf(
/* translators: 1: Browser update URL, 2: Browser name, 3: Browse Happy URL. */
__( '<a href="%1$s" class="update-browser-link">Update %2$s</a> or learn how to <a href="%3$s" class="browse-happy-link">browse happy</a>' ),
esc_attr( $response['update_url'] ),
esc_html( $response['name'] ),
esc_url( $browsehappy )
);
}
$notice .= '<p>' . $msg_browsehappy . '</p>';
$notice .= '<p class="hide-if-no-js"><a href="" class="dismiss" aria-label="' . esc_attr__( 'Dismiss the browser warning panel' ) . '">' . __( 'Dismiss' ) . '</a></p>';
$notice .= '<div class="clear"></div>';
}
/**
* Filters the notice output for the 'Browse Happy' nag meta box.
*
* @since 3.2.0
*
* @param string $notice The notice content.
* @param array|false $response An array containing web browser information, or
* false on failure. See wp_check_browser_version().
*/
echo apply_filters( 'browse-happy-notice', $notice, $response ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
}
Hooks
- apply_filters( ‘browse-happy-notice’, string $notice, array|false $response )
-
Filters the notice output for the ‘Browse Happy’ nag meta box.