Make WordPress Core


Ignore:
Timestamp:
11/15/2013 08:14:44 PM (11 years ago)
Author:
nacin
Message:

Dash cleanup.

  • Use wp_add_dashboard_widget() rather than add_meta_box().
  • Use original functions like wp_dashboard_primary(), wp_dashboard_right_now(), wp_dashboard_quick_press() (where possible).
  • Only include plugins section of the news widget when appropriate, which necessitates the set_current_screen() via the ajax action.
  • Remove some debug cruft that was preventing caching and invalidation.
  • Simplify lots of things where possible.

see #25824.

File:
1 edited

Legend:

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

    r26144 r26220  
    3838    // Right Now
    3939    if ( is_blog_admin() && current_user_can('edit_posts') )
    40         add_meta_box( 'dash-right-now', 'Site Content', 'dashboard_new_right_now', 'dashboard', 'normal', 'high' );
     40        wp_add_dashboard_widget( 'dash-right-now', __( 'Site Content' ), 'wp_dashboard_right_now' );
    4141
    4242    if ( is_network_admin() )
     
    4444
    4545    // Activity Widget
    46     add_meta_box( 'dashboard_activity', __( 'Activity' ), 'wp_dashboard_activity', 'dashboard', 'normal', 'high' );
     46    wp_add_dashboard_widget( 'dashboard_activity', __( 'Activity' ), 'wp_dashboard_activity' );
    4747
    4848    // QuickPress Widget
    4949    if ( is_blog_admin() && current_user_can( 'edit_posts' ) )
    50         add_meta_box( 'dashboard_quick_draft', __( 'Quick Draft' ), 'wp_dashboard_quick_draft', 'dashboard', 'side', 'high' );
     50        wp_add_dashboard_widget( 'dashboard_quick_press', __( 'Quick Draft' ), 'wp_dashboard_quick_press' );
    5151
    5252    // WordPress News
    53     add_meta_box( 'dashboard_rss', __( 'WordPress News' ), 'wp_dashboard_rss', 'dashboard', 'side', 'low' );
     53    wp_add_dashboard_widget( 'dashboard_primary', __( 'WordPress News' ), 'wp_dashboard_primary' );
    5454
    5555    // Hook to register new widgets
     
    105105    }
    106106
    107     if ( is_blog_admin () )
    108         $side_widgets = array('dashboard_quick_draft');
    109     else if (is_network_admin() )
    110         $side_widgets = array();
    111     else
    112         $side_widgets = array();
     107    $side_widgets = array( 'dashboard_quick_press', 'dashboard_primary' );
    113108
    114109    $location = 'normal';
     
    119114    if ( 'dashboard_browser_nag' === $widget_id )
    120115        $priority = 'high';
     116    elseif ( 'dashboard_primary' === $widget_id )
     117        $priority = 'low';
    121118
    122119    add_meta_box( $widget_id, $widget_name, $callback, $screen, $location, $priority, $callback_args );
     
    165162/* Dashboard Widgets */
    166163
    167 function dashboard_new_right_now() {
     164/**
     165 * Dashboard widget that displays some basic stats about the site.
     166 *
     167 * Formerly 'Right Now'. A streamlined 'Site Content' as of 3.8.
     168 *
     169 * @since 2.7.0
     170 */
     171function wp_dashboard_right_now() {
    168172    $theme = wp_get_theme();
    169173    if ( current_user_can( 'switch_themes' ) )
     
    284288 *
    285289 */
    286 function wp_dashboard_quick_draft( $error_msg=false ) {
     290function wp_dashboard_quick_press( $error_msg=false ) {
    287291    global $post_ID;
    288292
     
    306310
    307311    $post_ID = (int) $post->ID;
    308 
    309     do_action( 'dashboard_quickdraft_beginning', $post );
    310312?>
    311313
     
    339341<?php
    340342    wp_dashboard_recent_quickdrafts();
    341 
    342     do_action( 'dashboard_quickdraft_end' );
    343343}
    344344
     
    678678
    679679/**
     680 * Display generic dashboard RSS widget feed.
     681 *
     682 * @since 2.5.0
     683 *
     684 * @param string $widget_id
     685 */
     686function wp_dashboard_rss_output( $widget_id ) {
     687    $widgets = get_option( 'dashboard_widget_options' );
     688    echo '<div class="rss-widget">';
     689    wp_widget_rss_output( $widgets[ $widget_id ] );
     690    echo "</div>";
     691}
     692
     693/**
    680694 * Checks to see if all of the feed url in $check_urls are cached.
    681695 *
     
    746760
    747761/**
    748  * Returns default WordPress News feeds
    749  *
    750  *
    751  *
    752  * @since 3.8.0
    753  *
    754  */
    755 function wp_dashboard_default_feeds() {
    756     return array(
     762 * WordPress News dashboard widget.
     763 *
     764 * @since 2.7.0
     765 */
     766function wp_dashboard_primary() {
     767    $feeds = array(
    757768        'news'   => array(
    758769            'link'         => apply_filters( 'dashboard_primary_link', __( 'http://wordpress.org/news/' ) ),
     
    772783            'show_author'  => 0,
    773784            'show_date'    => 0,
    774         ),
    775         'plugins' => array(
     785        )
     786    );
     787
     788    if ( ( ! is_multisite() && is_blog_admin() && current_user_can( 'install_plugins' ) ) || ( is_network_admin() && current_user_can( 'manage_network_plugins' ) && current_user_can( 'install_plugins' ) ) ) {
     789        $feeds['plugins'] = array(
    776790            'link'         => '',
    777791            'url'          => array(
    778                     'popular' => 'http://wordpress.org/plugins/rss/browse/popular/'
     792                'popular' => 'http://wordpress.org/plugins/rss/browse/popular/',
    779793            ),
    780794            'title'        => '',
     
    783797            'show_author'  => 0,
    784798            'show_date'    => 0,
    785         )
    786     );
    787 }
    788 
    789 /**
    790  * Check for cached feeds
    791  *
    792  *
     799        );
     800    }
     801
     802    wp_dashboard_cached_rss_widget( 'dashboard_primary', 'wp_dashboard_primary_output', $feeds );
     803}
     804
     805/**
     806 * Display the WordPress news feeds.
    793807 *
    794808 * @since 3.8.0
    795  *
    796  */
    797 function wp_dashboard_rss() {
    798     $default_feeds = wp_dashboard_default_feeds();
    799 
    800     $widget_options = get_option( 'dashboard_widget_options' );
    801 
    802     if ( !$widget_options || !is_array($widget_options) )
    803         $widget_options = array();
    804 
    805     //if ( ! isset( $widget_options['dashboard_rss'] ) ) {
    806         $widget_options['dashboard_rss'] = $default_feeds;
    807         update_option( 'dashboard_widget_options', $widget_options );
    808     //}
    809 
    810     foreach( $default_feeds as $key => $value ) {
    811         $default_urls[] = $value['url'];
    812     }
    813 
    814     $cache_key = 'dash_' . md5( 'dashboard_rss' );
    815     delete_transient( $cache_key );
    816 
    817     do_action( 'dashboard_news_beginning' );
    818 
    819     wp_dashboard_cached_rss_widget( 'dashboard_rss', 'wp_dashboard_news_output', $default_urls );
    820 
    821     do_action( 'dashboard_news_end' );
    822 }
    823 
    824 /**
    825  * Display news feeds
    826  *
    827  *
    828  *
    829  * @since 3.8.0
    830  *
    831  */
    832 function wp_dashboard_news_output() {
    833     $widgets = get_option( 'dashboard_widget_options' );
    834 
    835     foreach( $widgets['dashboard_rss'] as $type => $args ) {
     809 */
     810function wp_dashboard_primary_output( $widget_id, $feeds ) {
     811    foreach( $feeds as $type => $args ) {
    836812        $args['type'] = $type;
    837813        echo '<div class="rss-widget">';
    838         wp_widget_news_output( $args['url'], $args );
     814        if ( $type === 'plugins' ) {
     815            wp_dashboard_plugins_output( $args['url'], $args );
     816        } else {
     817            wp_widget_rss_output( $args['url'], $args );
     818        }
    839819        echo "</div>";
    840820    }
     
    842822
    843823/**
    844  * Generate code for each news feed
    845  *
    846  *
    847  *
    848  * @since 3.8.0
    849  *
    850  */
    851 function wp_widget_news_output( $rss, $args = array() ) {
    852 
    853     // Regular RSS feeds
    854     if ( isset( $args['type'] ) && 'plugins' != $args['type'] )
    855         return wp_widget_rss_output( $rss, $args );
    856 
     824 * Display plugins text for the WordPress news widget.
     825 *
     826 * @since 2.5.0
     827 */
     828function wp_dashboard_plugins_output( $rss, $args = array() ) {
    857829    // Plugin feeds plus link to install them
    858     if ( ! is_multisite() && current_user_can( 'install_plugins' ) ) {
    859         $popular = fetch_feed( $args['url']['popular'] );
    860 
    861         if ( false === $plugin_slugs = get_transient( 'plugin_slugs' ) ) {
    862             $plugin_slugs = array_keys( get_plugins() );
    863             set_transient( 'plugin_slugs', $plugin_slugs, DAY_IN_SECONDS );
    864         }
    865 
    866         echo '<ul>';
    867 
    868         foreach ( array(
    869             'popular' => __( 'Popular Plugin' )
    870         ) as $feed => $label ) {
    871             if ( is_wp_error($$feed) || !$$feed->get_item_quantity() )
     830    $popular = fetch_feed( $args['url']['popular'] );
     831
     832    if ( false === $plugin_slugs = get_transient( 'plugin_slugs' ) ) {
     833        $plugin_slugs = array_keys( get_plugins() );
     834        set_transient( 'plugin_slugs', $plugin_slugs, DAY_IN_SECONDS );
     835    }
     836
     837    echo '<ul>';
     838
     839    foreach ( array(
     840        'popular' => __( 'Popular Plugin' )
     841    ) as $feed => $label ) {
     842        if ( is_wp_error($$feed) || !$$feed->get_item_quantity() )
     843            continue;
     844
     845        $items = $$feed->get_items(0, 5);
     846
     847        // Pick a random, non-installed plugin
     848        while ( true ) {
     849            // Abort this foreach loop iteration if there's no plugins left of this type
     850            if ( 0 == count($items) )
     851                continue 2;
     852
     853            $item_key = array_rand($items);
     854            $item = $items[$item_key];
     855
     856            list($link, $frag) = explode( '#', $item->get_link() );
     857
     858            $link = esc_url($link);
     859            if ( preg_match( '|/([^/]+?)/?$|', $link, $matches ) )
     860                $slug = $matches[1];
     861            else {
     862                unset( $items[$item_key] );
    872863                continue;
    873 
    874             $items = $$feed->get_items(0, 5);
    875 
    876             // Pick a random, non-installed plugin
    877             while ( true ) {
    878                 // Abort this foreach loop iteration if there's no plugins left of this type
    879                 if ( 0 == count($items) )
     864            }
     865
     866            // Is this random plugin's slug already installed? If so, try again.
     867            reset( $plugin_slugs );
     868            foreach ( $plugin_slugs as $plugin_slug ) {
     869                if ( $slug == substr( $plugin_slug, 0, strlen( $slug ) ) ) {
     870                    unset( $items[$item_key] );
    880871                    continue 2;
    881 
    882                 $item_key = array_rand($items);
    883                 $item = $items[$item_key];
    884 
    885                 list($link, $frag) = explode( '#', $item->get_link() );
    886 
    887                 $link = esc_url($link);
    888                 if ( preg_match( '|/([^/]+?)/?$|', $link, $matches ) )
    889                     $slug = $matches[1];
    890                 else {
    891                     unset( $items[$item_key] );
    892                     continue;
    893872                }
    894 
    895                 // Is this random plugin's slug already installed? If so, try again.
    896                 reset( $plugin_slugs );
    897                 foreach ( $plugin_slugs as $plugin_slug ) {
    898                     if ( $slug == substr( $plugin_slug, 0, strlen( $slug ) ) ) {
    899                         unset( $items[$item_key] );
    900                         continue 2;
    901                     }
    902                 }
    903 
    904                 // If we get to this point, then the random plugin isn't installed and we can stop the while().
    905                 break;
    906873            }
    907874
    908             // Eliminate some common badly formed plugin descriptions
    909             while ( ( null !== $item_key = array_rand($items) ) && false !== strpos( $items[$item_key]->get_description(), 'Plugin Name:' ) )
    910                 unset($items[$item_key]);
    911 
    912             if ( !isset($items[$item_key]) )
    913                 continue;
    914 
    915             $title = esc_html( $item->get_title() );
    916 
    917             $description = esc_html( strip_tags( @html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) ) ) );
    918 
    919             $ilink = wp_nonce_url('plugin-install.php?tab=plugin-information&plugin=' . $slug, 'install-plugin_' . $slug) . '&amp;TB_iframe=true&amp;width=600&amp;height=800';
    920 
    921             echo "<li class='dashboard-news-plugin'><span>$label:</span> <a href='$link' class='dashboard-news-plugin-link'>$title</a></h5>&nbsp;<span>(<a href='$ilink' class='thickbox' title='$title'>" . __( 'Install' ) . "</a>)</span></li>";
    922 
    923             $$feed->__destruct();
    924             unset( $$feed );
    925         }
    926 
    927         echo '</ul>';
    928     }
     875            // If we get to this point, then the random plugin isn't installed and we can stop the while().
     876            break;
     877        }
     878
     879        // Eliminate some common badly formed plugin descriptions
     880        while ( ( null !== $item_key = array_rand($items) ) && false !== strpos( $items[$item_key]->get_description(), 'Plugin Name:' ) )
     881            unset($items[$item_key]);
     882
     883        if ( !isset($items[$item_key]) )
     884            continue;
     885
     886        $title = esc_html( $item->get_title() );
     887
     888        $description = esc_html( strip_tags( @html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) ) ) );
     889
     890        $ilink = wp_nonce_url('plugin-install.php?tab=plugin-information&plugin=' . $slug, 'install-plugin_' . $slug) . '&amp;TB_iframe=true&amp;width=600&amp;height=800';
     891
     892        echo "<li class='dashboard-news-plugin'><span>$label:</span> <a href='$link' class='dashboard-news-plugin-link'>$title</a></h5>&nbsp;<span>(<a href='$ilink' class='thickbox' title='$title'>" . __( 'Install' ) . "</a>)</span></li>";
     893
     894        $$feed->__destruct();
     895        unset( $$feed );
     896    }
     897
     898    echo '</ul>';
    929899}
    930900
Note: See TracChangeset for help on using the changeset viewer.