PageRenderTime 67ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/post.php

https://bitbucket.org/mihteh/mihteh.com-wp
PHP | 5350 lines | 2592 code | 730 blank | 2028 comment | 745 complexity | 8f2ae0631d56193fe80f48c728a995e0 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.1, GPL-2.0, GPL-3.0, Apache-2.0

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

  1. <?php
  2. /**
  3. * Post functions and post utility function.
  4. *
  5. * @package WordPress
  6. * @subpackage Post
  7. * @since 1.5.0
  8. */
  9. //
  10. // Post Type Registration
  11. //
  12. /**
  13. * Creates the initial post types when 'init' action is fired.
  14. *
  15. * @since 2.9.0
  16. */
  17. function create_initial_post_types() {
  18. register_post_type( 'post', array(
  19. 'labels' => array(
  20. 'name_admin_bar' => _x( 'Post', 'add new on admin bar' ),
  21. ),
  22. 'public' => true,
  23. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  24. '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
  25. 'capability_type' => 'post',
  26. 'map_meta_cap' => true,
  27. 'hierarchical' => false,
  28. 'rewrite' => false,
  29. 'query_var' => false,
  30. 'delete_with_user' => true,
  31. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
  32. ) );
  33. register_post_type( 'page', array(
  34. 'labels' => array(
  35. 'name_admin_bar' => _x( 'Page', 'add new on admin bar' ),
  36. ),
  37. 'public' => true,
  38. 'publicly_queryable' => false,
  39. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  40. '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
  41. 'capability_type' => 'page',
  42. 'map_meta_cap' => true,
  43. 'hierarchical' => true,
  44. 'rewrite' => false,
  45. 'query_var' => false,
  46. 'delete_with_user' => true,
  47. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
  48. ) );
  49. register_post_type( 'attachment', array(
  50. 'labels' => array(
  51. 'name' => __( 'Media' ),
  52. 'edit_item' => __( 'Edit Media' ),
  53. ),
  54. 'public' => true,
  55. 'show_ui' => false,
  56. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  57. '_edit_link' => 'media.php?attachment_id=%d', /* internal use only. don't use this when registering your own post type. */
  58. 'capability_type' => 'post',
  59. 'map_meta_cap' => true,
  60. 'hierarchical' => false,
  61. 'rewrite' => false,
  62. 'query_var' => false,
  63. 'show_in_nav_menus' => false,
  64. 'delete_with_user' => true,
  65. 'supports' => array( 'comments', 'author' ),
  66. ) );
  67. register_post_type( 'revision', array(
  68. 'labels' => array(
  69. 'name' => __( 'Revisions' ),
  70. 'singular_name' => __( 'Revision' ),
  71. ),
  72. 'public' => false,
  73. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  74. '_edit_link' => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */
  75. 'capability_type' => 'post',
  76. 'map_meta_cap' => true,
  77. 'hierarchical' => false,
  78. 'rewrite' => false,
  79. 'query_var' => false,
  80. 'can_export' => false,
  81. 'delete_with_user' => true,
  82. 'supports' => array( 'author' ),
  83. ) );
  84. register_post_type( 'nav_menu_item', array(
  85. 'labels' => array(
  86. 'name' => __( 'Navigation Menu Items' ),
  87. 'singular_name' => __( 'Navigation Menu Item' ),
  88. ),
  89. 'public' => false,
  90. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  91. 'hierarchical' => false,
  92. 'rewrite' => false,
  93. 'delete_with_user' => false,
  94. 'query_var' => false,
  95. ) );
  96. register_post_status( 'publish', array(
  97. 'label' => _x( 'Published', 'post' ),
  98. 'public' => true,
  99. '_builtin' => true, /* internal use only. */
  100. 'label_count' => _n_noop( 'Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>' ),
  101. ) );
  102. register_post_status( 'future', array(
  103. 'label' => _x( 'Scheduled', 'post' ),
  104. 'protected' => true,
  105. '_builtin' => true, /* internal use only. */
  106. 'label_count' => _n_noop('Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>' ),
  107. ) );
  108. register_post_status( 'draft', array(
  109. 'label' => _x( 'Draft', 'post' ),
  110. 'protected' => true,
  111. '_builtin' => true, /* internal use only. */
  112. 'label_count' => _n_noop( 'Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>' ),
  113. ) );
  114. register_post_status( 'pending', array(
  115. 'label' => _x( 'Pending', 'post' ),
  116. 'protected' => true,
  117. '_builtin' => true, /* internal use only. */
  118. 'label_count' => _n_noop( 'Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>' ),
  119. ) );
  120. register_post_status( 'private', array(
  121. 'label' => _x( 'Private', 'post' ),
  122. 'private' => true,
  123. '_builtin' => true, /* internal use only. */
  124. 'label_count' => _n_noop( 'Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>' ),
  125. ) );
  126. register_post_status( 'trash', array(
  127. 'label' => _x( 'Trash', 'post' ),
  128. 'internal' => true,
  129. '_builtin' => true, /* internal use only. */
  130. 'label_count' => _n_noop( 'Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>' ),
  131. 'show_in_admin_status_list' => true,
  132. ) );
  133. register_post_status( 'auto-draft', array(
  134. 'label' => 'auto-draft',
  135. 'internal' => true,
  136. '_builtin' => true, /* internal use only. */
  137. ) );
  138. register_post_status( 'inherit', array(
  139. 'label' => 'inherit',
  140. 'internal' => true,
  141. '_builtin' => true, /* internal use only. */
  142. 'exclude_from_search' => false,
  143. ) );
  144. }
  145. add_action( 'init', 'create_initial_post_types', 0 ); // highest priority
  146. /**
  147. * Retrieve attached file path based on attachment ID.
  148. *
  149. * You can optionally send it through the 'get_attached_file' filter, but by
  150. * default it will just return the file path unfiltered.
  151. *
  152. * The function works by getting the single post meta name, named
  153. * '_wp_attached_file' and returning it. This is a convenience function to
  154. * prevent looking up the meta name and provide a mechanism for sending the
  155. * attached filename through a filter.
  156. *
  157. * @since 2.0.0
  158. * @uses apply_filters() Calls 'get_attached_file' on file path and attachment ID.
  159. *
  160. * @param int $attachment_id Attachment ID.
  161. * @param bool $unfiltered Whether to apply filters.
  162. * @return string|bool The file path to the attached file, or false if the attachment does not exist.
  163. */
  164. function get_attached_file( $attachment_id, $unfiltered = false ) {
  165. $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
  166. // If the file is relative, prepend upload dir
  167. if ( $file && 0 !== strpos($file, '/') && !preg_match('|^.:\\\|', $file) && ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) )
  168. $file = $uploads['basedir'] . "/$file";
  169. if ( $unfiltered )
  170. return $file;
  171. return apply_filters( 'get_attached_file', $file, $attachment_id );
  172. }
  173. /**
  174. * Update attachment file path based on attachment ID.
  175. *
  176. * Used to update the file path of the attachment, which uses post meta name
  177. * '_wp_attached_file' to store the path of the attachment.
  178. *
  179. * @since 2.1.0
  180. * @uses apply_filters() Calls 'update_attached_file' on file path and attachment ID.
  181. *
  182. * @param int $attachment_id Attachment ID
  183. * @param string $file File path for the attachment
  184. * @return bool False on failure, true on success.
  185. */
  186. function update_attached_file( $attachment_id, $file ) {
  187. if ( !get_post( $attachment_id ) )
  188. return false;
  189. $file = apply_filters( 'update_attached_file', $file, $attachment_id );
  190. $file = _wp_relative_upload_path($file);
  191. return update_post_meta( $attachment_id, '_wp_attached_file', $file );
  192. }
  193. /**
  194. * Return relative path to an uploaded file.
  195. *
  196. * The path is relative to the current upload dir.
  197. *
  198. * @since 2.9.0
  199. * @uses apply_filters() Calls '_wp_relative_upload_path' on file path.
  200. *
  201. * @param string $path Full path to the file
  202. * @return string relative path on success, unchanged path on failure.
  203. */
  204. function _wp_relative_upload_path( $path ) {
  205. $new_path = $path;
  206. if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) {
  207. if ( 0 === strpos($new_path, $uploads['basedir']) ) {
  208. $new_path = str_replace($uploads['basedir'], '', $new_path);
  209. $new_path = ltrim($new_path, '/');
  210. }
  211. }
  212. return apply_filters( '_wp_relative_upload_path', $new_path, $path );
  213. }
  214. /**
  215. * Retrieve all children of the post parent ID.
  216. *
  217. * Normally, without any enhancements, the children would apply to pages. In the
  218. * context of the inner workings of WordPress, pages, posts, and attachments
  219. * share the same table, so therefore the functionality could apply to any one
  220. * of them. It is then noted that while this function does not work on posts, it
  221. * does not mean that it won't work on posts. It is recommended that you know
  222. * what context you wish to retrieve the children of.
  223. *
  224. * Attachments may also be made the child of a post, so if that is an accurate
  225. * statement (which needs to be verified), it would then be possible to get
  226. * all of the attachments for a post. Attachments have since changed since
  227. * version 2.5, so this is most likely unaccurate, but serves generally as an
  228. * example of what is possible.
  229. *
  230. * The arguments listed as defaults are for this function and also of the
  231. * {@link get_posts()} function. The arguments are combined with the
  232. * get_children defaults and are then passed to the {@link get_posts()}
  233. * function, which accepts additional arguments. You can replace the defaults in
  234. * this function, listed below and the additional arguments listed in the
  235. * {@link get_posts()} function.
  236. *
  237. * The 'post_parent' is the most important argument and important attention
  238. * needs to be paid to the $args parameter. If you pass either an object or an
  239. * integer (number), then just the 'post_parent' is grabbed and everything else
  240. * is lost. If you don't specify any arguments, then it is assumed that you are
  241. * in The Loop and the post parent will be grabbed for from the current post.
  242. *
  243. * The 'post_parent' argument is the ID to get the children. The 'numberposts'
  244. * is the amount of posts to retrieve that has a default of '-1', which is
  245. * used to get all of the posts. Giving a number higher than 0 will only
  246. * retrieve that amount of posts.
  247. *
  248. * The 'post_type' and 'post_status' arguments can be used to choose what
  249. * criteria of posts to retrieve. The 'post_type' can be anything, but WordPress
  250. * post types are 'post', 'pages', and 'attachments'. The 'post_status'
  251. * argument will accept any post status within the write administration panels.
  252. *
  253. * @see get_posts() Has additional arguments that can be replaced.
  254. * @internal Claims made in the long description might be inaccurate.
  255. *
  256. * @since 2.0.0
  257. *
  258. * @param mixed $args Optional. User defined arguments for replacing the defaults.
  259. * @param string $output Optional. Constant for return type, either OBJECT (default), ARRAY_A, ARRAY_N.
  260. * @return array|bool False on failure and the type will be determined by $output parameter.
  261. */
  262. function get_children($args = '', $output = OBJECT) {
  263. $kids = array();
  264. if ( empty( $args ) ) {
  265. if ( isset( $GLOBALS['post'] ) ) {
  266. $args = array('post_parent' => (int) $GLOBALS['post']->post_parent );
  267. } else {
  268. return $kids;
  269. }
  270. } elseif ( is_object( $args ) ) {
  271. $args = array('post_parent' => (int) $args->post_parent );
  272. } elseif ( is_numeric( $args ) ) {
  273. $args = array('post_parent' => (int) $args);
  274. }
  275. $defaults = array(
  276. 'numberposts' => -1, 'post_type' => 'any',
  277. 'post_status' => 'any', 'post_parent' => 0,
  278. );
  279. $r = wp_parse_args( $args, $defaults );
  280. $children = get_posts( $r );
  281. if ( !$children )
  282. return $kids;
  283. update_post_cache($children);
  284. foreach ( $children as $key => $child )
  285. $kids[$child->ID] = $children[$key];
  286. if ( $output == OBJECT ) {
  287. return $kids;
  288. } elseif ( $output == ARRAY_A ) {
  289. foreach ( (array) $kids as $kid )
  290. $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);
  291. return $weeuns;
  292. } elseif ( $output == ARRAY_N ) {
  293. foreach ( (array) $kids as $kid )
  294. $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));
  295. return $babes;
  296. } else {
  297. return $kids;
  298. }
  299. }
  300. /**
  301. * Get extended entry info (<!--more-->).
  302. *
  303. * There should not be any space after the second dash and before the word
  304. * 'more'. There can be text or space(s) after the word 'more', but won't be
  305. * referenced.
  306. *
  307. * The returned array has 'main', 'extended', and 'more_text' keys. Main has the text before
  308. * the <code><!--more--></code>. The 'extended' key has the content after the
  309. * <code><!--more--></code> comment. The 'more_text' key has the custom "Read More" text.
  310. *
  311. * @since 1.0.0
  312. *
  313. * @param string $post Post content.
  314. * @return array Post before ('main'), after ('extended'), and custom readmore ('more_text').
  315. */
  316. function get_extended($post) {
  317. //Match the new style more links
  318. if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) {
  319. list($main, $extended) = explode($matches[0], $post, 2);
  320. $more_text = $matches[1];
  321. } else {
  322. $main = $post;
  323. $extended = '';
  324. $more_text = '';
  325. }
  326. // Strip leading and trailing whitespace
  327. $main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main);
  328. $extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended);
  329. $more_text = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $more_text);
  330. return array( 'main' => $main, 'extended' => $extended, 'more_text' => $more_text );
  331. }
  332. /**
  333. * Retrieves post data given a post ID or post object.
  334. *
  335. * See {@link sanitize_post()} for optional $filter values. Also, the parameter
  336. * $post, must be given as a variable, since it is passed by reference.
  337. *
  338. * @since 1.5.1
  339. * @uses $wpdb
  340. * @link http://codex.wordpress.org/Function_Reference/get_post
  341. *
  342. * @param int|object $post Post ID or post object.
  343. * @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
  344. * @param string $filter Optional, default is raw.
  345. * @return mixed Post data
  346. */
  347. function &get_post(&$post, $output = OBJECT, $filter = 'raw') {
  348. global $wpdb;
  349. $null = null;
  350. if ( empty($post) ) {
  351. if ( isset($GLOBALS['post']) )
  352. $_post = & $GLOBALS['post'];
  353. else
  354. return $null;
  355. } elseif ( is_object($post) && empty($post->filter) ) {
  356. _get_post_ancestors($post);
  357. $_post = sanitize_post($post, 'raw');
  358. wp_cache_add($post->ID, $_post, 'posts');
  359. } elseif ( is_object($post) && 'raw' == $post->filter ) {
  360. $_post = $post;
  361. } else {
  362. if ( is_object($post) )
  363. $post_id = $post->ID;
  364. else
  365. $post_id = $post;
  366. $post_id = (int) $post_id;
  367. if ( ! $_post = wp_cache_get($post_id, 'posts') ) {
  368. $_post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id));
  369. if ( ! $_post )
  370. return $null;
  371. _get_post_ancestors($_post);
  372. $_post = sanitize_post($_post, 'raw');
  373. wp_cache_add($_post->ID, $_post, 'posts');
  374. }
  375. }
  376. if ($filter != 'raw')
  377. $_post = sanitize_post($_post, $filter);
  378. if ( $output == OBJECT ) {
  379. return $_post;
  380. } elseif ( $output == ARRAY_A ) {
  381. $__post = get_object_vars($_post);
  382. return $__post;
  383. } elseif ( $output == ARRAY_N ) {
  384. $__post = array_values(get_object_vars($_post));
  385. return $__post;
  386. } else {
  387. return $_post;
  388. }
  389. }
  390. /**
  391. * Retrieve ancestors of a post.
  392. *
  393. * @since 2.5.0
  394. *
  395. * @param int|object $post Post ID or post object
  396. * @return array Ancestor IDs or empty array if none are found.
  397. */
  398. function get_post_ancestors($post) {
  399. $post = get_post($post);
  400. if ( ! isset( $post->ancestors ) )
  401. _get_post_ancestors( $post );
  402. if ( ! empty( $post->ancestors ) )
  403. return $post->ancestors;
  404. return array();
  405. }
  406. /**
  407. * Retrieve data from a post field based on Post ID.
  408. *
  409. * Examples of the post field will be, 'post_type', 'post_status', 'post_content',
  410. * etc and based off of the post object property or key names.
  411. *
  412. * The context values are based off of the taxonomy filter functions and
  413. * supported values are found within those functions.
  414. *
  415. * @since 2.3.0
  416. * @uses sanitize_post_field() See for possible $context values.
  417. *
  418. * @param string $field Post field name
  419. * @param id $post Post ID
  420. * @param string $context Optional. How to filter the field. Default is display.
  421. * @return WP_Error|string Value in post field or WP_Error on failure
  422. */
  423. function get_post_field( $field, $post, $context = 'display' ) {
  424. $post = (int) $post;
  425. $post = get_post( $post );
  426. if ( is_wp_error($post) )
  427. return $post;
  428. if ( !is_object($post) )
  429. return '';
  430. if ( !isset($post->$field) )
  431. return '';
  432. return sanitize_post_field($field, $post->$field, $post->ID, $context);
  433. }
  434. /**
  435. * Retrieve the mime type of an attachment based on the ID.
  436. *
  437. * This function can be used with any post type, but it makes more sense with
  438. * attachments.
  439. *
  440. * @since 2.0.0
  441. *
  442. * @param int $ID Optional. Post ID.
  443. * @return bool|string False on failure or returns the mime type
  444. */
  445. function get_post_mime_type($ID = '') {
  446. $post = & get_post($ID);
  447. if ( is_object($post) )
  448. return $post->post_mime_type;
  449. return false;
  450. }
  451. /**
  452. * Retrieve the format slug for a post
  453. *
  454. * @since 3.1.0
  455. *
  456. * @param int|object $post A post
  457. *
  458. * @return mixed The format if successful. False if no format is set. WP_Error if errors.
  459. */
  460. function get_post_format( $post = null ) {
  461. $post = get_post($post);
  462. if ( ! post_type_supports( $post->post_type, 'post-formats' ) )
  463. return false;
  464. $_format = get_the_terms( $post->ID, 'post_format' );
  465. if ( empty( $_format ) )
  466. return false;
  467. $format = array_shift( $_format );
  468. return ( str_replace('post-format-', '', $format->slug ) );
  469. }
  470. /**
  471. * Check if a post has a particular format
  472. *
  473. * @since 3.1.0
  474. * @uses has_term()
  475. *
  476. * @param string $format The format to check for
  477. * @param object|id $post The post to check. If not supplied, defaults to the current post if used in the loop.
  478. * @return bool True if the post has the format, false otherwise.
  479. */
  480. function has_post_format( $format, $post = null ) {
  481. return has_term('post-format-' . sanitize_key($format), 'post_format', $post);
  482. }
  483. /**
  484. * Assign a format to a post
  485. *
  486. * @since 3.1.0
  487. *
  488. * @param int|object $post The post for which to assign a format
  489. * @param string $format A format to assign. Use an empty string or array to remove all formats from the post.
  490. * @return mixed WP_Error on error. Array of affected term IDs on success.
  491. */
  492. function set_post_format( $post, $format ) {
  493. $post = get_post($post);
  494. if ( empty($post) )
  495. return new WP_Error('invalid_post', __('Invalid post'));
  496. if ( !empty($format) ) {
  497. $format = sanitize_key($format);
  498. if ( 'standard' == $format || !in_array( $format, array_keys( get_post_format_slugs() ) ) )
  499. $format = '';
  500. else
  501. $format = 'post-format-' . $format;
  502. }
  503. return wp_set_post_terms($post->ID, $format, 'post_format');
  504. }
  505. /**
  506. * Retrieve the post status based on the Post ID.
  507. *
  508. * If the post ID is of an attachment, then the parent post status will be given
  509. * instead.
  510. *
  511. * @since 2.0.0
  512. *
  513. * @param int $ID Post ID
  514. * @return string|bool Post status or false on failure.
  515. */
  516. function get_post_status($ID = '') {
  517. $post = get_post($ID);
  518. if ( !is_object($post) )
  519. return false;
  520. if ( 'attachment' == $post->post_type ) {
  521. if ( 'private' == $post->post_status )
  522. return 'private';
  523. // Unattached attachments are assumed to be published
  524. if ( ( 'inherit' == $post->post_status ) && ( 0 == $post->post_parent) )
  525. return 'publish';
  526. // Inherit status from the parent
  527. if ( $post->post_parent && ( $post->ID != $post->post_parent ) )
  528. return get_post_status($post->post_parent);
  529. }
  530. return $post->post_status;
  531. }
  532. /**
  533. * Retrieve all of the WordPress supported post statuses.
  534. *
  535. * Posts have a limited set of valid status values, this provides the
  536. * post_status values and descriptions.
  537. *
  538. * @since 2.5.0
  539. *
  540. * @return array List of post statuses.
  541. */
  542. function get_post_statuses( ) {
  543. $status = array(
  544. 'draft' => __('Draft'),
  545. 'pending' => __('Pending Review'),
  546. 'private' => __('Private'),
  547. 'publish' => __('Published')
  548. );
  549. return $status;
  550. }
  551. /**
  552. * Retrieve all of the WordPress support page statuses.
  553. *
  554. * Pages have a limited set of valid status values, this provides the
  555. * post_status values and descriptions.
  556. *
  557. * @since 2.5.0
  558. *
  559. * @return array List of page statuses.
  560. */
  561. function get_page_statuses( ) {
  562. $status = array(
  563. 'draft' => __('Draft'),
  564. 'private' => __('Private'),
  565. 'publish' => __('Published')
  566. );
  567. return $status;
  568. }
  569. /**
  570. * Register a post status. Do not use before init.
  571. *
  572. * A simple function for creating or modifying a post status based on the
  573. * parameters given. The function will accept an array (second optional
  574. * parameter), along with a string for the post status name.
  575. *
  576. *
  577. * Optional $args contents:
  578. *
  579. * label - A descriptive name for the post status marked for translation. Defaults to $post_status.
  580. * public - Whether posts of this status should be shown in the front end of the site. Defaults to true.
  581. * exclude_from_search - Whether to exclude posts with this post status from search results. Defaults to false.
  582. * show_in_admin_all_list - Whether to include posts in the edit listing for their post type
  583. * show_in_admin_status_list - Show in the list of statuses with post counts at the top of the edit
  584. * listings, e.g. All (12) | Published (9) | My Custom Status (2) ...
  585. *
  586. * Arguments prefixed with an _underscore shouldn't be used by plugins and themes.
  587. *
  588. * @package WordPress
  589. * @subpackage Post
  590. * @since 3.0.0
  591. * @uses $wp_post_statuses Inserts new post status object into the list
  592. *
  593. * @param string $post_status Name of the post status.
  594. * @param array|string $args See above description.
  595. */
  596. function register_post_status($post_status, $args = array()) {
  597. global $wp_post_statuses;
  598. if (!is_array($wp_post_statuses))
  599. $wp_post_statuses = array();
  600. // Args prefixed with an underscore are reserved for internal use.
  601. $defaults = array('label' => false, 'label_count' => false, 'exclude_from_search' => null, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'public' => null, 'internal' => null, 'protected' => null, 'private' => null, 'show_in_admin_all' => null, 'publicly_queryable' => null, 'show_in_admin_status_list' => null, 'show_in_admin_all_list' => null, 'single_view_cap' => null);
  602. $args = wp_parse_args($args, $defaults);
  603. $args = (object) $args;
  604. $post_status = sanitize_key($post_status);
  605. $args->name = $post_status;
  606. if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private )
  607. $args->internal = true;
  608. if ( null === $args->public )
  609. $args->public = false;
  610. if ( null === $args->private )
  611. $args->private = false;
  612. if ( null === $args->protected )
  613. $args->protected = false;
  614. if ( null === $args->internal )
  615. $args->internal = false;
  616. if ( null === $args->publicly_queryable )
  617. $args->publicly_queryable = $args->public;
  618. if ( null === $args->exclude_from_search )
  619. $args->exclude_from_search = $args->internal;
  620. if ( null === $args->show_in_admin_all_list )
  621. $args->show_in_admin_all_list = !$args->internal;
  622. if ( null === $args->show_in_admin_status_list )
  623. $args->show_in_admin_status_list = !$args->internal;
  624. if ( null === $args->single_view_cap )
  625. $args->single_view_cap = $args->public ? '' : 'edit';
  626. if ( false === $args->label )
  627. $args->label = $post_status;
  628. if ( false === $args->label_count )
  629. $args->label_count = array( $args->label, $args->label );
  630. $wp_post_statuses[$post_status] = $args;
  631. return $args;
  632. }
  633. /**
  634. * Retrieve a post status object by name
  635. *
  636. * @package WordPress
  637. * @subpackage Post
  638. * @since 3.0.0
  639. * @uses $wp_post_statuses
  640. * @see register_post_status
  641. * @see get_post_statuses
  642. *
  643. * @param string $post_status The name of a registered post status
  644. * @return object A post status object
  645. */
  646. function get_post_status_object( $post_status ) {
  647. global $wp_post_statuses;
  648. if ( empty($wp_post_statuses[$post_status]) )
  649. return null;
  650. return $wp_post_statuses[$post_status];
  651. }
  652. /**
  653. * Get a list of all registered post status objects.
  654. *
  655. * @package WordPress
  656. * @subpackage Post
  657. * @since 3.0.0
  658. * @uses $wp_post_statuses
  659. * @see register_post_status
  660. * @see get_post_status_object
  661. *
  662. * @param array|string $args An array of key => value arguments to match against the post status objects.
  663. * @param string $output The type of output to return, either post status 'names' or 'objects'. 'names' is the default.
  664. * @param string $operator The logical operation to perform. 'or' means only one element
  665. * from the array needs to match; 'and' means all elements must match. The default is 'and'.
  666. * @return array A list of post status names or objects
  667. */
  668. function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) {
  669. global $wp_post_statuses;
  670. $field = ('names' == $output) ? 'name' : false;
  671. return wp_filter_object_list($wp_post_statuses, $args, $operator, $field);
  672. }
  673. /**
  674. * Whether the post type is hierarchical.
  675. *
  676. * A false return value might also mean that the post type does not exist.
  677. *
  678. * @since 3.0.0
  679. * @see get_post_type_object
  680. *
  681. * @param string $post_type Post type name
  682. * @return bool Whether post type is hierarchical.
  683. */
  684. function is_post_type_hierarchical( $post_type ) {
  685. if ( ! post_type_exists( $post_type ) )
  686. return false;
  687. $post_type = get_post_type_object( $post_type );
  688. return $post_type->hierarchical;
  689. }
  690. /**
  691. * Checks if a post type is registered.
  692. *
  693. * @since 3.0.0
  694. * @uses get_post_type_object()
  695. *
  696. * @param string $post_type Post type name
  697. * @return bool Whether post type is registered.
  698. */
  699. function post_type_exists( $post_type ) {
  700. return (bool) get_post_type_object( $post_type );
  701. }
  702. /**
  703. * Retrieve the post type of the current post or of a given post.
  704. *
  705. * @since 2.1.0
  706. *
  707. * @uses $post The Loop current post global
  708. *
  709. * @param mixed $the_post Optional. Post object or post ID.
  710. * @return bool|string post type or false on failure.
  711. */
  712. function get_post_type( $the_post = false ) {
  713. global $post;
  714. if ( false === $the_post )
  715. $the_post = $post;
  716. elseif ( is_numeric($the_post) )
  717. $the_post = get_post($the_post);
  718. if ( is_object($the_post) )
  719. return $the_post->post_type;
  720. return false;
  721. }
  722. /**
  723. * Retrieve a post type object by name
  724. *
  725. * @package WordPress
  726. * @subpackage Post
  727. * @since 3.0.0
  728. * @uses $wp_post_types
  729. * @see register_post_type
  730. * @see get_post_types
  731. *
  732. * @param string $post_type The name of a registered post type
  733. * @return object A post type object
  734. */
  735. function get_post_type_object( $post_type ) {
  736. global $wp_post_types;
  737. if ( empty($wp_post_types[$post_type]) )
  738. return null;
  739. return $wp_post_types[$post_type];
  740. }
  741. /**
  742. * Get a list of all registered post type objects.
  743. *
  744. * @package WordPress
  745. * @subpackage Post
  746. * @since 2.9.0
  747. * @uses $wp_post_types
  748. * @see register_post_type
  749. *
  750. * @param array|string $args An array of key => value arguments to match against the post type objects.
  751. * @param string $output The type of output to return, either post type 'names' or 'objects'. 'names' is the default.
  752. * @param string $operator The logical operation to perform. 'or' means only one element
  753. * from the array needs to match; 'and' means all elements must match. The default is 'and'.
  754. * @return array A list of post type names or objects
  755. */
  756. function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) {
  757. global $wp_post_types;
  758. $field = ('names' == $output) ? 'name' : false;
  759. return wp_filter_object_list($wp_post_types, $args, $operator, $field);
  760. }
  761. /**
  762. * Register a post type. Do not use before init.
  763. *
  764. * A function for creating or modifying a post type based on the
  765. * parameters given. The function will accept an array (second optional
  766. * parameter), along with a string for the post type name.
  767. *
  768. * Optional $args contents:
  769. *
  770. * - label - Name of the post type shown in the menu. Usually plural. If not set, labels['name'] will be used.
  771. * - labels - An array of labels for this post type.
  772. * * If not set, post labels are inherited for non-hierarchical types and page labels for hierarchical ones.
  773. * * You can see accepted values in {@link get_post_type_labels()}.
  774. * - description - A short descriptive summary of what the post type is. Defaults to blank.
  775. * - public - Whether a post type is intended for use publicly either via the admin interface or by front-end users.
  776. * * Defaults to false.
  777. * * While the default settings of exclude_from_search, publicly_queryable, show_ui, and show_in_nav_menus are
  778. * inherited from public, each does not rely on this relationship and controls a very specific intention.
  779. * - exclude_from_search - Whether to exclude posts with this post type from front end search results.
  780. * * If not set, the the opposite of public's current value is used.
  781. * - publicly_queryable - Whether queries can be performed on the front end for the post type as part of parse_request().
  782. * * ?post_type={post_type_key}
  783. * * ?{post_type_key}={single_post_slug}
  784. * * ?{post_type_query_var}={single_post_slug}
  785. * * If not set, the default is inherited from public.
  786. * - show_ui - Whether to generate a default UI for managing this post type in the admin.
  787. * * If not set, the default is inherited from public.
  788. * - show_in_nav_menus - Makes this post type available for selection in navigation menus.
  789. * * If not set, the default is inherited from public.
  790. * - show_in_menu - Where to show the post type in the admin menu.
  791. * * If true, the post type is shown in its own top level menu.
  792. * * If false, no menu is shown
  793. * * If a string of an existing top level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post type will
  794. * be placed as a sub menu of that.
  795. * * show_ui must be true.
  796. * * If not set, the default is inherited from show_ui
  797. * - show_in_admin_bar - Makes this post type available via the admin bar.
  798. * * If not set, the default is inherited from show_in_menu
  799. * - menu_position - The position in the menu order the post type should appear.
  800. * * show_in_menu must be true
  801. * * Defaults to null, which places it at the bottom of its area.
  802. * - menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon.
  803. * - capability_type - The string to use to build the read, edit, and delete capabilities. Defaults to 'post'.
  804. * * May be passed as an array to allow for alternative plurals when using this argument as a base to construct the
  805. * capabilities, e.g. array('story', 'stories').
  806. * - capabilities - Array of capabilities for this post type.
  807. * * By default the capability_type is used as a base to construct capabilities.
  808. * * You can see accepted values in {@link get_post_type_capabilities()}.
  809. * - map_meta_cap - Whether to use the internal default meta capability handling. Defaults to false.
  810. * - hierarchical - Whether the post type is hierarchical (e.g. page). Defaults to false.
  811. * - supports - An alias for calling add_post_type_support() directly. Defaults to title and editor.
  812. * * See {@link add_post_type_support()} for documentation.
  813. * - register_meta_box_cb - Provide a callback function that will be called when setting up the
  814. * meta boxes for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback.
  815. * - taxonomies - An array of taxonomy identifiers that will be registered for the post type.
  816. * * Default is no taxonomies.
  817. * * Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type().
  818. * - has_archive - True to enable post type archives. Default is false.
  819. * * Will generate the proper rewrite rules if rewrite is enabled.
  820. * - rewrite - Triggers the handling of rewrites for this post type. Defaults to true, using $post_type as slug.
  821. * * To prevent rewrite, set to false.
  822. * * To specify rewrite rules, an array can be passed with any of these keys
  823. * * 'slug' => string Customize the permastruct slug. Defaults to $post_type key
  824. * * 'with_front' => bool Should the permastruct be prepended with WP_Rewrite::$front. Defaults to true.
  825. * * 'feeds' => bool Should a feed permastruct be built for this post type. Inherits default from has_archive.
  826. * * 'pages' => bool Should the permastruct provide for pagination. Defaults to true.
  827. * * 'ep_mask' => const Assign an endpoint mask.
  828. * * If not specified and permalink_epmask is set, inherits from permalink_epmask.
  829. * * If not specified and permalink_epmask is not set, defaults to EP_PERMALINK
  830. * - query_var - Sets the query_var key for this post type. Defaults to $post_type key
  831. * * If false, a post type cannot be loaded at ?{query_var}={post_slug}
  832. * * If specified as a string, the query ?{query_var_string}={post_slug} will be valid.
  833. * - can_export - Allows this post type to be exported. Defaults to true.
  834. * - delete_with_user - Whether to delete posts of this type when deleting a user.
  835. * * If true, posts of this type belonging to the user will be moved to trash when then user is deleted.
  836. * * If false, posts of this type belonging to the user will *not* be trashed or deleted.
  837. * * If not set (the default), posts are trashed if post_type_supports('author'). Otherwise posts are not trashed or deleted.
  838. * - _builtin - true if this post type is a native or "built-in" post_type. THIS IS FOR INTERNAL USE ONLY!
  839. * - _edit_link - URL segement to use for edit link of this post type. THIS IS FOR INTERNAL USE ONLY!
  840. *
  841. * @since 2.9.0
  842. * @uses $wp_post_types Inserts new post type object into the list
  843. *
  844. * @param string $post_type Post type key, must not exceed 20 characters
  845. * @param array|string $args See optional args description above.
  846. * @return object|WP_Error the registered post type object, or an error object
  847. */
  848. function register_post_type( $post_type, $args = array() ) {
  849. global $wp_post_types, $wp_rewrite, $wp;
  850. if ( !is_array($wp_post_types) )
  851. $wp_post_types = array();
  852. // Args prefixed with an underscore are reserved for internal use.
  853. $defaults = array(
  854. 'labels' => array(), 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => null,
  855. 'capability_type' => 'post', 'capabilities' => array(), 'map_meta_cap' => null,
  856. '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'hierarchical' => false,
  857. 'public' => false, 'rewrite' => true, 'has_archive' => false, 'query_var' => true,
  858. 'supports' => array(), 'register_meta_box_cb' => null,
  859. 'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null,
  860. 'can_export' => true,
  861. 'show_in_nav_menus' => null, 'show_in_menu' => null, 'show_in_admin_bar' => null,
  862. 'delete_with_user' => null,
  863. );
  864. $args = wp_parse_args($args, $defaults);
  865. $args = (object) $args;
  866. $post_type = sanitize_key($post_type);
  867. $args->name = $post_type;
  868. if ( strlen( $post_type ) > 20 )
  869. return new WP_Error( 'post_type_too_long', __( 'Post types cannot exceed 20 characters in length' ) );
  870. // If not set, default to the setting for public.
  871. if ( null === $args->publicly_queryable )
  872. $args->publicly_queryable = $args->public;
  873. // If not set, default to the setting for public.
  874. if ( null === $args->show_ui )
  875. $args->show_ui = $args->public;
  876. // If not set, default to the setting for show_ui.
  877. if ( null === $args->show_in_menu || ! $args->show_ui )
  878. $args->show_in_menu = $args->show_ui;
  879. // If not set, default to the whether the full UI is shown.
  880. if ( null === $args->show_in_admin_bar )
  881. $args->show_in_admin_bar = true === $args->show_in_menu;
  882. // Whether to show this type in nav-menus.php. Defaults to the setting for public.
  883. if ( null === $args->show_in_nav_menus )
  884. $args->show_in_nav_menus = $args->public;
  885. // If not set, default to true if not public, false if public.
  886. if ( null === $args->exclude_from_search )
  887. $args->exclude_from_search = !$args->public;
  888. // Back compat with quirky handling in version 3.0. #14122
  889. if ( empty( $args->capabilities ) && null === $args->map_meta_cap && in_array( $args->capability_type, array( 'post', 'page' ) ) )
  890. $args->map_meta_cap = true;
  891. if ( null === $args->map_meta_cap )
  892. $args->map_meta_cap = false;
  893. $args->cap = get_post_type_capabilities( $args );
  894. unset($args->capabilities);
  895. if ( is_array( $args->capability_type ) )
  896. $args->capability_type = $args->capability_type[0];
  897. if ( ! empty($args->supports) ) {
  898. add_post_type_support($post_type, $args->supports);
  899. unset($args->supports);
  900. } else {
  901. // Add default features
  902. add_post_type_support($post_type, array('title', 'editor'));
  903. }
  904. if ( false !== $args->query_var && !empty($wp) ) {
  905. if ( true === $args->query_var )
  906. $args->query_var = $post_type;
  907. $args->query_var = sanitize_title_with_dashes($args->query_var);
  908. $wp->add_query_var($args->query_var);
  909. }
  910. if ( false !== $args->rewrite && ( is_admin() || '' != get_option('permalink_structure') ) ) {
  911. if ( ! is_array( $args->rewrite ) )
  912. $args->rewrite = array();
  913. if ( empty( $args->rewrite['slug'] ) )
  914. $args->rewrite['slug'] = $post_type;
  915. if ( ! isset( $args->rewrite['with_front'] ) )
  916. $args->rewrite['with_front'] = true;
  917. if ( ! isset( $args->rewrite['pages'] ) )
  918. $args->rewrite['pages'] = true;
  919. if ( ! isset( $args->rewrite['feeds'] ) || ! $args->has_archive )
  920. $args->rewrite['feeds'] = (bool) $args->has_archive;
  921. if ( ! isset( $args->rewrite['ep_mask'] ) ) {
  922. if ( isset( $args->permalink_epmask ) )
  923. $args->rewrite['ep_mask'] = $args->permalink_epmask;
  924. else
  925. $args->rewrite['ep_mask'] = EP_PERMALINK;
  926. }
  927. if ( $args->hierarchical )
  928. add_rewrite_tag("%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
  929. else
  930. add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
  931. if ( $args->has_archive ) {
  932. $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive;
  933. if ( $args->rewrite['with_front'] )
  934. $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
  935. else
  936. $archive_slug = $wp_rewrite->root . $archive_slug;
  937. add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", 'top' );
  938. if ( $args->rewrite['feeds'] && $wp_rewrite->feeds ) {
  939. $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
  940. add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
  941. add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
  942. }
  943. if ( $args->rewrite['pages'] )
  944. add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' );
  945. }
  946. add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite );
  947. }
  948. if ( $args->register_meta_box_cb )
  949. add_action('add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1);
  950. $args->labels = get_post_type_labels( $args );
  951. $args->label = $args->labels->name;
  952. $wp_post_types[$post_type] = $args;
  953. add_action( 'future_' . $post_type, '_future_post_hook', 5, 2 );
  954. foreach ( $args->taxonomies as $taxonomy ) {
  955. register_taxonomy_for_object_type( $taxonomy, $post_type );
  956. }
  957. do_action( 'registered_post_type', $post_type, $args );
  958. return $args;
  959. }
  960. /**
  961. * Builds an object with all post type capabilities out of a post type object
  962. *
  963. * Post type capabilities use the 'capability_type' argument as a base, if the
  964. * capability is not set in the 'capabilities' argument array or if the
  965. * 'capabilities' argument is not supplied.
  966. *
  967. * The capability_type argument can optionally be registered as an array, with
  968. * the first value being singular and the second plural, e.g. array('story, 'stories')
  969. * Otherwise, an 's' will be added to the value for the plural form. After
  970. * registration, capability_type will always be a string of the singular value.
  971. *
  972. * By default, seven keys are accepted as part of the capabilities array:
  973. *
  974. * - edit_post, read_post, and delete_post are meta capabilities, which are then
  975. * generally mapped to corresponding primitive capabilities depending on the
  976. * context, which would be the post being edited/read/deleted and the user or
  977. * role being checked. Thus these capabilities would generally not be granted
  978. * directly to users or roles.
  979. *
  980. * - edit_posts - Controls whether objects of this post type can be edited.
  981. * - edit_others_posts - Controls whether objects of this type owned by other users
  982. * can be edited. If the post type does not support an author, then this will
  983. * behave like edit_posts.
  984. * - publish_posts - Controls publishing objects of this post type.
  985. * - read_private_posts - Controls whether private objects can be read.
  986. *
  987. * These four primitive capabilities are checked in core in various locations.
  988. * There are also seven other primitive capabilities which are not referenced
  989. * directly in core, except in map_meta_cap(), which takes the three aforementioned
  990. * meta capabilities and translates them into one or more primitive capabilities
  991. * that must then be checked against the user or role, depending on the context.
  992. *
  993. * - read - Controls whether objects of this post type can be read.
  994. * - delete_posts - Controls whether objects of this post type can be deleted.
  995. * - delete_private_posts - Controls whether private objects can be deleted.
  996. * - delete_published_posts - Controls whether published objects can be deleted.
  997. * - delete_others_posts - Controls whether objects owned by other users can be
  998. * can be deleted. If the post type does not support an author, then this will
  999. * behave like delete_posts.
  1000. * - edit_private_posts - Controls whether private objects can be edited.
  1001. * - edit_published_posts - Controls whether published objects can be edited.
  1002. *
  1003. * These additional capabilities are only used in map_meta_cap(). Thus, they are
  1004. * only assigned by default if the post type is registered with the 'map_meta_cap'
  1005. * argument set to true (default is false).
  1006. *
  1007. * @see map_meta_cap()
  1008. * @since 3.0.0
  1009. *
  1010. * @param object $args Post type registration arguments
  1011. * @return object object with all the capabilities as member variables
  1012. */
  1013. function get_post_type_capabilities( $args ) {
  1014. if ( ! is_array( $args->capability_type ) )
  1015. $args->capability_type = array( $args->capability_type, $args->capability_type . 's' );
  1016. // Singular base for meta capabilities, plural base for primitive capabilities.
  1017. list( $singular_base, $plural_base ) = $args->capability_type;
  1018. $default_capabilities = array(
  1019. // Meta capabilities
  1020. 'edit_post' => 'edit_' . $singular_base,
  1021. 'read_post' => 'read_' . $singular_base,
  1022. 'delete_post' => 'delete_' . $singular_base,
  1023. // Primitive capabilities used outside of map_meta_cap():
  1024. 'edit_posts' => 'edit_' . $plural_base,
  1025. 'edit_others_posts' => 'edit_others_' . $plural_base,
  1026. 'publish_posts' => 'publish_' . $plural_base,
  1027. 'read_private_posts' => 'read_private_' . $plural_base,
  1028. );
  1029. // Primitive capabilities used within map_meta_cap():
  1030. if ( $args->map_meta_cap ) {
  1031. $default_capabilities_for_mapping = array(
  1032. 'read' => 'read',
  1033. 'delete_posts' => 'delete_' . $plural_base,
  1034. 'delete_private_posts' => 'delete_private_' . $plural_base,
  1035. 'delete_published_posts' => 'delete_published_' . $plural_base,
  1036. 'delete_others_posts' => 'delete_others_' . $plural_base,
  1037. 'edit_private_posts' => 'edit_private_' . $plural_base,
  1038. 'edit_published_posts' => 'edit_published_' . $plural_base,
  1039. );
  1040. $default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
  1041. }
  1042. $capabilities = array_merge( $default_capabilities, $args->capabilities );
  1043. // Remember meta capabilities for future reference.
  1044. if ( $args->map_meta_cap )
  1045. _post_type_meta_capabilities( $capabilities );
  1046. return (object) $capabilities;
  1047. }
  1048. /**
  1049. * Stores or returns a list of post type meta caps for map_meta_cap().
  1050. *
  1051. * @since 3.1.0
  1052. * @access private
  1053. */
  1054. function _post_type_meta_capabilities( $capabilities = null ) {
  1055. static $meta_caps = array();
  1056. if ( null === $capabilities )
  1057. return $meta_caps;
  1058. foreach ( $capabilities as $core => $custom ) {
  1059. if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) )
  1060. $meta_caps[ $custom ] = $core;
  1061. }
  1062. }
  1063. /**
  1064. * Builds an object with all post type labels out of a post type object
  1065. *
  1066. * Accepted keys of the label array in the post type object:
  1067. * - name - general name for the post type, usually plural. The same and overridden by $post_type_object->label. Default is Posts/Pages
  1068. * - singular_name - name for one object of this post type. Default is Post/Page
  1069. * - add_new - Default is Add New for both hierarchical and non-hierarchical types. When internationalizing this string, please use a {@link http://codex.wordpress.org/I18n_for_WordPress_Developers#Disambiguation_by_context gettext context} matching your post type. Example: <code>_x('Add New', 'product');</code>
  1070. * - add_new_item - Default is Add New Post/Add New Page
  1071. * - edit_item - Default is Edit Post/Edit Page
  1072. * - new_item - Default is New Post/New Page
  1073. * - view_item - Default is View Post/View Page
  1074. * - search_items - Default is Search Posts/Search Pages
  1075. * - not_found - Default is No posts found/No pages found
  1076. * - not_found_in_trash - Default is No posts found in Trash/No pages found in Trash
  1077. * - parent_item_colon - This string isn't used on non-hierarchical types. In hierarchical ones the default is Parent Page:
  1078. * - all_items - String for the submenu. Default is All Posts/All Pages
  1079. * - menu_name - Default is the same as <code>name</code>
  1080. *
  1081. * Above, the first default value is for non-hierarchical post types (like posts) and the second one is for hierarchical post types (like pages).
  1082. *
  1083. * @since 3.0.0
  1084. * @param object $post_type_object
  1085. * @return object object with all the labels as member variables
  1086. */
  1087. function get_post_type_labels( $post_type_object ) {
  1088. $nohier_vs_hier_defaults = array(
  1089. 'name' => array( _x('Posts', 'post type general name'), _x('Pages', 'post type general name') ),
  1090. 'singular_name' => array( _x('Post', 'post type singular name'), _x('Page', 'post type singular name') ),
  1091. 'add_new' => array( _x('Add New', 'post'), _x('Add New', 'page') ),
  1092. 'add_new_item' => array( __('Add New Post'), __('Add New Page') ),
  1093. 'edit_item' => array( __('Edit Post'), __('Edit Page') ),
  1094. 'new_item' => array( __('New Post'), __('New Page') ),
  1095. 'view_item' => array( __('View Post'), __('View Page') ),
  1096. 'search_items' => array( __('Search Posts'), __('Search Pages') ),
  1097. 'not_found' => array( __('No posts found.'), __('No pages found.') ),
  1098. 'not_found_in_trash' => array( __('No posts found in Trash.'), __('No pages found in Trash.') ),
  1099. 'parent_item_colon' => array( null, __('Parent Page:') ),
  1100. 'all_items' => array( __( 'All Posts' ), __( 'All Pages' ) )
  1101. );
  1102. $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
  1103. return _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
  1104. }
  1105. /**
  1106. * Builds an object with custom-something object (post type, taxonomy) labels out of a custom-something object
  1107. *
  1108. * @access private
  1109. * @since 3.0.0
  1110. */
  1111. function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) {
  1112. if ( isset( $object->label ) && empty( $object->labels['name'] ) )
  1113. $object->labels['name'] = $object->label;
  1114. if ( !isset( $object->labels['singular_name'] ) && isset( $object->labels['name'] ) )
  1115. $object->labels['singular_name'] = $object->labels['name'];
  1116. if ( ! isset( $object->labels['name_admin_bar'] ) )
  1117. $object->labels['name_admin_bar'] = isset( $object->labels['singular_name'] ) ? $object->labels['singular_name'] : $object->name;
  1118. if ( !isset( $object->labels['menu_name'] ) && isset( $object->labels['name'] ) )
  1119. $object->labels['menu_name'] = $object->labels['name'];
  1120. if ( !isset( $object->labels['all_items'] ) && isset( $object->labels['menu_name'] ) )
  1121. $object->labels['all_items'] = $object->labels['menu_name'];
  1122. foreach ( $nohier_vs_hier_defaults as $key => $value )
  1123. $defaults[$key] = $object->hierarchical ? $value[1] : $value[0];
  1124. $labels = array_merge( $defaults, $object->labels );
  1125. return (object)$labels;
  1126. }
  1127. /**
  1128. * Adds submenus for post types.
  1129. *
  1130. * @access private
  1131. * @since 3.1.0
  1132. */
  1133. function _add_post_type_submenus() {
  1134. foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) {
  1135. $ptype_obj = get_post_type_object( $ptype );
  1136. // Submenus only.
  1137. if ( ! $ptype_obj->show_in_menu || $ptype_obj->show_in_menu === true )
  1138. continue;
  1139. add_submenu_page( $ptype_obj->show_in_menu, $ptype_obj->labels->name, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype" );
  1140. }
  1141. }
  1142. add_action( 'admin_menu', '_add_post_type_submenus' );
  1143. /**
  1144. * Register support of certain features for a post type.
  1145. *
  1146. * All features are directly associated with a functional area of the edit screen, such as the
  1147. * editor or a meta box: 'title', 'editor', 'comments', 'revisions', 'trackbacks', 'author',
  1148. * 'excerpt', 'page-attributes', 'thumbnail', and 'custom-fields'.
  1149. *
  1150. * Additionally, the 'revisions' feature dictates whether the post type will store revisions,
  1151. * and the 'comments' feature dictates whether the comments count will show on the edit screen.
  1152. *
  1153. * @since 3.0.0
  1154. * @param string $post_type The post type for which to add the feature
  1155. * @param string|array $feature the feature being added, can be an array of feature strings or a single string
  1156. */
  1157. function add_post_type_support( $post_type, $feature ) {
  1158. global $_wp_post_type_features;
  1159. $features = (array) $feature;
  1160. foreach ($features as $feature) {
  1161. if ( func_num_args() == 2 )
  1162. $_wp_post_type_features[$post_type][$feature] = true;
  1163. else
  1164. $_wp_post_type_features[$post_type][$feature] = array_slice( func_get_args(), 2 );
  1165. }
  1166. }
  1167. /**
  1168. * Remove support for a feature from a post type.
  1169. *
  1170. * @since 3.0.0
  1171. * @param string $post_type The post type for which to remove the feature
  1172. * @param string $featureā€¦

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