/wp-admin/includes/screen.php

  1. <?php 
  2. /** 
  3. * WordPress Administration Screen API. 
  4. * 
  5. * @package WordPress 
  6. * @subpackage Administration 
  7. */ 
  8.  
  9. /** 
  10. * Get the column headers for a screen 
  11. * 
  12. * @since 2.7.0 
  13. * 
  14. * @staticvar array $column_headers 
  15. * 
  16. * @param string|WP_Screen $screen The screen you want the headers for 
  17. * @return array Containing the headers in the format id => UI String 
  18. */ 
  19. function get_column_headers( $screen ) { 
  20. if ( is_string( $screen ) ) 
  21. $screen = convert_to_screen( $screen ); 
  22.  
  23. static $column_headers = array(); 
  24.  
  25. if ( ! isset( $column_headers[ $screen->id ] ) ) { 
  26.  
  27. /** 
  28. * Filters the column headers for a list table on a specific screen. 
  29. * 
  30. * The dynamic portion of the hook name, `$screen->id`, refers to the 
  31. * ID of a specific screen. For example, the screen ID for the Posts 
  32. * list table is edit-post, so the filter for that screen would be 
  33. * manage_edit-post_columns. 
  34. * 
  35. * @since 3.0.0 
  36. * 
  37. * @param array $columns An array of column headers. Default empty. 
  38. */ 
  39. $column_headers[ $screen->id ] = apply_filters( "manage_{$screen->id}_columns", array() ); 
  40.  
  41. return $column_headers[ $screen->id ]; 
  42.  
  43. /** 
  44. * Get a list of hidden columns. 
  45. * 
  46. * @since 2.7.0 
  47. * 
  48. * @param string|WP_Screen $screen The screen you want the hidden columns for 
  49. * @return array 
  50. */ 
  51. function get_hidden_columns( $screen ) { 
  52. if ( is_string( $screen ) ) { 
  53. $screen = convert_to_screen( $screen ); 
  54.  
  55. $hidden = get_user_option( 'manage' . $screen->id . 'columnshidden' ); 
  56.  
  57. $use_defaults = ! is_array( $hidden ); 
  58.  
  59. if ( $use_defaults ) { 
  60. $hidden = array(); 
  61.  
  62. /** 
  63. * Filters the default list of hidden columns. 
  64. * 
  65. * @since 4.4.0 
  66. * 
  67. * @param array $hidden An array of columns hidden by default. 
  68. * @param WP_Screen $screen WP_Screen object of the current screen. 
  69. */ 
  70. $hidden = apply_filters( 'default_hidden_columns', $hidden, $screen ); 
  71.  
  72. /** 
  73. * Filters the list of hidden columns. 
  74. * 
  75. * @since 4.4.0 
  76. * @since 4.4.1 Added the `use_defaults` parameter. 
  77. * 
  78. * @param array $hidden An array of hidden columns. 
  79. * @param WP_Screen $screen WP_Screen object of the current screen. 
  80. * @param bool $use_defaults Whether to show the default columns. 
  81. */ 
  82. return apply_filters( 'hidden_columns', $hidden, $screen, $use_defaults ); 
  83.  
  84. /** 
  85. * Prints the meta box preferences for screen meta. 
  86. * 
  87. * @since 2.7.0 
  88. * 
  89. * @global array $wp_meta_boxes 
  90. * 
  91. * @param WP_Screen $screen 
  92. */ 
  93. function meta_box_prefs( $screen ) { 
  94. global $wp_meta_boxes; 
  95.  
  96. if ( is_string( $screen ) ) 
  97. $screen = convert_to_screen( $screen ); 
  98.  
  99. if ( empty($wp_meta_boxes[$screen->id]) ) 
  100. return; 
  101.  
  102. $hidden = get_hidden_meta_boxes($screen); 
  103.  
  104. foreach ( array_keys( $wp_meta_boxes[ $screen->id ] ) as $context ) { 
  105. foreach ( array( 'high', 'core', 'default', 'low' ) as $priority ) { 
  106. if ( ! isset( $wp_meta_boxes[ $screen->id ][ $context ][ $priority ] ) ) { 
  107. continue; 
  108. foreach ( $wp_meta_boxes[ $screen->id ][ $context ][ $priority ] as $box ) { 
  109. if ( false == $box || ! $box['title'] ) 
  110. continue; 
  111. // Submit box cannot be hidden 
  112. if ( 'submitdiv' == $box['id'] || 'linksubmitdiv' == $box['id'] ) 
  113. continue; 
  114.  
  115. $widget_title = $box['title']; 
  116.  
  117. if ( is_array( $box['args'] ) && isset( $box['args']['__widget_basename'] ) ) { 
  118. $widget_title = $box['args']['__widget_basename']; 
  119.  
  120. printf( 
  121. '<label for="%1$s-hide"><input class="hide-postbox-tog" name="%1$s-hide" type="checkbox" id="%1$s-hide" value="%1$s" %2$s />%3$s</label>',  
  122. esc_attr( $box['id'] ),  
  123. checked( in_array( $box['id'], $hidden ), false, false ),  
  124. $widget_title 
  125. ); 
  126.  
  127. /** 
  128. * Get Hidden Meta Boxes 
  129. * 
  130. * @since 2.7.0 
  131. * 
  132. * @param string|WP_Screen $screen Screen identifier 
  133. * @return array Hidden Meta Boxes 
  134. */ 
  135. function get_hidden_meta_boxes( $screen ) { 
  136. if ( is_string( $screen ) ) 
  137. $screen = convert_to_screen( $screen ); 
  138.  
  139. $hidden = get_user_option( "metaboxhidden_{$screen->id}" ); 
  140.  
  141. $use_defaults = ! is_array( $hidden ); 
  142.  
  143. // Hide slug boxes by default 
  144. if ( $use_defaults ) { 
  145. $hidden = array(); 
  146. if ( 'post' == $screen->base ) { 
  147. if ( 'post' == $screen->post_type || 'page' == $screen->post_type || 'attachment' == $screen->post_type ) 
  148. $hidden = array('slugdiv', 'trackbacksdiv', 'postcustom', 'postexcerpt', 'commentstatusdiv', 'commentsdiv', 'authordiv', 'revisionsdiv'); 
  149. else 
  150. $hidden = array( 'slugdiv' ); 
  151.  
  152. /** 
  153. * Filters the default list of hidden meta boxes. 
  154. * 
  155. * @since 3.1.0 
  156. * 
  157. * @param array $hidden An array of meta boxes hidden by default. 
  158. * @param WP_Screen $screen WP_Screen object of the current screen. 
  159. */ 
  160. $hidden = apply_filters( 'default_hidden_meta_boxes', $hidden, $screen ); 
  161.  
  162. /** 
  163. * Filters the list of hidden meta boxes. 
  164. * 
  165. * @since 3.3.0 
  166. * 
  167. * @param array $hidden An array of hidden meta boxes. 
  168. * @param WP_Screen $screen WP_Screen object of the current screen. 
  169. * @param bool $use_defaults Whether to show the default meta boxes. 
  170. * Default true. 
  171. */ 
  172. return apply_filters( 'hidden_meta_boxes', $hidden, $screen, $use_defaults ); 
  173.  
  174. /** 
  175. * Register and configure an admin screen option 
  176. * 
  177. * @since 3.1.0 
  178. * 
  179. * @param string $option An option name. 
  180. * @param mixed $args Option-dependent arguments. 
  181. */ 
  182. function add_screen_option( $option, $args = array() ) { 
  183. $current_screen = get_current_screen(); 
  184.  
  185. if ( ! $current_screen ) 
  186. return; 
  187.  
  188. $current_screen->add_option( $option, $args ); 
  189.  
  190. /** 
  191. * Get the current screen object 
  192. * 
  193. * @since 3.1.0 
  194. * 
  195. * @global WP_Screen $current_screen 
  196. * 
  197. * @return WP_Screen|null Current screen object or null when screen not defined. 
  198. */ 
  199. function get_current_screen() { 
  200. global $current_screen; 
  201.  
  202. if ( ! isset( $current_screen ) ) 
  203. return null; 
  204.  
  205. return $current_screen; 
  206.  
  207. /** 
  208. * Set the current screen object 
  209. * 
  210. * @since 3.0.0 
  211. * 
  212. * @param mixed $hook_name Optional. The hook name (also known as the hook suffix) used to determine the screen,  
  213. * or an existing screen object. 
  214. */ 
  215. function set_current_screen( $hook_name = '' ) { 
  216. WP_Screen::get( $hook_name )->set_current_screen(); 
.