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