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 $mod_comments = wp_count_comments();
27 $mod_comments = $mod_comments->moderated;
28 if ( current_user_can( 'moderate_comments' ) && $mod_comments ) {
29 $notice = sprintf( __ngettext( '%d comment awaiting moderation', '%d comments awaiting moderation', $mod_comments ), $mod_comments );
30 $notice = "<a href='edit-comments.php?comment_status=moderated'>$notice</a>";
34 wp_register_sidebar_widget( 'dashboard_recent_comments', __( 'Recent Comments' ), 'wp_dashboard_recent_comments',
35 array( 'all_link' => 'edit-comments.php', 'notice' => $notice, 'width' => 'half' )
38 // Incoming Links Widget
39 if ( !isset( $widget_options['dashboard_incoming_links'] ) || !isset( $widget_options['dashboard_incoming_links']['home'] ) || $widget_options['dashboard_incoming_links']['home'] != get_option('home') ) {
41 $widget_options['dashboard_incoming_links'] = array(
42 'home' => get_option('home'),
43 'link' => apply_filters( 'dashboard_incoming_links_link', 'http://blogsearch.google.com/blogsearch?hl=en&scoring=d&partner=wordpress&q=link:' . trailingslashit( get_option('home') ) ),
44 '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') ) ),
49 wp_register_sidebar_widget( 'dashboard_incoming_links', __( 'Incoming Links' ), 'wp_dashboard_empty',
50 array( 'all_link' => $widget_options['dashboard_incoming_links']['link'], 'feed_link' => $widget_options['dashboard_incoming_links']['url'], 'width' => 'half' ),
51 'wp_dashboard_cached_rss_widget', 'wp_dashboard_incoming_links_output'
53 wp_register_widget_control( 'dashboard_incoming_links', __( 'Incoming Links' ), 'wp_dashboard_rss_control', array(),
54 array( 'widget_id' => 'dashboard_incoming_links', 'form_inputs' => array( 'title' => false, 'show_summary' => false, 'show_author' => false ) )
59 wp_register_sidebar_widget( 'dashboard_plugins', __( 'Plugins' ), 'wp_dashboard_empty',
60 array( 'all_link' => 'http://wordpress.org/extend/plugins/', 'feed_link' => 'http://wordpress.org/extend/plugins/rss/topics/', 'width' => 'half' ),
61 'wp_dashboard_cached_rss_widget', 'wp_dashboard_plugins_output',
62 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/' )
65 // Primary feed (Dev Blog) Widget
66 if ( !isset( $widget_options['dashboard_primary'] ) ) {
68 $widget_options['dashboard_primary'] = array(
69 'link' => apply_filters( 'dashboard_primary_link', __( 'http://wordpress.org/development/' ) ),
70 'url' => apply_filters( 'dashboard_primary_feed', __( 'http://wordpress.org/development/feed/' ) ),
71 'title' => apply_filters( 'dashboard_primary_title', __( 'WordPress Development Blog' ) ),
78 wp_register_sidebar_widget( 'dashboard_primary', $widget_options['dashboard_primary']['title'], 'wp_dashboard_empty',
79 array( 'all_link' => $widget_options['dashboard_primary']['link'], 'feed_link' => $widget_options['dashboard_primary']['url'], 'width' => 'half', 'class' => 'widget_rss' ),
80 'wp_dashboard_cached_rss_widget', 'wp_dashboard_rss_output'
82 wp_register_widget_control( 'dashboard_primary', __( 'Primary Feed' ), 'wp_dashboard_rss_control', array(),
83 array( 'widget_id' => 'dashboard_primary' )
87 // Secondary Feed (Planet) Widget
88 if ( !isset( $widget_options['dashboard_secondary'] ) ) {
90 $widget_options['dashboard_secondary'] = array(
91 'link' => apply_filters( 'dashboard_secondary_link', __( 'http://planet.wordpress.org/' ) ),
92 'url' => apply_filters( 'dashboard_secondary_feed', __( 'http://planet.wordpress.org/feed/' ) ),
93 'title' => apply_filters( 'dashboard_secondary_title', __( 'Other WordPress News' ) ),
97 wp_register_sidebar_widget( 'dashboard_secondary', $widget_options['dashboard_secondary']['title'], 'wp_dashboard_empty',
98 array( 'all_link' => $widget_options['dashboard_secondary']['link'], 'feed_link' => $widget_options['dashboard_secondary']['url'], 'width' => 'full' ),
99 'wp_dashboard_cached_rss_widget', 'wp_dashboard_secondary_output'
101 wp_register_widget_control( 'dashboard_secondary', __( 'Secondary Feed' ), 'wp_dashboard_rss_control', array(),
102 array( 'widget_id' => 'dashboard_secondary', 'form_inputs' => array( 'show_summary' => false, 'show_author' => false, 'show_date' => false ) )
106 /* Dashboard Widget Template
107 wp_register_sidebar_widget( $widget_id (unique slug) , $widget_title, $output_callback,
109 'all_link' => full url for "See All" link,
110 'feed_link' => full url for "RSS" link,
111 'width' => 'fourth', 'third', 'half', 'full' (defaults to 'half'),
112 'height' => 'single', 'double' (defaults to 'single'),
114 $wp_dashboard_empty_callback (only needed if using 'wp_dashboard_empty' as your $output_callback),
115 $arg, $arg, $arg... (further args passed to callbacks)
118 // optional: if you want users to be able to edit the settings of your widget, you need to register a widget_control
119 wp_register_widget_control( $widget_id, $widget_control_title, $control_output_callback,
120 array(), // leave an empty array here: oddity in widget code
122 'widget_id' => $widget_id, // Yes - again. This is required: oddity in widget code
123 'arg' => an arg to pass to the $control_output_callback,
124 'another' => another arg to pass to the $control_output_callback,
130 // Hook to register new widgets
131 do_action( 'wp_dashboard_setup' );
133 // Hard code the sidebar's widgets and order
134 $dashboard_widgets = array();
135 $dashboard_widgets[] = 'dashboard_recent_comments';
136 $dashboard_widgets[] = 'dashboard_incoming_links';
137 $dashboard_widgets[] = 'dashboard_primary';
138 if ( current_user_can( 'activate_plugins' ) )
139 $dashboard_widgets[] = 'dashboard_plugins';
140 $dashboard_widgets[] = 'dashboard_secondary';
142 // Filter widget order
143 $dashboard_widgets = apply_filters( 'wp_dashboard_widgets', $dashboard_widgets );
145 $wp_dashboard_sidebars = array( 'wp_dashboard' => $dashboard_widgets, 'array_version' => 3.5 );
147 add_filter( 'dynamic_sidebar_params', 'wp_dashboard_dynamic_sidebar_params' );
149 if ( 'POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['widget_id']) ) {
150 ob_start(); // hack - but the same hack wp-admin/widgets.php uses
151 wp_dashboard_trigger_widget_control( $_POST['widget_id'] );
153 wp_redirect( remove_query_arg( 'edit' ) );
158 update_option( 'dashboard_widget_options', $widget_options );
161 // Echoes out the dashboard
162 function wp_dashboard() {
163 echo "<div id='dashboard-widgets'>\n\n";
165 // We're already filtering dynamic_sidebar_params obove
166 add_filter( 'option_sidebars_widgets', 'wp_dashboard_sidebars_widgets' ); // here there be hackery
167 dynamic_sidebar( 'wp_dashboard' );
168 remove_filter( 'option_sidebars_widgets', 'wp_dashboard_sidebars_widgets' );
170 echo "<br class='clear' />\n</div>\n\n\n";
173 // Makes sidebar_widgets option reflect the dashboard settings
174 function wp_dashboard_sidebars_widgets() { // hackery
175 return $GLOBALS['wp_dashboard_sidebars'];
178 // Modifies sidbar params on the fly to set up ids, class names, titles for each widget (called once per widget)
179 // Switches widget to edit mode if $_GET['edit']
180 function wp_dashboard_dynamic_sidebar_params( $params ) {
181 global $wp_registered_widgets, $wp_registered_widget_controls;
183 $sidebar_defaults = array('widget_id' => 0, 'before_widget' => '', 'after_widget' => '', 'before_title' => '', 'after_title' => '');
184 extract( $sidebar_defaults, EXTR_PREFIX_ALL, 'sidebar' );
185 extract( $params[0], EXTR_PREFIX_ALL, 'sidebar' );
187 if ( !isset($wp_registered_widgets[$sidebar_widget_id]) || !is_array($wp_registered_widgets[$sidebar_widget_id]) ) {
190 $widget_defaults = array('id' => '', 'width' => '', 'height' => '', 'class' => '', 'feed_link' => '', 'all_link' => '', 'notice' => false, 'error' => false);
191 extract( $widget_defaults, EXTR_PREFIX_ALL, 'widget' );
192 extract( $wp_registered_widgets[$sidebar_widget_id], EXTR_PREFIX_ALL, 'widget' );
194 $the_classes = array();
195 if ( in_array($widget_width, array( 'third', 'fourth', 'full' ) ) )
196 $the_classes[] = $widget_width;
198 if ( 'double' == $widget_height )
199 $the_classes[] = 'double';
202 $the_classes[] = $widget_class;
204 // Add classes to the widget holder
206 $sidebar_before_widget = str_replace( "<div class='dashboard-widget-holder ", "<div class='dashboard-widget-holder " . join( ' ', $the_classes ) . ' ', $sidebar_before_widget );
209 if ( $widget_all_link )
210 $links[] = '<a href="' . clean_url( $widget_all_link ) . '">' . __( 'See All' ) . '</a>';
212 $content_class = 'dashboard-widget-content';
213 if ( current_user_can( 'edit_dashboard' ) && isset($wp_registered_widget_controls[$widget_id]) && is_callable($wp_registered_widget_controls[$widget_id]['callback']) ) {
214 // Switch this widget to edit mode
215 if ( isset($_GET['edit']) && $_GET['edit'] == $widget_id ) {
216 $content_class .= ' dashboard-widget-control';
217 $wp_registered_widgets[$widget_id]['callback'] = 'wp_dashboard_empty';
218 $sidebar_widget_name = $wp_registered_widget_controls[$widget_id]['name'];
219 $params[1] = 'wp_dashboard_trigger_widget_control';
220 $sidebar_before_widget .= '<form action="' . clean_url(remove_query_arg( 'edit' )) . '" method="post">';
221 $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";
222 $links[] = '<a href="' . clean_url(remove_query_arg( 'edit' )) . '">' . __( 'Cancel' ) . '</a>';
224 $links[] = '<a href="' . clean_url(add_query_arg( 'edit', $widget_id )) . "#$widget_id" . '">' . __( 'Edit' ) . '</a>';
228 if ( $widget_feed_link )
229 $links[] = '<img class="rss-icon" src="' . includes_url('images/rss.png') . '" alt="' . __( 'rss icon' ) . '" /> <a href="' . clean_url( $widget_feed_link ) . '">' . __( 'RSS' ) . '</a>';
231 $links = apply_filters( "wp_dashboard_widget_links_$widget_id", $links );
233 // Add links to widget's title bar
235 $sidebar_before_title .= '<span>';
236 $sidebar_after_title = '</span><small>' . join( ' | ', $links ) . "</small><br class='clear' />$sidebar_after_title";
239 // Could have put this in widget-content. Doesn't really matter
240 if ( $widget_notice )
241 $sidebar_after_title .= "\t\t\t<div class='dashboard-widget-notice'>$widget_notice</div>\n\n";
244 $sidebar_after_title .= "\t\t\t<div class='dashboard-widget-error'>$widget_error</div>\n\n";
246 $sidebar_after_title .= "\t\t\t<div class='$content_class'>\n\n";
248 $sidebar_after_widget .= "\t\t\t</div>\n\n";
250 foreach( array_keys( $params[0] ) as $key )
251 $$key = ${'sidebar_' . $key};
253 $params[0] = compact( array_keys( $params[0] ) );
259 /* Dashboard Widgets */
261 function wp_dashboard_recent_comments( $sidebar_args ) {
263 extract( $sidebar_args, EXTR_SKIP );
271 $lambda = create_function( '', 'return 5;' );
272 add_filter( 'option_posts_per_rss', $lambda ); // hack - comments query doesn't accept per_page parameter
273 $comments_query = new WP_Query(array('feed' => 'rss2', 'withcomments' => 1));
274 remove_filter( 'option_posts_per_rss', $lambda );
278 if ( $comments_query->have_comments() ) {
279 while ( $comments_query->have_comments() ) { $comments_query->the_comment();
281 $comment_post_url = get_permalink( $comment->comment_post_ID );
282 $comment_post_title = get_the_title( $comment->comment_post_ID );
283 $comment_post_link = "<a href='$comment_post_url'>$comment_post_title</a>";
284 $comment_link = '<a class="comment-link" href="' . get_comment_link() . '">#</a>';
285 $comment_meta = sprintf( __( 'From <strong>%1$s</strong> on %2$s %3$s' ), get_comment_author(), $comment_post_link, $comment_link );
287 if ( $is_first ) : $is_first = false;
289 <blockquote><p>“<?php comment_excerpt(); ?>”</p></blockquote>
290 <p class='comment-meta'><?php echo $comment_meta; ?></p>
292 if ( $comments_query->comment_count > 1 ) : ?>
293 <ul id="dashboard-comments-list">
295 endif; // comment_count
299 <li class='comment-meta'><?php echo $comment_meta; ?></li>
304 if ( $comments_query->comment_count > 1 ) : ?>
307 endif; // comment_count;
314 // $sidebar_args are handled by wp_dashboard_empty()
315 function wp_dashboard_incoming_links_output() {
316 $widgets = get_option( 'dashboard_widget_options' );
317 @extract( @$widgets['dashboard_incoming_links'], EXTR_SKIP );
318 $rss = @fetch_rss( $url );
319 if ( isset($rss->items) && 0 < count($rss->items) ) {
323 $rss->items = array_slice($rss->items, 0, $items);
324 foreach ( $rss->items as $item ) {
330 $link = clean_url( strip_tags( $item['link'] ) );
332 if ( isset( $item['author_uri'] ) )
333 $site_link = clean_url( strip_tags( $item['author_uri'] ) );
335 if ( !$publisher = wp_specialchars( strip_tags( isset($item['dc']['publisher']) ? $item['dc']['publisher'] : $item['author_name'] ) ) )
336 $publisher = __( 'Somebody' );
338 $publisher = "<a href='$site_link'>$publisher</a>";
340 $publisher = "<strong>$publisher</strong>";
342 if ( isset($item['description']) )
343 $content = $item['description'];
344 elseif ( isset($item['summary']) )
345 $content = $item['summary'];
346 elseif ( isset($item['atom_content']) )
347 $content = $item['atom_content'];
349 $content = __( 'something' );
350 $content = wp_html_excerpt($content, 50) . ' ...';
352 $text = _c( '%1$s linked here <a href="%2$s">saying</a>, "%3$s"|feed_display' );
354 $text = _c( '%1$s linked here saying, "%3$s"|feed_display' );
357 if ( $show_author || $show_summary )
358 $text .= _c( ' on %4$s|feed_display' );
359 $date = wp_specialchars( strip_tags( isset($item['pubdate']) ? $item['pubdate'] : $item['published'] ) );
360 $date = strtotime( $date );
361 $date = gmdate( get_option( 'date_format' ), $date );
364 echo "\t<li>" . sprintf( _c( "$text|feed_display" ), $publisher, $link, $content, $date ) . "</li>\n";
370 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";
374 // $sidebar_args are handled by wp_dashboard_empty()
375 function wp_dashboard_rss_output( $widget_id ) {
376 $widgets = get_option( 'dashboard_widget_options' );
377 wp_widget_rss_output( $widgets[$widget_id] );
380 // $sidebar_args are handled by wp_dashboard_empty()
381 function wp_dashboard_secondary_output() {
382 $widgets = get_option( 'dashboard_widget_options' );
383 @extract( @$widgets['dashboard_secondary'], EXTR_SKIP );
384 $rss = @fetch_rss( $url );
385 if ( !isset($rss->items) || 0 == count($rss->items) )
388 echo "<ul id='planetnews'>\n";
390 $rss->items = array_slice($rss->items, 0, $items);
391 foreach ($rss->items as $item ) {
392 $title = wp_specialchars($item['title']);
393 list($author,$post) = explode( ':', $title, 2 );
394 $link = clean_url($item['link']);
396 echo "\t<li><a href='$link'><span class='post'>$post</span><span class='hidden'> - </span><cite>$author</cite></a></li>\n";
399 echo "</ul>\n<br class='clear' />\n";
402 // $sidebar_args are handled by wp_dashboard_empty()
403 function wp_dashboard_plugins_output() {
404 $popular = @fetch_rss( 'http://wordpress.org/extend/plugins/rss/browse/popular/' );
405 $new = @fetch_rss( 'http://wordpress.org/extend/plugins/rss/browse/new/' );
406 $updated = @fetch_rss( 'http://wordpress.org/extend/plugins/rss/browse/updated/' );
408 foreach ( array( 'popular' => __('Most Popular'), 'new' => __('Newest Plugins'), 'updated' => __('Recently Updated') ) as $feed => $label ) {
409 if ( !isset($$feed->items) || 0 == count($$feed->items) )
412 $$feed->items = array_slice($$feed->items, 0, 5);
413 $item_key = array_rand($$feed->items);
415 // Eliminate some common badly formed plugin descriptions
416 while ( ( null !== $item_key = array_rand($$feed->items) ) && false !== strpos( $$feed->items[$item_key]['description'], 'Plugin Name:' ) )
417 unset($$feed->items[$item_key]);
419 if ( !isset($$feed->items[$item_key]) )
422 $item = $$feed->items[$item_key];
424 // current bbPress feed item titles are: user on "topic title"
425 if ( preg_match( '/"(.*)"/s', $item['title'], $matches ) )
426 $title = $matches[1];
427 else // but let's make it forward compatible if things change
428 $title = $item['title'];
429 $title = wp_specialchars( $title );
431 $description = wp_specialchars( strip_tags(html_entity_decode($item['description'], ENT_QUOTES)) );
433 list($link, $frag) = explode( '#', $item['link'] );
435 $link = clean_url($link);
436 $dlink = rtrim($link, '/') . '/download/';
438 echo "<h4>$label</h4>\n";
439 echo "<h5><a href='$link'>$title</a></h5> <span>(<a href='$dlink'>" . __( 'Download' ) . "</a>)</span>\n";
440 echo "<p>$description</p>\n";
444 // Checks to see if all of the feed url in $check_urls are cached.
445 // If $check_urls is empty, look for the rss feed url found in the dashboard widget optios of $widget_id.
446 // If cached, call $callback, a function that echoes out output for this widget.
447 // If not cache, echo a "Loading..." stub which is later replaced by AJAX call (see top of /wp-admin/index.php)
448 function wp_dashboard_cached_rss_widget( $widget_id, $callback, $check_urls = array() ) {
449 $loading = '<p class="widget-loading">' . __( 'Loading…' ) . '</p>';
451 if ( empty($check_urls) ) {
452 $widgets = get_option( 'dashboard_widget_options' );
453 if ( empty($widgets[$widget_id]['url']) ) {
457 $check_urls = array( $widgets[$widget_id]['url'] );
461 require_once( ABSPATH . WPINC . '/rss.php' );
462 init(); // initialize rss constants
464 $cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE );
466 foreach ( $check_urls as $check_url ) {
467 $status = $cache->check_cache( $check_url );
468 if ( 'HIT' !== $status ) {
474 if ( $callback && is_callable( $callback ) ) {
475 $args = array_slice( func_get_args(), 2 );
476 array_unshift( $args, $widget_id );
477 call_user_func_array( $callback, $args );
483 // Empty widget used for JS/AJAX created output.
484 // Callback inserts content between before_widget and after_widget. Used when widget is in edit mode. Can also be used for custom widgets.
485 function wp_dashboard_empty( $sidebar_args, $callback = false ) {
486 extract( $sidebar_args, EXTR_SKIP );
494 // When in edit mode, the callback passed to this function is the widget_control callback
495 if ( $callback && is_callable( $callback ) ) {
496 $args = array_slice( func_get_args(), 2 );
497 array_unshift( $args, $widget_id );
498 call_user_func_array( $callback, $args );
504 /* Dashboard Widgets Controls. Ssee also wp_dashboard_empty() */
506 // Calls widget_control callback
507 function wp_dashboard_trigger_widget_control( $widget_control_id = false ) {
508 global $wp_registered_widget_controls;
509 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']) )
510 call_user_func_array( $wp_registered_widget_controls[$widget_control_id]['callback'], $wp_registered_widget_controls[$widget_control_id]['params'] );
513 // Sets up $args to be used as input to wp_widget_rss_form(), handles POST data from RSS-type widgets
514 function wp_dashboard_rss_control( $args ) {
519 if ( !$widget_options = get_option( 'dashboard_widget_options' ) )
520 $widget_options = array();
522 if ( !isset($widget_options[$widget_id]) )
523 $widget_options[$widget_id] = array();
525 $number = 1; // Hack to use wp_widget_rss_form()
526 $widget_options[$widget_id]['number'] = $number;
528 if ( 'POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['widget-rss'][$number]) ) {
529 $_POST['widget-rss'][$number] = stripslashes_deep( $_POST['widget-rss'][$number] );
530 $widget_options[$widget_id] = wp_widget_rss_process( $_POST['widget-rss'][$number] );
531 // title is optional. If black, fill it if possible
532 if ( !$widget_options[$widget_id]['title'] && isset($_POST['widget-rss'][$number]['title']) ) {
533 require_once(ABSPATH . WPINC . '/rss.php');
534 $rss = fetch_rss($widget_options[$widget_id]['url']);
535 $widget_options[$widget_id]['title'] = htmlentities(strip_tags($rss->channel['title']));
537 update_option( 'dashboard_widget_options', $widget_options );
540 wp_widget_rss_form( $widget_options[$widget_id], $form_inputs );