class-wp-internal-pointers.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * Administration API: WP_Internal_Pointers class
  4. *
  5. * @package WordPress
  6. * @subpackage Administration
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core class used to implement an internal admin pointers API.
  11. *
  12. * @since 3.3.0
  13. */
  14. #[AllowDynamicProperties]
  15. final class WP_Internal_Pointers {
  16. /**
  17. * Initializes the new feature pointers.
  18. *
  19. * @since 3.3.0
  20. *
  21. * All pointers can be disabled using the following:
  22. * remove_action( 'admin_enqueue_scripts', array( 'WP_Internal_Pointers', 'enqueue_scripts' ) );
  23. *
  24. * Individual pointers (e.g. wp390_widgets) can be disabled using the following:
  25. *
  26. * function yourprefix_remove_pointers() {
  27. * remove_action(
  28. * 'admin_print_footer_scripts',
  29. * array( 'WP_Internal_Pointers', 'pointer_wp390_widgets' )
  30. * );
  31. * }
  32. * add_action( 'admin_enqueue_scripts', 'yourprefix_remove_pointers', 11 );
  33. *
  34. * @param string $hook_suffix The current admin page.
  35. */
  36. public static function enqueue_scripts( $hook_suffix ) {
  37. /*
  38. * Register feature pointers
  39. *
  40. * Format:
  41. * array(
  42. * hook_suffix => pointer callback
  43. * )
  44. *
  45. * Example:
  46. * array(
  47. * 'themes.php' => 'wp390_widgets'
  48. * )
  49. */
  50. $registered_pointers = array(
  51. // None currently.
  52. );
  53. // Check if screen related pointer is registered.
  54. if ( empty( $registered_pointers[ $hook_suffix ] ) ) {
  55. return;
  56. }
  57. $pointers = (array) $registered_pointers[ $hook_suffix ];
  58. /*
  59. * Specify required capabilities for feature pointers
  60. *
  61. * Format:
  62. * array(
  63. * pointer callback => Array of required capabilities
  64. * )
  65. *
  66. * Example:
  67. * array(
  68. * 'wp390_widgets' => array( 'edit_theme_options' )
  69. * )
  70. */
  71. $caps_required = array(
  72. // None currently.
  73. );
  74. // Get dismissed pointers.
  75. $dismissed = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
  76. $got_pointers = false;
  77. foreach ( array_diff( $pointers, $dismissed ) as $pointer ) {
  78. if ( isset( $caps_required[ $pointer ] ) ) {
  79. foreach ( $caps_required[ $pointer ] as $cap ) {
  80. if ( ! current_user_can( $cap ) ) {
  81. continue 2;
  82. }
  83. }
  84. }
  85. // Bind pointer print function.
  86. add_action( 'admin_print_footer_scripts', array( 'WP_Internal_Pointers', 'pointer_' . $pointer ) );
  87. $got_pointers = true;
  88. }
  89. if ( ! $got_pointers ) {
  90. return;
  91. }
  92. // Add pointers script and style to queue.
  93. wp_enqueue_style( 'wp-pointer' );
  94. wp_enqueue_script( 'wp-pointer' );
  95. }
  96. /**
  97. * Print the pointer JavaScript data.
  98. *
  99. * @since 3.3.0
  100. *
  101. * @param string $pointer_id The pointer ID.
  102. * @param string $selector The HTML elements, on which the pointer should be attached.
  103. * @param array $args Arguments to be passed to the pointer JS (see wp-pointer.js).
  104. */
  105. private static function print_js( $pointer_id, $selector, $args ) {
  106. if ( empty( $pointer_id ) || empty( $selector ) || empty( $args ) || empty( $args['content'] ) ) {
  107. return;
  108. }
  109. ?>
  110. <script type="text/javascript">
  111. (function($){
  112. var options = <?php echo wp_json_encode( $args ); ?>, setup;
  113. if ( ! options )
  114. return;
  115. options = $.extend( options, {
  116. close: function() {
  117. $.post( ajaxurl, {
  118. pointer: '<?php echo $pointer_id; ?>',
  119. action: 'dismiss-wp-pointer'
  120. });
  121. }
  122. });
  123. setup = function() {
  124. $('<?php echo $selector; ?>').first().pointer( options ).pointer('open');
  125. };
  126. if ( options.position && options.position.defer_loading )
  127. $(window).bind( 'load.wp-pointers', setup );
  128. else
  129. $( function() {
  130. setup();
  131. } );
  132. })( jQuery );
  133. </script>
  134. <?php
  135. }
  136. public static function pointer_wp330_toolbar() {}
  137. public static function pointer_wp330_media_uploader() {}
  138. public static function pointer_wp330_saving_widgets() {}
  139. public static function pointer_wp340_customize_current_theme_link() {}
  140. public static function pointer_wp340_choose_image_from_library() {}
  141. public static function pointer_wp350_media() {}
  142. public static function pointer_wp360_revisions() {}
  143. public static function pointer_wp360_locks() {}
  144. public static function pointer_wp390_widgets() {}
  145. public static function pointer_wp410_dfw() {}
  146. public static function pointer_wp496_privacy() {}
  147. /**
  148. * Prevents new users from seeing existing 'new feature' pointers.
  149. *
  150. * @since 3.3.0
  151. *
  152. * @param int $user_id User ID.
  153. */
  154. public static function dismiss_pointers_for_new_users( $user_id ) {
  155. add_user_meta( $user_id, 'dismissed_wp_pointers', '' );
  156. }
  157. }