PageRenderTime 57ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 1ms

/wp-includes/post.php

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

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