WP_Internal_Pointers
云策文档标注
概述
WP_Internal_Pointers 是 WordPress 核心类,用于实现内部管理指针 API,主要用于显示新功能提示。该类提供方法来初始化和控制指针的显示,包括禁用指针和设置权限要求。
关键要点
- WP_Internal_Pointers 是一个 final 类,用于管理后台新功能指针。
- 主要方法包括 enqueue_scripts 用于初始化指针,print_js 用于输出 JavaScript 数据。
- 指针可以通过 remove_action 全局或单独禁用,例如移除 enqueue_scripts 或特定指针回调。
- 指针显示受用户能力和已关闭指针列表控制,通过 get_user_meta 获取 dismissed_wp_pointers。
- 类中包含多个指针回调方法,如 pointer_wp390_widgets,对应不同 WordPress 版本的新功能。
代码示例
// 禁用所有指针
remove_action( 'admin_enqueue_scripts', array( 'WP_Internal_Pointers', 'enqueue_scripts' ) );
// 禁用特定指针(例如 wp390_widgets)
function yourprefix_remove_pointers() {
remove_action(
'admin_print_footer_scripts',
array( 'WP_Internal_Pointers', 'pointer_wp390_widgets' )
);
}
add_action( 'admin_enqueue_scripts', 'yourprefix_remove_pointers', 11 );注意事项
- 指针注册通过 $registered_pointers 数组配置,当前示例中为空,但格式为 hook_suffix => pointer callback。
- 权限要求通过 $caps_required 数组设置,指针回调对应所需能力数组。
- 指针数据输出需确保 $pointer_id、$selector 和 $args 非空,否则 print_js 方法会提前返回。
原文内容
Core class used to implement an internal admin pointers API.
Methods
| Name | Description |
|---|---|
| WP_Internal_Pointers::dismiss_pointers_for_new_users | Prevents new users from seeing existing ‘new feature’ pointers. |
| WP_Internal_Pointers::enqueue_scripts | Initializes the new feature pointers. |
| WP_Internal_Pointers::pointer_wp330_media_uploader | – |
| WP_Internal_Pointers::pointer_wp330_saving_widgets | – |
| WP_Internal_Pointers::pointer_wp330_toolbar | – |
| WP_Internal_Pointers::pointer_wp340_choose_image_from_library | – |
| WP_Internal_Pointers::pointer_wp340_customize_current_theme_link | – |
| WP_Internal_Pointers::pointer_wp350_media | – |
| WP_Internal_Pointers::pointer_wp360_locks | – |
| WP_Internal_Pointers::pointer_wp360_revisions | – |
| WP_Internal_Pointers::pointer_wp390_widgets | – |
| WP_Internal_Pointers::pointer_wp410_dfw | – |
| WP_Internal_Pointers::pointer_wp496_privacy | – |
| WP_Internal_Pointers::print_js | Prints the pointer JavaScript data. |
Source
final class WP_Internal_Pointers {
/**
* Initializes the new feature pointers.
*
* @since 3.3.0
*
* All pointers can be disabled using the following:
* remove_action( 'admin_enqueue_scripts', array( 'WP_Internal_Pointers', 'enqueue_scripts' ) );
*
* Individual pointers (e.g. wp390_widgets) can be disabled using the following:
*
* function yourprefix_remove_pointers() {
* remove_action(
* 'admin_print_footer_scripts',
* array( 'WP_Internal_Pointers', 'pointer_wp390_widgets' )
* );
* }
* add_action( 'admin_enqueue_scripts', 'yourprefix_remove_pointers', 11 );
*
* @param string $hook_suffix The current admin page.
*/
public static function enqueue_scripts( $hook_suffix ) {
/*
* Register feature pointers
*
* Format:
* array(
* hook_suffix => pointer callback
* )
*
* Example:
* array(
* 'themes.php' => 'wp390_widgets'
* )
*/
$registered_pointers = array(
// None currently.
);
// Check if screen related pointer is registered.
if ( empty( $registered_pointers[ $hook_suffix ] ) ) {
return;
}
$pointers = (array) $registered_pointers[ $hook_suffix ];
/*
* Specify required capabilities for feature pointers
*
* Format:
* array(
* pointer callback => Array of required capabilities
* )
*
* Example:
* array(
* 'wp390_widgets' => array( 'edit_theme_options' )
* )
*/
$caps_required = array(
// None currently.
);
// Get dismissed pointers.
$dismissed = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
$got_pointers = false;
foreach ( array_diff( $pointers, $dismissed ) as $pointer ) {
if ( isset( $caps_required[ $pointer ] ) ) {
foreach ( $caps_required[ $pointer ] as $cap ) {
if ( ! current_user_can( $cap ) ) {
continue 2;
}
}
}
// Bind pointer print function.
add_action( 'admin_print_footer_scripts', array( 'WP_Internal_Pointers', 'pointer_' . $pointer ) );
$got_pointers = true;
}
if ( ! $got_pointers ) {
return;
}
// Add pointers script and style to queue.
wp_enqueue_style( 'wp-pointer' );
wp_enqueue_script( 'wp-pointer' );
}
/**
* Prints the pointer JavaScript data.
*
* @since 3.3.0
*
* @param string $pointer_id The pointer ID.
* @param string $selector The HTML elements, on which the pointer should be attached.
* @param array $args Arguments to be passed to the pointer JS (see wp-pointer.js).
*/
private static function print_js( $pointer_id, $selector, $args ) {
if ( empty( $pointer_id ) || empty( $selector ) || empty( $args ) || empty( $args['content'] ) ) {
return;
}
?>
(function($){
var options = , setup;
if ( ! options )
return;
options = $.extend( options, {
close: function() {
$.post( ajaxurl, {
pointer: '',
action: 'dismiss-wp-pointer'
});
}
});
setup = function() {
$('').first().pointer( options ).pointer('open');
};
if ( options.position && options.position.defer_loading )
$(window).bind( 'load.wp-pointers', setup );
else
$( function() {
setup();
} );
})( jQuery );
Changelog
| Version | Description |
|---|---|
| 3.3.0 | Introduced. |