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