PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/post.php

https://bitbucket.org/squinlan/twolittlebirdsbakery
PHP | 5331 lines | 2588 code | 729 blank | 2014 comment | 749 complexity | 5d8b48b6e53418c4fbce3a86f23a59a2 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1

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

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

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