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