]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/template.php
Wordpress 3.1
[autoinstalls/wordpress.git] / wp-admin / includes / template.php
1 <?php
2 /**
3  * Template WordPress Administration API.
4  *
5  * A Big Mess. Also some neat functions that are nicely written.
6  *
7  * @package WordPress
8  * @subpackage Administration
9  */
10
11
12 //
13 // Category Checklists
14 //
15
16 /**
17  * {@internal Missing Short Description}}
18  *
19  * @since 2.5.1
20  */
21 class Walker_Category_Checklist extends Walker {
22         var $tree_type = 'category';
23         var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
24
25         function start_lvl(&$output, $depth, $args) {
26                 $indent = str_repeat("\t", $depth);
27                 $output .= "$indent<ul class='children'>\n";
28         }
29
30         function end_lvl(&$output, $depth, $args) {
31                 $indent = str_repeat("\t", $depth);
32                 $output .= "$indent</ul>\n";
33         }
34
35         function start_el(&$output, $category, $depth, $args) {
36                 extract($args);
37                 if ( empty($taxonomy) )
38                         $taxonomy = 'category';
39
40                 if ( $taxonomy == 'category' )
41                         $name = 'post_category';
42                 else
43                         $name = 'tax_input['.$taxonomy.']';
44
45                 $class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : '';
46                 $output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" . '<label class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $category->term_id . '"' . checked( in_array( $category->term_id, $selected_cats ), true, false ) . disabled( empty( $args['disabled'] ), false, false ) . ' /> ' . esc_html( apply_filters('the_category', $category->name )) . '</label>';
47         }
48
49         function end_el(&$output, $category, $depth, $args) {
50                 $output .= "</li>\n";
51         }
52 }
53
54 /**
55  * {@internal Missing Short Description}}
56  *
57  * @since 2.5.1
58  *
59  * @param unknown_type $post_id
60  * @param unknown_type $descendants_and_self
61  * @param unknown_type $selected_cats
62  * @param unknown_type $popular_cats
63  */
64 function wp_category_checklist( $post_id = 0, $descendants_and_self = 0, $selected_cats = false, $popular_cats = false, $walker = null, $checked_ontop = true ) {
65         wp_terms_checklist($post_id,
66                 array(
67                         'taxonomy' => 'category',
68                         'descendants_and_self' => $descendants_and_self,
69                         'selected_cats' => $selected_cats,
70                         'popular_cats' => $popular_cats,
71                         'walker' => $walker,
72                         'checked_ontop' => $checked_ontop
73   ));
74 }
75
76 /**
77  * Taxonomy independent version of wp_category_checklist
78  *
79  * @since 3.0.0
80  *
81  * @param int $post_id
82  * @param array $args
83  */
84 function wp_terms_checklist($post_id = 0, $args = array()) {
85         $defaults = array(
86                 'descendants_and_self' => 0,
87                 'selected_cats' => false,
88                 'popular_cats' => false,
89                 'walker' => null,
90                 'taxonomy' => 'category',
91                 'checked_ontop' => true
92         );
93         extract( wp_parse_args($args, $defaults), EXTR_SKIP );
94
95         if ( empty($walker) || !is_a($walker, 'Walker') )
96                 $walker = new Walker_Category_Checklist;
97
98         $descendants_and_self = (int) $descendants_and_self;
99
100         $args = array('taxonomy' => $taxonomy);
101
102         $tax = get_taxonomy($taxonomy);
103         $args['disabled'] = !current_user_can($tax->cap->assign_terms);
104
105         if ( is_array( $selected_cats ) )
106                 $args['selected_cats'] = $selected_cats;
107         elseif ( $post_id )
108                 $args['selected_cats'] = wp_get_object_terms($post_id, $taxonomy, array_merge($args, array('fields' => 'ids')));
109         else
110                 $args['selected_cats'] = array();
111
112         if ( is_array( $popular_cats ) )
113                 $args['popular_cats'] = $popular_cats;
114         else
115                 $args['popular_cats'] = get_terms( $taxonomy, array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
116
117         if ( $descendants_and_self ) {
118                 $categories = (array) get_terms($taxonomy, array( 'child_of' => $descendants_and_self, 'hierarchical' => 0, 'hide_empty' => 0 ) );
119                 $self = get_term( $descendants_and_self, $taxonomy );
120                 array_unshift( $categories, $self );
121         } else {
122                 $categories = (array) get_terms($taxonomy, array('get' => 'all'));
123         }
124
125         if ( $checked_ontop ) {
126                 // 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)
127                 $checked_categories = array();
128                 $keys = array_keys( $categories );
129
130                 foreach( $keys as $k ) {
131                         if ( in_array( $categories[$k]->term_id, $args['selected_cats'] ) ) {
132                                 $checked_categories[] = $categories[$k];
133                                 unset( $categories[$k] );
134                         }
135                 }
136
137                 // Put checked cats on top
138                 echo call_user_func_array(array(&$walker, 'walk'), array($checked_categories, 0, $args));
139         }
140         // Then the rest of them
141         echo call_user_func_array(array(&$walker, 'walk'), array($categories, 0, $args));
142 }
143
144 /**
145  * {@internal Missing Short Description}}
146  *
147  * @since 2.5.0
148  *
149  * @param unknown_type $taxonomy
150  * @param unknown_type $default
151  * @param unknown_type $number
152  * @param unknown_type $echo
153  * @return unknown
154  */
155 function wp_popular_terms_checklist( $taxonomy, $default = 0, $number = 10, $echo = true ) {
156         global $post_ID;
157
158         if ( $post_ID )
159                 $checked_terms = wp_get_object_terms($post_ID, $taxonomy, array('fields'=>'ids'));
160         else
161                 $checked_terms = array();
162
163         $terms = get_terms( $taxonomy, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => $number, 'hierarchical' => false ) );
164
165         $tax = get_taxonomy($taxonomy);
166         if ( ! current_user_can($tax->cap->assign_terms) )
167                 $disabled = 'disabled="disabled"';
168         else
169                 $disabled = '';
170
171         $popular_ids = array();
172         foreach ( (array) $terms as $term ) {
173                 $popular_ids[] = $term->term_id;
174                 if ( !$echo ) // hack for AJAX use
175                         continue;
176                 $id = "popular-$taxonomy-$term->term_id";
177                 $checked = in_array( $term->term_id, $checked_terms ) ? 'checked="checked"' : '';
178                 ?>
179
180                 <li id="<?php echo $id; ?>" class="popular-category">
181                         <label class="selectit">
182                         <input id="in-<?php echo $id; ?>" type="checkbox" <?php echo $checked; ?> value="<?php echo (int) $term->term_id; ?>" <?php echo $disabled ?>/>
183                                 <?php echo esc_html( apply_filters( 'the_category', $term->name ) ); ?>
184                         </label>
185                 </li>
186
187                 <?php
188         }
189         return $popular_ids;
190 }
191
192 /**
193  * {@internal Missing Short Description}}
194  *
195  * @since 2.5.1
196  *
197  * @param unknown_type $link_id
198  */
199 function wp_link_category_checklist( $link_id = 0 ) {
200         $default = 1;
201
202         if ( $link_id ) {
203                 $checked_categories = wp_get_link_cats( $link_id );
204                 // No selected categories, strange
205                 if ( ! count( $checked_categories ) )
206                         $checked_categories[] = $default;
207         } else {
208                 $checked_categories[] = $default;
209         }
210
211         $categories = get_terms( 'link_category', array( 'orderby' => 'name', 'hide_empty' => 0 ) );
212
213         if ( empty( $categories ) )
214                 return;
215
216         foreach ( $categories as $category ) {
217                 $cat_id = $category->term_id;
218                 $name = esc_html( apply_filters( 'the_category', $category->name ) );
219                 $checked = in_array( $cat_id, $checked_categories ) ? ' checked="checked"' : '';
220                 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, '/> ', $name, "</label></li>";
221         }
222 }
223
224 /**
225  * Get the column headers for a screen
226  *
227  * @since 2.7.0
228  *
229  * @param string|object $screen The screen you want the headers for
230  * @return array Containing the headers in the format id => UI String
231  */
232 function get_column_headers( $screen ) {
233         if ( is_string( $screen ) )
234                 $screen = convert_to_screen( $screen );
235
236         global $_wp_column_headers;
237
238         if ( !isset( $_wp_column_headers[ $screen->id ] ) ) {
239                 $_wp_column_headers[ $screen->id ] = apply_filters( 'manage_' . $screen->id . '_columns', array() );
240         }
241
242         return $_wp_column_headers[ $screen->id ];
243 }
244
245 /**
246  * Get a list of hidden columns.
247  *
248  * @since 2.7.0
249  *
250  * @param string|object $screen The screen you want the hidden columns for
251  * @return array
252  */
253 function get_hidden_columns( $screen ) {
254         if ( is_string( $screen ) )
255                 $screen = convert_to_screen( $screen );
256
257         return (array) get_user_option( 'manage' . $screen->id . 'columnshidden' );
258 }
259
260 // adds hidden fields with the data for use in the inline editor for posts and pages
261 /**
262  * {@internal Missing Short Description}}
263  *
264  * @since 2.7.0
265  *
266  * @param unknown_type $post
267  */
268 function get_inline_data($post) {
269         $post_type_object = get_post_type_object($post->post_type);
270         if ( ! current_user_can($post_type_object->cap->edit_post, $post->ID) )
271                 return;
272
273         $title = esc_textarea( trim( $post->post_title ) );
274
275         echo '
276 <div class="hidden" id="inline_' . $post->ID . '">
277         <div class="post_title">' . $title . '</div>
278         <div class="post_name">' . apply_filters('editable_slug', $post->post_name) . '</div>
279         <div class="post_author">' . $post->post_author . '</div>
280         <div class="comment_status">' . esc_html( $post->comment_status ) . '</div>
281         <div class="ping_status">' . esc_html( $post->ping_status ) . '</div>
282         <div class="_status">' . esc_html( $post->post_status ) . '</div>
283         <div class="jj">' . mysql2date( 'd', $post->post_date, false ) . '</div>
284         <div class="mm">' . mysql2date( 'm', $post->post_date, false ) . '</div>
285         <div class="aa">' . mysql2date( 'Y', $post->post_date, false ) . '</div>
286         <div class="hh">' . mysql2date( 'H', $post->post_date, false ) . '</div>
287         <div class="mn">' . mysql2date( 'i', $post->post_date, false ) . '</div>
288         <div class="ss">' . mysql2date( 's', $post->post_date, false ) . '</div>
289         <div class="post_password">' . esc_html( $post->post_password ) . '</div>';
290
291         if ( $post_type_object->hierarchical )
292                 echo '<div class="post_parent">' . $post->post_parent . '</div>';
293
294         if ( $post->post_type == 'page' )
295                 echo '<div class="page_template">' . esc_html( get_post_meta( $post->ID, '_wp_page_template', true ) ) . '</div>';
296
297         if ( $post_type_object->hierarchical )
298                 echo '<div class="menu_order">' . $post->menu_order . '</div>';
299
300         $taxonomy_names = get_object_taxonomies( $post->post_type );
301         foreach ( $taxonomy_names as $taxonomy_name) {
302                 $taxonomy = get_taxonomy( $taxonomy_name );
303
304                 if ( $taxonomy->hierarchical && $taxonomy->show_ui )
305                                 echo '<div class="post_category" id="'.$taxonomy_name.'_'.$post->ID.'">' . implode( ',', wp_get_object_terms( $post->ID, $taxonomy_name, array('fields'=>'ids')) ) . '</div>';
306                 elseif ( $taxonomy->show_ui )
307                         echo '<div class="tags_input" id="'.$taxonomy_name.'_'.$post->ID.'">' . esc_html( str_replace( ',', ', ', get_terms_to_edit($post->ID, $taxonomy_name) ) ) . '</div>';
308         }
309
310         if ( !$post_type_object->hierarchical )
311                 echo '<div class="sticky">' . (is_sticky($post->ID) ? 'sticky' : '') . '</div>';
312
313         echo '</div>';
314 }
315
316 /**
317  * {@internal Missing Short Description}}
318  *
319  * @since 2.7.0
320  *
321  * @param unknown_type $position
322  * @param unknown_type $checkbox
323  * @param unknown_type $mode
324  */
325 function wp_comment_reply($position = '1', $checkbox = false, $mode = 'single', $table_row = true) {
326         // allow plugin to replace the popup content
327         $content = apply_filters( 'wp_comment_reply', '', array('position' => $position, 'checkbox' => $checkbox, 'mode' => $mode) );
328
329         if ( ! empty($content) ) {
330                 echo $content;
331                 return;
332         }
333
334         if ( $mode == 'single' ) {
335                 $wp_list_table = _get_list_table('WP_Post_Comments_List_Table');
336         } else {
337                 $wp_list_table = _get_list_table('WP_Comments_List_Table');
338         }
339
340 ?>
341 <form method="get" action="">
342 <?php if ( $table_row ) : ?>
343 <table style="display:none;"><tbody id="com-reply"><tr id="replyrow" style="display:none;"><td colspan="<?php echo $wp_list_table->get_column_count(); ?>" class="colspanchange">
344 <?php else : ?>
345 <div id="com-reply" style="display:none;"><div id="replyrow" style="display:none;">
346 <?php endif; ?>
347         <div id="replyhead" style="display:none;"><?php _e('Reply to Comment'); ?></div>
348
349         <div id="edithead" style="display:none;">
350                 <div class="inside">
351                 <label for="author"><?php _e('Name') ?></label>
352                 <input type="text" name="newcomment_author" size="50" value="" tabindex="101" id="author" />
353                 </div>
354
355                 <div class="inside">
356                 <label for="author-email"><?php _e('E-mail') ?></label>
357                 <input type="text" name="newcomment_author_email" size="50" value="" tabindex="102" id="author-email" />
358                 </div>
359
360                 <div class="inside">
361                 <label for="author-url"><?php _e('URL') ?></label>
362                 <input type="text" id="author-url" name="newcomment_author_url" size="103" value="" tabindex="103" />
363                 </div>
364                 <div style="clear:both;"></div>
365         </div>
366
367         <div id="replycontainer"><textarea rows="8" cols="40" name="replycontent" tabindex="104" id="replycontent"></textarea></div>
368
369         <p id="replysubmit" class="submit">
370         <a href="#comments-form" class="cancel button-secondary alignleft" tabindex="106"><?php _e('Cancel'); ?></a>
371         <a href="#comments-form" class="save button-primary alignright" tabindex="104">
372         <span id="savebtn" style="display:none;"><?php _e('Update Comment'); ?></span>
373         <span id="replybtn" style="display:none;"><?php _e('Submit Reply'); ?></span></a>
374         <img class="waiting" style="display:none;" src="<?php echo esc_url( admin_url( 'images/wpspin_light.gif' ) ); ?>" alt="" />
375         <span class="error" style="display:none;"></span>
376         <br class="clear" />
377         </p>
378
379         <input type="hidden" name="user_ID" id="user_ID" value="<?php echo get_current_user_id(); ?>" />
380         <input type="hidden" name="action" id="action" value="" />
381         <input type="hidden" name="comment_ID" id="comment_ID" value="" />
382         <input type="hidden" name="comment_post_ID" id="comment_post_ID" value="" />
383         <input type="hidden" name="status" id="status" value="" />
384         <input type="hidden" name="position" id="position" value="<?php echo $position; ?>" />
385         <input type="hidden" name="checkbox" id="checkbox" value="<?php echo $checkbox ? 1 : 0; ?>" />
386         <input type="hidden" name="mode" id="mode" value="<?php echo esc_attr($mode); ?>" />
387         <?php wp_nonce_field( 'replyto-comment', '_ajax_nonce-replyto-comment', false ); ?>
388         <?php wp_comment_form_unfiltered_html_nonce(); ?>
389 <?php if ( $table_row ) : ?>
390 </td></tr></tbody></table>
391 <?php else : ?>
392 </div></div>
393 <?php endif; ?>
394 </form>
395 <?php
396 }
397
398 /**
399  * Output 'undo move to trash' text for comments
400  *
401  * @since 2.9.0
402  */
403 function wp_comment_trashnotice() {
404 ?>
405 <div class="hidden" id="trash-undo-holder">
406         <div class="trash-undo-inside"><?php printf(__('Comment by %s moved to the trash.'), '<strong></strong>'); ?> <span class="undo untrash"><a href="#"><?php _e('Undo'); ?></a></span></div>
407 </div>
408 <div class="hidden" id="spam-undo-holder">
409         <div class="spam-undo-inside"><?php printf(__('Comment by %s marked as spam.'), '<strong></strong>'); ?> <span class="undo unspam"><a href="#"><?php _e('Undo'); ?></a></span></div>
410 </div>
411 <?php
412 }
413
414 /**
415  * {@internal Missing Short Description}}
416  *
417  * @since 1.2.0
418  *
419  * @param unknown_type $meta
420  */
421 function list_meta( $meta ) {
422         // Exit if no meta
423         if ( ! $meta ) {
424                 echo '
425 <table id="list-table" style="display: none;">
426         <thead>
427         <tr>
428                 <th class="left">' . __( 'Name' ) . '</th>
429                 <th>' . __( 'Value' ) . '</th>
430         </tr>
431         </thead>
432         <tbody id="the-list" class="list:meta">
433         <tr><td></td></tr>
434         </tbody>
435 </table>'; //TBODY needed for list-manipulation JS
436                 return;
437         }
438         $count = 0;
439 ?>
440 <table id="list-table">
441         <thead>
442         <tr>
443                 <th class="left"><?php _e( 'Name' ) ?></th>
444                 <th><?php _e( 'Value' ) ?></th>
445         </tr>
446         </thead>
447         <tbody id='the-list' class='list:meta'>
448 <?php
449         foreach ( $meta as $entry )
450                 echo _list_meta_row( $entry, $count );
451 ?>
452         </tbody>
453 </table>
454 <?php
455 }
456
457 /**
458  * {@internal Missing Short Description}}
459  *
460  * @since 2.5.0
461  *
462  * @param unknown_type $entry
463  * @param unknown_type $count
464  * @return unknown
465  */
466 function _list_meta_row( $entry, &$count ) {
467         static $update_nonce = false;
468         if ( !$update_nonce )
469                 $update_nonce = wp_create_nonce( 'add-meta' );
470
471         $r = '';
472         ++ $count;
473         if ( $count % 2 )
474                 $style = 'alternate';
475         else
476                 $style = '';
477         if ('_' == $entry['meta_key'] { 0 } )
478                 $style .= ' hidden';
479
480         if ( is_serialized( $entry['meta_value'] ) ) {
481                 if ( is_serialized_string( $entry['meta_value'] ) ) {
482                         // this is a serialized string, so we should display it
483                         $entry['meta_value'] = maybe_unserialize( $entry['meta_value'] );
484                 } else {
485                         // this is a serialized array/object so we should NOT display it
486                         --$count;
487                         return;
488                 }
489         }
490
491         $entry['meta_key'] = esc_attr($entry['meta_key']);
492         $entry['meta_value'] = esc_textarea( $entry['meta_value'] ); // using a <textarea />
493         $entry['meta_id'] = (int) $entry['meta_id'];
494
495         $delete_nonce = wp_create_nonce( 'delete-meta_' . $entry['meta_id'] );
496
497         $r .= "\n\t<tr id='meta-{$entry['meta_id']}' class='$style'>";
498         $r .= "\n\t\t<td class='left'><label class='screen-reader-text' 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']}' />";
499
500         $r .= "\n\t\t<div class='submit'>";
501         $r .= get_submit_button( __( 'Delete' ), "delete:the-list:meta-{$entry['meta_id']}::_ajax_nonce=$delete_nonce deletemeta", "deletemeta[{$entry['meta_id']}]", false, array( 'tabindex' => '6' ) );
502         $r .= "\n\t\t";
503         $r .= get_submit_button( __( 'Update' ), "add:the-list:meta-{$entry['meta_id']}::_ajax_nonce-add-meta=$update_nonce updatemeta" , 'updatemeta', false, array( 'tabindex' => '6' ) );
504         $r .= "</div>";
505         $r .= wp_nonce_field( 'change-meta', '_ajax_nonce', false, false );
506         $r .= "</td>";
507
508         $r .= "\n\t\t<td><label class='screen-reader-text' 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>\n\t</tr>";
509         return $r;
510 }
511
512 /**
513  * {@internal Missing Short Description}}
514  *
515  * @since 1.2.0
516  */
517 function meta_form() {
518         global $wpdb;
519         $limit = (int) apply_filters( 'postmeta_form_limit', 30 );
520         $keys = $wpdb->get_col( "
521                 SELECT meta_key
522                 FROM $wpdb->postmeta
523                 GROUP BY meta_key
524                 HAVING meta_key NOT LIKE '\_%'
525                 ORDER BY meta_key
526                 LIMIT $limit" );
527         if ( $keys )
528                 natcasesort($keys);
529 ?>
530 <p><strong><?php _e( 'Add New Custom Field:' ) ?></strong></p>
531 <table id="newmeta">
532 <thead>
533 <tr>
534 <th class="left"><label for="metakeyselect"><?php _e( 'Name' ) ?></label></th>
535 <th><label for="metavalue"><?php _e( 'Value' ) ?></label></th>
536 </tr>
537 </thead>
538
539 <tbody>
540 <tr>
541 <td id="newmetaleft" class="left">
542 <?php if ( $keys ) { ?>
543 <select id="metakeyselect" name="metakeyselect" tabindex="7">
544 <option value="#NONE#"><?php _e( '&mdash; Select &mdash;' ); ?></option>
545 <?php
546
547         foreach ( $keys as $key ) {
548                 echo "\n<option value='" . esc_attr($key) . "'>" . esc_html($key) . "</option>";
549         }
550 ?>
551 </select>
552 <input class="hide-if-js" type="text" id="metakeyinput" name="metakeyinput" tabindex="7" value="" />
553 <a href="#postcustomstuff" class="hide-if-no-js" onclick="jQuery('#metakeyinput, #metakeyselect, #enternew, #cancelnew').toggle();return false;">
554 <span id="enternew"><?php _e('Enter new'); ?></span>
555 <span id="cancelnew" class="hidden"><?php _e('Cancel'); ?></span></a>
556 <?php } else { ?>
557 <input type="text" id="metakeyinput" name="metakeyinput" tabindex="7" value="" />
558 <?php } ?>
559 </td>
560 <td><textarea id="metavalue" name="metavalue" rows="2" cols="25" tabindex="8"></textarea></td>
561 </tr>
562
563 <tr><td colspan="2" class="submit">
564 <?php submit_button( __( 'Add Custom Field' ), 'add:the-list:newmeta', 'addmeta', false, array( 'id' => 'addmetasub', 'tabindex' => '9' ) ); ?>
565 <?php wp_nonce_field( 'add-meta', '_ajax_nonce-add-meta', false ); ?>
566 </td></tr>
567 </tbody>
568 </table>
569 <?php
570
571 }
572
573 /**
574  * {@internal Missing Short Description}}
575  *
576  * @since 0.71
577  *
578  * @param unknown_type $edit
579  * @param unknown_type $for_post
580  * @param unknown_type $tab_index
581  * @param unknown_type $multi
582  */
583 function touch_time( $edit = 1, $for_post = 1, $tab_index = 0, $multi = 0 ) {
584         global $wp_locale, $post, $comment;
585
586         if ( $for_post )
587                 $edit = ! ( in_array($post->post_status, array('draft', 'pending') ) && (!$post->post_date_gmt || '0000-00-00 00:00:00' == $post->post_date_gmt ) );
588
589         $tab_index_attribute = '';
590         if ( (int) $tab_index > 0 )
591                 $tab_index_attribute = " tabindex=\"$tab_index\"";
592
593         // 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 />';
594
595         $time_adj = current_time('timestamp');
596         $post_date = ($for_post) ? $post->post_date : $comment->comment_date;
597         $jj = ($edit) ? mysql2date( 'd', $post_date, false ) : gmdate( 'd', $time_adj );
598         $mm = ($edit) ? mysql2date( 'm', $post_date, false ) : gmdate( 'm', $time_adj );
599         $aa = ($edit) ? mysql2date( 'Y', $post_date, false ) : gmdate( 'Y', $time_adj );
600         $hh = ($edit) ? mysql2date( 'H', $post_date, false ) : gmdate( 'H', $time_adj );
601         $mn = ($edit) ? mysql2date( 'i', $post_date, false ) : gmdate( 'i', $time_adj );
602         $ss = ($edit) ? mysql2date( 's', $post_date, false ) : gmdate( 's', $time_adj );
603
604         $cur_jj = gmdate( 'd', $time_adj );
605         $cur_mm = gmdate( 'm', $time_adj );
606         $cur_aa = gmdate( 'Y', $time_adj );
607         $cur_hh = gmdate( 'H', $time_adj );
608         $cur_mn = gmdate( 'i', $time_adj );
609
610         $month = "<select " . ( $multi ? '' : 'id="mm" ' ) . "name=\"mm\"$tab_index_attribute>\n";
611         for ( $i = 1; $i < 13; $i = $i +1 ) {
612                 $month .= "\t\t\t" . '<option value="' . zeroise($i, 2) . '"';
613                 if ( $i == $mm )
614                         $month .= ' selected="selected"';
615                 $month .= '>' . $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) ) . "</option>\n";
616         }
617         $month .= '</select>';
618
619         $day = '<input type="text" ' . ( $multi ? '' : 'id="jj" ' ) . 'name="jj" value="' . $jj . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />';
620         $year = '<input type="text" ' . ( $multi ? '' : 'id="aa" ' ) . 'name="aa" value="' . $aa . '" size="4" maxlength="4"' . $tab_index_attribute . ' autocomplete="off" />';
621         $hour = '<input type="text" ' . ( $multi ? '' : 'id="hh" ' ) . 'name="hh" value="' . $hh . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />';
622         $minute = '<input type="text" ' . ( $multi ? '' : 'id="mn" ' ) . 'name="mn" value="' . $mn . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />';
623
624         echo '<div class="timestamp-wrap">';
625         /* translators: 1: month input, 2: day input, 3: year input, 4: hour input, 5: minute input */
626         printf(__('%1$s%2$s, %3$s @ %4$s : %5$s'), $month, $day, $year, $hour, $minute);
627
628         echo '</div><input type="hidden" id="ss" name="ss" value="' . $ss . '" />';
629
630         if ( $multi ) return;
631
632         echo "\n\n";
633         foreach ( array('mm', 'jj', 'aa', 'hh', 'mn') as $timeunit ) {
634                 echo '<input type="hidden" id="hidden_' . $timeunit . '" name="hidden_' . $timeunit . '" value="' . $$timeunit . '" />' . "\n";
635                 $cur_timeunit = 'cur_' . $timeunit;
636                 echo '<input type="hidden" id="'. $cur_timeunit . '" name="'. $cur_timeunit . '" value="' . $$cur_timeunit . '" />' . "\n";
637         }
638 ?>
639
640 <p>
641 <a href="#edit_timestamp" class="save-timestamp hide-if-no-js button"><?php _e('OK'); ?></a>
642 <a href="#edit_timestamp" class="cancel-timestamp hide-if-no-js"><?php _e('Cancel'); ?></a>
643 </p>
644 <?php
645 }
646
647 /**
648  * {@internal Missing Short Description}}
649  *
650  * @since 1.5.0
651  *
652  * @param unknown_type $default
653  */
654 function page_template_dropdown( $default = '' ) {
655         $templates = get_page_templates();
656         ksort( $templates );
657         foreach (array_keys( $templates ) as $template )
658                 : if ( $default == $templates[$template] )
659                         $selected = " selected='selected'";
660                 else
661                         $selected = '';
662         echo "\n\t<option value='".$templates[$template]."' $selected>$template</option>";
663         endforeach;
664 }
665
666 /**
667  * {@internal Missing Short Description}}
668  *
669  * @since 1.5.0
670  *
671  * @param unknown_type $default
672  * @param unknown_type $parent
673  * @param unknown_type $level
674  * @return unknown
675  */
676 function parent_dropdown( $default = 0, $parent = 0, $level = 0 ) {
677         global $wpdb, $post_ID;
678         $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) );
679
680         if ( $items ) {
681                 foreach ( $items as $item ) {
682                         // A page cannot be its own parent.
683                         if (!empty ( $post_ID ) ) {
684                                 if ( $item->ID == $post_ID ) {
685                                         continue;
686                                 }
687                         }
688                         $pad = str_repeat( '&nbsp;', $level * 3 );
689                         if ( $item->ID == $default)
690                                 $current = ' selected="selected"';
691                         else
692                                 $current = '';
693
694                         echo "\n\t<option class='level-$level' value='$item->ID'$current>$pad " . esc_html($item->post_title) . "</option>";
695                         parent_dropdown( $default, $item->ID, $level +1 );
696                 }
697         } else {
698                 return false;
699         }
700 }
701
702 /**
703  * {@internal Missing Short Description}}
704  *
705  * @since 2.0.0
706  *
707  * @param unknown_type $id
708  * @return unknown
709  */
710 function the_attachment_links( $id = false ) {
711         $id = (int) $id;
712         $post = & get_post( $id );
713
714         if ( $post->post_type != 'attachment' )
715                 return false;
716
717         $icon = wp_get_attachment_image( $post->ID, 'thumbnail', true );
718         $attachment_data = wp_get_attachment_metadata( $id );
719         $thumb = isset( $attachment_data['thumb'] );
720 ?>
721 <form id="the-attachment-links">
722 <table>
723         <col />
724         <col class="widefat" />
725         <tr>
726                 <th scope="row"><?php _e( 'URL' ) ?></th>
727                 <td><textarea rows="1" cols="40" type="text" class="attachmentlinks" readonly="readonly"><?php echo esc_textarea( wp_get_attachment_url() ); ?></textarea></td>
728         </tr>
729 <?php if ( $icon ) : ?>
730         <tr>
731                 <th scope="row"><?php $thumb ? _e( 'Thumbnail linked to file' ) : _e( 'Image linked to file' ); ?></th>
732                 <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>
733         </tr>
734         <tr>
735                 <th scope="row"><?php $thumb ? _e( 'Thumbnail linked to page' ) : _e( 'Image linked to page' ); ?></th>
736                 <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>
737         </tr>
738 <?php else : ?>
739         <tr>
740                 <th scope="row"><?php _e( 'Link to file' ) ?></th>
741                 <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>
742         </tr>
743         <tr>
744                 <th scope="row"><?php _e( 'Link to page' ) ?></th>
745                 <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>
746         </tr>
747 <?php endif; ?>
748 </table>
749 </form>
750 <?php
751 }
752
753
754 /**
755  * Print out <option> html elements for role selectors
756  *
757  * @since 2.1.0
758  *
759  * @param string $selected slug for the role that should be already selected
760  */
761 function wp_dropdown_roles( $selected = false ) {
762         $p = '';
763         $r = '';
764
765         $editable_roles = get_editable_roles();
766
767         foreach ( $editable_roles as $role => $details ) {
768                 $name = translate_user_role($details['name'] );
769                 if ( $selected == $role ) // preselect specified role
770                         $p = "\n\t<option selected='selected' value='" . esc_attr($role) . "'>$name</option>";
771                 else
772                         $r .= "\n\t<option value='" . esc_attr($role) . "'>$name</option>";
773         }
774         echo $p . $r;
775 }
776
777 /**
778  * {@internal Missing Short Description}}
779  *
780  * @since 2.3.0
781  *
782  * @param unknown_type $size
783  * @return unknown
784  */
785 function wp_convert_hr_to_bytes( $size ) {
786         $size = strtolower($size);
787         $bytes = (int) $size;
788         if ( strpos($size, 'k') !== false )
789                 $bytes = intval($size) * 1024;
790         elseif ( strpos($size, 'm') !== false )
791                 $bytes = intval($size) * 1024 * 1024;
792         elseif ( strpos($size, 'g') !== false )
793                 $bytes = intval($size) * 1024 * 1024 * 1024;
794         return $bytes;
795 }
796
797 /**
798  * {@internal Missing Short Description}}
799  *
800  * @since 2.3.0
801  *
802  * @param unknown_type $bytes
803  * @return unknown
804  */
805 function wp_convert_bytes_to_hr( $bytes ) {
806         $units = array( 0 => 'B', 1 => 'kB', 2 => 'MB', 3 => 'GB' );
807         $log = log( $bytes, 1024 );
808         $power = (int) $log;
809         $size = pow(1024, $log - $power);
810         return $size . $units[$power];
811 }
812
813 /**
814  * {@internal Missing Short Description}}
815  *
816  * @since 2.5.0
817  *
818  * @return unknown
819  */
820 function wp_max_upload_size() {
821         $u_bytes = wp_convert_hr_to_bytes( ini_get( 'upload_max_filesize' ) );
822         $p_bytes = wp_convert_hr_to_bytes( ini_get( 'post_max_size' ) );
823         $bytes = apply_filters( 'upload_size_limit', min($u_bytes, $p_bytes), $u_bytes, $p_bytes );
824         return $bytes;
825 }
826
827 /**
828  * Outputs the form used by the importers to accept the data to be imported
829  *
830  * @since 2.0.0
831  *
832  * @param string $action The action attribute for the form.
833  */
834 function wp_import_upload_form( $action ) {
835         $bytes = apply_filters( 'import_upload_size_limit', wp_max_upload_size() );
836         $size = wp_convert_bytes_to_hr( $bytes );
837         $upload_dir = wp_upload_dir();
838         if ( ! empty( $upload_dir['error'] ) ) :
839                 ?><div class="error"><p><?php _e('Before you can upload your import file, you will need to fix the following error:'); ?></p>
840                 <p><strong><?php echo $upload_dir['error']; ?></strong></p></div><?php
841         else :
842 ?>
843 <form enctype="multipart/form-data" id="import-upload-form" method="post" action="<?php echo esc_attr(wp_nonce_url($action, 'import-upload')); ?>">
844 <p>
845 <label for="upload"><?php _e( 'Choose a file from your computer:' ); ?></label> (<?php printf( __('Maximum size: %s' ), $size ); ?>)
846 <input type="file" id="upload" name="import" size="25" />
847 <input type="hidden" name="action" value="save" />
848 <input type="hidden" name="max_file_size" value="<?php echo $bytes; ?>" />
849 </p>
850 <?php submit_button( __('Upload file and import'), 'button' ); ?>
851 </form>
852 <?php
853         endif;
854 }
855
856 /**
857  * Add a meta box to an edit form.
858  *
859  * @since 2.5.0
860  *
861  * @param string $id String for use in the 'id' attribute of tags.
862  * @param string $title Title of the meta box.
863  * @param string $callback Function that fills the box with the desired content. The function should echo its output.
864  * @param string $page The type of edit page on which to show the box (post, page, link).
865  * @param string $context The context within the page where the boxes should show ('normal', 'advanced').
866  * @param string $priority The priority within the context where the boxes should show ('high', 'low').
867  */
868 function add_meta_box($id, $title, $callback, $page, $context = 'advanced', $priority = 'default', $callback_args=null) {
869         global $wp_meta_boxes;
870
871         if ( !isset($wp_meta_boxes) )
872                 $wp_meta_boxes = array();
873         if ( !isset($wp_meta_boxes[$page]) )
874                 $wp_meta_boxes[$page] = array();
875         if ( !isset($wp_meta_boxes[$page][$context]) )
876                 $wp_meta_boxes[$page][$context] = array();
877
878         foreach ( array_keys($wp_meta_boxes[$page]) as $a_context ) {
879                 foreach ( array('high', 'core', 'default', 'low') as $a_priority ) {
880                         if ( !isset($wp_meta_boxes[$page][$a_context][$a_priority][$id]) )
881                                 continue;
882
883                         // If a core box was previously added or removed by a plugin, don't add.
884                         if ( 'core' == $priority ) {
885                                 // If core box previously deleted, don't add
886                                 if ( false === $wp_meta_boxes[$page][$a_context][$a_priority][$id] )
887                                         return;
888                                 // If box was added with default priority, give it core priority to maintain sort order
889                                 if ( 'default' == $a_priority ) {
890                                         $wp_meta_boxes[$page][$a_context]['core'][$id] = $wp_meta_boxes[$page][$a_context]['default'][$id];
891                                         unset($wp_meta_boxes[$page][$a_context]['default'][$id]);
892                                 }
893                                 return;
894                         }
895                         // If no priority given and id already present, use existing priority
896                         if ( empty($priority) ) {
897                                 $priority = $a_priority;
898                         // else if we're adding to the sorted priortiy, we don't know the title or callback. Glab them from the previously added context/priority.
899                         } elseif ( 'sorted' == $priority ) {
900                                 $title = $wp_meta_boxes[$page][$a_context][$a_priority][$id]['title'];
901                                 $callback = $wp_meta_boxes[$page][$a_context][$a_priority][$id]['callback'];
902                                 $callback_args = $wp_meta_boxes[$page][$a_context][$a_priority][$id]['args'];
903                         }
904                         // An id can be in only one priority and one context
905                         if ( $priority != $a_priority || $context != $a_context )
906                                 unset($wp_meta_boxes[$page][$a_context][$a_priority][$id]);
907                 }
908         }
909
910         if ( empty($priority) )
911                 $priority = 'low';
912
913         if ( !isset($wp_meta_boxes[$page][$context][$priority]) )
914                 $wp_meta_boxes[$page][$context][$priority] = array();
915
916         $wp_meta_boxes[$page][$context][$priority][$id] = array('id' => $id, 'title' => $title, 'callback' => $callback, 'args' => $callback_args);
917 }
918
919 /**
920  * Meta-Box template function
921  *
922  * @since 2.5.0
923  *
924  * @param string $page page identifier, also known as screen identifier
925  * @param string $context box context
926  * @param mixed $object gets passed to the box callback function as first parameter
927  * @return int number of meta_boxes
928  */
929 function do_meta_boxes($page, $context, $object) {
930         global $wp_meta_boxes;
931         static $already_sorted = false;
932
933         $hidden = get_hidden_meta_boxes($page);
934
935         printf('<div id="%s-sortables" class="meta-box-sortables">', htmlspecialchars($context));
936
937         $i = 0;
938         do {
939                 // Grab the ones the user has manually sorted. Pull them out of their previous context/priority and into the one the user chose
940                 if ( !$already_sorted && $sorted = get_user_option( "meta-box-order_$page" ) ) {
941                         foreach ( $sorted as $box_context => $ids )
942                                 foreach ( explode(',', $ids) as $id )
943                                         if ( $id )
944                                                 add_meta_box( $id, null, null, $page, $box_context, 'sorted' );
945                 }
946                 $already_sorted = true;
947
948                 if ( !isset($wp_meta_boxes) || !isset($wp_meta_boxes[$page]) || !isset($wp_meta_boxes[$page][$context]) )
949                         break;
950
951                 foreach ( array('high', 'sorted', 'core', 'default', 'low') as $priority ) {
952                         if ( isset($wp_meta_boxes[$page][$context][$priority]) ) {
953                                 foreach ( (array) $wp_meta_boxes[$page][$context][$priority] as $box ) {
954                                         if ( false == $box || ! $box['title'] )
955                                                 continue;
956                                         $i++;
957                                         $style = '';
958                                         $hidden_class = in_array($box['id'], $hidden) ? ' hide-if-js' : '';
959                                         echo '<div id="' . $box['id'] . '" class="postbox ' . postbox_classes($box['id'], $page) . $hidden_class . '" ' . '>' . "\n";
960                                         echo '<div class="handlediv" title="' . esc_attr__('Click to toggle') . '"><br /></div>';
961                                         echo "<h3 class='hndle'><span>{$box['title']}</span></h3>\n";
962                                         echo '<div class="inside">' . "\n";
963                                         call_user_func($box['callback'], $object, $box);
964                                         echo "</div>\n";
965                                         echo "</div>\n";
966                                 }
967                         }
968                 }
969         } while(0);
970
971         echo "</div>";
972
973         return $i;
974
975 }
976
977 /**
978  * Remove a meta box from an edit form.
979  *
980  * @since 2.6.0
981  *
982  * @param string $id String for use in the 'id' attribute of tags.
983  * @param string $page The type of edit page on which to show the box (post, page, link).
984  * @param string $context The context within the page where the boxes should show ('normal', 'advanced').
985  */
986 function remove_meta_box($id, $page, $context) {
987         global $wp_meta_boxes;
988
989         if ( !isset($wp_meta_boxes) )
990                 $wp_meta_boxes = array();
991         if ( !isset($wp_meta_boxes[$page]) )
992                 $wp_meta_boxes[$page] = array();
993         if ( !isset($wp_meta_boxes[$page][$context]) )
994                 $wp_meta_boxes[$page][$context] = array();
995
996         foreach ( array('high', 'core', 'default', 'low') as $priority )
997                 $wp_meta_boxes[$page][$context][$priority][$id] = false;
998 }
999
1000 /**
1001  * {@internal Missing Short Description}}
1002  *
1003  * @since 2.7.0
1004  *
1005  * @param unknown_type $screen
1006  */
1007 function meta_box_prefs($screen) {
1008         global $wp_meta_boxes;
1009
1010         if ( is_string($screen) )
1011                 $screen = convert_to_screen($screen);
1012
1013         if ( empty($wp_meta_boxes[$screen->id]) )
1014                 return;
1015
1016         $hidden = get_hidden_meta_boxes($screen);
1017
1018         foreach ( array_keys($wp_meta_boxes[$screen->id]) as $context ) {
1019                 foreach ( array_keys($wp_meta_boxes[$screen->id][$context]) as $priority ) {
1020                         foreach ( $wp_meta_boxes[$screen->id][$context][$priority] as $box ) {
1021                                 if ( false == $box || ! $box['title'] )
1022                                         continue;
1023                                 // Submit box cannot be hidden
1024                                 if ( 'submitdiv' == $box['id'] || 'linksubmitdiv' == $box['id'] )
1025                                         continue;
1026                                 $box_id = $box['id'];
1027                                 echo '<label for="' . $box_id . '-hide">';
1028                                 echo '<input class="hide-postbox-tog" name="' . $box_id . '-hide" type="checkbox" id="' . $box_id . '-hide" value="' . $box_id . '"' . (! in_array($box_id, $hidden) ? ' checked="checked"' : '') . ' />';
1029                                 echo "{$box['title']}</label>\n";
1030                         }
1031                 }
1032         }
1033 }
1034
1035 /**
1036  * Get Hidden Meta Boxes
1037  *
1038  * @since 2.7.0
1039  *
1040  * @param string|object $screen Screen identifier
1041  * @return array Hidden Meta Boxes
1042  */
1043 function get_hidden_meta_boxes( $screen ) {
1044         if ( is_string( $screen ) )
1045                 $screen = convert_to_screen( $screen );
1046
1047         $hidden = get_user_option( "metaboxhidden_{$screen->id}" );
1048
1049         // Hide slug boxes by default
1050         if ( !is_array( $hidden ) ) {
1051                 if ( 'post' == $screen->base || 'page' == $screen->base )
1052                         $hidden = array('slugdiv', 'trackbacksdiv', 'postcustom', 'postexcerpt', 'commentstatusdiv', 'commentsdiv', 'authordiv', 'revisionsdiv');
1053                 else
1054                         $hidden = array( 'slugdiv' );
1055                 $hidden = apply_filters('default_hidden_meta_boxes', $hidden, $screen);
1056         }
1057
1058         return $hidden;
1059 }
1060
1061 /**
1062  * Add a new section to a settings page.
1063  *
1064  * Part of the Settings API. Use this to define new settings sections for an admin page.
1065  * Show settings sections in your admin page callback function with do_settings_sections().
1066  * Add settings fields to your section with add_settings_field()
1067  *
1068  * The $callback argument should be the name of a function that echoes out any
1069  * content you want to show at the top of the settings section before the actual
1070  * fields. It can output nothing if you want.
1071  *
1072  * @since 2.7.0
1073  *
1074  * @global $wp_settings_sections Storage array of all settings sections added to admin pages
1075  *
1076  * @param string $id Slug-name to identify the section. Used in the 'id' attribute of tags.
1077  * @param string $title Formatted title of the section. Shown as the heading for the section.
1078  * @param string $callback Function that echos out any content at the top of the section (between heading and fields).
1079  * @param string $page The slug-name of the settings page on which to show the section. Built-in pages include 'general', 'reading', 'writing', 'discussion', 'media', etc. Create your own using add_options_page();
1080  */
1081 function add_settings_section($id, $title, $callback, $page) {
1082         global $wp_settings_sections;
1083
1084         if ( 'misc' == $page ) {
1085                 _deprecated_argument( __FUNCTION__, '3.0', __( 'The miscellaneous options group has been removed. Use another settings group.' ) );
1086                 $page = 'general';
1087         }
1088
1089         if ( !isset($wp_settings_sections) )
1090                 $wp_settings_sections = array();
1091         if ( !isset($wp_settings_sections[$page]) )
1092                 $wp_settings_sections[$page] = array();
1093         if ( !isset($wp_settings_sections[$page][$id]) )
1094                 $wp_settings_sections[$page][$id] = array();
1095
1096         $wp_settings_sections[$page][$id] = array('id' => $id, 'title' => $title, 'callback' => $callback);
1097 }
1098
1099 /**
1100  * Add a new field to a section of a settings page
1101  *
1102  * Part of the Settings API. Use this to define a settings field that will show
1103  * as part of a settings section inside a settings page. The fields are shown using
1104  * do_settings_fields() in do_settings-sections()
1105  *
1106  * The $callback argument should be the name of a function that echoes out the
1107  * html input tags for this setting field. Use get_option() to retrive existing
1108  * values to show.
1109  *
1110  * @since 2.7.0
1111  *
1112  * @global $wp_settings_fields Storage array of settings fields and info about their pages/sections
1113  *
1114  * @param string $id Slug-name to identify the field. Used in the 'id' attribute of tags.
1115  * @param string $title Formatted title of the field. Shown as the label for the field during output.
1116  * @param string $callback Function that fills the field with the desired form inputs. The function should echo its output.
1117  * @param string $page The slug-name of the settings page on which to show the section (general, reading, writing, ...).
1118  * @param string $section The slug-name of the section of the settingss page in which to show the box (default, ...).
1119  * @param array $args Additional arguments
1120  */
1121 function add_settings_field($id, $title, $callback, $page, $section = 'default', $args = array()) {
1122         global $wp_settings_fields;
1123
1124         if ( 'misc' == $page ) {
1125                 _deprecated_argument( __FUNCTION__, '3.0', __( 'The miscellaneous options group has been removed. Use another settings group.' ) );
1126                 $page = 'general';
1127         }
1128
1129         if ( !isset($wp_settings_fields) )
1130                 $wp_settings_fields = array();
1131         if ( !isset($wp_settings_fields[$page]) )
1132                 $wp_settings_fields[$page] = array();
1133         if ( !isset($wp_settings_fields[$page][$section]) )
1134                 $wp_settings_fields[$page][$section] = array();
1135
1136         $wp_settings_fields[$page][$section][$id] = array('id' => $id, 'title' => $title, 'callback' => $callback, 'args' => $args);
1137 }
1138
1139 /**
1140  * Prints out all settings sections added to a particular settings page
1141  *
1142  * Part of the Settings API. Use this in a settings page callback function
1143  * to output all the sections and fields that were added to that $page with
1144  * add_settings_section() and add_settings_field()
1145  *
1146  * @global $wp_settings_sections Storage array of all settings sections added to admin pages
1147  * @global $wp_settings_fields Storage array of settings fields and info about their pages/sections
1148  * @since 2.7.0
1149  *
1150  * @param string $page The slug name of the page whos settings sections you want to output
1151  */
1152 function do_settings_sections($page) {
1153         global $wp_settings_sections, $wp_settings_fields;
1154
1155         if ( !isset($wp_settings_sections) || !isset($wp_settings_sections[$page]) )
1156                 return;
1157
1158         foreach ( (array) $wp_settings_sections[$page] as $section ) {
1159                 echo "<h3>{$section['title']}</h3>\n";
1160                 call_user_func($section['callback'], $section);
1161                 if ( !isset($wp_settings_fields) || !isset($wp_settings_fields[$page]) || !isset($wp_settings_fields[$page][$section['id']]) )
1162                         continue;
1163                 echo '<table class="form-table">';
1164                 do_settings_fields($page, $section['id']);
1165                 echo '</table>';
1166         }
1167 }
1168
1169 /**
1170  * Print out the settings fields for a particular settings section
1171  *
1172  * Part of the Settings API. Use this in a settings page to output
1173  * a specific section. Should normally be called by do_settings_sections()
1174  * rather than directly.
1175  *
1176  * @global $wp_settings_fields Storage array of settings fields and their pages/sections
1177  *
1178  * @since 2.7.0
1179  *
1180  * @param string $page Slug title of the admin page who's settings fields you want to show.
1181  * @param section $section Slug title of the settings section who's fields you want to show.
1182  */
1183 function do_settings_fields($page, $section) {
1184         global $wp_settings_fields;
1185
1186         if ( !isset($wp_settings_fields) || !isset($wp_settings_fields[$page]) || !isset($wp_settings_fields[$page][$section]) )
1187                 return;
1188
1189         foreach ( (array) $wp_settings_fields[$page][$section] as $field ) {
1190                 echo '<tr valign="top">';
1191                 if ( !empty($field['args']['label_for']) )
1192                         echo '<th scope="row"><label for="' . $field['args']['label_for'] . '">' . $field['title'] . '</label></th>';
1193                 else
1194                         echo '<th scope="row">' . $field['title'] . '</th>';
1195                 echo '<td>';
1196                 call_user_func($field['callback'], $field['args']);
1197                 echo '</td>';
1198                 echo '</tr>';
1199         }
1200 }
1201
1202 /**
1203  * Register a settings error to be displayed to the user
1204  *
1205  * Part of the Settings API. Use this to show messages to users about settings validation
1206  * problems, missing settings or anything else.
1207  *
1208  * Settings errors should be added inside the $sanitize_callback function defined in
1209  * register_setting() for a given setting to give feedback about the submission.
1210  *
1211  * By default messages will show immediately after the submission that generated the error.
1212  * Additional calls to settings_errors() can be used to show errors even when the settings
1213  * page is first accessed.
1214  *
1215  * @since 3.0.0
1216  *
1217  * @global array $wp_settings_errors Storage array of errors registered during this pageload
1218  *
1219  * @param string $setting Slug title of the setting to which this error applies
1220  * @param string $code Slug-name to identify the error. Used as part of 'id' attribute in HTML output.
1221  * @param string $message The formatted message text to display to the user (will be shown inside styled <div> and <p>)
1222  * @param string $type The type of message it is, controls HTML class. Use 'error' or 'updated'.
1223  */
1224 function add_settings_error( $setting, $code, $message, $type = 'error' ) {
1225         global $wp_settings_errors;
1226
1227         if ( !isset($wp_settings_errors) )
1228                 $wp_settings_errors = array();
1229
1230         $new_error = array(
1231                 'setting' => $setting,
1232                 'code' => $code,
1233                 'message' => $message,
1234                 'type' => $type
1235         );
1236         $wp_settings_errors[] = $new_error;
1237 }
1238
1239 /**
1240  * Fetch settings errors registered by add_settings_error()
1241  *
1242  * Checks the $wp_settings_errors array for any errors declared during the current
1243  * pageload and returns them.
1244  *
1245  * If changes were just submitted ($_GET['settings-updated']) and settings errors were saved
1246  * to the 'settings_errors' transient then those errors will be returned instead. This
1247  * is used to pass errors back across pageloads.
1248  *
1249  * Use the $sanitize argument to manually re-sanitize the option before returning errors.
1250  * This is useful if you have errors or notices you want to show even when the user
1251  * hasn't submitted data (i.e. when they first load an options page, or in admin_notices action hook)
1252  *
1253  * @since 3.0.0
1254  *
1255  * @global array $wp_settings_errors Storage array of errors registered during this pageload
1256  *
1257  * @param string $setting Optional slug title of a specific setting who's errors you want.
1258  * @param boolean $sanitize Whether to re-sanitize the setting value before returning errors.
1259  * @return array Array of settings errors
1260  */
1261 function get_settings_errors( $setting = '', $sanitize = FALSE ) {
1262         global $wp_settings_errors;
1263
1264         // If $sanitize is true, manually re-run the sanitizisation for this option
1265         // This allows the $sanitize_callback from register_setting() to run, adding
1266         // any settings errors you want to show by default.
1267         if ( $sanitize )
1268                 sanitize_option( $setting, get_option($setting));
1269
1270         // If settings were passed back from options.php then use them
1271         // Ignore transients if $sanitize is true, we dont' want the old values anyway
1272         if ( isset($_GET['settings-updated']) && $_GET['settings-updated'] && get_transient('settings_errors') ) {
1273                 $settings_errors = get_transient('settings_errors');
1274                 delete_transient('settings_errors');
1275         // Otherwise check global in case validation has been run on this pageload
1276         } elseif ( count( $wp_settings_errors ) ) {
1277                 $settings_errors = $wp_settings_errors;
1278         } else {
1279                 return;
1280         }
1281
1282         // Filter the results to those of a specific setting if one was set
1283         if ( $setting ) {
1284                 foreach ( (array) $settings_errors as $key => $details )
1285                         if ( $setting != $details['setting'] )
1286                                 unset( $settings_errors[$key] );
1287         }
1288         return $settings_errors;
1289 }
1290
1291 /**
1292  * Display settings errors registered by add_settings_error()
1293  *
1294  * Part of the Settings API. Outputs a <div> for each error retrieved by get_settings_errors().
1295  *
1296  * This is called automatically after a settings page based on the Settings API is submitted.
1297  * Errors should be added during the validation callback function for a setting defined in register_setting()
1298  *
1299  * The $sanitize option is passed into get_settings_errors() and will re-run the setting sanitization
1300  * on its current value.
1301  *
1302  * The $hide_on_update option will cause errors to only show when the settings page is first loaded.
1303  * if the user has already saved new values it will be hidden to avoid repeating messages already
1304  * shown in the default error reporting after submission. This is useful to show general errors like missing
1305  * settings when the user arrives at the settings page.
1306  *
1307  * @since 3.0.0
1308  *
1309  * @param string $setting Optional slug title of a specific setting who's errors you want.
1310  * @param boolean $sanitize Whether to re-sanitize the setting value before returning errors.
1311  * @param boolean $hide_on_update If set to true errors will not be shown if the settings page has already been submitted.
1312  */
1313 function settings_errors( $setting = '', $sanitize = FALSE, $hide_on_update = FALSE ) {
1314
1315         if ($hide_on_update AND $_GET['settings-updated']) return;
1316
1317         $settings_errors = get_settings_errors( $setting, $sanitize );
1318
1319         if ( !is_array($settings_errors) ) return;
1320
1321         $output = '';
1322         foreach ( $settings_errors as $key => $details ) {
1323                 $css_id = 'setting-error-' . $details['code'];
1324                 $css_class = $details['type'] . ' settings-error';
1325                 $output .= "<div id='$css_id' class='$css_class'> \n";
1326                 $output .= "<p><strong>{$details['message']}</strong></p>";
1327                 $output .= "</div> \n";
1328         }
1329         echo $output;
1330 }
1331
1332 /**
1333  * {@internal Missing Short Description}}
1334  *
1335  * @since 2.7.0
1336  *
1337  * @param unknown_type $found_action
1338  */
1339 function find_posts_div($found_action = '') {
1340 ?>
1341         <div id="find-posts" class="find-box" style="display:none;">
1342                 <div id="find-posts-head" class="find-box-head"><?php _e('Find Posts or Pages'); ?></div>
1343                 <div class="find-box-inside">
1344                         <div class="find-box-search">
1345                                 <?php if ( $found_action ) { ?>
1346                                         <input type="hidden" name="found_action" value="<?php echo esc_attr($found_action); ?>" />
1347                                 <?php } ?>
1348
1349                                 <input type="hidden" name="affected" id="affected" value="" />
1350                                 <?php wp_nonce_field( 'find-posts', '_ajax_nonce', false ); ?>
1351                                 <label class="screen-reader-text" for="find-posts-input"><?php _e( 'Search' ); ?></label>
1352                                 <input type="text" id="find-posts-input" name="ps" value="" />
1353                                 <input type="button" id="find-posts-search" value="<?php esc_attr_e( 'Search' ); ?>" class="button" /><br />
1354
1355                                 <?php
1356                                 $post_types = get_post_types( array('public' => true), 'objects' );
1357                                 foreach ( $post_types as $post ) {
1358                                         if ( 'attachment' == $post->name )
1359                                                 continue;
1360                                 ?>
1361                                 <input type="radio" name="find-posts-what" id="find-posts-<?php echo esc_attr($post->name); ?>" value="<?php echo esc_attr($post->name); ?>" <?php checked($post->name,  'post'); ?> />
1362                                 <label for="find-posts-<?php echo esc_attr($post->name); ?>"><?php echo $post->label; ?></label>
1363                                 <?php
1364                                 } ?>
1365                         </div>
1366                         <div id="find-posts-response"></div>
1367                 </div>
1368                 <div class="find-box-buttons">
1369                         <input id="find-posts-close" type="button" class="button alignleft" value="<?php esc_attr_e('Close'); ?>" />
1370                         <?php submit_button( __( 'Select' ), 'button-primary alignright', 'find-posts-submit', false ); ?>
1371                 </div>
1372         </div>
1373 <?php
1374 }
1375
1376 /**
1377  * Display the post password.
1378  *
1379  * The password is passed through {@link esc_attr()} to ensure that it
1380  * is safe for placing in an html attribute.
1381  *
1382  * @uses attr
1383  * @since 2.7.0
1384  */
1385 function the_post_password() {
1386         global $post;
1387         if ( isset( $post->post_password ) ) echo esc_attr( $post->post_password );
1388 }
1389
1390 /**
1391  * {@internal Missing Short Description}}
1392  *
1393  * @since 2.7.0
1394  */
1395 function favorite_actions( $screen = null ) {
1396         $default_action = false;
1397
1398         if ( is_string($screen) )
1399                 $screen = convert_to_screen($screen);
1400
1401         if ( $screen->is_user )
1402                 return;
1403
1404         if ( isset($screen->post_type) ) {
1405                 $post_type_object = get_post_type_object($screen->post_type);
1406                 if ( 'add' != $screen->action )
1407                         $default_action = array('post-new.php?post_type=' . $post_type_object->name => array($post_type_object->labels->new_item, $post_type_object->cap->edit_posts));
1408                 else
1409                         $default_action = array('edit.php?post_type=' . $post_type_object->name => array($post_type_object->labels->name, $post_type_object->cap->edit_posts));
1410         }
1411
1412         if ( !$default_action ) {
1413                 if ( $screen->is_network ) {
1414                         $default_action = array('sites.php' => array( __('Sites'), 'manage_sites'));
1415                 } else {
1416                         switch ( $screen->id ) {
1417                                 case 'upload':
1418                                         $default_action = array('media-new.php' => array(__('New Media'), 'upload_files'));
1419                                         break;
1420                                 case 'media':
1421                                         $default_action = array('upload.php' => array(__('Edit Media'), 'upload_files'));
1422                                         break;
1423                                 case 'link-manager':
1424                                 case 'link':
1425                                         if ( 'add' != $screen->action )
1426                                                 $default_action = array('link-add.php' => array(__('New Link'), 'manage_links'));
1427                                         else
1428                                                 $default_action = array('link-manager.php' => array(__('Edit Links'), 'manage_links'));
1429                                         break;
1430                                 case 'users':
1431                                         $default_action = array('user-new.php' => array(__('New User'), 'create_users'));
1432                                         break;
1433                                 case 'user':
1434                                         $default_action = array('users.php' => array(__('Edit Users'), 'edit_users'));
1435                                         break;
1436                                 case 'plugins':
1437                                         $default_action = array('plugin-install.php' => array(__('Install Plugins'), 'install_plugins'));
1438                                         break;
1439                                 case 'plugin-install':
1440                                         $default_action = array('plugins.php' => array(__('Manage Plugins'), 'activate_plugins'));
1441                                         break;
1442                                 case 'themes':
1443                                         $default_action = array('theme-install.php' => array(__('Install Themes'), 'install_themes'));
1444                                         break;
1445                                 case 'theme-install':
1446                                         $default_action = array('themes.php' => array(__('Manage Themes'), 'switch_themes'));
1447                                         break;
1448                                 default:
1449                                         $default_action = array('post-new.php' => array(__('New Post'), 'edit_posts'));
1450                                         break;
1451                         }
1452                 }
1453         }
1454
1455         if ( !$screen->is_network ) {
1456                 $actions = array(
1457                         'post-new.php' => array(__('New Post'), 'edit_posts'),
1458                         'edit.php?post_status=draft' => array(__('Drafts'), 'edit_posts'),
1459                         'post-new.php?post_type=page' => array(__('New Page'), 'edit_pages'),
1460                         'media-new.php' => array(__('Upload'), 'upload_files'),
1461                         'edit-comments.php' => array(__('Comments'), 'moderate_comments')
1462                         );
1463         } else {
1464                 $actions = array(
1465                         'sites.php' => array( __('Sites'), 'manage_sites'),
1466                         'users.php' => array( __('Users'), 'manage_network_users')
1467                 );
1468         }
1469
1470         $default_key = array_keys($default_action);
1471         $default_key = $default_key[0];
1472         if ( isset($actions[$default_key]) )
1473                 unset($actions[$default_key]);
1474         $actions = array_merge($default_action, $actions);
1475         $actions = apply_filters( 'favorite_actions', $actions, $screen );
1476
1477         $allowed_actions = array();
1478         foreach ( $actions as $action => $data ) {
1479                 if ( current_user_can($data[1]) )
1480                         $allowed_actions[$action] = $data[0];
1481         }
1482
1483         if ( empty($allowed_actions) )
1484                 return;
1485
1486         $first = array_keys($allowed_actions);
1487         $first = $first[0];
1488         echo '<div id="favorite-actions">';
1489         echo '<div id="favorite-first"><a href="' . $first . '">' . $allowed_actions[$first] . '</a></div><div id="favorite-toggle"><br /></div>';
1490         echo '<div id="favorite-inside">';
1491
1492         array_shift($allowed_actions);
1493
1494         foreach ( $allowed_actions as $action => $label) {
1495                 echo "<div class='favorite-action'><a href='$action'>";
1496                 echo $label;
1497                 echo "</a></div>\n";
1498         }
1499         echo "</div></div>\n";
1500 }
1501
1502 /**
1503  * Get the post title.
1504  *
1505  * The post title is fetched and if it is blank then a default string is
1506  * returned.
1507  *
1508  * @since 2.7.0
1509  * @param int $post_id The post id. If not supplied the global $post is used.
1510  * @return string The post title if set
1511  */
1512 function _draft_or_post_title( $post_id = 0 ) {
1513         $title = get_the_title($post_id);
1514         if ( empty($title) )
1515                 $title = __('(no title)');
1516         return $title;
1517 }
1518
1519 /**
1520  * Display the search query.
1521  *
1522  * A simple wrapper to display the "s" parameter in a GET URI. This function
1523  * should only be used when {@link the_search_query()} cannot.
1524  *
1525  * @uses attr
1526  * @since 2.7.0
1527  *
1528  */
1529 function _admin_search_query() {
1530         echo isset($_REQUEST['s']) ? esc_attr( stripslashes( $_REQUEST['s'] ) ) : '';
1531 }
1532
1533 /**
1534  * Generic Iframe header for use with Thickbox
1535  *
1536  * @since 2.7.0
1537  * @param string $title Title of the Iframe page.
1538  * @param bool $limit_styles Limit styles to colour-related styles only (unless others are enqueued).
1539  *
1540  */
1541 function iframe_header( $title = '', $limit_styles = false ) {
1542         show_admin_bar( false );
1543         global $hook_suffix, $current_screen, $current_user, $admin_body_class, $wp_locale;
1544         $admin_body_class = preg_replace('/[^a-z0-9_-]+/i', '-', $hook_suffix);
1545         $admin_body_class .= ' iframe';
1546
1547 ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1548 <html xmlns="http://www.w3.org/1999/xhtml" <?php do_action('admin_xml_ns'); ?> <?php language_attributes(); ?>>
1549 <head>
1550 <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_option('blog_charset'); ?>" />
1551 <title><?php bloginfo('name') ?> &rsaquo; <?php echo $title ?> &#8212; <?php _e('WordPress'); ?></title>
1552 <?php
1553 wp_enqueue_style( 'global' );
1554 if ( ! $limit_styles )
1555         wp_enqueue_style( 'wp-admin' );
1556 wp_enqueue_style( 'colors' );
1557 ?>
1558 <script type="text/javascript">
1559 //<![CDATA[
1560 addLoadEvent = function(func){if(typeof jQuery!="undefined")jQuery(document).ready(func);else if(typeof wpOnload!='function'){wpOnload=func;}else{var oldonload=wpOnload;wpOnload=function(){oldonload();func();}}};
1561 function tb_close(){var win=window.dialogArguments||opener||parent||top;win.tb_remove();}
1562 var userSettings = {
1563                 'url': '<?php echo SITECOOKIEPATH; ?>',
1564                 'uid': '<?php if ( ! isset($current_user) ) $current_user = wp_get_current_user(); echo $current_user->ID; ?>',
1565                 'time':'<?php echo time() ?>'
1566         },
1567         ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>',
1568         pagenow = '<?php echo $current_screen->id; ?>',
1569         typenow = '<?php if ( isset($current_screen->post_type) ) echo $current_screen->post_type; ?>',
1570         adminpage = '<?php echo $admin_body_class; ?>',
1571         thousandsSeparator = '<?php echo addslashes( $wp_locale->number_format['thousands_sep'] ); ?>',
1572         decimalPoint = '<?php echo addslashes( $wp_locale->number_format['decimal_point'] ); ?>',
1573         isRtl = <?php echo (int) is_rtl(); ?>;
1574 //]]>
1575 </script>
1576 <?php
1577 do_action('admin_enqueue_scripts', $hook_suffix);
1578 do_action("admin_print_styles-$hook_suffix");
1579 do_action('admin_print_styles');
1580 do_action("admin_print_scripts-$hook_suffix");
1581 do_action('admin_print_scripts');
1582 do_action("admin_head-$hook_suffix");
1583 do_action('admin_head');
1584 ?>
1585 </head>
1586 <body<?php if ( isset($GLOBALS['body_id']) ) echo ' id="' . $GLOBALS['body_id'] . '"'; ?>  class="no-js <?php echo $admin_body_class; ?>">
1587 <script type="text/javascript">
1588 //<![CDATA[
1589 (function(){
1590 var c = document.body.className;
1591 c = c.replace(/no-js/, 'js');
1592 document.body.className = c;
1593 })();
1594 //]]>
1595 </script>
1596 <?php
1597 }
1598
1599 /**
1600  * Generic Iframe footer for use with Thickbox
1601  *
1602  * @since 2.7.0
1603  *
1604  */
1605 function iframe_footer() {
1606         //We're going to hide any footer output on iframe pages, but run the hooks anyway since they output Javascript or other needed content. ?>
1607         <div class="hidden">
1608 <?php
1609         do_action('admin_footer', '');
1610         do_action('admin_print_footer_scripts'); ?>
1611         </div>
1612 <script type="text/javascript">if(typeof wpOnload=="function")wpOnload();</script>
1613 </body>
1614 </html>
1615 <?php
1616 }
1617
1618 function _post_states($post) {
1619         $post_states = array();
1620         if ( isset($_GET['post_status']) )
1621                 $post_status = $_GET['post_status'];
1622         else
1623                 $post_status = '';
1624
1625         if ( !empty($post->post_password) )
1626                 $post_states[] = __('Password protected');
1627         if ( 'private' == $post->post_status && 'private' != $post_status )
1628                 $post_states[] = __('Private');
1629         if ( 'draft' == $post->post_status && 'draft' != $post_status )
1630                 $post_states[] = __('Draft');
1631         if ( 'pending' == $post->post_status && 'pending' != $post_status )
1632                 /* translators: post state */
1633                 $post_states[] = _x('Pending', 'post state');
1634         if ( is_sticky($post->ID) )
1635                 $post_states[] = __('Sticky');
1636
1637         $post_states = apply_filters( 'display_post_states', $post_states );
1638
1639         if ( ! empty($post_states) ) {
1640                 $state_count = count($post_states);
1641                 $i = 0;
1642                 echo ' - ';
1643                 foreach ( $post_states as $state ) {
1644                         ++$i;
1645                         ( $i == $state_count ) ? $sep = '' : $sep = ', ';
1646                         echo "<span class='post-state'>$state$sep</span>";
1647                 }
1648         }
1649
1650         if ( get_post_format( $post->ID ) )
1651                 echo ' - <span class="post-state-format">' . get_post_format_string( get_post_format( $post->ID ) ) . '</span>';
1652 }
1653
1654 /**
1655  * Convert a screen string to a screen object
1656  *
1657  * @since 3.0.0
1658  *
1659  * @param string $screen The name of the screen
1660  * @return object An object containing the safe screen name and id
1661  */
1662 function convert_to_screen( $screen ) {
1663         $screen = str_replace( array('.php', '-new', '-add', '-network', '-user' ), '', $screen);
1664
1665         if ( is_network_admin() )
1666                 $screen .= '-network';
1667         elseif ( is_user_admin() )
1668                 $screen .= '-user';
1669
1670         $screen = (string) apply_filters( 'screen_meta_screen', $screen );
1671         $screen = (object) array('id' => $screen, 'base' => $screen);
1672         return $screen;
1673 }
1674
1675 function screen_meta($screen) {
1676         global $wp_meta_boxes, $_wp_contextual_help, $wp_list_table, $wp_current_screen_options;
1677
1678         if ( is_string($screen) )
1679                 $screen = convert_to_screen($screen);
1680
1681         $columns = get_column_headers( $screen );
1682         $hidden = get_hidden_columns( $screen );
1683
1684         $meta_screens = array('index' => 'dashboard');
1685
1686         if ( isset($meta_screens[$screen->id]) ) {
1687                 $screen->id = $meta_screens[$screen->id];
1688                 $screen->base = $screen->id;
1689         }
1690
1691         $show_screen = false;
1692         if ( !empty($wp_meta_boxes[$screen->id]) || !empty($columns) )
1693                 $show_screen = true;
1694
1695         $screen_options = screen_options($screen);
1696         if ( $screen_options )
1697                 $show_screen = true;
1698
1699         if ( !isset($_wp_contextual_help) )
1700                 $_wp_contextual_help = array();
1701
1702         $settings = apply_filters('screen_settings', '', $screen);
1703
1704         switch ( $screen->id ) {
1705                 case 'widgets':
1706                         $settings = '<p><a id="access-on" href="widgets.php?widgets-access=on">' . __('Enable accessibility mode') . '</a><a id="access-off" href="widgets.php?widgets-access=off">' . __('Disable accessibility mode') . "</a></p>\n";
1707                         $show_screen = true;
1708                         break;
1709         }
1710         if ( ! empty( $settings ) )
1711                 $show_screen = true;
1712
1713         if ( !empty($wp_current_screen_options) )
1714                 $show_screen = true;
1715
1716 ?>
1717 <div id="screen-meta">
1718 <?php if ( $show_screen ) : ?>
1719 <div id="screen-options-wrap" class="hidden">
1720         <form id="adv-settings" action="" method="post">
1721         <?php if ( isset($wp_meta_boxes[$screen->id]) ) : ?>
1722                 <h5><?php _ex('Show on screen', 'Metaboxes') ?></h5>
1723                 <div class="metabox-prefs">
1724                         <?php meta_box_prefs($screen); ?>
1725                         <br class="clear" />
1726                 </div>
1727                 <?php endif;
1728                 if ( ! empty($columns) ) : ?>
1729                 <h5><?php echo ( isset( $columns['_title'] ) ?  $columns['_title'] :  _x('Show on screen', 'Columns') ) ?></h5>
1730                 <div class="metabox-prefs">
1731 <?php
1732         $special = array('_title', 'cb', 'comment', 'media', 'name', 'title', 'username', 'blogname');
1733
1734         foreach ( $columns as $column => $title ) {
1735                 // Can't hide these for they are special
1736                 if ( in_array( $column, $special ) )
1737                         continue;
1738                 if ( empty( $title ) )
1739                         continue;
1740
1741                 if ( 'comments' == $column )
1742                         $title = __( 'Comments' );
1743                 $id = "$column-hide";
1744                 echo '<label for="' . $id . '">';
1745                 echo '<input class="hide-column-tog" name="' . $id . '" type="checkbox" id="' . $id . '" value="' . $column . '"' . checked( !in_array($column, $hidden), true, false ) . ' />';
1746                 echo "$title</label>\n";
1747         }
1748 ?>
1749                         <br class="clear" />
1750                 </div>
1751         <?php endif;
1752         echo screen_layout($screen);
1753
1754         if ( !empty( $screen_options ) ) {
1755                 ?>
1756                 <h5><?php _ex('Show on screen', 'Screen Options') ?></h5>
1757                 <?php
1758         }
1759
1760         echo $screen_options;
1761         echo $settings; ?>
1762 <div><?php wp_nonce_field( 'screen-options-nonce', 'screenoptionnonce', false ); ?></div>
1763 </form>
1764 </div>
1765
1766 <?php endif; // $show_screen
1767
1768         $_wp_contextual_help = apply_filters('contextual_help_list', $_wp_contextual_help, $screen);
1769         ?>
1770         <div id="contextual-help-wrap" class="hidden">
1771         <?php
1772         $contextual_help = '';
1773         if ( isset($_wp_contextual_help[$screen->id]) ) {
1774                 $contextual_help .= '<div class="metabox-prefs">' . $_wp_contextual_help[$screen->id] . "</div>\n";
1775         } else {
1776                 $contextual_help .= '<div class="metabox-prefs">';
1777                 $default_help = __('<a href="http://codex.wordpress.org/" target="_blank">Documentation</a>');
1778                 $default_help .= '<br />';
1779                 $default_help .= __('<a href="http://wordpress.org/support/" target="_blank">Support Forums</a>');
1780                 $contextual_help .= apply_filters('default_contextual_help', $default_help);
1781                 $contextual_help .= "</div>\n";
1782         }
1783
1784         echo apply_filters('contextual_help', $contextual_help, $screen->id, $screen);
1785         ?>
1786         </div>
1787
1788 <div id="screen-meta-links">
1789 <div id="contextual-help-link-wrap" class="hide-if-no-js screen-meta-toggle">
1790 <a href="#contextual-help" id="contextual-help-link" class="show-settings"><?php _e('Help') ?></a>
1791 </div>
1792 <?php if ( $show_screen ) { ?>
1793 <div id="screen-options-link-wrap" class="hide-if-no-js screen-meta-toggle">
1794 <a href="#screen-options" id="show-settings-link" class="show-settings"><?php _e('Screen Options') ?></a>
1795 </div>
1796 <?php } ?>
1797 </div>
1798 </div>
1799 <?php
1800 }
1801
1802 /**
1803  * Add contextual help text for a page
1804  *
1805  * @since 2.7.0
1806  *
1807  * @param string $screen The handle for the screen to add help to.  This is usually the hook name returned by the add_*_page() functions.
1808  * @param string $help Arbitrary help text
1809  */
1810 function add_contextual_help($screen, $help) {
1811         global $_wp_contextual_help;
1812
1813         if ( is_string($screen) )
1814                 $screen = convert_to_screen($screen);
1815
1816         if ( !isset($_wp_contextual_help) )
1817                 $_wp_contextual_help = array();
1818
1819         $_wp_contextual_help[$screen->id] = $help;
1820 }
1821
1822 function screen_layout($screen) {
1823         global $screen_layout_columns, $wp_current_screen_options;
1824
1825         if ( is_string($screen) )
1826                 $screen = convert_to_screen($screen);
1827
1828         // Back compat for plugins using the filter instead of add_screen_option()
1829         $columns = apply_filters('screen_layout_columns', array(), $screen->id, $screen);
1830         if ( !empty($columns) && isset($columns[$screen->id]) )
1831                 add_screen_option('layout_columns', array('max' => $columns[$screen->id]) );
1832
1833         if ( !isset($wp_current_screen_options['layout_columns']) ) {
1834                 $screen_layout_columns = 0;
1835                 return '';
1836         }
1837
1838         $screen_layout_columns = get_user_option("screen_layout_$screen->id");
1839         $num = $wp_current_screen_options['layout_columns']['max'];
1840
1841         if ( ! $screen_layout_columns ) {
1842                 if ( isset($wp_current_screen_options['layout_columns']['default']) )
1843                         $screen_layout_columns = $wp_current_screen_options['layout_columns']['default'];
1844                 else
1845                         $screen_layout_columns = 2;
1846         }
1847
1848         $i = 1;
1849         $return = '<h5>' . __('Screen Layout') . "</h5>\n<div class='columns-prefs'>" . __('Number of Columns:') . "\n";
1850         while ( $i <= $num ) {
1851                 $return .= "<label><input type='radio' name='screen_columns' value='$i'" . ( ($screen_layout_columns == $i) ? " checked='checked'" : "" ) . " /> $i</label>\n";
1852                 ++$i;
1853         }
1854         $return .= "</div>\n";
1855         return $return;
1856 }
1857
1858 /**
1859  * Register and configure an admin screen option
1860  *
1861  * @since 3.1.0
1862  *
1863  * @param string $option An option name.
1864  * @param mixed $args Option dependent arguments
1865  * @return void
1866  */
1867 function add_screen_option( $option, $args = array() ) {
1868         global $wp_current_screen_options;
1869
1870         if ( !isset($wp_current_screen_options) )
1871                 $wp_current_screen_options = array();
1872
1873         $wp_current_screen_options[$option] = $args;
1874 }
1875
1876 function screen_options($screen) {
1877         global $wp_current_screen_options;
1878
1879         if ( is_string($screen) )
1880                 $screen = convert_to_screen($screen);
1881
1882         if ( !isset($wp_current_screen_options['per_page']) )
1883                 return '';
1884
1885         $per_page_label = $wp_current_screen_options['per_page']['label'];
1886
1887         if ( empty($wp_current_screen_options['per_page']['option']) ) {
1888                 $option = str_replace( '-', '_', "{$screen->id}_per_page" );
1889         } else {
1890                 $option = $wp_current_screen_options['per_page']['option'];
1891         }
1892
1893         $per_page = (int) get_user_option( $option );
1894         if ( empty( $per_page ) || $per_page < 1 ) {
1895                 if ( isset($wp_current_screen_options['per_page']['default']) )
1896                         $per_page = $wp_current_screen_options['per_page']['default'];
1897                 else
1898                         $per_page = 20;
1899         }
1900
1901         if ( 'edit_comments_per_page' == $option )
1902                 $per_page = apply_filters( 'comments_per_page', $per_page, isset($_REQUEST['comment_status']) ? $_REQUEST['comment_status'] : 'all' );
1903         elseif ( 'categories_per_page' == $option )
1904                 $per_page = apply_filters( 'edit_categories_per_page', $per_page );
1905         else
1906                 $per_page = apply_filters( $option, $per_page );
1907
1908         // Back compat
1909         if ( isset( $screen->post_type ) )
1910                 $per_page = apply_filters( 'edit_posts_per_page', $per_page, $screen->post_type );
1911
1912         $return = "<div class='screen-options'>\n";
1913         if ( !empty($per_page_label) )
1914                 $return .= "<input type='text' class='screen-per-page' name='wp_screen_options[value]' id='$option' maxlength='3' value='$per_page' /> <label for='$option'>$per_page_label</label>\n";
1915         $return .= get_submit_button( __( 'Apply' ), 'button', 'screen-options-apply', false );
1916         $return .= "<input type='hidden' name='wp_screen_options[option]' value='" . esc_attr($option) . "' />";
1917         $return .= "</div>\n";
1918         return $return;
1919 }
1920
1921 function screen_icon($screen = '') {
1922         global $current_screen, $typenow;
1923
1924         if ( empty($screen) )
1925                 $screen = $current_screen;
1926         elseif ( is_string($screen) )
1927                 $name = $screen;
1928
1929         $class = 'icon32';
1930
1931         if ( empty($name) ) {
1932                 if ( !empty($screen->parent_base) )
1933                         $name = $screen->parent_base;
1934                 else
1935                         $name = $screen->base;
1936
1937                 if ( 'edit' == $name && isset($screen->post_type) && 'page' == $screen->post_type )
1938                         $name = 'edit-pages';
1939
1940                 $post_type = '';
1941                 if ( isset( $screen->post_type ) )
1942                         $post_type = $screen->post_type;
1943                 elseif ( $current_screen == $screen )
1944                         $post_type = $typenow;
1945                 if ( $post_type )
1946                         $class .= ' ' . sanitize_html_class( 'icon32-posts-' . $post_type );
1947         }
1948
1949 ?>
1950         <div id="icon-<?php echo $name; ?>" class="<?php echo $class; ?>"><br /></div>
1951 <?php
1952 }
1953
1954 /**
1955  * Test support for compressing JavaScript from PHP
1956  *
1957  * Outputs JavaScript that tests if compression from PHP works as expected
1958  * and sets an option with the result. Has no effect when the current user
1959  * is not an administrator. To run the test again the option 'can_compress_scripts'
1960  * has to be deleted.
1961  *
1962  * @since 2.8.0
1963  */
1964 function compression_test() {
1965 ?>
1966         <script type="text/javascript">
1967         /* <![CDATA[ */
1968         var testCompression = {
1969                 get : function(test) {
1970                         var x;
1971                         if ( window.XMLHttpRequest ) {
1972                                 x = new XMLHttpRequest();
1973                         } else {
1974                                 try{x=new ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{x=new ActiveXObject('Microsoft.XMLHTTP');}catch(e){};}
1975                         }
1976
1977                         if (x) {
1978                                 x.onreadystatechange = function() {
1979                                         var r, h;
1980                                         if ( x.readyState == 4 ) {
1981                                                 r = x.responseText.substr(0, 18);
1982                                                 h = x.getResponseHeader('Content-Encoding');
1983                                                 testCompression.check(r, h, test);
1984                                         }
1985                                 }
1986
1987                                 x.open('GET', ajaxurl + '?action=wp-compression-test&test='+test+'&'+(new Date()).getTime(), true);
1988                                 x.send('');
1989                         }
1990                 },
1991
1992                 check : function(r, h, test) {
1993                         if ( ! r && ! test )
1994                                 this.get(1);
1995
1996                         if ( 1 == test ) {
1997                                 if ( h && ( h.match(/deflate/i) || h.match(/gzip/i) ) )
1998                                         this.get('no');
1999                                 else
2000                                         this.get(2);
2001
2002                                 return;
2003                         }
2004
2005                         if ( 2 == test ) {
2006                                 if ( '"wpCompressionTest' == r )
2007                                         this.get('yes');
2008                                 else
2009                                         this.get('no');
2010                         }
2011                 }
2012         };
2013         testCompression.check();
2014         /* ]]> */
2015         </script>
2016 <?php
2017 }
2018
2019 /**
2020  *  Get the current screen object
2021  *
2022  *  @since 3.1.0
2023  *
2024  * @return object Current screen object
2025  */
2026 function get_current_screen() {
2027         global $current_screen;
2028
2029         if ( !isset($current_screen) )
2030                 return null;
2031
2032         return $current_screen;
2033 }
2034
2035 /**
2036  * Set the current screen object
2037  *
2038  * @since 3.0.0
2039  *
2040  * @uses $current_screen
2041  *
2042  * @param string $id Screen id, optional.
2043  */
2044 function set_current_screen( $id =  '' ) {
2045         global $current_screen, $hook_suffix, $typenow, $taxnow;
2046
2047         $action = '';
2048
2049         if ( empty($id) ) {
2050                 $current_screen = $hook_suffix;
2051                 $current_screen = str_replace('.php', '', $current_screen);
2052                 if ( preg_match('/-add|-new$/', $current_screen) )
2053                         $action = 'add';
2054                 $current_screen = str_replace('-new', '', $current_screen);
2055                 $current_screen = str_replace('-add', '', $current_screen);
2056                 $current_screen = array('id' => $current_screen, 'base' => $current_screen);
2057         } else {
2058                 $id = sanitize_key($id);
2059                 if ( false !== strpos($id, '-') ) {
2060                         list( $id, $typenow ) = explode('-', $id, 2);
2061                         if ( taxonomy_exists( $typenow ) ) {
2062                                 $id = 'edit-tags';
2063                                 $taxnow = $typenow;
2064                                 $typenow = '';
2065                         }
2066                 }
2067                 $current_screen = array('id' => $id, 'base' => $id);
2068         }
2069
2070         $current_screen = (object) $current_screen;
2071
2072         $current_screen->action = $action;
2073
2074         // Map index to dashboard
2075         if ( 'index' == $current_screen->base )
2076                 $current_screen->base = 'dashboard';
2077         if ( 'index' == $current_screen->id )
2078                 $current_screen->id = 'dashboard';
2079
2080         if ( 'edit' == $current_screen->id ) {
2081                 if ( empty($typenow) )
2082                         $typenow = 'post';
2083                 $current_screen->id .= '-' . $typenow;
2084                 $current_screen->post_type = $typenow;
2085         } elseif ( 'post' == $current_screen->id ) {
2086                 if ( empty($typenow) )
2087                         $typenow = 'post';
2088                 $current_screen->id = $typenow;
2089                 $current_screen->post_type = $typenow;
2090         } elseif ( 'edit-tags' == $current_screen->id ) {
2091                 if ( empty($taxnow) )
2092                         $taxnow = 'post_tag';
2093                 $current_screen->id = 'edit-' . $taxnow;
2094                 $current_screen->taxonomy = $taxnow;
2095         }
2096
2097         $current_screen->is_network = is_network_admin();
2098         $current_screen->is_user = is_user_admin();
2099
2100         if ( $current_screen->is_network ) {
2101                 $current_screen->base .= '-network';
2102                 $current_screen->id .= '-network';
2103         } elseif ( $current_screen->is_user ) {
2104                 $current_screen->base .= '-user';
2105                 $current_screen->id .= '-user';
2106         }
2107
2108         $current_screen = apply_filters('current_screen', $current_screen);
2109 }
2110
2111 /**
2112  * Echos a submit button, with provided text and appropriate class
2113  *
2114  * @since 3.1.0
2115  *
2116  * @param string $text The text of the button (defaults to 'Save Changes')
2117  * @param string $type The type of button. One of: primary, secondary, delete
2118  * @param string $name The HTML name of the submit button. Defaults to "submit". If no id attribute
2119  *               is given in $other_attributes below, $name will be used as the button's id.
2120  * @param bool $wrap True if the output button should be wrapped in a paragraph tag,
2121  *                         false otherwise. Defaults to true
2122  * @param array|string $other_attributes Other attributes that should be output with the button,
2123  *                     mapping attributes to their values, such as array( 'tabindex' => '1' ).
2124  *                     These attributes will be ouput as attribute="value", such as tabindex="1".
2125  *                     Defaults to no other attributes. Other attributes can also be provided as a
2126  *                     string such as 'tabindex="1"', though the array format is typically cleaner.
2127  */
2128 function submit_button( $text = NULL, $type = 'primary', $name = 'submit', $wrap = true, $other_attributes = NULL ) {
2129         echo get_submit_button( $text, $type, $name, $wrap, $other_attributes );
2130 }
2131
2132 /**
2133  * Returns a submit button, with provided text and appropriate class
2134  *
2135  * @since 3.1.0
2136  *
2137  * @param string $text The text of the button (defaults to 'Save Changes')
2138  * @param string $type The type of button. One of: primary, secondary, delete
2139  * @param string $name The HTML name of the submit button. Defaults to "submit". If no id attribute
2140  *               is given in $other_attributes below, $name will be used as the button's id.
2141  * @param bool $wrap True if the output button should be wrapped in a paragraph tag,
2142  *                         false otherwise. Defaults to true
2143  * @param array|string $other_attributes Other attributes that should be output with the button,
2144  *                     mapping attributes to their values, such as array( 'tabindex' => '1' ).
2145  *                     These attributes will be ouput as attribute="value", such as tabindex="1".
2146  *                     Defaults to no other attributes. Other attributes can also be provided as a
2147  *                     string such as 'tabindex="1"', though the array format is typically cleaner.
2148  */
2149 function get_submit_button( $text = NULL, $type = 'primary', $name = 'submit', $wrap = true, $other_attributes = NULL ) {
2150         switch ( $type ) :
2151                 case 'primary' :
2152                 case 'secondary' :
2153                         $class = 'button-' . $type;
2154                         break;
2155                 case 'delete' :
2156                         $class = 'button-secondary delete';
2157                         break;
2158                 default :
2159                         $class = $type; // Custom cases can just pass in the classes they want to be used
2160         endswitch;
2161         $text = ( NULL == $text ) ? __( 'Save Changes' ) : $text;
2162
2163         // Default the id attribute to $name unless an id was specifically provided in $other_attributes
2164         $id = $name;
2165         if ( is_array( $other_attributes ) && isset( $other_attributes['id'] ) ) {
2166                 $id = $other_attributes['id'];
2167                 unset( $other_attributes['id'] );
2168         }
2169
2170         $attributes = '';
2171         if ( is_array( $other_attributes ) ) {
2172                 foreach ( $other_attributes as $attribute => $value ) {
2173                         $attributes .= $attribute . '="' . esc_attr( $value ) . '" '; // Trailing space is important
2174                 }
2175         } else if ( !empty( $other_attributes ) ) { // Attributes provided as a string
2176                 $attributes = $other_attributes;
2177         }
2178
2179         $button = '<input type="submit" name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '" class="' . esc_attr( $class );
2180         $button .= '" value="' . esc_attr( $text ) . '" ' . $attributes . ' />';
2181
2182         if ( $wrap ) {
2183                 $button = '<p class="submit">' . $button . '</p>';
2184         }
2185
2186         return $button;
2187 }
2188