PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/post.php

https://github.com/shaiquddin/WordPress
PHP | 5398 lines | 2628 code | 749 blank | 2021 comment | 753 complexity | 4195354b7a1fdd6e1d439d39316d7176 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1

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

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

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