Make WordPress Core


Ignore:
Timestamp:
09/26/2011 09:03:38 PM (14 years ago)
Author:
koopersmith
Message:

Introduce help tabs and WP_Screen. props mbijon, goldenapples, natebedortha, ryan. see #18690.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/template.php

    r18736 r18779  
    18141814    <?php
    18151815    $contextual_help = '';
    1816     if ( isset($_wp_contextual_help[$screen->id]) ) {
     1816    if ( isset($_wp_contextual_help[$screen->id]) && is_array($_wp_contextual_help[$screen->id]) ) {
     1817        $contextual_help .= '<div class="metabox-prefs">' . "\n";
     1818
     1819        /*
     1820         * Loop through ['contextual-help-tabs']
     1821         *   - It's a nested array where $key=>$value >> $title=>$content
     1822         * Has no output so can only loop the array once
     1823         */
     1824        $contextual_help_tabs = ''; // store looped content for later
     1825        $contextual_help_panels = ''; // store looped content for later
     1826
     1827        $tab_active = true;
     1828
     1829        foreach ( $_wp_contextual_help[$screen->id]['tabs'] as $tab ) {
     1830            $tab_slug = sanitize_html_class( $tab[ 0 ] );
     1831            $contextual_help_tabs .= '<li class="tab-' . $tab_slug . ( ($tab_active) ? ' active' : '' ) . '">';
     1832            $contextual_help_tabs .= '<a href="#' . $tab_slug . '">' . $tab[1] . '</a>';
     1833            $contextual_help_tabs .= '</li>' ."\n";
     1834           
     1835            $contextual_help_panels .= '<div id="' . $tab_slug . '" class="help-tab-content' . ( ($tab_active) ? ' active' : '' ) . '">';
     1836            $contextual_help_panels .= $tab[2];
     1837            $contextual_help_panels .= "</div>\n";
     1838
     1839            $tab_active = false;
     1840        }
     1841
     1842        // Start output from loop: Tabbed help content
     1843        $contextual_help .= '<ul class="contextual-help-tabs">' . "\n";
     1844        $contextual_help .= $contextual_help_tabs;
     1845        $contextual_help .= '</ul>' ."\n";
     1846        $contextual_help .= '<div class="contextual-help-tabs-wrap">' . "\n";
     1847        $contextual_help .= $contextual_help_panels;
     1848        $contextual_help .= "</div>\n";
     1849        // END: Tabbed help content
     1850
     1851        // Sidebar to right of tabs
     1852        $contextual_help .= '<div class="contextual-help-links">' . "\n";
     1853        $contextual_help .= $_wp_contextual_help[$screen->id]['sidebar'];
     1854        $contextual_help .= "</div>\n";
     1855       
     1856        $contextual_help .= "</div>\n"; // end metabox
     1857       
     1858    } elseif ( isset($_wp_contextual_help[$screen->id]) ) {
    18171859        $contextual_help .= '<div class="metabox-prefs">' . $_wp_contextual_help[$screen->id] . "</div>\n";
    18181860    } else {
    18191861        $contextual_help .= '<div class="metabox-prefs">';
    1820         $default_help = __('<a href="http://codex.wordpress.org/" target="_blank">Documentation</a>');
     1862        $default_help  = __('<a href="http://codex.wordpress.org/" target="_blank">Documentation</a>');
    18211863        $default_help .= '<br />';
    18221864        $default_help .= __('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>');
    18231865        $contextual_help .= apply_filters('default_contextual_help', $default_help);
    1824         $contextual_help .= "</div>\n";
     1866        $contextual_help .= '</div>' . "\n";
    18251867    }
    18261868
     
    18351877 * Add contextual help text for a page
    18361878 *
     1879 * The array $help takes the following format:
     1880 *  array( 'contextual-help-tabs'   => array( $tab1_title => $tab1_value [, $tab2_title => $tab2_value, ...] ),
     1881 *      'contextual-help-links' => $help_links_as_string )
     1882 *
     1883 * For backwards compatability, a string is also accepted.
     1884 *
    18371885 * @since 2.7.0
    18381886 *
    1839  * @param string $screen The handle for the screen to add help to.  This is usually the hook name returned by the add_*_page() functions.
    1840  * @param string $help Arbitrary help text
     1887 * @param string    $screen The handle for the screen to add help to.  This is usually the hook name returned by the add_*_page() functions.
     1888 * @param array|string  $help   Creates tabs & links columns within help text in array.
     1889 *
    18411890 */
    18421891function add_contextual_help($screen, $help) {
     
    20782127 */
    20792128function set_current_screen( $id =  '' ) {
    2080     global $current_screen, $hook_suffix, $typenow, $taxnow;
    2081 
    2082     $action = '';
    2083 
    2084     if ( empty($id) ) {
    2085         $current_screen = $hook_suffix;
    2086         $current_screen = str_replace('.php', '', $current_screen);
    2087         if ( preg_match('/-add|-new$/', $current_screen) )
    2088             $action = 'add';
    2089         $current_screen = str_replace('-new', '', $current_screen);
    2090         $current_screen = str_replace('-add', '', $current_screen);
    2091         $current_screen = array('id' => $current_screen, 'base' => $current_screen);
    2092     } else {
    2093         $id = sanitize_key($id);
    2094         if ( false !== strpos($id, '-') ) {
    2095             list( $id, $typenow ) = explode('-', $id, 2);
    2096             if ( taxonomy_exists( $typenow ) ) {
    2097                 $id = 'edit-tags';
    2098                 $taxnow = $typenow;
    2099                 $typenow = '';
    2100             }
    2101         }
    2102         $current_screen = array('id' => $id, 'base' => $id);
    2103     }
    2104 
    2105     $current_screen = (object) $current_screen;
    2106 
    2107     $current_screen->action = $action;
    2108 
    2109     // Map index to dashboard
    2110     if ( 'index' == $current_screen->base )
    2111         $current_screen->base = 'dashboard';
    2112     if ( 'index' == $current_screen->id )
    2113         $current_screen->id = 'dashboard';
    2114 
    2115     if ( 'edit' == $current_screen->id ) {
    2116         if ( empty($typenow) )
    2117             $typenow = 'post';
    2118         $current_screen->id .= '-' . $typenow;
    2119         $current_screen->post_type = $typenow;
    2120     } elseif ( 'post' == $current_screen->id ) {
    2121         if ( empty($typenow) )
    2122             $typenow = 'post';
    2123         $current_screen->id = $typenow;
    2124         $current_screen->post_type = $typenow;
    2125     } elseif ( 'edit-tags' == $current_screen->id ) {
    2126         if ( empty($taxnow) )
    2127             $taxnow = 'post_tag';
    2128         $current_screen->id = 'edit-' . $taxnow;
    2129         $current_screen->taxonomy = $taxnow;
    2130     }
    2131 
    2132     $current_screen->is_network = is_network_admin();
    2133     $current_screen->is_user = is_user_admin();
    2134 
    2135     if ( $current_screen->is_network ) {
    2136         $current_screen->base .= '-network';
    2137         $current_screen->id .= '-network';
    2138     } elseif ( $current_screen->is_user ) {
    2139         $current_screen->base .= '-user';
    2140         $current_screen->id .= '-user';
    2141     }
     2129    global $current_screen;
     2130
     2131    $current_screen = new WP_Screen( $id );
    21422132
    21432133    $current_screen = apply_filters('current_screen', $current_screen);
     
    22722262<?php
    22732263}
     2264
     2265class WP_Screen {
     2266    var $action = '';
     2267    var $base;
     2268    var $id;
     2269    var $is_network;
     2270    var $is_user;
     2271    var $parent_base;
     2272    var $parent_file;
     2273    var $post_type;
     2274    var $taxonomy;
     2275
     2276    function __construct( $id = '' ) {
     2277        global $hook_suffix, $typenow, $taxnow;
     2278
     2279        $action = '';
     2280
     2281        if ( empty( $id ) ) {
     2282            $screen = $hook_suffix;
     2283            $screen = str_replace('.php', '', $screen);
     2284            if ( preg_match('/-add|-new$/', $screen) )
     2285                $action = 'add';
     2286            $screen = str_replace('-new', '', $screen);
     2287            $screen = str_replace('-add', '', $screen);
     2288            $this->id = $this->base = $screen;
     2289        } else {
     2290            $id = sanitize_key( $id );
     2291            if ( false !== strpos($id, '-') ) {
     2292                list( $id, $typenow ) = explode('-', $id, 2);
     2293                if ( taxonomy_exists( $typenow ) ) {
     2294                    $id = 'edit-tags';
     2295                    $taxnow = $typenow;
     2296                    $typenow = '';
     2297                }
     2298            }
     2299            $this->id = $this->base = $id;
     2300        }
     2301
     2302        $this->action = $action;
     2303
     2304        // Map index to dashboard
     2305        if ( 'index' == $this->base )
     2306            $this->base = 'dashboard';
     2307        if ( 'index' == $this->id )
     2308            $this->id = 'dashboard';
     2309
     2310        if ( 'edit' == $this->id ) {
     2311            if ( empty($typenow) )
     2312                $typenow = 'post';
     2313            $this->id .= '-' . $typenow;
     2314            $this->post_type = $typenow;
     2315        } elseif ( 'post' == $this->id ) {
     2316            if ( empty($typenow) )
     2317                $typenow = 'post';
     2318            $this->id = $typenow;
     2319            $this->post_type = $typenow;
     2320        } elseif ( 'edit-tags' == $this->id ) {
     2321            if ( empty($taxnow) )
     2322                $taxnow = 'post_tag';
     2323            $this->id = 'edit-' . $taxnow;
     2324            $this->taxonomy = $taxnow;
     2325        }
     2326
     2327        $this->is_network = is_network_admin();
     2328        $this->is_user = is_user_admin();
     2329
     2330        if ( $this->is_network ) {
     2331            $this->base .= '-network';
     2332            $this->id .= '-network';
     2333        } elseif ( $this->is_user ) {
     2334            $this->base .= '-user';
     2335            $this->id .= '-user';
     2336        }
     2337    }
     2338
     2339    function set_parentage( $parent_file ) {
     2340        $current_screen->parent_file = $parent_file;
     2341        $current_screen->parent_base = preg_replace('/\?.*$/', '', $parent_file);
     2342        $current_screen->parent_base = str_replace('.php', '', $current_screen->parent_base);
     2343    }
     2344
     2345    function add_option( $option, $args = array() ) {
     2346        return add_screen_option( $option, $args );
     2347    }
     2348
     2349    function add_help_tab( $id, $title, $content) {
     2350        global $_wp_contextual_help;
     2351
     2352        $_wp_contextual_help[$this->id]['tabs'][] = array( $id, $title, $content );
     2353    }
     2354
     2355    function add_help_sidebar( $content ) {
     2356        global $_wp_contextual_help;
     2357
     2358        $_wp_contextual_help[$this->id]['sidebar'] = $content;
     2359    }
     2360}
Note: See TracChangeset for help on using the changeset viewer.