class-wp-internal-pointers.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. * remove_action( 'admin_print_footer_scripts', array( 'WP_Internal_Pointers', 'pointer_wp390_widgets' ) );
  25. *
  26. * @static
  27. *
  28. * @param string $hook_suffix The current admin page.
  29. */
  30. public static function enqueue_scripts( $hook_suffix ) {
  31. /*
  32. * Register feature pointers
  33. *
  34. * Format:
  35. * array(
  36. * hook_suffix => pointer callback
  37. * )
  38. *
  39. * Example:
  40. * array(
  41. * 'themes.php' => 'wp390_widgets'
  42. * )
  43. */
  44. $registered_pointers = array(
  45. 'index.php' => 'wp496_privacy',
  46. );
  47. // Check if screen related pointer is registered
  48. if ( empty( $registered_pointers[ $hook_suffix ] ) )
  49. return;
  50. $pointers = (array) $registered_pointers[ $hook_suffix ];
  51. /*
  52. * Specify required capabilities for feature pointers
  53. *
  54. * Format:
  55. * array(
  56. * pointer callback => Array of required capabilities
  57. * )
  58. *
  59. * Example:
  60. * array(
  61. * 'wp390_widgets' => array( 'edit_theme_options' )
  62. * )
  63. */
  64. $caps_required = array(
  65. 'wp496_privacy' => array(
  66. 'manage_privacy_options',
  67. 'export_others_personal_data',
  68. 'erase_others_personal_data',
  69. ),
  70. );
  71. // Get dismissed pointers
  72. $dismissed = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
  73. $got_pointers = false;
  74. foreach ( array_diff( $pointers, $dismissed ) as $pointer ) {
  75. if ( isset( $caps_required[ $pointer ] ) ) {
  76. foreach ( $caps_required[ $pointer ] as $cap ) {
  77. if ( ! current_user_can( $cap ) )
  78. continue 2;
  79. }
  80. }
  81. // Bind pointer print function
  82. add_action( 'admin_print_footer_scripts', array( 'WP_Internal_Pointers', 'pointer_' . $pointer ) );
  83. $got_pointers = true;
  84. }
  85. if ( ! $got_pointers )
  86. return;
  87. // Add pointers script and style to queue
  88. wp_enqueue_style( 'wp-pointer' );
  89. wp_enqueue_script( 'wp-pointer' );
  90. }
  91. /**
  92. * Print the pointer JavaScript data.
  93. *
  94. * @since 3.3.0
  95. *
  96. * @static
  97. *
  98. * @param string $pointer_id The pointer ID.
  99. * @param string $selector The HTML elements, on which the pointer should be attached.
  100. * @param array $args Arguments to be passed to the pointer JS (see wp-pointer.js).
  101. */
  102. private static function print_js( $pointer_id, $selector, $args ) {
  103. if ( empty( $pointer_id ) || empty( $selector ) || empty( $args ) || empty( $args['content'] ) )
  104. return;
  105. ?>
  106. <script type="text/javascript">
  107. (function($){
  108. var options = <?php echo wp_json_encode( $args ); ?>, setup;
  109. if ( ! options )
  110. return;
  111. options = $.extend( options, {
  112. close: function() {
  113. $.post( ajaxurl, {
  114. pointer: '<?php echo $pointer_id; ?>',
  115. action: 'dismiss-wp-pointer'
  116. });
  117. }
  118. });
  119. setup = function() {
  120. $('<?php echo $selector; ?>').first().pointer( options ).pointer('open');
  121. };
  122. if ( options.position && options.position.defer_loading )
  123. $(window).bind( 'load.wp-pointers', setup );
  124. else
  125. $(document).ready( setup );
  126. })( jQuery );
  127. </script>
  128. <?php
  129. }
  130. public static function pointer_wp330_toolbar() {}
  131. public static function pointer_wp330_media_uploader() {}
  132. public static function pointer_wp330_saving_widgets() {}
  133. public static function pointer_wp340_customize_current_theme_link() {}
  134. public static function pointer_wp340_choose_image_from_library() {}
  135. public static function pointer_wp350_media() {}
  136. public static function pointer_wp360_revisions() {}
  137. public static function pointer_wp360_locks() {}
  138. public static function pointer_wp390_widgets() {}
  139. public static function pointer_wp410_dfw() {}
  140. /**
  141. * Display a pointer for the new privacy tools.
  142. *
  143. * @since 4.9.6
  144. */
  145. public static function pointer_wp496_privacy() {
  146. $content = '<h3>' . __( 'Personal Data and Privacy' ) . '</h3>';
  147. $content .= '<h4>' . __( 'Personal Data Export and Erasure' ) . '</h4>';
  148. $content .= '<p>' . __( 'New <strong>Tools</strong> have been added to help you with personal data export and erasure requests.' ) . '</p>';
  149. $content .= '<h4>' . __( 'Privacy Policy' ) . '</h4>';
  150. $content .= '<p>' . __( 'Create or select your site&#8217;s privacy policy page under <strong>Settings &gt; Privacy</strong> to keep your users informed and aware.' ) . '</p>';
  151. if ( is_rtl() ) {
  152. $position = array(
  153. 'edge' => 'right',
  154. 'align' => 'bottom',
  155. );
  156. } else {
  157. $position = array(
  158. 'edge' => 'left',
  159. 'align' => 'bottom',
  160. );
  161. }
  162. $js_args = array(
  163. 'content' => $content,
  164. 'position' => $position,
  165. 'pointerClass' => 'wp-pointer arrow-bottom',
  166. 'pointerWidth' => 420,
  167. );
  168. self::print_js( 'wp496_privacy', '#menu-tools', $js_args );
  169. }
  170. /**
  171. * Prevents new users from seeing existing 'new feature' pointers.
  172. *
  173. * @since 3.3.0
  174. *
  175. * @static
  176. *
  177. * @param int $user_id User ID.
  178. */
  179. public static function dismiss_pointers_for_new_users( $user_id ) {
  180. add_user_meta( $user_id, 'dismissed_wp_pointers', 'wp496_privacy' );
  181. }
  182. }