PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/post.php

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

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