]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/dashboard.php
Wordpress 2.8-scripts
[autoinstalls/wordpress.git] / wp-admin / includes / dashboard.php
1 <?php
2 /**
3  * WordPress Dashboard Widget Administration Panel API
4  *
5  * @package WordPress
6  * @subpackage Administration
7  */
8
9 /**
10  * Registers dashboard widgets.
11  *
12  * handles POST data, sets up filters.
13  *
14  * @since unknown
15  */
16 function wp_dashboard_setup() {
17         global $wp_registered_widgets, $wp_registered_widget_controls, $wp_dashboard_control_callbacks;
18         $wp_dashboard_control_callbacks = array();
19
20         $update = false;
21         $widget_options = get_option( 'dashboard_widget_options' );
22         if ( !$widget_options || !is_array($widget_options) )
23                 $widget_options = array();
24
25         /* Register Widgets and Controls */
26
27         // Right Now
28         wp_add_dashboard_widget( 'dashboard_right_now', __( 'Right Now' ), 'wp_dashboard_right_now' );
29
30         // Recent Comments Widget
31         $recent_comments_title = __( 'Recent Comments' );
32         wp_add_dashboard_widget( 'dashboard_recent_comments', $recent_comments_title, 'wp_dashboard_recent_comments' );
33
34         // Incoming Links Widget
35         if ( !isset( $widget_options['dashboard_incoming_links'] ) || !isset( $widget_options['dashboard_incoming_links']['home'] ) || $widget_options['dashboard_incoming_links']['home'] != get_option('home') ) {
36                 $update = true;
37                 $widget_options['dashboard_incoming_links'] = array(
38                         'home' => get_option('home'),
39                         'link' => apply_filters( 'dashboard_incoming_links_link', 'http://blogsearch.google.com/blogsearch?hl=en&scoring=d&partner=wordpress&q=link:' . trailingslashit( get_option('home') ) ),
40                         'url' => isset($widget_options['dashboard_incoming_links']['url']) ? apply_filters( 'dashboard_incoming_links_feed', $widget_options['dashboard_incoming_links']['url'] ) : apply_filters( 'dashboard_incoming_links_feed', 'http://blogsearch.google.com/blogsearch_feeds?hl=en&scoring=d&ie=utf-8&num=20&output=rss&partner=wordpress&q=link:' . trailingslashit( get_option('home') ) ),
41                         'items' => isset($widget_options['dashboard_incoming_links']['items']) ? $widget_options['dashboard_incoming_links']['items'] : 10,
42                         'show_date' => isset($widget_options['dashboard_incoming_links']['show_date']) ? $widget_options['dashboard_incoming_links']['show_date'] : false
43                 );
44         }
45         wp_add_dashboard_widget( 'dashboard_incoming_links', __( 'Incoming Links' ), 'wp_dashboard_incoming_links', 'wp_dashboard_incoming_links_control' );
46
47         // WP Plugins Widget
48         if ( current_user_can( 'activate_plugins' ) )
49                 wp_add_dashboard_widget( 'dashboard_plugins', __( 'Plugins' ), 'wp_dashboard_plugins' );
50
51         // QuickPress Widget
52         if ( current_user_can('edit_posts') )
53                 wp_add_dashboard_widget( 'dashboard_quick_press', __( 'QuickPress' ), 'wp_dashboard_quick_press' );
54
55         // Recent Drafts
56         if ( current_user_can('edit_posts') )
57                 wp_add_dashboard_widget( 'dashboard_recent_drafts', __('Recent Drafts'), 'wp_dashboard_recent_drafts' );
58
59         // Primary feed (Dev Blog) Widget
60         if ( !isset( $widget_options['dashboard_primary'] ) ) {
61                 $update = true;
62                 $widget_options['dashboard_primary'] = array(
63                         'link' => apply_filters( 'dashboard_primary_link',  __( 'http://wordpress.org/development/' ) ),
64                         'url' => apply_filters( 'dashboard_primary_feed',  __( 'http://wordpress.org/development/feed/' ) ),
65                         'title' => apply_filters( 'dashboard_primary_title', __( 'WordPress Development Blog' ) ),
66                         'items' => 2,
67                         'show_summary' => 1,
68                         'show_author' => 0,
69                         'show_date' => 1
70                 );
71         }
72         wp_add_dashboard_widget( 'dashboard_primary', $widget_options['dashboard_primary']['title'], 'wp_dashboard_primary', 'wp_dashboard_primary_control' );
73
74         // Secondary Feed (Planet) Widget
75         if ( !isset( $widget_options['dashboard_secondary'] ) ) {
76                 $update = true;
77                 $widget_options['dashboard_secondary'] = array(
78                         'link' => apply_filters( 'dashboard_secondary_link',  __( 'http://planet.wordpress.org/' ) ),
79                         'url' => apply_filters( 'dashboard_secondary_feed',  __( 'http://planet.wordpress.org/feed/' ) ),
80                         'title' => apply_filters( 'dashboard_secondary_title', __( 'Other WordPress News' ) ),
81                         'items' => 5
82                 );
83         }
84         wp_add_dashboard_widget( 'dashboard_secondary', $widget_options['dashboard_secondary']['title'], 'wp_dashboard_secondary', 'wp_dashboard_secondary_control' );
85
86         // Hook to register new widgets
87         do_action( 'wp_dashboard_setup' );
88
89         // Filter widget order
90         $dashboard_widgets = apply_filters( 'wp_dashboard_widgets', array() );
91
92         foreach ( $dashboard_widgets as $widget_id ) {
93                 $name = empty( $wp_registered_widgets[$widget_id]['all_link'] ) ? $wp_registered_widgets[$widget_id]['name'] : $wp_registered_widgets[$widget_id]['name'] . " <a href='{$wp_registered_widgets[$widget_id]['all_link']}' class='edit-box open-box'>" . __('View all') . '</a>';
94                 wp_add_dashboard_widget( $widget_id, $name, $wp_registered_widgets[$widget_id]['callback'], $wp_registered_widget_controls[$widget_id]['callback'] );
95         }
96
97         if ( 'POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['widget_id']) ) {
98                 ob_start(); // hack - but the same hack wp-admin/widgets.php uses
99                 wp_dashboard_trigger_widget_control( $_POST['widget_id'] );
100                 ob_end_clean();
101                 wp_redirect( remove_query_arg( 'edit' ) );
102                 exit;
103         }
104
105         if ( $update )
106                 update_option( 'dashboard_widget_options', $widget_options );
107
108         do_action('do_meta_boxes', 'dashboard', 'normal', '');
109         do_action('do_meta_boxes', 'dashboard', 'side', '');
110 }
111
112 function wp_add_dashboard_widget( $widget_id, $widget_name, $callback, $control_callback = null ) {
113         global $wp_dashboard_control_callbacks;
114         if ( $control_callback && current_user_can( 'edit_dashboard' ) && is_callable( $control_callback ) ) {
115                 $wp_dashboard_control_callbacks[$widget_id] = $control_callback;
116                 if ( isset( $_GET['edit'] ) && $widget_id == $_GET['edit'] ) {
117                         list($url) = explode( '#', add_query_arg( 'edit', false ), 2 );
118                         $widget_name .= ' <span class="postbox-title-action"><a href="' . esc_url( $url ) . '">' . __( 'Cancel' ) . '</a></span>';
119                         add_meta_box( $widget_id, $widget_name, '_wp_dashboard_control_callback', 'dashboard', 'normal', 'core' );
120                         return;
121                 }
122                 list($url) = explode( '#', add_query_arg( 'edit', $widget_id ), 2 );
123                 $widget_name .= ' <span class="postbox-title-action"><a href="' . esc_url( "$url#$widget_id" ) . '" class="edit-box open-box">' . __( 'Configure' ) . '</a></span>';
124         }
125         $side_widgets = array('dashboard_quick_press', 'dashboard_recent_drafts', 'dashboard_primary', 'dashboard_secondary');
126         $location = 'normal';
127         if ( in_array($widget_id, $side_widgets) )
128                 $location = 'side';
129         add_meta_box( $widget_id, $widget_name , $callback, 'dashboard', $location, 'core' );
130 }
131
132 function _wp_dashboard_control_callback( $dashboard, $meta_box ) {
133         echo '<form action="" method="post" class="dashboard-widget-control-form">';
134         wp_dashboard_trigger_widget_control( $meta_box['id'] );
135         echo "<p class='submit'><input type='hidden' name='widget_id' value='" . esc_attr($meta_box['id']) . "' /><input type='submit' value='" . esc_attr__( 'Submit' ) . "' /></p>";
136
137         echo '</form>';
138 }
139
140 /**
141  * Displays the dashboard.
142  *
143  * @since unknown
144  */
145 function wp_dashboard() {
146         global $screen_layout_columns;
147
148         $hide2 = $hide3 = $hide4 = '';
149         switch ( $screen_layout_columns ) {
150                 case 4:
151                         $width = 'width:24.5%;';
152                         break;
153                 case 3:
154                         $width = 'width:32.67%;';
155                         $hide4 = 'display:none;';
156                         break;
157                 case 2:
158                         $width = 'width:49%;';
159                         $hide3 = $hide4 = 'display:none;';
160                         break;
161                 default:
162                         $width = 'width:98%;';
163                         $hide2 = $hide3 = $hide4 = 'display:none;';
164         }
165 ?>
166 <div id='dashboard-widgets' class='metabox-holder'>
167 <?php
168         echo "\t<div class='postbox-container' style='$width'>\n";
169         do_meta_boxes( 'dashboard', 'normal', '' );
170
171         echo "\t</div><div class='postbox-container' style='{$hide2}$width'>\n";
172         do_meta_boxes( 'dashboard', 'side', '' );
173
174         echo "\t</div><div class='postbox-container' style='{$hide3}$width'>\n";
175         do_meta_boxes( 'dashboard', 'column3', '' );
176
177         echo "\t</div><div class='postbox-container' style='{$hide4}$width'>\n";
178         do_meta_boxes( 'dashboard', 'column4', '' );
179 ?>
180 </div></div>
181
182 <form style='display: none' method='get' action=''>
183         <p>
184 <?php
185         wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
186         wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
187 ?>
188         </p>
189 </form>
190
191 <?php
192 }
193
194 /* Dashboard Widgets */
195
196 function wp_dashboard_right_now() {
197         global $wp_registered_sidebars;
198
199         $num_posts = wp_count_posts( 'post' );
200         $num_pages = wp_count_posts( 'page' );
201
202         $num_cats  = wp_count_terms('category');
203
204         $num_tags = wp_count_terms('post_tag');
205
206         $num_comm = wp_count_comments( );
207
208         echo "\n\t".'<p class="sub">' . __('At a Glance') . '</p>';
209         echo "\n\t".'<div class="table">'."\n\t".'<table>';
210         echo "\n\t".'<tr class="first">';
211
212         // Posts
213         $num = number_format_i18n( $num_posts->publish );
214         $text = _n( 'Post', 'Posts', intval($num_posts->publish) );
215         if ( current_user_can( 'edit_posts' ) ) {
216                 $num = "<a href='edit.php'>$num</a>";
217                 $text = "<a href='edit.php'>$text</a>";
218         }
219         echo '<td class="first b b-posts">' . $num . '</td>';
220         echo '<td class="t posts">' . $text . '</td>';
221         /* TODO: Show status breakdown on hover
222         if ( $can_edit_pages && !empty($num_pages->publish) ) { // how many pages is not exposed in feeds.  Don't show if !current_user_can
223                 $post_type_texts[] = '<a href="edit-pages.php">'.sprintf( _n( '%s page', '%s pages', $num_pages->publish ), number_format_i18n( $num_pages->publish ) ).'</a>';
224         }
225         if ( $can_edit_posts && !empty($num_posts->draft) ) {
226                 $post_type_texts[] = '<a href="edit.php?post_status=draft">'.sprintf( _n( '%s draft', '%s drafts', $num_posts->draft ), number_format_i18n( $num_posts->draft ) ).'</a>';
227         }
228         if ( $can_edit_posts && !empty($num_posts->future) ) {
229                 $post_type_texts[] = '<a href="edit.php?post_status=future">'.sprintf( _n( '%s scheduled post', '%s scheduled posts', $num_posts->future ), number_format_i18n( $num_posts->future ) ).'</a>';
230         }
231         if ( current_user_can('publish_posts') && !empty($num_posts->pending) ) {
232                 $pending_text = sprintf( _n( 'There is <a href="%1$s">%2$s post</a> pending your review.', 'There are <a href="%1$s">%2$s posts</a> pending your review.', $num_posts->pending ), 'edit.php?post_status=pending', number_format_i18n( $num_posts->pending ) );
233         } else {
234                 $pending_text = '';
235         }
236         */
237
238         // Total Comments
239         $num = number_format_i18n($num_comm->total_comments);
240         $text = _n( 'Comment', 'Comments', $num_comm->total_comments );
241         if ( current_user_can( 'moderate_comments' ) ) {
242                 $num = "<a href='edit-comments.php'>$num</a>";
243                 $text = "<a href='edit-comments.php'>$text</a>";
244         }
245         echo '<td class="b b-comments">' . $num . '</td>';
246         echo '<td class="last t comments">' . $text . '</td>';
247
248         echo '</tr><tr>';
249
250         // Pages
251         $num = number_format_i18n( $num_pages->publish );
252         $text = _n( 'Page', 'Pages', $num_pages->publish );
253         if ( current_user_can( 'edit_pages' ) ) {
254                 $num = "<a href='edit-pages.php'>$num</a>";
255                 $text = "<a href='edit-pages.php'>$text</a>";
256         }
257         echo '<td class="first b b_pages">' . $num . '</td>';
258         echo '<td class="t pages">' . $text . '</td>';
259
260         // Approved Comments
261         $num = number_format_i18n($num_comm->approved);
262         $text = _nc( 'Approved|Right Now', 'Approved', $num_comm->approved );
263         if ( current_user_can( 'moderate_comments' ) ) {
264                 $num = "<a href='edit-comments.php?comment_status=approved'>$num</a>";
265                 $text = "<a class='approved' href='edit-comments.php?comment_status=approved'>$text</a>";
266         }
267         echo '<td class="b b_approved">' . $num . '</td>';
268         echo '<td class="last t">' . $text . '</td>';
269
270         echo "</tr>\n\t<tr>";
271
272         // Categories
273         $num = number_format_i18n( $num_cats );
274         $text = _n( 'Category', 'Categories', $num_cats );
275         if ( current_user_can( 'manage_categories' ) ) {
276                 $num = "<a href='categories.php'>$num</a>";
277                 $text = "<a href='categories.php'>$text</a>";
278         }
279         echo '<td class="first b b-cats">' . $num . '</td>';
280         echo '<td class="t cats">' . $text . '</td>';
281
282         // Pending Comments
283         $num = number_format_i18n($num_comm->moderated);
284         $text = _n( 'Pending', 'Pending', $num_comm->moderated );
285         if ( current_user_can( 'moderate_comments' ) ) {
286                 $num = "<a href='edit-comments.php?comment_status=moderated'><span class='pending-count'>$num</span></a>";
287                 $text = "<a class='waiting' href='edit-comments.php?comment_status=moderated'>$text</a>";
288         }
289         echo '<td class="b b-waiting">' . $num . '</td>';
290         echo '<td class="last t">' . $text . '</td>';
291
292         echo "</tr>\n\t<tr>";
293
294         // Tags
295         $num = number_format_i18n( $num_tags );
296         $text = _n( 'Tag', 'Tags', $num_tags );
297         if ( current_user_can( 'manage_categories' ) ) {
298                 $num = "<a href='edit-tags.php'>$num</a>";
299                 $text = "<a href='edit-tags.php'>$text</a>";
300         }
301         echo '<td class="first b b-tags">' . $num . '</td>';
302         echo '<td class="t tags">' . $text . '</td>';
303
304         // Spam Comments
305         $num = number_format_i18n($num_comm->spam);
306         $text = _n( 'Spam', 'Spam', $num_comm->spam );
307         if ( current_user_can( 'moderate_comments' ) ) {
308                 $num = "<a href='edit-comments.php?comment_status=spam'><span class='spam-count'>$num</span></a>";
309                 $text = "<a class='spam' href='edit-comments.php?comment_status=spam'>$text</a>";
310         }
311         echo '<td class="b b-spam">' . $num . '</td>';
312         echo '<td class="last t">' . $text . '</td>';
313
314         echo "</tr>";
315         do_action('right_now_table_end');
316         echo "\n\t</table>\n\t</div>";
317
318         echo "\n\t".'<div class="versions">';
319         $ct = current_theme_info();
320
321         echo "\n\t<p>";
322         if ( !empty($wp_registered_sidebars) ) {
323                 $sidebars_widgets = wp_get_sidebars_widgets();
324                 $num_widgets = 0;
325                 foreach ( (array) $sidebars_widgets as $k => $v ) {
326                         if ( 'wp_inactive_widgets' == $k )
327                                 continue;
328                         if ( is_array($v) )
329                                 $num_widgets = $num_widgets + count($v);
330                 }
331                 $num = number_format_i18n( $num_widgets );
332
333                 if ( current_user_can( 'switch_themes' ) ) {
334                         echo '<a href="themes.php" class="button rbutton">' . __('Change Theme') . '</a>';
335                         printf(_n('Theme <span class="b"><a href="themes.php">%1$s</a></span> with <span class="b"><a href="widgets.php">%2$s Widget</a></span>', 'Theme <span class="b"><a href="themes.php">%1$s</a></span> with <span class="b"><a href="widgets.php">%2$s Widgets</a></span>', $num_widgets), $ct->title, $num);
336                 } else {
337                         printf(_n('Theme <span class="b">%1$s</span> with <span class="b">%2$s Widget</span>', 'Theme <span class="b">%1$s</span> with <span class="b">%2$s Widgets</span>', $num_widgets), $ct->title, $num);
338                 }
339         } else {
340                 if ( current_user_can( 'switch_themes' ) ) {
341                         echo '<a href="themes.php" class="button rbutton">' . __('Change Theme') . '</a>';
342                         printf('Theme <span class="b"><a href="themes.php">%1$s</a></span>', $ct->title);
343                 } else {
344                         printf('Theme <span class="b">%1$s</span>', $ct->title);
345                 }
346         }
347         echo '</p>';
348
349         update_right_now_message();
350
351         echo "\n\t".'<br class="clear" /></div>';
352         do_action( 'rightnow_end' );
353         do_action( 'activity_box_end' );
354 }
355
356 function wp_dashboard_quick_press() {
357         $drafts = false;
358         if ( 'post' === strtolower( $_SERVER['REQUEST_METHOD'] ) && isset( $_POST['action'] ) && 0 === strpos( $_POST['action'], 'post-quickpress' ) && (int) $_POST['post_ID'] ) {
359                 $view = get_permalink( $_POST['post_ID'] );
360                 $edit = esc_url( get_edit_post_link( $_POST['post_ID'] ) );
361                 if ( 'post-quickpress-publish' == $_POST['action'] ) {
362                         if ( current_user_can('publish_posts') )
363                                 printf( '<div class="message"><p>' . __( 'Post Published. <a href="%s">View post</a> | <a href="%s">Edit post</a>' ) . '</p></div>', esc_url( $view ), $edit );
364                         else
365                                 printf( '<div class="message"><p>' . __( 'Post submitted. <a href="%s">Preview post</a> | <a href="%s">Edit post</a>' ) . '</p></div>', esc_url( add_query_arg( 'preview', 1, $view ) ), $edit );
366                 } else {
367                         printf( '<div class="message"><p>' . __( 'Draft Saved. <a href="%s">Preview post</a> | <a href="%s">Edit post</a>' ) . '</p></div>', esc_url( add_query_arg( 'preview', 1, $view ) ), $edit );
368                         $drafts_query = new WP_Query( array(
369                                 'post_type' => 'post',
370                                 'post_status' => 'draft',
371                                 'author' => $GLOBALS['current_user']->ID,
372                                 'posts_per_page' => 1,
373                                 'orderby' => 'modified',
374                                 'order' => 'DESC'
375                         ) );
376
377                         if ( $drafts_query->posts )
378                                 $drafts =& $drafts_query->posts;
379                 }
380                 printf('<p class="textright">' . __('You can also try %s, easy blogging from anywhere on the Web.') . '</p>', '<a href="tools.php">' . __('Press This') . '</a>' );
381                 $_REQUEST = array(); // hack for get_default_post_to_edit()
382         }
383
384         $post = get_default_post_to_edit();
385 ?>
386
387         <form name="post" action="<?php echo esc_url( admin_url( 'post.php' ) ); ?>" method="post" id="quick-press">
388                 <h4 id="quick-post-title"><label for="title"><?php _e('Title') ?></label></h4>
389                 <div class="input-text-wrap">
390                         <input type="text" name="post_title" id="title" tabindex="1" autocomplete="off" value="<?php echo esc_attr( $post->post_title ); ?>" />
391                 </div>
392
393                 <?php if ( current_user_can( 'upload_files' ) ) : ?>
394                 <div id="media-buttons" class="hide-if-no-js">
395                         <?php do_action( 'media_buttons' ); ?>
396                 </div>
397                 <?php endif; ?>
398
399                 <h4 id="content-label"><label for="content"><?php _e('Content') ?></label></h4>
400                 <div class="textarea-wrap">
401                         <textarea name="content" id="content" class="mceEditor" rows="3" cols="15" tabindex="2"><?php echo $post->post_content; ?></textarea>
402                 </div>
403
404                 <script type="text/javascript">edCanvas = document.getElementById('content');edInsertContent = null;</script>
405
406                 <h4><label for="tags-input"><?php _e('Tags') ?></label></h4>
407                 <div class="input-text-wrap">
408                         <input type="text" name="tags_input" id="tags-input" tabindex="3" value="<?php echo get_tags_to_edit( $post->ID ); ?>" />
409                 </div>
410
411                 <p class="submit">
412                         <input type="hidden" name="action" id="quickpost-action" value="post-quickpress-save" />
413                         <input type="hidden" name="quickpress_post_ID" value="<?php echo (int) $post->ID; ?>" />
414                         <?php wp_nonce_field('add-post'); ?>
415                         <input type="submit" name="save" id="save-post" class="button" tabindex="4" value="<?php esc_attr_e('Save Draft'); ?>" />
416                         <input type="reset" value="<?php esc_attr_e( 'Reset' ); ?>" class="button" />
417                         <?php if ( current_user_can('publish_posts') ) { ?>
418                         <input type="submit" name="publish" id="publish" accesskey="p" tabindex="5" class="button-primary" value="<?php esc_attr_e('Publish'); ?>" />
419                         <?php } else { ?>
420                         <input type="submit" name="publish" id="publish" accesskey="p" tabindex="5" class="button-primary" value="<?php esc_attr_e('Submit for Review'); ?>" />
421                         <?php } ?>
422                         <br class="clear" />
423                 </p>
424
425         </form>
426
427 <?php
428         if ( $drafts )
429                 wp_dashboard_recent_drafts( $drafts );
430 }
431
432 function wp_dashboard_recent_drafts( $drafts = false ) {
433         if ( !$drafts ) {
434                 $drafts_query = new WP_Query( array(
435                         'post_type' => 'post',
436                         'post_status' => 'draft',
437                         'author' => $GLOBALS['current_user']->ID,
438                         'posts_per_page' => 5,
439                         'orderby' => 'modified',
440                         'order' => 'DESC'
441                 ) );
442                 $drafts =& $drafts_query->posts;
443         }
444
445         if ( $drafts && is_array( $drafts ) ) {
446                 $list = array();
447                 foreach ( $drafts as $draft ) {
448                         $url = get_edit_post_link( $draft->ID );
449                         $title = _draft_or_post_title( $draft->ID );
450                         $item = "<h4><a href='$url' title='" . sprintf( __( 'Edit &#8220;%s&#8221;' ), esc_attr( $title ) ) . "'>$title</a> <abbr title='" . get_the_time(__('Y/m/d g:i:s A'), $draft) . "'>" . get_the_time( get_option( 'date_format' ), $draft ) . '</abbr></h4>';
451                         if ( $the_content = preg_split( '#\s#', strip_tags( $draft->post_content ), 11, PREG_SPLIT_NO_EMPTY ) )
452                                 $item .= '<p>' . join( ' ', array_slice( $the_content, 0, 10 ) ) . ( 10 < count( $the_content ) ? '&hellip;' : '' ) . '</p>';
453                         $list[] = $item;
454                 }
455 ?>
456         <ul>
457                 <li><?php echo join( "</li>\n<li>", $list ); ?></li>
458         </ul>
459         <p class="textright"><a href="edit.php?post_status=draft" class="button"><?php _e('View all'); ?></a></p>
460 <?php
461         } else {
462                 _e('There are no drafts at the moment');
463         }
464 }
465
466 /**
467  * Display recent comments dashboard widget content.
468  *
469  * @since unknown
470  */
471 function wp_dashboard_recent_comments() {
472         global $wpdb;
473
474         if ( current_user_can('edit_posts') )
475                 $allowed_states = array('0', '1');
476         else
477                 $allowed_states = array('1');
478
479         // Select all comment types and filter out spam later for better query performance.
480         $comments = array();
481         $start = 0;
482
483         while ( count( $comments ) < 5 && $possible = $wpdb->get_results( "SELECT * FROM $wpdb->comments ORDER BY comment_date_gmt DESC LIMIT $start, 50" ) ) {
484
485                 foreach ( $possible as $comment ) {
486                         if ( count( $comments ) >= 5 )
487                                 break;
488                         if ( in_array( $comment->comment_approved, $allowed_states ) )
489                                 $comments[] = $comment;
490                 }
491
492                 $start = $start + 50;
493         }
494
495         if ( $comments ) :
496 ?>
497
498                 <div id="the-comment-list" class="list:comment">
499 <?php
500                 foreach ( $comments as $comment )
501                         _wp_dashboard_recent_comments_row( $comment );
502 ?>
503
504                 </div>
505
506 <?php
507                 if ( current_user_can('edit_posts') ) { ?>
508                         <p class="textright"><a href="edit-comments.php" class="button"><?php _e('View all'); ?></a></p>
509 <?php   }
510
511                 wp_comment_reply( -1, false, 'dashboard', false );
512
513         else :
514 ?>
515
516         <p><?php _e( 'No comments yet.' ); ?></p>
517
518 <?php
519         endif; // $comments;
520 }
521
522 function _wp_dashboard_recent_comments_row( &$comment, $show_date = true ) {
523         $GLOBALS['comment'] =& $comment;
524
525         $comment_post_url = get_edit_post_link( $comment->comment_post_ID );
526         $comment_post_title = strip_tags(get_the_title( $comment->comment_post_ID ));
527         $comment_post_link = "<a href='$comment_post_url'>$comment_post_title</a>";
528         $comment_link = '<a class="comment-link" href="' . esc_url(get_comment_link()) . '">#</a>';
529
530         $delete_url = esc_url( wp_nonce_url( "comment.php?action=deletecomment&p=$comment->comment_post_ID&c=$comment->comment_ID", "delete-comment_$comment->comment_ID" ) );
531         $approve_url = esc_url( wp_nonce_url( "comment.php?action=approvecomment&p=$comment->comment_post_ID&c=$comment->comment_ID", "approve-comment_$comment->comment_ID" ) );
532         $unapprove_url = esc_url( wp_nonce_url( "comment.php?action=unapprovecomment&p=$comment->comment_post_ID&c=$comment->comment_ID", "unapprove-comment_$comment->comment_ID" ) );
533         $spam_url = esc_url( wp_nonce_url( "comment.php?action=deletecomment&dt=spam&p=$comment->comment_post_ID&c=$comment->comment_ID", "delete-comment_$comment->comment_ID" ) );
534
535         $actions = array();
536
537         $actions_string = '';
538         if ( current_user_can('edit_post', $comment->comment_post_ID) ) {
539                 $actions['approve'] = "<a href='$approve_url' class='dim:the-comment-list:comment-$comment->comment_ID:unapproved:e7e7d3:e7e7d3:new=approved vim-a' title='" . __( 'Approve this comment' ) . "'>" . __( 'Approve' ) . '</a>';
540                 $actions['unapprove'] = "<a href='$unapprove_url' class='dim:the-comment-list:comment-$comment->comment_ID:unapproved:e7e7d3:e7e7d3:new=unapproved vim-u' title='" . __( 'Unapprove this comment' ) . "'>" . __( 'Unapprove' ) . '</a>';
541                 $actions['edit'] = "<a href='comment.php?action=editcomment&amp;c={$comment->comment_ID}' title='" . __('Edit comment') . "'>". __('Edit') . '</a>';
542                 //$actions['quickedit'] = '<a onclick="commentReply.open(\''.$comment->comment_ID.'\',\''.$comment->comment_post_ID.'\',\'edit\');return false;" class="vim-q" title="'.__('Quick Edit').'" href="#">' . __('Quick&nbsp;Edit') . '</a>';
543                 $actions['reply'] = '<a onclick="commentReply.open(\''.$comment->comment_ID.'\',\''.$comment->comment_post_ID.'\');return false;" class="vim-r hide-if-no-js" title="'.__('Reply to this comment').'" href="#">' . __('Reply') . '</a>';
544                 $actions['spam'] = "<a href='$spam_url' class='delete:the-comment-list:comment-$comment->comment_ID::spam=1 vim-s vim-destructive' title='" . __( 'Mark this comment as spam' ) . "'>" . /* translators: mark as spam link */  _x( 'Spam', 'verb' ) . '</a>';
545                 $actions['delete'] = "<a href='$delete_url' class='delete:the-comment-list:comment-$comment->comment_ID delete vim-d vim-destructive'>" . __('Delete') . '</a>';
546
547                 $actions = apply_filters( 'comment_row_actions', $actions, $comment );
548
549                 $i = 0;
550                 foreach ( $actions as $action => $link ) {
551                         ++$i;
552                         ( ( ('approve' == $action || 'unapprove' == $action) && 2 === $i ) || 1 === $i ) ? $sep = '' : $sep = ' | ';
553
554                         // Reply and quickedit need a hide-if-no-js span
555                         if ( 'reply' == $action || 'quickedit' == $action )
556                                 $action .= ' hide-if-no-js';
557
558                         $actions_string .= "<span class='$action'>$sep$link</span>";
559                 }
560         }
561
562 ?>
563
564                 <div id="comment-<?php echo $comment->comment_ID; ?>" <?php comment_class( array( 'comment-item', wp_get_comment_status($comment->comment_ID) ) ); ?>>
565                         <?php if ( !$comment->comment_type || 'comment' == $comment->comment_type ) : ?>
566
567                         <?php echo get_avatar( $comment, 50 ); ?>
568
569                         <div class="dashboard-comment-wrap">
570                         <h4 class="comment-meta"><?php printf( __( 'From %1$s on %2$s%3$s' ), '<cite class="comment-author">' . get_comment_author_link() . '</cite>', $comment_post_link." ".$comment_link, ' <span class="approve">' . __( '[Pending]' ) . '</span>' ); ?></h4>
571
572                         <?php
573                         else :
574                                 switch ( $comment->comment_type ) :
575                                 case 'pingback' :
576                                         $type = __( 'Pingback' );
577                                         break;
578                                 case 'trackback' :
579                                         $type = __( 'Trackback' );
580                                         break;
581                                 default :
582                                         $type = ucwords( $comment->comment_type );
583                                 endswitch;
584                                 $type = esc_html( $type );
585                         ?>
586                         <div class="dashboard-comment-wrap">
587                         <?php /* translators: %1$s is type of comment, %2$s is link to the post */ ?>
588                         <h4 class="comment-meta"><?php printf( _x( '%1$s on %2$s', 'dashboard' ), "<strong>$type</strong>", $comment_post_link ); ?></h4>
589                         <p class="comment-author"><?php comment_author_link(); ?></p>
590
591                         <?php endif; // comment_type ?>
592                         <blockquote><p><?php comment_excerpt(); ?></p></blockquote>
593                         <p class="row-actions"><?php echo $actions_string; ?></p>
594
595                         <div id="inline-<?php echo $comment->comment_ID; ?>" class="hidden">
596                                 <textarea class="comment" rows="3" cols="10"><?php echo $comment->comment_content; ?></textarea>
597                                 <div class="author-email"><?php echo esc_attr( $comment->comment_author_email ); ?></div>
598                                 <div class="author"><?php echo esc_attr( $comment->comment_author ); ?></div>
599                                 <div class="author-url"><?php echo esc_attr( $comment->comment_author_url ); ?></div>
600                                 <div class="comment_status"><?php echo $comment->comment_approved; ?></div>
601                         </div>
602                         </div>
603                 </div>
604 <?php
605 }
606
607 function wp_dashboard_incoming_links() {
608         wp_dashboard_cached_rss_widget( 'dashboard_incoming_links', 'wp_dashboard_incoming_links_output' );
609 }
610
611 /**
612  * Display incoming links dashboard widget content.
613  *
614  * @since unknown
615  */
616 function wp_dashboard_incoming_links_output() {
617         $widgets = get_option( 'dashboard_widget_options' );
618         @extract( @$widgets['dashboard_incoming_links'], EXTR_SKIP );
619         $rss = fetch_feed( $url );
620
621         if ( is_wp_error($rss) ) {
622                 if ( is_admin() || current_user_can('manage_options') ) {
623                         echo '<p>';
624                         printf(__('<strong>RSS Error</strong>: %s'), $rss->get_error_message());
625                         echo '</p>';
626                 }
627                 return;
628         }
629
630         if ( !$rss->get_item_quantity() ) {
631                 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&hellip; yet. It&#8217;s okay &#8212; there is no rush.') . "</p>\n";
632                 return;
633         }
634
635         echo "<ul>\n";
636
637         $count = 0;
638         foreach ( $rss->get_items() as $item ) {
639                 $publisher = '';
640                 $site_link = '';
641                 $link = '';
642                 $content = '';
643                 $date = '';
644                 $link = esc_url( strip_tags( $item->get_link() ) );
645
646                 $author = $item->get_author();
647                 $site_link = esc_url( strip_tags( $author->get_link() ) );
648
649                 if ( !$publisher = esc_html( strip_tags( $author->get_name() ) ) )
650                         $publisher = __( 'Somebody' );
651                 if ( $site_link )
652                         $publisher = "<a href='$site_link'>$publisher</a>";
653                 else
654                         $publisher = "<strong>$publisher</strong>";
655
656                 $content = $item->get_content();
657                 $content = wp_html_excerpt($content, 50) . ' ...';
658
659                 if ( $link )
660                         /* translators: incoming links feed, %1$s is other person, %3$s is content */
661                         $text = __( '%1$s linked here <a href="%2$s">saying</a>, "%3$s"' );
662                 else
663                         /* translators: incoming links feed, %1$s is other person, %3$s is content */
664                         $text = __( '%1$s linked here saying, "%3$s"' );
665
666                 if ( $show_date ) {
667                         if ( $show_author || $show_summary )
668                                 /* translators: incoming links feed, %4$s is the date */
669                                 $text .= ' ' . __( 'on %4$s' );
670                         $date = esc_html( strip_tags( $item->get_date() ) );
671                         $date = strtotime( $date );
672                         $date = gmdate( get_option( 'date_format' ), $date );
673                 }
674
675                 echo "\t<li>" . sprintf( $text, $publisher, $link, $content, $date ) . "</li>\n";
676         }
677
678         echo "</ul>\n";
679
680 }
681
682 function wp_dashboard_incoming_links_control() {
683         wp_dashboard_rss_control( 'dashboard_incoming_links', array( 'title' => false, 'show_summary' => false, 'show_author' => false ) );
684 }
685
686 function wp_dashboard_primary() {
687         wp_dashboard_cached_rss_widget( 'dashboard_primary', 'wp_dashboard_rss_output' );
688 }
689
690 function wp_dashboard_primary_control() {
691         wp_dashboard_rss_control( 'dashboard_primary' );
692 }
693
694 /**
695  * {@internal Missing Short Description}}
696  *
697  * @since unknown
698  *
699  * @param int $widget_id
700  */
701 function wp_dashboard_rss_output( $widget_id ) {
702         $widgets = get_option( 'dashboard_widget_options' );
703         echo "<div class='rss-widget'>";
704         wp_widget_rss_output( $widgets[$widget_id] );
705         echo "</div>";
706 }
707
708 function wp_dashboard_secondary() {
709         wp_dashboard_cached_rss_widget( 'dashboard_secondary', 'wp_dashboard_secondary_output' );
710 }
711
712 function wp_dashboard_secondary_control() {
713         wp_dashboard_rss_control( 'dashboard_secondary' );
714 }
715
716 /**
717  * Display secondary dashboard RSS widget feed.
718  *
719  * @since unknown
720  *
721  * @return unknown
722  */
723 function wp_dashboard_secondary_output() {
724         $widgets = get_option( 'dashboard_widget_options' );
725         @extract( @$widgets['dashboard_secondary'], EXTR_SKIP );
726         $rss = @fetch_feed( $url );
727
728         if ( is_wp_error($rss) ) {
729                 if ( is_admin() || current_user_can('manage_options') ) {
730                         echo '<div class="rss-widget"><p>';
731                         printf(__('<strong>RSS Error</strong>: %s'), $rss->get_error_message());
732                         echo '</p></div>';
733                 }
734         } elseif ( !$rss->get_item_quantity() ) {
735                 return false;
736         } else {
737                 echo '<div class="rss-widget">';
738                 wp_widget_rss_output( $rss, $widgets['dashboard_secondary'] );
739                 echo '</div>';
740         }
741 }
742
743 function wp_dashboard_plugins() {
744         wp_dashboard_cached_rss_widget( 'dashboard_plugins', 'wp_dashboard_plugins_output', array(
745                 'http://wordpress.org/extend/plugins/rss/browse/popular/',
746                 'http://wordpress.org/extend/plugins/rss/browse/new/',
747                 'http://wordpress.org/extend/plugins/rss/browse/updated/'
748         ) );
749 }
750
751 /**
752  * Display plugins most popular, newest plugins, and recently updated widget text.
753  *
754  * @since unknown
755  */
756 function wp_dashboard_plugins_output() {
757         $popular = fetch_feed( 'http://wordpress.org/extend/plugins/rss/browse/popular/' );
758         $new     = fetch_feed( 'http://wordpress.org/extend/plugins/rss/browse/new/' );
759         $updated = fetch_feed( 'http://wordpress.org/extend/plugins/rss/browse/updated/' );
760
761         if ( false === $plugin_slugs = get_transient( 'plugin_slugs' ) ) {
762                 $plugin_slugs = array_keys( get_plugins() );
763                 set_transient( 'plugin_slugs', $plugin_slugs, 86400 );
764         }
765
766         foreach ( array( 'popular' => __('Most Popular'), 'new' => __('Newest Plugins'), 'updated' => __('Recently Updated') ) as $feed => $label ) {
767                 if ( is_wp_error($$feed) || !$$feed->get_item_quantity() )
768                         continue;
769
770                 $items = $$feed->get_items(0, 5);
771
772                 // Pick a random, non-installed plugin
773                 while ( true ) {
774                         // Abort this foreach loop iteration if there's no plugins left of this type
775                         if ( 0 == count($items) )
776                                 continue 2;
777
778                         $item_key = array_rand($items);
779                         $item = $items[$item_key];
780
781                         list($link, $frag) = explode( '#', $item->get_link() );
782
783                         $link = esc_url($link);
784                         if ( preg_match( '|/([^/]+?)/?$|', $link, $matches ) )
785                                 $slug = $matches[1];
786                         else {
787                                 unset( $items[$item_key] );
788                                 continue;
789                         }
790
791                         // Is this random plugin's slug already installed? If so, try again.
792                         reset( $plugin_slugs );
793                         foreach ( $plugin_slugs as $plugin_slug ) {
794                                 if ( $slug == substr( $plugin_slug, 0, strlen( $slug ) ) ) {
795                                         unset( $items[$item_key] );
796                                         continue 2;
797                                 }
798                         }
799
800                         // If we get to this point, then the random plugin isn't installed and we can stop the while().
801                         break;
802                 }
803
804                 // Eliminate some common badly formed plugin descriptions
805                 while ( ( null !== $item_key = array_rand($items) ) && false !== strpos( $items[$item_key]->get_description(), 'Plugin Name:' ) )
806                         unset($items[$item_key]);
807
808                 if ( !isset($items[$item_key]) )
809                         continue;
810
811                 // current bbPress feed item titles are: user on "topic title"
812                 if ( preg_match( '/&quot;(.*)&quot;/s', $item->get_title(), $matches ) )
813                         $title = $matches[1];
814                 else // but let's make it forward compatible if things change
815                         $title = $item->get_title();
816                 $title = esc_html( $title );
817
818                 $description = esc_html( strip_tags(@html_entity_decode($item->get_description(), ENT_QUOTES, get_option('blog_charset'))) );
819
820                 $ilink = wp_nonce_url('plugin-install.php?tab=plugin-information&plugin=' . $slug, 'install-plugin_' . $slug) .
821                                                         '&amp;TB_iframe=true&amp;width=600&amp;height=800';
822
823                 echo "<h4>$label</h4>\n";
824                 echo "<h5><a href='$link'>$title</a></h5>&nbsp;<span>(<a href='$ilink' class='thickbox' title='$title'>" . __( 'Install' ) . "</a>)</span>\n";
825                 echo "<p>$description</p>\n";
826         }
827 }
828
829 /**
830  * Checks to see if all of the feed url in $check_urls are cached.
831  *
832  * If $check_urls is empty, look for the rss feed url found in the dashboard
833  * widget optios of $widget_id. If cached, call $callback, a function that
834  * echoes out output for this widget. If not cache, echo a "Loading..." stub
835  * which is later replaced by AJAX call (see top of /wp-admin/index.php)
836  *
837  * @since unknown
838  *
839  * @param int $widget_id
840  * @param callback $callback
841  * @param array $check_urls RSS feeds
842  * @return bool False on failure. True on success.
843  */
844 function wp_dashboard_cached_rss_widget( $widget_id, $callback, $check_urls = array() ) {
845         $loading = '<p class="widget-loading">' . __( 'Loading&#8230;' ) . '</p>';
846
847         if ( empty($check_urls) ) {
848                 $widgets = get_option( 'dashboard_widget_options' );
849                 if ( empty($widgets[$widget_id]['url']) ) {
850                         echo $loading;
851                         return false;
852                 }
853                 $check_urls = array( $widgets[$widget_id]['url'] );
854         }
855
856         include_once ABSPATH . WPINC . '/class-feed.php';
857         foreach ( $check_urls as $check_url ) {
858                 $cache = new WP_Feed_Cache_Transient('', md5($check_url), '');
859                 if ( ! $cache->load() ) {
860                         echo $loading;
861                         return false;
862                 }
863         }
864
865         if ( $callback && is_callable( $callback ) ) {
866                 $args = array_slice( func_get_args(), 2 );
867                 array_unshift( $args, $widget_id );
868                 call_user_func_array( $callback, $args );
869         }
870
871         return true;
872 }
873
874 /* Dashboard Widgets Controls */
875
876 // Calls widget_control callback
877 /**
878  * Calls widget control callback.
879  *
880  * @since unknown
881  *
882  * @param int $widget_control_id Registered Widget ID.
883  */
884 function wp_dashboard_trigger_widget_control( $widget_control_id = false ) {
885         global $wp_dashboard_control_callbacks;
886
887         if ( is_scalar($widget_control_id) && $widget_control_id && isset($wp_dashboard_control_callbacks[$widget_control_id]) && is_callable($wp_dashboard_control_callbacks[$widget_control_id]) ) {
888                 call_user_func( $wp_dashboard_control_callbacks[$widget_control_id], '', array( 'id' => $widget_control_id, 'callback' => $wp_dashboard_control_callbacks[$widget_control_id] ) );
889         }
890 }
891
892 /**
893  * The RSS dashboard widget control.
894  *
895  * Sets up $args to be used as input to wp_widget_rss_form(). Handles POST data
896  * from RSS-type widgets.
897  *
898  * @since unknown
899  *
900  * @param string widget_id
901  * @param array form_inputs
902  */
903 function wp_dashboard_rss_control( $widget_id, $form_inputs = array() ) {
904         if ( !$widget_options = get_option( 'dashboard_widget_options' ) )
905                 $widget_options = array();
906
907         if ( !isset($widget_options[$widget_id]) )
908                 $widget_options[$widget_id] = array();
909
910         $number = 1; // Hack to use wp_widget_rss_form()
911         $widget_options[$widget_id]['number'] = $number;
912
913         if ( 'POST' == $_SERVER['REQUEST_METHOD'] && isset($_POST['widget-rss'][$number]) ) {
914                 $_POST['widget-rss'][$number] = stripslashes_deep( $_POST['widget-rss'][$number] );
915                 $widget_options[$widget_id] = wp_widget_rss_process( $_POST['widget-rss'][$number] );
916                 // title is optional.  If black, fill it if possible
917                 if ( !$widget_options[$widget_id]['title'] && isset($_POST['widget-rss'][$number]['title']) ) {
918                         $rss = fetch_feed($widget_options[$widget_id]['url']);
919                         if ( ! is_wp_error($rss) )
920                                 $widget_options[$widget_id]['title'] = htmlentities(strip_tags($rss->get_title()));
921                         else
922                                 $widget_options[$widget_id]['title'] = htmlentities(__('Unknown Feed'));
923                 }
924                 update_option( 'dashboard_widget_options', $widget_options );
925         }
926
927         wp_widget_rss_form( $widget_options[$widget_id], $form_inputs );
928 }
929
930 /**
931  * Empty function usable by plugins to output empty dashboard widget (to be populated later by JS).
932  */
933 function wp_dashboard_empty() {}
934
935 ?>