PageRenderTime 804ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/post.php

https://github.com/davodey/WordPress
PHP | 5801 lines | 2567 code | 749 blank | 2485 comment | 712 complexity | 128385616ccbfa14fe7cc0fc04d0902c 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. return get_post_status($post->post_parent);
  727. }
  728. return $post->post_status;
  729. }
  730. /**
  731. * Retrieve all of the WordPress supported post statuses.
  732. *
  733. * Posts have a limited set of valid status values, this provides the
  734. * post_status values and descriptions.
  735. *
  736. * @since 2.5.0
  737. *
  738. * @return array List of post statuses.
  739. */
  740. function get_post_statuses() {
  741. $status = array(
  742. 'draft' => __('Draft'),
  743. 'pending' => __('Pending Review'),
  744. 'private' => __('Private'),
  745. 'publish' => __('Published')
  746. );
  747. return $status;
  748. }
  749. /**
  750. * Retrieve all of the WordPress support page statuses.
  751. *
  752. * Pages have a limited set of valid status values, this provides the
  753. * post_status values and descriptions.
  754. *
  755. * @since 2.5.0
  756. *
  757. * @return array List of page statuses.
  758. */
  759. function get_page_statuses() {
  760. $status = array(
  761. 'draft' => __('Draft'),
  762. 'private' => __('Private'),
  763. 'publish' => __('Published')
  764. );
  765. return $status;
  766. }
  767. /**
  768. * Register a post status. Do not use before init.
  769. *
  770. * A simple function for creating or modifying a post status based on the
  771. * parameters given. The function will accept an array (second optional
  772. * parameter), along with a string for the post status name.
  773. *
  774. *
  775. * Optional $args contents:
  776. *
  777. * label - A descriptive name for the post status marked for translation. Defaults to $post_status.
  778. * public - Whether posts of this status should be shown in the front end of the site. Defaults to true.
  779. * exclude_from_search - Whether to exclude posts with this post status from search results. Defaults to false.
  780. * show_in_admin_all_list - Whether to include posts in the edit listing for their post type
  781. * show_in_admin_status_list - Show in the list of statuses with post counts at the top of the edit
  782. * listings, e.g. All (12) | Published (9) | My Custom Status (2) ...
  783. *
  784. * Arguments prefixed with an _underscore shouldn't be used by plugins and themes.
  785. *
  786. * @since 3.0.0
  787. * @uses $wp_post_statuses Inserts new post status object into the list
  788. *
  789. * @param string $post_status Name of the post status.
  790. * @param array|string $args See above description.
  791. */
  792. function register_post_status($post_status, $args = array()) {
  793. global $wp_post_statuses;
  794. if (!is_array($wp_post_statuses))
  795. $wp_post_statuses = array();
  796. // Args prefixed with an underscore are reserved for internal use.
  797. $defaults = array(
  798. 'label' => false,
  799. 'label_count' => false,
  800. 'exclude_from_search' => null,
  801. '_builtin' => false,
  802. 'public' => null,
  803. 'internal' => null,
  804. 'protected' => null,
  805. 'private' => null,
  806. 'publicly_queryable' => null,
  807. 'show_in_admin_status_list' => null,
  808. 'show_in_admin_all_list' => null,
  809. );
  810. $args = wp_parse_args($args, $defaults);
  811. $args = (object) $args;
  812. $post_status = sanitize_key($post_status);
  813. $args->name = $post_status;
  814. if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private )
  815. $args->internal = true;
  816. if ( null === $args->public )
  817. $args->public = false;
  818. if ( null === $args->private )
  819. $args->private = false;
  820. if ( null === $args->protected )
  821. $args->protected = false;
  822. if ( null === $args->internal )
  823. $args->internal = false;
  824. if ( null === $args->publicly_queryable )
  825. $args->publicly_queryable = $args->public;
  826. if ( null === $args->exclude_from_search )
  827. $args->exclude_from_search = $args->internal;
  828. if ( null === $args->show_in_admin_all_list )
  829. $args->show_in_admin_all_list = !$args->internal;
  830. if ( null === $args->show_in_admin_status_list )
  831. $args->show_in_admin_status_list = !$args->internal;
  832. if ( false === $args->label )
  833. $args->label = $post_status;
  834. if ( false === $args->label_count )
  835. $args->label_count = array( $args->label, $args->label );
  836. $wp_post_statuses[$post_status] = $args;
  837. return $args;
  838. }
  839. /**
  840. * Retrieve a post status object by name
  841. *
  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. * @since 3.0.0
  860. * @uses $wp_post_statuses
  861. * @see register_post_status
  862. * @see get_post_status_object
  863. *
  864. * @param array|string $args An array of key => value arguments to match against the post status objects.
  865. * @param string $output The type of output to return, either post status 'names' or 'objects'. 'names' is the default.
  866. * @param string $operator The logical operation to perform. 'or' means only one element
  867. * from the array needs to match; 'and' means all elements must match. The default is 'and'.
  868. * @return array A list of post status names or objects
  869. */
  870. function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) {
  871. global $wp_post_statuses;
  872. $field = ('names' == $output) ? 'name' : false;
  873. return wp_filter_object_list($wp_post_statuses, $args, $operator, $field);
  874. }
  875. /**
  876. * Whether the post type is hierarchical.
  877. *
  878. * A false return value might also mean that the post type does not exist.
  879. *
  880. * @since 3.0.0
  881. * @see get_post_type_object
  882. *
  883. * @param string $post_type Post type name
  884. * @return bool Whether post type is hierarchical.
  885. */
  886. function is_post_type_hierarchical( $post_type ) {
  887. if ( ! post_type_exists( $post_type ) )
  888. return false;
  889. $post_type = get_post_type_object( $post_type );
  890. return $post_type->hierarchical;
  891. }
  892. /**
  893. * Checks if a post type is registered.
  894. *
  895. * @since 3.0.0
  896. * @uses get_post_type_object()
  897. *
  898. * @param string $post_type Post type name
  899. * @return bool Whether post type is registered.
  900. */
  901. function post_type_exists( $post_type ) {
  902. return (bool) get_post_type_object( $post_type );
  903. }
  904. /**
  905. * Retrieve the post type of the current post or of a given post.
  906. *
  907. * @since 2.1.0
  908. *
  909. * @param int|WP_Post $post Optional. Post ID or post object.
  910. * @return string|bool Post type on success, false on failure.
  911. */
  912. function get_post_type( $post = null ) {
  913. if ( $post = get_post( $post ) )
  914. return $post->post_type;
  915. return false;
  916. }
  917. /**
  918. * Retrieve a post type object by name
  919. *
  920. * @since 3.0.0
  921. * @uses $wp_post_types
  922. * @see register_post_type
  923. * @see get_post_types
  924. *
  925. * @param string $post_type The name of a registered post type
  926. * @return object A post type object
  927. */
  928. function get_post_type_object( $post_type ) {
  929. global $wp_post_types;
  930. if ( empty($wp_post_types[$post_type]) )
  931. return null;
  932. return $wp_post_types[$post_type];
  933. }
  934. /**
  935. * Get a list of all registered post type objects.
  936. *
  937. * @since 2.9.0
  938. * @uses $wp_post_types
  939. * @see register_post_type
  940. *
  941. * @param array|string $args An array of key => value arguments to match against the post type objects.
  942. * @param string $output The type of output to return, either post type 'names' or 'objects'. 'names' is the default.
  943. * @param string $operator The logical operation to perform. 'or' means only one element
  944. * from the array needs to match; 'and' means all elements must match. The default is 'and'.
  945. * @return array A list of post type names or objects
  946. */
  947. function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) {
  948. global $wp_post_types;
  949. $field = ('names' == $output) ? 'name' : false;
  950. return wp_filter_object_list($wp_post_types, $args, $operator, $field);
  951. }
  952. /**
  953. * Register a post type. Do not use before init.
  954. *
  955. * A function for creating or modifying a post type based on the
  956. * parameters given. The function will accept an array (second optional
  957. * parameter), along with a string for the post type name.
  958. *
  959. * Optional $args contents:
  960. *
  961. * - label - Name of the post type shown in the menu. Usually plural. If not set, labels['name'] will be used.
  962. * - labels - An array of labels for this post type.
  963. * * If not set, post labels are inherited for non-hierarchical types and page labels for hierarchical ones.
  964. * * You can see accepted values in {@link get_post_type_labels()}.
  965. * - description - A short descriptive summary of what the post type is. Defaults to blank.
  966. * - public - Whether a post type is intended for use publicly either via the admin interface or by front-end users.
  967. * * Defaults to false.
  968. * * While the default settings of exclude_from_search, publicly_queryable, show_ui, and show_in_nav_menus are
  969. * inherited from public, each does not rely on this relationship and controls a very specific intention.
  970. * - hierarchical - Whether the post type is hierarchical (e.g. page). Defaults to false.
  971. * - exclude_from_search - Whether to exclude posts with this post type from front end search results.
  972. * * If not set, the opposite of public's current value is used.
  973. * - publicly_queryable - Whether queries can be performed on the front end for the post type as part of parse_request().
  974. * * ?post_type={post_type_key}
  975. * * ?{post_type_key}={single_post_slug}
  976. * * ?{post_type_query_var}={single_post_slug}
  977. * * If not set, the default is inherited from public.
  978. * - show_ui - Whether to generate a default UI for managing this post type in the admin.
  979. * * If not set, the default is inherited from public.
  980. * - show_in_menu - Where to show the post type in the admin menu.
  981. * * If true, the post type is shown in its own top level menu.
  982. * * If false, no menu is shown
  983. * * If a string of an existing top level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post type will
  984. * be placed as a sub menu of that.
  985. * * show_ui must be true.
  986. * * If not set, the default is inherited from show_ui
  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_admin_bar - Makes this post type available via the admin bar.
  990. * * If not set, the default is inherited from show_in_menu
  991. * - menu_position - The position in the menu order the post type should appear.
  992. * * show_in_menu must be true
  993. * * Defaults to null, which places it at the bottom of its area.
  994. * - menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon.
  995. * * Pass a base64-encoded SVG using a data URI, which will be colored to match the color scheme.
  996. * This should begin with 'data:image/svg+xml;base64,'.
  997. * * Pass the name of a Dashicons helper class to use a font icon, e.g. 'dashicons-chart-pie'.
  998. * * Pass 'none' to leave div.wp-menu-image empty so an icon can be added via CSS.
  999. * - capability_type - The string to use to build the read, edit, and delete capabilities. Defaults to 'post'.
  1000. * * May be passed as an array to allow for alternative plurals when using this argument as a base to construct the
  1001. * capabilities, e.g. array('story', 'stories').
  1002. * - capabilities - Array of capabilities for this post type.
  1003. * * By default the capability_type is used as a base to construct capabilities.
  1004. * * You can see accepted values in {@link get_post_type_capabilities()}.
  1005. * - map_meta_cap - Whether to use the internal default meta capability handling. Defaults to false.
  1006. * - supports - An alias for calling add_post_type_support() directly. Defaults to title and editor.
  1007. * * See {@link add_post_type_support()} for documentation.
  1008. * - register_meta_box_cb - Provide a callback function that sets up the meta boxes
  1009. * for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback.
  1010. * - taxonomies - An array of taxonomy identifiers that will be registered for the post type.
  1011. * * Default is no taxonomies.
  1012. * * Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type().
  1013. * - has_archive - True to enable post type archives. Default is false.
  1014. * * Will generate the proper rewrite rules if rewrite is enabled.
  1015. * - rewrite - Triggers the handling of rewrites for this post type. Defaults to true, using $post_type as slug.
  1016. * * To prevent rewrite, set to false.
  1017. * * To specify rewrite rules, an array can be passed with any of these keys
  1018. * * 'slug' => string Customize the permastruct slug. Defaults to $post_type key
  1019. * * 'with_front' => bool Should the permastruct be prepended with WP_Rewrite::$front. Defaults to true.
  1020. * * 'feeds' => bool Should a feed permastruct be built for this post type. Inherits default from has_archive.
  1021. * * 'pages' => bool Should the permastruct provide for pagination. Defaults to true.
  1022. * * 'ep_mask' => const Assign an endpoint mask.
  1023. * * If not specified and permalink_epmask is set, inherits from permalink_epmask.
  1024. * * If not specified and permalink_epmask is not set, defaults to EP_PERMALINK
  1025. * - query_var - Sets the query_var key for this post type. Defaults to $post_type key
  1026. * * If false, a post type cannot be loaded at ?{query_var}={post_slug}
  1027. * * If specified as a string, the query ?{query_var_string}={post_slug} will be valid.
  1028. * - can_export - Allows this post type to be exported. Defaults to true.
  1029. * - delete_with_user - Whether to delete posts of this type when deleting a user.
  1030. * * If true, posts of this type belonging to the user will be moved to trash when then user is deleted.
  1031. * * If false, posts of this type belonging to the user will *not* be trashed or deleted.
  1032. * * If not set (the default), posts are trashed if post_type_supports('author'). Otherwise posts are not trashed or deleted.
  1033. * - _builtin - true if this post type is a native or "built-in" post_type. THIS IS FOR INTERNAL USE ONLY!
  1034. * - _edit_link - URL segement to use for edit link of this post type. THIS IS FOR INTERNAL USE ONLY!
  1035. *
  1036. * @since 2.9.0
  1037. * @uses $wp_post_types Inserts new post type object into the list
  1038. * @uses $wp_rewrite Gets default feeds
  1039. * @uses $wp Adds query vars
  1040. *
  1041. * @param string $post_type Post type key, must not exceed 20 characters.
  1042. * @param array|string $args See optional args description above.
  1043. * @return object|WP_Error the registered post type object, or an error object.
  1044. */
  1045. function register_post_type( $post_type, $args = array() ) {
  1046. global $wp_post_types, $wp_rewrite, $wp;
  1047. if ( ! is_array( $wp_post_types ) )
  1048. $wp_post_types = array();
  1049. // Args prefixed with an underscore are reserved for internal use.
  1050. $defaults = array(
  1051. 'labels' => array(),
  1052. 'description' => '',
  1053. 'public' => false,
  1054. 'hierarchical' => false,
  1055. 'exclude_from_search' => null,
  1056. 'publicly_queryable' => null,
  1057. 'show_ui' => null,
  1058. 'show_in_menu' => null,
  1059. 'show_in_nav_menus' => null,
  1060. 'show_in_admin_bar' => null,
  1061. 'menu_position' => null,
  1062. 'menu_icon' => null,
  1063. 'capability_type' => 'post',
  1064. 'capabilities' => array(),
  1065. 'map_meta_cap' => null,
  1066. 'supports' => array(),
  1067. 'register_meta_box_cb' => null,
  1068. 'taxonomies' => array(),
  1069. 'has_archive' => false,
  1070. 'rewrite' => true,
  1071. 'query_var' => true,
  1072. 'can_export' => true,
  1073. 'delete_with_user' => null,
  1074. '_builtin' => false,
  1075. '_edit_link' => 'post.php?post=%d',
  1076. );
  1077. $args = wp_parse_args( $args, $defaults );
  1078. $args = (object) $args;
  1079. $post_type = sanitize_key( $post_type );
  1080. $args->name = $post_type;
  1081. if ( strlen( $post_type ) > 20 )
  1082. return new WP_Error( 'post_type_too_long', __( 'Post types cannot exceed 20 characters in length' ) );
  1083. // If not set, default to the setting for public.
  1084. if ( null === $args->publicly_queryable )
  1085. $args->publicly_queryable = $args->public;
  1086. // If not set, default to the setting for public.
  1087. if ( null === $args->show_ui )
  1088. $args->show_ui = $args->public;
  1089. // If not set, default to the setting for show_ui.
  1090. if ( null === $args->show_in_menu || ! $args->show_ui )
  1091. $args->show_in_menu = $args->show_ui;
  1092. // If not set, default to the whether the full UI is shown.
  1093. if ( null === $args->show_in_admin_bar )
  1094. $args->show_in_admin_bar = true === $args->show_in_menu;
  1095. // If not set, default to the setting for public.
  1096. if ( null === $args->show_in_nav_menus )
  1097. $args->show_in_nav_menus = $args->public;
  1098. // If not set, default to true if not public, false if public.
  1099. if ( null === $args->exclude_from_search )
  1100. $args->exclude_from_search = !$args->public;
  1101. // Back compat with quirky handling in version 3.0. #14122
  1102. if ( empty( $args->capabilities ) && null === $args->map_meta_cap && in_array( $args->capability_type, array( 'post', 'page' ) ) )
  1103. $args->map_meta_cap = true;
  1104. // If not set, default to false.
  1105. if ( null === $args->map_meta_cap )
  1106. $args->map_meta_cap = false;
  1107. $args->cap = get_post_type_capabilities( $args );
  1108. unset( $args->capabilities );
  1109. if ( is_array( $args->capability_type ) )
  1110. $args->capability_type = $args->capability_type[0];
  1111. if ( ! empty( $args->supports ) ) {
  1112. add_post_type_support( $post_type, $args->supports );
  1113. unset( $args->supports );
  1114. } elseif ( false !== $args->supports ) {
  1115. // Add default features
  1116. add_post_type_support( $post_type, array( 'title', 'editor' ) );
  1117. }
  1118. if ( false !== $args->query_var && ! empty( $wp ) ) {
  1119. if ( true === $args->query_var )
  1120. $args->query_var = $post_type;
  1121. else
  1122. $args->query_var = sanitize_title_with_dashes( $args->query_var );
  1123. $wp->add_query_var( $args->query_var );
  1124. }
  1125. if ( false !== $args->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
  1126. if ( ! is_array( $args->rewrite ) )
  1127. $args->rewrite = array();
  1128. if ( empty( $args->rewrite['slug'] ) )
  1129. $args->rewrite['slug'] = $post_type;
  1130. if ( ! isset( $args->rewrite['with_front'] ) )
  1131. $args->rewrite['with_front'] = true;
  1132. if ( ! isset( $args->rewrite['pages'] ) )
  1133. $args->rewrite['pages'] = true;
  1134. if ( ! isset( $args->rewrite['feeds'] ) || ! $args->has_archive )
  1135. $args->rewrite['feeds'] = (bool) $args->has_archive;
  1136. if ( ! isset( $args->rewrite['ep_mask'] ) ) {
  1137. if ( isset( $args->permalink_epmask ) )
  1138. $args->rewrite['ep_mask'] = $args->permalink_epmask;
  1139. else
  1140. $args->rewrite['ep_mask'] = EP_PERMALINK;
  1141. }
  1142. if ( $args->hierarchical )
  1143. add_rewrite_tag( "%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&pagename=" );
  1144. else
  1145. add_rewrite_tag( "%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=" );
  1146. if ( $args->has_archive ) {
  1147. $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive;
  1148. if ( $args->rewrite['with_front'] )
  1149. $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
  1150. else
  1151. $archive_slug = $wp_rewrite->root . $archive_slug;
  1152. add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", 'top' );
  1153. if ( $args->rewrite['feeds'] && $wp_rewrite->feeds ) {
  1154. $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
  1155. add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
  1156. add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
  1157. }
  1158. if ( $args->rewrite['pages'] )
  1159. add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' );
  1160. }
  1161. $permastruct_args = $args->rewrite;
  1162. $permastruct_args['feed'] = $permastruct_args['feeds'];
  1163. add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $permastruct_args );
  1164. }
  1165. if ( $args->register_meta_box_cb )
  1166. add_action( 'add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1 );
  1167. $args->labels = get_post_type_labels( $args );
  1168. $args->label = $args->labels->name;
  1169. $wp_post_types[ $post_type ] = $args;
  1170. add_action( 'future_' . $post_type, '_future_post_hook', 5, 2 );
  1171. foreach ( $args->taxonomies as $taxonomy ) {
  1172. register_taxonomy_for_object_type( $taxonomy, $post_type );
  1173. }
  1174. /**
  1175. * Fires after a post type is registered.
  1176. *
  1177. * @since 3.3.0
  1178. *
  1179. * @param string $post_type Post type.
  1180. * @param array $args Arguments used to register the post type.
  1181. */
  1182. do_action( 'registered_post_type', $post_type, $args );
  1183. return $args;
  1184. }
  1185. /**
  1186. * Builds an object with all post type capabilities out of a post type object
  1187. *
  1188. * Post type capabilities use the 'capability_type' argument as a base, if the
  1189. * capability is not set in the 'capabilities' argument array or if the
  1190. * 'capabilities' argument is not supplied.
  1191. *
  1192. * The capability_type argument can optionally be registered as an array, with
  1193. * the first value being singular and the second plural, e.g. array('story, 'stories')
  1194. * Otherwise, an 's' will be added to the value for the plural form. After
  1195. * registration, capability_type will always be a string of the singular value.
  1196. *
  1197. * By default, seven keys are accepted as part of the capabilities array:
  1198. *
  1199. * - edit_post, read_post, and delete_post are meta capabilities, which are then
  1200. * generally mapped to corresponding primitive capabilities depending on the
  1201. * context, which would be the post being edited/read/deleted and the user or
  1202. * role being checked. Thus these capabilities would generally not be granted
  1203. * directly to users or roles.
  1204. *
  1205. * - edit_posts - Controls whether objects of this post type can be edited.
  1206. * - edit_others_posts - Controls whether objects of this type owned by other users
  1207. * can be edited. If the post type does not support an author, then this will
  1208. * behave like edit_posts.
  1209. * - publish_posts - Controls publishing objects of this post type.
  1210. * - read_private_posts - Controls whether private objects can be read.
  1211. *
  1212. * These four primitive capabilities are checked in core in various locations.
  1213. * There are also seven other primitive capabilities which are not referenced
  1214. * directly in core, except in map_meta_cap(), which takes the three aforementioned
  1215. * meta capabilities and translates them into one or more primitive capabilities
  1216. * that must then be checked against the user or role, depending on the context.
  1217. *
  1218. * - read - Controls whether objects of this post type can be read.
  1219. * - delete_posts - Controls whether objects of this post type can be deleted.
  1220. * - delete_private_posts - Controls whether private objects can be deleted.
  1221. * - delete_published_posts - Controls whether published objects can be deleted.
  1222. * - delete_others_posts - Controls whether objects owned by other users can be
  1223. * can be deleted. If the post type does not support an author, then this will
  1224. * behave like delete_posts.
  1225. * - edit_private_posts - Controls whether private objects can be edited.
  1226. * - edit_published_posts - Controls whether published objects can be edited.
  1227. *
  1228. * These additional capabilities are only used in map_meta_cap(). Thus, they are
  1229. * only assigned by default if the post type is registered with the 'map_meta_cap'
  1230. * argument set to true (default is false).
  1231. *
  1232. * @see map_meta_cap()
  1233. * @since 3.0.0
  1234. *
  1235. * @param object $args Post type registration arguments
  1236. * @return object object with all the capabilities as member variables
  1237. */
  1238. function get_post_type_capabilities( $args ) {
  1239. if ( ! is_array( $args->capability_type ) )
  1240. $args->capability_type = array( $args->capability_type, $args->capability_type . 's' );
  1241. // Singular base for meta capabilities, plural base for primitive capabilities.
  1242. list( $singular_base, $plural_base ) = $args->capability_type;
  1243. $default_capabilities = array(
  1244. // Meta capabilities
  1245. 'edit_post' => 'edit_' . $singular_base,
  1246. 'read_post' => 'read_' . $singular_base,
  1247. 'delete_post' => 'delete_' . $singular_base,
  1248. // Primitive capabilities used outside of map_meta_cap():
  1249. 'edit_posts' => 'edit_' . $plural_base,
  1250. 'edit_others_posts' => 'edit_others_' . $plural_base,
  1251. 'publish_posts' => 'publish_' . $plural_base,
  1252. 'read_private_posts' => 'read_private_' . $plural_base,
  1253. );
  1254. // Primitive capabilities used within map_meta_cap():
  1255. if ( $args->map_meta_cap ) {
  1256. $default_capabilities_for_mapping = array(
  1257. 'read' => 'read',
  1258. 'delete_posts' => 'delete_' . $plural_base,
  1259. 'delete_private_posts' => 'delete_private_' . $plural_base,
  1260. 'delete_published_posts' => 'delete_published_' . $plural_base,
  1261. 'delete_others_posts' => 'delete_others_' . $plural_base,
  1262. 'edit_private_posts' => 'edit_private_' . $plural_base,
  1263. 'edit_published_posts' => 'edit_published_' . $plural_base,
  1264. );
  1265. $default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
  1266. }
  1267. $capabilities = array_merge( $default_capabilities, $args->capabilities );
  1268. // Post creation capability simply maps to edit_posts by default:
  1269. if ( ! isset( $capabilities['create_posts'] ) )
  1270. $capabilities['create_posts'] = $capabilities['edit_posts'];
  1271. // Remember meta capabilities for future reference.
  1272. if ( $args->map_meta_cap )
  1273. _post_type_meta_capabilities( $capabilities );
  1274. return (object) $capabilities;
  1275. }
  1276. /**
  1277. * Stores or returns a list of post type meta caps for map_meta_cap().
  1278. *
  1279. * @since 3.1.0
  1280. * @access private
  1281. */
  1282. function _post_type_meta_capabilities( $capabilities = null ) {
  1283. static $meta_caps = array();
  1284. if ( null === $capabilities )
  1285. return $meta_caps;
  1286. foreach ( $capabilities as $core => $custom ) {
  1287. if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) )
  1288. $meta_caps[ $custom ] = $core;
  1289. }
  1290. }
  1291. /**
  1292. * Builds an object with all post type labels out of a post type object
  1293. *
  1294. * Accepted keys of the label array in the post type object:
  1295. * - name - general name for the post type, usually plural. The same and overridden by $post_type_object->label. Default is Posts/Pages
  1296. * - singular_name - name for one object of this post type. Default is Post/Page
  1297. * - 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>
  1298. * - add_new_item - Default is Add New Post/Add New Page
  1299. * - edit_item - Default is Edit Post/Edit Page
  1300. * - new_item - Default is New Post/New Page
  1301. * - view_item - Default is View Post/View Page
  1302. * - search_items - Default is Search Posts/Search Pages
  1303. * - not_found - Default is No posts found/No pages found
  1304. * - not_found_in_trash - Default is No posts found in Trash/No pages found in Trash
  1305. * - parent_item_colon - This string isn't used on non-hierarchical type…

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