WordPress.org

Make WordPress Core

Changeset 11090


Ignore:
Timestamp:
04/26/2009 08:09:08 PM (9 years ago)
Author:
azaozz
Message:

Move recent comments widget to WP_Widget, extend is_active_widget() to use $id_base, see #8441

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/default-widgets.php

    r11088 r11090  
    505505
    506506    function widget($args, $instance) {
    507         if ( $output = wp_cache_get('widget_recent_posts' . $args['widget_id'], 'widget') )
    508             return print($output);
     507        $cache = wp_cache_get('widget_recent_posts', 'widget');
     508       
     509        if ( !is_array($cache) )
     510            $cache = array();
     511       
     512        if ( isset($cache[$args['widget_id']]) )
     513            return $cache[$args['widget_id']];
    509514
    510515        ob_start();
     
    534539        endif;
    535540
    536         wp_cache_add('widget_recent_posts' . $args['widget_id'], ob_get_flush(), 'widget');
     541        $cache[$args['widget_id']] = ob_get_flush();
     542        wp_cache_add('widget_recent_posts', $cache, 'widget');
    537543    }
    538544
     
    551557
    552558    function flush_widget_cache() {
    553         wp_cache_delete('widget_recent_entries', 'widget');
     559        wp_cache_delete('widget_recent_posts', 'widget');
    554560    }
    555561
     
    559565            $number = 5;
    560566?>
    561         <p>
    562             <label for="<?php echo $this->get_field_id('title'); ?>">
    563                 <?php _e('Title:'); ?>
    564                 <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
    565             </label>
    566         </p>
    567         <p>
    568             <label for="<?php echo $this->get_field_id('number'); ?>">
    569                 <?php _e('Number of posts to show:'); ?>
    570                 <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" />
    571             </label><br />
    572             <small><?php _e('(at most 15)'); ?></small>
    573         </p>
    574 <?php
    575     }
    576 }
    577 
    578 /**
    579  * Display recent comments widget.
    580  *
    581  * @since 2.2.0
    582  *
    583  * @param array $args Widget arguments.
    584  */
    585 function wp_widget_recent_comments($args) {
    586     global $wpdb, $comments, $comment;
    587     extract($args, EXTR_SKIP);
    588     $options = get_option('widget_recent_comments');
    589     $title = empty($options['title']) ? __('Recent Comments') : apply_filters('widget_title', $options['title']);
    590     if ( !$number = (int) $options['number'] )
    591         $number = 5;
    592     else if ( $number < 1 )
    593         $number = 1;
    594     else if ( $number > 15 )
    595         $number = 15;
    596 
    597     if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
    598         $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number");
    599         wp_cache_add( 'recent_comments', $comments, 'widget' );
    600     }
    601 ?>
    602 
     567    <p><label for="<?php echo $this->get_field_id('title'); ?>">
     568    <?php _e('Title:'); ?>
     569    <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
     570
     571    <p><label for="<?php echo $this->get_field_id('number'); ?>">
     572    <?php _e('Number of posts to show:'); ?>
     573    <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" /></label>
     574    <br /><small><?php _e('(at most 15)'); ?></small></p>
     575<?php
     576    }
     577}
     578
     579/**
     580 * Recent_Comments widget class
     581 *
     582 * @since 2.8.0
     583 */
     584class WP_Widget_Recent_Comments extends WP_Widget {
     585
     586    function WP_Widget_Recent_Comments() {
     587        $widget_ops = array('classname' => 'widget_recent_comments', 'description' => __( 'The most recent comments' ) );
     588        $this->WP_Widget('recent-comments', __('Recent Comments'), $widget_ops);
     589        $this->alt_option_name = 'widget_recent_comments';
     590
     591        if ( is_active_widget(false, false, $this->id_base) )
     592            add_action( 'wp_head', array(&$this, 'recent_comments_style') );
     593
     594        add_action( 'comment_post', array(&$this, 'flush_widget_cache') );
     595        add_action( 'wp_set_comment_status', array(&$this, 'flush_widget_cache') );
     596    }
     597   
     598    function recent_comments_style() { ?>
     599    <style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>
     600<?php
     601    }
     602
     603    function flush_widget_cache() {
     604        wp_cache_delete('recent_comments', 'widget');
     605    }
     606
     607    function widget( $args, $instance ) {
     608        global $wpdb, $comments, $comment;
     609       
     610        extract($args, EXTR_SKIP);
     611        $title = empty($instance['title']) ? __('Recent Comments') : apply_filters('widget_title', $instance['title']);
     612        if ( !$number = (int) $instance['number'] )
     613            $number = 5;
     614        else if ( $number < 1 )
     615            $number = 1;
     616        else if ( $number > 15 )
     617            $number = 15;
     618   
     619        if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) {
     620            $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 15");
     621            wp_cache_add( 'recent_comments', $comments, 'widget' );
     622        }
     623       
     624        $comments = array_slice( (array) $comments, 0, $number );
     625?>
    603626        <?php echo $before_widget; ?>
    604627            <?php echo $before_title . $title . $after_title; ?>
     
    609632        <?php echo $after_widget; ?>
    610633<?php
    611 }
    612 
    613 /**
    614  * Remove the cache for recent comments widget.
    615  *
    616  * @since 2.2.0
    617  */
    618 function wp_delete_recent_comments_cache() {
    619     wp_cache_delete( 'recent_comments', 'widget' );
    620 }
    621 add_action( 'comment_post', 'wp_delete_recent_comments_cache' );
    622 add_action( 'wp_set_comment_status', 'wp_delete_recent_comments_cache' );
    623 
    624 /**
    625  * Display and process recent comments widget options form.
    626  *
    627  * @since 2.2.0
    628  */
    629 function wp_widget_recent_comments_control() {
    630     $options = $newoptions = get_option('widget_recent_comments');
    631     if ( isset($_POST["recent-comments-submit"]) ) {
    632         $newoptions['title'] = strip_tags(stripslashes($_POST["recent-comments-title"]));
    633         $newoptions['number'] = (int) $_POST["recent-comments-number"];
    634     }
    635     if ( $options != $newoptions ) {
    636         $options = $newoptions;
    637         update_option('widget_recent_comments', $options);
    638         wp_delete_recent_comments_cache();
    639     }
    640     $title = attribute_escape($options['title']);
    641     if ( !$number = (int) $options['number'] )
    642         $number = 5;
    643 ?>
    644             <p><label for="recent-comments-title"><?php _e('Title:'); ?> <input class="widefat" id="recent-comments-title" name="recent-comments-title" type="text" value="<?php echo $title; ?>" /></label></p>
    645             <p>
    646                 <label for="recent-comments-number"><?php _e('Number of comments to show:'); ?> <input style="width: 25px; text-align: center;" id="recent-comments-number" name="recent-comments-number" type="text" value="<?php echo $number; ?>" /></label>
    647                 <br />
    648                 <small><?php _e('(at most 15)'); ?></small>
    649             </p>
    650             <input type="hidden" id="recent-comments-submit" name="recent-comments-submit" value="1" />
    651 <?php
    652 }
    653 
    654 /**
    655  * Display the style for recent comments widget.
    656  *
    657  * @since 2.2.0
    658  */
    659 function wp_widget_recent_comments_style() {
    660 ?>
    661 <style type="text/css">.recentcomments a{display:inline !important;padding: 0 !important;margin: 0 !important;}</style>
    662 <?php
    663 }
    664 
    665 /**
    666  * Register recent comments with control and hook for 'wp_head' action.
    667  *
    668  * @since 2.2.0
    669  */
    670 function wp_widget_recent_comments_register() {
    671     $widget_ops = array('classname' => 'widget_recent_comments', 'description' => __( 'The most recent comments' ) );
    672     wp_register_sidebar_widget('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments', $widget_ops);
    673     wp_register_widget_control('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments_control');
    674 
    675     if ( is_active_widget('wp_widget_recent_comments') )
    676         add_action('wp_head', 'wp_widget_recent_comments_style');
     634    }
     635
     636    function update( $new_instance, $old_instance ) {
     637        $instance = $old_instance;
     638        $instance['title'] = strip_tags($new_instance['title']);
     639        $instance['number'] = (int) $new_instance['number'];
     640        $this->flush_widget_cache();
     641
     642        $alloptions = wp_cache_get( 'alloptions', 'options' );
     643        if ( isset($alloptions['widget_recent_comments']) )
     644            delete_option('widget_recent_comments');
     645
     646        return $instance;
     647    }
     648
     649    function form( $instance ) {
     650        $title = attribute_escape($instance['title']);
     651        if ( !$number = (int) $instance['number'] )
     652            $number = 5;
     653?>
     654    <p><label for="<?php echo $this->get_field_id('title'); ?>">
     655    <?php _e('Title:'); ?>
     656    <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></label></p>
     657
     658    <p><label for="<?php echo $this->get_field_id('number'); ?>">
     659    <?php _e('Number of comments to show:'); ?>
     660    <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" /></label>
     661    <br /><small><?php _e('(at most 15)'); ?></small></p>
     662<?php
     663    }
    677664}
    678665
     
    11391126
    11401127    register_widget('WP_Widget_Recent_Posts');
     1128   
     1129    register_widget('WP_Widget_Recent_Comments');
    11411130
    11421131    $widget_ops = array('classname' => 'widget_tag_cloud', 'description' => __( "Your most used tags in cloud format") );
     
    11451134
    11461135    wp_widget_rss_register();
    1147     wp_widget_recent_comments_register();
    11481136
    11491137    do_action('widgets_init');
  • trunk/wp-includes/widgets.php

    r11088 r11090  
    784784
    785785/**
    786  * Whether widget is registered using callback with widget ID.
    787  *
    788  * Without the optional $widget_id parameter, returns the ID of the first sidebar in which the first instance of the widget with the given callback is found.
    789  * With the $widget_id parameter, returns the ID of the sidebar in which the widget with that callback AND that ID is found.
    790  *
    791  * @since 2.2.0
    792  *
    793  * @param callback $callback Widget callback to check.
     786 * Whether widget is displayied on the front-end.
     787 *
     788 * Either $callback or $id_base can be used
     789 * $id_base is the first argument when extending WP_Widget class
     790 * Without the optional $widget_id parameter, returns the ID of the first sidebar
     791 * in which the first instance of the widget with the given callback or $id_base is found.
     792 * With the $widget_id parameter, returns the ID of the sidebar where
     793 * the widget with that callback/$id_base AND that ID is found.
     794 *
     795 * NOTE: $widget_id and $id_base are the same for single widgets. To be effective
     796 * this function has to run after widgets have initialized, at action 'init' or later.
     797 *
     798 * @since 2.2.0
     799 *
     800 * @param callback Optional, Widget callback to check.
    794801 * @param int $widget_id Optional, but needed for checking. Widget ID.
    795 /* @return mixed false if widget is not active or id of sidebar in which the widget is active.
    796  */
    797 function is_active_widget($callback, $widget_id = false) {
     802 * @param string $id_base Optional, the base ID of a widget created by extending WP_Widget.
     803 * @return mixed false if widget is not active or id of sidebar in which the widget is active.
     804 */
     805function is_active_widget($callback = false, $widget_id = false, $id_base = false) {
    798806    global $wp_registered_widgets;
    799807
     
    801809
    802810    if ( is_array($sidebars_widgets) ) foreach ( $sidebars_widgets as $sidebar => $widgets )
     811        if ( 'wp_inactive_widgets' == $sidebar )
     812            continue;
     813
    803814        if ( is_array($widgets) ) foreach ( $widgets as $widget )
    804             if ( isset($wp_registered_widgets[$widget]['callback']) && $wp_registered_widgets[$widget]['callback'] == $callback )
     815            if ( ( $callback && isset($wp_registered_widgets[$widget]['callback']) && $wp_registered_widgets[$widget]['callback'] == $callback ) || ( $id_base && preg_replace( '/-[0-9]+$/', '', $widget ) == $id_base ) ) {
    805816                if ( !$widget_id || $widget_id == $wp_registered_widgets[$widget]['id'] )
    806817                    return $sidebar;
    807 
     818            }
    808819
    809820    return false;
Note: See TracChangeset for help on using the changeset viewer.