class-wp-internal-pointers.php 4.4 KB

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