Ticket #9657: ticket.9657.2.diff

File ticket.9657.2.diff, 16.2 KB (added by ptahdunbar, 2 years ago)

second pass for screen meta api. bug fixes and improvements

Line 
1Index: wp-admin/includes/template.php
2===================================================================
3--- wp-admin/includes/template.php      (revision 17535)
4+++ wp-admin/includes/template.php      (working copy)
5@@ -1672,16 +1672,195 @@
6        return $screen;
7 }
8 
9-function screen_meta($screen) {
10-       global $wp_meta_boxes, $_wp_contextual_help, $wp_list_table, $wp_current_screen_options;
11+/**
12+ * Displays the screen meta for the current page.
13+ *
14+ * @uses do_screen_meta()
15+ *
16+ * @param string $screen The name of the screen
17+ */
18+function screen_meta( $screen ) {
19+       ?>
20+       <div id="screen-meta">
21+               <?php do_screen_meta( $screen->id ); ?>
22+       </div><!-- #screen-meta -->
23+       <?php
24+}
25 
26-       if ( is_string($screen) )
27-               $screen = convert_to_screen($screen);
28+/**
29+ * Adds a new screen meta tab.
30+ *
31+ * @since 3.2.0
32+ *
33+ * @param string $id Identifier.
34+ * @param string $label Label used for the tab's display name.
35+ * @param string $screen Id of the screen to add tab to.
36+ * @param string $capability capability required to view this tab. Viewable by all by default.
37+ * @param int $priority which one first.
38+ * @param string $screens Which page to add the tab to.
39+ */
40+function add_screen_meta_tab( $id, $label, $screen, $capability = 'read', $priority = 10 ) {
41+       global $wp_screen_meta;
42 
43+       if ( !isset($wp_screen_meta) )
44+               $wp_screen_meta = array();
45+       if ( !isset( $wp_screen_meta[$screen] ) )
46+               $wp_screen_meta[$screen] = array();
47+
48+       if ( !$label )
49+               $label = $id;
50+
51+       $args = array(
52+               'id' => $id,
53+               'label' => $label,
54+               'capability' => $capability,
55+               'priority' => $priority,
56+       );
57+
58+       $wp_screen_meta[$screen][$priority][$id] = $args;
59+}
60+
61+/**
62+ * Adds a new section to a screen tab.
63+ *
64+ * @since 3.2.0
65+ *
66+ * @param string $id Identifier for the content.
67+ * @param string $callback Callback function used to output the content.
68+ * @param string $tab A string or array of tabs to add the content to.
69+ * @param string $priority Priority to display this content.
70+ * @param string $callback_args Optional. Additional arguments to pass to the callback.
71+ */
72+function add_screen_meta_section( $id, $callback, $tab, $priority = 10, $callback_args = null ) {
73+       global $wp_screen_meta_data;
74+       
75+       if ( !isset($wp_screen_meta_data) )
76+               $wp_screen_meta_data = array();
77+
78+       $args = array(
79+               'id' => $id,
80+               'callback' => $callback,
81+               'tab' => $tab,
82+               'priority' => $priority,
83+               'callback_args' => $callback_args,
84+       );
85+
86+       $wp_screen_meta_data[$tab][$priority][$id] = $args;
87+}
88+
89+/**
90+ * Displays the screen meta tabs and their content based on the current screen.
91+ *
92+ * @since 3.2.0
93+ *
94+ * @param string $screen Identifier of the screen.
95+ */
96+function do_screen_meta( $screen ) {
97+       global $wp_screen_meta, $wp_screen_meta_data;
98+       
99+       add_screen_meta_tab( 'screen-options', __( 'Screen Options' ), 'global', 'read' );
100+       add_screen_meta_tab( 'contextual-help', __( 'Help' ), 'global', 'read', 1 );
101+
102+       add_screen_meta_section( 'contextual-help', 'wp_screen_meta_contextual_help', 'contextual-help' );
103+       add_screen_meta_section( 'screen-settings', 'wp_screen_meta_screen_settings', 'screen-options' );
104+
105+       do_action( 'screen_meta', $screen );
106+       do_action( 'screen_meta_' . $screen );
107+
108+       // Get the registered tabs from the global and $screen scope.
109+       $the_tabs = !empty( $wp_screen_meta['global'] ) ? $wp_screen_meta['global'] : array();
110+
111+       if ( !empty( $wp_screen_meta[$screen] ) ) {
112+               foreach ( $wp_screen_meta[$screen] as $priority => $tabs ) {
113+
114+                       if ( isset($the_tabs[$priority]) )
115+                               $the_tabs[$priority] = array_merge( $the_tabs[$priority], $tabs );
116+                       else
117+                               $the_tabs[$priority] = $tabs;
118+               }
119+       }
120+
121+       // Bail if no tabs are registered
122+       if ( empty( $the_tabs ) )
123+               return;
124+
125+       // Sort tabs by priority
126+       ksort( $the_tabs );
127+
128+       $screen_meta = array();
129+       foreach ( $the_tabs as $priority => $tab ) {
130+               foreach ( $tab as $tab_id => $tab_info ) {
131+
132+                       // Check for permissions
133+                       if ( !current_user_can( $tab_info['capability'] ) )
134+                               continue;
135+
136+                       // Check to see if the tab has any data
137+                       // Uncomment the following lines to remove tabs that don't have any sections registered to it.
138+                       // if ( empty( $wp_screen_meta_data[ $tab_id ] ) )
139+                       //      continue;
140+
141+                       // Store all the tab data in $screen_meta
142+                       $screen_meta[$priority] = $tab;
143+                       
144+                       // Sort the content by priority
145+                       if ( isset( $wp_screen_meta_data[$tab_id] ) )
146+                               ksort( $wp_screen_meta_data[$tab_id] );
147+               }
148+       }
149+       ?>
150+       <div id="screen-meta" class="screen-meta">
151+               <?php
152+               foreach ( $screen_meta as $priority => $tabs ) {
153+                       foreach ( $tabs as $tab_id => $tab ) {
154+                               ?>
155+                               <div id="<?php echo esc_attr( $tab_id ); ?>-wrap" class="hidden screen-meta-section">
156+                                       <?php
157+                                       if ( isset( $wp_screen_meta_data[$tab_id] ) ) {
158+                                               foreach ( $wp_screen_meta_data[$tab_id] as $priority => $_tab_data ) {
159+                                                       foreach ( $_tab_data as $tab_data_id => $tab_data ) {
160+                                                               call_user_func( $tab_data['callback'], $screen, $tab_data['callback_args'] );
161+                                                       }
162+                                               }
163+                                       }
164+                                       ?>
165+                               </div><!-- .screen-meta-section -->
166+                               <?php
167+                       }
168+               }
169+               ?>
170+               <div id="screen-meta-links">
171+                       <?php
172+                       foreach ( $screen_meta as $priority => $tab ) {
173+                               foreach ($tab as $tab_id => $tab_info ) {
174+                                       ?>
175+                                       <div id="<?php echo esc_attr( $tab_id ); ?>-link-wrap" class="hide-if-no-js screen-meta-toggle">
176+                                               <a href="#<?php echo esc_attr( $tab_id ); ?>-wrap" id="<?php echo esc_attr( $tab_id ); ?>-link" class="show-settings"><?php echo esc_html( $tab_info['label'] ); ?></a>
177+                                       </div><!-- .screen-meta-toggle -->
178+                                       <?php
179+                               }
180+                       }
181+                       ?>
182+               </div><!-- #screen-meta-links -->
183+       </div><!-- #screen-meta -->
184+       <?php
185+}
186+
187+/**
188+ * Outputs various screen settings for the current page.
189+ *
190+ * @since 3.2.0
191+ *
192+ * @param string $screen The current page's screen.
193+ */
194+function wp_screen_meta_screen_settings( $screen ) {
195+       global $wp_meta_boxes, $wp_current_screen_options;
196+
197+       $screen = convert_to_screen( $screen );
198        $columns = get_column_headers( $screen );
199        $hidden = get_hidden_columns( $screen );
200 
201-       $meta_screens = array('index' => 'dashboard');
202+       $meta_screens = array( 'index' => 'dashboard' );
203 
204        if ( isset($meta_screens[$screen->id]) ) {
205                $screen->id = $meta_screens[$screen->id];
206@@ -1692,111 +1871,104 @@
207        if ( !empty($wp_meta_boxes[$screen->id]) || !empty($columns) )
208                $show_screen = true;
209 
210-       $screen_options = screen_options($screen);
211+       $screen_options = screen_options( $screen );
212        if ( $screen_options )
213                $show_screen = true;
214 
215-       if ( !isset($_wp_contextual_help) )
216-               $_wp_contextual_help = array();
217+       $settings = apply_filters( 'screen_settings', '', $screen );
218 
219-       $settings = apply_filters('screen_settings', '', $screen);
220-
221        switch ( $screen->id ) {
222                case 'widgets':
223                        $settings = '<p><a id="access-on" href="widgets.php?widgets-access=on">' . __('Enable accessibility mode') . '</a><a id="access-off" href="widgets.php?widgets-access=off">' . __('Disable accessibility mode') . "</a></p>\n";
224                        $show_screen = true;
225                        break;
226        }
227+
228        if ( ! empty( $settings ) )
229                $show_screen = true;
230 
231        if ( !empty($wp_current_screen_options) )
232                $show_screen = true;
233+       ?>
234+       <form id="adv-settings" action="" method="post">
235+               <?php if ( isset($wp_meta_boxes[$screen->id]) ) : ?>
236+                       <h5><?php _ex( 'Show on screen', 'Metaboxes' ); ?></h5>
237+                       <div class="metabox-prefs">
238+                               <?php meta_box_prefs( $screen ); ?>
239+                               <br class="clear" />
240+                       </div>
241+                       <?php endif;
242+                       if ( ! empty($columns) ) : ?>
243+                       <h5><?php echo ( isset( $columns['_title'] ) ?  $columns['_title'] :  _x('Show on screen', 'Columns') ) ?></h5>
244+                       <div class="metabox-prefs">
245+               <?php
246+               $special = array('_title', 'cb', 'comment', 'media', 'name', 'title', 'username', 'blogname');
247 
248-?>
249-<div id="screen-meta">
250-<?php if ( $show_screen ) : ?>
251-<div id="screen-options-wrap" class="hidden">
252-       <form id="adv-settings" action="" method="post">
253-       <?php if ( isset($wp_meta_boxes[$screen->id]) ) : ?>
254-               <h5><?php _ex('Show on screen', 'Metaboxes') ?></h5>
255-               <div class="metabox-prefs">
256-                       <?php meta_box_prefs($screen); ?>
257-                       <br class="clear" />
258-               </div>
259+               foreach ( $columns as $column => $title ) {
260+                       // Can't hide these for they are special
261+                       if ( in_array( $column, $special ) )
262+                               continue;
263+                       if ( empty( $title ) )
264+                               continue;
265+
266+                       if ( 'comments' == $column )
267+                               $title = __( 'Comments' );
268+                       $id = "$column-hide";
269+                       echo '<label for="' . $id . '">';
270+                       echo '<input class="hide-column-tog" name="' . $id . '" type="checkbox" id="' . $id . '" value="' . $column . '"' . checked( !in_array($column, $hidden), true, false ) . ' />';
271+                       echo "$title</label>\n";
272+               }
273+               ?>
274+                               <br class="clear" />
275+                       </div>
276                <?php endif;
277-               if ( ! empty($columns) ) : ?>
278-               <h5><?php echo ( isset( $columns['_title'] ) ?  $columns['_title'] :  _x('Show on screen', 'Columns') ) ?></h5>
279-               <div class="metabox-prefs">
280-<?php
281-       $special = array('_title', 'cb', 'comment', 'media', 'name', 'title', 'username', 'blogname');
282+               echo screen_layout( $screen );
283 
284-       foreach ( $columns as $column => $title ) {
285-               // Can't hide these for they are special
286-               if ( in_array( $column, $special ) )
287-                       continue;
288-               if ( empty( $title ) )
289-                       continue;
290+               if ( !empty( $screen_options ) ) {
291+                       ?>
292+                       <h5><?php _ex('Show on screen', 'Screen Options') ?></h5>
293+                       <?php
294+               }
295 
296-               if ( 'comments' == $column )
297-                       $title = __( 'Comments' );
298-               $id = "$column-hide";
299-               echo '<label for="' . $id . '">';
300-               echo '<input class="hide-column-tog" name="' . $id . '" type="checkbox" id="' . $id . '" value="' . $column . '"' . checked( !in_array($column, $hidden), true, false ) . ' />';
301-               echo "$title</label>\n";
302-       }
303-?>
304-                       <br class="clear" />
305-               </div>
306-       <?php endif;
307-       echo screen_layout($screen);
308+               echo $screen_options;
309+               echo $settings; ?>
310+               <div><?php wp_nonce_field( 'screen-options-nonce', 'screenoptionnonce', false ); ?></div>
311+       </form><!-- #adv-settings -->
312+       <?php
313+}
314 
315-       if ( !empty( $screen_options ) ) {
316-               ?>
317-               <h5><?php _ex('Show on screen', 'Screen Options') ?></h5>
318-               <?php
319-       }
320+/**
321+ * Outputs the contextual help content.
322+ *
323+ * @since 3.2.0
324+ *
325+ * @param string $screen The current page's screen.
326+ */
327+function wp_screen_meta_contextual_help( $screen ) {
328+       global $_wp_contextual_help;
329 
330-       echo $screen_options;
331-       echo $settings; ?>
332-<div><?php wp_nonce_field( 'screen-options-nonce', 'screenoptionnonce', false ); ?></div>
333-</form>
334-</div>
335+       if ( !isset($_wp_contextual_help) )
336+               $_wp_contextual_help = array();
337 
338-<?php endif; // $show_screen
339+       $_wp_contextual_help = apply_filters( 'contextual_help_list', $_wp_contextual_help, $screen );
340+       
341+       $contextual_help = '';
342 
343-       $_wp_contextual_help = apply_filters('contextual_help_list', $_wp_contextual_help, $screen);
344-       ?>
345-       <div id="contextual-help-wrap" class="hidden">
346-       <?php
347-       $contextual_help = '';
348-       if ( isset($_wp_contextual_help[$screen->id]) ) {
349-               $contextual_help .= '<div class="metabox-prefs">' . $_wp_contextual_help[$screen->id] . "</div>\n";
350+       if ( isset($_wp_contextual_help[$screen]) ) {
351+               $contextual_help .= '<div class="metabox-prefs">' . $_wp_contextual_help[$screen] . "</div>\n";
352        } else {
353                $contextual_help .= '<div class="metabox-prefs">';
354-               $default_help = __('<a href="http://codex.wordpress.org/" target="_blank">Documentation</a>');
355+               $default_help = __( '<a href="http://codex.wordpress.org/" target="_blank">Documentation</a>' );
356                $default_help .= '<br />';
357-               $default_help .= __('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>');
358-               $contextual_help .= apply_filters('default_contextual_help', $default_help);
359+               $default_help .= __( '<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>' );
360+               $contextual_help .= apply_filters( 'default_contextual_help', $default_help );
361                $contextual_help .= "</div>\n";
362        }
363+       
364+       // Backwards compat.
365+       $screen_object = convert_to_screen( $screen );
366 
367-       echo apply_filters('contextual_help', $contextual_help, $screen->id, $screen);
368-       ?>
369-       </div>
370-
371-<div id="screen-meta-links">
372-<div id="contextual-help-link-wrap" class="hide-if-no-js screen-meta-toggle">
373-<a href="#contextual-help" id="contextual-help-link" class="show-settings"><?php _e('Help') ?></a>
374-</div>
375-<?php if ( $show_screen ) { ?>
376-<div id="screen-options-link-wrap" class="hide-if-no-js screen-meta-toggle">
377-<a href="#screen-options" id="show-settings-link" class="show-settings"><?php _e('Screen Options') ?></a>
378-</div>
379-<?php } ?>
380-</div>
381-</div>
382-<?php
383+       echo apply_filters( 'contextual_help', $contextual_help, $screen, $screen_object );
384 }
385 
386 /**
387Index: wp-admin/js/common.dev.js
388===================================================================
389--- wp-admin/js/common.dev.js   (revision 17535)
390+++ wp-admin/js/common.dev.js   (working copy)
391@@ -207,37 +207,26 @@
392        $('div.wrap h2:first').nextAll('div.updated, div.error').addClass('below-h2');
393        $('div.updated, div.error').not('.below-h2, .inline').insertAfter( $('div.wrap h2:first') );
394 
395-       // screen settings tab
396-       $('#show-settings-link').click(function () {
397-               if ( ! $('#screen-options-wrap').hasClass('screen-options-open') )
398-                       $('#contextual-help-link-wrap').css('visibility', 'hidden');
399+       // screen meta tabs
400+       $('.screen-meta a.show-settings').click(function(e) {   
401+               var id = $(e.target).attr('href');
402+               id = id.substring( 0, id.length - 5 ); // remove the -wrap
403 
404-               $('#screen-options-wrap').slideToggle('fast', function(){
405-                       if ( $(this).hasClass('screen-options-open') ) {
406-                               $('#show-settings-link').css({'backgroundPosition':'top '+bgx});
407-                               $('#contextual-help-link-wrap').css('visibility', '');
408-                               $(this).removeClass('screen-options-open');
409-                       } else {
410-                               $('#show-settings-link').css({'backgroundPosition':'bottom '+bgx});
411-                               $(this).addClass('screen-options-open');
412-                       }
413-               });
414-               return false;
415-       });
416+               // Maybe hide all other tabs if we're closing.
417+               if ( ! $(id + '-wrap' ).hasClass('screen-tab-open') )
418+                       $('.screen-meta-toggle').not( id + '-link-wrap').css( 'visibility', 'hidden' );
419 
420-       // help tab
421-       $('#contextual-help-link').click(function () {
422-               if ( ! $('#contextual-help-wrap').hasClass('contextual-help-open') )
423-                       $('#screen-options-link-wrap').css('visibility', 'hidden');
424-
425-               $('#contextual-help-wrap').slideToggle('fast', function() {
426-                       if ( $(this).hasClass('contextual-help-open') ) {
427-                               $('#contextual-help-link').css({'backgroundPosition':'top '+bgx});
428-                               $('#screen-options-link-wrap').css('visibility', '');
429-                               $(this).removeClass('contextual-help-open');
430+               // Open the clicked tab.
431+               $(id + '-wrap').slideToggle('fast', function(e) {
432+                       // If a tab is open, close it and make the other tabs visible.
433+                       if ( $(this).hasClass( 'screen-tab-open' ) ) {
434+                               $(id + '-link').css( { 'backgroundPosition' : 'top ' + bgx } );
435+                               $('.screen-meta-toggle').not( id +'-link-wrap' ).css( 'visibility', '' );
436+                               $(this).removeClass( 'screen-tab-open' );
437+                       // Open a tab.
438                        } else {
439-                               $('#contextual-help-link').css({'backgroundPosition':'bottom '+bgx});
440-                               $(this).addClass('contextual-help-open');
441+                               $(id + '-link').css( { 'backgroundPosition' : 'bottom ' + bgx } );
442+                               $(this).addClass( 'screen-tab-open' );
443                        }
444                });
445                return false;
446Index: wp-admin/css/colors-classic.dev.css
447===================================================================
448--- wp-admin/css/colors-classic.dev.css (revision 17535)
449+++ wp-admin/css/colors-classic.dev.css (working copy)
450@@ -1279,6 +1279,7 @@
451        color: #D54E21;
452 }
453 
454+.screen-meta-section,
455 #screen-options-wrap,
456 #contextual-help-wrap {
457        background-color: #F8F7F3;
458Index: wp-admin/css/colors-fresh.dev.css
459===================================================================
460--- wp-admin/css/colors-fresh.dev.css   (revision 17535)
461+++ wp-admin/css/colors-fresh.dev.css   (working copy)
462@@ -1276,6 +1276,7 @@
463        color: #D54E21;
464 }
465 
466+.screen-meta-section,
467 #screen-options-wrap,
468 #contextual-help-wrap {
469        background-color: #f1f1f1;
470Index: wp-admin/css/wp-admin.dev.css
471===================================================================
472--- wp-admin/css/wp-admin.dev.css       (revision 17535)
473+++ wp-admin/css/wp-admin.dev.css       (working copy)
474@@ -729,6 +729,7 @@
475        visibility: hidden;
476 }
477 
478+.screen-meta-toggle,
479 #screen-options-link-wrap,
480 #contextual-help-link-wrap {
481        float: right;
482@@ -776,12 +777,14 @@
483        text-decoration: none;
484 }
485 
486+.screen-meta-section h5,
487 #screen-options-wrap h5,
488 #contextual-help-wrap h5 {
489        margin: 8px 0;
490        font-size: 13px;
491 }
492 
493+.screen-meta-section,
494 #screen-options-wrap,
495 #contextual-help-wrap {
496        border-style: none solid solid;