PageRenderTime 51ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/post.php

https://bitbucket.org/nlyn/mr.-peacocks
PHP | 5298 lines | 2577 code | 727 blank | 1994 comment | 736 complexity | feff727a2a6da9f6e01c084062d8de0f 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. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ),
  31. ) );
  32. register_post_type( 'page', array(
  33. 'labels' => array(
  34. 'name_admin_bar' => _x( 'Page', 'add new on admin bar' ),
  35. ),
  36. 'public' => true,
  37. 'publicly_queryable' => false,
  38. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  39. '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
  40. 'capability_type' => 'page',
  41. 'map_meta_cap' => true,
  42. 'hierarchical' => true,
  43. 'rewrite' => false,
  44. 'query_var' => false,
  45. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
  46. ) );
  47. register_post_type( 'attachment', array(
  48. 'labels' => array(
  49. 'name' => __( 'Media' ),
  50. ),
  51. 'public' => true,
  52. 'show_ui' => false,
  53. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  54. '_edit_link' => 'media.php?attachment_id=%d', /* internal use only. don't use this when registering your own post type. */
  55. 'capability_type' => 'post',
  56. 'map_meta_cap' => true,
  57. 'hierarchical' => false,
  58. 'rewrite' => false,
  59. 'query_var' => false,
  60. 'show_in_nav_menus' => false,
  61. 'supports' => array( 'comments' ),
  62. ) );
  63. register_post_type( 'revision', array(
  64. 'labels' => array(
  65. 'name' => __( 'Revisions' ),
  66. 'singular_name' => __( 'Revision' ),
  67. ),
  68. 'public' => false,
  69. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  70. '_edit_link' => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */
  71. 'capability_type' => 'post',
  72. 'map_meta_cap' => true,
  73. 'hierarchical' => false,
  74. 'rewrite' => false,
  75. 'query_var' => false,
  76. 'can_export' => false,
  77. ) );
  78. register_post_type( 'nav_menu_item', array(
  79. 'labels' => array(
  80. 'name' => __( 'Navigation Menu Items' ),
  81. 'singular_name' => __( 'Navigation Menu Item' ),
  82. ),
  83. 'public' => false,
  84. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  85. 'hierarchical' => false,
  86. 'rewrite' => false,
  87. 'query_var' => false,
  88. ) );
  89. register_post_status( 'publish', array(
  90. 'label' => _x( 'Published', 'post' ),
  91. 'public' => true,
  92. '_builtin' => true, /* internal use only. */
  93. 'label_count' => _n_noop( 'Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>' ),
  94. ) );
  95. register_post_status( 'future', array(
  96. 'label' => _x( 'Scheduled', 'post' ),
  97. 'protected' => true,
  98. '_builtin' => true, /* internal use only. */
  99. 'label_count' => _n_noop('Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>' ),
  100. ) );
  101. register_post_status( 'draft', array(
  102. 'label' => _x( 'Draft', 'post' ),
  103. 'protected' => true,
  104. '_builtin' => true, /* internal use only. */
  105. 'label_count' => _n_noop( 'Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>' ),
  106. ) );
  107. register_post_status( 'pending', array(
  108. 'label' => _x( 'Pending', 'post' ),
  109. 'protected' => true,
  110. '_builtin' => true, /* internal use only. */
  111. 'label_count' => _n_noop( 'Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>' ),
  112. ) );
  113. register_post_status( 'private', array(
  114. 'label' => _x( 'Private', 'post' ),
  115. 'private' => true,
  116. '_builtin' => true, /* internal use only. */
  117. 'label_count' => _n_noop( 'Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>' ),
  118. ) );
  119. register_post_status( 'trash', array(
  120. 'label' => _x( 'Trash', 'post' ),
  121. 'internal' => true,
  122. '_builtin' => true, /* internal use only. */
  123. 'label_count' => _n_noop( 'Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>' ),
  124. 'show_in_admin_status_list' => true,
  125. ) );
  126. register_post_status( 'auto-draft', array(
  127. 'label' => 'auto-draft',
  128. 'internal' => true,
  129. '_builtin' => true, /* internal use only. */
  130. ) );
  131. register_post_status( 'inherit', array(
  132. 'label' => 'inherit',
  133. 'internal' => true,
  134. '_builtin' => true, /* internal use only. */
  135. 'exclude_from_search' => false,
  136. ) );
  137. }
  138. add_action( 'init', 'create_initial_post_types', 0 ); // highest priority
  139. /**
  140. * Retrieve attached file path based on attachment ID.
  141. *
  142. * You can optionally send it through the 'get_attached_file' filter, but by
  143. * default it will just return the file path unfiltered.
  144. *
  145. * The function works by getting the single post meta name, named
  146. * '_wp_attached_file' and returning it. This is a convenience function to
  147. * prevent looking up the meta name and provide a mechanism for sending the
  148. * attached filename through a filter.
  149. *
  150. * @since 2.0.0
  151. * @uses apply_filters() Calls 'get_attached_file' on file path and attachment ID.
  152. *
  153. * @param int $attachment_id Attachment ID.
  154. * @param bool $unfiltered Whether to apply filters.
  155. * @return string The file path to the attached file.
  156. */
  157. function get_attached_file( $attachment_id, $unfiltered = false ) {
  158. $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
  159. // If the file is relative, prepend upload dir
  160. if ( 0 !== strpos($file, '/') && !preg_match('|^.:\\\|', $file) && ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) )
  161. $file = $uploads['basedir'] . "/$file";
  162. if ( $unfiltered )
  163. return $file;
  164. return apply_filters( 'get_attached_file', $file, $attachment_id );
  165. }
  166. /**
  167. * Update attachment file path based on attachment ID.
  168. *
  169. * Used to update the file path of the attachment, which uses post meta name
  170. * '_wp_attached_file' to store the path of the attachment.
  171. *
  172. * @since 2.1.0
  173. * @uses apply_filters() Calls 'update_attached_file' on file path and attachment ID.
  174. *
  175. * @param int $attachment_id Attachment ID
  176. * @param string $file File path for the attachment
  177. * @return bool False on failure, true on success.
  178. */
  179. function update_attached_file( $attachment_id, $file ) {
  180. if ( !get_post( $attachment_id ) )
  181. return false;
  182. $file = apply_filters( 'update_attached_file', $file, $attachment_id );
  183. $file = _wp_relative_upload_path($file);
  184. return update_post_meta( $attachment_id, '_wp_attached_file', $file );
  185. }
  186. /**
  187. * Return relative path to an uploaded file.
  188. *
  189. * The path is relative to the current upload dir.
  190. *
  191. * @since 2.9.0
  192. * @uses apply_filters() Calls '_wp_relative_upload_path' on file path.
  193. *
  194. * @param string $path Full path to the file
  195. * @return string relative path on success, unchanged path on failure.
  196. */
  197. function _wp_relative_upload_path( $path ) {
  198. $new_path = $path;
  199. if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) {
  200. if ( 0 === strpos($new_path, $uploads['basedir']) ) {
  201. $new_path = str_replace($uploads['basedir'], '', $new_path);
  202. $new_path = ltrim($new_path, '/');
  203. }
  204. }
  205. return apply_filters( '_wp_relative_upload_path', $new_path, $path );
  206. }
  207. /**
  208. * Retrieve all children of the post parent ID.
  209. *
  210. * Normally, without any enhancements, the children would apply to pages. In the
  211. * context of the inner workings of WordPress, pages, posts, and attachments
  212. * share the same table, so therefore the functionality could apply to any one
  213. * of them. It is then noted that while this function does not work on posts, it
  214. * does not mean that it won't work on posts. It is recommended that you know
  215. * what context you wish to retrieve the children of.
  216. *
  217. * Attachments may also be made the child of a post, so if that is an accurate
  218. * statement (which needs to be verified), it would then be possible to get
  219. * all of the attachments for a post. Attachments have since changed since
  220. * version 2.5, so this is most likely unaccurate, but serves generally as an
  221. * example of what is possible.
  222. *
  223. * The arguments listed as defaults are for this function and also of the
  224. * {@link get_posts()} function. The arguments are combined with the
  225. * get_children defaults and are then passed to the {@link get_posts()}
  226. * function, which accepts additional arguments. You can replace the defaults in
  227. * this function, listed below and the additional arguments listed in the
  228. * {@link get_posts()} function.
  229. *
  230. * The 'post_parent' is the most important argument and important attention
  231. * needs to be paid to the $args parameter. If you pass either an object or an
  232. * integer (number), then just the 'post_parent' is grabbed and everything else
  233. * is lost. If you don't specify any arguments, then it is assumed that you are
  234. * in The Loop and the post parent will be grabbed for from the current post.
  235. *
  236. * The 'post_parent' argument is the ID to get the children. The 'numberposts'
  237. * is the amount of posts to retrieve that has a default of '-1', which is
  238. * used to get all of the posts. Giving a number higher than 0 will only
  239. * retrieve that amount of posts.
  240. *
  241. * The 'post_type' and 'post_status' arguments can be used to choose what
  242. * criteria of posts to retrieve. The 'post_type' can be anything, but WordPress
  243. * post types are 'post', 'pages', and 'attachments'. The 'post_status'
  244. * argument will accept any post status within the write administration panels.
  245. *
  246. * @see get_posts() Has additional arguments that can be replaced.
  247. * @internal Claims made in the long description might be inaccurate.
  248. *
  249. * @since 2.0.0
  250. *
  251. * @param mixed $args Optional. User defined arguments for replacing the defaults.
  252. * @param string $output Optional. Constant for return type, either OBJECT (default), ARRAY_A, ARRAY_N.
  253. * @return array|bool False on failure and the type will be determined by $output parameter.
  254. */
  255. function get_children($args = '', $output = OBJECT) {
  256. $kids = array();
  257. if ( empty( $args ) ) {
  258. if ( isset( $GLOBALS['post'] ) ) {
  259. $args = array('post_parent' => (int) $GLOBALS['post']->post_parent );
  260. } else {
  261. return $kids;
  262. }
  263. } elseif ( is_object( $args ) ) {
  264. $args = array('post_parent' => (int) $args->post_parent );
  265. } elseif ( is_numeric( $args ) ) {
  266. $args = array('post_parent' => (int) $args);
  267. }
  268. $defaults = array(
  269. 'numberposts' => -1, 'post_type' => 'any',
  270. 'post_status' => 'any', 'post_parent' => 0,
  271. );
  272. $r = wp_parse_args( $args, $defaults );
  273. $children = get_posts( $r );
  274. if ( !$children )
  275. return $kids;
  276. update_post_cache($children);
  277. foreach ( $children as $key => $child )
  278. $kids[$child->ID] = $children[$key];
  279. if ( $output == OBJECT ) {
  280. return $kids;
  281. } elseif ( $output == ARRAY_A ) {
  282. foreach ( (array) $kids as $kid )
  283. $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);
  284. return $weeuns;
  285. } elseif ( $output == ARRAY_N ) {
  286. foreach ( (array) $kids as $kid )
  287. $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));
  288. return $babes;
  289. } else {
  290. return $kids;
  291. }
  292. }
  293. /**
  294. * Get extended entry info (<!--more-->).
  295. *
  296. * There should not be any space after the second dash and before the word
  297. * 'more'. There can be text or space(s) after the word 'more', but won't be
  298. * referenced.
  299. *
  300. * The returned array has 'main' and 'extended' keys. Main has the text before
  301. * the <code><!--more--></code>. The 'extended' key has the content after the
  302. * <code><!--more--></code> comment.
  303. *
  304. * @since 1.0.0
  305. *
  306. * @param string $post Post content.
  307. * @return array Post before ('main') and after ('extended').
  308. */
  309. function get_extended($post) {
  310. //Match the new style more links
  311. if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) {
  312. list($main, $extended) = explode($matches[0], $post, 2);
  313. } else {
  314. $main = $post;
  315. $extended = '';
  316. }
  317. // Strip leading and trailing whitespace
  318. $main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main);
  319. $extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended);
  320. return array('main' => $main, 'extended' => $extended);
  321. }
  322. /**
  323. * Retrieves post data given a post ID or post object.
  324. *
  325. * See {@link sanitize_post()} for optional $filter values. Also, the parameter
  326. * $post, must be given as a variable, since it is passed by reference.
  327. *
  328. * @since 1.5.1
  329. * @uses $wpdb
  330. * @link http://codex.wordpress.org/Function_Reference/get_post
  331. *
  332. * @param int|object $post Post ID or post object.
  333. * @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
  334. * @param string $filter Optional, default is raw.
  335. * @return mixed Post data
  336. */
  337. function &get_post(&$post, $output = OBJECT, $filter = 'raw') {
  338. global $wpdb;
  339. $null = null;
  340. if ( empty($post) ) {
  341. if ( isset($GLOBALS['post']) )
  342. $_post = & $GLOBALS['post'];
  343. else
  344. return $null;
  345. } elseif ( is_object($post) && empty($post->filter) ) {
  346. _get_post_ancestors($post);
  347. $_post = sanitize_post($post, 'raw');
  348. wp_cache_add($post->ID, $_post, 'posts');
  349. } else {
  350. if ( is_object($post) )
  351. $post_id = $post->ID;
  352. else
  353. $post_id = $post;
  354. $post_id = (int) $post_id;
  355. if ( ! $_post = wp_cache_get($post_id, 'posts') ) {
  356. $_post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id));
  357. if ( ! $_post )
  358. return $null;
  359. _get_post_ancestors($_post);
  360. $_post = sanitize_post($_post, 'raw');
  361. wp_cache_add($_post->ID, $_post, 'posts');
  362. }
  363. }
  364. if ($filter != 'raw')
  365. $_post = sanitize_post($_post, $filter);
  366. if ( $output == OBJECT ) {
  367. return $_post;
  368. } elseif ( $output == ARRAY_A ) {
  369. $__post = get_object_vars($_post);
  370. return $__post;
  371. } elseif ( $output == ARRAY_N ) {
  372. $__post = array_values(get_object_vars($_post));
  373. return $__post;
  374. } else {
  375. return $_post;
  376. }
  377. }
  378. /**
  379. * Retrieve ancestors of a post.
  380. *
  381. * @since 2.5.0
  382. *
  383. * @param int|object $post Post ID or post object
  384. * @return array Ancestor IDs or empty array if none are found.
  385. */
  386. function get_post_ancestors($post) {
  387. $post = get_post($post);
  388. if ( !empty($post->ancestors) )
  389. return $post->ancestors;
  390. return array();
  391. }
  392. /**
  393. * Retrieve data from a post field based on Post ID.
  394. *
  395. * Examples of the post field will be, 'post_type', 'post_status', 'content',
  396. * etc and based off of the post object property or key names.
  397. *
  398. * The context values are based off of the taxonomy filter functions and
  399. * supported values are found within those functions.
  400. *
  401. * @since 2.3.0
  402. * @uses sanitize_post_field() See for possible $context values.
  403. *
  404. * @param string $field Post field name
  405. * @param id $post Post ID
  406. * @param string $context Optional. How to filter the field. Default is display.
  407. * @return WP_Error|string Value in post field or WP_Error on failure
  408. */
  409. function get_post_field( $field, $post, $context = 'display' ) {
  410. $post = (int) $post;
  411. $post = get_post( $post );
  412. if ( is_wp_error($post) )
  413. return $post;
  414. if ( !is_object($post) )
  415. return '';
  416. if ( !isset($post->$field) )
  417. return '';
  418. return sanitize_post_field($field, $post->$field, $post->ID, $context);
  419. }
  420. /**
  421. * Retrieve the mime type of an attachment based on the ID.
  422. *
  423. * This function can be used with any post type, but it makes more sense with
  424. * attachments.
  425. *
  426. * @since 2.0.0
  427. *
  428. * @param int $ID Optional. Post ID.
  429. * @return bool|string False on failure or returns the mime type
  430. */
  431. function get_post_mime_type($ID = '') {
  432. $post = & get_post($ID);
  433. if ( is_object($post) )
  434. return $post->post_mime_type;
  435. return false;
  436. }
  437. /**
  438. * Retrieve the format slug for a post
  439. *
  440. * @since 3.1.0
  441. *
  442. * @param int|object $post A post
  443. *
  444. * @return mixed The format if successful. False if no format is set. WP_Error if errors.
  445. */
  446. function get_post_format( $post = null ) {
  447. $post = get_post($post);
  448. if ( ! post_type_supports( $post->post_type, 'post-formats' ) )
  449. return false;
  450. $_format = get_the_terms( $post->ID, 'post_format' );
  451. if ( empty( $_format ) )
  452. return false;
  453. $format = array_shift( $_format );
  454. return ( str_replace('post-format-', '', $format->slug ) );
  455. }
  456. /**
  457. * Check if a post has a particular format
  458. *
  459. * @since 3.1.0
  460. * @uses has_term()
  461. *
  462. * @param string $format The format to check for
  463. * @param object|id $post The post to check. If not supplied, defaults to the current post if used in the loop.
  464. * @return bool True if the post has the format, false otherwise.
  465. */
  466. function has_post_format( $format, $post = null ) {
  467. return has_term('post-format-' . sanitize_key($format), 'post_format', $post);
  468. }
  469. /**
  470. * Assign a format to a post
  471. *
  472. * @since 3.1.0
  473. *
  474. * @param int|object $post The post for which to assign a format
  475. * @param string $format A format to assign. Use an empty string or array to remove all formats from the post.
  476. * @return mixed WP_Error on error. Array of affected term IDs on success.
  477. */
  478. function set_post_format( $post, $format ) {
  479. $post = get_post($post);
  480. if ( empty($post) )
  481. return new WP_Error('invalid_post', __('Invalid post'));
  482. if ( !empty($format) ) {
  483. $format = sanitize_key($format);
  484. if ( 'standard' == $format || !in_array( $format, array_keys( get_post_format_slugs() ) ) )
  485. $format = '';
  486. else
  487. $format = 'post-format-' . $format;
  488. }
  489. return wp_set_post_terms($post->ID, $format, 'post_format');
  490. }
  491. /**
  492. * Retrieve the post status based on the Post ID.
  493. *
  494. * If the post ID is of an attachment, then the parent post status will be given
  495. * instead.
  496. *
  497. * @since 2.0.0
  498. *
  499. * @param int $ID Post ID
  500. * @return string|bool Post status or false on failure.
  501. */
  502. function get_post_status($ID = '') {
  503. $post = get_post($ID);
  504. if ( !is_object($post) )
  505. return false;
  506. if ( 'attachment' == $post->post_type ) {
  507. if ( 'private' == $post->post_status )
  508. return 'private';
  509. // Unattached attachments are assumed to be published
  510. if ( ( 'inherit' == $post->post_status ) && ( 0 == $post->post_parent) )
  511. return 'publish';
  512. // Inherit status from the parent
  513. if ( $post->post_parent && ( $post->ID != $post->post_parent ) )
  514. return get_post_status($post->post_parent);
  515. }
  516. return $post->post_status;
  517. }
  518. /**
  519. * Retrieve all of the WordPress supported post statuses.
  520. *
  521. * Posts have a limited set of valid status values, this provides the
  522. * post_status values and descriptions.
  523. *
  524. * @since 2.5.0
  525. *
  526. * @return array List of post statuses.
  527. */
  528. function get_post_statuses( ) {
  529. $status = array(
  530. 'draft' => __('Draft'),
  531. 'pending' => __('Pending Review'),
  532. 'private' => __('Private'),
  533. 'publish' => __('Published')
  534. );
  535. return $status;
  536. }
  537. /**
  538. * Retrieve all of the WordPress support page statuses.
  539. *
  540. * Pages have a limited set of valid status values, this provides the
  541. * post_status values and descriptions.
  542. *
  543. * @since 2.5.0
  544. *
  545. * @return array List of page statuses.
  546. */
  547. function get_page_statuses( ) {
  548. $status = array(
  549. 'draft' => __('Draft'),
  550. 'private' => __('Private'),
  551. 'publish' => __('Published')
  552. );
  553. return $status;
  554. }
  555. /**
  556. * Register a post type. Do not use before init.
  557. *
  558. * A simple function for creating or modifying a post status based on the
  559. * parameters given. The function will accept an array (second optional
  560. * parameter), along with a string for the post status name.
  561. *
  562. *
  563. * Optional $args contents:
  564. *
  565. * label - A descriptive name for the post status marked for translation. Defaults to $post_status.
  566. * public - Whether posts of this status should be shown in the front end of the site. Defaults to true.
  567. * exclude_from_search - Whether to exclude posts with this post status from search results. Defaults to false.
  568. * show_in_admin_all_list - Whether to include posts in the edit listing for their post type
  569. * show_in_admin_status_list - Show in the list of statuses with post counts at the top of the edit
  570. * listings, e.g. All (12) | Published (9) | My Custom Status (2) ...
  571. *
  572. * Arguments prefixed with an _underscore shouldn't be used by plugins and themes.
  573. *
  574. * @package WordPress
  575. * @subpackage Post
  576. * @since 3.0.0
  577. * @uses $wp_post_statuses Inserts new post status object into the list
  578. *
  579. * @param string $post_status Name of the post status.
  580. * @param array|string $args See above description.
  581. */
  582. function register_post_status($post_status, $args = array()) {
  583. global $wp_post_statuses;
  584. if (!is_array($wp_post_statuses))
  585. $wp_post_statuses = array();
  586. // Args prefixed with an underscore are reserved for internal use.
  587. $defaults = array('label' => false, 'label_count' => false, 'exclude_from_search' => null, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'public' => null, 'internal' => null, 'protected' => null, 'private' => null, 'show_in_admin_all' => null, 'publicly_queryable' => null, 'show_in_admin_status_list' => null, 'show_in_admin_all_list' => null, 'single_view_cap' => null);
  588. $args = wp_parse_args($args, $defaults);
  589. $args = (object) $args;
  590. $post_status = sanitize_key($post_status);
  591. $args->name = $post_status;
  592. if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private )
  593. $args->internal = true;
  594. if ( null === $args->public )
  595. $args->public = false;
  596. if ( null === $args->private )
  597. $args->private = false;
  598. if ( null === $args->protected )
  599. $args->protected = false;
  600. if ( null === $args->internal )
  601. $args->internal = false;
  602. if ( null === $args->publicly_queryable )
  603. $args->publicly_queryable = $args->public;
  604. if ( null === $args->exclude_from_search )
  605. $args->exclude_from_search = $args->internal;
  606. if ( null === $args->show_in_admin_all_list )
  607. $args->show_in_admin_all_list = !$args->internal;
  608. if ( null === $args->show_in_admin_status_list )
  609. $args->show_in_admin_status_list = !$args->internal;
  610. if ( null === $args->single_view_cap )
  611. $args->single_view_cap = $args->public ? '' : 'edit';
  612. if ( false === $args->label )
  613. $args->label = $post_status;
  614. if ( false === $args->label_count )
  615. $args->label_count = array( $args->label, $args->label );
  616. $wp_post_statuses[$post_status] = $args;
  617. return $args;
  618. }
  619. /**
  620. * Retrieve a post status object by name
  621. *
  622. * @package WordPress
  623. * @subpackage Post
  624. * @since 3.0.0
  625. * @uses $wp_post_statuses
  626. * @see register_post_status
  627. * @see get_post_statuses
  628. *
  629. * @param string $post_status The name of a registered post status
  630. * @return object A post status object
  631. */
  632. function get_post_status_object( $post_status ) {
  633. global $wp_post_statuses;
  634. if ( empty($wp_post_statuses[$post_status]) )
  635. return null;
  636. return $wp_post_statuses[$post_status];
  637. }
  638. /**
  639. * Get a list of all registered post status objects.
  640. *
  641. * @package WordPress
  642. * @subpackage Post
  643. * @since 3.0.0
  644. * @uses $wp_post_statuses
  645. * @see register_post_status
  646. * @see get_post_status_object
  647. *
  648. * @param array|string $args An array of key => value arguments to match against the post status objects.
  649. * @param string $output The type of output to return, either post status 'names' or 'objects'. 'names' is the default.
  650. * @param string $operator The logical operation to perform. 'or' means only one element
  651. * from the array needs to match; 'and' means all elements must match. The default is 'and'.
  652. * @return array A list of post type names or objects
  653. */
  654. function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) {
  655. global $wp_post_statuses;
  656. $field = ('names' == $output) ? 'name' : false;
  657. return wp_filter_object_list($wp_post_statuses, $args, $operator, $field);
  658. }
  659. /**
  660. * Whether the post type is hierarchical.
  661. *
  662. * A false return value might also mean that the post type does not exist.
  663. *
  664. * @since 3.0.0
  665. * @see get_post_type_object
  666. *
  667. * @param string $post_type Post type name
  668. * @return bool Whether post type is hierarchical.
  669. */
  670. function is_post_type_hierarchical( $post_type ) {
  671. if ( ! post_type_exists( $post_type ) )
  672. return false;
  673. $post_type = get_post_type_object( $post_type );
  674. return $post_type->hierarchical;
  675. }
  676. /**
  677. * Checks if a post type is registered.
  678. *
  679. * @since 3.0.0
  680. * @uses get_post_type_object()
  681. *
  682. * @param string $post_type Post type name
  683. * @return bool Whether post type is registered.
  684. */
  685. function post_type_exists( $post_type ) {
  686. return (bool) get_post_type_object( $post_type );
  687. }
  688. /**
  689. * Retrieve the post type of the current post or of a given post.
  690. *
  691. * @since 2.1.0
  692. *
  693. * @uses $post The Loop current post global
  694. *
  695. * @param mixed $the_post Optional. Post object or post ID.
  696. * @return bool|string post type or false on failure.
  697. */
  698. function get_post_type( $the_post = false ) {
  699. global $post;
  700. if ( false === $the_post )
  701. $the_post = $post;
  702. elseif ( is_numeric($the_post) )
  703. $the_post = get_post($the_post);
  704. if ( is_object($the_post) )
  705. return $the_post->post_type;
  706. return false;
  707. }
  708. /**
  709. * Retrieve a post type object by name
  710. *
  711. * @package WordPress
  712. * @subpackage Post
  713. * @since 3.0.0
  714. * @uses $wp_post_types
  715. * @see register_post_type
  716. * @see get_post_types
  717. *
  718. * @param string $post_type The name of a registered post type
  719. * @return object A post type object
  720. */
  721. function get_post_type_object( $post_type ) {
  722. global $wp_post_types;
  723. if ( empty($wp_post_types[$post_type]) )
  724. return null;
  725. return $wp_post_types[$post_type];
  726. }
  727. /**
  728. * Get a list of all registered post type objects.
  729. *
  730. * @package WordPress
  731. * @subpackage Post
  732. * @since 2.9.0
  733. * @uses $wp_post_types
  734. * @see register_post_type
  735. *
  736. * @param array|string $args An array of key => value arguments to match against the post type objects.
  737. * @param string $output The type of output to return, either post type 'names' or 'objects'. 'names' is the default.
  738. * @param string $operator The logical operation to perform. 'or' means only one element
  739. * from the array needs to match; 'and' means all elements must match. The default is 'and'.
  740. * @return array A list of post type names or objects
  741. */
  742. function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) {
  743. global $wp_post_types;
  744. $field = ('names' == $output) ? 'name' : false;
  745. return wp_filter_object_list($wp_post_types, $args, $operator, $field);
  746. }
  747. /**
  748. * Register a post type. Do not use before init.
  749. *
  750. * A function for creating or modifying a post type based on the
  751. * parameters given. The function will accept an array (second optional
  752. * parameter), along with a string for the post type name.
  753. *
  754. * Optional $args contents:
  755. *
  756. * - label - Name of the post type shown in the menu. Usually plural. If not set, labels['name'] will be used.
  757. * - description - A short descriptive summary of what the post type is. Defaults to blank.
  758. * - public - Whether posts of this type should be shown in the admin UI. Defaults to false.
  759. * - exclude_from_search - Whether to exclude posts with this post type from search results.
  760. * Defaults to true if the type is not public, false if the type is public.
  761. * - publicly_queryable - Whether post_type queries can be performed from the front page.
  762. * Defaults to whatever public is set as.
  763. * - show_ui - Whether to generate a default UI for managing this post type. Defaults to true
  764. * if the type is public, false if the type is not public.
  765. * - show_in_menu - Where to show the post type in the admin menu. True for a top level menu,
  766. * false for no menu, or can be a top level page like 'tools.php' or 'edit.php?post_type=page'.
  767. * show_ui must be true.
  768. * - menu_position - The position in the menu order the post type should appear. Defaults to the bottom.
  769. * - menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon.
  770. * - capability_type - The string to use to build the read, edit, and delete capabilities. Defaults to 'post'.
  771. * May be passed as an array to allow for alternative plurals when using this argument as a base to construct the
  772. * capabilities, e.g. array('story', 'stories').
  773. * - capabilities - Array of capabilities for this post type. By default the capability_type is used
  774. * as a base to construct capabilities. You can see accepted values in {@link get_post_type_capabilities()}.
  775. * - map_meta_cap - Whether to use the internal default meta capability handling. Defaults to false.
  776. * - hierarchical - Whether the post type is hierarchical. Defaults to false.
  777. * - supports - An alias for calling add_post_type_support() directly. See {@link add_post_type_support()}
  778. * for documentation. Defaults to none.
  779. * - register_meta_box_cb - Provide a callback function that will be called when setting up the
  780. * meta boxes for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback.
  781. * - taxonomies - An array of taxonomy identifiers that will be registered for the post type.
  782. * Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or
  783. * register_taxonomy_for_object_type().
  784. * - labels - An array of labels for this post type. By default post labels are used for non-hierarchical
  785. * types and page labels for hierarchical ones. You can see accepted values in {@link get_post_type_labels()}.
  786. * - permalink_epmask - The default rewrite endpoint bitmasks.
  787. * - has_archive - True to enable post type archives. Will generate the proper rewrite rules if rewrite is enabled.
  788. * - rewrite - false to prevent rewrite. Defaults to true. Use array('slug'=>$slug) to customize permastruct;
  789. * default will use $post_type as slug. Other options include 'with_front', 'feeds', and 'pages'.
  790. * - query_var - false to prevent queries, or string to value of the query var to use for this post type
  791. * - can_export - true allows this post type to be exported.
  792. * - show_in_nav_menus - true makes this post type available for selection in navigation menus.
  793. * - _builtin - true if this post type is a native or "built-in" post_type. THIS IS FOR INTERNAL USE ONLY!
  794. * - _edit_link - URL segement to use for edit link of this post type. THIS IS FOR INTERNAL USE ONLY!
  795. *
  796. * @since 2.9.0
  797. * @uses $wp_post_types Inserts new post type object into the list
  798. *
  799. * @param string $post_type Name of the post type.
  800. * @param array|string $args See above description.
  801. * @return object|WP_Error the registered post type object, or an error object
  802. */
  803. function register_post_type($post_type, $args = array()) {
  804. global $wp_post_types, $wp_rewrite, $wp;
  805. if ( !is_array($wp_post_types) )
  806. $wp_post_types = array();
  807. // Args prefixed with an underscore are reserved for internal use.
  808. $defaults = array(
  809. 'labels' => array(), 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => null,
  810. 'capability_type' => 'post', 'capabilities' => array(), 'map_meta_cap' => null,
  811. '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'hierarchical' => false,
  812. 'public' => false, 'rewrite' => true, 'has_archive' => false, 'query_var' => true,
  813. 'supports' => array(), 'register_meta_box_cb' => null,
  814. 'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null,
  815. 'permalink_epmask' => EP_PERMALINK, 'can_export' => true,
  816. 'show_in_nav_menus' => null, 'show_in_menu' => null, 'show_in_admin_bar' => null,
  817. );
  818. $args = wp_parse_args($args, $defaults);
  819. $args = (object) $args;
  820. $post_type = sanitize_key($post_type);
  821. $args->name = $post_type;
  822. if ( strlen( $post_type ) > 20 )
  823. return new WP_Error( 'post_type_too_long', __( 'Post types cannot exceed 20 characters in length' ) );
  824. // If not set, default to the setting for public.
  825. if ( null === $args->publicly_queryable )
  826. $args->publicly_queryable = $args->public;
  827. // If not set, default to the setting for public.
  828. if ( null === $args->show_ui )
  829. $args->show_ui = $args->public;
  830. // If not set, default to the setting for show_ui.
  831. if ( null === $args->show_in_menu || ! $args->show_ui )
  832. $args->show_in_menu = $args->show_ui;
  833. // If not set, default to the whether the full UI is shown.
  834. if ( null === $args->show_in_admin_bar )
  835. $args->show_in_admin_bar = true === $args->show_in_menu;
  836. // Whether to show this type in nav-menus.php. Defaults to the setting for public.
  837. if ( null === $args->show_in_nav_menus )
  838. $args->show_in_nav_menus = $args->public;
  839. // If not set, default to true if not public, false if public.
  840. if ( null === $args->exclude_from_search )
  841. $args->exclude_from_search = !$args->public;
  842. // Back compat with quirky handling in version 3.0. #14122
  843. if ( empty( $args->capabilities ) && null === $args->map_meta_cap && in_array( $args->capability_type, array( 'post', 'page' ) ) )
  844. $args->map_meta_cap = true;
  845. if ( null === $args->map_meta_cap )
  846. $args->map_meta_cap = false;
  847. $args->cap = get_post_type_capabilities( $args );
  848. unset($args->capabilities);
  849. if ( is_array( $args->capability_type ) )
  850. $args->capability_type = $args->capability_type[0];
  851. if ( ! empty($args->supports) ) {
  852. add_post_type_support($post_type, $args->supports);
  853. unset($args->supports);
  854. } else {
  855. // Add default features
  856. add_post_type_support($post_type, array('title', 'editor'));
  857. }
  858. if ( false !== $args->query_var && !empty($wp) ) {
  859. if ( true === $args->query_var )
  860. $args->query_var = $post_type;
  861. $args->query_var = sanitize_title_with_dashes($args->query_var);
  862. $wp->add_query_var($args->query_var);
  863. }
  864. if ( false !== $args->rewrite && '' != get_option('permalink_structure') ) {
  865. if ( ! is_array( $args->rewrite ) )
  866. $args->rewrite = array();
  867. if ( empty( $args->rewrite['slug'] ) )
  868. $args->rewrite['slug'] = $post_type;
  869. if ( ! isset( $args->rewrite['with_front'] ) )
  870. $args->rewrite['with_front'] = true;
  871. if ( ! isset( $args->rewrite['pages'] ) )
  872. $args->rewrite['pages'] = true;
  873. if ( ! isset( $args->rewrite['feeds'] ) || ! $args->has_archive )
  874. $args->rewrite['feeds'] = (bool) $args->has_archive;
  875. if ( $args->hierarchical )
  876. $wp_rewrite->add_rewrite_tag("%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
  877. else
  878. $wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
  879. if ( $args->has_archive ) {
  880. $archive_slug = $args->has_archive === true ? $args->rewrite['slug'] : $args->has_archive;
  881. if ( $args->rewrite['with_front'] )
  882. $archive_slug = substr( $wp_rewrite->front, 1 ) . $archive_slug;
  883. else
  884. $archive_slug = $wp_rewrite->root . $archive_slug;
  885. $wp_rewrite->add_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", 'top' );
  886. if ( $args->rewrite['feeds'] && $wp_rewrite->feeds ) {
  887. $feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
  888. $wp_rewrite->add_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
  889. $wp_rewrite->add_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
  890. }
  891. if ( $args->rewrite['pages'] )
  892. $wp_rewrite->add_rule( "{$archive_slug}/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' );
  893. }
  894. $wp_rewrite->add_permastruct($post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite['with_front'], $args->permalink_epmask);
  895. }
  896. if ( $args->register_meta_box_cb )
  897. add_action('add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1);
  898. $args->labels = get_post_type_labels( $args );
  899. $args->label = $args->labels->name;
  900. $wp_post_types[$post_type] = $args;
  901. add_action( 'future_' . $post_type, '_future_post_hook', 5, 2 );
  902. foreach ( $args->taxonomies as $taxonomy ) {
  903. register_taxonomy_for_object_type( $taxonomy, $post_type );
  904. }
  905. return $args;
  906. }
  907. /**
  908. * Builds an object with all post type capabilities out of a post type object
  909. *
  910. * Post type capabilities use the 'capability_type' argument as a base, if the
  911. * capability is not set in the 'capabilities' argument array or if the
  912. * 'capabilities' argument is not supplied.
  913. *
  914. * The capability_type argument can optionally be registered as an array, with
  915. * the first value being singular and the second plural, e.g. array('story, 'stories')
  916. * Otherwise, an 's' will be added to the value for the plural form. After
  917. * registration, capability_type will always be a string of the singular value.
  918. *
  919. * By default, seven keys are accepted as part of the capabilities array:
  920. *
  921. * - edit_post, read_post, and delete_post are meta capabilities, which are then
  922. * generally mapped to corresponding primitive capabilities depending on the
  923. * context, which would be the post being edited/read/deleted and the user or
  924. * role being checked. Thus these capabilities would generally not be granted
  925. * directly to users or roles.
  926. *
  927. * - edit_posts - Controls whether objects of this post type can be edited.
  928. * - edit_others_posts - Controls whether objects of this type owned by other users
  929. * can be edited. If the post type does not support an author, then this will
  930. * behave like edit_posts.
  931. * - publish_posts - Controls publishing objects of this post type.
  932. * - read_private_posts - Controls whether private objects can be read.
  933. * These four primitive capabilities are checked in core in various locations.
  934. * There are also seven other primitive capabilities which are not referenced
  935. * directly in core, except in map_meta_cap(), which takes the three aforementioned
  936. * meta capabilities and translates them into one or more primitive capabilities
  937. * that must then be checked against the user or role, depending on the context.
  938. *
  939. * - read - Controls whether objects of this post type can be read.
  940. * - delete_posts - Controls whether objects of this post type can be deleted.
  941. * - delete_private_posts - Controls whether private objects can be deleted.
  942. * - delete_published_posts - Controls whether published objects can be deleted.
  943. * - delete_others_posts - Controls whether objects owned by other users can be
  944. * can be deleted. If the post type does not support an author, then this will
  945. * behave like delete_posts.
  946. * - edit_private_posts - Controls whether private objects can be edited.
  947. * - edit_published_posts - Controls whether published objects can be edited.
  948. *
  949. * These additional capabilities are only used in map_meta_cap(). Thus, they are
  950. * only assigned by default if the post type is registered with the 'map_meta_cap'
  951. * argument set to true (default is false).
  952. *
  953. * @see map_meta_cap()
  954. * @since 3.0.0
  955. *
  956. * @param object $args Post type registration arguments
  957. * @return object object with all the capabilities as member variables
  958. */
  959. function get_post_type_capabilities( $args ) {
  960. if ( ! is_array( $args->capability_type ) )
  961. $args->capability_type = array( $args->capability_type, $args->capability_type . 's' );
  962. // Singular base for meta capabilities, plural base for primitive capabilities.
  963. list( $singular_base, $plural_base ) = $args->capability_type;
  964. $default_capabilities = array(
  965. // Meta capabilities
  966. 'edit_post' => 'edit_' . $singular_base,
  967. 'read_post' => 'read_' . $singular_base,
  968. 'delete_post' => 'delete_' . $singular_base,
  969. // Primitive capabilities used outside of map_meta_cap():
  970. 'edit_posts' => 'edit_' . $plural_base,
  971. 'edit_others_posts' => 'edit_others_' . $plural_base,
  972. 'publish_posts' => 'publish_' . $plural_base,
  973. 'read_private_posts' => 'read_private_' . $plural_base,
  974. );
  975. // Primitive capabilities used within map_meta_cap():
  976. if ( $args->map_meta_cap ) {
  977. $default_capabilities_for_mapping = array(
  978. 'read' => 'read',
  979. 'delete_posts' => 'delete_' . $plural_base,
  980. 'delete_private_posts' => 'delete_private_' . $plural_base,
  981. 'delete_published_posts' => 'delete_published_' . $plural_base,
  982. 'delete_others_posts' => 'delete_others_' . $plural_base,
  983. 'edit_private_posts' => 'edit_private_' . $plural_base,
  984. 'edit_published_posts' => 'edit_published_' . $plural_base,
  985. );
  986. $default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
  987. }
  988. $capabilities = array_merge( $default_capabilities, $args->capabilities );
  989. // Remember meta capabilities for future reference.
  990. if ( $args->map_meta_cap )
  991. _post_type_meta_capabilities( $capabilities );
  992. return (object) $capabilities;
  993. }
  994. /**
  995. * Stores or returns a list of post type meta caps for map_meta_cap().
  996. *
  997. * @since 3.1.0
  998. * @access private
  999. */
  1000. function _post_type_meta_capabilities( $capabilities = null ) {
  1001. static $meta_caps = array();
  1002. if ( null === $capabilities )
  1003. return $meta_caps;
  1004. foreach ( $capabilities as $core => $custom ) {
  1005. if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) )
  1006. $meta_caps[ $custom ] = $core;
  1007. }
  1008. }
  1009. /**
  1010. * Builds an object with all post type labels out of a post type object
  1011. *
  1012. * Accepted keys of the label array in the post type object:
  1013. * - name - general name for the post type, usually plural. The same and overriden by $post_type_object->label. Default is Posts/Pages
  1014. * - singular_name - name for one object of this post type. Default is Post/Page
  1015. * - 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>
  1016. * - add_new_item - Default is Add New Post/Add New Page
  1017. * - edit_item - Default is Edit Post/Edit Page
  1018. * - new_item - Default is New Post/New Page
  1019. * - view_item - Default is View Post/View Page
  1020. * - search_items - Default is Search Posts/Search Pages
  1021. * - not_found - Default is No posts found/No pages found
  1022. * - not_found_in_trash - Default is No posts found in Trash/No pages found in Trash
  1023. * - parent_item_colon - This string isn't used on non-hierarchical types. In hierarchical ones the default is Parent Page:
  1024. *
  1025. * Above, the first default value is for non-hierarchical post types (like posts) and the second one is for hierarchical post types (like pages).
  1026. *
  1027. * @since 3.0.0
  1028. * @param object $post_type_object
  1029. * @return object object with all the labels as member variables
  1030. */
  1031. function get_post_type_labels( $post_type_object ) {
  1032. $nohier_vs_hier_defaults = array(
  1033. 'name' => array( _x('Posts', 'post type general name'), _x('Pages', 'post type general name') ),
  1034. 'singular_name' => array( _x('Post', 'post type singular name'), _x('Page', 'post type singular name') ),
  1035. 'add_new' => array( _x('Add New', 'post'), _x('Add New', 'page') ),
  1036. 'add_new_item' => array( __('Add New Post'), __('Add New Page') ),
  1037. 'edit_item' => array( __('Edit Post'), __('Edit Page') ),
  1038. 'new_item' => array( __('New Post'), __('New Page') ),
  1039. 'view_item' => array( __('View Post'), __('View Page') ),
  1040. 'search_items' => array( __('Search Posts'), __('Search Pages') ),
  1041. 'not_found' => array( __('No posts found.'), __('No pages found.') ),
  1042. 'not_found_in_trash' => array( __('No posts found in Trash.'), __('No pages found in Trash.') ),
  1043. 'parent_item_colon' => array( null, __('Parent Page:') ),
  1044. 'all_items' => array( __( 'All Posts' ), __( 'All Pages' ) )
  1045. );
  1046. $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
  1047. return _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
  1048. }
  1049. /**
  1050. * Builds an object with custom-something object (post type, taxonomy) labels out of a custom-something object
  1051. *
  1052. * @access private
  1053. * @since 3.0.0
  1054. */
  1055. function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) {
  1056. if ( isset( $object->label ) && empty( $object->labels['name'] ) )
  1057. $object->labels['name'] = $object->label;
  1058. if ( !isset( $object->labels['singular_name'] ) && isset( $object->labels['name'] ) )
  1059. $object->labels['singular_name'] = $object->labels['name'];
  1060. if ( ! isset( $object->labels['name_admin_bar'] ) )
  1061. $object->labels['name_admin_bar'] = isset( $object->labels['singular_name'] ) ? $object->labels['singular_name'] : $object->name;
  1062. if ( !isset( $object->labels['menu_name'] ) && isset( $object->labels['name'] ) )
  1063. $object->labels['menu_name'] = $object->labels['name'];
  1064. if ( !isset( $object->labels['all_items'] ) && isset( $object->labels['menu_name'] ) )
  1065. $object->labels['all_items'] = $object->labels['menu_name'];
  1066. foreach ( $nohier_vs_hier_defaults as $key => $value )
  1067. $defaults[$key] = $object->hierarchical ? $value[1] : $value[0];
  1068. $labels = array_merge( $defaults, $object->labels );
  1069. return (object)$labels;
  1070. }
  1071. /**
  1072. * Adds submenus for post types.
  1073. *
  1074. * @access private
  1075. * @since 3.1.0
  1076. */
  1077. function _add_post_type_submenus() {
  1078. foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) {
  1079. $ptype_obj = get_post_type_object( $ptype );
  1080. // Submenus only.
  1081. if ( ! $ptype_obj->show_in_menu || $ptype_obj->show_in_menu === true )
  1082. continue;
  1083. add_submenu_page( $ptype_obj->show_in_menu, $ptype_obj->labels->name, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype" );
  1084. }
  1085. }
  1086. add_action( 'admin_menu', '_add_post_type_submenus' );
  1087. /**
  1088. * Register support of certain features for a post type.
  1089. *
  1090. * All features are directly associated with a functional area of the edit screen, such as the
  1091. * editor or a meta box: 'title', 'editor', 'comments', 'revisions', 'trackbacks', 'author',
  1092. * 'excerpt', 'page-attributes', 'thumbnail', and 'custom-fields'.
  1093. *
  1094. * Additionally, the 'revisions' feature dictates whether the post type will store revisions,
  1095. * and the 'comments' feature dicates whether the comments count will show on the edit screen.
  1096. *
  1097. * @since 3.0.0
  1098. * @param string $post_type The post type for which to add the feature
  1099. * @param string|array $feature the feature being added, can be an array of feature strings or a single string
  1100. */
  1101. function add_post_type_support( $post_type, $feature ) {
  1102. global $_wp_post_type_features;
  1103. $features = (array) $feature;
  1104. foreach ($features as $feature) {
  1105. if ( func_num_args() == 2 )
  1106. $_wp_post_type_features[$post_type][$feature] = true;
  1107. else
  1108. $_wp_post_type_features[$post_type][$feature] = array_slice( func_get_args(), 2 );
  1109. }
  1110. }
  1111. /**
  1112. * Remove support for a feature from a post type.
  1113. *
  1114. * @since 3.0.0
  1115. * @param string $post_type The post type for which to remove the feature
  1116. * @param string $feature The feature being removed
  1117. */
  1118. function remove_post_type_support( $post_type, $feature ) {
  1119. global $_wp_post_type_features;
  1120. if ( !isset($_wp_post_type_features[$post_type]) )
  1121. return;
  1122. if ( isset($_wp_post_type_features[$post_type][$feature]) )
  1123. unset($_wp_post_type_features[$post_type][$feature]);
  1124. }
  1125. /**
  1126. * Checks a post type's support for a given feature
  1127. *
  1128. * @since 3.0.0
  1129. * @param string $post_type The post type being checked
  1130. * @param string $feature the feature being checked
  1131. * @return boolean
  1132. */
  1133. function post_type_supports( $post_type, $feature ) {
  1134. global $_wp_post_type_features;
  1135. if ( !isset( $_wp_post_type_features[$post_type][$feature] ) )
  1136. return false;
  1137. // If no args passed then no extra checks need be performed
  1138. if ( func_num_args() <= 2 )
  1139. return true;
  1140. // @todo Allow pluggable arg checking
  1141. //$args = array_slice( func_get_args(), 2 );
  1142. return true;
  1143. }
  1144. /**
  1145. * Updates the post type for the post ID.
  1146. *
  1147. * The page or post cache will be cleaned for the post ID.
  1148. *
  1149. * @since 2.5.0
  1150. *
  1151. * @uses $wpdb
  1152. *
  1153. * @param int $post_id Post ID to change post type. Not actually optional.
  1154. * @param string $post_type Optional, default is post. Supported values are 'post' or 'page' to
  1155. * name a few.
  1156. * @return int Amount of rows changed. Should be 1 for success and 0 for failure.
  1157. */
  1158. function set_post_type( $post_id = 0, $post_type = 'post' ) {
  1159. global $wpdb;
  1160. $post_type = sanitize_post_field('post_type', $post_type, $post_id, 'db');
  1161. $return = $wpdb->update($wpdb->posts, array('post_type' => $post_type), array('ID' => $post_id) );
  1162. if ( 'page' == $post_type )
  1163. clean_page_cache($post_id);
  1164. else
  1165. clean_post_cache($post_id);
  1166. return $return;
  1167. }
  1168. /**
  1169. * Retrieve list of latest posts or posts matching criteria.
  1170. *
  1171. * The defaults are as follows:
  1172. * 'numberposts' - Default is 5. Total number of posts to retrieve.
  1173. * 'offset' - Default is 0. See {@link WP_Query::query()} for more.
  1174. * 'category' - What category to pull the posts from.
  1175. * 'orderby' - Default is 'post_date'. How to order the posts.
  1176. * 'order' - Default is 'DESC'. The order to retrieve the posts.
  1177. * 'include' - See {@link WP_Query::query()} for more.
  1178. * 'exclude' - See {@link WP_Query::query()} for more.
  1179. * 'meta_key' - See {@link WP_Query::query()} for more.
  1180. * 'meta_value' - See {@link WP_Query::query()} for more.
  1181. * 'post_type' - Default is 'post'. Can be 'page', or 'attachment' to name a few.
  1182. * 'post_parent' - The parent of the post or post type.
  1183. * 'post_status' - Default is 'publish'. Post status to retrieve.
  1184. *
  1185. * @since 1.2.0
  1186. * @uses $wpdb
  1187. * @uses WP_Query::query() See for more default arguments and information.
  1188. * @link http://codex.wordpress.org/Template_Tags/get_posts
  1189. *
  1190. * @param array $args Optional. Overrides defaults.
  1191. * @return array List of posts.
  1192. */
  1193. function get_posts($args = null) {
  1194. $defaults = array(
  1195. 'numberposts' => 5, 'offset' => 0,
  1196. 'category' => 0, 'orderby' => 'post_date',
  1197. 'order' => 'DESC', 'include' => array(),
  1198. 'exclude' => array(), 'meta_key' => '',
  1199. 'meta_value' =>'', 'post_type' => 'post',
  1200. 'suppress_filters' => true
  1201. );
  1202. $r = wp_parse_args( $args, $defaults );
  1203. if ( empty( $r['post_status'] ) )
  1204. $r['post_status'] = ( 'attachment' == $

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