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

/htdocs/wp-includes/post.php

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

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