3 // Registers dashboard widgets, handles POST data, sets up filters
4 function wp_dashboard_setup() {
5 global $wpdb, $wp_dashboard_sidebars;
7 $widget_options = get_option( 'dashboard_widget_options' );
8 if ( !$widget_options || !is_array($widget_options) )
9 $widget_options = array();
12 /* Register WP Dashboard Dynamic Sidebar */
13 register_sidebar( array(
14 'name' => 'WordPress Dashboard',
15 'id' => 'wp_dashboard',
16 'before_widget' => "\t<div class='dashboard-widget-holder %2\$s' id='%1\$s'>\n\n\t\t<div class='dashboard-widget'>\n\n",
17 'after_widget' => "\t\t</div>\n\n\t</div>\n\n",
18 'before_title' => "\t\t\t<h3 class='dashboard-widget-title'>",
19 'after_title' => "</h3>\n\n"
23 /* Register Widgets and Controls */
25 // Recent Comments Widget
26 if ( current_user_can( 'moderate_comments' ) && $mod_comments = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '0'") ) {
27 $notice = sprintf( __ngettext( '%d comment awaiting moderation', '%d comments awaiting moderation', $mod_comments ), $mod_comments );
28 $notice = "<a href='edit-comments.php?comment_status=moderated'>$notice</a>";
32 wp_register_sidebar_widget( 'dashboard_recent_comments', __( 'Recent Comments' ), 'wp_dashboard_recent_comments',
33 array( 'all_link' => 'edit-comments.php', 'notice' => $notice, 'width' => 'half' )
36 // Incoming Links Widget
37 if ( !isset( $widget_options['dashboard_incoming_links'] ) ) {
39 $widget_options['dashboard_incoming_links'] = array(
40 'link' => apply_filters( 'dashboard_incoming_links_link', 'http://blogsearch.google.com/blogsearch?hl=en&scoring=d&partner=wordpress&q=link:' . trailingslashit( get_option('home') ) ),
41 'url' => apply_filters( 'dashboard_incoming_links_feed', 'http://blogsearch.google.com/blogsearch_feeds?hl=en&scoring=d&ie=utf-8&num=10&output=rss&partner=wordpress&q=link:' . trailingslashit( get_option('home') ) ),
46 wp_register_sidebar_widget( 'dashboard_incoming_links', __( 'Incoming Links' ), 'wp_dashboard_empty',
47 array( 'all_link' => $widget_options['dashboard_incoming_links']['link'], 'feed_link' => $widget_options['dashboard_incoming_links']['url'], 'width' => 'half' ),
48 'wp_dashboard_cached_rss_widget', 'wp_dashboard_incoming_links_output'
50 wp_register_widget_control( 'dashboard_incoming_links', __( 'Incoming Links' ), 'wp_dashboard_rss_control', array(),
51 array( 'widget_id' => 'dashboard_incoming_links', 'form_inputs' => array( 'title' => false, 'show_summary' => false, 'show_author' => false ) )
56 wp_register_sidebar_widget( 'dashboard_plugins', __( 'Plugins' ), 'wp_dashboard_empty',
57 array( 'all_link' => 'http://wordpress.org/extend/plugins/', 'feed_link' => 'http://wordpress.org/extend/plugins/rss/topics/', 'width' => 'half' ),
58 'wp_dashboard_cached_rss_widget', 'wp_dashboard_plugins_output',
59 array( 'http://wordpress.org/extend/plugins/rss/browse/popular/', 'http://wordpress.org/extend/plugins/rss/browse/new/', 'http://wordpress.org/extend/plugins/rss/browse/updated/' )
62 // Primary feed (Dev Blog) Widget
63 if ( !isset( $widget_options['dashboard_primary'] ) ) {
65 $widget_options['dashboard_primary'] = array(
66 'link' => apply_filters( 'dashboard_primary_link', __( 'http://wordpress.org/development/' ) ),
67 'url' => apply_filters( 'dashboard_primary_feed', __( 'http://wordpress.org/development/feed/' ) ),
68 'title' => apply_filters( 'dashboard_primary_title', __( 'WordPress Development Blog' ) ),
75 wp_register_sidebar_widget( 'dashboard_primary', $widget_options['dashboard_primary']['title'], 'wp_dashboard_empty',
76 array( 'all_link' => $widget_options['dashboard_primary']['link'], 'feed_link' => $widget_options['dashboard_primary']['url'], 'width' => 'half', 'class' => 'widget_rss' ),
77 'wp_dashboard_cached_rss_widget', 'wp_dashboard_rss_output'
79 wp_register_widget_control( 'dashboard_primary', __( 'Primary Feed' ), 'wp_dashboard_rss_control', array(),
80 array( 'widget_id' => 'dashboard_primary' )
84 // Secondary Feed (Planet) Widget
85 if ( !isset( $widget_options['dashboard_secondary'] ) ) {
87 $widget_options['dashboard_secondary'] = array(
88 'link' => apply_filters( 'dashboard_secondary_link', __( 'http://planet.wordpress.org/' ) ),
89 'url' => apply_filters( 'dashboard_secondary_feed', __( 'http://planet.wordpress.org/feed/' ) ),
90 'title' => apply_filters( 'dashboard_secondary_title', __( 'Other WordPress News' ) ),
94 wp_register_sidebar_widget( 'dashboard_secondary', $widget_options['dashboard_secondary']['title'], 'wp_dashboard_empty',
95 array( 'all_link' => $widget_options['dashboard_secondary']['link'], 'feed_link' => $widget_options['dashboard_secondary']['url'], 'width' => 'full' ),
96 'wp_dashboard_cached_rss_widget', 'wp_dashboard_secondary_output'
98 wp_register_widget_control( 'dashboard_secondary', __( 'Secondary Feed' ), 'wp_dashboard_rss_control', array(),
99 array( 'widget_id' => 'dashboard_secondary', 'form_inputs' => array( 'show_summary' => false, 'show_author' => false, 'show_date' => false ) )
103 /* Dashboard Widget Template
104 wp_register_sidebar_widget( $widget_id (unique slug) , $widget_title, $output_callback,
106 'all_link' => full url for "See All" link,
107 'feed_link' => full url for "RSS" link,
108 'width' => 'fourth', 'third', 'half', 'full' (defaults to 'half'),
109 'height' => 'single', 'double' (defaults to 'single'),
111 $wp_dashboard_empty_callback (only needed if using 'wp_dashboard_empty' as your $output_callback),
112 $arg, $arg, $arg... (further args passed to callbacks)
115 // optional: if you want users to be able to edit the settings of your widget, you need to register a widget_control
116 wp_register_widget_control( $widget_id, $widget_control_title, $control_output_callback,
117 array(), // leave an empty array here: oddity in widget code
119 'widget_id' => $widget_id, // Yes - again. This is required: oddity in widget code
120 'arg' => an arg to pass to the $control_output_callback,
121 'another' => another arg to pass to the $control_output_callback,
127 // Hook to register new widgets
128 do_action( 'wp_dashboard_setup' );
130 // Hard code the sidebar's widgets and order
131 $dashboard_widgets = array();
132 $dashboard_widgets[] = 'dashboard_recent_comments';
133 $dashboard_widgets[] = 'dashboard_incoming_links';
134 $dashboard_widgets[] = 'dashboard_primary';
135 if ( current_user_can( 'activate_plugins' ) )
136 $dashboard_widgets[] = 'dashboard_plugins';
137 $dashboard_widgets[] = 'dashboard_secondary';
139 // Filter widget order
140 $dashboard_widgets = apply_filters( 'wp_dashboard_widgets', $dashboard_widgets );
142 $wp_dashboard_sidebars = array( 'wp_dashboard' => $dashboard_widgets, 'array_version' => 3.5 );
144 add_filter( 'dynamic_sidebar_params', 'wp_dashboard_dynamic_sidebar_params' );
146 if ( 'POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['widget_id']) ) {
147 ob_start(); // hack - but the same hack wp-admin/widgets.php uses
148 wp_dashboard_trigger_widget_control( $_POST['widget_id'] );
150 wp_redirect( remove_query_arg( 'edit' ) );
155 update_option( 'dashboard_widget_options', $widget_options );
158 // Echoes out the dashboard
159 function wp_dashboard() {
160 echo "<div id='dashboard-widgets'>\n\n";
162 // We're already filtering dynamic_sidebar_params obove
163 add_filter( 'option_sidebars_widgets', 'wp_dashboard_sidebars_widgets' ); // here there be hackery
164 dynamic_sidebar( 'wp_dashboard' );
165 remove_filter( 'option_sidebars_widgets', 'wp_dashboard_sidebars_widgets' );
167 echo "<br class='clear' />\n</div>\n\n\n";
170 // Makes sidebar_widgets option reflect the dashboard settings
171 function wp_dashboard_sidebars_widgets() { // hackery
172 return $GLOBALS['wp_dashboard_sidebars'];
175 // Modifies sidbar params on the fly to set up ids, class names, titles for each widget (called once per widget)
176 // Switches widget to edit mode if $_GET['edit']
177 function wp_dashboard_dynamic_sidebar_params( $params ) {
178 global $wp_registered_widgets, $wp_registered_widget_controls;
180 $sidebar_defaults = array('widget_id' => 0, 'before_widget' => '', 'after_widget' => '', 'before_title' => '', 'after_title' => '');
181 extract( $sidebar_defaults, EXTR_PREFIX_ALL, 'sidebar' );
182 extract( $params[0], EXTR_PREFIX_ALL, 'sidebar' );
184 if ( !isset($wp_registered_widgets[$sidebar_widget_id]) || !is_array($wp_registered_widgets[$sidebar_widget_id]) ) {
187 $widget_defaults = array('id' => '', 'width' => '', 'height' => '', 'class' => '', 'feed_link' => '', 'all_link' => '', 'notice' => false, 'error' => false);
188 extract( $widget_defaults, EXTR_PREFIX_ALL, 'widget' );
189 extract( $wp_registered_widgets[$sidebar_widget_id], EXTR_PREFIX_ALL, 'widget' );
191 $the_classes = array();
192 if ( in_array($widget_width, array( 'third', 'fourth', 'full' ) ) )
193 $the_classes[] = $widget_width;
195 if ( 'double' == $widget_height )
196 $the_classes[] = 'double';
199 $the_classes[] = $widget_class;
201 // Add classes to the widget holder
203 $sidebar_before_widget = str_replace( "<div class='dashboard-widget-holder ", "<div class='dashboard-widget-holder " . join( ' ', $the_classes ) . ' ', $sidebar_before_widget );
206 if ( $widget_all_link )
207 $links[] = '<a href="' . clean_url( $widget_all_link ) . '">' . __( 'See All' ) . '</a>';
209 $content_class = 'dashboard-widget-content';
210 if ( current_user_can( 'edit_dashboard' ) && isset($wp_registered_widget_controls[$widget_id]) && is_callable($wp_registered_widget_controls[$widget_id]['callback']) ) {
211 // Switch this widget to edit mode
212 if ( isset($_GET['edit']) && $_GET['edit'] == $widget_id ) {
213 $content_class .= ' dashboard-widget-control';
214 $wp_registered_widgets[$widget_id]['callback'] = 'wp_dashboard_empty';
215 $sidebar_widget_name = $wp_registered_widget_controls[$widget_id]['name'];
216 $params[1] = 'wp_dashboard_trigger_widget_control';
217 $sidebar_before_widget .= '<form action="' . remove_query_arg( 'edit' ) . '" method="post">';
218 $sidebar_after_widget = "<div class='dashboard-widget-submit'><input type='hidden' name='sidebar' value='wp_dashboard' /><input type='hidden' name='widget_id' value='$widget_id' /><input type='submit' value='" . __( 'Save' ) . "' /></div></form>$sidebar_after_widget";
219 $links[] = '<a href="' . remove_query_arg( 'edit' ) . '">' . __( 'Cancel' ) . '</a>';
221 $links[] = '<a href="' . add_query_arg( 'edit', $widget_id ) . "#$widget_id" . '">' . __( 'Edit' ) . '</a>';
225 if ( $widget_feed_link )
226 $links[] = '<img class="rss-icon" src="' . get_option( 'siteurl' ) . '/' . WPINC . '/images/rss.png" alt="' . __( 'rss icon' ) . '" /> <a href="' . clean_url( $widget_feed_link ) . '">' . __( 'RSS' ) . '</a>';
228 $links = apply_filters( "wp_dashboard_widget_links_$widget_id", $links );
230 // Add links to widget's title bar
232 $sidebar_before_title .= '<span>';
233 $sidebar_after_title = '</span><small>' . join( ' | ', $links ) . "</small><br class='clear' />$sidebar_after_title";
236 // Could have put this in widget-content. Doesn't really matter
237 if ( $widget_notice )
238 $sidebar_after_title .= "\t\t\t<div class='dashboard-widget-notice'>$widget_notice</div>\n\n";
241 $sidebar_after_title .= "\t\t\t<div class='dashboard-widget-error'>$widget_error</div>\n\n";
243 $sidebar_after_title .= "\t\t\t<div class='$content_class'>\n\n";
245 $sidebar_after_widget .= "\t\t\t</div>\n\n";
247 foreach( array_keys( $params[0] ) as $key )
248 $$key = ${'sidebar_' . $key};
250 $params[0] = compact( array_keys( $params[0] ) );
256 /* Dashboard Widgets */
258 function wp_dashboard_recent_comments( $sidebar_args ) {
260 extract( $sidebar_args, EXTR_SKIP );
268 $lambda = create_function( '', 'return 5;' );
269 add_filter( 'option_posts_per_rss', $lambda ); // hack - comments query doesn't accept per_page parameter
270 $comments_query = new WP_Query('feed=rss2&withcomments=1');
271 remove_filter( 'option_posts_per_rss', $lambda );
275 if ( $comments_query->have_comments() ) {
276 while ( $comments_query->have_comments() ) { $comments_query->the_comment();
278 $comment_post_url = get_permalink( $comment->comment_post_ID );
279 $comment_post_title = get_the_title( $comment->comment_post_ID );
280 $comment_post_link = "<a href='$comment_post_url'>$comment_post_title</a>";
281 $comment_link = '<a class="comment-link" href="' . get_comment_link() . '">#</a>';
282 $comment_meta = sprintf( __( 'From <strong>%1$s</strong> on %2$s %3$s' ), get_comment_author(), $comment_post_link, $comment_link );
284 if ( $is_first ) : $is_first = false;
286 <blockquote><p>“<?php comment_excerpt(); ?>”</p></blockquote>
287 <p class='comment-meta'><?php echo $comment_meta; ?></p>
289 if ( $comments_query->comment_count > 1 ) : ?>
290 <ul id="dashboard-comments-list">
292 endif; // comment_count
296 <li class='comment-meta'><?php echo $comment_meta; ?></li>
301 if ( $comments_query->comment_count > 1 ) : ?>
304 endif; // comment_count;
311 // $sidebar_args are handled by wp_dashboard_empty()
312 function wp_dashboard_incoming_links_output() {
313 $widgets = get_option( 'dashboard_widget_options' );
314 @extract( @$widgets['dashboard_incoming_links'], EXTR_SKIP );
315 $rss = @fetch_rss( $url );
316 if ( isset($rss->items) && 1 < count($rss->items) ) {// Technorati returns a 1-item feed when it has no results
320 $rss->items = array_slice($rss->items, 0, $items);
321 foreach ( $rss->items as $item ) {
327 $link = clean_url( strip_tags( $item['link'] ) );
329 if ( isset( $item['author_uri'] ) )
330 $site_link = clean_url( strip_tags( $item['author_uri'] ) );
332 if ( !$publisher = wp_specialchars( strip_tags( isset($item['dc']['publisher']) ? $item['dc']['publisher'] : $item['author_name'] ) ) )
333 $publisher = __( 'Somebody' );
335 $publisher = "<a href='$site_link'>$publisher</a>";
337 $publisher = "<strong>$publisher</strong>";
339 if ( isset($item['description']) )
340 $content = $item['description'];
341 elseif ( isset($item['summary']) )
342 $content = $item['summary'];
343 elseif ( isset($item['atom_content']) )
344 $content = $item['atom_content'];
346 $content = __( 'something' );
347 $content = wp_html_excerpt($content, 50) . ' ...';
349 $text = _c( '%1$s linked here <a href="%2$s">saying</a>, "%3$s"|feed_display' );
351 $text = _c( '%1$s linked here saying, "%3$s"|feed_display' );
354 if ( $show_author || $show_summary )
355 $text .= _c( ' on %4$s|feed_display' );
356 $date = wp_specialchars( strip_tags( isset($item['pubdate']) ? $item['pubdate'] : $item['published'] ) );
357 $date = strtotime( $date );
358 $date = gmdate( get_option( 'date_format' ), $date );
361 echo "\t<li>" . sprintf( _c( "$text|feed_display" ), $publisher, $link, $content, $date ) . "</li>\n";
367 echo '<p>' . __('This dashboard widget queries <a href="http://blogsearch.google.com/">Google Blog Search</a> so that when another blog links to your site it will show up here. It has found no incoming links… yet. It’s okay — there is no rush.') . "</p>\n";
371 // $sidebar_args are handled by wp_dashboard_empty()
372 function wp_dashboard_rss_output( $widget_id ) {
373 $widgets = get_option( 'dashboard_widget_options' );
374 wp_widget_rss_output( $widgets[$widget_id] );
377 // $sidebar_args are handled by wp_dashboard_empty()
378 function wp_dashboard_secondary_output() {
379 $widgets = get_option( 'dashboard_widget_options' );
380 @extract( @$widgets['dashboard_secondary'], EXTR_SKIP );
381 $rss = @fetch_rss( $url );
382 if ( !isset($rss->items) || 0 == count($rss->items) )
385 echo "<ul id='planetnews'>\n";
387 $rss->items = array_slice($rss->items, 0, $items);
388 foreach ($rss->items as $item ) {
389 $title = wp_specialchars($item['title']);
390 $author = preg_replace( '|(.+?):.+|s', '$1', $item['title'] );
391 $post = preg_replace( '|.+?:(.+)|s', '$1', $item['title'] );
392 $link = clean_url($item['link']);
394 echo "\t<li><a href='$link'><span class='post'>$post</span><span class='hidden'> - </span><cite>$author</cite></a></li>\n";
397 echo "</ul>\n<br class='clear' />\n";
400 // $sidebar_args are handled by wp_dashboard_empty()
401 function wp_dashboard_plugins_output() {
402 $popular = @fetch_rss( 'http://wordpress.org/extend/plugins/rss/browse/popular/' );
403 $new = @fetch_rss( 'http://wordpress.org/extend/plugins/rss/browse/new/' );
404 $updated = @fetch_rss( 'http://wordpress.org/extend/plugins/rss/browse/updated/' );
406 foreach ( array( 'popular' => __('Most Popular'), 'new' => __('Newest Plugins'), 'updated' => __('Recently Updated') ) as $feed => $label ) {
407 if ( !isset($$feed->items) || 0 == count($$feed->items) )
410 $$feed->items = array_slice($$feed->items, 0, 5);
411 $item_key = array_rand($$feed->items);
413 // Eliminate some common badly formed plugin descriptions
414 while ( ( null !== $item_key = array_rand($$feed->items) ) && false !== strpos( $$feed->items[$item_key]['description'], 'Plugin Name:' ) )
415 unset($$feed->items[$item_key]);
417 if ( !isset($$feed->items[$item_key]) )
420 $item = $$feed->items[$item_key];
422 // current bbPress feed item titles are: user on "topic title"
423 if ( preg_match( '/"(.*)"/s', $item['title'], $matches ) )
424 $title = $matches[1];
425 else // but let's make it forward compatible if things change
426 $title = $item['title'];
427 $title = wp_specialchars( $title );
429 $description = wp_specialchars( strip_tags(html_entity_decode($item['description'], ENT_QUOTES)) );
431 list($link, $frag) = explode( '#', $item['link'] );
433 $link = clean_url($link);
434 $dlink = rtrim($link, '/') . '/download/';
436 echo "<h4>$label</h4>\n";
437 echo "<h5><a href='$link'>$title</a></h5> <span>(<a href='$dlink'>" . __( 'Download' ) . "</a>)</span>\n";
438 echo "<p>$description</p>\n";
442 // Checks to see if all of the feed url in $check_urls are cached.
443 // If $check_urls is empty, look for the rss feed url found in the dashboard widget optios of $widget_id.
444 // If cached, call $callback, a function that echoes out output for this widget.
445 // If not cache, echo a "Loading..." stub which is later replaced by AJAX call (see top of /wp-admin/index.php)
446 function wp_dashboard_cached_rss_widget( $widget_id, $callback, $check_urls = array() ) {
447 $loading = '<p class="widget-loading">' . __( 'Loading…' ) . '</p>';
449 if ( empty($check_urls) ) {
450 $widgets = get_option( 'dashboard_widget_options' );
451 if ( empty($widgets[$widget_id]['url']) ) {
455 $check_urls = array( $widgets[$widget_id]['url'] );
459 require_once( ABSPATH . WPINC . '/rss.php' );
460 init(); // initialize rss constants
462 $cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE );
464 foreach ( $check_urls as $check_url ) {
465 $status = $cache->check_cache( $check_url );
466 if ( 'HIT' !== $status ) {
472 if ( $callback && is_callable( $callback ) ) {
473 $args = array_slice( func_get_args(), 2 );
474 array_unshift( $args, $widget_id );
475 call_user_func_array( $callback, $args );
481 // Empty widget used for JS/AJAX created output.
482 // Callback inserts content between before_widget and after_widget. Used when widget is in edit mode. Can also be used for custom widgets.
483 function wp_dashboard_empty( $sidebar_args, $callback = false ) {
484 extract( $sidebar_args, EXTR_SKIP );
492 // When in edit mode, the callback passed to this function is the widget_control callback
493 if ( $callback && is_callable( $callback ) ) {
494 $args = array_slice( func_get_args(), 2 );
495 array_unshift( $args, $widget_id );
496 call_user_func_array( $callback, $args );
502 /* Dashboard Widgets Controls. Ssee also wp_dashboard_empty() */
504 // Calls widget_control callback
505 function wp_dashboard_trigger_widget_control( $widget_control_id = false ) {
506 global $wp_registered_widget_controls;
507 if ( is_scalar($widget_control_id) && $widget_control_id && isset($wp_registered_widget_controls[$widget_control_id]) && is_callable($wp_registered_widget_controls[$widget_control_id]['callback']) )
508 call_user_func_array( $wp_registered_widget_controls[$widget_control_id]['callback'], $wp_registered_widget_controls[$widget_control_id]['params'] );
511 // Sets up $args to be used as input to wp_widget_rss_form(), handles POST data from RSS-type widgets
512 function wp_dashboard_rss_control( $args ) {
517 if ( !$widget_options = get_option( 'dashboard_widget_options' ) )
518 $widget_options = array();
520 if ( !isset($widget_options[$widget_id]) )
521 $widget_options[$widget_id] = array();
523 $number = 1; // Hack to use wp_widget_rss_form()
524 $widget_options[$widget_id]['number'] = $number;
526 if ( 'POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['widget-rss'][$number]) ) {
527 $_POST['widget-rss'][$number] = stripslashes_deep( $_POST['widget-rss'][$number] );
528 $widget_options[$widget_id] = wp_widget_rss_process( $_POST['widget-rss'][$number] );
529 // title is optional. If black, fill it if possible
530 if ( !$widget_options[$widget_id]['title'] && isset($_POST['widget-rss'][$number]['title']) ) {
531 require_once(ABSPATH . WPINC . '/rss.php');
532 $rss = fetch_rss($widget_options[$widget_id]['url']);
533 $widget_options[$widget_id]['title'] = htmlentities(strip_tags($rss->channel['title']));
535 update_option( 'dashboard_widget_options', $widget_options );
538 wp_widget_rss_form( $widget_options[$widget_id], $form_inputs );