WordPress.org

Make WordPress Core

Changeset 13084


Ignore:
Timestamp:
02/13/2010 04:39:39 AM (9 years ago)
Author:
nacin
Message:

Make Recent Comments dashboard widget configurable. Allows user to choose how many comments to display, see #11891.

File:
1 edited

Legend:

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

    r13057 r13084  
    2929
    3030    // Recent Comments Widget
     31    if ( !isset( $widget_options['dashboard_recent_comments'] ) || !isset( $widget_options['dashboard_recent_comments']['items'] ) ) {
     32        $update = true;
     33        $widget_options['dashboard_recent_comments'] = array(
     34            'items' => 5,
     35        );
     36    }
    3137    $recent_comments_title = __( 'Recent Comments' );
    32     wp_add_dashboard_widget( 'dashboard_recent_comments', $recent_comments_title, 'wp_dashboard_recent_comments' );
     38    wp_add_dashboard_widget( 'dashboard_recent_comments', $recent_comments_title, 'wp_dashboard_recent_comments', 'wp_dashboard_recent_comments_control' );
    3339
    3440    // Incoming Links Widget
     
    483489    $start = 0;
    484490
     491    $widgets = get_option( 'dashboard_widget_options' );
     492    if ( isset( $widgets['dashboard_recent_comments'] ) && isset( $widgets['dashboard_recent_comments']['items'] ) )
     493        $total_items = (int) $widgets['dashboard_recent_comments']['items'];
     494    else
     495        $total_items = 5;
     496
    485497    while ( count( $comments ) < 5 && $possible = $wpdb->get_results( "SELECT * FROM $wpdb->comments c LEFT JOIN $wpdb->posts p ON c.comment_post_ID = p.ID WHERE p.post_status != 'trash' ORDER BY c.comment_date_gmt DESC LIMIT $start, 50" ) ) {
    486498
    487499        foreach ( $possible as $comment ) {
    488             if ( count( $comments ) >= 5 )
     500            if ( count( $comments ) >= $total_items )
    489501                break;
    490502            if ( in_array( $comment->comment_approved, $allowed_states ) )
     
    616628}
    617629
     630/**
     631 * The recent comments dashboard widget control.
     632 *
     633 * @since 3.0.0
     634 */
     635function wp_dashboard_recent_comments_control() {
     636    if ( !$widget_options = get_option( 'dashboard_widget_options' ) )
     637        $widget_options = array();
     638
     639    if ( !isset($widget_options['dashboard_recent_comments']) )
     640        $widget_options['dashboard_recent_comments'] = array();
     641
     642    if ( 'POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['widget-recent-comments']) ) {
     643        $number = (int) stripslashes($_POST['widget-recent-comments']['items']);
     644        if ( $number < 1 || $number > 15 )
     645            $number = 5;
     646        $widget_options['dashboard_recent_comments']['items'] = $number;
     647        update_option( 'dashboard_widget_options', $widget_options );
     648    }
     649
     650    $number = isset( $widget_options['dashboard_recent_comments']['items'] ) ? (int) $widget_options['dashboard_recent_comments']['items'] : '';
     651
     652    echo '<p><label for="comments-number">' . __('Number of comments to show:') . '</label>';
     653    echo '<input id="comments-number" name="widget-recent-comments[items]" type="text" value="' . $number . '" size="3" /> <small>' . __( '(at most 15)' ) . '</p>';
     654}
     655
    618656function wp_dashboard_incoming_links() {
    619657    echo '<p class="widget-loading hide-if-no-js">' . __( 'Loading&#8230;' ) . '</p><p class="describe hide-if-js">' . __('This widget requires JavaScript.') . '</p>';
Note: See TracChangeset for help on using the changeset viewer.