PageRenderTime 53ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/APP/wp-admin/includes/template.php

https://bitbucket.org/AFelipeTrujillo/goblog
PHP | 2102 lines | 1176 code | 248 blank | 678 comment | 221 complexity | 3217838dc59cb6a0a29b223a22af2553 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  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. // Category Checklists
  12. //
  13. /**
  14. * Walker to output an unordered list of category checkbox <input> elements.
  15. *
  16. * @see Walker
  17. * @see wp_category_checklist()
  18. * @see wp_terms_checklist()
  19. * @since 2.5.1
  20. */
  21. class Walker_Category_Checklist extends Walker {
  22. var $tree_type = 'category';
  23. var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
  24. /**
  25. * Starts the list before the elements are added.
  26. *
  27. * @see Walker:start_lvl()
  28. *
  29. * @since 2.5.1
  30. *
  31. * @param string $output Passed by reference. Used to append additional content.
  32. * @param int $depth Depth of category. Used for tab indentation.
  33. * @param array $args An array of arguments. @see wp_terms_checklist()
  34. */
  35. function start_lvl( &$output, $depth = 0, $args = array() ) {
  36. $indent = str_repeat("\t", $depth);
  37. $output .= "$indent<ul class='children'>\n";
  38. }
  39. /**
  40. * Ends the list of after the elements are added.
  41. *
  42. * @see Walker::end_lvl()
  43. *
  44. * @since 2.5.1
  45. *
  46. * @param string $output Passed by reference. Used to append additional content.
  47. * @param int $depth Depth of category. Used for tab indentation.
  48. * @param array $args An array of arguments. @see wp_terms_checklist()
  49. */
  50. function end_lvl( &$output, $depth = 0, $args = array() ) {
  51. $indent = str_repeat("\t", $depth);
  52. $output .= "$indent</ul>\n";
  53. }
  54. /**
  55. * Start the element output.
  56. *
  57. * @see Walker::start_el()
  58. *
  59. * @since 2.5.1
  60. *
  61. * @param string $output Passed by reference. Used to append additional content.
  62. * @param object $category The current term object.
  63. * @param int $depth Depth of the term in reference to parents. Default 0.
  64. * @param array $args An array of arguments. @see wp_terms_checklist()
  65. * @param int $id ID of the current term.
  66. */
  67. function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
  68. extract($args);
  69. if ( empty($taxonomy) )
  70. $taxonomy = 'category';
  71. if ( $taxonomy == 'category' )
  72. $name = 'post_category';
  73. else
  74. $name = 'tax_input['.$taxonomy.']';
  75. $class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : '';
  76. /** This filter is documented in wp-includes/category-template.php */
  77. $output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" . '<label class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $category->term_id . '"' . checked( in_array( $category->term_id, $selected_cats ), true, false ) . disabled( empty( $args['disabled'] ), false, false ) . ' /> ' . esc_html( apply_filters( 'the_category', $category->name ) ) . '</label>';
  78. }
  79. /**
  80. * Ends the element output, if needed.
  81. *
  82. * @see Walker::end_el()
  83. *
  84. * @since 2.5.1
  85. *
  86. * @param string $output Passed by reference. Used to append additional content.
  87. * @param object $category The current term object.
  88. * @param int $depth Depth of the term in reference to parents. Default 0.
  89. * @param array $args An array of arguments. @see wp_terms_checklist()
  90. */
  91. function end_el( &$output, $category, $depth = 0, $args = array() ) {
  92. $output .= "</li>\n";
  93. }
  94. }
  95. /**
  96. * Output an unordered list of checkbox <input> elements labelled
  97. * with category names.
  98. *
  99. * @see wp_terms_checklist()
  100. * @since 2.5.1
  101. *
  102. * @param int $post_id Mark categories associated with this post as checked. $selected_cats must not be an array.
  103. * @param int $descendants_and_self ID of the category to output along with its descendents.
  104. * @param bool|array $selected_cats List of categories to mark as checked.
  105. * @param bool|array $popular_cats Override the list of categories that receive the "popular-category" class.
  106. * @param object $walker Walker object to use to build the output.
  107. * @param bool $checked_ontop Move checked items out of the hierarchy and to the top of the list.
  108. */
  109. function wp_category_checklist( $post_id = 0, $descendants_and_self = 0, $selected_cats = false, $popular_cats = false, $walker = null, $checked_ontop = true ) {
  110. wp_terms_checklist( $post_id, array(
  111. 'taxonomy' => 'category',
  112. 'descendants_and_self' => $descendants_and_self,
  113. 'selected_cats' => $selected_cats,
  114. 'popular_cats' => $popular_cats,
  115. 'walker' => $walker,
  116. 'checked_ontop' => $checked_ontop
  117. ) );
  118. }
  119. /**
  120. * Output an unordered list of checkbox <input> elements labelled
  121. * with term names. Taxonomy independent version of wp_category_checklist().
  122. *
  123. * @since 3.0.0
  124. *
  125. * @param int $post_id
  126. * @param array $args
  127. */
  128. function wp_terms_checklist($post_id = 0, $args = array()) {
  129. $defaults = array(
  130. 'descendants_and_self' => 0,
  131. 'selected_cats' => false,
  132. 'popular_cats' => false,
  133. 'walker' => null,
  134. 'taxonomy' => 'category',
  135. 'checked_ontop' => true
  136. );
  137. /**
  138. * Filter the taxonomy terms checklist arguments.
  139. *
  140. * @since 3.4.0
  141. *
  142. * @see wp_terms_checklist()
  143. *
  144. * @param array $args An array of arguments.
  145. * @param int $post_id The post ID.
  146. */
  147. $args = apply_filters( 'wp_terms_checklist_args', $args, $post_id );
  148. extract( wp_parse_args($args, $defaults), EXTR_SKIP );
  149. if ( empty($walker) || !is_a($walker, 'Walker') )
  150. $walker = new Walker_Category_Checklist;
  151. $descendants_and_self = (int) $descendants_and_self;
  152. $args = array('taxonomy' => $taxonomy);
  153. $tax = get_taxonomy($taxonomy);
  154. $args['disabled'] = !current_user_can($tax->cap->assign_terms);
  155. if ( is_array( $selected_cats ) )
  156. $args['selected_cats'] = $selected_cats;
  157. elseif ( $post_id )
  158. $args['selected_cats'] = wp_get_object_terms($post_id, $taxonomy, array_merge($args, array('fields' => 'ids')));
  159. else
  160. $args['selected_cats'] = array();
  161. if ( is_array( $popular_cats ) )
  162. $args['popular_cats'] = $popular_cats;
  163. else
  164. $args['popular_cats'] = get_terms( $taxonomy, array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
  165. if ( $descendants_and_self ) {
  166. $categories = (array) get_terms($taxonomy, array( 'child_of' => $descendants_and_self, 'hierarchical' => 0, 'hide_empty' => 0 ) );
  167. $self = get_term( $descendants_and_self, $taxonomy );
  168. array_unshift( $categories, $self );
  169. } else {
  170. $categories = (array) get_terms($taxonomy, array('get' => 'all'));
  171. }
  172. if ( $checked_ontop ) {
  173. // 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)
  174. $checked_categories = array();
  175. $keys = array_keys( $categories );
  176. foreach( $keys as $k ) {
  177. if ( in_array( $categories[$k]->term_id, $args['selected_cats'] ) ) {
  178. $checked_categories[] = $categories[$k];
  179. unset( $categories[$k] );
  180. }
  181. }
  182. // Put checked cats on top
  183. echo call_user_func_array(array(&$walker, 'walk'), array($checked_categories, 0, $args));
  184. }
  185. // Then the rest of them
  186. echo call_user_func_array(array(&$walker, 'walk'), array($categories, 0, $args));
  187. }
  188. /**
  189. * Retrieve a list of the most popular terms from the specified taxonomy.
  190. *
  191. * If the $echo argument is true then the elements for a list of checkbox
  192. * <input> elements labelled with the names of the selected terms is output.
  193. * If the $post_ID global isn't empty then the terms associated with that
  194. * post will be marked as checked.
  195. *
  196. * @since 2.5.0
  197. *
  198. * @param string $taxonomy Taxonomy to retrieve terms from.
  199. * @param int $default Unused.
  200. * @param int $number Number of terms to retrieve. Defaults to 10.
  201. * @param bool $echo Optionally output the list as well. Defaults to true.
  202. * @return array List of popular term IDs.
  203. */
  204. function wp_popular_terms_checklist( $taxonomy, $default = 0, $number = 10, $echo = true ) {
  205. $post = get_post();
  206. if ( $post && $post->ID )
  207. $checked_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields'=>'ids'));
  208. else
  209. $checked_terms = array();
  210. $terms = get_terms( $taxonomy, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => $number, 'hierarchical' => false ) );
  211. $tax = get_taxonomy($taxonomy);
  212. $popular_ids = array();
  213. foreach ( (array) $terms as $term ) {
  214. $popular_ids[] = $term->term_id;
  215. if ( !$echo ) // hack for AJAX use
  216. continue;
  217. $id = "popular-$taxonomy-$term->term_id";
  218. $checked = in_array( $term->term_id, $checked_terms ) ? 'checked="checked"' : '';
  219. ?>
  220. <li id="<?php echo $id; ?>" class="popular-category">
  221. <label class="selectit">
  222. <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 ) ); ?> />
  223. <?php
  224. /** This filter is documented in wp-includes/category-template.php */
  225. echo esc_html( apply_filters( 'the_category', $term->name ) );
  226. ?>
  227. </label>
  228. </li>
  229. <?php
  230. }
  231. return $popular_ids;
  232. }
  233. /**
  234. * {@internal Missing Short Description}}
  235. *
  236. * @since 2.5.1
  237. *
  238. * @param unknown_type $link_id
  239. */
  240. function wp_link_category_checklist( $link_id = 0 ) {
  241. $default = 1;
  242. if ( $link_id ) {
  243. $checked_categories = wp_get_link_cats( $link_id );
  244. // No selected categories, strange
  245. if ( ! count( $checked_categories ) )
  246. $checked_categories[] = $default;
  247. } else {
  248. $checked_categories[] = $default;
  249. }
  250. $categories = get_terms( 'link_category', array( 'orderby' => 'name', 'hide_empty' => 0 ) );
  251. if ( empty( $categories ) )
  252. return;
  253. foreach ( $categories as $category ) {
  254. $cat_id = $category->term_id;
  255. /** This filter is documented in wp-includes/category-template.php */
  256. $name = esc_html( apply_filters( 'the_category', $category->name ) );
  257. $checked = in_array( $cat_id, $checked_categories ) ? ' checked="checked"' : '';
  258. 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>";
  259. }
  260. }
  261. // adds hidden fields with the data for use in the inline editor for posts and pages
  262. /**
  263. * {@internal Missing Short Description}}
  264. *
  265. * @since 2.7.0
  266. *
  267. * @param unknown_type $post
  268. */
  269. function get_inline_data($post) {
  270. $post_type_object = get_post_type_object($post->post_type);
  271. if ( ! current_user_can( 'edit_post', $post->ID ) )
  272. return;
  273. $title = esc_textarea( trim( $post->post_title ) );
  274. /** This filter is documented in wp-admin/edit-tag-form.php */
  275. echo '
  276. <div class="hidden" id="inline_' . $post->ID . '">
  277. <div class="post_title">' . $title . '</div>
  278. <div class="post_name">' . apply_filters( 'editable_slug', $post->post_name ) . '</div>
  279. <div class="post_author">' . $post->post_author . '</div>
  280. <div class="comment_status">' . esc_html( $post->comment_status ) . '</div>
  281. <div class="ping_status">' . esc_html( $post->ping_status ) . '</div>
  282. <div class="_status">' . esc_html( $post->post_status ) . '</div>
  283. <div class="jj">' . mysql2date( 'd', $post->post_date, false ) . '</div>
  284. <div class="mm">' . mysql2date( 'm', $post->post_date, false ) . '</div>
  285. <div class="aa">' . mysql2date( 'Y', $post->post_date, false ) . '</div>
  286. <div class="hh">' . mysql2date( 'H', $post->post_date, false ) . '</div>
  287. <div class="mn">' . mysql2date( 'i', $post->post_date, false ) . '</div>
  288. <div class="ss">' . mysql2date( 's', $post->post_date, false ) . '</div>
  289. <div class="post_password">' . esc_html( $post->post_password ) . '</div>';
  290. if ( $post_type_object->hierarchical )
  291. echo '<div class="post_parent">' . $post->post_parent . '</div>';
  292. if ( $post->post_type == 'page' )
  293. echo '<div class="page_template">' . esc_html( get_post_meta( $post->ID, '_wp_page_template', true ) ) . '</div>';
  294. if ( post_type_supports( $post->post_type, 'page-attributes' ) )
  295. echo '<div class="menu_order">' . $post->menu_order . '</div>';
  296. $taxonomy_names = get_object_taxonomies( $post->post_type );
  297. foreach ( $taxonomy_names as $taxonomy_name) {
  298. $taxonomy = get_taxonomy( $taxonomy_name );
  299. if ( $taxonomy->hierarchical && $taxonomy->show_ui ) {
  300. echo '<div class="post_category" id="' . $taxonomy_name . '_' . $post->ID . '">'
  301. . implode( ',', wp_get_object_terms( $post->ID, $taxonomy_name, array( 'fields' => 'ids' ) ) ) . '</div>';
  302. } elseif ( $taxonomy->show_ui ) {
  303. echo '<div class="tags_input" id="'.$taxonomy_name.'_'.$post->ID.'">'
  304. . esc_html( str_replace( ',', ', ', get_terms_to_edit( $post->ID, $taxonomy_name ) ) ) . '</div>';
  305. }
  306. }
  307. if ( !$post_type_object->hierarchical )
  308. echo '<div class="sticky">' . (is_sticky($post->ID) ? 'sticky' : '') . '</div>';
  309. if ( post_type_supports( $post->post_type, 'post-formats' ) )
  310. echo '<div class="post_format">' . esc_html( get_post_format( $post->ID ) ) . '</div>';
  311. echo '</div>';
  312. }
  313. /**
  314. * {@internal Missing Short Description}}
  315. *
  316. * @since 2.7.0
  317. *
  318. * @param unknown_type $position
  319. * @param unknown_type $checkbox
  320. * @param unknown_type $mode
  321. */
  322. function wp_comment_reply($position = '1', $checkbox = false, $mode = 'single', $table_row = true) {
  323. /**
  324. * Filter the in-line comment reply-to form output in the Comments
  325. * list table.
  326. *
  327. * Returning a non-empty value here will short-circuit display
  328. * of the in-line comment-reply form in the Comments list table,
  329. * echoing the returned value instead.
  330. *
  331. * @since 2.7.0
  332. *
  333. * @see wp_comment_reply()
  334. *
  335. * @param string $content The reply-to form content.
  336. * @param array $args An array of default args.
  337. */
  338. $content = apply_filters( 'wp_comment_reply', '', array( 'position' => $position, 'checkbox' => $checkbox, 'mode' => $mode ) );
  339. if ( ! empty($content) ) {
  340. echo $content;
  341. return;
  342. }
  343. if ( $mode == 'single' ) {
  344. $wp_list_table = _get_list_table('WP_Post_Comments_List_Table');
  345. } else {
  346. $wp_list_table = _get_list_table('WP_Comments_List_Table');
  347. }
  348. ?>
  349. <form method="get" action="">
  350. <?php if ( $table_row ) : ?>
  351. <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">
  352. <?php else : ?>
  353. <div id="com-reply" style="display:none;"><div id="replyrow" style="display:none;">
  354. <?php endif; ?>
  355. <div id="replyhead" style="display:none;"><h5><?php _e( 'Reply to Comment' ); ?></h5></div>
  356. <div id="addhead" style="display:none;"><h5><?php _e('Add new Comment'); ?></h5></div>
  357. <div id="edithead" style="display:none;">
  358. <div class="inside">
  359. <label for="author"><?php _e('Name') ?></label>
  360. <input type="text" name="newcomment_author" size="50" value="" id="author" />
  361. </div>
  362. <div class="inside">
  363. <label for="author-email"><?php _e('E-mail') ?></label>
  364. <input type="text" name="newcomment_author_email" size="50" value="" id="author-email" />
  365. </div>
  366. <div class="inside">
  367. <label for="author-url"><?php _e('URL') ?></label>
  368. <input type="text" id="author-url" name="newcomment_author_url" size="103" value="" />
  369. </div>
  370. <div style="clear:both;"></div>
  371. </div>
  372. <div id="replycontainer">
  373. <?php
  374. $quicktags_settings = array( 'buttons' => 'strong,em,link,block,del,ins,img,ul,ol,li,code,close' );
  375. wp_editor( '', 'replycontent', array( 'media_buttons' => false, 'tinymce' => false, 'quicktags' => $quicktags_settings ) );
  376. ?>
  377. </div>
  378. <p id="replysubmit" class="submit">
  379. <a href="#comments-form" class="save button-primary alignright">
  380. <span id="addbtn" style="display:none;"><?php _e('Add Comment'); ?></span>
  381. <span id="savebtn" style="display:none;"><?php _e('Update Comment'); ?></span>
  382. <span id="replybtn" style="display:none;"><?php _e('Submit Reply'); ?></span></a>
  383. <a href="#comments-form" class="cancel button-secondary alignleft"><?php _e('Cancel'); ?></a>
  384. <span class="waiting spinner"></span>
  385. <span class="error" style="display:none;"></span>
  386. <br class="clear" />
  387. </p>
  388. <input type="hidden" name="user_ID" id="user_ID" value="<?php echo get_current_user_id(); ?>" />
  389. <input type="hidden" name="action" id="action" value="" />
  390. <input type="hidden" name="comment_ID" id="comment_ID" value="" />
  391. <input type="hidden" name="comment_post_ID" id="comment_post_ID" value="" />
  392. <input type="hidden" name="status" id="status" value="" />
  393. <input type="hidden" name="position" id="position" value="<?php echo $position; ?>" />
  394. <input type="hidden" name="checkbox" id="checkbox" value="<?php echo $checkbox ? 1 : 0; ?>" />
  395. <input type="hidden" name="mode" id="mode" value="<?php echo esc_attr($mode); ?>" />
  396. <?php
  397. wp_nonce_field( 'replyto-comment', '_ajax_nonce-replyto-comment', false );
  398. if ( current_user_can( 'unfiltered_html' ) )
  399. wp_nonce_field( 'unfiltered-html-comment', '_wp_unfiltered_html_comment', false );
  400. ?>
  401. <?php if ( $table_row ) : ?>
  402. </td></tr></tbody></table>
  403. <?php else : ?>
  404. </div></div>
  405. <?php endif; ?>
  406. </form>
  407. <?php
  408. }
  409. /**
  410. * Output 'undo move to trash' text for comments
  411. *
  412. * @since 2.9.0
  413. */
  414. function wp_comment_trashnotice() {
  415. ?>
  416. <div class="hidden" id="trash-undo-holder">
  417. <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>
  418. </div>
  419. <div class="hidden" id="spam-undo-holder">
  420. <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>
  421. </div>
  422. <?php
  423. }
  424. /**
  425. * {@internal Missing Short Description}}
  426. *
  427. * @since 1.2.0
  428. *
  429. * @param unknown_type $meta
  430. */
  431. function list_meta( $meta ) {
  432. // Exit if no meta
  433. if ( ! $meta ) {
  434. echo '
  435. <table id="list-table" style="display: none;">
  436. <thead>
  437. <tr>
  438. <th class="left">' . _x( 'Name', 'meta name' ) . '</th>
  439. <th>' . __( 'Value' ) . '</th>
  440. </tr>
  441. </thead>
  442. <tbody id="the-list" data-wp-lists="list:meta">
  443. <tr><td></td></tr>
  444. </tbody>
  445. </table>'; //TBODY needed for list-manipulation JS
  446. return;
  447. }
  448. $count = 0;
  449. ?>
  450. <table id="list-table">
  451. <thead>
  452. <tr>
  453. <th class="left"><?php _ex( 'Name', 'meta name' ) ?></th>
  454. <th><?php _e( 'Value' ) ?></th>
  455. </tr>
  456. </thead>
  457. <tbody id='the-list' data-wp-lists='list:meta'>
  458. <?php
  459. foreach ( $meta as $entry )
  460. echo _list_meta_row( $entry, $count );
  461. ?>
  462. </tbody>
  463. </table>
  464. <?php
  465. }
  466. /**
  467. * {@internal Missing Short Description}}
  468. *
  469. * @since 2.5.0
  470. *
  471. * @param unknown_type $entry
  472. * @param unknown_type $count
  473. * @return unknown
  474. */
  475. function _list_meta_row( $entry, &$count ) {
  476. static $update_nonce = false;
  477. if ( is_protected_meta( $entry['meta_key'], 'post' ) )
  478. return;
  479. if ( !$update_nonce )
  480. $update_nonce = wp_create_nonce( 'add-meta' );
  481. $r = '';
  482. ++ $count;
  483. if ( $count % 2 )
  484. $style = 'alternate';
  485. else
  486. $style = '';
  487. if ( is_serialized( $entry['meta_value'] ) ) {
  488. if ( is_serialized_string( $entry['meta_value'] ) ) {
  489. // this is a serialized string, so we should display it
  490. $entry['meta_value'] = maybe_unserialize( $entry['meta_value'] );
  491. } else {
  492. // this is a serialized array/object so we should NOT display it
  493. --$count;
  494. return;
  495. }
  496. }
  497. $entry['meta_key'] = esc_attr($entry['meta_key']);
  498. $entry['meta_value'] = esc_textarea( $entry['meta_value'] ); // using a <textarea />
  499. $entry['meta_id'] = (int) $entry['meta_id'];
  500. $delete_nonce = wp_create_nonce( 'delete-meta_' . $entry['meta_id'] );
  501. $r .= "\n\t<tr id='meta-{$entry['meta_id']}' class='$style'>";
  502. $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']}' />";
  503. $r .= "\n\t\t<div class='submit'>";
  504. $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" ) );
  505. $r .= "\n\t\t";
  506. $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" ) );
  507. $r .= "</div>";
  508. $r .= wp_nonce_field( 'change-meta', '_ajax_nonce', false, false );
  509. $r .= "</td>";
  510. $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>";
  511. return $r;
  512. }
  513. /**
  514. * Prints the form in the Custom Fields meta box.
  515. *
  516. * @since 1.2.0
  517. *
  518. * @param WP_Post $post Optional. The post being edited.
  519. */
  520. function meta_form( $post = null ) {
  521. global $wpdb;
  522. $post = get_post( $post );
  523. /**
  524. * Filter the number of custom fields to retrieve for the drop-down
  525. * in the Custom Fields meta box.
  526. *
  527. * @since 2.1.0
  528. *
  529. * @param int $limit Number of custom fields to retrieve. Default 30.
  530. */
  531. $limit = (int) apply_filters( 'postmeta_form_limit', 30 );
  532. $keys = $wpdb->get_col( "
  533. SELECT meta_key
  534. FROM $wpdb->postmeta
  535. GROUP BY meta_key
  536. HAVING meta_key NOT LIKE '\_%'
  537. ORDER BY meta_key
  538. LIMIT $limit" );
  539. if ( $keys ) {
  540. natcasesort( $keys );
  541. $meta_key_input_id = 'metakeyselect';
  542. } else {
  543. $meta_key_input_id = 'metakeyinput';
  544. }
  545. ?>
  546. <p><strong><?php _e( 'Add New Custom Field:' ) ?></strong></p>
  547. <table id="newmeta">
  548. <thead>
  549. <tr>
  550. <th class="left"><label for="<?php echo $meta_key_input_id; ?>"><?php _ex( 'Name', 'meta name' ) ?></label></th>
  551. <th><label for="metavalue"><?php _e( 'Value' ) ?></label></th>
  552. </tr>
  553. </thead>
  554. <tbody>
  555. <tr>
  556. <td id="newmetaleft" class="left">
  557. <?php if ( $keys ) { ?>
  558. <select id="metakeyselect" name="metakeyselect">
  559. <option value="#NONE#"><?php _e( '&mdash; Select &mdash;' ); ?></option>
  560. <?php
  561. foreach ( $keys as $key ) {
  562. if ( is_protected_meta( $key, 'post' ) || ! current_user_can( 'add_post_meta', $post->ID, $key ) )
  563. continue;
  564. echo "\n<option value='" . esc_attr($key) . "'>" . esc_html($key) . "</option>";
  565. }
  566. ?>
  567. </select>
  568. <input class="hide-if-js" type="text" id="metakeyinput" name="metakeyinput" value="" />
  569. <a href="#postcustomstuff" class="hide-if-no-js" onclick="jQuery('#metakeyinput, #metakeyselect, #enternew, #cancelnew').toggle();return false;">
  570. <span id="enternew"><?php _e('Enter new'); ?></span>
  571. <span id="cancelnew" class="hidden"><?php _e('Cancel'); ?></span></a>
  572. <?php } else { ?>
  573. <input type="text" id="metakeyinput" name="metakeyinput" value="" />
  574. <?php } ?>
  575. </td>
  576. <td><textarea id="metavalue" name="metavalue" rows="2" cols="25"></textarea></td>
  577. </tr>
  578. <tr><td colspan="2">
  579. <div class="submit">
  580. <?php submit_button( __( 'Add Custom Field' ), 'secondary', 'addmeta', false, array( 'id' => 'newmeta-submit', 'data-wp-lists' => 'add:the-list:newmeta' ) ); ?>
  581. </div>
  582. <?php wp_nonce_field( 'add-meta', '_ajax_nonce-add-meta', false ); ?>
  583. </td></tr>
  584. </tbody>
  585. </table>
  586. <?php
  587. }
  588. /**
  589. * Print out HTML form date elements for editing post or comment publish date.
  590. *
  591. * @since 0.71
  592. *
  593. * @param int|bool $edit Accepts 1|true for editing the date, 0|false for adding the date.
  594. * @param int|bool $for_post Accepts 1|true for applying the date to a post, 0|false for a comment.
  595. * @param int|bool $tab_index The tabindex attribute to add. Default 0.
  596. * @param int|bool $multi Optional. Whether the additional fields and buttons should be added.
  597. * Default 0|false.
  598. */
  599. function touch_time( $edit = 1, $for_post = 1, $tab_index = 0, $multi = 0 ) {
  600. global $wp_locale, $comment;
  601. $post = get_post();
  602. if ( $for_post )
  603. $edit = ! ( in_array($post->post_status, array('draft', 'pending') ) && (!$post->post_date_gmt || '0000-00-00 00:00:00' == $post->post_date_gmt ) );
  604. $tab_index_attribute = '';
  605. if ( (int) $tab_index > 0 )
  606. $tab_index_attribute = " tabindex=\"$tab_index\"";
  607. // 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 />';
  608. $time_adj = current_time('timestamp');
  609. $post_date = ($for_post) ? $post->post_date : $comment->comment_date;
  610. $jj = ($edit) ? mysql2date( 'd', $post_date, false ) : gmdate( 'd', $time_adj );
  611. $mm = ($edit) ? mysql2date( 'm', $post_date, false ) : gmdate( 'm', $time_adj );
  612. $aa = ($edit) ? mysql2date( 'Y', $post_date, false ) : gmdate( 'Y', $time_adj );
  613. $hh = ($edit) ? mysql2date( 'H', $post_date, false ) : gmdate( 'H', $time_adj );
  614. $mn = ($edit) ? mysql2date( 'i', $post_date, false ) : gmdate( 'i', $time_adj );
  615. $ss = ($edit) ? mysql2date( 's', $post_date, false ) : gmdate( 's', $time_adj );
  616. $cur_jj = gmdate( 'd', $time_adj );
  617. $cur_mm = gmdate( 'm', $time_adj );
  618. $cur_aa = gmdate( 'Y', $time_adj );
  619. $cur_hh = gmdate( 'H', $time_adj );
  620. $cur_mn = gmdate( 'i', $time_adj );
  621. $month = "<select " . ( $multi ? '' : 'id="mm" ' ) . "name=\"mm\"$tab_index_attribute>\n";
  622. for ( $i = 1; $i < 13; $i = $i +1 ) {
  623. $monthnum = zeroise($i, 2);
  624. $month .= "\t\t\t" . '<option value="' . $monthnum . '" ' . selected( $monthnum, $mm, false ) . '>';
  625. /* translators: 1: month number (01, 02, etc.), 2: month abbreviation */
  626. $month .= sprintf( __( '%1$s-%2$s' ), $monthnum, $wp_locale->get_month_abbrev( $wp_locale->get_month( $i ) ) ) . "</option>\n";
  627. }
  628. $month .= '</select>';
  629. $day = '<input type="text" ' . ( $multi ? '' : 'id="jj" ' ) . 'name="jj" value="' . $jj . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />';
  630. $year = '<input type="text" ' . ( $multi ? '' : 'id="aa" ' ) . 'name="aa" value="' . $aa . '" size="4" maxlength="4"' . $tab_index_attribute . ' autocomplete="off" />';
  631. $hour = '<input type="text" ' . ( $multi ? '' : 'id="hh" ' ) . 'name="hh" value="' . $hh . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />';
  632. $minute = '<input type="text" ' . ( $multi ? '' : 'id="mn" ' ) . 'name="mn" value="' . $mn . '" size="2" maxlength="2"' . $tab_index_attribute . ' autocomplete="off" />';
  633. echo '<div class="timestamp-wrap">';
  634. /* translators: 1: month, 2: day, 3: year, 4: hour, 5: minute */
  635. printf( __( '%1$s %2$s, %3$s @ %4$s : %5$s' ), $month, $day, $year, $hour, $minute );
  636. echo '</div><input type="hidden" id="ss" name="ss" value="' . $ss . '" />';
  637. if ( $multi ) return;
  638. echo "\n\n";
  639. foreach ( array('mm', 'jj', 'aa', 'hh', 'mn') as $timeunit ) {
  640. echo '<input type="hidden" id="hidden_' . $timeunit . '" name="hidden_' . $timeunit . '" value="' . $$timeunit . '" />' . "\n";
  641. $cur_timeunit = 'cur_' . $timeunit;
  642. echo '<input type="hidden" id="' . $cur_timeunit . '" name="' . $cur_timeunit . '" value="' . $$cur_timeunit . '" />' . "\n";
  643. }
  644. ?>
  645. <p>
  646. <a href="#edit_timestamp" class="save-timestamp hide-if-no-js button"><?php _e('OK'); ?></a>
  647. <a href="#edit_timestamp" class="cancel-timestamp hide-if-no-js button-cancel"><?php _e('Cancel'); ?></a>
  648. </p>
  649. <?php
  650. }
  651. /**
  652. * Print out <option> HTML elements for the page templates drop-down.
  653. *
  654. * @since 1.5.0
  655. *
  656. * @param string $default Optional. The template file name. Default empty.
  657. */
  658. function page_template_dropdown( $default = '' ) {
  659. $templates = get_page_templates( get_post() );
  660. ksort( $templates );
  661. foreach ( array_keys( $templates ) as $template ) {
  662. $selected = selected( $default, $templates[ $template ], false );
  663. echo "\n\t<option value='" . $templates[ $template ] . "' $selected>$template</option>";
  664. }
  665. }
  666. /**
  667. * Print out <option> HTML elements for the page parents drop-down.
  668. *
  669. * @since 1.5.0
  670. *
  671. * @param int $default Optional. The default page ID to be pre-selected. Default 0.
  672. * @param int $parent Optional. The parent page ID. Default 0.
  673. * @param int $level Optional. Page depth level. Default 0.
  674. *
  675. * @return void|bool Boolean False if page has no children, otherwise print out html elements
  676. */
  677. function parent_dropdown( $default = 0, $parent = 0, $level = 0 ) {
  678. global $wpdb;
  679. $post = get_post();
  680. $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) );
  681. if ( $items ) {
  682. foreach ( $items as $item ) {
  683. // A page cannot be its own parent.
  684. if ( $post && $post->ID && $item->ID == $post->ID )
  685. continue;
  686. $pad = str_repeat( '&nbsp;', $level * 3 );
  687. $selected = selected( $default, $item->ID, false );
  688. echo "\n\t<option class='level-$level' value='$item->ID' $selected>$pad " . esc_html($item->post_title) . "</option>";
  689. parent_dropdown( $default, $item->ID, $level +1 );
  690. }
  691. } else {
  692. return false;
  693. }
  694. }
  695. /**
  696. * Print out <option> html elements for role selectors
  697. *
  698. * @since 2.1.0
  699. *
  700. * @param string $selected slug for the role that should be already selected
  701. */
  702. function wp_dropdown_roles( $selected = false ) {
  703. $p = '';
  704. $r = '';
  705. $editable_roles = array_reverse( get_editable_roles() );
  706. foreach ( $editable_roles as $role => $details ) {
  707. $name = translate_user_role($details['name'] );
  708. if ( $selected == $role ) // preselect specified role
  709. $p = "\n\t<option selected='selected' value='" . esc_attr($role) . "'>$name</option>";
  710. else
  711. $r .= "\n\t<option value='" . esc_attr($role) . "'>$name</option>";
  712. }
  713. echo $p . $r;
  714. }
  715. /**
  716. * Outputs the form used by the importers to accept the data to be imported
  717. *
  718. * @since 2.0.0
  719. *
  720. * @param string $action The action attribute for the form.
  721. */
  722. function wp_import_upload_form( $action ) {
  723. /**
  724. * Filter the maximum allowed upload size for import files.
  725. *
  726. * @since 2.3.0
  727. *
  728. * @see wp_max_upload_size()
  729. *
  730. * @param int $max_upload_size Allowed upload size. Default 1 MB.
  731. */
  732. $bytes = apply_filters( 'import_upload_size_limit', wp_max_upload_size() );
  733. $size = size_format( $bytes );
  734. $upload_dir = wp_upload_dir();
  735. if ( ! empty( $upload_dir['error'] ) ) :
  736. ?><div class="error"><p><?php _e('Before you can upload your import file, you will need to fix the following error:'); ?></p>
  737. <p><strong><?php echo $upload_dir['error']; ?></strong></p></div><?php
  738. else :
  739. ?>
  740. <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' ) ); ?>">
  741. <p>
  742. <label for="upload"><?php _e( 'Choose a file from your computer:' ); ?></label> (<?php printf( __('Maximum size: %s' ), $size ); ?>)
  743. <input type="file" id="upload" name="import" size="25" />
  744. <input type="hidden" name="action" value="save" />
  745. <input type="hidden" name="max_file_size" value="<?php echo $bytes; ?>" />
  746. </p>
  747. <?php submit_button( __('Upload file and import'), 'button' ); ?>
  748. </form>
  749. <?php
  750. endif;
  751. }
  752. /**
  753. * Add a meta box to an edit form.
  754. *
  755. * @since 2.5.0
  756. *
  757. * @param string $id String for use in the 'id' attribute of tags.
  758. * @param string $title Title of the meta box.
  759. * @param callback $callback Function that fills the box with the desired content.
  760. * The function should echo its output.
  761. * @param string|WP_Screen $screen Optional. The screen on which to show the box (like a post
  762. * type, 'link', or 'comment'). Default is the current screen.
  763. * @param string $context Optional. The context within the screen where the boxes
  764. * should display. Available contexts vary from screen to
  765. * screen. Post edit screen contexts include 'normal', 'side',
  766. * and 'advanced'. Comments screen contexts include 'normal'
  767. * and 'side'. Menus meta boxes (accordion sections) all use
  768. * the 'side' context. Global default is 'advanced'.
  769. * @param string $priority Optional. The priority within the context where the boxes
  770. * should show ('high', 'low'). Default 'default'.
  771. * @param array $callback_args Optional. Data that should be set as the $args property
  772. * of the box array (which is the second parameter passed
  773. * to your callback). Default null.
  774. */
  775. function add_meta_box( $id, $title, $callback, $screen = null, $context = 'advanced', $priority = 'default', $callback_args = null ) {
  776. global $wp_meta_boxes;
  777. if ( empty( $screen ) )
  778. $screen = get_current_screen();
  779. elseif ( is_string( $screen ) )
  780. $screen = convert_to_screen( $screen );
  781. $page = $screen->id;
  782. if ( !isset($wp_meta_boxes) )
  783. $wp_meta_boxes = array();
  784. if ( !isset($wp_meta_boxes[$page]) )
  785. $wp_meta_boxes[$page] = array();
  786. if ( !isset($wp_meta_boxes[$page][$context]) )
  787. $wp_meta_boxes[$page][$context] = array();
  788. foreach ( array_keys($wp_meta_boxes[$page]) as $a_context ) {
  789. foreach ( array('high', 'core', 'default', 'low') as $a_priority ) {
  790. if ( !isset($wp_meta_boxes[$page][$a_context][$a_priority][$id]) )
  791. continue;
  792. // If a core box was previously added or removed by a plugin, don't add.
  793. if ( 'core' == $priority ) {
  794. // If core box previously deleted, don't add
  795. if ( false === $wp_meta_boxes[$page][$a_context][$a_priority][$id] )
  796. return;
  797. // If box was added with default priority, give it core priority to maintain sort order
  798. if ( 'default' == $a_priority ) {
  799. $wp_meta_boxes[$page][$a_context]['core'][$id] = $wp_meta_boxes[$page][$a_context]['default'][$id];
  800. unset($wp_meta_boxes[$page][$a_context]['default'][$id]);
  801. }
  802. return;
  803. }
  804. // If no priority given and id already present, use existing priority
  805. if ( empty($priority) ) {
  806. $priority = $a_priority;
  807. // else if we're adding to the sorted priority, we don't know the title or callback. Grab them from the previously added context/priority.
  808. } elseif ( 'sorted' == $priority ) {
  809. $title = $wp_meta_boxes[$page][$a_context][$a_priority][$id]['title'];
  810. $callback = $wp_meta_boxes[$page][$a_context][$a_priority][$id]['callback'];
  811. $callback_args = $wp_meta_boxes[$page][$a_context][$a_priority][$id]['args'];
  812. }
  813. // An id can be in only one priority and one context
  814. if ( $priority != $a_priority || $context != $a_context )
  815. unset($wp_meta_boxes[$page][$a_context][$a_priority][$id]);
  816. }
  817. }
  818. if ( empty($priority) )
  819. $priority = 'low';
  820. if ( !isset($wp_meta_boxes[$page][$context][$priority]) )
  821. $wp_meta_boxes[$page][$context][$priority] = array();
  822. $wp_meta_boxes[$page][$context][$priority][$id] = array('id' => $id, 'title' => $title, 'callback' => $callback, 'args' => $callback_args);
  823. }
  824. /**
  825. * Meta-Box template function
  826. *
  827. * @since 2.5.0
  828. *
  829. * @param string|object $screen Screen identifier
  830. * @param string $context box context
  831. * @param mixed $object gets passed to the box callback function as first parameter
  832. * @return int number of meta_boxes
  833. */
  834. function do_meta_boxes( $screen, $context, $object ) {
  835. global $wp_meta_boxes;
  836. static $already_sorted = false;
  837. if ( empty( $screen ) )
  838. $screen = get_current_screen();
  839. elseif ( is_string( $screen ) )
  840. $screen = convert_to_screen( $screen );
  841. $page = $screen->id;
  842. $hidden = get_hidden_meta_boxes( $screen );
  843. printf('<div id="%s-sortables" class="meta-box-sortables">', htmlspecialchars($context));
  844. $i = 0;
  845. do {
  846. // Grab the ones the user has manually sorted. Pull them out of their previous context/priority and into the one the user chose
  847. if ( !$already_sorted && $sorted = get_user_option( "meta-box-order_$page" ) ) {
  848. foreach ( $sorted as $box_context => $ids ) {
  849. foreach ( explode(',', $ids ) as $id ) {
  850. if ( $id && 'dashboard_browser_nag' !== $id )
  851. add_meta_box( $id, null, null, $screen, $box_context, 'sorted' );
  852. }
  853. }
  854. }
  855. $already_sorted = true;
  856. if ( !isset($wp_meta_boxes) || !isset($wp_meta_boxes[$page]) || !isset($wp_meta_boxes[$page][$context]) )
  857. break;
  858. foreach ( array('high', 'sorted', 'core', 'default', 'low') as $priority ) {
  859. if ( isset($wp_meta_boxes[$page][$context][$priority]) ) {
  860. foreach ( (array) $wp_meta_boxes[$page][$context][$priority] as $box ) {
  861. if ( false == $box || ! $box['title'] )
  862. continue;
  863. $i++;
  864. $hidden_class = in_array($box['id'], $hidden) ? ' hide-if-js' : '';
  865. echo '<div id="' . $box['id'] . '" class="postbox ' . postbox_classes($box['id'], $page) . $hidden_class . '" ' . '>' . "\n";
  866. if ( 'dashboard_browser_nag' != $box['id'] )
  867. echo '<div class="handlediv" title="' . esc_attr__('Click to toggle') . '"><br /></div>';
  868. echo "<h3 class='hndle'><span>{$box['title']}</span></h3>\n";
  869. echo '<div class="inside">' . "\n";
  870. call_user_func($box['callback'], $object, $box);
  871. echo "</div>\n";
  872. echo "</div>\n";
  873. }
  874. }
  875. }
  876. } while(0);
  877. echo "</div>";
  878. return $i;
  879. }
  880. /**
  881. * Remove a meta box from an edit form.
  882. *
  883. * @since 2.6.0
  884. *
  885. * @param string $id String for use in the 'id' attribute of tags.
  886. * @param string|object $screen The screen on which to show the box (post, page, link).
  887. * @param string $context The context within the page where the boxes should show ('normal', 'advanced').
  888. */
  889. function remove_meta_box($id, $screen, $context) {
  890. global $wp_meta_boxes;
  891. if ( empty( $screen ) )
  892. $screen = get_current_screen();
  893. elseif ( is_string( $screen ) )
  894. $screen = convert_to_screen( $screen );
  895. $page = $screen->id;
  896. if ( !isset($wp_meta_boxes) )
  897. $wp_meta_boxes = array();
  898. if ( !isset($wp_meta_boxes[$page]) )
  899. $wp_meta_boxes[$page] = array();
  900. if ( !isset($wp_meta_boxes[$page][$context]) )
  901. $wp_meta_boxes[$page][$context] = array();
  902. foreach ( array('high', 'core', 'default', 'low') as $priority )
  903. $wp_meta_boxes[$page][$context][$priority][$id] = false;
  904. }
  905. /**
  906. * Meta Box Accordion Template Function
  907. *
  908. * Largely made up of abstracted code from {@link do_meta_boxes()}, this
  909. * function serves to build meta boxes as list items for display as
  910. * a collapsible accordion.
  911. *
  912. * @since 3.6.0
  913. *
  914. * @uses global $wp_meta_boxes Used to retrieve registered meta boxes.
  915. *
  916. * @param string|object $screen The screen identifier.
  917. * @param string $context The meta box context.
  918. * @param mixed $object gets passed to the section callback function as first parameter.
  919. * @return int number of meta boxes as accordion sections.
  920. */
  921. function do_accordion_sections( $screen, $context, $object ) {
  922. global $wp_meta_boxes;
  923. wp_enqueue_script( 'accordion' );
  924. if ( empty( $screen ) )
  925. $screen = get_current_screen();
  926. elseif ( is_string( $screen ) )
  927. $screen = convert_to_screen( $screen );
  928. $page = $screen->id;
  929. $hidden = get_hidden_meta_boxes( $screen );
  930. ?>
  931. <div id="side-sortables" class="accordion-container">
  932. <ul class="outer-border">
  933. <?php
  934. $i = 0;
  935. $first_open = false;
  936. do {
  937. if ( ! isset( $wp_meta_boxes ) || ! isset( $wp_meta_boxes[$page] ) || ! isset( $wp_meta_boxes[$page][$context] ) )
  938. break;
  939. foreach ( array( 'high', 'core', 'default', 'low' ) as $priority ) {
  940. if ( isset( $wp_meta_boxes[$page][$context][$priority] ) ) {
  941. foreach ( $wp_meta_boxes[$page][$context][$priority] as $box ) {
  942. if ( false == $box || ! $box['title'] )
  943. continue;
  944. $i++;
  945. $hidden_class = in_array( $box['id'], $hidden ) ? 'hide-if-js' : '';
  946. $open_class = '';
  947. if ( ! $first_open && empty( $hidden_class ) ) {
  948. $first_open = true;
  949. $open_class = 'open';
  950. }
  951. ?>
  952. <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'] ); ?>">
  953. <h3 class="accordion-section-title hndle" tabindex="0" title="<?php echo esc_attr( $box['title'] ); ?>"><?php echo esc_html( $box['title'] ); ?></h3>
  954. <div class="accordion-section-content <?php postbox_classes( $box['id'], $page ); ?>">
  955. <div class="inside">
  956. <?php call_user_func( $box['callback'], $object, $box ); ?>
  957. </div><!-- .inside -->
  958. </div><!-- .accordion-section-content -->
  959. </li><!-- .accordion-section -->
  960. <?php
  961. }
  962. }
  963. }
  964. } while(0);
  965. ?>
  966. </ul><!-- .outer-border -->
  967. </div><!-- .accordion-container -->
  968. <?php
  969. return $i;
  970. }
  971. /**
  972. * Add a new section to a settings page.
  973. *
  974. * Part of the Settings API. Use this to define new settings sections for an admin page.
  975. * Show settings sections in your admin page callback function with do_settings_sections().
  976. * Add settings fields to your section with add_settings_field()
  977. *
  978. * The $callback argument should be the name of a function that echoes out any
  979. * content you want to show at the top of the settings section before the actual
  980. * fields. It can output nothing if you want.
  981. *
  982. * @since 2.7.0
  983. *
  984. * @global $wp_settings_sections Storage array of all settings sections added to admin pages
  985. *
  986. * @param string $id Slug-name to identify the section. Used in the 'id' attribute of tags.
  987. * @param string $title Formatted title of the section. Shown as the heading for the section.
  988. * @param string $callback Function that echos out any content at the top of the section (between heading and fields).
  989. * @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();
  990. */
  991. function add_settings_section($id, $title, $callback, $page) {
  992. global $wp_settings_sections;
  993. if ( 'misc' == $page ) {
  994. _deprecated_argument( __FUNCTION__, '3.0', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'misc' ) );
  995. $page = 'general';
  996. }
  997. if ( 'privacy' == $page ) {
  998. _deprecated_argument( __FUNCTION__, '3.5', sprintf( __( 'The "%s" options group has been removed. Use another settings group.' ), 'privacy' ) );
  999. $page = 'reading';
  1000. }
  1001. $wp_settings_sections[$page][$id] = array('id' => $id, 'title' => $title, 'callback' => $callback);
  1002. }
  1003. /**
  1004. * Add a new field to a section of a settings page
  1005. *
  1006. * Part of the Settings API. Use this to define a settings field that will show
  1007. * as part of a settings section inside a settings page. The fields are shown using
  1008. * do_settings_fields() in do_settings-sections()
  1009. *
  1010. * The $callback argument should be the name of a function that echoes out the
  1011. * html input tags for this setting field. Use get_option() to retrieve existing
  1012. * values to show.
  1013. *
  1014. * @since 2.7.0
  1015. *
  1016. * @global $wp_settings_fields Storage array of settings fields and info about their pages/sections
  1017. *
  1018. * @param string $id Slug-name to identify the field. Used in the 'id' attribute of tags.
  1019. * @param string $title Formatted title of the field. Shown as the label for the field during output.
  1020. * @param string $callback Function that fills the field with the desired form inputs. The function should echo its output.
  1021. * @param string $page The slug-name of the settings page on which to show the section (general, reading, writing, ...).
  1022. * @param string $section The slug-name of the section of the settings page in which to show the box (default, ...).
  1023. * @param array $args Additional arguments
  1024. */
  1025. function add_settings_field($id, $title, $callback, $page, $section = 'default', $args = array()) {
  1026. global $wp_settings_fields;
  1027. if ( 'misc' == $page ) {
  1028. _deprecated_argument( __FUNCTION__, '3.0', __( 'The miscellaneous options group has been removed. Use another settings group.' ) );
  1029. $page = 'general';
  1030. }
  1031. if ( 'privacy' == $page ) {
  1032. _deprecated_argument( __FUNCTION__, '3.5', __( 'The privacy options group has been removed. Use another settings group.' ) );
  1033. $page = 'reading';
  1034. }
  1035. $wp_settings_fields[$page][$section][$id] = array('id' => $id, 'title' => $title, 'callback' => $callback, 'args' => $args);
  1036. }
  1037. /**
  1038. * Prints out all settings sections added to a particular settings page
  1039. *
  1040. * Part of the Settings API. Use this in a settings page callback function
  1041. * to output all the sections and fields that were added to that $page with
  1042. * add_settings_section() and add_settings_field()
  1043. *
  1044. * @global $wp_settings_sections Storage array of all settings sections added to admin pages
  1045. * @global $wp_settings_fields Storage array of settings fields and info about their pages/sections
  1046. * @since 2.7.0
  1047. *
  1048. * @param string $page The slug name of the page whos settings sections you want to output
  1049. */
  1050. function do_settings_sections( $page ) {
  1051. global $wp_settings_sections, $wp_settings_fields;
  1052. if ( ! isset( $wp_settings_sections[$page] ) )
  1053. return;
  1054. foreach ( (array) $wp_settings_sections[$page] as $section ) {
  1055. if ( $section['title'] )
  1056. echo "<h3>{$section['title']}</h3>\n";
  1057. if ( $section['callback'] )
  1058. call_user_func( $section['callback'], $section );
  1059. if ( ! isset( $wp_settings_fields ) || !isset( $wp_settings_fields[$page] ) || !isset( $wp_settings_fields[$page][$section['id']] ) )
  1060. continue;
  1061. echo '<table class="form-table">';
  1062. do_settings_fields( $page, $section['id'] );
  1063. echo '</table>';
  1064. }
  1065. }
  1066. /**
  1067. * Print out the settings fields for a particular settings section
  1068. *
  1069. * Part of the Settings API. Use this in a settings page to output
  1070. * a specific section. Should normally be called by do_settings_sections()
  1071. * rather than directly.
  1072. *
  1073. * @global $wp_settings_fields Storage array of settings fields and their pages/sections
  1074. *
  1075. * @since 2.7.0
  1076. *
  1077. * @param string $page Slug title of the admin page who's settings fields you want to show.
  1078. * @param section $section Slug title of the settings section who's fields you want to show.
  1079. */
  1080. function do_settings_fields($page, $section) {
  1081. global $wp_settings_fields;
  1082. if ( ! isset( $wp_settings_fields[$page][$section] ) )
  1083. return;
  1084. foreach ( (array) $wp_settings_fields[$page][$section] as $field ) {
  1085. echo '<tr>';
  1086. if ( !empty($field['args']['label_for']) )
  1087. echo '<th scope="row"><label for="' . esc_attr( $field['args']['label_for'] ) . '">' . $field['title'] . '</label></th>';
  1088. else
  1089. echo '<th scope="row">' . $field['title'] . '</th>';
  1090. echo '<td>';
  1091. call_user_func($field['callback'], $field['args']);
  1092. echo '</td>';
  1093. echo '</tr>';
  1094. }
  1095. }
  1096. /**
  1097. * Register a settings error to be displayed to the user
  1098. *
  1099. * Part of the Settings API. Use this to show messages to users about settings validation
  1100. * problems, missing settings or anything else.
  1101. *
  1102. * Settings errors should be added inside the $sanitize_callback function defined in
  1103. * register_setting() for a given setting to give feedback about the submission.
  1104. *
  1105. * By default messages will show immediately after the submission that generated the error.
  1106. * Additional calls to settings_errors() can be used to show errors even when the settings
  1107. * page is first accessed.
  1108. *
  1109. * @since 3.0.0
  1110. *
  1111. * @global array $wp_settings_errors Storage array of errors registered during this pageload
  1112. *
  1113. * @param string $setting Slug title of the setting to which this error applies
  1114. * @param string $code Slug-name to identify the error. Used as part of 'id' attribute in HTML output.
  1115. * @param string $message The formatted message text to display to the user (will be shown inside styled <div> and <p>)
  1116. * @param string $type The type of message it is, controls HTML class. Use 'error' or 'updated'.
  1117. */
  1118. function add_settings_error( $setting, $code, $message, $type = 'error' ) {
  1119. global $wp_settings_errors;
  1120. $new_error = array(
  1121. 'setting' => $setting,
  1122. 'code' => $code,
  1123. 'message' => $message,
  1124. 'type' => $type
  1125. );
  1126. $wp_settings_errors[] = $new_error;
  1127. }
  1128. /**
  1129. * Fetch settings errors registered by add_settings_error()
  1130. *
  1131. * Checks the $wp_settings_errors array for any errors declared during the current
  1132. * pageload and returns them.
  1133. *
  1134. * If changes were just submitted ($_GET['settings-updated']) and settings errors were saved
  1135. * to the 'settings_errors' transient then those errors will be returned instead. This
  1136. * is used to pass errors back across pageloads.
  1137. *
  1138. * Use the $sanitize argument to manually re-sanitize the option before returning errors.
  1139. * This is useful if you have errors or notices you want to show even when the user
  1140. * hasn't submitted data (i.e. when they first load an options page, or in admin_notices action hook)
  1141. *
  1142. * @since 3.0.0
  1143. *
  1144. * @global array $wp_settings_errors Storage array of errors registered during this pageload
  1145. *
  1146. * @param string $setting Optional slug title of a specific setting who's errors you want.
  1147. * @param boolean $sanitize Whether to re-sanitize the setting value before returning errors.
  1148. * @return array Array of settings errors
  1149. */
  1150. function get_settings_errors( $setting = '', $sanitize = false ) {
  1151. global $wp_settings_errors;
  1152. // If $sanitize is true, manually re-run the sanitization for this option
  1153. // This allows the $sanitize_callback from register_setting() to run, adding
  1154. // any settings errors you want to show by default.
  1155. if ( $sanitize )
  1156. sanitize_option( $setting, get_option( $setting ) );
  1157. // If settings were passed back from options.php then use them
  1158. if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] && get_transient( 'settings_errors' ) ) {
  1159. $wp_settings_errors = array_merge( (array) $wp_settings_errors, get_transient( 'settings_errors' ) );
  1160. delete_transient( 'settings_errors' );
  1161. }
  1162. // Check global in case errors have been added on this pageload
  1163. if ( ! count( $wp_settings_errors ) )
  1164. return array();
  1165. // Filter the results to those of a specific setting if one was set
  1166. if ( $setting ) {
  1167. $setting_errors = array();
  1168. foreach ( (array) $wp_settings_errors as $key => $details ) {
  1169. if ( $setting == $details['setting'] )
  1170. $setting_errors[] = $wp_settings_errors[$key];
  1171. }
  1172. return $setting_errors;
  1173. }
  1174. return $wp_settings_errors;
  1175. }
  1176. /**
  1177. * Display settings errors registered by add_settings_error()
  1178. *
  1179. * Part of the Settings API. Outputs a <div> for each error retrieved by get_settings_errors().
  1180. *
  1181. * This is called automatically after a settings page based on the Settings API is submitted.
  1182. * Errors should be added during the validation callback function for a setting defined in register_setting()
  1183. *
  1184. * The $sanitize option is passed into get_settings_e…

Large files files are truncated, but you can click here to view the full file