3 * Template WordPress Administration API.
5 * A Big Mess. Also some neat functions that are nicely written.
8 * @subpackage Administration
12 // Category Checklists
16 * Walker to output an unordered list of category checkbox input elements.
21 * @see wp_category_checklist()
22 * @see wp_terms_checklist()
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
29 * Starts the list before the elements are added.
31 * @see Walker:start_lvl()
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()
39 public function start_lvl( &$output, $depth = 0, $args = array() ) {
40 $indent = str_repeat("\t", $depth);
41 $output .= "$indent<ul class='children'>\n";
45 * Ends the list of after the elements are added.
47 * @see Walker::end_lvl()
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()
55 public function end_lvl( &$output, $depth = 0, $args = array() ) {
56 $indent = str_repeat("\t", $depth);
57 $output .= "$indent</ul>\n";
61 * Start the element output.
63 * @see Walker::start_el()
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.
73 public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
74 if ( empty( $args['taxonomy'] ) ) {
75 $taxonomy = 'category';
77 $taxonomy = $args['taxonomy'];
80 if ( $taxonomy == 'category' ) {
81 $name = 'post_category';
83 $name = 'tax_input[' . $taxonomy . ']';
85 $args['popular_cats'] = empty( $args['popular_cats'] ) ? array() : $args['popular_cats'];
86 $class = in_array( $category->term_id, $args['popular_cats'] ) ? ' class="popular-category"' : '';
88 $args['selected_cats'] = empty( $args['selected_cats'] ) ? array() : $args['selected_cats'];
90 /** This filter is documented in wp-includes/category-template.php */
91 $output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" .
92 '<label class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $category->term_id . '"' .
93 checked( in_array( $category->term_id, $args['selected_cats'] ), true, false ) .
94 disabled( empty( $args['disabled'] ), false, false ) . ' /> ' .
95 esc_html( apply_filters( 'the_category', $category->name ) ) . '</label>';
99 * Ends the element output, if needed.
101 * @see Walker::end_el()
105 * @param string $output Passed by reference. Used to append additional content.
106 * @param object $category The current term object.
107 * @param int $depth Depth of the term in reference to parents. Default 0.
108 * @param array $args An array of arguments. @see wp_terms_checklist()
110 public function end_el( &$output, $category, $depth = 0, $args = array() ) {
111 $output .= "</li>\n";
116 * Output an unordered list of checkbox input elements labelled
117 * with category names.
121 * @todo Properly document optional arguments as such.
123 * @see wp_terms_checklist()
125 * @param int $post_id Mark categories associated with this post as checked. $selected_cats must not be an array.
126 * @param int $descendants_and_self ID of the category to output along with its descendents.
127 * @param bool|array $selected_cats List of categories to mark as checked.
128 * @param bool|array $popular_cats Override the list of categories that receive the "popular-category" class.
129 * @param object $walker Walker object to use to build the output.
130 * @param bool $checked_ontop Move checked items out of the hierarchy and to the top of the list.
132 function wp_category_checklist( $post_id = 0, $descendants_and_self = 0, $selected_cats = false, $popular_cats = false, $walker = null, $checked_ontop = true ) {
133 wp_terms_checklist( $post_id, array(
134 'taxonomy' => 'category',
135 'descendants_and_self' => $descendants_and_self,
136 'selected_cats' => $selected_cats,
137 'popular_cats' => $popular_cats,
139 'checked_ontop' => $checked_ontop
144 * Output an unordered list of checkbox input elements labelled with term names.
146 * Taxonomy independent version of {@see wp_category_checklist()}.
150 * @todo Properly document optional default arguments.
152 * @param int $post_id Post ID.
153 * @param array $args Arguments to form the terms checklist.
155 function wp_terms_checklist( $post_id = 0, $args = array() ) {
157 'descendants_and_self' => 0,
158 'selected_cats' => false,
159 'popular_cats' => false,
161 'taxonomy' => 'category',
162 'checked_ontop' => true
166 * Filter the taxonomy terms checklist arguments.
170 * @see wp_terms_checklist()
172 * @param array $args An array of arguments.
173 * @param int $post_id The post ID.
175 $params = apply_filters( 'wp_terms_checklist_args', $args, $post_id );
177 $r = wp_parse_args( $params, $defaults );
179 if ( empty( $r['walker'] ) || ! is_a( $r['walker'], 'Walker' ) ) {
180 $walker = new Walker_Category_Checklist;
182 $walker = $r['walker'];
185 $taxonomy = $r['taxonomy'];
186 $descendants_and_self = (int) $r['descendants_and_self'];
188 $args = array( 'taxonomy' => $taxonomy );
190 $tax = get_taxonomy( $taxonomy );
191 $args['disabled'] = ! current_user_can( $tax->cap->assign_terms );
193 if ( is_array( $r['selected_cats'] ) ) {
194 $args['selected_cats'] = $r['selected_cats'];
195 } elseif ( $post_id ) {
196 $args['selected_cats'] = wp_get_object_terms( $post_id, $taxonomy, array_merge( $args, array( 'fields' => 'ids' ) ) );
198 $args['selected_cats'] = array();
200 if ( is_array( $r['popular_cats'] ) ) {
201 $args['popular_cats'] = $r['popular_cats'];
203 $args['popular_cats'] = get_terms( $taxonomy, array(
205 'orderby' => 'count',
208 'hierarchical' => false
211 if ( $descendants_and_self ) {
212 $categories = (array) get_terms( $taxonomy, array(
213 'child_of' => $descendants_and_self,
217 $self = get_term( $descendants_and_self, $taxonomy );
218 array_unshift( $categories, $self );
220 $categories = (array) get_terms( $taxonomy, array( 'get' => 'all' ) );
223 if ( $r['checked_ontop'] ) {
224 // 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)
225 $checked_categories = array();
226 $keys = array_keys( $categories );
228 foreach( $keys as $k ) {
229 if ( in_array( $categories[$k]->term_id, $args['selected_cats'] ) ) {
230 $checked_categories[] = $categories[$k];
231 unset( $categories[$k] );
235 // Put checked cats on top
236 echo call_user_func_array( array( $walker, 'walk' ), array( $checked_categories, 0, $args ) );
238 // Then the rest of them
239 echo call_user_func_array( array( $walker, 'walk' ), array( $categories, 0, $args ) );
243 * Retrieve a list of the most popular terms from the specified taxonomy.
245 * If the $echo argument is true then the elements for a list of checkbox
246 * `<input>` elements labelled with the names of the selected terms is output.
247 * If the $post_ID global isn't empty then the terms associated with that
248 * post will be marked as checked.
252 * @param string $taxonomy Taxonomy to retrieve terms from.
253 * @param int $default Unused.
254 * @param int $number Number of terms to retrieve. Defaults to 10.
255 * @param bool $echo Optionally output the list as well. Defaults to true.
256 * @return array List of popular term IDs.
258 function wp_popular_terms_checklist( $taxonomy, $default = 0, $number = 10, $echo = true ) {
261 if ( $post && $post->ID )
262 $checked_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields'=>'ids'));
264 $checked_terms = array();
266 $terms = get_terms( $taxonomy, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => $number, 'hierarchical' => false ) );
268 $tax = get_taxonomy($taxonomy);
270 $popular_ids = array();
271 foreach ( (array) $terms as $term ) {
272 $popular_ids[] = $term->term_id;
273 if ( !$echo ) // hack for AJAX use
275 $id = "popular-$taxonomy-$term->term_id";
276 $checked = in_array( $term->term_id, $checked_terms ) ? 'checked="checked"' : '';
279 <li id="<?php echo $id; ?>" class="popular-category">
280 <label class="selectit">
281 <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 ) ); ?> />
283 /** This filter is documented in wp-includes/category-template.php */
284 echo esc_html( apply_filters( 'the_category', $term->name ) );
295 * {@internal Missing Short Description}}
299 * @param int $link_id
301 function wp_link_category_checklist( $link_id = 0 ) {
305 $checked_categories = wp_get_link_cats( $link_id );
306 // No selected categories, strange
307 if ( ! count( $checked_categories ) )
308 $checked_categories[] = $default;
310 $checked_categories[] = $default;
313 $categories = get_terms( 'link_category', array( 'orderby' => 'name', 'hide_empty' => 0 ) );
315 if ( empty( $categories ) )
318 foreach ( $categories as $category ) {
319 $cat_id = $category->term_id;
321 /** This filter is documented in wp-includes/category-template.php */
322 $name = esc_html( apply_filters( 'the_category', $category->name ) );
323 $checked = in_array( $cat_id, $checked_categories ) ? ' checked="checked"' : '';
324 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>";
328 // adds hidden fields with the data for use in the inline editor for posts and pages
330 * {@internal Missing Short Description}}
334 * @param WP_Post $post
336 function get_inline_data($post) {
337 $post_type_object = get_post_type_object($post->post_type);
338 if ( ! current_user_can( 'edit_post', $post->ID ) )
341 $title = esc_textarea( trim( $post->post_title ) );
343 /** This filter is documented in wp-admin/edit-tag-form.php */
345 <div class="hidden" id="inline_' . $post->ID . '">
346 <div class="post_title">' . $title . '</div>
347 <div class="post_name">' . apply_filters( 'editable_slug', $post->post_name ) . '</div>
348 <div class="post_author">' . $post->post_author . '</div>
349 <div class="comment_status">' . esc_html( $post->comment_status ) . '</div>
350 <div class="ping_status">' . esc_html( $post->ping_status ) . '</div>
351 <div class="_status">' . esc_html( $post->post_status ) . '</div>
352 <div class="jj">' . mysql2date( 'd', $post->post_date, false ) . '</div>
353 <div class="mm">' . mysql2date( 'm', $post->post_date, false ) . '</div>
354 <div class="aa">' . mysql2date( 'Y', $post->post_date, false ) . '</div>
355 <div class="hh">' . mysql2date( 'H', $post->post_date, false ) . '</div>
356 <div class="mn">' . mysql2date( 'i', $post->post_date, false ) . '</div>
357 <div class="ss">' . mysql2date( 's', $post->post_date, false ) . '</div>
358 <div class="post_password">' . esc_html( $post->post_password ) . '</div>';
360 if ( $post_type_object->hierarchical )
361 echo '<div class="post_parent">' . $post->post_parent . '</div>';
363 if ( $post->post_type == 'page' )
364 echo '<div class="page_template">' . esc_html( get_post_meta( $post->ID, '_wp_page_template', true ) ) . '</div>';
366 if ( post_type_supports( $post->post_type, 'page-attributes' ) )
367 echo '<div class="menu_order">' . $post->menu_order . '</div>';
369 $taxonomy_names = get_object_taxonomies( $post->post_type );
370 foreach ( $taxonomy_names as $taxonomy_name) {
371 $taxonomy = get_taxonomy( $taxonomy_name );
373 if ( $taxonomy->hierarchical && $taxonomy->show_ui ) {
375 $terms = get_object_term_cache( $post->ID, $taxonomy_name );
376 if ( false === $terms ) {
377 $terms = wp_get_object_terms( $post->ID, $taxonomy_name );
378 wp_cache_add( $post->ID, $terms, $taxonomy_name . '_relationships' );
380 $term_ids = empty( $terms ) ? array() : wp_list_pluck( $terms, 'term_id' );
382 echo '<div class="post_category" id="' . $taxonomy_name . '_' . $post->ID . '">' . implode( ',', $term_ids ) . '</div>';
384 } elseif ( $taxonomy->show_ui ) {
386 echo '<div class="tags_input" id="'.$taxonomy_name.'_'.$post->ID.'">'
387 . esc_html( str_replace( ',', ', ', get_terms_to_edit( $post->ID, $taxonomy_name ) ) ) . '</div>';
392 if ( !$post_type_object->hierarchical )
393 echo '<div class="sticky">' . (is_sticky($post->ID) ? 'sticky' : '') . '</div>';
395 if ( post_type_supports( $post->post_type, 'post-formats' ) )
396 echo '<div class="post_format">' . esc_html( get_post_format( $post->ID ) ) . '</div>';
402 * {@internal Missing Short Description}}
406 * @param int $position
407 * @param bool $checkbox
408 * @param string $mode
409 * @param bool $table_row
411 function wp_comment_reply($position = '1', $checkbox = false, $mode = 'single', $table_row = true) {
414 * Filter the in-line comment reply-to form output in the Comments
417 * Returning a non-empty value here will short-circuit display
418 * of the in-line comment-reply form in the Comments list table,
419 * echoing the returned value instead.
423 * @see wp_comment_reply()
425 * @param string $content The reply-to form content.
426 * @param array $args An array of default args.
428 $content = apply_filters( 'wp_comment_reply', '', array( 'position' => $position, 'checkbox' => $checkbox, 'mode' => $mode ) );
430 if ( ! empty($content) ) {
435 if ( $mode == 'single' ) {
436 $wp_list_table = _get_list_table('WP_Post_Comments_List_Table');
438 $wp_list_table = _get_list_table('WP_Comments_List_Table');
442 <form method="get" action="">
443 <?php if ( $table_row ) : ?>
444 <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">
446 <div id="com-reply" style="display:none;"><div id="replyrow" style="display:none;">
448 <div id="replyhead" style="display:none;"><h5><?php _e( 'Reply to Comment' ); ?></h5></div>
449 <div id="addhead" style="display:none;"><h5><?php _e('Add new Comment'); ?></h5></div>
450 <div id="edithead" style="display:none;">
452 <label for="author"><?php _e('Name') ?></label>
453 <input type="text" name="newcomment_author" size="50" value="" id="author" />
457 <label for="author-email"><?php _e('E-mail') ?></label>
458 <input type="text" name="newcomment_author_email" size="50" value="" id="author-email" />
462 <label for="author-url"><?php _e('URL') ?></label>
463 <input type="text" id="author-url" name="newcomment_author_url" class="code" size="103" value="" />
465 <div style="clear:both;"></div>
468 <div id="replycontainer">
470 $quicktags_settings = array( 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,close' );
471 wp_editor( '', 'replycontent', array( 'media_buttons' => false, 'tinymce' => false, 'quicktags' => $quicktags_settings ) );
475 <p id="replysubmit" class="submit">
476 <a href="#comments-form" class="save button-primary alignright">
477 <span id="addbtn" style="display:none;"><?php _e('Add Comment'); ?></span>
478 <span id="savebtn" style="display:none;"><?php _e('Update Comment'); ?></span>
479 <span id="replybtn" style="display:none;"><?php _e('Submit Reply'); ?></span></a>
480 <a href="#comments-form" class="cancel button-secondary alignleft"><?php _e('Cancel'); ?></a>
481 <span class="waiting spinner"></span>
482 <span class="error" style="display:none;"></span>
486 <input type="hidden" name="user_ID" id="user_ID" value="<?php echo get_current_user_id(); ?>" />
487 <input type="hidden" name="action" id="action" value="" />
488 <input type="hidden" name="comment_ID" id="comment_ID" value="" />
489 <input type="hidden" name="comment_post_ID" id="comment_post_ID" value="" />
490 <input type="hidden" name="status" id="status" value="" />
491 <input type="hidden" name="position" id="position" value="<?php echo $position; ?>" />
492 <input type="hidden" name="checkbox" id="checkbox" value="<?php echo $checkbox ? 1 : 0; ?>" />
493 <input type="hidden" name="mode" id="mode" value="<?php echo esc_attr($mode); ?>" />
495 wp_nonce_field( 'replyto-comment', '_ajax_nonce-replyto-comment', false );
496 if ( current_user_can( 'unfiltered_html' ) )
497 wp_nonce_field( 'unfiltered-html-comment', '_wp_unfiltered_html_comment', false );
499 <?php if ( $table_row ) : ?>
500 </td></tr></tbody></table>
509 * Output 'undo move to trash' text for comments
513 function wp_comment_trashnotice() {
515 <div class="hidden" id="trash-undo-holder">
516 <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>
518 <div class="hidden" id="spam-undo-holder">
519 <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>
525 * {@internal Missing Short Description}}
531 function list_meta( $meta ) {
535 <table id="list-table" style="display: none;">
538 <th class="left">' . _x( 'Name', 'meta name' ) . '</th>
539 <th>' . __( 'Value' ) . '</th>
542 <tbody id="the-list" data-wp-lists="list:meta">
545 </table>'; //TBODY needed for list-manipulation JS
550 <table id="list-table">
553 <th class="left"><?php _ex( 'Name', 'meta name' ) ?></th>
554 <th><?php _e( 'Value' ) ?></th>
557 <tbody id='the-list' data-wp-lists='list:meta'>
559 foreach ( $meta as $entry )
560 echo _list_meta_row( $entry, $count );
568 * {@internal Missing Short Description}}
572 * @param array $entry
576 function _list_meta_row( $entry, &$count ) {
577 static $update_nonce = false;
579 if ( is_protected_meta( $entry['meta_key'], 'post' ) )
582 if ( !$update_nonce )
583 $update_nonce = wp_create_nonce( 'add-meta' );
588 $style = 'alternate';
592 if ( is_serialized( $entry['meta_value'] ) ) {
593 if ( is_serialized_string( $entry['meta_value'] ) ) {
594 // This is a serialized string, so we should display it.
595 $entry['meta_value'] = maybe_unserialize( $entry['meta_value'] );
597 // This is a serialized array/object so we should NOT display it.
603 $entry['meta_key'] = esc_attr($entry['meta_key']);
604 $entry['meta_value'] = esc_textarea( $entry['meta_value'] ); // using a <textarea />
605 $entry['meta_id'] = (int) $entry['meta_id'];
607 $delete_nonce = wp_create_nonce( 'delete-meta_' . $entry['meta_id'] );
609 $r .= "\n\t<tr id='meta-{$entry['meta_id']}' class='$style'>";
610 $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']}' />";
612 $r .= "\n\t\t<div class='submit'>";
613 $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" ) );
615 $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" ) );
617 $r .= wp_nonce_field( 'change-meta', '_ajax_nonce', false, false );
620 $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>";
625 * Prints the form in the Custom Fields meta box.
629 * @param WP_Post $post Optional. The post being edited.
631 function meta_form( $post = null ) {
633 $post = get_post( $post );
636 * Filter the number of custom fields to retrieve for the drop-down
637 * in the Custom Fields meta box.
641 * @param int $limit Number of custom fields to retrieve. Default 30.
643 $limit = apply_filters( 'postmeta_form_limit', 30 );
644 $sql = "SELECT meta_key
647 HAVING meta_key NOT LIKE %s
650 $keys = $wpdb->get_col( $wpdb->prepare( $sql, $wpdb->esc_like( '_' ) . '%', $limit ) );
652 natcasesort( $keys );
653 $meta_key_input_id = 'metakeyselect';
655 $meta_key_input_id = 'metakeyinput';
658 <p><strong><?php _e( 'Add New Custom Field:' ) ?></strong></p>
662 <th class="left"><label for="<?php echo $meta_key_input_id; ?>"><?php _ex( 'Name', 'meta name' ) ?></label></th>
663 <th><label for="metavalue"><?php _e( 'Value' ) ?></label></th>
669 <td id="newmetaleft" class="left">
670 <?php if ( $keys ) { ?>
671 <select id="metakeyselect" name="metakeyselect">
672 <option value="#NONE#"><?php _e( '— Select —' ); ?></option>
675 foreach ( $keys as $key ) {
676 if ( is_protected_meta( $key, 'post' ) || ! current_user_can( 'add_post_meta', $post->ID, $key ) )
678 echo "\n<option value='" . esc_attr($key) . "'>" . esc_html($key) . "</option>";
682 <input class="hide-if-js" type="text" id="metakeyinput" name="metakeyinput" value="" />
683 <a href="#postcustomstuff" class="hide-if-no-js" onclick="jQuery('#metakeyinput, #metakeyselect, #enternew, #cancelnew').toggle();return false;">
684 <span id="enternew"><?php _e('Enter new'); ?></span>
685 <span id="cancelnew" class="hidden"><?php _e('Cancel'); ?></span></a>
687 <input type="text" id="metakeyinput" name="metakeyinput" value="" />
690 <td><textarea id="metavalue" name="metavalue" rows="2" cols="25"></textarea></td>
695 <?php submit_button( __( 'Add Custom Field' ), 'secondary', 'addmeta', false, array( 'id' => 'newmeta-submit', 'data-wp-lists' => 'add:the-list:newmeta' ) ); ?>
697 <?php wp_nonce_field( 'add-meta', '_ajax_nonce-add-meta', false ); ?>
706 * Print out HTML form date elements for editing post or comment publish date.
710 * @param int $edit Accepts 1|true for editing the date, 0|false for adding the date.
711 * @param int $for_post Accepts 1|true for applying the date to a post, 0|false for a comment.
712 * @param int $tab_index The tabindex attribute to add. Default 0.
713 * @param int $multi Optional. Whether the additional fields and buttons should be added.
716 function touch_time( $edit = 1, $for_post = 1, $tab_index = 0, $multi = 0 ) {
717 global $wp_locale, $comment;
721 $edit = ! ( in_array($post->post_status, array('draft', 'pending') ) && (!$post->post_date_gmt || '0000-00-00 00:00:00' == $post->post_date_gmt ) );
723 $tab_index_attribute = '';
724 if ( (int) $tab_index > 0 )
725 $tab_index_attribute = " tabindex=\"$tab_index\"";
727 // todo: Remove this?
728 // 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 />';
730 $time_adj = current_time('timestamp');
731 $post_date = ($for_post) ? $post->post_date : $comment->comment_date;
732 $jj = ($edit) ? mysql2date( 'd', $post_date, false ) : gmdate( 'd', $time_adj );
733 $mm = ($edit) ? mysql2date( 'm', $post_date, false ) : gmdate( 'm', $time_adj );
734 $aa = ($edit) ? mysql2date( 'Y', $post_date, false ) : gmdate( 'Y', $time_adj );
735 $hh = ($edit) ? mysql2date( 'H', $post_date, false ) : gmdate( 'H', $time_adj );
736 $mn = ($edit) ? mysql2date( 'i', $post_date, false ) : gmdate( 'i', $time_adj );
737 $ss = ($edit) ? mysql2date( 's', $post_date, false ) : gmdate( 's', $time_adj );
739 $cur_jj = gmdate( 'd', $time_adj );
740 $cur_mm = gmdate( 'm', $time_adj );
741 $cur_aa = gmdate( 'Y', $time_adj );
742 $cur_hh = gmdate( 'H', $time_adj );
743 $cur_mn = gmdate( 'i', $time_adj );
745 $month = '<label for="mm" class="screen-reader-text">' . __( 'Month' ) . '</label><select ' . ( $multi ? '' : 'id="mm" ' ) . 'name="mm"' . $tab_index_attribute . ">\n";
746 for ( $i = 1; $i < 13; $i = $i +1 ) {
747 $monthnum = zeroise($i, 2);
748 $month .= "\t\t\t" . '<option value="' . $monthnum . '" ' . selected( $monthnum, $mm, false ) . '>';
749 /* translators: 1: month number (01, 02, etc.), 2: month abbreviation */
750 $month .= sprintf( __( '%1$s-%2$s' ), $monthnum, $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) ) ) . "</option>\n";
752 $month .= '</select>';
754 $day = '<label for="jj" class="screen-reader-text">' . __( 'Day' ) . '</label><input type="text" ' . ( $multi ? '' : 'id="jj" ' ) . 'name="jj" value="' . $jj . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />';
755 $year = '<label for="aa" class="screen-reader-text">' . __( 'Year' ) . '</label><input type="text" ' . ( $multi ? '' : 'id="aa" ' ) . 'name="aa" value="' . $aa . '" size="4" maxlength="4"' . $tab_index_attribute . ' autocomplete="off" />';
756 $hour = '<label for="hh" class="screen-reader-text">' . __( 'Hour' ) . '</label><input type="text" ' . ( $multi ? '' : 'id="hh" ' ) . 'name="hh" value="' . $hh . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />';
757 $minute = '<label for="mn" class="screen-reader-text">' . __( 'Minute' ) . '</label><input type="text" ' . ( $multi ? '' : 'id="mn" ' ) . 'name="mn" value="' . $mn . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />';
759 echo '<div class="timestamp-wrap">';
760 /* translators: 1: month, 2: day, 3: year, 4: hour, 5: minute */
761 printf( __( '%1$s %2$s, %3$s @ %4$s : %5$s' ), $month, $day, $year, $hour, $minute );
763 echo '</div><input type="hidden" id="ss" name="ss" value="' . $ss . '" />';
765 if ( $multi ) return;
769 'mm' => array( $mm, $cur_mm ),
770 'jj' => array( $jj, $cur_jj ),
771 'aa' => array( $aa, $cur_aa ),
772 'hh' => array( $hh, $cur_hh ),
773 'mn' => array( $mn, $cur_mn ),
775 foreach ( $map as $timeunit => $value ) {
776 list( $unit, $curr ) = $value;
778 echo '<input type="hidden" id="hidden_' . $timeunit . '" name="hidden_' . $timeunit . '" value="' . $unit . '" />' . "\n";
779 $cur_timeunit = 'cur_' . $timeunit;
780 echo '<input type="hidden" id="' . $cur_timeunit . '" name="' . $cur_timeunit . '" value="' . $curr . '" />' . "\n";
785 <a href="#edit_timestamp" class="save-timestamp hide-if-no-js button"><?php _e('OK'); ?></a>
786 <a href="#edit_timestamp" class="cancel-timestamp hide-if-no-js button-cancel"><?php _e('Cancel'); ?></a>
792 * Print out option HTML elements for the page templates drop-down.
796 * @param string $default Optional. The template file name. Default empty.
798 function page_template_dropdown( $default = '' ) {
799 $templates = get_page_templates( get_post() );
801 foreach ( array_keys( $templates ) as $template ) {
802 $selected = selected( $default, $templates[ $template ], false );
803 echo "\n\t<option value='" . $templates[ $template ] . "' $selected>$template</option>";
808 * Print out option HTML elements for the page parents drop-down.
812 * @param int $default Optional. The default page ID to be pre-selected. Default 0.
813 * @param int $parent Optional. The parent page ID. Default 0.
814 * @param int $level Optional. Page depth level. Default 0.
816 * @return null|false Boolean False if page has no children, otherwise print out html elements
818 function parent_dropdown( $default = 0, $parent = 0, $level = 0 ) {
821 $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) );
824 foreach ( $items as $item ) {
825 // A page cannot be its own parent.
826 if ( $post && $post->ID && $item->ID == $post->ID )
829 $pad = str_repeat( ' ', $level * 3 );
830 $selected = selected( $default, $item->ID, false );
832 echo "\n\t<option class='level-$level' value='$item->ID' $selected>$pad " . esc_html($item->post_title) . "</option>";
833 parent_dropdown( $default, $item->ID, $level +1 );
841 * Print out option html elements for role selectors.
845 * @param string $selected slug for the role that should be already selected
847 function wp_dropdown_roles( $selected = false ) {
851 $editable_roles = array_reverse( get_editable_roles() );
853 foreach ( $editable_roles as $role => $details ) {
854 $name = translate_user_role($details['name'] );
855 if ( $selected == $role ) // preselect specified role
856 $p = "\n\t<option selected='selected' value='" . esc_attr($role) . "'>$name</option>";
858 $r .= "\n\t<option value='" . esc_attr($role) . "'>$name</option>";
864 * Outputs the form used by the importers to accept the data to be imported
868 * @param string $action The action attribute for the form.
870 function wp_import_upload_form( $action ) {
873 * Filter the maximum allowed upload size for import files.
877 * @see wp_max_upload_size()
879 * @param int $max_upload_size Allowed upload size. Default 1 MB.
881 $bytes = apply_filters( 'import_upload_size_limit', wp_max_upload_size() );
882 $size = size_format( $bytes );
883 $upload_dir = wp_upload_dir();
884 if ( ! empty( $upload_dir['error'] ) ) :
885 ?><div class="error"><p><?php _e('Before you can upload your import file, you will need to fix the following error:'); ?></p>
886 <p><strong><?php echo $upload_dir['error']; ?></strong></p></div><?php
889 <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' ) ); ?>">
891 <label for="upload"><?php _e( 'Choose a file from your computer:' ); ?></label> (<?php printf( __('Maximum size: %s' ), $size ); ?>)
892 <input type="file" id="upload" name="import" size="25" />
893 <input type="hidden" name="action" value="save" />
894 <input type="hidden" name="max_file_size" value="<?php echo $bytes; ?>" />
896 <?php submit_button( __('Upload file and import'), 'button' ); ?>
903 * Add a meta box to an edit form.
907 * @param string $id String for use in the 'id' attribute of tags.
908 * @param string $title Title of the meta box.
909 * @param callback $callback Function that fills the box with the desired content.
910 * The function should echo its output.
911 * @param string|WP_Screen $screen Optional. The screen on which to show the box (like a post
912 * type, 'link', or 'comment'). Default is the current screen.
913 * @param string $context Optional. The context within the screen where the boxes
914 * should display. Available contexts vary from screen to
915 * screen. Post edit screen contexts include 'normal', 'side',
916 * and 'advanced'. Comments screen contexts include 'normal'
917 * and 'side'. Menus meta boxes (accordion sections) all use
918 * the 'side' context. Global default is 'advanced'.
919 * @param string $priority Optional. The priority within the context where the boxes
920 * should show ('high', 'low'). Default 'default'.
921 * @param array $callback_args Optional. Data that should be set as the $args property
922 * of the box array (which is the second parameter passed
923 * to your callback). Default null.
925 function add_meta_box( $id, $title, $callback, $screen = null, $context = 'advanced', $priority = 'default', $callback_args = null ) {
926 global $wp_meta_boxes;
928 if ( empty( $screen ) )
929 $screen = get_current_screen();
930 elseif ( is_string( $screen ) )
931 $screen = convert_to_screen( $screen );
935 if ( !isset($wp_meta_boxes) )
936 $wp_meta_boxes = array();
937 if ( !isset($wp_meta_boxes[$page]) )
938 $wp_meta_boxes[$page] = array();
939 if ( !isset($wp_meta_boxes[$page][$context]) )
940 $wp_meta_boxes[$page][$context] = array();
942 foreach ( array_keys($wp_meta_boxes[$page]) as $a_context ) {
943 foreach ( array('high', 'core', 'default', 'low') as $a_priority ) {
944 if ( !isset($wp_meta_boxes[$page][$a_context][$a_priority][$id]) )
947 // If a core box was previously added or removed by a plugin, don't add.
948 if ( 'core' == $priority ) {
949 // If core box previously deleted, don't add
950 if ( false === $wp_meta_boxes[$page][$a_context][$a_priority][$id] )
954 * If box was added with default priority, give it core priority to
955 * maintain sort order.
957 if ( 'default' == $a_priority ) {
958 $wp_meta_boxes[$page][$a_context]['core'][$id] = $wp_meta_boxes[$page][$a_context]['default'][$id];
959 unset($wp_meta_boxes[$page][$a_context]['default'][$id]);
963 // If no priority given and id already present, use existing priority.
964 if ( empty($priority) ) {
965 $priority = $a_priority;
967 * Else, if we're adding to the sorted priority, we don't know the title
968 * or callback. Grab them from the previously added context/priority.
970 } elseif ( 'sorted' == $priority ) {
971 $title = $wp_meta_boxes[$page][$a_context][$a_priority][$id]['title'];
972 $callback = $wp_meta_boxes[$page][$a_context][$a_priority][$id]['callback'];
973 $callback_args = $wp_meta_boxes[$page][$a_context][$a_priority][$id]['args'];
975 // An id can be in only one priority and one context.
976 if ( $priority != $a_priority || $context != $a_context )
977 unset($wp_meta_boxes[$page][$a_context][$a_priority][$id]);
981 if ( empty($priority) )
984 if ( !isset($wp_meta_boxes[$page][$context][$priority]) )
985 $wp_meta_boxes[$page][$context][$priority] = array();
987 $wp_meta_boxes[$page][$context][$priority][$id] = array('id' => $id, 'title' => $title, 'callback' => $callback, 'args' => $callback_args);
991 * Meta-Box template function
995 * @staticvar bool $already_sorted
996 * @param string|WP_Screen $screen Screen identifier
997 * @param string $context box context
998 * @param mixed $object gets passed to the box callback function as first parameter
999 * @return int number of meta_boxes
1001 function do_meta_boxes( $screen, $context, $object ) {
1002 global $wp_meta_boxes;
1003 static $already_sorted = false;
1005 if ( empty( $screen ) )
1006 $screen = get_current_screen();
1007 elseif ( is_string( $screen ) )
1008 $screen = convert_to_screen( $screen );
1010 $page = $screen->id;
1012 $hidden = get_hidden_meta_boxes( $screen );
1014 printf('<div id="%s-sortables" class="meta-box-sortables">', htmlspecialchars($context));
1016 // Grab the ones the user has manually sorted. Pull them out of their previous context/priority and into the one the user chose
1017 if ( ! $already_sorted && $sorted = get_user_option( "meta-box-order_$page" ) ) {
1018 foreach ( $sorted as $box_context => $ids ) {
1019 foreach ( explode( ',', $ids ) as $id ) {
1020 if ( $id && 'dashboard_browser_nag' !== $id ) {
1021 add_meta_box( $id, null, null, $screen, $box_context, 'sorted' );
1027 $already_sorted = true;
1031 if ( isset( $wp_meta_boxes[ $page ][ $context ] ) ) {
1032 foreach ( array( 'high', 'sorted', 'core', 'default', 'low' ) as $priority ) {
1033 if ( isset( $wp_meta_boxes[ $page ][ $context ][ $priority ]) ) {
1034 foreach ( (array) $wp_meta_boxes[ $page ][ $context ][ $priority ] as $box ) {
1035 if ( false == $box || ! $box['title'] )
1038 $hidden_class = in_array($box['id'], $hidden) ? ' hide-if-js' : '';
1039 echo '<div id="' . $box['id'] . '" class="postbox ' . postbox_classes($box['id'], $page) . $hidden_class . '" ' . '>' . "\n";
1040 if ( 'dashboard_browser_nag' != $box['id'] )
1041 echo '<div class="handlediv" title="' . esc_attr__('Click to toggle') . '"><br /></div>';
1042 echo "<h3 class='hndle'><span>{$box['title']}</span></h3>\n";
1043 echo '<div class="inside">' . "\n";
1044 call_user_func($box['callback'], $object, $box);
1059 * Remove a meta box from an edit form.
1063 * @param string $id String for use in the 'id' attribute of tags.
1064 * @param string|object $screen The screen on which to show the box (post, page, link).
1065 * @param string $context The context within the page where the boxes should show ('normal', 'advanced').
1067 function remove_meta_box($id, $screen, $context) {
1068 global $wp_meta_boxes;
1070 if ( empty( $screen ) )
1071 $screen = get_current_screen();
1072 elseif ( is_string( $screen ) )
1073 $screen = convert_to_screen( $screen );
1075 $page = $screen->id;
1077 if ( !isset($wp_meta_boxes) )
1078 $wp_meta_boxes = array();
1079 if ( !isset($wp_meta_boxes[$page]) )
1080 $wp_meta_boxes[$page] = array();
1081 if ( !isset($wp_meta_boxes[$page][$context]) )
1082 $wp_meta_boxes[$page][$context] = array();
1084 foreach ( array('high', 'core', 'default', 'low') as $priority )
1085 $wp_meta_boxes[$page][$context][$priority][$id] = false;
1089 * Meta Box Accordion Template Function
1091 * Largely made up of abstracted code from {@link do_meta_boxes()}, this
1092 * function serves to build meta boxes as list items for display as
1093 * a collapsible accordion.
1097 * @uses global $wp_meta_boxes Used to retrieve registered meta boxes.
1099 * @param string|object $screen The screen identifier.
1100 * @param string $context The meta box context.
1101 * @param mixed $object gets passed to the section callback function as first parameter.
1102 * @return int number of meta boxes as accordion sections.
1104 function do_accordion_sections( $screen, $context, $object ) {
1105 global $wp_meta_boxes;
1107 wp_enqueue_script( 'accordion' );
1109 if ( empty( $screen ) )
1110 $screen = get_current_screen();
1111 elseif ( is_string( $screen ) )
1112 $screen = convert_to_screen( $screen );
1114 $page = $screen->id;
1116 $hidden = get_hidden_meta_boxes( $screen );
1118 <div id="side-sortables" class="accordion-container">
1119 <ul class="outer-border">
1122 $first_open = false;
1124 if ( isset( $wp_meta_boxes[ $page ][ $context ] ) ) {
1125 foreach ( array( 'high', 'core', 'default', 'low' ) as $priority ) {
1126 if ( isset( $wp_meta_boxes[ $page ][ $context ][ $priority ] ) ) {
1127 foreach ( $wp_meta_boxes[ $page ][ $context ][ $priority ] as $box ) {
1128 if ( false == $box || ! $box['title'] )
1131 $hidden_class = in_array( $box['id'], $hidden ) ? 'hide-if-js' : '';
1134 if ( ! $first_open && empty( $hidden_class ) ) {
1136 $open_class = 'open';
1139 <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'] ); ?>">
1140 <h3 class="accordion-section-title hndle" tabindex="0">
1141 <?php echo esc_html( $box['title'] ); ?>
1142 <span class="screen-reader-text"><?php _e( 'Press return or enter to expand' ); ?></span>
1144 <div class="accordion-section-content <?php postbox_classes( $box['id'], $page ); ?>">
1145 <div class="inside">
1146 <?php call_user_func( $box['callback'], $object, $box ); ?>
1147 </div><!-- .inside -->
1148 </div><!-- .accordion-section-content -->
1149 </li><!-- .accordion-section -->
1156 </ul><!-- .outer-border -->
1157 </div><!-- .accordion-container -->
1163 * Add a new section to a settings page.
1165 * Part of the Settings API. Use this to define new settings sections for an admin page.
1166 * Show settings sections in your admin page callback function with do_settings_sections().
1167 * Add settings fields to your section with add_settings_field()
1169 * The $callback argument should be the name of a function that echoes out any
1170 * content you want to show at the top of the settings section before the actual
1171 * fields. It can output nothing if you want.
1175 * @global $wp_settings_sections Storage array of all settings sections added to admin pages
1177 * @param string $id Slug-name to identify the section. Used in the 'id' attribute of tags.
1178 * @param string $title Formatted title of the section. Shown as the heading for the section.
1179 * @param string $callback Function that echos out any content at the top of the section (between heading and fields).
1180 * @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();
1182 function add_settings_section($id, $title, $callback, $page) {
1183 global $wp_settings_sections;
1185 if ( 'misc' == $page ) {
1186 _deprecated_argument( __FUNCTION__, '3.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'misc' ) );
1190 if ( 'privacy' == $page ) {
1191 _deprecated_argument( __FUNCTION__, '3.5', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'privacy' ) );
1195 $wp_settings_sections[$page][$id] = array('id' => $id, 'title' => $title, 'callback' => $callback);
1199 * Add a new field to a section of a settings page
1201 * Part of the Settings API. Use this to define a settings field that will show
1202 * as part of a settings section inside a settings page. The fields are shown using
1203 * do_settings_fields() in do_settings-sections()
1205 * The $callback argument should be the name of a function that echoes out the
1206 * html input tags for this setting field. Use get_option() to retrieve existing
1211 * @global $wp_settings_fields Storage array of settings fields and info about their pages/sections
1213 * @param string $id Slug-name to identify the field. Used in the 'id' attribute of tags.
1214 * @param string $title Formatted title of the field. Shown as the label for the field during output.
1215 * @param string $callback Function that fills the field with the desired form inputs. The function should echo its output.
1216 * @param string $page The slug-name of the settings page on which to show the section (general, reading, writing, ...).
1217 * @param string $section The slug-name of the section of the settings page in which to show the box (default, ...).
1218 * @param array $args Additional arguments
1220 function add_settings_field($id, $title, $callback, $page, $section = 'default', $args = array()) {
1221 global $wp_settings_fields;
1223 if ( 'misc' == $page ) {
1224 _deprecated_argument( __FUNCTION__, '3.0', __( 'The miscellaneous options group has been removed. Use another settings group.' ) );
1228 if ( 'privacy' == $page ) {
1229 _deprecated_argument( __FUNCTION__, '3.5', __( 'The privacy options group has been removed. Use another settings group.' ) );
1233 $wp_settings_fields[$page][$section][$id] = array('id' => $id, 'title' => $title, 'callback' => $callback, 'args' => $args);
1237 * Prints out all settings sections added to a particular settings page
1239 * Part of the Settings API. Use this in a settings page callback function
1240 * to output all the sections and fields that were added to that $page with
1241 * add_settings_section() and add_settings_field()
1243 * @global $wp_settings_sections Storage array of all settings sections added to admin pages
1244 * @global $wp_settings_fields Storage array of settings fields and info about their pages/sections
1247 * @param string $page The slug name of the page whos settings sections you want to output
1249 function do_settings_sections( $page ) {
1250 global $wp_settings_sections, $wp_settings_fields;
1252 if ( ! isset( $wp_settings_sections[$page] ) )
1255 foreach ( (array) $wp_settings_sections[$page] as $section ) {
1256 if ( $section['title'] )
1257 echo "<h3>{$section['title']}</h3>\n";
1259 if ( $section['callback'] )
1260 call_user_func( $section['callback'], $section );
1262 if ( ! isset( $wp_settings_fields ) || !isset( $wp_settings_fields[$page] ) || !isset( $wp_settings_fields[$page][$section['id']] ) )
1264 echo '<table class="form-table">';
1265 do_settings_fields( $page, $section['id'] );
1271 * Print out the settings fields for a particular settings section
1273 * Part of the Settings API. Use this in a settings page to output
1274 * a specific section. Should normally be called by do_settings_sections()
1275 * rather than directly.
1277 * @global $wp_settings_fields Storage array of settings fields and their pages/sections
1281 * @param string $page Slug title of the admin page who's settings fields you want to show.
1282 * @param string $section Slug title of the settings section who's fields you want to show.
1284 function do_settings_fields($page, $section) {
1285 global $wp_settings_fields;
1287 if ( ! isset( $wp_settings_fields[$page][$section] ) )
1290 foreach ( (array) $wp_settings_fields[$page][$section] as $field ) {
1292 if ( !empty($field['args']['label_for']) )
1293 echo '<th scope="row"><label for="' . esc_attr( $field['args']['label_for'] ) . '">' . $field['title'] . '</label></th>';
1295 echo '<th scope="row">' . $field['title'] . '</th>';
1297 call_user_func($field['callback'], $field['args']);
1304 * Register a settings error to be displayed to the user
1306 * Part of the Settings API. Use this to show messages to users about settings validation
1307 * problems, missing settings or anything else.
1309 * Settings errors should be added inside the $sanitize_callback function defined in
1310 * register_setting() for a given setting to give feedback about the submission.
1312 * By default messages will show immediately after the submission that generated the error.
1313 * Additional calls to settings_errors() can be used to show errors even when the settings
1314 * page is first accessed.
1318 * @todo Properly document optional arguments as such.
1320 * @global array $wp_settings_errors Storage array of errors registered during this pageload
1322 * @param string $setting Slug title of the setting to which this error applies
1323 * @param string $code Slug-name to identify the error. Used as part of 'id' attribute in HTML output.
1324 * @param string $message The formatted message text to display to the user (will be shown inside styled
1325 * `<div>` and `<p>` tags).
1326 * @param string $type The type of message it is, controls HTML class. Use 'error' or 'updated'.
1328 function add_settings_error( $setting, $code, $message, $type = 'error' ) {
1329 global $wp_settings_errors;
1331 $wp_settings_errors[] = array(
1332 'setting' => $setting,
1334 'message' => $message,
1340 * Fetch settings errors registered by add_settings_error()
1342 * Checks the $wp_settings_errors array for any errors declared during the current
1343 * pageload and returns them.
1345 * If changes were just submitted ($_GET['settings-updated']) and settings errors were saved
1346 * to the 'settings_errors' transient then those errors will be returned instead. This
1347 * is used to pass errors back across pageloads.
1349 * Use the $sanitize argument to manually re-sanitize the option before returning errors.
1350 * This is useful if you have errors or notices you want to show even when the user
1351 * hasn't submitted data (i.e. when they first load an options page, or in admin_notices action hook)
1355 * @global array $wp_settings_errors Storage array of errors registered during this pageload
1357 * @param string $setting Optional slug title of a specific setting who's errors you want.
1358 * @param boolean $sanitize Whether to re-sanitize the setting value before returning errors.
1359 * @return array Array of settings errors
1361 function get_settings_errors( $setting = '', $sanitize = false ) {
1362 global $wp_settings_errors;
1365 * If $sanitize is true, manually re-run the sanitization for this option
1366 * This allows the $sanitize_callback from register_setting() to run, adding
1367 * any settings errors you want to show by default.
1370 sanitize_option( $setting, get_option( $setting ) );
1372 // If settings were passed back from options.php then use them.
1373 if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] && get_transient( 'settings_errors' ) ) {
1374 $wp_settings_errors = array_merge( (array) $wp_settings_errors, get_transient( 'settings_errors' ) );
1375 delete_transient( 'settings_errors' );
1378 // Check global in case errors have been added on this pageload.
1379 if ( ! count( $wp_settings_errors ) )
1382 // Filter the results to those of a specific setting if one was set.
1384 $setting_errors = array();
1385 foreach ( (array) $wp_settings_errors as $key => $details ) {
1386 if ( $setting == $details['setting'] )
1387 $setting_errors[] = $wp_settings_errors[$key];
1389 return $setting_errors;
1392 return $wp_settings_errors;
1396 * Display settings errors registered by {@see add_settings_error()}.
1398 * Part of the Settings API. Outputs a div for each error retrieved by
1399 * {@see get_settings_errors()}.
1401 * This is called automatically after a settings page based on the
1402 * Settings API is submitted. Errors should be added during the validation
1403 * callback function for a setting defined in {@see register_setting()}
1405 * The $sanitize option is passed into {@see get_settings_errors()} and will
1406 * re-run the setting sanitization
1407 * on its current value.
1409 * The $hide_on_update option will cause errors to only show when the settings
1410 * page is first loaded. if the user has already saved new values it will be
1411 * hidden to avoid repeating messages already shown in the default error
1412 * reporting after submission. This is useful to show general errors like
1413 * missing settings when the user arrives at the settings page.
1417 * @param string $setting Optional slug title of a specific setting who's errors you want.
1418 * @param boolean $sanitize Whether to re-sanitize the setting value before returning errors.
1419 * @param boolean $hide_on_update If set to true errors will not be shown if the settings page has already been submitted.
1421 function settings_errors( $setting = '', $sanitize = false, $hide_on_update = false ) {
1423 if ( $hide_on_update && ! empty( $_GET['settings-updated'] ) )
1426 $settings_errors = get_settings_errors( $setting, $sanitize );
1428 if ( empty( $settings_errors ) )
1432 foreach ( $settings_errors as $key => $details ) {
1433 $css_id = 'setting-error-' . $details['code'];
1434 $css_class = $details['type'] . ' settings-error';
1435 $output .= "<div id='$css_id' class='$css_class'> \n";
1436 $output .= "<p><strong>{$details['message']}</strong></p>";
1437 $output .= "</div> \n";
1443 * {@internal Missing Short Description}}
1447 * @param string $found_action
1449 function find_posts_div($found_action = '') {
1451 <div id="find-posts" class="find-box" style="display: none;">
1452 <div id="find-posts-head" class="find-box-head">
1453 <?php _e( 'Find Posts or Pages' ); ?>
1454 <div id="find-posts-close"></div>
1456 <div class="find-box-inside">
1457 <div class="find-box-search">
1458 <?php if ( $found_action ) { ?>
1459 <input type="hidden" name="found_action" value="<?php echo esc_attr($found_action); ?>" />
1461 <input type="hidden" name="affected" id="affected" value="" />
1462 <?php wp_nonce_field( 'find-posts', '_ajax_nonce', false ); ?>
1463 <label class="screen-reader-text" for="find-posts-input"><?php _e( 'Search' ); ?></label>
1464 <input type="text" id="find-posts-input" name="ps" value="" />
1465 <span class="spinner"></span>
1466 <input type="button" id="find-posts-search" value="<?php esc_attr_e( 'Search' ); ?>" class="button" />
1467 <div class="clear"></div>
1469 <div id="find-posts-response"></div>
1471 <div class="find-box-buttons">
1472 <?php submit_button( __( 'Select' ), 'button-primary alignright', 'find-posts-submit', false ); ?>
1473 <div class="clear"></div>
1480 * Display the post password.
1482 * The password is passed through {@link esc_attr()} to ensure that it
1483 * is safe for placing in an html attribute.
1487 function the_post_password() {
1489 if ( isset( $post->post_password ) )
1490 echo esc_attr( $post->post_password );
1494 * Get the post title.
1496 * The post title is fetched and if it is blank then a default string is
1501 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
1502 * @return string The post title if set.
1504 function _draft_or_post_title( $post = 0 ) {
1505 $title = get_the_title( $post );
1506 if ( empty( $title ) )
1507 $title = __( '(no title)' );
1508 return esc_html( $title );
1512 * Display the search query.
1514 * A simple wrapper to display the "s" parameter in a GET URI. This function
1515 * should only be used when {@link the_search_query()} cannot.
1520 function _admin_search_query() {
1521 echo isset($_REQUEST['s']) ? esc_attr( wp_unslash( $_REQUEST['s'] ) ) : '';
1525 * Generic Iframe header for use with Thickbox
1528 * @param string $title Title of the Iframe page.
1529 * @param bool $limit_styles Limit styles to colour-related styles only (unless others are enqueued).
1532 function iframe_header( $title = '', $limit_styles = false ) {
1533 show_admin_bar( false );
1534 global $hook_suffix, $current_user, $admin_body_class, $wp_locale;
1535 $admin_body_class = preg_replace('/[^a-z0-9_-]+/i', '-', $hook_suffix);
1537 $current_screen = get_current_screen();
1539 @header( 'Content-Type: ' . get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' ) );
1540 _wp_admin_html_begin();
1542 <title><?php bloginfo('name') ?> › <?php echo $title ?> — <?php _e('WordPress'); ?></title>
1544 wp_enqueue_style( 'colors' );
1546 <script type="text/javascript">
1548 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();}}};
1549 function tb_close(){var win=window.dialogArguments||opener||parent||top;win.tb_remove();}
1550 var ajaxurl = '<?php echo admin_url( 'admin-ajax.php', 'relative' ); ?>',
1551 pagenow = '<?php echo $current_screen->id; ?>',
1552 typenow = '<?php echo $current_screen->post_type; ?>',
1553 adminpage = '<?php echo $admin_body_class; ?>',
1554 thousandsSeparator = '<?php echo addslashes( $wp_locale->number_format['thousands_sep'] ); ?>',
1555 decimalPoint = '<?php echo addslashes( $wp_locale->number_format['decimal_point'] ); ?>',
1556 isRtl = <?php echo (int) is_rtl(); ?>;
1560 /** This action is documented in wp-admin/admin-header.php */
1561 do_action( 'admin_enqueue_scripts', $hook_suffix );
1563 /** This action is documented in wp-admin/admin-header.php */
1564 do_action( "admin_print_styles-$hook_suffix" );
1566 /** This action is documented in wp-admin/admin-header.php */
1567 do_action( 'admin_print_styles' );
1569 /** This action is documented in wp-admin/admin-header.php */
1570 do_action( "admin_print_scripts-$hook_suffix" );
1572 /** This action is documented in wp-admin/admin-header.php */
1573 do_action( 'admin_print_scripts' );
1575 /** This action is documented in wp-admin/admin-header.php */
1576 do_action( "admin_head-$hook_suffix" );
1578 /** This action is documented in wp-admin/admin-header.php */
1579 do_action( 'admin_head' );
1581 $admin_body_class .= ' locale-' . sanitize_html_class( strtolower( str_replace( '_', '-', get_locale() ) ) );
1584 $admin_body_class .= ' rtl';
1589 /** This filter is documented in wp-admin/admin-header.php */
1590 $admin_body_classes = apply_filters( 'admin_body_class', '' );
1592 <body<?php if ( isset($GLOBALS['body_id']) ) echo ' id="' . $GLOBALS['body_id'] . '"'; ?> class="wp-admin wp-core-ui no-js iframe <?php echo $admin_body_classes . ' ' . $admin_body_class; ?>">
1593 <script type="text/javascript">
1596 var c = document.body.className;
1597 c = c.replace(/no-js/, 'js');
1598 document.body.className = c;
1606 * Generic Iframe footer for use with Thickbox
1611 function iframe_footer() {
1613 * We're going to hide any footer output on iFrame pages,
1614 * but run the hooks anyway since they output JavaScript
1615 * or other needed content.
1618 <div class="hidden">
1620 /** This action is documented in wp-admin/admin-footer.php */
1621 do_action( 'admin_footer', '' );
1623 /** This action is documented in wp-admin/admin-footer.php */
1624 do_action( 'admin_print_footer_scripts' );
1627 <script type="text/javascript">if(typeof wpOnload=="function")wpOnload();</script>
1633 function _post_states($post) {
1634 $post_states = array();
1635 if ( isset( $_REQUEST['post_status'] ) )
1636 $post_status = $_REQUEST['post_status'];
1640 if ( !empty($post->post_password) )
1641 $post_states['protected'] = __('Password protected');
1642 if ( 'private' == $post->post_status && 'private' != $post_status )
1643 $post_states['private'] = __('Private');
1644 if ( 'draft' == $post->post_status && 'draft' != $post_status )
1645 $post_states['draft'] = __('Draft');
1646 if ( 'pending' == $post->post_status && 'pending' != $post_status )
1647 /* translators: post state */
1648 $post_states['pending'] = _x('Pending', 'post state');
1649 if ( is_sticky($post->ID) )
1650 $post_states['sticky'] = __('Sticky');
1653 * Filter the default post display states used in the Posts list table.
1657 * @param array $post_states An array of post display states. Values include 'Password protected',
1658 * 'Private', 'Draft', 'Pending', and 'Sticky'.
1659 * @param int $post The post ID.
1661 $post_states = apply_filters( 'display_post_states', $post_states, $post );
1663 if ( ! empty($post_states) ) {
1664 $state_count = count($post_states);
1667 foreach ( $post_states as $state ) {
1669 ( $i == $state_count ) ? $sep = '' : $sep = ', ';
1670 echo "<span class='post-state'>$state$sep</span>";
1676 function _media_states( $post ) {
1677 $media_states = array();
1678 $stylesheet = get_option('stylesheet');
1680 if ( current_theme_supports( 'custom-header') ) {
1681 $meta_header = get_post_meta($post->ID, '_wp_attachment_is_custom_header', true );
1682 if ( ! empty( $meta_header ) && $meta_header == $stylesheet )
1683 $media_states[] = __( 'Header Image' );
1686 if ( current_theme_supports( 'custom-background') ) {
1687 $meta_background = get_post_meta($post->ID, '_wp_attachment_is_custom_background', true );
1688 if ( ! empty( $meta_background ) && $meta_background == $stylesheet )
1689 $media_states[] = __( 'Background Image' );
1693 * Filter the default media display states for items in the Media list table.
1697 * @param array $media_states An array of media states. Default 'Header Image',
1698 * 'Background Image'.
1700 $media_states = apply_filters( 'display_media_states', $media_states );
1702 if ( ! empty( $media_states ) ) {
1703 $state_count = count( $media_states );
1706 foreach ( $media_states as $state ) {
1708 ( $i == $state_count ) ? $sep = '' : $sep = ', ';
1709 echo "<span class='post-state'>$state$sep</span>";
1715 * Test support for compressing JavaScript from PHP
1717 * Outputs JavaScript that tests if compression from PHP works as expected
1718 * and sets an option with the result. Has no effect when the current user
1719 * is not an administrator. To run the test again the option 'can_compress_scripts'
1720 * has to be deleted.
1724 function compression_test() {
1726 <script type="text/javascript">
1728 var testCompression = {
1729 get : function(test) {
1731 if ( window.XMLHttpRequest ) {
1732 x = new XMLHttpRequest();
1734 try{x=new ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{x=new ActiveXObject('Microsoft.XMLHTTP');}catch(e){};}
1738 x.onreadystatechange = function() {
1740 if ( x.readyState == 4 ) {
1741 r = x.responseText.substr(0, 18);
1742 h = x.getResponseHeader('Content-Encoding');
1743 testCompression.check(r, h, test);
1747 x.open('GET', ajaxurl + '?action=wp-compression-test&test='+test+'&'+(new Date()).getTime(), true);
1752 check : function(r, h, test) {
1753 if ( ! r && ! test )
1757 if ( h && ( h.match(/deflate/i) || h.match(/gzip/i) ) )
1766 if ( '"wpCompressionTest' == r )
1773 testCompression.check();
1780 * Echoes a submit button, with provided text and appropriate class(es).
1784 * @see get_submit_button()
1786 * @param string $text The text of the button (defaults to 'Save Changes')
1787 * @param string $type Optional. The type and CSS class(es) of the button. Core values
1788 * include 'primary', 'secondary', 'delete'. Default 'primary'
1789 * @param string $name The HTML name of the submit button. Defaults to "submit". If no
1790 * id attribute is given in $other_attributes below, $name will be
1791 * used as the button's id.
1792 * @param bool $wrap True if the output button should be wrapped in a paragraph tag,
1793 * false otherwise. Defaults to true
1794 * @param array|string $other_attributes Other attributes that should be output with the button, mapping
1795 * attributes to their values, such as setting tabindex to 1, etc.
1796 * These key/value attribute pairs will be output as attribute="value",
1797 * where attribute is the key. Other attributes can also be provided
1798 * as a string such as 'tabindex="1"', though the array format is
1799 * preferred. Default null.
1801 function submit_button( $text = null, $type = 'primary', $name = 'submit', $wrap = true, $other_attributes = null ) {
1802 echo get_submit_button( $text, $type, $name, $wrap, $other_attributes );
1806 * Returns a submit button, with provided text and appropriate class
1810 * @param string $text The text of the button (defaults to 'Save Changes')
1811 * @param string $type The type of button. One of: primary, secondary, delete
1812 * @param string $name The HTML name of the submit button. Defaults to "submit". If no id attribute
1813 * is given in $other_attributes below, $name will be used as the button's id.
1814 * @param bool $wrap True if the output button should be wrapped in a paragraph tag,
1815 * false otherwise. Defaults to true
1816 * @param array|string $other_attributes Other attributes that should be output with the button,
1817 * mapping attributes to their values, such as array( 'tabindex' => '1' ).
1818 * These attributes will be output as attribute="value", such as tabindex="1".
1819 * Defaults to no other attributes. Other attributes can also be provided as a
1820 * string such as 'tabindex="1"', though the array format is typically cleaner.
1822 function get_submit_button( $text = null, $type = 'primary large', $name = 'submit', $wrap = true, $other_attributes = null ) {
1823 if ( ! is_array( $type ) )
1824 $type = explode( ' ', $type );
1826 $button_shorthand = array( 'primary', 'small', 'large' );
1827 $classes = array( 'button' );
1828 foreach ( $type as $t ) {
1829 if ( 'secondary' === $t || 'button-secondary' === $t )
1831 $classes[] = in_array( $t, $button_shorthand ) ? 'button-' . $t : $t;
1833 $class = implode( ' ', array_unique( $classes ) );
1835 if ( 'delete' === $type )
1836 $class = 'button-secondary delete';
1838 $text = $text ? $text : __( 'Save Changes' );
1840 // Default the id attribute to $name unless an id was specifically provided in $other_attributes
1842 if ( is_array( $other_attributes ) && isset( $other_attributes['id'] ) ) {
1843 $id = $other_attributes['id'];
1844 unset( $other_attributes['id'] );
1848 if ( is_array( $other_attributes ) ) {
1849 foreach ( $other_attributes as $attribute => $value ) {
1850 $attributes .= $attribute . '="' . esc_attr( $value ) . '" '; // Trailing space is important
1852 } else if ( !empty( $other_attributes ) ) { // Attributes provided as a string
1853 $attributes = $other_attributes;
1856 $button = '<input type="submit" name="' . esc_attr( $name ) . '" id="' . esc_attr( $id ) . '" class="' . esc_attr( $class );
1857 $button .= '" value="' . esc_attr( $text ) . '" ' . $attributes . ' />';
1860 $button = '<p class="submit">' . $button . '</p>';
1866 function _wp_admin_html_begin() {
1869 $admin_html_class = ( is_admin_bar_showing() ) ? 'wp-toolbar' : '';
1872 @header('X-UA-Compatible: IE=edge');
1877 <html xmlns="http://www.w3.org/1999/xhtml" class="ie8 <?php echo $admin_html_class; ?>" <?php
1879 * Fires inside the HTML tag in the admin header.
1883 do_action( 'admin_xml_ns' );
1884 ?> <?php language_attributes(); ?>>
1886 <!--[if !(IE 8) ]><!-->
1887 <html xmlns="http://www.w3.org/1999/xhtml" class="<?php echo $admin_html_class; ?>" <?php
1888 /** This action is documented in wp-admin/includes/template.php */
1889 do_action( 'admin_xml_ns' );
1890 ?> <?php language_attributes(); ?>>
1893 <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_option('blog_charset'); ?>" />
1897 final class WP_Internal_Pointers {
1899 * Initializes the new feature pointers.
1903 * All pointers can be disabled using the following:
1904 * remove_action( 'admin_enqueue_scripts', array( 'WP_Internal_Pointers', 'enqueue_scripts' ) );
1906 * Individual pointers (e.g. wp390_widgets) can be disabled using the following:
1907 * remove_action( 'admin_print_footer_scripts', array( 'WP_Internal_Pointers', 'pointer_wp390_widgets' ) );
1909 public static function enqueue_scripts( $hook_suffix ) {
1911 * Register feature pointers
1912 * Format: array( hook_suffix => pointer_id )
1915 $registered_pointers = array(
1916 'post-new.php' => 'wp410_dfw',
1917 'post.php' => 'wp410_dfw',
1918 'edit.php' => 'wp360_locks',
1919 'widgets.php' => 'wp390_widgets',
1920 'themes.php' => 'wp390_widgets',
1923 // Check if screen related pointer is registered
1924 if ( empty( $registered_pointers[ $hook_suffix ] ) )
1927 $pointers = (array) $registered_pointers[ $hook_suffix ];
1929 $caps_required = array(
1930 'wp390_widgets' => array( 'edit_theme_options' ),
1933 // Get dismissed pointers
1934 $dismissed = explode( ',', (string) get_user_meta( get_current_user_id(), 'dismissed_wp_pointers', true ) );
1936 $got_pointers = false;
1937 foreach ( array_diff( $pointers, $dismissed ) as $pointer ) {
1938 if ( isset( $caps_required[ $pointer ] ) ) {
1939 foreach ( $caps_required[ $pointer ] as $cap ) {
1940 if ( ! current_user_can( $cap ) )
1945 // Bind pointer print function
1946 add_action( 'admin_print_footer_scripts', array( 'WP_Internal_Pointers', 'pointer_' . $pointer ) );
1947 $got_pointers = true;
1950 if ( ! $got_pointers )
1953 // Add pointers script and style to queue
1954 wp_enqueue_style( 'wp-pointer' );
1955 wp_enqueue_script( 'wp-pointer' );
1959 * Print the pointer JavaScript data.
1963 * @param string $pointer_id The pointer ID.
1964 * @param string $selector The HTML elements, on which the pointer should be attached.
1965 * @param array $args Arguments to be passed to the pointer JS (see wp-pointer.js).
1967 private static function print_js( $pointer_id, $selector, $args ) {
1968 if ( empty( $pointer_id ) || empty( $selector ) || empty( $args ) || empty( $args['content'] ) )
1972 <script type="text/javascript">
1975 var options = <?php echo wp_json_encode( $args ); ?>, setup;
1980 options = $.extend( options, {
1983 pointer: '<?php echo $pointer_id; ?>',
1984 action: 'dismiss-wp-pointer'
1989 setup = function() {
1990 $('<?php echo $selector; ?>').first().pointer( options ).pointer('open');
1993 if ( options.position && options.position.defer_loading )
1994 $(window).bind( 'load.wp-pointers', setup );
1996 $(document).ready( setup );
2004 public static function pointer_wp330_toolbar() {}
2005 public static function pointer_wp330_media_uploader() {}
2006 public static function pointer_wp330_saving_widgets() {}
2007 public static function pointer_wp340_customize_current_theme_link() {}
2008 public static function pointer_wp340_choose_image_from_library() {}
2009 public static function pointer_wp350_media() {}
2010 public static function pointer_wp360_revisions() {}
2012 public static function pointer_wp360_locks() {
2013 if ( ! is_multi_author() ) {
2017 $content = '<h3>' . __( 'Edit Lock' ) . '</h3>';
2018 $content .= '<p>' . __( 'Someone else is editing this. No need to refresh; the lock will disappear when they’re done.' ) . '</p>';
2020 self::print_js( 'wp360_locks', 'tr.wp-locked .locked-indicator', array(
2021 'content' => $content,
2022 'position' => array( 'edge' => 'left', 'align' => 'left' ),
2026 public static function pointer_wp390_widgets() {
2027 if ( ! current_theme_supports( 'widgets' ) ) {
2031 $content = '<h3>' . __( 'New Feature: Live Widget Previews' ) . '</h3>';
2032 $content .= '<p>' . __( 'Add, edit, and play around with your widgets from the Customizer.' ) . ' ' . __( 'Preview your changes in real-time and only save them when you’re ready.' ) . '</p>';
2034 if ( 'themes' === get_current_screen()->id ) {
2035 $selector = '.theme.active .customize';
2036 $position = array( 'edge' => is_rtl() ? 'right' : 'left', 'align' => 'center' );
2038 $selector = 'a[href^="customize.php"]';
2040 $position = array( 'edge' => 'right', 'align' => 'center', 'my' => 'right-5px' );
2042 $position = array( 'edge' => 'left', 'align' => 'center', 'my' => 'left-5px' );
2046 self::print_js( 'wp390_widgets', $selector, array(
2047 'content' => $content,
2048 'position' => $position,
2052 public static function pointer_wp410_dfw() {
2053 // Don't show when editor-scrolling is not used.
2054 if ( empty( $GLOBALS['_wp_editor_expand'] ) ) {
2058 $content = '<h3>' . __( 'Distraction-Free Writing' ) . '</h3>';
2059 $content .= '<p>' . __( 'Enable distraction-free writing mode, and everything surrounding the editor will fade away when you start typing. Move your mouse out of the editor to reveal everything again.' ) . '</p>';
2062 $position = array( 'edge' => 'left', 'align' => 'center', 'my' => 'left+40 top-11', 'at' => 'left top' );
2064 $position = array( 'edge' => 'right', 'align' => 'center', 'my' => 'right-40 top-11', 'at' => 'right top' );
2067 self::print_js( 'wp410_dfw', '#wp-content-wrap', array(
2068 'content' => $content,
2069 'position' => $position,
2074 * Prevents new users from seeing existing 'new feature' pointers.
2078 public static function dismiss_pointers_for_new_users( $user_id ) {
2079 add_user_meta( $user_id, 'dismissed_wp_pointers', 'wp360_locks,wp390_widgets' );
2083 add_action( 'admin_enqueue_scripts', array( 'WP_Internal_Pointers', 'enqueue_scripts' ) );
2084 add_action( 'user_register', array( 'WP_Internal_Pointers', 'dismiss_pointers_for_new_users' ) );
2087 * Convert a screen string to a screen object
2091 * @param string $hook_name The hook name (also known as the hook suffix) used to determine the screen.
2092 * @return WP_Screen Screen object.
2094 function convert_to_screen( $hook_name ) {
2095 if ( ! class_exists( 'WP_Screen' ) ) {
2096 _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' );
2097 return (object) array( 'id' => '_invalid', 'base' => '_are_belong_to_us' );
2100 return WP_Screen::get( $hook_name );
2104 * Output the HTML for restoring the post data from DOM storage
2109 function _local_storage_notice() {
2111 <div id="local-storage-notice" class="hidden notice">
2112 <p class="local-restore">
2113 <?php _e('The backup of this post in your browser is different from the version below.'); ?>
2114 <a class="restore-backup" href="#"><?php _e('Restore the backup.'); ?></a>
2116 <p class="undo-restore hidden">
2117 <?php _e('Post restored successfully.'); ?>
2118 <a class="undo-restore-backup" href="#"><?php _e('Undo.'); ?></a>
2125 * Output a HTML element with a star rating for a given rating.
2127 * Outputs a HTML element with the star rating exposed on a 0..5 scale in
2128 * half star increments (ie. 1, 1.5, 2 stars). Optionally, if specified, the
2129 * number of ratings may also be displayed by passing the $number parameter.
2132 * @param array $args {
2133 * Optional. Array of star ratings arguments.
2135 * @type int $rating The rating to display, expressed in either a 0.5 rating increment,
2136 * or percentage. Default 0.
2137 * @type string $type Format that the $rating is in. Valid values are 'rating' (default),
2138 * or, 'percent'. Default 'rating'.
2139 * @type int $number The number of ratings that makes up this rating. Default 0.
2142 function wp_star_rating( $args = array() ) {
2148 $r = wp_parse_args( $args, $defaults );
2150 // Non-english decimal places when the $rating is coming from a string
2151 $rating = str_replace( ',', '.', $r['rating'] );
2153 // Convert Percentage to star rating, 0..5 in .5 increments
2154 if ( 'percent' == $r['type'] ) {
2155 $rating = round( $rating / 10, 0 ) / 2;
2158 // Calculate the number of each type of star needed
2159 $full_stars = floor( $rating );
2160 $half_stars = ceil( $rating - $full_stars );
2161 $empty_stars = 5 - $full_stars - $half_stars;
2163 if ( $r['number'] ) {
2164 /* translators: 1: The rating, 2: The number of ratings */
2165 $format = _n( '%1$s rating based on %2$s rating', '%1$s rating based on %2$s ratings', $r['number'] );
2166 $title = sprintf( $format, number_format_i18n( $rating, 1 ), number_format_i18n( $r['number'] ) );
2168 /* translators: 1: The rating */
2169 $title = sprintf( __( '%s rating' ), number_format_i18n( $rating, 1 ) );
2172 echo '<div class="star-rating" title="' . esc_attr( $title ) . '">';
2173 echo '<span class="screen-reader-text">' . $title . '</span>';
2174 echo str_repeat( '<div class="star star-full"></div>', $full_stars );
2175 echo str_repeat( '<div class="star star-half"></div>', $half_stars );
2176 echo str_repeat( '<div class="star star-empty"></div>', $empty_stars);