PageRenderTime 77ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/post.php

https://bitbucket.org/broderboy/shannonbroder-wordpress
PHP | 5685 lines | 2478 code | 741 blank | 2466 comment | 695 complexity | f4991d55c8b2e16e8b8e35877ab22a99 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, AGPL-1.0

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

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