]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/template.php
Wordpress 2.5.1
[autoinstalls/wordpress.git] / wp-admin / includes / template.php
1 <?php
2
3 //
4 // Big Mess
5 //
6
7 // Dandy new recursive multiple category stuff.
8 function cat_rows( $parent = 0, $level = 0, $categories = 0 ) {
9         if ( !$categories ) {
10                 $args = array('hide_empty' => 0);
11                 if ( !empty($_GET['s']) )
12                         $args['search'] = $_GET['s'];
13                 $categories = get_categories( $args );
14         }
15
16         $children = _get_term_hierarchy('category');
17
18         if ( $categories ) {
19                 ob_start();
20                 foreach ( $categories as $category ) {
21                         if ( $category->parent == $parent) {
22                                 echo "\t" . _cat_row( $category, $level );
23                                 if ( isset($children[$category->term_id]) )
24                                         cat_rows( $category->term_id, $level +1, $categories );
25                         }
26                 }
27                 $output = ob_get_contents();
28                 ob_end_clean();
29
30                 $output = apply_filters('cat_rows', $output);
31
32                 echo $output;
33         } else {
34                 return false;
35         }
36 }
37
38 function _cat_row( $category, $level, $name_override = false ) {
39         global $class;
40
41         $category = get_category( $category );
42
43         $pad = str_repeat( '&#8212; ', $level );
44         $name = ( $name_override ? $name_override : $pad . ' ' . $category->name );
45         if ( current_user_can( 'manage_categories' ) ) {
46                 $edit = "<a class='row-title' href='categories.php?action=edit&amp;cat_ID=$category->term_id' title='" . attribute_escape(sprintf(__('Edit "%s"'), $category->name)) . "'>$name</a>";
47         } else {
48                 $edit = $name;
49         }
50
51         $class = " class='alternate'" == $class ? '' : " class='alternate'";
52
53         $category->count = number_format_i18n( $category->count );
54         $posts_count = ( $category->count > 0 ) ? "<a href='edit.php?cat=$category->term_id'>$category->count</a>" : $category->count;
55         $output = "<tr id='cat-$category->term_id'$class>
56                            <th scope='row' class='check-column'>";
57         if ( absint(get_option( 'default_category' ) ) != $category->term_id ) {
58                 $output .= "<input type='checkbox' name='delete[]' value='$category->term_id' />";
59         } else {
60                 $output .= "&nbsp;";
61         }
62         $output .= "</th>
63                                 <td>$edit</td>
64                                 <td>$category->description</td>
65                                 <td class='num'>$posts_count</td>\n\t</tr>\n";
66
67         return apply_filters('cat_row', $output);
68 }
69
70 function link_cat_row( $category ) {
71         global $class;
72
73         if ( !$category = get_term( $category, 'link_category' ) )
74                 return false;
75         if ( is_wp_error( $category ) )
76                 return $category;
77
78         $name = ( $name_override ? $name_override : $category->name );
79         if ( current_user_can( 'manage_categories' ) ) {
80                 $edit = "<a class='row-title' href='link-category.php?action=edit&amp;cat_ID=$category->term_id' title='" . attribute_escape(sprintf(__('Edit "%s"'), $category->name)) . "' class='edit'>$name</a>";
81                 $default_cat_id = (int) get_option( 'default_link_category' );
82         } else {
83                 $edit = $name;
84         }
85
86         $class = " class='alternate'" == $class ? '' : " class='alternate'";
87
88         $category->count = number_format_i18n( $category->count );
89         $count = ( $category->count > 0 ) ? "<a href='link-manager.php?cat_id=$category->term_id'>$category->count</a>" : $category->count;
90         $output = "<tr id='link-cat-$category->term_id'$class>
91                            <th scope='row' class='check-column'>";
92         if ( absint( get_option( 'default_link_category' ) ) != $category->term_id ) {
93                 $output .= "<input type='checkbox' name='delete[]' value='$category->term_id' />";
94         } else {
95                 $output .= "&nbsp;";
96         }
97         $output .= "</th>
98                                 <td>$edit</td>
99                                 <td>$category->description</td>
100                                 <td class='num'>$count</td></tr>";
101
102         return apply_filters( 'link_cat_row', $output );
103 }
104
105 function checked( $checked, $current) {
106         if ( $checked == $current)
107                 echo ' checked="checked"';
108 }
109
110 function selected( $selected, $current) {
111         if ( $selected == $current)
112                 echo ' selected="selected"';
113 }
114
115 //
116 // Category Checklists
117 //
118
119 // Deprecated. Use wp_link_category_checklist
120 function dropdown_categories( $default = 0, $parent = 0, $popular_ids = array() ) {
121         global $post_ID;
122         wp_category_checklist($post_ID);
123 }
124
125 class Walker_Category_Checklist extends Walker {
126         var $tree_type = 'category';
127         var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
128
129         function start_lvl(&$output, $depth, $args) {
130                 $indent = str_repeat("\t", $depth);
131                 $output .= "$indent<ul class='children'>\n";
132         }
133
134         function end_lvl(&$output, $depth, $args) {
135                 $indent = str_repeat("\t", $depth);
136                 $output .= "$indent</ul>\n";
137         }
138
139         function start_el(&$output, $category, $depth, $args) {
140                 extract($args);
141
142                 $class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : '';
143                 $output .= "\n<li id='category-$category->term_id'$class>" . '<label for="in-category-' . $category->term_id . '" class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="post_category[]" id="in-category-' . $category->term_id . '"' . (in_array( $category->term_id, $selected_cats ) ? ' checked="checked"' : "" ) . '/> ' . wp_specialchars( apply_filters('the_category', $category->name )) . '</label>';
144         }
145
146         function end_el(&$output, $category, $depth, $args) {
147                 $output .= "</li>\n";
148         }
149 }
150
151 function wp_category_checklist( $post_id = 0, $descendants_and_self = 0, $selected_cats = false ) {
152         $walker = new Walker_Category_Checklist;
153         $descendants_and_self = (int) $descendants_and_self;
154
155         $args = array();
156         
157         if ( $post_id )
158                 $args['selected_cats'] = wp_get_post_categories($post_id);
159         else
160                 $args['selected_cats'] = array();
161         if ( is_array( $selected_cats ) )
162                 $args['selected_cats'] = $selected_cats;
163         $args['popular_cats'] = get_terms( 'category', array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
164         if ( $descendants_and_self ) {
165                 $categories = get_categories( "child_of=$descendants_and_self&hierarchical=0&hide_empty=0" );
166                 $self = get_category( $descendants_and_self );
167                 array_unshift( $categories, $self );
168         } else {
169                 $categories = get_categories('get=all');
170         }
171
172         $args = array($categories, 0, $args);
173         $output = call_user_func_array(array(&$walker, 'walk'), $args);
174
175         echo $output;
176 }
177
178 function wp_popular_terms_checklist( $taxonomy, $default = 0, $number = 10 ) {
179         global $post_ID;
180         if ( $post_ID )
181                 $checked_categories = wp_get_post_categories($post_ID);
182         else
183                 $checked_categories = array();
184         $categories = get_terms( $taxonomy, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => $number, 'hierarchical' => false ) );
185
186         $popular_ids = array();
187         foreach ( (array) $categories as $category ) {
188                 $popular_ids[] = $category->term_id;
189                 $id = "popular-category-$category->term_id";
190                 ?>
191
192                 <li id="<?php echo $id; ?>" class="popular-category">
193                         <label class="selectit" for="in-<?php echo $id; ?>">
194                         <input id="in-<?php echo $id; ?>" type="checkbox" value="<?php echo (int) $category->term_id; ?>" />
195                                 <?php echo wp_specialchars( apply_filters( 'the_category', $category->name ) ); ?>
196                         </label>
197                 </li>
198
199                 <?php
200         }
201         return $popular_ids;
202 }
203
204 // Deprecated. Use wp_link_category_checklist
205 function dropdown_link_categories( $default = 0 ) {
206         global $link_id;
207
208         wp_link_category_checklist($link_id);
209 }
210
211 function wp_link_category_checklist( $link_id = 0 ) {
212         if ( $link_id ) {
213                 $checked_categories = wp_get_link_cats($link_id);
214
215                 if ( count( $checked_categories ) == 0 ) {
216                         // No selected categories, strange
217                         $checked_categories[] = $default;
218                 }
219         } else {
220                 $checked_categories[] = $default;
221         }
222
223         $categories = get_terms('link_category', 'orderby=count&hide_empty=0');
224
225         if ( empty($categories) )
226                 return;
227
228         foreach ( $categories as $category ) {
229                 $cat_id = $category->term_id;
230                 $name = wp_specialchars( apply_filters('the_category', $category->name));
231                 $checked = in_array( $cat_id, $checked_categories );
232                 echo '<li id="link-category-', $cat_id, '"><label for="in-link-category-', $cat_id, '" class="selectit"><input value="', $cat_id, '" type="checkbox" name="link_category[]" id="in-link-category-', $cat_id, '"', ($checked ? ' checked="checked"' : "" ), '/> ', $name, "</label></li>";
233         }
234 }
235
236 // Tag stuff
237
238 // Returns a single tag row (see tag_rows below)
239 // Note: this is also used in admin-ajax.php!
240 function _tag_row( $tag, $class = '' ) {
241                 $count = number_format_i18n( $tag->count );
242                 $count = ( $count > 0 ) ? "<a href='edit.php?tag=$tag->slug'>$count</a>" : $count;
243
244                 $name = apply_filters( 'term_name', $tag->name );
245                 $out = '';
246                 $out .= '<tr id="tag-' . $tag->term_id . '"' . $class . '>';
247                 $out .= '<th scope="row" class="check-column"> <input type="checkbox" name="delete_tags[]" value="' . $tag->term_id . '" /></th>';
248                 $out .= '<td><strong><a class="row-title" href="edit-tags.php?action=edit&amp;tag_ID=' . $tag->term_id . '" title="' . attribute_escape(sprintf(__('Edit "%s"'), $name)) . '">' .
249                         $name . '</a></td>';
250
251                 $out .= "<td class='num'>$count</td>";
252                 $out .= '</tr>';
253
254                 return $out;
255 }
256
257 // Outputs appropriate rows for the Nth page of the Tag Management screen,
258 // assuming M tags displayed at a time on the page
259 // Returns the number of tags displayed
260 function tag_rows( $page = 1, $pagesize = 20, $searchterms = '' ) {
261
262         // Get a page worth of tags
263         $start = ($page - 1) * $pagesize;
264
265         $args = array('offset' => $start, 'number' => $pagesize, 'hide_empty' => 0);
266
267         if ( !empty( $searchterms ) ) {
268                 $args['search'] = $searchterms;
269         }
270
271         $tags = get_terms( 'post_tag', $args );
272
273         // convert it to table rows
274         $out = '';
275         $class = '';
276         $count = 0;
277         foreach( $tags as $tag )
278                 $out .= _tag_row( $tag, ++$count % 2 ? ' class="alternate"' : '' );
279
280         // filter and send to screen
281         $out = apply_filters('tag_rows', $out);
282         echo $out;
283         return $count;
284 }
285
286 // define the columns to display, the syntax is 'internal name' => 'display name'
287 function wp_manage_posts_columns() {
288         $posts_columns = array();
289         $posts_columns['cb'] = '<input type="checkbox" onclick="checkAll(document.getElementById(\'posts-filter\'));" />';
290         if ( 'draft' === $_GET['post_status'] )
291                 $posts_columns['modified'] = __('Modified');
292         elseif ( 'pending' === $_GET['post_status'] )
293                 $posts_columns['modified'] = __('Submitted');
294         else
295                 $posts_columns['date'] = __('Date');
296         $posts_columns['title'] = __('Title');
297         $posts_columns['author'] = __('Author');
298         $posts_columns['categories'] = __('Categories');
299         $posts_columns['tags'] = __('Tags');
300         if ( !in_array($_GET['post_status'], array('pending', 'draft', 'future')) )
301                 $posts_columns['comments'] = '<div class="vers"><img alt="Comments" src="images/comment-grey-bubble.png" /></div>';
302         $posts_columns['status'] = __('Status');
303         $posts_columns = apply_filters('manage_posts_columns', $posts_columns);
304
305         return $posts_columns;
306 }
307
308 // define the columns to display, the syntax is 'internal name' => 'display name'
309 function wp_manage_media_columns() {
310         $posts_columns = array();
311         $posts_columns['cb'] = '<input type="checkbox" onclick="checkAll(document.getElementById(\'posts-filter\'));" />';
312         $posts_columns['icon'] = '';
313         $posts_columns['media'] = _c('Media|media column header');
314         $posts_columns['desc'] = _c('Description|media column header');
315         $posts_columns['date'] = _c('Date Added|media column header');
316         $posts_columns['parent'] = _c('Appears with|media column header');
317         $posts_columns['comments'] = '<div class="vers"><img alt="Comments" src="images/comment-grey-bubble.png" /></div>';
318         $posts_columns['location'] = _c('Location|media column header');
319         $posts_columns = apply_filters('manage_media_columns', $posts_columns);
320
321         return $posts_columns;
322 }
323
324 function wp_manage_pages_columns() {
325         $posts_columns = array();
326         $posts_columns['cb'] = '<input type="checkbox" onclick="checkAll(document.getElementById(\'posts-filter\'));" />';
327         if ( 'draft' === $_GET['post_status'] )
328                 $posts_columns['modified'] = __('Modified');
329         elseif ( 'pending' === $_GET['post_status'] )
330                 $posts_columns['modified'] = __('Submitted');
331         else
332                 $posts_columns['date'] = __('Date');
333         $posts_columns['title'] = __('Title');
334         $posts_columns['author'] = __('Author');
335         if ( !in_array($_GET['post_status'], array('pending', 'draft', 'future')) )
336                 $posts_columns['comments'] = '<div class="vers"><img alt="" src="images/comment-grey-bubble.png" /></div>';
337         $posts_columns['status'] = __('Status');
338         $posts_columns = apply_filters('manage_pages_columns', $posts_columns);
339
340         return $posts_columns;
341 }
342
343 /*
344  * display one row if the page doesn't have any children
345  * otherwise, display the row and its children in subsequent rows
346  */
347 function display_page_row( $page, &$children_pages, $level = 0 ) {
348         global $post;
349         static $class;
350
351         $post = $page;
352         setup_postdata($page);
353
354         $page->post_title = wp_specialchars( $page->post_title );
355         $pad = str_repeat( '&#8212; ', $level );
356         $id = (int) $page->ID;
357         $class = ('alternate' == $class ) ? '' : 'alternate';
358         $posts_columns = wp_manage_pages_columns();
359         $title = get_the_title();
360         if ( empty($title) )
361                 $title = __('(no title)');
362 ?>
363   <tr id='page-<?php echo $id; ?>' class='<?php echo $class; ?>'>
364
365
366  <?php
367
368 foreach ($posts_columns as $column_name=>$column_display_name) {
369
370         switch ($column_name) {
371
372         case 'cb':
373                 ?>
374                 <th scope="row" class="check-column"><input type="checkbox" name="delete[]" value="<?php the_ID(); ?>" /></th>
375                 <?php
376                 break;
377         case 'modified':
378         case 'date':
379                 if ( '0000-00-00 00:00:00' == $page->post_date && 'date' == $column_name ) {
380                         $t_time = $h_time = __('Unpublished');
381                 } else {
382                         if ( 'modified' == $column_name ) {
383                                 $t_time = get_the_modified_time(__('Y/m/d g:i:s A'));
384                                 $m_time = $page->post_modified;
385                                 $time = get_post_modified_time('G', true);
386                         } else {
387                                 $t_time = get_the_time(__('Y/m/d g:i:s A'));
388                                 $m_time = $page->post_date;
389                                 $time = get_post_time('G', true);
390                         }
391                         if ( ( abs(time() - $time) ) < 86400 ) {
392                                 if ( ( 'future' == $page->post_status) )
393                                         $h_time = sprintf( __('%s from now'), human_time_diff( $time ) );
394                                 else
395                                         $h_time = sprintf( __('%s ago'), human_time_diff( $time ) );
396                         } else {
397                                 $h_time = mysql2date(__('Y/m/d'), $m_time);
398                         }
399                 }
400                 ?>
401                 <td><abbr title="<?php echo $t_time ?>"><?php echo $h_time ?></abbr></td>
402                 <?php
403                 break;
404         case 'title':
405                 ?>
406                 <td><strong><a class="row-title" href="page.php?action=edit&amp;post=<?php the_ID(); ?>" title="<?php echo attribute_escape(sprintf(__('Edit "%s"'), $title)); ?>"><?php echo $pad; echo $title ?></a></strong>
407                 <?php if ('private' == $page->post_status) _e(' &#8212; <strong>Private</strong>'); ?></td>
408                 <?php
409                 break;
410
411         case 'comments':
412                 ?>
413                 <td class="num"><div class="post-com-count-wrapper">
414                 <?php
415                 $left = get_pending_comments_num( $page->ID );
416                 $pending_phrase = sprintf( __('%s pending'), number_format( $left ) );
417                 if ( $left )
418                         echo '<strong>';
419                 comments_number("<a href='edit-pages.php?page_id=$id' title='$pending_phrase' class='post-com-count'><span class='comment-count'>" . __('0') . '</span></a>', "<a href='edit-pages.php?page_id=$id' title='$pending_phrase' class='post-com-count'><span class='comment-count'>" . __('1') . '</span></a>', "<a href='edit-pages.php?page_id=$id' title='$pending_phrase' class='post-com-count'><span class='comment-count'>" . __('%') . '</span></a>');
420                 if ( $left )
421                         echo '</strong>';
422                 ?>
423                 </div></td>
424                 <?php
425                 break;
426
427         case 'author':
428                 ?>
429                 <td><a href="edit-pages.php?author=<?php the_author_ID(); ?>"><?php the_author() ?></a></td>
430                 <?php
431                 break;
432
433         case 'status':
434                 ?>
435                 <td>
436                 <a href="<?php the_permalink(); ?>" title="<?php echo attribute_escape(sprintf(__('View "%s"'), $title)); ?>" rel="permalink">
437                 <?php
438                 switch ( $page->post_status ) {
439                         case 'publish' :
440                         case 'private' :
441                                 _e('Published');
442                                 break;
443                         case 'future' :
444                                 _e('Scheduled');
445                                 break;
446                         case 'pending' :
447                                 _e('Pending Review');
448                                 break;
449                         case 'draft' :
450                                 _e('Unpublished');
451                                 break;
452                 }
453                 ?>
454                 </a>
455                 </td>
456                 <?php
457                 break;
458
459         default:
460                 ?>
461                 <td><?php do_action('manage_pages_custom_column', $column_name, $id); ?></td>
462                 <?php
463                 break;
464         }
465 }
466  ?>
467
468    </tr>
469
470 <?php
471
472         if ( ! $children_pages )
473                 return true;
474
475         for ( $i = 0; $i < count($children_pages); $i++ ) {
476
477                 $child = $children_pages[$i];
478
479                 if ( $child->post_parent == $id ) {
480                         array_splice($children_pages, $i, 1);
481                         display_page_row($child, $children_pages, $level+1);
482                         $i = -1; //as numeric keys in $children_pages are not preserved after splice
483                 }
484         }
485 }
486
487 /*
488  * displays pages in hierarchical order
489  */
490 function page_rows( $pages ) {
491         if ( ! $pages )
492                 $pages = get_pages( 'sort_column=menu_order' );
493
494         if ( ! $pages )
495                 return false;
496
497         // splice pages into two parts: those without parent and those with parent
498
499         $top_level_pages = array();
500         $children_pages  = array();
501
502         foreach ( $pages as $page ) {
503
504                 // catch and repair bad pages
505                 if ( $page->post_parent == $page->ID ) {
506                         $page->post_parent = 0;
507                         $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_parent = '0' WHERE ID = %d", $page->ID) );
508                         clean_page_cache( $page->ID );
509                 }
510
511                 if ( 0 == $page->post_parent )
512                         $top_level_pages[] = $page;
513                 else
514                         $children_pages[] = $page;
515         }
516
517         foreach ( $top_level_pages as $page )
518                 display_page_row($page, $children_pages, 0);
519
520         /*
521          * display the remaining children_pages which are orphans
522          * having orphan requires parental attention
523          */
524          if ( count($children_pages) > 0 ) {
525                 $empty_array = array();
526                 foreach ( $children_pages as $orphan_page ) {
527                         clean_page_cache( $orphan_page->ID);
528                         display_page_row( $orphan_page, $empty_array, 0 );
529                 }
530          }
531 }
532
533 function user_row( $user_object, $style = '', $role = '' ) {
534         global $wp_roles;
535
536         $current_user = wp_get_current_user();
537         
538         if ( !( is_object( $user_object) && is_a( $user_object, 'WP_User' ) ) )
539                 $user_object = new WP_User( (int) $user_object );
540         $email = $user_object->user_email;
541         $url = $user_object->user_url;
542         $short_url = str_replace( 'http://', '', $url );
543         $short_url = str_replace( 'www.', '', $short_url );
544         if ('/' == substr( $short_url, -1 ))
545                 $short_url = substr( $short_url, 0, -1 );
546         if ( strlen( $short_url ) > 35 )
547                 $short_url =  substr( $short_url, 0, 32 ).'...';
548         $numposts = get_usernumposts( $user_object->ID );
549         if ( current_user_can( 'edit_user', $user_object->ID ) ) {
550                 if ($current_user->ID == $user_object->ID) {
551                         $edit = 'profile.php';
552                 } else {
553                         $edit = clean_url( add_query_arg( 'wp_http_referer', urlencode( clean_url( stripslashes( $_SERVER['REQUEST_URI'] ) ) ), "user-edit.php?user_id=$user_object->ID" ) );
554                 }
555                 $edit = "<a href=\"$edit\">$user_object->user_login</a>";
556         } else {
557                 $edit = $user_object->user_login;
558         }
559         $role_name = translate_with_context($wp_roles->role_names[$role]);
560         $r = "<tr id='user-$user_object->ID'$style>
561                 <th scope='row' class='check-column'><input type='checkbox' name='users[]' id='user_{$user_object->ID}' class='$role' value='{$user_object->ID}' /></th>
562                 <td><strong>$edit</strong></td>
563                 <td>$user_object->first_name $user_object->last_name</td>
564                 <td><a href='mailto:$email' title='" . sprintf( __('e-mail: %s' ), $email ) . "'>$email</a></td>
565                 <td>$role_name</td>";
566         $r .= "\n\t\t<td class='num'>";
567         if ( $numposts > 0 ) {
568                 $r .= "<a href='edit.php?author=$user_object->ID' title='" . __( 'View posts by this author' ) . "' class='edit'>";
569                 $r .= $numposts;
570                 $r .= '</a>';
571         } else {
572                 $r .= 0;
573         }
574         $r .= "</td>\n\t</tr>";
575         return $r;
576 }
577
578 function _wp_get_comment_list( $status = '', $s = false, $start, $num ) {
579         global $wpdb;
580
581         $start = abs( (int) $start );
582         $num = (int) $num;
583
584         if ( 'moderated' == $status )
585                 $approved = "comment_approved = '0'";
586         elseif ( 'approved' == $status )
587                 $approved = "comment_approved = '1'";
588         elseif ( 'spam' == $status )
589                 $approved = "comment_approved = 'spam'";
590         else
591                 $approved = "( comment_approved = '0' OR comment_approved = '1' )";
592
593         if ( $s ) {
594                 $s = $wpdb->escape($s);
595                 $comments = $wpdb->get_results("SELECT SQL_CALC_FOUND_ROWS * FROM $wpdb->comments WHERE
596                         (comment_author LIKE '%$s%' OR
597                         comment_author_email LIKE '%$s%' OR
598                         comment_author_url LIKE ('%$s%') OR
599                         comment_author_IP LIKE ('%$s%') OR
600                         comment_content LIKE ('%$s%') ) AND
601                         $approved
602                         ORDER BY comment_date_gmt DESC LIMIT $start, $num");
603         } else {
604                 $comments = $wpdb->get_results( "SELECT SQL_CALC_FOUND_ROWS * FROM $wpdb->comments USE INDEX (comment_date_gmt) WHERE $approved ORDER BY comment_date_gmt DESC LIMIT $start, $num" );
605         }
606
607         update_comment_cache($comments);
608
609         $total = $wpdb->get_var( "SELECT FOUND_ROWS()" );
610
611         return array($comments, $total);
612 }
613
614 function _wp_comment_row( $comment_id, $mode, $comment_status, $checkbox = true ) {
615         global $comment, $post;
616         $comment = get_comment( $comment_id );
617         $post = get_post($comment->comment_post_ID);
618         $authordata = get_userdata($post->post_author);
619         $the_comment_status = wp_get_comment_status($comment->comment_ID);
620         $class = ('unapproved' == $the_comment_status) ? 'unapproved' : '';
621
622         if ( current_user_can( 'edit_post', $post->ID ) ) {
623                 $post_link = "<a href='" . get_comment_link() . "'>";
624
625                 $post_link .= get_the_title($comment->comment_post_ID) . '</a>';
626                         
627                 $edit_link_start = "<a class='row-title' href='comment.php?action=editcomment&amp;c={$comment->comment_ID}' title='" . __('Edit comment') . "'>";
628                 $edit_link_end = '</a>';
629         } else {
630                 $post_link = get_the_title($comment->comment_post_ID);
631                 $edit_link_start = $edit_link_end ='';
632         }
633         
634         $author_url = get_comment_author_url();
635         if ( 'http://' == $author_url )
636                 $author_url = '';
637         $author_url_display = $author_url;
638         if ( strlen($author_url_display) > 50 )
639                 $author_url_display = substr($author_url_display, 0, 49) . '...';
640
641         $ptime = date('G', strtotime( $comment->comment_date ) );
642         if ( ( abs(time() - $ptime) ) < 86400 )
643                 $ptime = sprintf( __('%s ago'), human_time_diff( $ptime ) );
644         else
645                 $ptime = mysql2date(__('Y/m/d \a\t g:i A'), $comment->comment_date );
646
647         $delete_url    = clean_url( wp_nonce_url( "comment.php?action=deletecomment&p=$comment->comment_post_ID&c=$comment->comment_ID", "delete-comment_$comment->comment_ID" ) );
648         $approve_url   = clean_url( wp_nonce_url( "comment.php?action=approvecomment&p=$comment->comment_post_ID&c=$comment->comment_ID", "approve-comment_$comment->comment_ID" ) );
649         $unapprove_url = clean_url( wp_nonce_url( "comment.php?action=unapprovecomment&p=$comment->comment_post_ID&c=$comment->comment_ID", "unapprove-comment_$comment->comment_ID" ) );
650         $spam_url      = clean_url( wp_nonce_url( "comment.php?action=deletecomment&dt=spam&p=$comment->comment_post_ID&c=$comment->comment_ID", "delete-comment_$comment->comment_ID" ) );
651
652 ?>
653   <tr id="comment-<?php echo $comment->comment_ID; ?>" class='<?php echo $class; ?>'>
654 <?php if ( $checkbox ) : ?>
655     <td class="check-column"><?php if ( current_user_can('edit_post', $comment->comment_post_ID) ) { ?><input type="checkbox" name="delete_comments[]" value="<?php echo $comment->comment_ID; ?>" /><?php } ?></td>
656 <?php endif; ?>
657     <td class="comment">
658     <p class="comment-author"><strong><?php echo $edit_link_start; comment_author(); echo $edit_link_end; ?></strong><br />
659     <?php if ( !empty($author_url) ) : ?>
660     <a href="<?php echo $author_url ?>"><?php echo $author_url_display; ?></a> |
661     <?php endif; ?>
662     <?php if ( current_user_can( 'edit_post', $post->ID ) ) : ?>
663     <?php if ( !empty($comment->comment_author_email) ): ?>
664     <?php comment_author_email_link() ?> |
665     <?php endif; ?>
666     <a href="edit-comments.php?s=<?php comment_author_IP() ?>&amp;mode=detail"><?php comment_author_IP() ?></a>
667         <?php endif; //current_user_can?>    
668     </p>
669         <?php if ( 'detail' == $mode ) comment_text(); ?>
670         <p><?php printf(__('From %1$s, %2$s'), $post_link, $ptime) ?></p>
671     </td>
672     <td><?php comment_date(__('Y/m/d')); ?></td>
673     <td class="action-links">
674 <?php
675
676         $actions = array();
677
678                 $actions['approve']   = "<a href='$approve_url' class='dim:the-comment-list:comment-$comment->comment_ID:unapproved:e7e7d3:e7e7d3' title='" . __( 'Approve this comment' ) . "'>" . __( 'Approve' ) . '</a> | ';
679                 $actions['unapprove'] = "<a href='$unapprove_url' class='dim:the-comment-list:comment-$comment->comment_ID:unapproved:e7e7d3:e7e7d3' title='" . __( 'Unapprove this comment' ) . "'>" . __( 'Unapprove' ) . '</a> | ';
680
681                 // we're looking at list of only approved or only unapproved comments
682                 if ( 'moderated' == $comment_status ) {
683                         $actions['approve'] = "<a href='$approve_url' class='delete:the-comment-list:comment-$comment->comment_ID:e7e7d3:action=dim-comment' title='" . __( 'Approve this comment' ) . "'>" . __( 'Approve' ) . '</a> | ';
684                         unset($actions['unapprove']);
685                 } elseif ( 'approved' == $comment_status ) {
686                         $actions['unapprove'] = "<a href='$unapprove_url' class='delete:the-comment-list:comment-$comment->comment_ID:e7e7d3:action=dim-comment' title='" . __( 'Unapprove this comment' ) . "'>" . __( 'Unapprove' ) . '</a> | ';
687                         unset($actions['approve']);
688                 }
689
690         if ( current_user_can('edit_post', $comment->comment_post_ID) ) {
691                 $actions['spam']      = "<a href='$spam_url' class='delete:the-comment-list:comment-$comment->comment_ID::spam=1' title='" . __( 'Mark this comment as spam' ) . "'>" . __( 'Spam' ) . '</a> | ';
692                 $actions['delete']    = "<a href='$delete_url' class='delete:the-comment-list:comment-$comment->comment_ID delete'>" . __('Delete') . '</a>';
693                 foreach ( $actions as $action => $link )
694                         echo "<span class='$action'>$link</span>";
695         }
696         ?>
697         </td>
698   </tr>
699         <?php
700 }
701
702 function wp_dropdown_cats( $currentcat = 0, $currentparent = 0, $parent = 0, $level = 0, $categories = 0 ) {
703         if (!$categories )
704                 $categories = get_categories( 'hide_empty=0' );
705
706         if ( $categories ) {
707                 foreach ( $categories as $category ) {
708                         if ( $currentcat != $category->term_id && $parent == $category->parent) {
709                                 $pad = str_repeat( '&#8211; ', $level );
710                                 $category->name = wp_specialchars( $category->name );
711                                 echo "\n\t<option value='$category->term_id'";
712                                 if ( $currentparent == $category->term_id )
713                                         echo " selected='selected'";
714                                 echo ">$pad$category->name</option>";
715                                 wp_dropdown_cats( $currentcat, $currentparent, $category->term_id, $level +1, $categories );
716                         }
717                 }
718         } else {
719                 return false;
720         }
721 }
722
723 function list_meta( $meta ) {
724         // Exit if no meta
725         if (!$meta ) {
726                 echo '<tbody id="the-list" class="list:meta"><tr style="display: none;"><td>&nbsp;</td></tr></tbody>'; //TBODY needed for list-manipulation JS
727                 return;
728         }
729         $count = 0;
730 ?>
731         <thead>
732         <tr>
733                 <th><?php _e( 'Key' ) ?></th>
734                 <th><?php _e( 'Value' ) ?></th>
735                 <th colspan='2'><?php _e( 'Action' ) ?></th>
736         </tr>
737         </thead>
738         <tbody id='the-list' class='list:meta'>
739 <?php
740         foreach ( $meta as $entry )
741                 echo _list_meta_row( $entry, $count );
742         echo "\n\t</tbody>";
743 }
744
745 function _list_meta_row( $entry, &$count ) {
746         static $update_nonce = false;
747         if ( !$update_nonce )
748                 $update_nonce = wp_create_nonce( 'add-meta' );
749
750         $r = '';
751         ++ $count;
752         if ( $count % 2 )
753                 $style = 'alternate';
754         else
755                 $style = '';
756         if ('_' == $entry['meta_key'] { 0 } )
757                 $style .= ' hidden';
758
759         if ( is_serialized( $entry['meta_value'] ) ) {
760                 if ( is_serialized_string( $entry['meta_value'] ) ) {
761                         // this is a serialized string, so we should display it
762                         $entry['meta_value'] = maybe_unserialize( $entry['meta_value'] );
763                 } else {
764                         // this is a serialized array/object so we should NOT display it
765                         --$count;
766                         return;
767                 }
768         }
769
770         $entry['meta_key']   = attribute_escape($entry['meta_key']);
771         $entry['meta_value'] = htmlspecialchars($entry['meta_value']); // using a <textarea />
772         $entry['meta_id'] = (int) $entry['meta_id'];
773
774         $delete_nonce = wp_create_nonce( 'delete-meta_' . $entry['meta_id'] );
775
776         $r .= "\n\t<tr id='meta-{$entry['meta_id']}' class='$style'>";
777         $r .= "\n\t\t<td valign='top'><input name='meta[{$entry['meta_id']}][key]' tabindex='6' type='text' size='20' value='{$entry['meta_key']}' /></td>";
778         $r .= "\n\t\t<td><textarea name='meta[{$entry['meta_id']}][value]' tabindex='6' rows='2' cols='30'>{$entry['meta_value']}</textarea></td>";
779         $r .= "\n\t\t<td style='text-align: center;'><input name='updatemeta' type='submit' tabindex='6' value='".attribute_escape(__( 'Update' ))."' class='add:the-list:meta-{$entry['meta_id']}::_ajax_nonce=$update_nonce updatemeta' /><br />";
780         $r .= "\n\t\t<input name='deletemeta[{$entry['meta_id']}]' type='submit' ";
781         $r .= "class='delete:the-list:meta-{$entry['meta_id']}::_ajax_nonce=$delete_nonce deletemeta' tabindex='6' value='".attribute_escape(__( 'Delete' ))."' />";
782         $r .= wp_nonce_field( 'change-meta', '_ajax_nonce', false, false );
783         $r .= "</td>\n\t</tr>";
784         return $r;
785 }
786
787 function meta_form() {
788         global $wpdb;
789         $limit = (int) apply_filters( 'postmeta_form_limit', 30 );
790         $keys = $wpdb->get_col( "
791                 SELECT meta_key
792                 FROM $wpdb->postmeta
793                 WHERE meta_key NOT LIKE '\_%'
794                 GROUP BY meta_key
795                 ORDER BY meta_id DESC
796                 LIMIT $limit" );
797         if ( $keys )
798                 natcasesort($keys);
799 ?>
800 <p><strong><?php _e( 'Add a new custom field:' ) ?></strong></p>
801 <table id="newmeta" cellspacing="3" cellpadding="3">
802         <tr>
803 <th colspan="2"><?php _e( 'Key' ) ?></th>
804 <th><?php _e( 'Value' ) ?></th>
805 </tr>
806         <tr valign="top">
807                 <td style="width: 18%;" class="textright">
808 <?php if ( $keys ) : ?>
809 <select id="metakeyselect" name="metakeyselect" tabindex="7">
810 <option value="#NONE#"><?php _e( '- Select -' ); ?></option>
811 <?php
812
813         foreach ( $keys as $key ) {
814                 $key = attribute_escape( $key );
815                 echo "\n\t<option value='$key'>$key</option>";
816         }
817 ?>
818 </select> <?php _e( 'or' ); ?>
819 <?php endif; ?>
820 </td>
821 <td><input type="text" id="metakeyinput" name="metakeyinput" tabindex="7" /></td>
822                 <td><textarea id="metavalue" name="metavalue" rows="3" cols="25" tabindex="8"></textarea></td>
823         </tr>
824 <tr class="submit"><td colspan="3">
825         <?php wp_nonce_field( 'add-meta', '_ajax_nonce', false ); ?>
826         <input type="submit" id="addmetasub" name="addmeta" class="add:the-list:newmeta::post_id=<?php echo $GLOBALS['post_ID'] ? $GLOBALS['post_ID'] : $GLOBALS['temp_ID']; ?>" tabindex="9" value="<?php _e( 'Add Custom Field' ) ?>" />
827 </td></tr>
828 </table>
829 <?php
830
831 }
832
833 function touch_time( $edit = 1, $for_post = 1, $tab_index = 0 ) {
834         global $wp_locale, $post, $comment;
835
836         if ( $for_post )
837                 $edit = ( in_array($post->post_status, array('draft', 'pending') ) && (!$post->post_date || '0000-00-00 00:00:00' == $post->post_date ) ) ? false : true;
838
839         $tab_index_attribute = '';
840         if ( (int) $tab_index > 0 )
841                 $tab_index_attribute = " tabindex=\"$tab_index\"";
842
843         // echo '<label for="timestamp" style="display: block;"><input type="checkbox" class="checkbox" name="edit_date" value="1" id="timestamp"'.$tab_index_attribute.' /> '.__( 'Edit timestamp' ).'</label><br />';
844
845         $time_adj = time() + (get_option( 'gmt_offset' ) * 3600 );
846         $post_date = ($for_post) ? $post->post_date : $comment->comment_date;
847         $jj = ($edit) ? mysql2date( 'd', $post_date ) : gmdate( 'd', $time_adj );
848         $mm = ($edit) ? mysql2date( 'm', $post_date ) : gmdate( 'm', $time_adj );
849         $aa = ($edit) ? mysql2date( 'Y', $post_date ) : gmdate( 'Y', $time_adj );
850         $hh = ($edit) ? mysql2date( 'H', $post_date ) : gmdate( 'H', $time_adj );
851         $mn = ($edit) ? mysql2date( 'i', $post_date ) : gmdate( 'i', $time_adj );
852         $ss = ($edit) ? mysql2date( 's', $post_date ) : gmdate( 's', $time_adj );
853
854         $month = "<select id=\"mm\" name=\"mm\"$tab_index_attribute>\n";
855         for ( $i = 1; $i < 13; $i = $i +1 ) {
856                 $month .= "\t\t\t" . '<option value="' . zeroise($i, 2) . '"';
857                 if ( $i == $mm )
858                         $month .= ' selected="selected"';
859                 $month .= '>' . $wp_locale->get_month( $i ) . "</option>\n";
860         }
861         $month .= '</select>';
862
863         $day = '<input type="text" id="jj" name="jj" value="' . $jj . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off"  />';
864         $year = '<input type="text" id="aa" name="aa" value="' . $aa . '" size="4" maxlength="5"' . $tab_index_attribute . ' autocomplete="off"  />';
865         $hour = '<input type="text" id="hh" name="hh" value="' . $hh . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off"  />';
866         $minute = '<input type="text" id="mn" name="mn" value="' . $mn . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off"  />';
867         printf(_c('%1$s%2$s, %3$s <br />@ %4$s : %5$s|1: month input, 2: day input, 3: year input, 4: hour input, 5: minute input'), $month, $day, $year, $hour, $minute);
868         echo "\n\n";
869         foreach ( array('mm', 'jj', 'aa', 'hh', 'mn') as $timeunit )
870                 echo '<input type="hidden" id="hidden_' . $timeunit . '" name="hidden_' . $timeunit . '" value="' . $$timeunit . '" />' . "\n";
871 ?>
872
873 <input type="hidden" id="ss" name="ss" value="<?php echo $ss ?>" size="2" maxlength="2" />
874 <?php
875 }
876
877 function page_template_dropdown( $default = '' ) {
878         $templates = get_page_templates();
879         ksort( $templates );
880         foreach (array_keys( $templates ) as $template )
881                 : if ( $default == $templates[$template] )
882                         $selected = " selected='selected'";
883                 else
884                         $selected = '';
885         echo "\n\t<option value='".$templates[$template]."' $selected>$template</option>";
886         endforeach;
887 }
888
889 function parent_dropdown( $default = 0, $parent = 0, $level = 0 ) {
890         global $wpdb, $post_ID;
891         $items = $wpdb->get_results( "SELECT ID, post_parent, post_title FROM $wpdb->posts WHERE post_parent = $parent AND post_type = 'page' ORDER BY menu_order" );
892
893         if ( $items ) {
894                 foreach ( $items as $item ) {
895                         // A page cannot be its own parent.
896                         if (!empty ( $post_ID ) ) {
897                                 if ( $item->ID == $post_ID ) {
898                                         continue;
899                                 }
900                         }
901                         $pad = str_repeat( '&nbsp;', $level * 3 );
902                         if ( $item->ID == $default)
903                                 $current = ' selected="selected"';
904                         else
905                                 $current = '';
906
907                         echo "\n\t<option value='$item->ID'$current>$pad " . wp_specialchars($item->post_title) . "</option>";
908                         parent_dropdown( $default, $item->ID, $level +1 );
909                 }
910         } else {
911                 return false;
912         }
913 }
914
915 function browse_happy() {
916         $getit = __( 'WordPress recommends a better browser' );
917         echo '
918                 <span id="bh" class="alignright"><a href="http://browsehappy.com/" title="'.$getit.'"><img src="images/browse-happy.gif" alt="Browse Happy" /></a></span>
919                 ';
920 }
921
922 if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false)
923         add_action( 'in_admin_footer', 'browse_happy' );
924
925 function the_attachment_links( $id = false ) {
926         $id = (int) $id;
927         $post = & get_post( $id );
928
929         if ( $post->post_type != 'attachment' )
930                 return false;
931
932         $icon = get_attachment_icon( $post->ID );
933         $attachment_data = wp_get_attachment_metadata( $id );
934         $thumb = isset( $attachment_data['thumb'] );
935 ?>
936 <form id="the-attachment-links">
937 <table>
938         <col />
939         <col class="widefat" />
940         <tr>
941                 <th scope="row"><?php _e( 'URL' ) ?></th>
942                 <td><textarea rows="1" cols="40" type="text" class="attachmentlinks" readonly="readonly"><?php echo wp_get_attachment_url(); ?></textarea></td>
943         </tr>
944 <?php if ( $icon ) : ?>
945         <tr>
946                 <th scope="row"><?php $thumb ? _e( 'Thumbnail linked to file' ) : _e( 'Image linked to file' ); ?></th>
947                 <td><textarea rows="1" cols="40" type="text" class="attachmentlinks" readonly="readonly"><a href="<?php echo wp_get_attachment_url(); ?>"><?php echo $icon ?></a></textarea></td>
948         </tr>
949         <tr>
950                 <th scope="row"><?php $thumb ? _e( 'Thumbnail linked to page' ) : _e( 'Image linked to page' ); ?></th>
951                 <td><textarea rows="1" cols="40" type="text" class="attachmentlinks" readonly="readonly"><a href="<?php echo get_attachment_link( $post->ID ) ?>" rel="attachment wp-att-<?php echo $post->ID; ?>"><?php echo $icon ?></a></textarea></td>
952         </tr>
953 <?php else : ?>
954         <tr>
955                 <th scope="row"><?php _e( 'Link to file' ) ?></th>
956                 <td><textarea rows="1" cols="40" type="text" class="attachmentlinks" readonly="readonly"><a href="<?php echo wp_get_attachment_url(); ?>" class="attachmentlink"><?php echo basename( wp_get_attachment_url() );  ?></a></textarea></td>
957         </tr>
958         <tr>
959                 <th scope="row"><?php _e( 'Link to page' ) ?></th>
960                 <td><textarea rows="1" cols="40" type="text" class="attachmentlinks" readonly="readonly"><a href="<?php echo get_attachment_link( $post->ID ) ?>" rel="attachment wp-att-<?php echo $post->ID ?>"><?php the_title(); ?></a></textarea></td>
961         </tr>
962 <?php endif; ?>
963 </table>
964 </form>
965 <?php
966 }
967
968 function wp_dropdown_roles( $default = false ) {
969         global $wp_roles;
970         $r = '';
971         foreach( $wp_roles->role_names as $role => $name ) {
972                 $name = translate_with_context($name);
973                 if ( $default == $role ) // Make default first in list
974                         $p = "\n\t<option selected='selected' value='$role'>$name</option>";
975                 else
976                         $r .= "\n\t<option value='$role'>$name</option>";
977         }
978         echo $p . $r;
979 }
980
981 function wp_convert_hr_to_bytes( $size ) {
982         $size = strtolower($size);
983         $bytes = (int) $size;
984         if ( strpos($size, 'k') !== false )
985                 $bytes = intval($size) * 1024;
986         elseif ( strpos($size, 'm') !== false )
987                 $bytes = intval($size) * 1024 * 1024;
988         elseif ( strpos($size, 'g') !== false )
989                 $bytes = intval($size) * 1024 * 1024 * 1024;
990         return $bytes;
991 }
992
993 function wp_convert_bytes_to_hr( $bytes ) {
994         $units = array( 0 => 'B', 1 => 'kB', 2 => 'MB', 3 => 'GB' );
995         $log = log( $bytes, 1024 );
996         $power = (int) $log;
997         $size = pow(1024, $log - $power);
998         return $size . $units[$power];
999 }
1000
1001 function wp_max_upload_size() {
1002         $u_bytes = wp_convert_hr_to_bytes( ini_get( 'upload_max_filesize' ) );
1003         $p_bytes = wp_convert_hr_to_bytes( ini_get( 'post_max_size' ) );
1004         $bytes = apply_filters( 'upload_size_limit', min($u_bytes, $p_bytes), $u_bytes, $p_bytes );
1005         return $bytes;
1006 }
1007
1008 function wp_import_upload_form( $action ) {
1009         $bytes = apply_filters( 'import_upload_size_limit', wp_max_upload_size() );
1010         $size = wp_convert_bytes_to_hr( $bytes );
1011 ?>
1012 <form enctype="multipart/form-data" id="import-upload-form" method="post" action="<?php echo attribute_escape($action) ?>">
1013 <p>
1014 <?php wp_nonce_field('import-upload'); ?>
1015 <label for="upload"><?php _e( 'Choose a file from your computer:' ); ?></label> (<?php printf( __('Maximum size: %s' ), $size ); ?>)
1016 <input type="file" id="upload" name="import" size="25" />
1017 <input type="hidden" name="action" value="save" />
1018 <input type="hidden" name="max_file_size" value="<?php echo $bytes; ?>" />
1019 </p>
1020 <p class="submit">
1021 <input type="submit" class="button" value="<?php _e( 'Upload file and import' ); ?>" />
1022 </p>
1023 </form>
1024 <?php
1025 }
1026
1027 function wp_remember_old_slug() {
1028         global $post;
1029         $name = attribute_escape($post->post_name); // just in case
1030         if ( strlen($name) )
1031                 echo '<input type="hidden" id="wp-old-slug" name="wp-old-slug" value="' . $name . '" />';
1032 }
1033
1034 /**
1035  * add_meta_box() - Add a meta box to an edit form
1036  *
1037  * @since 2.5
1038  *
1039  * @param string $id String for use in the 'id' attribute of tags.
1040  * @param string $title Title of the meta box
1041  * @param string $callback Function that fills the box with the desired content.  The function should echo its output.
1042  * @param string $page The type of edit page on which to show the box (post, page, link)
1043  * @param string $context The context within the page where the boxes should show ('normal', 'advanced')
1044  */
1045 function add_meta_box($id, $title, $callback, $page, $context = 'advanced') {
1046         global $wp_meta_boxes;
1047
1048         if  ( !isset($wp_meta_boxes) )
1049                 $wp_meta_boxes = array();
1050         if ( !isset($wp_meta_boxes[$page]) )
1051                 $wp_meta_boxes[$page] = array();
1052         if ( !isset($wp_meta_boxes[$page][$context]) )
1053                 $wp_meta_boxes[$page][$context] = array();
1054
1055         $wp_meta_boxes[$page][$context][$id] = array('id' => $id, 'title' => $title, 'callback' => $callback);
1056 }
1057
1058 function do_meta_boxes($page, $context, $object) {
1059         global $wp_meta_boxes;
1060
1061         if ( !isset($wp_meta_boxes) || !isset($wp_meta_boxes[$page]) || !isset($wp_meta_boxes[$page][$context]) )
1062                 return;
1063
1064         foreach ( (array) $wp_meta_boxes[$page][$context] as $box ) {
1065                 echo '<div id="' . $box['id'] . '" class="postbox ' . postbox_classes($box['id'], $page) . '">' . "\n";
1066                 echo "<h3>{$box['title']}</h3>\n";
1067                 echo '<div class="inside">' . "\n";
1068                 call_user_func($box['callback'], $object, $box);
1069                 echo "</div>\n";
1070                 echo "</div>\n";
1071         }
1072 }
1073
1074 ?>