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

/wp-includes/post.php

https://github.com/dipakdotyadav/WordPress
PHP | 4962 lines | 2396 code | 699 blank | 1867 comment | 678 complexity | 9324821b9c70b1d5a975726c254d6dd6 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1

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

  1. <?php
  2. /**
  3. * Post functions and post utility function.
  4. *
  5. * @package WordPress
  6. * @subpackage Post
  7. * @since 1.5.0
  8. */
  9. //
  10. // Post Type Registration
  11. //
  12. /**
  13. * Creates the initial post types when 'init' action is fired.
  14. *
  15. * @since 2.9.0
  16. */
  17. function create_initial_post_types() {
  18. register_post_type( 'post', array(
  19. 'labels' => array(
  20. 'name_admin_bar' => _x( 'Post', 'add new on admin bar' ),
  21. ),
  22. 'public' => true,
  23. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  24. '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
  25. 'capability_type' => 'post',
  26. 'map_meta_cap' => true,
  27. 'hierarchical' => false,
  28. 'rewrite' => false,
  29. 'query_var' => false,
  30. 'delete_with_user' => true,
  31. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
  32. ) );
  33. register_post_type( 'page', array(
  34. 'labels' => array(
  35. 'name_admin_bar' => _x( 'Page', 'add new on admin bar' ),
  36. ),
  37. 'public' => true,
  38. 'publicly_queryable' => false,
  39. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  40. '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
  41. 'capability_type' => 'page',
  42. 'map_meta_cap' => true,
  43. 'hierarchical' => true,
  44. 'rewrite' => false,
  45. 'query_var' => false,
  46. 'delete_with_user' => true,
  47. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
  48. ) );
  49. register_post_type( 'attachment', array(
  50. 'labels' => array(
  51. 'name' => _x('Media', 'post type general name'),
  52. 'name_admin_bar' => _x( 'Media', 'add new from admin bar' ),
  53. 'add_new' => _x( 'Add New', 'add new media' ),
  54. 'edit_item' => __( 'Edit Media' ),
  55. 'view_item' => __( 'View Attachment Page' ),
  56. ),
  57. 'public' => true,
  58. 'show_ui' => true,
  59. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  60. '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
  61. 'capability_type' => 'post',
  62. 'capabilities' => array(
  63. 'create_posts' => 'upload_files',
  64. ),
  65. 'map_meta_cap' => true,
  66. 'hierarchical' => false,
  67. 'rewrite' => false,
  68. 'query_var' => false,
  69. 'show_in_nav_menus' => false,
  70. 'delete_with_user' => true,
  71. 'supports' => array( 'title', 'author', 'comments' ),
  72. ) );
  73. 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. // ` 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. $post = get_post( $post );
  592. if ( ! $post || empty( $post->post_parent ) || $post->post_parent == $post->ID )
  593. return array();
  594. $ancestors = array();
  595. $id = $ancestors[] = $post->post_parent;
  596. while ( $ancestor = get_post( $id ) ) {
  597. // Loop detection: If the ancestor has been seen before, break.
  598. if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors ) )
  599. break;
  600. $id = $ancestors[] = $ancestor->post_parent;
  601. }
  602. return $ancestors;
  603. }
  604. /**
  605. * Retrieve data from a post field based on Post ID.
  606. *
  607. * Examples of the post field will be, 'post_type', 'post_status', 'post_content',
  608. * etc and based off of the post object property or key names.
  609. *
  610. * The context values are based off of the taxonomy filter functions and
  611. * supported values are found within those functions.
  612. *
  613. * @since 2.3.0
  614. * @uses sanitize_post_field() See for possible $context values.
  615. *
  616. * @param string $field Post field name
  617. * @param id $post Post ID
  618. * @param string $context Optional. How to filter the field. Default is display.
  619. * @return bool|string False on failure or returns the value in post field
  620. */
  621. function get_post_field( $field, $post, $context = 'display' ) {
  622. $post = get_post( $post );
  623. if ( !$post )
  624. return '';
  625. if ( !isset($post->$field) )
  626. return '';
  627. return sanitize_post_field($field, $post->$field, $post->ID, $context);
  628. }
  629. /**
  630. * Retrieve the mime type of an attachment based on the ID.
  631. *
  632. * This function can be used with any post type, but it makes more sense with
  633. * attachments.
  634. *
  635. * @since 2.0.0
  636. *
  637. * @param int $ID Optional. Post ID.
  638. * @return bool|string False on failure or returns the mime type
  639. */
  640. function get_post_mime_type($ID = '') {
  641. $post = get_post($ID);
  642. if ( is_object($post) )
  643. return $post->post_mime_type;
  644. return false;
  645. }
  646. /**
  647. * Retrieve the post status based on the Post ID.
  648. *
  649. * If the post ID is of an attachment, then the parent post status will be given
  650. * instead.
  651. *
  652. * @since 2.0.0
  653. *
  654. * @param int $ID Post ID
  655. * @return string|bool Post status or false on failure.
  656. */
  657. function get_post_status($ID = '') {
  658. $post = get_post($ID);
  659. if ( !is_object($post) )
  660. return false;
  661. if ( 'attachment' == $post->post_type ) {
  662. if ( 'private' == $post->post_status )
  663. return 'private';
  664. // Unattached attachments are assumed to be published
  665. if ( ( 'inherit' == $post->post_status ) && ( 0 == $post->post_parent) )
  666. return 'publish';
  667. // Inherit status from the parent
  668. if ( $post->post_parent && ( $post->ID != $post->post_parent ) )
  669. return get_post_status($post->post_parent);
  670. }
  671. return $post->post_status;
  672. }
  673. /**
  674. * Retrieve all of the WordPress supported post statuses.
  675. *
  676. * Posts have a limited set of valid status values, this provides the
  677. * post_status values and descriptions.
  678. *
  679. * @since 2.5.0
  680. *
  681. * @return array List of post statuses.
  682. */
  683. function get_post_statuses() {
  684. $status = array(
  685. 'draft' => __('Draft'),
  686. 'pending' => __('Pending Review'),
  687. 'private' => __('Private'),
  688. 'publish' => __('Published')
  689. );
  690. return $status;
  691. }
  692. /**
  693. * Retrieve all of the WordPress support page statuses.
  694. *
  695. * Pages have a limited set of valid status values, this provides the
  696. * post_status values and descriptions.
  697. *
  698. * @since 2.5.0
  699. *
  700. * @return array List of page statuses.
  701. */
  702. function get_page_statuses() {
  703. $status = array(
  704. 'draft' => __('Draft'),
  705. 'private' => __('Private'),
  706. 'publish' => __('Published')
  707. );
  708. return $status;
  709. }
  710. /**
  711. * Register a post status. Do not use before init.
  712. *
  713. * A simple function for creating or modifying a post status based on the
  714. * parameters given. The function will accept an array (second optional
  715. * parameter), along with a string for the post status name.
  716. *
  717. *
  718. * Optional $args contents:
  719. *
  720. * label - A descriptive name for the post status marked for translation. Defaults to $post_status.
  721. * public - Whether posts of this status should be shown in the front end of the site. Defaults to true.
  722. * exclude_from_search - Whether to exclude posts with this post status from search results. Defaults to false.
  723. * show_in_admin_all_list - Whether to include posts in the edit listing for their post type
  724. * show_in_admin_status_list - Show in the list of statuses with post counts at the top of the edit
  725. * listings, e.g. All (12) | Published (9) | My Custom Status (2) ...
  726. *
  727. * Arguments prefixed with an _underscore shouldn't be used by plugins and themes.
  728. *
  729. * @package WordPress
  730. * @subpackage Post
  731. * @since 3.0.0
  732. * @uses $wp_post_statuses Inserts new post status object into the list
  733. *
  734. * @param string $post_status Name of the post status.
  735. * @param array|string $args See above description.
  736. */
  737. function register_post_status($post_status, $args = array()) {
  738. global $wp_post_statuses;
  739. if (!is_array($wp_post_statuses))
  740. $wp_post_statuses = array();
  741. // Args prefixed with an underscore are reserved for internal use.
  742. $defaults = array(
  743. 'label' => false,
  744. 'label_count' => false,
  745. 'exclude_from_search' => null,
  746. '_builtin' => false,
  747. 'public' => null,
  748. 'internal' => null,
  749. 'protected' => null,
  750. 'private' => null,
  751. 'publicly_queryable' => null,
  752. 'show_in_admin_status_list' => null,
  753. 'show_in_admin_all_list' => null,
  754. );
  755. $args = wp_parse_args($args, $defaults);
  756. $args = (object) $args;
  757. $post_status = sanitize_key($post_status);
  758. $args->name = $post_status;
  759. if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private )
  760. $args->internal = true;
  761. if ( null === $args->public )
  762. $args->public = false;
  763. if ( null === $args->private )
  764. $args->private = false;
  765. if ( null === $args->protected )
  766. $args->protected = false;
  767. if ( null === $args->internal )
  768. $args->internal = false;
  769. if ( null === $args->publicly_queryable )
  770. $args->publicly_queryable = $args->public;
  771. if ( null === $args->exclude_from_search )
  772. $args->exclude_from_search = $args->internal;
  773. if ( null === $args->show_in_admin_all_list )
  774. $args->show_in_admin_all_list = !$args->internal;
  775. if ( null === $args->show_in_admin_status_list )
  776. $args->show_in_admin_status_list = !$args->internal;
  777. if ( false === $args->label )
  778. $args->label = $post_status;
  779. if ( false === $args->label_count )
  780. $args->label_count = array( $args->label, $args->label );
  781. $wp_post_statuses[$post_status] = $args;
  782. return $args;
  783. }
  784. /**
  785. * Retrieve a post status object by name
  786. *
  787. * @package WordPress
  788. * @subpackage Post
  789. * @since 3.0.0
  790. * @uses $wp_post_statuses
  791. * @see register_post_status
  792. * @see get_post_statuses
  793. *
  794. * @param string $post_status The name of a registered post status
  795. * @return object A post status object
  796. */
  797. function get_post_status_object( $post_status ) {
  798. global $wp_post_statuses;
  799. if ( empty($wp_post_statuses[$post_status]) )
  800. return null;
  801. return $wp_post_statuses[$post_status];
  802. }
  803. /**
  804. * Get a list of all registered post status objects.
  805. *
  806. * @package WordPress
  807. * @subpackage Post
  808. * @since 3.0.0
  809. * @uses $wp_post_statuses
  810. * @see register_post_status
  811. * @see get_post_status_object
  812. *
  813. * @param array|string $args An array of key => value arguments to match against the post status objects.
  814. * @param string $output The type of output to return, either post status 'names' or 'objects'. 'names' is the default.
  815. * @param string $operator The logical operation to perform. 'or' means only one element
  816. * from the array needs to match; 'and' means all elements must match. The default is 'and'.
  817. * @return array A list of post status names or objects
  818. */
  819. function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) {
  820. global $wp_post_statuses;
  821. $field = ('names' == $output) ? 'name' : false;
  822. return wp_filter_object_list($wp_post_statuses, $args, $operator, $field);
  823. }
  824. /**
  825. * Whether the post type is hierarchical.
  826. *
  827. * A false return value might also mean that the post type does not exist.
  828. *
  829. * @since 3.0.0
  830. * @see get_post_type_object
  831. *
  832. * @param string $post_type Post type name
  833. * @return bool Whether post type is hierarchical.
  834. */
  835. function is_post_type_hierarchical( $post_type ) {
  836. if ( ! post_type_exists( $post_type ) )
  837. return false;
  838. $post_type = get_post_type_object( $post_type );
  839. return $post_type->hierarchical;
  840. }
  841. /**
  842. * Checks if a post type is registered.
  843. *
  844. * @since 3.0.0
  845. * @uses get_post_type_object()
  846. *
  847. * @param string $post_type Post type name
  848. * @return bool Whether post type is registered.
  849. */
  850. function post_type_exists( $post_type ) {
  851. return (bool) get_post_type_object( $post_type );
  852. }
  853. /**
  854. * Retrieve the post type of the current post or of a given post.
  855. *
  856. * @since 2.1.0
  857. *
  858. * @uses $post The Loop current post global
  859. *
  860. * @param mixed $post Optional. Post object or post ID.
  861. * @return bool|string post type or false on failure.
  862. */
  863. function get_post_type( $post = null ) {
  864. if ( $post = get_post( $post ) )
  865. return $post->post_type;
  866. return false;
  867. }
  868. /**
  869. * Retrieve a post type object by name
  870. *
  871. * @package WordPress
  872. * @subpackage Post
  873. * @since 3.0.0
  874. * @uses $wp_post_types
  875. * @see register_post_type
  876. * @see get_post_types
  877. *
  878. * @param string $post_type The name of a registered post type
  879. * @return object A post type object
  880. */
  881. function get_post_type_object( $post_type ) {
  882. global $wp_post_types;
  883. if ( empty($wp_post_types[$post_type]) )
  884. return null;
  885. return $wp_post_types[$post_type];
  886. }
  887. /**
  888. * Get a list of all registered post type objects.
  889. *
  890. * @package WordPress
  891. * @subpackage Post
  892. * @since 2.9.0
  893. * @uses $wp_post_types
  894. * @see register_post_type
  895. *
  896. * @param array|string $args An array of key => value arguments to match against the post type objects.
  897. * @param string $output The type of output to return, either post type 'names' or 'objects'. 'names' is the default.
  898. * @param string $operator The logical operation to perform. 'or' means only one element
  899. * from the array needs to match; 'and' means all elements must match. The default is 'and'.
  900. * @return array A list of post type names or objects
  901. */
  902. function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) {
  903. global $wp_post_types;
  904. $field = ('names' == $output) ? 'name' : false;
  905. return wp_filter_object_list($wp_post_types, $args, $operator, $field);
  906. }
  907. /**
  908. * Register a post type. Do not use before init.
  909. *
  910. * A function for creating or modifying a post type based on the
  911. * parameters given. The function will accept an array (second optional
  912. * parameter), along with a string for the post type name.
  913. *
  914. * Optional $args contents:
  915. *
  916. * - label - Name of the post type shown in the menu. Usually plural. If not set, labels['name'] will be used.
  917. * - labels - An array of labels for this post type.
  918. * * If not set, post labels are inherited for non-hierarchical types and page labels for hierarchical ones.
  919. * * You can see accepted values in {@link get_post_type_labels()}.
  920. * - description - A short descriptive summary of what the post type is. Defaults to blank.
  921. * - public - Whether a post type is intended for use publicly either via the admin interface or by front-end users.
  922. * * Defaults to false.
  923. * * While the default settings of exclude_from_search, publicly_queryable, show_ui, and show_in_nav_menus are
  924. * inherited from public, each does not rely on this relationship and controls a very specific intention.
  925. * - exclude_from_search - Whether to exclude posts with this post type from front end search results.
  926. * * If not set, the the opposite of public's current value is used.
  927. * - publicly_queryable - Whether queries can be performed on the front end for the post type as part of parse_request().
  928. * * ?post_type={post_type_key}
  929. * * ?{post_type_key}={single_post_slug}
  930. * * ?{post_type_query_var}={single_post_slug}
  931. * * If not set, the default is inherited from public.
  932. * - show_ui - Whether to generate a default UI for managing this post type in the admin.
  933. * * If not set, the default is inherited from public.
  934. * - show_in_nav_menus - Makes this post type available for selection in navigation menus.
  935. * * If not set, the default is inherited from public.
  936. * - show_in_menu - Where to show the post type in the admin menu.
  937. * * If true, the post type is shown in its own top level menu.
  938. * * If false, no menu is shown
  939. * * If a string of an existing top level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post type will
  940. * be placed as a sub menu of that.
  941. * * show_ui must be true.
  942. * * If not set, the default is inherited from show_ui
  943. * - show_in_admin_bar - Makes this post type available via the admin bar.
  944. * * If not set, the default is inherited from show_in_menu
  945. * - menu_position - The position in the menu order the post type should appear.
  946. * * show_in_menu must be true
  947. * * Defaults to null, which places it at the bottom of its area.
  948. * - menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon.
  949. * - capability_type - The string to use to build the read, edit, and delete capabilities. Defaults to 'post'.
  950. * * May be passed as an array to allow for alternative plurals when using this argument as a base to construct the
  951. * capabilities, e.g. array('story', 'stories').
  952. * - capabilities - Array of capabilities for this post type.
  953. * * By default the capability_type is used as a base to construct capabilities.
  954. * * You can see accepted values in {@link get_post_type_capabilities()}.
  955. * - map_meta_cap - Whether to use the internal default meta capability handling. Defaults to false.
  956. * - hierarchical - Whether the post type is hierarchical (e.g. page). Defaults to false.
  957. * - supports - An alias for calling add_post_type_support() directly. Defaults to title and editor.
  958. * * See {@link add_post_type_support()} for documentation.
  959. * - register_meta_box_cb - Provide a callback function that will be called when setting up the
  960. * meta boxes for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback.
  961. * - taxonomies - An array of taxonomy identifiers that will be registered for the post type.
  962. * * Default is no taxonomies.
  963. * * Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type().
  964. * - has_archive - True to enable post type archives. Default is false.
  965. * * Will generate the proper rewrite rules if rewrite is enabled.
  966. * - rewrite - Triggers the handling of rewrites for this post type. Defaults to true, using $post_type as slug.
  967. * * To prevent rewrite, set to false.
  968. * * To specify rewrite rules, an array can be passed with any of these keys
  969. * * 'slug' => string Customize the permastruct slug. Defaults to $post_type key
  970. * * 'with_front' => bool Should the permastruct be prepended with WP_Rewrite::$front. Defaults to true.
  971. * * 'feeds' => bool Should a feed permastruct be built for this post type. Inherits default from has_archive.
  972. * * 'pages' => bool Should the permastruct provide for pagination. Defaults to true.
  973. * * 'ep_mask' => const Assign an endpoint mask.
  974. * * If not specified and permalink_epmask is set, inherits from permalink_epmask.
  975. * * If not specified and permalink_epmask is not set, defaults to EP_PERMALINK
  976. * - query_var - Sets the query_var key for this post type. Defaults to $post_type key
  977. * * If false, a post type cannot be loaded at ?{query_var}={post_slug}
  978. * * If specified as a string, the query ?{query_var_string}={post_slug} will be valid.
  979. * - can_export - Allows this post type to be exported. Defaults to true.
  980. * - delete_with_user - Whether to delete posts of this type when deleting a user.
  981. * * If true, posts of this type belonging to the user will be moved to trash when then user is deleted.
  982. * * If false, posts of this type belonging to the user will *not* be trashed or deleted.
  983. * * If not set (the default), posts are trashed if post_type_supports('author'). Otherwise posts are not trashed or deleted.
  984. * - _builtin - true if this post type is a native or "built-in" post_type. THIS IS FOR INTERNAL USE ONLY!
  985. * - _edit_link - URL segement to use for edit link of this post type. THIS IS FOR INTERNAL USE ONLY!
  986. *
  987. * @since 2.9.0
  988. * @uses $wp_post_types Inserts new post type object into the list
  989. *
  990. * @param string $post_type Post type key, must not exceed 20 characters
  991. * @param array|string $args See optional args description above.
  992. * @return object|WP_Error the registered post type object, or an error object
  993. */
  994. function register_post_type( $post_type, $args = array() ) {
  995. global $wp_post_types, $wp_rewrite, $wp;
  996. if ( !is_array($wp_post_types) )
  997. $wp_post_types = array();
  998. // Args prefixed with an underscore are reserved for internal use.
  999. $defaults = array(
  1000. 'labels' => array(), 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => null,
  1001. 'capability_type' => 'post', 'capabilities' => array(), 'map_meta_cap' => null,
  1002. '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'hierarchical' => false,
  1003. 'public' => false, 'rewrite' => true, 'has_archive' => false, 'query_var' => true,
  1004. 'supports' => array(), 'register_meta_box_cb' => null,
  1005. 'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null,
  1006. 'can_export' => true,
  1007. 'show_in_nav_menus' => null, 'show_in_menu' => null, 'show_in_admin_bar' => null,
  1008. 'delete_with_user' => null,
  1009. );
  1010. $args = wp_parse_args($args, $defaults);
  1011. $args = (object) $args;
  1012. $post_type = sanitize_key($post_type);
  1013. $args->name = $post_type;
  1014. if ( strlen( $post_type ) > 20 )
  1015. return new WP_Error( 'post_type_too_long', __( 'Post types cannot exceed 20 characters in length' ) );
  1016. // If not set, default to the setting for public.
  1017. if ( null === $args->publicly_queryable )
  1018. $args->publicly_queryable = $args->public;
  1019. // If not set, default to the setting for public.
  1020. if ( null === $args->show_ui )
  1021. $args->show_ui = $args->public;
  1022. // If not set, default to the setting for show_ui.
  1023. if ( null === $args->show_in_menu || ! $args->show_ui )
  1024. $args->show_in_menu = $args->show_ui;
  1025. // If not set, default to the whether the full UI is shown.
  1026. if ( null === $args->show_in_admin_bar )
  1027. $args->show_in_admin_bar = true === $args->show_in_menu;
  1028. // Whether to show this type in nav-menus.php. Defaults to the setting for public.
  1029. if ( null === $args->show_in_nav_menus )
  1030. $args->show_in_nav_menus = $args->public;
  1031. // If not set, default to true if not public, false if public.
  1032. if ( null === $args->exclude_from_search )
  1033. $args->exclude_from_search = !$args->public;
  1034. // Back compat with quirky handling in version 3.0. #14122
  1035. if ( empty( $args->capabilities ) && null === $args->map_meta_cap && in_array( $args->capability_type, array( 'post', 'page' ) ) )
  1036. $args->map_meta_cap = true;
  1037. if ( null === $args->map_meta_cap )
  1038. $args->map_meta_cap = false;
  1039. $args->cap = get_post_type_capabilities( $args );
  1040. unset($args->capabilities);
  1041. if ( is_array( $args->capability_type ) )
  1042. $args->capability_type = $args->capability_type[0];
  1043. if ( ! empty($args->supports) ) {
  1044. add_post_type_support($post_type, $args->supports);
  1045. unset($args->supports);
  1046. } elseif ( false !== $args->supports ) {
  1047. // Add default features
  1048. add_post_type_support($post_type, array('title', 'editor'));
  1049. }
  1050. if ( false !== $args->query_var && !empty($wp) ) {
  1051. if ( true === $args->query_var )
  1052. $args->query_var = $post_type;
  1053. else
  1054. $args->query_var = sanitize_title_with_dashes($args->query_var);
  1055. $wp->add_query_var($args->query_var);
  1056. }
  1057. if ( false !== $args->rewrite && ( is_admin() || '' != get_option('permalink_structure') ) ) {
  1058. if ( ! is_array( $args->rewrite ) )
  1059. $args->rewrite = array();
  1060. if ( empty( $args->rewrite['slug'] ) )
  1061. $args->rewrite['slug'] = $post_type;
  1062. if ( ! isset( $args->rewrite['with_front'] ) )
  1063. $args->rewrite['with_front'] = true;
  1064. if ( ! isset( $args->rewrite['pages'] ) )
  1065. $args->rewrite['pages'] = true;
  1066. if ( ! isset( $args->rewrite['feeds'] ) || ! $args->has_archive )
  1067. $args->rewrite['feeds'] = (bool) $args->has_archive;
  1068. if ( ! isset( $args->rewrite['ep_mask'] ) ) {
  1069. if ( isset( $args->permalink_epmask ) )
  1070. $args->rewrite['ep_mask'] = $args->permalink_epmask;
  1071. else
  1072. $args->rewrite['ep_mask'] = EP_PERMALINK;
  1073. }
  1074. if ( $args->hierarchical )
  1075. add_rewrite_tag("%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
  1076. else
  1077. add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
  1078. if ( $args->has_archive ) {
  1079. $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive;
  1080. if ( $args->rewrite['with_front'] )
  1081. $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
  1082. else
  1083. $archive_slug = $wp_rewrite->root . $archive_slug;
  1084. add_rewrite_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", 'top' );
  1085. if ( $args->rewrite['feeds'] && $wp_rewrite->feeds ) {
  1086. $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
  1087. add_rewrite_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
  1088. add_rewrite_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
  1089. }
  1090. if ( $args->rewrite['pages'] )
  1091. add_rewrite_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' );
  1092. }
  1093. add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite );
  1094. }
  1095. if ( $args->register_meta_box_cb )
  1096. add_action('add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1);
  1097. $args->labels = get_post_type_labels( $args );
  1098. $args->label = $args->labels->name;
  1099. $wp_post_types[$post_type] = $args;
  1100. add_action( 'future_' . $post_type, '_future_post_hook', 5, 2 );
  1101. foreach ( $args->taxonomies as $taxonomy ) {
  1102. register_taxonomy_for_object_type( $taxonomy, $post_type );
  1103. }
  1104. do_action( 'registered_post_type', $post_type, $args );
  1105. return $args;
  1106. }
  1107. /**
  1108. * Builds an object with all post type capabilities out of a post type object
  1109. *
  1110. * Post type capabilities use the 'capability_type' argument as a base, if the
  1111. * capability is not set in the 'capabilities' argument array or if the
  1112. * 'capabilities' argument is not supplied.
  1113. *
  1114. * The capability_type argument can optionally be registered as an array, with
  1115. * the first value being singular and the second plural, e.g. array('story, 'stories')
  1116. * Otherwise, an 's' will be added to the value for the plural form. After
  1117. * registration, capability_type will always be a string of the singular value.
  1118. *
  1119. * By default, seven keys are accepted as part of the capabilities array:
  1120. *
  1121. * - edit_post, read_post, and delete_post are meta capabilities, which are then
  1122. * generally mapped to corresponding primitive capabilities depending on the
  1123. * context, which would be the post being edited/read/deleted and the user or
  1124. * role being checked. Thus these capabilities would generally not be granted
  1125. * directly to users or roles.
  1126. *
  1127. * - edit_posts - Controls whether objects of this post type can be edited.
  1128. * - edit_others_posts - Controls whether objects of this type owned by other users
  1129. * can be edited. If the post type does not support an author, then this will
  1130. * behave like edit_posts.
  1131. * - publish_posts - Controls publishing objects of this post type.
  1132. * - read_private_posts - Controls whether private objects can be read.
  1133. *
  1134. * These four primitive capabilities are checked in core in various locations.
  1135. * There are also seven other primitive capabilities which are not referenced
  1136. * directly in core, except in map_meta_cap(), which takes the three aforementioned
  1137. * meta capabilities and translates them into one or more primitive capabilities
  1138. * that must then be checked against the user or role, depending on the context.
  1139. *
  1140. * - read - Controls whether objects of this post type can be read.
  1141. * - delete_posts - Controls whether objects of this post type can be deleted.
  1142. * - delete_private_posts - Controls whether private objects can be deleted.
  1143. * - delete_published_posts - Controls whether published objects can be deleted.
  1144. * - delete_others_posts - Controls whether objects owned by other users can be
  1145. * can be deleted. If the post type does not support an author, then this will
  1146. * behave like delete_posts.
  1147. * - edit_private_posts - Controls whether private objects can be edited.
  1148. * - edit_published_posts - Controls whether published objects can be edited.
  1149. *
  1150. * These additional capabilities are only used in map_meta_cap(). Thus, they are
  1151. * only assigned by default if the post type is registered with the 'map_meta_cap'
  1152. * argument set to true (default is false).
  1153. *
  1154. * @see map_meta_cap()
  1155. * @since 3.0.0
  1156. *
  1157. * @param object $args Post type registration arguments
  1158. * @return object object with all the capabilities as member variables
  1159. */
  1160. function get_post_type_capabilities( $args ) {
  1161. if ( ! is_array( $args->capability_type ) )
  1162. $args->capability_type = array( $args->capability_type, $args->capability_type . 's' );
  1163. // Singular base for meta capabilities, plural base for primitive capabilities.
  1164. list( $singular_base, $plural_base ) = $args->capability_type;
  1165. $default_capabilities = array(
  1166. // Meta capabilities
  1167. 'edit_post' => 'edit_' . $singular_base,
  1168. 'read_post' => 'read_' . $singular_base,
  1169. 'delete_post' => 'delete_' . $singular_base,
  1170. // Primitive capabilities used outside of map_meta_cap():
  1171. 'edit_posts' => 'edit_' . $plural_base,
  1172. 'edit_others_posts' => 'edit_others_' . $plural_base,
  1173. 'publish_posts' => 'publish_' . $plural_base,
  1174. 'read_private_posts' => 'read_private_' . $plural_base,
  1175. );
  1176. // Primitive capabilities used within map_meta_cap():
  1177. if ( $args->map_meta_cap ) {
  1178. $default_capabilities_for_mapping = array(
  1179. 'read' => 'read',
  1180. 'delete_posts' => 'delete_' . $plural_base,
  1181. 'delete_private_posts' => 'delete_private_' . $plural_base,
  1182. 'delete_published_posts' => 'delete_published_' . $plural_base,
  1183. 'delete_others_posts' => 'delete_others_' . $plural_base,
  1184. 'edit_private_posts' => 'edit_private_' . $plural_base,
  1185. 'edit_published_posts' => 'edit_published_' . $plural_base,
  1186. );
  1187. $default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
  1188. }
  1189. $capabilities = array_merge( $default_capabilities, $args->capabilities );
  1190. // Post creation capability simply maps to edit_posts by default:
  1191. if ( ! isset( $capabilities['create_posts'] ) )
  1192. $capabilities['create_posts'] = $capabilities['edit_posts'];
  1193. // Remember meta capabilities for future reference.
  1194. if ( $args->map_meta_cap )
  1195. _post_type_meta_capabilities( $capabilities );
  1196. return (object) $capabilities;
  1197. }
  1198. /**
  1199. * Stores or returns a list of post type meta caps for map_meta_cap().
  1200. *
  1201. * @since 3.1.0
  1202. * @access private
  1203. */
  1204. function _post_type_meta_capabilities( $capabilities = null ) {
  1205. static $meta_caps = array();
  1206. if ( null === $capabilities )
  1207. return $meta_caps;
  1208. foreach ( $capabilities as $core => $custom ) {
  1209. if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) )
  1210. $meta_caps[ $custom ] = $core;
  1211. }
  1212. }
  1213. /**
  1214. * Builds an object with all post type labels out of a post type object
  1215. *
  1216. * Accepted keys of the label array in the post type object:
  1217. * - name - general name for the post type, usually plural. The same and overridden by $post_type_object->label. Default is Posts/Pages
  1218. * - singular_name - name for one object of this post type. Default is Post/Page
  1219. * - 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>
  1220. * - add_new_item - Default is Add New Post/Add New Page
  1221. * - edit_item - Default is Edit Post/Edit Page
  1222. * - new_item - Default is New Post/New Page
  1223. * - view_item - Default is View Post/View Page
  1224. * - search_items - Default is Search Posts/Search Pages
  1225. * - not_found - Default is No posts found/No pages found
  1226. * - not_found_in_trash - Default is No posts found in Trash/No pages found in Trash
  1227. * - parent_item_colon - This string isn't used on non-hierarchical types. In hierarchical ones the default is Parent Page:
  1228. * - all_items - String for the submenu. Default is All Posts/All Pages
  1229. * - menu_name - Default is the same as <code>name</code>
  1230. *
  1231. * Above, the first default value is for non-hierarchical post types (like posts) and the second one is for hierarchical post types (like pages).
  1232. *
  1233. * @since 3.0.0
  1234. * @param object $post_type_object
  1235. * @return object object with all the labels as member variables
  1236. */
  1237. function get_post_type_labels( $post_type_object ) {
  1238. $nohier_vs_hier_defaults = array(
  1239. 'name' => array( _x('Posts', 'post type general name'), _x('Pages', 'post type general name') ),
  1240. 'singular_name' => array( _x('Post', 'post type singular name'), _x('Page', 'post type singular name') ),
  1241. 'add_new' => array( _x('Add New', 'post'), _x('Add New', 'page') ),
  1242. 'add_new_item' => array( __('Add New Post'), __('Add New Page') ),
  1243. 'edit_item' => array( __('Edit Post'), __('Edit Page') ),
  1244. 'new_item' => array( __('New Post'), __('New Page') ),
  1245. 'view_item' => array( __('View Post'), __('View Page') ),
  1246. 'search_items' => array( __('Search Posts'), __('Search Pages') ),
  1247. 'not_found' => array( __('No posts found.'), __('No pages found.') ),
  1248. 'not_found_in_trash' => array( __('No posts found in Trash.'), __('No pages found in Trash.') ),
  1249. 'parent_item_colon' => array( null, __('Parent Page:') ),
  1250. 'all_items' => array( __( 'All Posts' ), __( 'All Pages' ) )
  1251. );
  1252. $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
  1253. $labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
  1254. $post_type = $post_type_object->name;
  1255. return apply_filters( "post_type_labels_{$post_type}", $labels );
  1256. }
  1257. /**
  1258. * Builds an object with custom-something object (post type, taxonomy) labels out of a custom-something object
  1259. *
  1260. * @access private
  1261. * @since 3.0.0
  1262. */
  1263. function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) {
  1264. if ( isset( $object->label ) && empty( $object->labels['name'] ) )
  1265. $object->labels['name'] = $object->label;
  1266. if ( !isset( $object->labels['singular_name'] ) && isset( $object->labels['name'] ) )
  1267. $object->labels['singular_name'] = $object->labels['name'];
  1268. if ( ! isset( $object->labels['name_admin_bar'] ) )
  1269. $object->labels['name_admin_bar'] = isset( $object->labels['singular_name'] ) ? $obje

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