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