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

/wp-includes/post.php

https://bitbucket.org/abnopanda/wordpress
PHP | 5548 lines | 2668 code | 780 blank | 2100 comment | 760 complexity | fe42a2eb6072630af61831b282d7ddef MD5 | raw file

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

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