/wp-includes/post.php

https://bitbucket.org/crafttheweb/wordpress-fold · PHP · 5431 lines · 2636 code · 751 blank · 2044 comment · 749 complexity · c9ab13daaedcc1ecdb97fce91cb4f5a9 MD5 · raw file

Large files are truncated 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 WP_Post|null WP_Post on success or null on failure
  346. */
  347. function get_post( $post, $output = OBJECT, $filter = 'raw' ) {
  348. if ( empty( $post ) && isset( $GLOBALS['post'] ) )
  349. $post = $GLOBALS['post'];
  350. if ( is_a( $post, 'WP_Post' ) ) {
  351. $_post = $post;
  352. } elseif ( is_object( $post ) ) {
  353. if ( empty( $post->filter ) ) {
  354. $_post = sanitize_post( $post, 'raw' );
  355. wp_cache_add( $post->ID, $_post, 'posts' );
  356. $_post = new WP_Post( $_post );
  357. } elseif ( 'raw' == $post->filter ) {
  358. $_post = new WP_Post( $post );
  359. } else {
  360. $_post = WP_Post::get_instance( $post->ID );
  361. }
  362. } else {
  363. $_post = WP_Post::get_instance( $post );
  364. }
  365. if ( ! $_post )
  366. return null;
  367. $_post = $_post->filter( $filter );
  368. if ( $output == ARRAY_A )
  369. return $_post->to_array();
  370. elseif ( $output == ARRAY_N )
  371. return array_values( $_post->to_array() );
  372. return $_post;
  373. }
  374. /**
  375. * WordPress Post class.
  376. *
  377. * @since 3.5.0
  378. *
  379. * @property $ID;
  380. * @property $post_author;
  381. * @property $post_date;
  382. * @property $post_date_gmt;
  383. * @property $post_content;
  384. * @property $post_title;
  385. * @property $post_excerpt;
  386. * @property $post_status;
  387. * @property $comment_status;
  388. * @property $ping_status;
  389. * @property $post_password;
  390. * @property $post_name;
  391. * @property $to_ping;
  392. * @property $pinged;
  393. * @property $post_modified;
  394. * @property $post_modified_gmt;
  395. * @property $post_content_filtered;
  396. * @property $post_parent;
  397. * @property $guid;
  398. * @property $menu_order;
  399. * @property $post_type;
  400. * @property $post_mime_type;
  401. * @property $comment_count;
  402. * @property $ancestors;
  403. */
  404. final class WP_Post {
  405. public $filter;
  406. public static function get_instance( $post_id ) {
  407. global $wpdb;
  408. $post_id = (int) $post_id;
  409. if ( !$post_id )
  410. return false;
  411. if ( ! $_post = wp_cache_get( $post_id, 'posts' ) ) {
  412. $_post = $wpdb->get_row( $wpdb->prepare( "
  413. SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1
  414. ", $post_id ) );
  415. if ( ! $_post )
  416. return false;
  417. $_post = sanitize_post( $_post, 'raw' );
  418. wp_cache_add( $_post->ID, $_post, 'posts' );
  419. }
  420. return new WP_Post( $_post );
  421. }
  422. public function __construct( $post ) {
  423. foreach ( get_object_vars( $post ) as $key => $value )
  424. $this->$key = $value;
  425. }
  426. public function __isset( $key ) {
  427. if ( 'ancestors' == $key )
  428. return true;
  429. return metadata_exists( 'post', $this->ID, $key );
  430. }
  431. public function &__get( $key ) {
  432. if ( 'ancestors' == $key ) {
  433. $value = get_post_ancestors( $this );
  434. } else {
  435. $value = get_post_meta( $this->ID, $key, true );
  436. }
  437. if ( $this->filter ) {
  438. $value = sanitize_post_field( $key, $value, $this->ID, $this->filter );
  439. }
  440. return $value;
  441. }
  442. public function filter( $filter ) {
  443. if ( $this->filter == $filter )
  444. return $this;
  445. if ( $filter == 'raw' )
  446. return self::get_instance( $this->ID );
  447. return sanitize_post( $this, $filter );
  448. }
  449. public function to_array() {
  450. $post = get_object_vars( $this );
  451. $post['ancestors'] = array();
  452. return $post;
  453. }
  454. }
  455. /**
  456. * Retrieve ancestors of a post.
  457. *
  458. * @since 2.5.0
  459. *
  460. * @param int|object $post Post ID or post object
  461. * @return array Ancestor IDs or empty array if none are found.
  462. */
  463. function get_post_ancestors( $post ) {
  464. if ( ! $post )
  465. return false;
  466. $post = get_post( $post );
  467. if ( ! $ancestors = wp_cache_get( $post->ID, 'post_ancestors' ) ) {
  468. $ancestors = array();
  469. if ( !empty( $post->post_parent ) && $post->ID != $post->post_parent ) {
  470. $id = $ancestors[] = $post->post_parent;
  471. while ( $ancestor = get_post( $id ) ) {
  472. // Loop detection: If the ancestor has been seen before, break.
  473. if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors ) )
  474. break;
  475. $id = $ancestors[] = $ancestor->post_parent;
  476. }
  477. }
  478. wp_cache_add( $post->ID, $ancestors, 'post_ancestors' );
  479. }
  480. return $ancestors;
  481. }
  482. /**
  483. * Retrieve data from a post field based on Post ID.
  484. *
  485. * Examples of the post field will be, 'post_type', 'post_status', 'post_content',
  486. * etc and based off of the post object property or key names.
  487. *
  488. * The context values are based off of the taxonomy filter functions and
  489. * supported values are found within those functions.
  490. *
  491. * @since 2.3.0
  492. * @uses sanitize_post_field() See for possible $context values.
  493. *
  494. * @param string $field Post field name
  495. * @param id $post Post ID
  496. * @param string $context Optional. How to filter the field. Default is display.
  497. * @return bool|string False on failure or returns the value in post field
  498. */
  499. function get_post_field( $field, $post, $context = 'display' ) {
  500. $post = get_post( $post );
  501. if ( !$post )
  502. return '';
  503. if ( !isset($post->$field) )
  504. return '';
  505. return sanitize_post_field($field, $post->$field, $post->ID, $context);
  506. }
  507. /**
  508. * Retrieve the mime type of an attachment based on the ID.
  509. *
  510. * This function can be used with any post type, but it makes more sense with
  511. * attachments.
  512. *
  513. * @since 2.0.0
  514. *
  515. * @param int $ID Optional. Post ID.
  516. * @return bool|string False on failure or returns the mime type
  517. */
  518. function get_post_mime_type($ID = '') {
  519. $post = get_post($ID);
  520. if ( is_object($post) )
  521. return $post->post_mime_type;
  522. return false;
  523. }
  524. /**
  525. * Retrieve the format slug for a post
  526. *
  527. * @since 3.1.0
  528. *
  529. * @param int|object $post A post
  530. *
  531. * @return mixed The format if successful. False if no format is set. WP_Error if errors.
  532. */
  533. function get_post_format( $post = null ) {
  534. $post = get_post($post);
  535. if ( ! post_type_supports( $post->post_type, 'post-formats' ) )
  536. return false;
  537. $_format = get_the_terms( $post->ID, 'post_format' );
  538. if ( empty( $_format ) )
  539. return false;
  540. $format = array_shift( $_format );
  541. return ( str_replace('post-format-', '', $format->slug ) );
  542. }
  543. /**
  544. * Check if a post has a particular format
  545. *
  546. * @since 3.1.0
  547. * @uses has_term()
  548. *
  549. * @param string $format The format to check for
  550. * @param object|id $post The post to check. If not supplied, defaults to the current post if used in the loop.
  551. * @return bool True if the post has the format, false otherwise.
  552. */
  553. function has_post_format( $format, $post = null ) {
  554. return has_term('post-format-' . sanitize_key($format), 'post_format', $post);
  555. }
  556. /**
  557. * Assign a format to a post
  558. *
  559. * @since 3.1.0
  560. *
  561. * @param int|object $post The post for which to assign a format
  562. * @param string $format A format to assign. Use an empty string or array to remove all formats from the post.
  563. * @return mixed WP_Error on error. Array of affected term IDs on success.
  564. */
  565. function set_post_format( $post, $format ) {
  566. $post = get_post($post);
  567. if ( empty($post) )
  568. return new WP_Error('invalid_post', __('Invalid post'));
  569. if ( !empty($format) ) {
  570. $format = sanitize_key($format);
  571. if ( 'standard' == $format || !in_array( $format, array_keys( get_post_format_slugs() ) ) )
  572. $format = '';
  573. else
  574. $format = 'post-format-' . $format;
  575. }
  576. return wp_set_post_terms($post->ID, $format, 'post_format');
  577. }
  578. /**
  579. * Retrieve the post status based on the Post ID.
  580. *
  581. * If the post ID is of an attachment, then the parent post status will be given
  582. * instead.
  583. *
  584. * @since 2.0.0
  585. *
  586. * @param int $ID Post ID
  587. * @return string|bool Post status or false on failure.
  588. */
  589. function get_post_status($ID = '') {
  590. $post = get_post($ID);
  591. if ( !is_object($post) )
  592. return false;
  593. if ( 'attachment' == $post->post_type ) {
  594. if ( 'private' == $post->post_status )
  595. return 'private';
  596. // Unattached attachments are assumed to be published
  597. if ( ( 'inherit' == $post->post_status ) && ( 0 == $post->post_parent) )
  598. return 'publish';
  599. // Inherit status from the parent
  600. if ( $post->post_parent && ( $post->ID != $post->post_parent ) )
  601. return get_post_status($post->post_parent);
  602. }
  603. return $post->post_status;
  604. }
  605. /**
  606. * Retrieve all of the WordPress supported post statuses.
  607. *
  608. * Posts have a limited set of valid status values, this provides the
  609. * post_status values and descriptions.
  610. *
  611. * @since 2.5.0
  612. *
  613. * @return array List of post statuses.
  614. */
  615. function get_post_statuses( ) {
  616. $status = array(
  617. 'draft' => __('Draft'),
  618. 'pending' => __('Pending Review'),
  619. 'private' => __('Private'),
  620. 'publish' => __('Published')
  621. );
  622. return $status;
  623. }
  624. /**
  625. * Retrieve all of the WordPress support page statuses.
  626. *
  627. * Pages have a limited set of valid status values, this provides the
  628. * post_status values and descriptions.
  629. *
  630. * @since 2.5.0
  631. *
  632. * @return array List of page statuses.
  633. */
  634. function get_page_statuses( ) {
  635. $status = array(
  636. 'draft' => __('Draft'),
  637. 'private' => __('Private'),
  638. 'publish' => __('Published')
  639. );
  640. return $status;
  641. }
  642. /**
  643. * Register a post status. Do not use before init.
  644. *
  645. * A simple function for creating or modifying a post status based on the
  646. * parameters given. The function will accept an array (second optional
  647. * parameter), along with a string for the post status name.
  648. *
  649. *
  650. * Optional $args contents:
  651. *
  652. * label - A descriptive name for the post status marked for translation. Defaults to $post_status.
  653. * public - Whether posts of this status should be shown in the front end of the site. Defaults to true.
  654. * exclude_from_search - Whether to exclude posts with this post status from search results. Defaults to false.
  655. * show_in_admin_all_list - Whether to include posts in the edit listing for their post type
  656. * show_in_admin_status_list - Show in the list of statuses with post counts at the top of the edit
  657. * listings, e.g. All (12) | Published (9) | My Custom Status (2) ...
  658. *
  659. * Arguments prefixed with an _underscore shouldn't be used by plugins and themes.
  660. *
  661. * @package WordPress
  662. * @subpackage Post
  663. * @since 3.0.0
  664. * @uses $wp_post_statuses Inserts new post status object into the list
  665. *
  666. * @param string $post_status Name of the post status.
  667. * @param array|string $args See above description.
  668. */
  669. function register_post_status($post_status, $args = array()) {
  670. global $wp_post_statuses;
  671. if (!is_array($wp_post_statuses))
  672. $wp_post_statuses = array();
  673. // Args prefixed with an underscore are reserved for internal use.
  674. $defaults = array(
  675. 'label' => false,
  676. 'label_count' => false,
  677. 'exclude_from_search' => null,
  678. '_builtin' => false,
  679. 'public' => null,
  680. 'internal' => null,
  681. 'protected' => null,
  682. 'private' => null,
  683. 'show_in_admin_all' => null,
  684. 'publicly_queryable' => null,
  685. 'show_in_admin_status_list' => null,
  686. 'show_in_admin_all_list' => null,
  687. 'single_view_cap' => null,
  688. );
  689. $args = wp_parse_args($args, $defaults);
  690. $args = (object) $args;
  691. $post_status = sanitize_key($post_status);
  692. $args->name = $post_status;
  693. if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private )
  694. $args->internal = true;
  695. if ( null === $args->public )
  696. $args->public = false;
  697. if ( null === $args->private )
  698. $args->private = false;
  699. if ( null === $args->protected )
  700. $args->protected = false;
  701. if ( null === $args->internal )
  702. $args->internal = false;
  703. if ( null === $args->publicly_queryable )
  704. $args->publicly_queryable = $args->public;
  705. if ( null === $args->exclude_from_search )
  706. $args->exclude_from_search = $args->internal;
  707. if ( null === $args->show_in_admin_all_list )
  708. $args->show_in_admin_all_list = !$args->internal;
  709. if ( null === $args->show_in_admin_status_list )
  710. $args->show_in_admin_status_list = !$args->internal;
  711. if ( null === $args->single_view_cap )
  712. $args->single_view_cap = $args->public ? '' : 'edit';
  713. if ( false === $args->label )
  714. $args->label = $post_status;
  715. if ( false === $args->label_count )
  716. $args->label_count = array( $args->label, $args->label );
  717. $wp_post_statuses[$post_status] = $args;
  718. return $args;
  719. }
  720. /**
  721. * Retrieve a post status object by name
  722. *
  723. * @package WordPress
  724. * @subpackage Post
  725. * @since 3.0.0
  726. * @uses $wp_post_statuses
  727. * @see register_post_status
  728. * @see get_post_statuses
  729. *
  730. * @param string $post_status The name of a registered post status
  731. * @return object A post status object
  732. */
  733. function get_post_status_object( $post_status ) {
  734. global $wp_post_statuses;
  735. if ( empty($wp_post_statuses[$post_status]) )
  736. return null;
  737. return $wp_post_statuses[$post_status];
  738. }
  739. /**
  740. * Get a list of all registered post status objects.
  741. *
  742. * @package WordPress
  743. * @subpackage Post
  744. * @since 3.0.0
  745. * @uses $wp_post_statuses
  746. * @see register_post_status
  747. * @see get_post_status_object
  748. *
  749. * @param array|string $args An array of key => value arguments to match against the post status objects.
  750. * @param string $output The type of output to return, either post status 'names' or 'objects'. 'names' is the default.
  751. * @param string $operator The logical operation to perform. 'or' means only one element
  752. * from the array needs to match; 'and' means all elements must match. The default is 'and'.
  753. * @return array A list of post status names or objects
  754. */
  755. function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) {
  756. global $wp_post_statuses;
  757. $field = ('names' == $output) ? 'name' : false;
  758. return wp_filter_object_list($wp_post_statuses, $args, $operator, $field);
  759. }
  760. /**
  761. * Whether the post type is hierarchical.
  762. *
  763. * A false return value might also mean that the post type does not exist.
  764. *
  765. * @since 3.0.0
  766. * @see get_post_type_object
  767. *
  768. * @param string $post_type Post type name
  769. * @return bool Whether post type is hierarchical.
  770. */
  771. function is_post_type_hierarchical( $post_type ) {
  772. if ( ! post_type_exists( $post_type ) )
  773. return false;
  774. $post_type = get_post_type_object( $post_type );
  775. return $post_type->hierarchical;
  776. }
  777. /**
  778. * Checks if a post type is registered.
  779. *
  780. * @since 3.0.0
  781. * @uses get_post_type_object()
  782. *
  783. * @param string $post_type Post type name
  784. * @return bool Whether post type is registered.
  785. */
  786. function post_type_exists( $post_type ) {
  787. return (bool) get_post_type_object( $post_type );
  788. }
  789. /**
  790. * Retrieve the post type of the current post or of a given post.
  791. *
  792. * @since 2.1.0
  793. *
  794. * @uses $post The Loop current post global
  795. *
  796. * @param mixed $the_post Optional. Post object or post ID.
  797. * @return bool|string post type or false on failure.
  798. */
  799. function get_post_type( $the_post = false ) {
  800. global $post;
  801. if ( false === $the_post )
  802. $the_post = $post;
  803. elseif ( is_numeric($the_post) )
  804. $the_post = get_post($the_post);
  805. if ( is_object($the_post) )
  806. return $the_post->post_type;
  807. return false;
  808. }
  809. /**
  810. * Retrieve a post type object by name
  811. *
  812. * @package WordPress
  813. * @subpackage Post
  814. * @since 3.0.0
  815. * @uses $wp_post_types
  816. * @see register_post_type
  817. * @see get_post_types
  818. *
  819. * @param string $post_type The name of a registered post type
  820. * @return object A post type object
  821. */
  822. function get_post_type_object( $post_type ) {
  823. global $wp_post_types;
  824. if ( empty($wp_post_types[$post_type]) )
  825. return null;
  826. return $wp_post_types[$post_type];
  827. }
  828. /**
  829. * Get a list of all registered post type objects.
  830. *
  831. * @package WordPress
  832. * @subpackage Post
  833. * @since 2.9.0
  834. * @uses $wp_post_types
  835. * @see register_post_type
  836. *
  837. * @param array|string $args An array of key => value arguments to match against the post type objects.
  838. * @param string $output The type of output to return, either post type 'names' or 'objects'. 'names' is the default.
  839. * @param string $operator The logical operation to perform. 'or' means only one element
  840. * from the array needs to match; 'and' means all elements must match. The default is 'and'.
  841. * @return array A list of post type names or objects
  842. */
  843. function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) {
  844. global $wp_post_types;
  845. $field = ('names' == $output) ? 'name' : false;
  846. return wp_filter_object_list($wp_post_types, $args, $operator, $field);
  847. }
  848. /**
  849. * Register a post type. Do not use before init.
  850. *
  851. * A function for creating or modifying a post type based on the
  852. * parameters given. The function will accept an array (second optional
  853. * parameter), along with a string for the post type name.
  854. *
  855. * Optional $args contents:
  856. *
  857. * - label - Name of the post type shown in the menu. Usually plural. If not set, labels['name'] will be used.
  858. * - labels - An array of labels for this post type.
  859. * * If not set, post labels are inherited for non-hierarchical types and page labels for hierarchical ones.
  860. * * You can see accepted values in {@link get_post_type_labels()}.
  861. * - description - A short descriptive summary of what the post type is. Defaults to blank.
  862. * - public - Whether a post type is intended for use publicly either via the admin interface or by front-end users.
  863. * * Defaults to false.
  864. * * While the default settings of exclude_from_search, publicly_queryable, show_ui, and show_in_nav_menus are
  865. * inherited from public, each does not rely on this relationship and controls a very specific intention.
  866. * - exclude_from_search - Whether to exclude posts with this post type from front end search results.
  867. * * If not set, the the opposite of public's current value is used.
  868. * - publicly_queryable - Whether queries can be performed on the front end for the post type as part of parse_request().
  869. * * ?post_type={post_type_key}
  870. * * ?{post_type_key}={single_post_slug}
  871. * * ?{post_type_query_var}={single_post_slug}
  872. * * If not set, the default is inherited from public.
  873. * - show_ui - Whether to generate a default UI for managing this post type in the admin.
  874. * * If not set, the default is inherited from public.
  875. * - show_in_nav_menus - Makes this post type available for selection in navigation menus.
  876. * * If not set, the default is inherited from public.
  877. * - show_in_menu - Where to show the post type in the admin menu.
  878. * * If true, the post type is shown in its own top level menu.
  879. * * If false, no menu is shown
  880. * * If a string of an existing top level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post type will
  881. * be placed as a sub menu of that.
  882. * * show_ui must be true.
  883. * * If not set, the default is inherited from show_ui
  884. * - show_in_admin_bar - Makes this post type available via the admin bar.
  885. * * If not set, the default is inherited from show_in_menu
  886. * - menu_position - The position in the menu order the post type should appear.
  887. * * show_in_menu must be true
  888. * * Defaults to null, which places it at the bottom of its area.
  889. * - menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon.
  890. * - capability_type - The string to use to build the read, edit, and delete capabilities. Defaults to 'post'.
  891. * * May be passed as an array to allow for alternative plurals when using this argument as a base to construct the
  892. * capabilities, e.g. array('story', 'stories').
  893. * - capabilities - Array of capabilities for this post type.
  894. * * By default the capability_type is used as a base to construct capabilities.
  895. * * You can see accepted values in {@link get_post_type_capabilities()}.
  896. * - map_meta_cap - Whether to use the internal default meta capability handling. Defaults to false.
  897. * - hierarchical - Whether the post type is hierarchical (e.g. page). Defaults to false.
  898. * - supports - An alias for calling add_post_type_support() directly. Defaults to title and editor.
  899. * * See {@link add_post_type_support()} for documentation.
  900. * - register_meta_box_cb - Provide a callback function that will be called when setting up the
  901. * meta boxes for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback.
  902. * - taxonomies - An array of taxonomy identifiers that will be registered for the post type.
  903. * * Default is no taxonomies.
  904. * * Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type().
  905. * - has_archive - True to enable post type archives. Default is false.
  906. * * Will generate the proper rewrite rules if rewrite is enabled.
  907. * - rewrite - Triggers the handling of rewrites for this post type. Defaults to true, using $post_type as slug.
  908. * * To prevent rewrite, set to false.
  909. * * To specify rewrite rules, an array can be passed with any of these keys
  910. * * 'slug' => string Customize the permastruct slug. Defaults to $post_type key
  911. * * 'with_front' => bool Should the permastruct be prepended with WP_Rewrite::$front. Defaults to true.
  912. * * 'feeds' => bool Should a feed permastruct be built for this post type. Inherits default from has_archive.
  913. * * 'pages' => bool Should the permastruct provide for pagination. Defaults to true.
  914. * * 'ep_mask' => const Assign an endpoint mask.
  915. * * If not specified and permalink_epmask is set, inherits from permalink_epmask.
  916. * * If not specified and permalink_epmask is not set, defaults to EP_PERMALINK
  917. * - query_var - Sets the query_var key for this post type. Defaults to $post_type key
  918. * * If false, a post type cannot be loaded at ?{query_var}={post_slug}
  919. * * If specified as a string, the query ?{query_var_string}={post_slug} will be valid.
  920. * - can_export - Allows this post type to be exported. Defaults to true.
  921. * - delete_with_user - Whether to delete posts of this type when deleting a user.
  922. * * If true, posts of this type belonging to the user will be moved to trash when then user is deleted.
  923. * * If false, posts of this type belonging to the user will *not* be trashed or deleted.
  924. * * If not set (the default), posts are trashed if post_type_supports('author'). Otherwise posts are not trashed or deleted.
  925. * - _builtin - true if this post type is a native or "built-in" post_type. THIS IS FOR INTERNAL USE ONLY!
  926. * - _edit_link - URL segement to use for edit link of this post type. THIS IS FOR INTERNAL USE ONLY!
  927. *
  928. * @since 2.9.0
  929. * @uses $wp_post_types Inserts new post type object into the list
  930. *
  931. * @param string $post_type Post type key, must not exceed 20 characters
  932. * @param array|string $args See optional args description above.
  933. * @return object|WP_Error the registered post type object, or an error object
  934. */
  935. function register_post_type( $post_type, $args = array() ) {
  936. global $wp_post_types, $wp_rewrite, $wp;
  937. if ( !is_array($wp_post_types) )
  938. $wp_post_types = array();
  939. // Args prefixed with an underscore are reserved for internal use.
  940. $defaults = array(
  941. 'labels' => array(), 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => null,
  942. 'capability_type' => 'post', 'capabilities' => array(), 'map_meta_cap' => null,
  943. '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'hierarchical' => false,
  944. 'public' => false, 'rewrite' => true, 'has_archive' => false, 'query_var' => true,
  945. 'supports' => array(), 'register_meta_box_cb' => null,
  946. 'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null,
  947. 'can_export' => true,
  948. 'show_in_nav_menus' => null, 'show_in_menu' => null, 'show_in_admin_bar' => null,
  949. 'delete_with_user' => null,
  950. );
  951. $args = wp_parse_args($args, $defaults);
  952. $args = (object) $args;
  953. $post_type = sanitize_key($post_type);
  954. $args->name = $post_type;
  955. if ( strlen( $post_type ) > 20 )
  956. return new WP_Error( 'post_type_too_long', __( 'Post types cannot exceed 20 characters in length' ) );
  957. // If not set, default to the setting for public.
  958. if ( null === $args->publicly_queryable )
  959. $args->publicly_queryable = $args->public;
  960. // If not set, default to the setting for public.
  961. if ( null === $args->show_ui )
  962. $args->show_ui = $args->public;
  963. // If not set, default to the setting for show_ui.
  964. if ( null === $args->show_in_menu || ! $args->show_ui )
  965. $args->show_in_menu = $args->show_ui;
  966. // If not set, default to the whether the full UI is shown.
  967. if ( null === $args->show_in_admin_bar )
  968. $args->show_in_admin_bar = true === $args->show_in_menu;
  969. // Whether to show this type in nav-menus.php. Defaults to the setting for public.
  970. if ( null === $args->show_in_nav_menus )
  971. $args->show_in_nav_menus = $args->public;
  972. // If not set, default to true if not public, false if public.
  973. if ( null === $args->exclude_from_search )
  974. $args->exclude_from_search = !$args->public;
  975. // Back compat with quirky handling in version 3.0. #14122
  976. if ( empty( $args->capabilities ) && null === $args->map_meta_cap && in_array( $args->capability_type, array( 'post', 'page' ) ) )
  977. $args->map_meta_cap = true;
  978. if ( null === $args->map_meta_cap )
  979. $args->map_meta_cap = false;
  980. $args->cap = get_post_type_capabilities( $args );
  981. unset($args->capabilities);
  982. if ( is_array( $args->capability_type ) )
  983. $args->capability_type = $args->capability_type[0];
  984. if ( ! empty($args->supports) ) {
  985. add_post_type_support($post_type, $args->supports);
  986. unset($args->supports);
  987. } else {
  988. // Add default features
  989. add_post_type_support($post_type, array('title', 'editor'));
  990. }
  991. if ( false !== $args->query_var && !empty($wp) ) {
  992. if ( true === $args->query_var )
  993. $args->query_var = $post_type;
  994. else
  995. $args->query_var = sanitize_title_with_dashes($args->query_var);
  996. $wp->add_query_var($args->query_var);
  997. }
  998. if ( false !== $args->rewrite && ( is_admin() || '' != get_option('permalink_structure') ) ) {
  999. if ( ! is_array( $args->rewrite ) )
  1000. $args->rewrite = array();
  1001. if ( empty( $args->rewrite['slug'] ) )
  1002. $args->rewrite['slug'] = $post_type;
  1003. if ( ! isset( $args->rewrite['with_front'] ) )
  1004. $args->rewrite['with_front'] = true;
  1005. if ( ! isset( $args->rewrite['pages'] ) )
  1006. $args->rewrite['pages'] = true;
  1007. if ( ! isset( $args->rewrite['feeds'] ) || ! $args->has_archive )
  1008. $args->rewrite['feeds'] = (bool) $args->has_archive;
  1009. if ( ! isset( $args->rewrite['ep_mask'] ) ) {
  1010. if ( isset( $args->permalink_epmask ) )
  1011. $args->rewrite['ep_mask'] = $args->permalink_epmask;
  1012. else
  1013. $args->rewrite['ep_mask'] = EP_PERMALINK;
  1014. }
  1015. if ( $args->hierarchical )
  1016. add_rewrite_tag("%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
  1017. else
  1018. add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
  1019. if ( $args->has_archive ) {
  1020. $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive;
  1021. if ( $args->rewrite['with_front'] )
  1022. $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
  1023. else
  1024. $archive_slug = $wp_rewrite->root . $archive_slug;
  1025. add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", 'top' );
  1026. if ( $args->rewrite['feeds'] && $wp_rewrite->feeds ) {
  1027. $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
  1028. add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
  1029. add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
  1030. }
  1031. if ( $args->rewrite['pages'] )
  1032. add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' );
  1033. }
  1034. add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite );
  1035. }
  1036. if ( $args->register_meta_box_cb )
  1037. add_action('add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1);
  1038. $args->labels = get_post_type_labels( $args );
  1039. $args->label = $args->labels->name;
  1040. $wp_post_types[$post_type] = $args;
  1041. add_action( 'future_' . $post_type, '_future_post_hook', 5, 2 );
  1042. foreach ( $args->taxonomies as $taxonomy ) {
  1043. register_taxonomy_for_object_type( $taxonomy, $post_type );
  1044. }
  1045. do_action( 'registered_post_type', $post_type, $args );
  1046. return $args;
  1047. }
  1048. /**
  1049. * Builds an object with all post type capabilities out of a post type object
  1050. *
  1051. * Post type capabilities use the 'capability_type' argument as a base, if the
  1052. * capability is not set in the 'capabilities' argument array or if the
  1053. * 'capabilities' argument is not supplied.
  1054. *
  1055. * The capability_type argument can optionally be registered as an array, with
  1056. * the first value being singular and the second plural, e.g. array('story, 'stories')
  1057. * Otherwise, an 's' will be added to the value for the plural form. After
  1058. * registration, capability_type will always be a string of the singular value.
  1059. *
  1060. * By default, seven keys are accepted as part of the capabilities array:
  1061. *
  1062. * - edit_post, read_post, and delete_post are meta capabilities, which are then
  1063. * generally mapped to corresponding primitive capabilities depending on the
  1064. * context, which would be the post being edited/read/deleted and the user or
  1065. * role being checked. Thus these capabilities would generally not be granted
  1066. * directly to users or roles.
  1067. *
  1068. * - edit_posts - Controls whether objects of this post type can be edited.
  1069. * - edit_others_posts - Controls whether objects of this type owned by other users
  1070. * can be edited. If the post type does not support an author, then this will
  1071. * behave like edit_posts.
  1072. * - publish_posts - Controls publishing objects of this post type.
  1073. * - read_private_posts - Controls whether private objects can be read.
  1074. *
  1075. * These four primitive capabilities are checked in core in various locations.
  1076. * There are also seven other primitive capabilities which are not referenced
  1077. * directly in core, except in map_meta_cap(), which takes the three aforementioned
  1078. * meta capabilities and translates them into one or more primitive capabilities
  1079. * that must then be checked against the user or role, depending on the context.
  1080. *
  1081. * - read - Controls whether objects of this post type can be read.
  1082. * - delete_posts - Controls whether objects of this post type can be deleted.
  1083. * - delete_private_posts - Controls whether private objects can be deleted.
  1084. * - delete_published_posts - Controls whether published objects can be deleted.
  1085. * - delete_others_posts - Controls whether objects owned by other users can be
  1086. * can be deleted. If the post type does not support an author, then this will
  1087. * behave like delete_posts.
  1088. * - edit_private_posts - Controls whether private objects can be edited.
  1089. * - edit_published_posts - Controls whether published objects can be edited.
  1090. *
  1091. * These additional capabilities are only used in map_meta_cap(). Thus, they are
  1092. * only assigned by default if the post type is registered with the 'map_meta_cap'
  1093. * argument set to true (default is false).
  1094. *
  1095. * @see map_meta_cap()
  1096. * @since 3.0.0
  1097. *
  1098. * @param object $args Post type registration arguments
  1099. * @return object object with all the capabilities as member variables
  1100. */
  1101. function get_post_type_capabilities( $args ) {
  1102. if ( ! is_array( $args->capability_type ) )
  1103. $args->capability_type = array( $args->capability_type, $args->capability_type . 's' );
  1104. // Singular base for meta capabilities, plural base for primitive capabilities.
  1105. list( $singular_base, $plural_base ) = $args->capability_type;
  1106. $default_capabilities = array(
  1107. // Meta capabilities
  1108. 'edit_post' => 'edit_' . $singular_base,
  1109. 'read_post' => 'read_' . $singular_base,
  1110. 'delete_post' => 'delete_' . $singular_base,
  1111. // Primitive capabilities used outside of map_meta_cap():
  1112. 'edit_posts' => 'edit_' . $plural_base,
  1113. 'edit_others_posts' => 'edit_others_' . $plural_base,
  1114. 'publish_posts' => 'publish_' . $plural_base,
  1115. 'read_private_posts' => 'read_private_' . $plural_base,
  1116. );
  1117. // Primitive capabilities used within map_meta_cap():
  1118. if ( $args->map_meta_cap ) {
  1119. $default_capabilities_for_mapping = array(
  1120. 'read' => 'read',
  1121. 'delete_posts' => 'delete_' . $plural_base,
  1122. 'delete_private_posts' => 'delete_private_' . $plural_base,
  1123. 'delete_published_posts' => 'delete_published_' . $plural_base,
  1124. 'delete_others_posts' => 'delete_others_' . $plural_base,
  1125. 'edit_private_posts' => 'edit_private_' . $plural_base,
  1126. 'edit_published_posts' => 'edit_published_' . $plural_base,
  1127. );
  1128. $default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
  1129. }
  1130. $capabilities = array_merge( $default_capabilities, $args->capabilities );
  1131. // Remember meta capabilities for future reference.
  1132. if ( $args->map_meta_cap )
  1133. _post_type_meta_capabilities( $capabilities );
  1134. return (object) $capabilities;
  1135. }
  1136. /**
  1137. * Stores or returns a list of post type meta caps for map_meta_cap().
  1138. *
  1139. * @since 3.1.0
  1140. * @access private
  1141. */
  1142. function _post_type_meta_capabilities( $capabilities = null ) {
  1143. static $meta_caps = array();
  1144. if ( null === $capabilities )
  1145. return $meta_caps;
  1146. foreach ( $capabilities as $core => $custom ) {
  1147. if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) )
  1148. $meta_caps[ $custom ] = $core;
  1149. }
  1150. }
  1151. /**
  1152. * Builds an object with all post type labels out of a post type object
  1153. *
  1154. * Accepted keys of the label array in the post type object:
  1155. * - name - general name for the post type, usually plural. The same and overridden by $post_type_object->label. Default is Posts/Pages
  1156. * - singular_name - name for one object of this post type. Default is Post/Page
  1157. * - 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>
  1158. * - add_new_item - Default is Add New Post/Add New Page
  1159. * - edit_item - Default is Edit Post/Edit Page
  1160. * - new_item - Default is New Post/New Page
  1161. * - view_item - Default is View Post/View Page
  1162. * - search_items - Default is Search Posts/Search Pages
  1163. * - not_found - Default is No posts found/No pages found
  1164. * - not_found_in_trash - Default is No posts found in Trash/No pages found in Trash
  1165. * - parent_item_colon - This string isn't used on non-hierarchical types. In hierarchical ones the default is Parent Page:
  1166. * - all_items - String for the submenu. Default is All Posts/All Pages
  1167. * - menu_name - Default is the same as <code>name</code>
  1168. *
  1169. * Above, the first default value is for non-hierarchical post types (like posts) and the second one is for hierarchical post types (like pages).
  1170. *
  1171. * @since 3.0.0
  1172. * @param object $post_type_object
  1173. * @return object object with all the labels as member variables
  1174. */
  1175. function get_post_type_labels( $post_type_object ) {
  1176. $nohier_vs_hier_defaults = array(
  1177. 'name' => array( _x('Posts', 'post type general name'), _x('Pages', 'post type general name') ),
  1178. 'singular_name' => array( _x('Post', 'post type singular name'), _x('Page', 'post type singular name') ),
  1179. 'add_new' => array( _x('Add New', 'post'), _x('Add New', 'page') ),
  1180. 'add_new_item' => array( __('Add New Post'), __('Add New Page') ),
  1181. 'edit_item' => array( __('Edit Post'), __('Edit Page') ),
  1182. 'new_item' => array( __('New Post'), __('New Page') ),
  1183. 'view_item' => array( __('View Post'), __('View Page') ),
  1184. 'search_items' => array( __('Search Posts'), __('Search Pages') ),
  1185. 'not_found' => array( __('No posts found.'), __('No pages found.') ),
  1186. 'not_found_in_trash' => array( __('No posts found in Trash.'), __('No pages found in Trash.') ),
  1187. 'parent_item_colon' => array( null, __('Parent Page:') ),
  1188. 'all_items' => array( __( 'All Posts' ), __( 'All Pages' ) )
  1189. );
  1190. $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
  1191. return _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
  1192. }
  1193. /**
  1194. * Builds an object with custom-something object (post type, taxonomy) labels out of a custom-something object
  1195. *
  1196. * @access private
  1197. * @since 3.0.0
  1198. */
  1199. function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) {
  1200. if ( isset( $object->label ) && empty( $object->labels['name'] ) )
  1201. $object->labels['name'] = $object->label;
  1202. if ( !isset( $object->labels['singular_name'] ) && isset( $object->labels['name'] ) )
  1203. $object->labels['singular_name'] = $object->labels['name'];
  1204. if ( ! isset( $object->labels['name_admin_bar'] ) )
  1205. $object->labels['name_admin_bar'] = isset( $object->labels['singular_name'] ) ? $object->labels['singular_name'] : $object->name;
  1206. if ( !isset( $object->labels['menu_name'] ) && isset( $object->labels['name'] ) )
  1207. $object->labels['menu_name'] = $object->labels['name'];
  1208. if ( !isset( $object->labels['all_items'] ) && isset( $obje