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

/wp-includes/post.php

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