PageRenderTime 63ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/post.php

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

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