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