From 15588b40800cdb732d920f64060f0a8b8f009f2d Mon Sep 17 00:00:00 2001 From: azaozz Date: Sun, 26 Apr 2009 20:09:08 +0000 Subject: [PATCH] Move recent comments widget to WP_Widget, extend is_active_widget() to use $id_base, see #8441 git-svn-id: http://svn.automattic.com/wordpress/trunk@11090 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/default-widgets.php | 186 +++++++++++++++----------------- wp-includes/widgets.php | 27 +++-- 2 files changed, 106 insertions(+), 107 deletions(-) diff --git a/wp-includes/default-widgets.php b/wp-includes/default-widgets.php index e3444e777..e4a5dcb4d 100644 --- a/wp-includes/default-widgets.php +++ b/wp-includes/default-widgets.php @@ -504,8 +504,13 @@ class WP_Widget_Recent_Posts extends WP_Widget { } function widget($args, $instance) { - if ( $output = wp_cache_get('widget_recent_posts' . $args['widget_id'], 'widget') ) - return print($output); + $cache = wp_cache_get('widget_recent_posts', 'widget'); + + if ( !is_array($cache) ) + $cache = array(); + + if ( isset($cache[$args['widget_id']]) ) + return $cache[$args['widget_id']]; ob_start(); extract($args); @@ -533,7 +538,8 @@ class WP_Widget_Recent_Posts extends WP_Widget { wp_reset_query(); // Restore global post data stomped by the_post(). endif; - wp_cache_add('widget_recent_posts' . $args['widget_id'], ob_get_flush(), 'widget'); + $cache[$args['widget_id']] = ob_get_flush(); + wp_cache_add('widget_recent_posts', $cache, 'widget'); } function update( $new_instance, $old_instance ) { @@ -550,7 +556,7 @@ class WP_Widget_Recent_Posts extends WP_Widget { } function flush_widget_cache() { - wp_cache_delete('widget_recent_entries', 'widget'); + wp_cache_delete('widget_recent_posts', 'widget'); } function form( $instance ) { @@ -558,48 +564,65 @@ class WP_Widget_Recent_Posts extends WP_Widget { if ( !$number = (int) $instance['number'] ) $number = 5; ?> -

- -

-

-
- -

+

+ +

+

15 ) - $number = 15; +class WP_Widget_Recent_Comments extends WP_Widget { - if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) { - $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $number"); - wp_cache_add( 'recent_comments', $comments, 'widget' ); + function WP_Widget_Recent_Comments() { + $widget_ops = array('classname' => 'widget_recent_comments', 'description' => __( 'The most recent comments' ) ); + $this->WP_Widget('recent-comments', __('Recent Comments'), $widget_ops); + $this->alt_option_name = 'widget_recent_comments'; + + if ( is_active_widget(false, false, $this->id_base) ) + add_action( 'wp_head', array(&$this, 'recent_comments_style') ); + + add_action( 'comment_post', array(&$this, 'flush_widget_cache') ); + add_action( 'wp_set_comment_status', array(&$this, 'flush_widget_cache') ); + } + + function recent_comments_style() { ?> + + + function flush_widget_cache() { + wp_cache_delete('recent_comments', 'widget'); + } + + function widget( $args, $instance ) { + global $wpdb, $comments, $comment; + + extract($args, EXTR_SKIP); + $title = empty($instance['title']) ? __('Recent Comments') : apply_filters('widget_title', $instance['title']); + if ( !$number = (int) $instance['number'] ) + $number = 5; + else if ( $number < 1 ) + $number = 1; + else if ( $number > 15 ) + $number = 15; + + if ( !$comments = wp_cache_get( 'recent_comments', 'widget' ) ) { + $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT 15"); + wp_cache_add( 'recent_comments', $comments, 'widget' ); + } + + $comments = array_slice( (array) $comments, 0, $number ); +?> flush_widget_cache(); + + $alloptions = wp_cache_get( 'alloptions', 'options' ); + if ( isset($alloptions['widget_recent_comments']) ) + delete_option('widget_recent_comments'); + + return $instance; } - $title = attribute_escape($options['title']); - if ( !$number = (int) $options['number'] ) - $number = 5; + + function form( $instance ) { + $title = attribute_escape($instance['title']); + if ( !$number = (int) $instance['number'] ) + $number = 5; ?> -

-

- -
- -

- +

+ +

+

- - 'widget_recent_comments', 'description' => __( 'The most recent comments' ) ); - wp_register_sidebar_widget('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments', $widget_ops); - wp_register_widget_control('recent-comments', __('Recent Comments'), 'wp_widget_recent_comments_control'); - - if ( is_active_widget('wp_widget_recent_comments') ) - add_action('wp_head', 'wp_widget_recent_comments_style'); + } } /** @@ -1138,13 +1125,14 @@ function wp_widgets_init() { register_widget('WP_Widget_Categories'); register_widget('WP_Widget_Recent_Posts'); + + register_widget('WP_Widget_Recent_Comments'); $widget_ops = array('classname' => 'widget_tag_cloud', 'description' => __( "Your most used tags in cloud format") ); wp_register_sidebar_widget('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud', $widget_ops); wp_register_widget_control('tag_cloud', __('Tag Cloud'), 'wp_widget_tag_cloud_control' ); wp_widget_rss_register(); - wp_widget_recent_comments_register(); do_action('widgets_init'); } diff --git a/wp-includes/widgets.php b/wp-includes/widgets.php index 80f69fab7..66003c869 100644 --- a/wp-includes/widgets.php +++ b/wp-includes/widgets.php @@ -783,28 +783,39 @@ function dynamic_sidebar($index = 1) { } /** - * Whether widget is registered using callback with widget ID. + * Whether widget is displayied on the front-end. * - * 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. - * With the $widget_id parameter, returns the ID of the sidebar in which the widget with that callback AND that ID is found. + * Either $callback or $id_base can be used + * $id_base is the first argument when extending WP_Widget class + * 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 or $id_base is found. + * With the $widget_id parameter, returns the ID of the sidebar where + * the widget with that callback/$id_base AND that ID is found. + * + * NOTE: $widget_id and $id_base are the same for single widgets. To be effective + * this function has to run after widgets have initialized, at action 'init' or later. * * @since 2.2.0 * - * @param callback $callback Widget callback to check. + * @param callback Optional, Widget callback to check. * @param int $widget_id Optional, but needed for checking. Widget ID. -/* @return mixed false if widget is not active or id of sidebar in which the widget is active. + * @param string $id_base Optional, the base ID of a widget created by extending WP_Widget. + * @return mixed false if widget is not active or id of sidebar in which the widget is active. */ -function is_active_widget($callback, $widget_id = false) { +function is_active_widget($callback = false, $widget_id = false, $id_base = false) { global $wp_registered_widgets; $sidebars_widgets = wp_get_sidebars_widgets(false); if ( is_array($sidebars_widgets) ) foreach ( $sidebars_widgets as $sidebar => $widgets ) + if ( 'wp_inactive_widgets' == $sidebar ) + continue; + if ( is_array($widgets) ) foreach ( $widgets as $widget ) - if ( isset($wp_registered_widgets[$widget]['callback']) && $wp_registered_widgets[$widget]['callback'] == $callback ) + if ( ( $callback && isset($wp_registered_widgets[$widget]['callback']) && $wp_registered_widgets[$widget]['callback'] == $callback ) || ( $id_base && preg_replace( '/-[0-9]+$/', '', $widget ) == $id_base ) ) { if ( !$widget_id || $widget_id == $wp_registered_widgets[$widget]['id'] ) return $sidebar; - + } return false; }