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

/msw/dev/wp-includes/post.php

https://github.com/chrissiebrodigan/USC
PHP | 4643 lines | 2248 code | 653 blank | 1742 comment | 652 complexity | 929b5cb882b435c843ad604799b0828f MD5 | raw file

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. function create_initial_post_types() {
  16. register_post_type( 'post', array(
  17. 'label' => __( 'Posts' ),
  18. 'singular_label' => __( 'Post' ),
  19. 'public' => true,
  20. 'show_ui' => false,
  21. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  22. '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
  23. 'capability_type' => 'post',
  24. 'hierarchical' => false,
  25. 'rewrite' => false,
  26. 'query_var' => false,
  27. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions' ),
  28. ) );
  29. register_post_type( 'page', array(
  30. 'label' => __( 'Pages' ),
  31. 'singular_label' => __( 'Page' ),
  32. 'public' => true,
  33. 'show_ui' => false,
  34. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  35. '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
  36. 'capability_type' => 'page',
  37. 'hierarchical' => true,
  38. 'rewrite' => false,
  39. 'query_var' => false,
  40. 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
  41. ) );
  42. register_post_type( 'attachment', array(
  43. 'label' => __( 'Media' ),
  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. 'hierarchical' => false,
  50. 'rewrite' => false,
  51. 'query_var' => false,
  52. 'can_export' => false,
  53. ) );
  54. register_post_type( 'revision', array(
  55. 'label' => __( 'Revisions' ),
  56. 'singular_label' => __( 'Revision' ),
  57. 'public' => false,
  58. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  59. '_edit_link' => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */
  60. 'capability_type' => 'post',
  61. 'hierarchical' => false,
  62. 'rewrite' => false,
  63. 'query_var' => false,
  64. ) );
  65. register_post_type( 'nav_menu_item', array(
  66. 'label' => __( 'Navigation Menu Items' ),
  67. 'singular_label' => __( 'Navigation Menu Item' ),
  68. 'public' => false,
  69. 'show_ui' => false,
  70. '_builtin' => true, /* internal use only. don't use this when registering your own post type. */
  71. 'capability_type' => 'post',
  72. 'hierarchical' => false,
  73. 'rewrite' => false,
  74. 'query_var' => false,
  75. ) );
  76. register_post_status( 'publish', array(
  77. 'label' => _x( 'Published', 'post' ),
  78. 'public' => true,
  79. '_builtin' => true, /* internal use only. */
  80. 'label_count' => _n_noop( 'Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>' ),
  81. ) );
  82. register_post_status( 'future', array(
  83. 'label' => _x( 'Scheduled', 'post' ),
  84. 'protected' => true,
  85. '_builtin' => true, /* internal use only. */
  86. 'label_count' => _n_noop('Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>' ),
  87. ) );
  88. register_post_status( 'draft', array(
  89. 'label' => _x( 'Draft', 'post' ),
  90. 'protected' => true,
  91. '_builtin' => true, /* internal use only. */
  92. 'label_count' => _n_noop( 'Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>' ),
  93. ) );
  94. register_post_status( 'pending', array(
  95. 'label' => _x( 'Pending', 'post' ),
  96. 'protected' => true,
  97. '_builtin' => true, /* internal use only. */
  98. 'label_count' => _n_noop( 'Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>' ),
  99. ) );
  100. register_post_status( 'private', array(
  101. 'label' => _x( 'Private', 'post' ),
  102. 'private' => true,
  103. '_builtin' => true, /* internal use only. */
  104. 'label_count' => _n_noop( 'Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>' ),
  105. ) );
  106. register_post_status( 'trash', array(
  107. 'label' => _x( 'Trash', 'post' ),
  108. 'internal' => true,
  109. '_builtin' => true, /* internal use only. */
  110. 'label_count' => _n_noop( 'Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>' ),
  111. 'show_in_admin_status_list' => true,
  112. ) );
  113. register_post_status( 'auto-draft', array(
  114. 'label' => 'auto-draft',
  115. 'internal' => true,
  116. '_builtin' => true, /* internal use only. */
  117. ) );
  118. register_post_status( 'inherit', array(
  119. 'label' => 'inherit',
  120. 'internal' => true,
  121. '_builtin' => true, /* internal use only. */
  122. 'exclude_from_search' => false,
  123. ) );
  124. }
  125. add_action( 'init', 'create_initial_post_types', 0 ); // highest priority
  126. /**
  127. * Retrieve attached file path based on attachment ID.
  128. *
  129. * You can optionally send it through the 'get_attached_file' filter, but by
  130. * default it will just return the file path unfiltered.
  131. *
  132. * The function works by getting the single post meta name, named
  133. * '_wp_attached_file' and returning it. This is a convenience function to
  134. * prevent looking up the meta name and provide a mechanism for sending the
  135. * attached filename through a filter.
  136. *
  137. * @since 2.0.0
  138. * @uses apply_filters() Calls 'get_attached_file' on file path and attachment ID.
  139. *
  140. * @param int $attachment_id Attachment ID.
  141. * @param bool $unfiltered Whether to apply filters.
  142. * @return string The file path to the attached file.
  143. */
  144. function get_attached_file( $attachment_id, $unfiltered = false ) {
  145. $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
  146. // If the file is relative, prepend upload dir
  147. if ( 0 !== strpos($file, '/') && !preg_match('|^.:\\\|', $file) && ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) )
  148. $file = $uploads['basedir'] . "/$file";
  149. if ( $unfiltered )
  150. return $file;
  151. return apply_filters( 'get_attached_file', $file, $attachment_id );
  152. }
  153. /**
  154. * Update attachment file path based on attachment ID.
  155. *
  156. * Used to update the file path of the attachment, which uses post meta name
  157. * '_wp_attached_file' to store the path of the attachment.
  158. *
  159. * @since 2.1.0
  160. * @uses apply_filters() Calls 'update_attached_file' on file path and attachment ID.
  161. *
  162. * @param int $attachment_id Attachment ID
  163. * @param string $file File path for the attachment
  164. * @return bool False on failure, true on success.
  165. */
  166. function update_attached_file( $attachment_id, $file ) {
  167. if ( !get_post( $attachment_id ) )
  168. return false;
  169. $file = apply_filters( 'update_attached_file', $file, $attachment_id );
  170. $file = _wp_relative_upload_path($file);
  171. return update_post_meta( $attachment_id, '_wp_attached_file', $file );
  172. }
  173. /**
  174. * Return relative path to an uploaded file.
  175. *
  176. * The path is relative to the current upload dir.
  177. *
  178. * @since 2.9.0
  179. * @uses apply_filters() Calls '_wp_relative_upload_path' on file path.
  180. *
  181. * @param string $path Full path to the file
  182. * @return string relative path on success, unchanged path on failure.
  183. */
  184. function _wp_relative_upload_path( $path ) {
  185. $new_path = $path;
  186. if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) {
  187. if ( 0 === strpos($new_path, $uploads['basedir']) ) {
  188. $new_path = str_replace($uploads['basedir'], '', $new_path);
  189. $new_path = ltrim($new_path, '/');
  190. }
  191. }
  192. return apply_filters( '_wp_relative_upload_path', $new_path, $path );
  193. }
  194. /**
  195. * Retrieve all children of the post parent ID.
  196. *
  197. * Normally, without any enhancements, the children would apply to pages. In the
  198. * context of the inner workings of WordPress, pages, posts, and attachments
  199. * share the same table, so therefore the functionality could apply to any one
  200. * of them. It is then noted that while this function does not work on posts, it
  201. * does not mean that it won't work on posts. It is recommended that you know
  202. * what context you wish to retrieve the children of.
  203. *
  204. * Attachments may also be made the child of a post, so if that is an accurate
  205. * statement (which needs to be verified), it would then be possible to get
  206. * all of the attachments for a post. Attachments have since changed since
  207. * version 2.5, so this is most likely unaccurate, but serves generally as an
  208. * example of what is possible.
  209. *
  210. * The arguments listed as defaults are for this function and also of the
  211. * {@link get_posts()} function. The arguments are combined with the
  212. * get_children defaults and are then passed to the {@link get_posts()}
  213. * function, which accepts additional arguments. You can replace the defaults in
  214. * this function, listed below and the additional arguments listed in the
  215. * {@link get_posts()} function.
  216. *
  217. * The 'post_parent' is the most important argument and important attention
  218. * needs to be paid to the $args parameter. If you pass either an object or an
  219. * integer (number), then just the 'post_parent' is grabbed and everything else
  220. * is lost. If you don't specify any arguments, then it is assumed that you are
  221. * in The Loop and the post parent will be grabbed for from the current post.
  222. *
  223. * The 'post_parent' argument is the ID to get the children. The 'numberposts'
  224. * is the amount of posts to retrieve that has a default of '-1', which is
  225. * used to get all of the posts. Giving a number higher than 0 will only
  226. * retrieve that amount of posts.
  227. *
  228. * The 'post_type' and 'post_status' arguments can be used to choose what
  229. * criteria of posts to retrieve. The 'post_type' can be anything, but WordPress
  230. * post types are 'post', 'pages', and 'attachments'. The 'post_status'
  231. * argument will accept any post status within the write administration panels.
  232. *
  233. * @see get_posts() Has additional arguments that can be replaced.
  234. * @internal Claims made in the long description might be inaccurate.
  235. *
  236. * @since 2.0.0
  237. *
  238. * @param mixed $args Optional. User defined arguments for replacing the defaults.
  239. * @param string $output Optional. Constant for return type, either OBJECT (default), ARRAY_A, ARRAY_N.
  240. * @return array|bool False on failure and the type will be determined by $output parameter.
  241. */
  242. function &get_children($args = '', $output = OBJECT) {
  243. $kids = array();
  244. if ( empty( $args ) ) {
  245. if ( isset( $GLOBALS['post'] ) ) {
  246. $args = array('post_parent' => (int) $GLOBALS['post']->post_parent );
  247. } else {
  248. return $kids;
  249. }
  250. } elseif ( is_object( $args ) ) {
  251. $args = array('post_parent' => (int) $args->post_parent );
  252. } elseif ( is_numeric( $args ) ) {
  253. $args = array('post_parent' => (int) $args);
  254. }
  255. $defaults = array(
  256. 'numberposts' => -1, 'post_type' => 'any',
  257. 'post_status' => 'any', 'post_parent' => 0,
  258. );
  259. $r = wp_parse_args( $args, $defaults );
  260. $children = get_posts( $r );
  261. if ( !$children )
  262. return $kids;
  263. update_post_cache($children);
  264. foreach ( $children as $key => $child )
  265. $kids[$child->ID] =& $children[$key];
  266. if ( $output == OBJECT ) {
  267. return $kids;
  268. } elseif ( $output == ARRAY_A ) {
  269. foreach ( (array) $kids as $kid )
  270. $weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);
  271. return $weeuns;
  272. } elseif ( $output == ARRAY_N ) {
  273. foreach ( (array) $kids as $kid )
  274. $babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));
  275. return $babes;
  276. } else {
  277. return $kids;
  278. }
  279. }
  280. /**
  281. * Get extended entry info (<!--more-->).
  282. *
  283. * There should not be any space after the second dash and before the word
  284. * 'more'. There can be text or space(s) after the word 'more', but won't be
  285. * referenced.
  286. *
  287. * The returned array has 'main' and 'extended' keys. Main has the text before
  288. * the <code><!--more--></code>. The 'extended' key has the content after the
  289. * <code><!--more--></code> comment.
  290. *
  291. * @since 1.0.0
  292. *
  293. * @param string $post Post content.
  294. * @return array Post before ('main') and after ('extended').
  295. */
  296. function get_extended($post) {
  297. //Match the new style more links
  298. if ( preg_match('/<!--more(.*?)?-->/', $post, $matches) ) {
  299. list($main, $extended) = explode($matches[0], $post, 2);
  300. } else {
  301. $main = $post;
  302. $extended = '';
  303. }
  304. // Strip leading and trailing whitespace
  305. $main = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $main);
  306. $extended = preg_replace('/^[\s]*(.*)[\s]*$/', '\\1', $extended);
  307. return array('main' => $main, 'extended' => $extended);
  308. }
  309. /**
  310. * Retrieves post data given a post ID or post object.
  311. *
  312. * See {@link sanitize_post()} for optional $filter values. Also, the parameter
  313. * $post, must be given as a variable, since it is passed by reference.
  314. *
  315. * @since 1.5.1
  316. * @uses $wpdb
  317. * @link http://codex.wordpress.org/Function_Reference/get_post
  318. *
  319. * @param int|object $post Post ID or post object.
  320. * @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
  321. * @param string $filter Optional, default is raw.
  322. * @return mixed Post data
  323. */
  324. function &get_post(&$post, $output = OBJECT, $filter = 'raw') {
  325. global $wpdb;
  326. $null = null;
  327. if ( empty($post) ) {
  328. if ( isset($GLOBALS['post']) )
  329. $_post = & $GLOBALS['post'];
  330. else
  331. return $null;
  332. } elseif ( is_object($post) && empty($post->filter) ) {
  333. _get_post_ancestors($post);
  334. $_post = sanitize_post($post, 'raw');
  335. wp_cache_add($post->ID, $_post, 'posts');
  336. } else {
  337. if ( is_object($post) )
  338. $post = $post->ID;
  339. $post = (int) $post;
  340. if ( ! $_post = wp_cache_get($post, 'posts') ) {
  341. $_post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post));
  342. if ( ! $_post )
  343. return $null;
  344. _get_post_ancestors($_post);
  345. $_post = sanitize_post($_post, 'raw');
  346. wp_cache_add($_post->ID, $_post, 'posts');
  347. }
  348. }
  349. if ($filter != 'raw')
  350. $_post = sanitize_post($_post, $filter);
  351. if ( $output == OBJECT ) {
  352. return $_post;
  353. } elseif ( $output == ARRAY_A ) {
  354. $__post = get_object_vars($_post);
  355. return $__post;
  356. } elseif ( $output == ARRAY_N ) {
  357. $__post = array_values(get_object_vars($_post));
  358. return $__post;
  359. } else {
  360. return $_post;
  361. }
  362. }
  363. /**
  364. * Retrieve ancestors of a post.
  365. *
  366. * @since 2.5.0
  367. *
  368. * @param int|object $post Post ID or post object
  369. * @return array Ancestor IDs or empty array if none are found.
  370. */
  371. function get_post_ancestors($post) {
  372. $post = get_post($post);
  373. if ( !empty($post->ancestors) )
  374. return $post->ancestors;
  375. return array();
  376. }
  377. /**
  378. * Retrieve data from a post field based on Post ID.
  379. *
  380. * Examples of the post field will be, 'post_type', 'post_status', 'content',
  381. * etc and based off of the post object property or key names.
  382. *
  383. * The context values are based off of the taxonomy filter functions and
  384. * supported values are found within those functions.
  385. *
  386. * @since 2.3.0
  387. * @uses sanitize_post_field() See for possible $context values.
  388. *
  389. * @param string $field Post field name
  390. * @param id $post Post ID
  391. * @param string $context Optional. How to filter the field. Default is display.
  392. * @return WP_Error|string Value in post field or WP_Error on failure
  393. */
  394. function get_post_field( $field, $post, $context = 'display' ) {
  395. $post = (int) $post;
  396. $post = get_post( $post );
  397. if ( is_wp_error($post) )
  398. return $post;
  399. if ( !is_object($post) )
  400. return '';
  401. if ( !isset($post->$field) )
  402. return '';
  403. return sanitize_post_field($field, $post->$field, $post->ID, $context);
  404. }
  405. /**
  406. * Retrieve the mime type of an attachment based on the ID.
  407. *
  408. * This function can be used with any post type, but it makes more sense with
  409. * attachments.
  410. *
  411. * @since 2.0.0
  412. *
  413. * @param int $ID Optional. Post ID.
  414. * @return bool|string False on failure or returns the mime type
  415. */
  416. function get_post_mime_type($ID = '') {
  417. $post = & get_post($ID);
  418. if ( is_object($post) )
  419. return $post->post_mime_type;
  420. return false;
  421. }
  422. /**
  423. * Retrieve the post status based on the Post ID.
  424. *
  425. * If the post ID is of an attachment, then the parent post status will be given
  426. * instead.
  427. *
  428. * @since 2.0.0
  429. *
  430. * @param int $ID Post ID
  431. * @return string|bool Post status or false on failure.
  432. */
  433. function get_post_status($ID = '') {
  434. $post = get_post($ID);
  435. if ( !is_object($post) )
  436. return false;
  437. // Unattached attachments are assumed to be published.
  438. if ( ('attachment' == $post->post_type) && ('inherit' == $post->post_status) && ( 0 == $post->post_parent) )
  439. return 'publish';
  440. if ( ('attachment' == $post->post_type) && $post->post_parent && ($post->ID != $post->post_parent) )
  441. return get_post_status($post->post_parent);
  442. return $post->post_status;
  443. }
  444. /**
  445. * Retrieve all of the WordPress supported post statuses.
  446. *
  447. * Posts have a limited set of valid status values, this provides the
  448. * post_status values and descriptions.
  449. *
  450. * @since 2.5.0
  451. *
  452. * @return array List of post statuses.
  453. */
  454. function get_post_statuses( ) {
  455. $status = array(
  456. 'draft' => __('Draft'),
  457. 'pending' => __('Pending Review'),
  458. 'private' => __('Private'),
  459. 'publish' => __('Published')
  460. );
  461. return $status;
  462. }
  463. /**
  464. * Retrieve all of the WordPress support page statuses.
  465. *
  466. * Pages have a limited set of valid status values, this provides the
  467. * post_status values and descriptions.
  468. *
  469. * @since 2.5.0
  470. *
  471. * @return array List of page statuses.
  472. */
  473. function get_page_statuses( ) {
  474. $status = array(
  475. 'draft' => __('Draft'),
  476. 'private' => __('Private'),
  477. 'publish' => __('Published')
  478. );
  479. return $status;
  480. }
  481. /**
  482. * Register a post type. Do not use before init.
  483. *
  484. * A simple function for creating or modifying a post status based on the
  485. * parameters given. The function will accept an array (second optional
  486. * parameter), along with a string for the post status name.
  487. *
  488. *
  489. * Optional $args contents:
  490. *
  491. * label - A descriptive name for the post status marked for translation. Defaults to $post_status.
  492. * public - Whether posts of this status should be shown in the admin UI. Defaults to true.
  493. * exclude_from_search - Whether to exclude posts with this post status from search results. Defaults to true.
  494. *
  495. * @package WordPress
  496. * @subpackage Post
  497. * @since 3.0.0
  498. * @uses $wp_post_statuses Inserts new post status object into the list
  499. *
  500. * @param string $post_status Name of the post status.
  501. * @param array|string $args See above description.
  502. */
  503. function register_post_status($post_status, $args = array()) {
  504. global $wp_post_statuses;
  505. if (!is_array($wp_post_statuses))
  506. $wp_post_statuses = array();
  507. // Args prefixed with an underscore are reserved for internal use.
  508. $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);
  509. $args = wp_parse_args($args, $defaults);
  510. $args = (object) $args;
  511. $post_status = sanitize_user($post_status, true);
  512. $args->name = $post_status;
  513. if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private )
  514. $args->internal = true;
  515. if ( null === $args->public )
  516. $args->public = false;
  517. if ( null === $args->private )
  518. $args->private = false;
  519. if ( null === $args->protected )
  520. $args->protected = false;
  521. if ( null === $args->internal )
  522. $args->internal = false;
  523. if ( null === $args->publicly_queryable )
  524. $args->publicly_queryable = $args->public;
  525. if ( null === $args->exclude_from_search )
  526. $args->exclude_from_search = $args->internal;
  527. if ( null === $args->show_in_admin_all_list )
  528. $args->show_in_admin_all_list = !$args->internal;
  529. if ( null === $args->show_in_admin_status_list )
  530. $args->show_in_admin_status_list = !$args->internal;
  531. if ( null === $args->single_view_cap )
  532. $args->single_view_cap = $args->public ? '' : 'edit';
  533. if ( false === $args->label )
  534. $args->label = $post_status;
  535. if ( false === $args->label_count )
  536. $args->label_count = array( $args->label, $args->label );
  537. $wp_post_statuses[$post_status] = $args;
  538. return $args;
  539. }
  540. /**
  541. * Retrieve a post status object by name
  542. *
  543. * @package WordPress
  544. * @subpackage Post
  545. * @since 3.0.0
  546. * @uses $wp_post_statuses
  547. * @see register_post_status
  548. * @see get_post_statuses
  549. *
  550. * @param string $post_type The name of a registered post status
  551. * @return object A post status object
  552. */
  553. function get_post_status_object( $post_status ) {
  554. global $wp_post_statuses;
  555. if ( empty($wp_post_statuses[$post_status]) )
  556. return null;
  557. return $wp_post_statuses[$post_status];
  558. }
  559. /**
  560. * Get a list of all registered post status objects.
  561. *
  562. * @package WordPress
  563. * @subpackage Post
  564. * @since 3.0.0
  565. * @uses $wp_post_statuses
  566. * @see register_post_status
  567. * @see get_post_status_object
  568. *
  569. * @param array|string $args An array of key => value arguments to match against the post status objects.
  570. * @param string $output The type of output to return, either post status 'names' or 'objects'. 'names' is the default.
  571. * @param string $operator The logical operation to perform. 'or' means only one element
  572. * from the array needs to match; 'and' means all elements must match. The default is 'and'.
  573. * @return array A list of post type names or objects
  574. */
  575. function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) {
  576. global $wp_post_statuses;
  577. $field = ('names' == $output) ? 'name' : false;
  578. return wp_filter_object_list($wp_post_statuses, $args, $operator, $field);
  579. }
  580. /**
  581. * Whether the post type is hierarchical.
  582. *
  583. * A false return value might also mean that the post type does not exist.
  584. *
  585. * @since 3.0.0
  586. * @see get_post_type_object
  587. *
  588. * @param string|int|object $post Post type name, post id, or a post object.
  589. * @return bool true if post type is hierarchical, else false.
  590. */
  591. function is_post_type_hierarchical( $post = false ) {
  592. if ( is_string($post) && $is_post_type = get_post_type_object($post) )
  593. return $is_post_type->hierarchical;
  594. $ptype = get_post( $post );
  595. if ( $ptype && $is_post_type = get_post_type_object($ptype->post_type) )
  596. return $is_post_type->hierarchical;
  597. return false;
  598. }
  599. /**
  600. * Checks if a post type is registered, can also check if the current or specified post is of a post type.
  601. *
  602. * @since 3.0.0
  603. * @uses get_post_type()
  604. *
  605. * @param string|array $types Type or types to check. Defaults to all post types.
  606. * @param int $id Post ID. Defaults to current ID.
  607. * @return bool
  608. */
  609. function is_post_type( $types = false, $id = false ) {
  610. $types = ( $types === false ) ? get_post_types() : (array) $types;
  611. return in_array( get_post_type( $id ), $types );
  612. }
  613. /**
  614. * Retrieve the post type of the current post or of a given post.
  615. *
  616. * @since 2.1.0
  617. *
  618. * @uses $post The Loop current post global
  619. *
  620. * @param mixed $the_post Optional. Post object or post ID.
  621. * @return bool|string post type or false on failure.
  622. */
  623. function get_post_type( $the_post = false ) {
  624. global $post;
  625. if ( false === $the_post )
  626. $the_post = $post;
  627. elseif ( is_numeric($the_post) )
  628. $the_post = get_post($the_post);
  629. if ( is_object($the_post) )
  630. return $the_post->post_type;
  631. return false;
  632. }
  633. /**
  634. * Retrieve a post type object by name
  635. *
  636. * @package WordPress
  637. * @subpackage Post
  638. * @since 3.0.0
  639. * @uses $wp_post_types
  640. * @see register_post_type
  641. * @see get_post_types
  642. *
  643. * @param string $post_type The name of a registered post type
  644. * @return object A post type object
  645. */
  646. function get_post_type_object( $post_type ) {
  647. global $wp_post_types;
  648. if ( empty($wp_post_types[$post_type]) )
  649. return null;
  650. return $wp_post_types[$post_type];
  651. }
  652. /**
  653. * Get a list of all registered post type objects.
  654. *
  655. * @package WordPress
  656. * @subpackage Post
  657. * @since 2.9.0
  658. * @uses $wp_post_types
  659. * @see register_post_type
  660. *
  661. * @param array|string $args An array of key => value arguments to match against the post type objects.
  662. * @param string $output The type of output to return, either post type 'names' or 'objects'. 'names' is the default.
  663. * @param string $operator The logical operation to perform. 'or' means only one element
  664. * from the array needs to match; 'and' means all elements must match. The default is 'and'.
  665. * @return array A list of post type names or objects
  666. */
  667. function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) {
  668. global $wp_post_types;
  669. $field = ('names' == $output) ? 'name' : false;
  670. return wp_filter_object_list($wp_post_types, $args, $operator, $field);
  671. }
  672. /**
  673. * Register a post type. Do not use before init.
  674. *
  675. * A simple function for creating or modifying a post type based on the
  676. * parameters given. The function will accept an array (second optional
  677. * parameter), along with a string for the post type name.
  678. *
  679. *
  680. * Optional $args contents:
  681. *
  682. * label - A (plural) descriptive name for the post type marked for translation. Defaults to $post_type.
  683. * singular_label - A (singular) descriptive name for the post type marked for translation. Defaults to $label.
  684. * description - A short descriptive summary of what the post type is. Defaults to blank.
  685. * public - Whether posts of this type should be shown in the admin UI. Defaults to false.
  686. * exclude_from_search - Whether to exclude posts with this post type from search results. Defaults to true if the type is not public, false if the type is public.
  687. * publicly_queryable - Whether post_type queries can be performed from the front page. Defaults to whatever public is set as.
  688. * show_ui - Whether to generate a default UI for managing this post type. Defaults to true if the type is public, false if the type is not public.
  689. * menu_position - The position in the menu order the post type should appear. Defaults to the bottom.
  690. * menu_icon - The url to the icon to be used for this menu. Defaults to use the posts icon.
  691. * inherit_type - The post type from which to inherit the edit link and capability type. Defaults to none.
  692. * capability_type - The post type to use for checking read, edit, and delete capabilities. Defaults to "post".
  693. * edit_cap - The capability that controls editing a particular object of this post type. Defaults to "edit_$capability_type" (edit_post).
  694. * edit_type_cap - The capability that controls editing objects of this post type as a class. Defaults to "edit_ . $capability_type . s" (edit_posts).
  695. * edit_others_cap - The capability that controls editing objects of this post type that are owned by other users. Defaults to "edit_others_ . $capability_type . s" (edit_others_posts).
  696. * publish_others_cap - The capability that controls publishing objects of this post type. Defaults to "publish_ . $capability_type . s" (publish_posts).
  697. * read_cap - The capability that controls reading a particular object of this post type. Defaults to "read_$capability_type" (read_post).
  698. * delete_cap - The capability that controls deleting a particular object of this post type. Defaults to "delete_$capability_type" (delete_post).
  699. * hierarchical - Whether the post type is hierarchical. Defaults to false.
  700. * supports - An alias for calling add_post_type_support() directly. See add_post_type_support() for Documentation. Defaults to none.
  701. * register_meta_box_cb - Provide a callback function that will be called when setting up the meta boxes for the edit form. Do remove_meta_box() and add_meta_box() calls in the callback.
  702. * taxonomies - An array of taxonomy identifiers that will be registered for the post type. Default is no taxonomies. Taxonomies can be registered later with register_taxonomy() or register_taxonomy_for_object_type().
  703. *
  704. * @package WordPress
  705. * @subpackage Post
  706. * @since 2.9.0
  707. * @uses $wp_post_types Inserts new post type object into the list
  708. *
  709. * @param string $post_type Name of the post type.
  710. * @param array|string $args See above description.
  711. */
  712. function register_post_type($post_type, $args = array()) {
  713. global $wp_post_types, $wp_rewrite, $wp;
  714. if ( !is_array($wp_post_types) )
  715. $wp_post_types = array();
  716. // Args prefixed with an underscore are reserved for internal use.
  717. $defaults = array('label' => false, 'singular_label' => false, 'description' => '', 'publicly_queryable' => null, 'exclude_from_search' => null, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'public' => false, 'rewrite' => true, 'query_var' => true, 'supports' => array(), 'register_meta_box_cb' => null, 'taxonomies' => array(), 'show_ui' => null, 'menu_position' => null, 'menu_icon' => null, 'permalink_epmask' => EP_PERMALINK, 'can_export' => true );
  718. $args = wp_parse_args($args, $defaults);
  719. $args = (object) $args;
  720. $post_type = sanitize_user($post_type, true);
  721. $args->name = $post_type;
  722. // If not set, default to the setting for public.
  723. if ( null === $args->publicly_queryable )
  724. $args->publicly_queryable = $args->public;
  725. // If not set, default to the setting for public.
  726. if ( null === $args->show_ui )
  727. $args->show_ui = $args->public;
  728. // If not set, default to true if not public, false if public.
  729. if ( null === $args->exclude_from_search )
  730. $args->exclude_from_search = !$args->public;
  731. if ( false === $args->label )
  732. $args->label = $post_type;
  733. if ( false === $args->singular_label )
  734. $args->singular_label = $args->label;
  735. if ( empty($args->capability_type) )
  736. $args->capability_type = 'post';
  737. if ( empty($args->edit_cap) )
  738. $args->edit_cap = 'edit_' . $args->capability_type;
  739. if ( empty($args->edit_type_cap) )
  740. $args->edit_type_cap = 'edit_' . $args->capability_type . 's';
  741. if ( empty($args->edit_others_cap) )
  742. $args->edit_others_cap = 'edit_others_' . $args->capability_type . 's';
  743. if ( empty($args->publish_cap) )
  744. $args->publish_cap = 'publish_' . $args->capability_type . 's';
  745. if ( empty($args->read_cap) )
  746. $args->read_cap = 'read_' . $args->capability_type;
  747. if ( empty($args->read_private_cap) )
  748. $args->read_private_cap = 'read_private_' . $args->capability_type . 's';
  749. if ( empty($args->delete_cap) )
  750. $args->delete_cap = 'delete_' . $args->capability_type;
  751. if ( ! empty($args->supports) ) {
  752. add_post_type_support($post_type, $args->supports);
  753. unset($args->supports);
  754. } else {
  755. // Add default features
  756. add_post_type_support($post_type, array('title', 'editor'));
  757. }
  758. if ( false !== $args->query_var && !empty($wp) ) {
  759. if ( true === $args->query_var )
  760. $args->query_var = $post_type;
  761. $args->query_var = sanitize_title_with_dashes($args->query_var);
  762. $wp->add_query_var($args->query_var);
  763. }
  764. if ( false !== $args->rewrite && '' != get_option('permalink_structure') ) {
  765. if ( !is_array($args->rewrite) )
  766. $args->rewrite = array();
  767. if ( !isset($args->rewrite['slug']) )
  768. $args->rewrite['slug'] = $post_type;
  769. if ( !isset($args->rewrite['with_front']) )
  770. $args->rewrite['with_front'] = true;
  771. if ( $args->hierarchical )
  772. $wp_rewrite->add_rewrite_tag("%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
  773. else
  774. $wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
  775. $wp_rewrite->add_permastruct($post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite['with_front'], $args->permalink_epmask);
  776. }
  777. if ( $args->register_meta_box_cb )
  778. add_action('add_meta_boxes_' . $post_type, $args->register_meta_box_cb, 10, 1);
  779. $wp_post_types[$post_type] = $args;
  780. add_action( 'future_' . $post_type, '_future_post_hook', 5, 2 );
  781. foreach ( $args->taxonomies as $taxonomy ) {
  782. register_taxonomy_for_object_type( $taxonomy, $post_type );
  783. }
  784. return $args;
  785. }
  786. /**
  787. * Register support of certain features for a post type.
  788. *
  789. * All features are directly associated with a functional area of the edit screen, such as the
  790. * editor or a meta box: 'title', 'editor', 'comments', 'revisions', 'trackbacks', 'author',
  791. * 'excerpt', 'page-attributes', 'thumbnail', and 'custom-fields'.
  792. *
  793. * Additionally, the 'revisions' feature dictates whether the post type will store revisions,
  794. * and the 'comments' feature dicates whether the comments count will show on the edit screen.
  795. *
  796. * @since 3.0.0
  797. * @param string $post_type The post type for which to add the feature
  798. * @param string|array $feature the feature being added, can be an array of feature strings or a single string
  799. */
  800. function add_post_type_support( $post_type, $feature ) {
  801. global $_wp_post_type_features;
  802. $features = (array) $feature;
  803. foreach ($features as $feature) {
  804. if ( func_num_args() == 2 )
  805. $_wp_post_type_features[$post_type][$feature] = true;
  806. else
  807. $_wp_post_type_features[$post_type][$feature] = array_slice( func_get_args(), 2 );
  808. }
  809. }
  810. /**
  811. * Remove support for a feature from a post type.
  812. *
  813. * @since 3.0.0
  814. * @param string $post_type The post type for which to remove the feature
  815. * @param string $feature The feature being removed
  816. */
  817. function remove_post_type_support( $post_type, $feature ) {
  818. global $_wp_post_type_features;
  819. if ( !isset($_wp_post_type_features[$post_type]) )
  820. return;
  821. if ( isset($_wp_post_type_features[$post_type][$feature]) )
  822. unset($_wp_post_type_features[$post_type][$feature]);
  823. }
  824. /**
  825. * Checks a post type's support for a given feature
  826. *
  827. * @since 3.0.0
  828. * @param string $post_type The post type being checked
  829. * @param string $feature the feature being checked
  830. * @return boolean
  831. */
  832. function post_type_supports( $post_type, $feature ) {
  833. global $_wp_post_type_features;
  834. if ( !isset( $_wp_post_type_features[$post_type][$feature] ) )
  835. return false;
  836. // If no args passed then no extra checks need be performed
  837. if ( func_num_args() <= 2 )
  838. return true;
  839. // @todo Allow pluggable arg checking
  840. //$args = array_slice( func_get_args(), 2 );
  841. return true;
  842. }
  843. /**
  844. * Updates the post type for the post ID.
  845. *
  846. * The page or post cache will be cleaned for the post ID.
  847. *
  848. * @since 2.5.0
  849. *
  850. * @uses $wpdb
  851. *
  852. * @param int $post_id Post ID to change post type. Not actually optional.
  853. * @param string $post_type Optional, default is post. Supported values are 'post' or 'page' to
  854. * name a few.
  855. * @return int Amount of rows changed. Should be 1 for success and 0 for failure.
  856. */
  857. function set_post_type( $post_id = 0, $post_type = 'post' ) {
  858. global $wpdb;
  859. $post_type = sanitize_post_field('post_type', $post_type, $post_id, 'db');
  860. $return = $wpdb->update($wpdb->posts, array('post_type' => $post_type), array('ID' => $post_id) );
  861. if ( 'page' == $post_type )
  862. clean_page_cache($post_id);
  863. else
  864. clean_post_cache($post_id);
  865. return $return;
  866. }
  867. /**
  868. * Retrieve list of latest posts or posts matching criteria.
  869. *
  870. * The defaults are as follows:
  871. * 'numberposts' - Default is 5. Total number of posts to retrieve.
  872. * 'offset' - Default is 0. See {@link WP_Query::query()} for more.
  873. * 'category' - What category to pull the posts from.
  874. * 'orderby' - Default is 'post_date'. How to order the posts.
  875. * 'order' - Default is 'DESC'. The order to retrieve the posts.
  876. * 'include' - See {@link WP_Query::query()} for more.
  877. * 'exclude' - See {@link WP_Query::query()} for more.
  878. * 'meta_key' - See {@link WP_Query::query()} for more.
  879. * 'meta_value' - See {@link WP_Query::query()} for more.
  880. * 'post_type' - Default is 'post'. Can be 'page', or 'attachment' to name a few.
  881. * 'post_parent' - The parent of the post or post type.
  882. * 'post_status' - Default is 'published'. Post status to retrieve.
  883. *
  884. * @since 1.2.0
  885. * @uses $wpdb
  886. * @uses WP_Query::query() See for more default arguments and information.
  887. * @link http://codex.wordpress.org/Template_Tags/get_posts
  888. *
  889. * @param array $args Optional. Overrides defaults.
  890. * @return array List of posts.
  891. */
  892. function get_posts($args = null) {
  893. $defaults = array(
  894. 'numberposts' => 5, 'offset' => 0,
  895. 'category' => 0, 'orderby' => 'post_date',
  896. 'order' => 'DESC', 'include' => array(),
  897. 'exclude' => array(), 'meta_key' => '',
  898. 'meta_value' =>'', 'post_type' => 'post',
  899. 'suppress_filters' => true
  900. );
  901. $r = wp_parse_args( $args, $defaults );
  902. if ( empty( $r['post_status'] ) )
  903. $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
  904. if ( ! empty($r['numberposts']) )
  905. $r['posts_per_page'] = $r['numberposts'];
  906. if ( ! empty($r['category']) )
  907. $r['cat'] = $r['category'];
  908. if ( ! empty($r['include']) ) {
  909. $incposts = wp_parse_id_list( $r['include'] );
  910. $r['posts_per_page'] = count($incposts); // only the number of posts included
  911. $r['post__in'] = $incposts;
  912. } elseif ( ! empty($r['exclude']) )
  913. $r['post__not_in'] = wp_parse_id_list( $r['exclude'] );
  914. $r['caller_get_posts'] = true;
  915. $get_posts = new WP_Query;
  916. return $get_posts->query($r);
  917. }
  918. //
  919. // Post meta functions
  920. //
  921. /**
  922. * Add meta data field to a post.
  923. *
  924. * Post meta data is called "Custom Fields" on the Administration Panels.
  925. *
  926. * @since 1.5.0
  927. * @uses $wpdb
  928. * @link http://codex.wordpress.org/Function_Reference/add_post_meta
  929. *
  930. * @param int $post_id Post ID.
  931. * @param string $key Metadata name.
  932. * @param mixed $value Metadata value.
  933. * @param bool $unique Optional, default is false. Whether the same key should not be added.
  934. * @return bool False for failure. True for success.
  935. */
  936. function add_post_meta($post_id, $meta_key, $meta_value, $unique = false) {
  937. // make sure meta is added to the post, not a revision
  938. if ( $the_post = wp_is_post_revision($post_id) )
  939. $post_id = $the_post;
  940. return add_metadata('post', $post_id, $meta_key, $meta_value, $unique);
  941. }
  942. /**
  943. * Remove metadata matching criteria from a post.
  944. *
  945. * You can match based on the key, or key and value. Removing based on key and
  946. * value, will keep from removing duplicate metadata with the same key. It also
  947. * allows removing all metadata matching key, if needed.
  948. *
  949. * @since 1.5.0
  950. * @uses $wpdb
  951. * @link http://codex.wordpress.org/Function_Reference/delete_post_meta
  952. *
  953. * @param int $post_id post ID
  954. * @param string $meta_key Metadata name.
  955. * @param mixed $meta_value Optional. Metadata value.
  956. * @return bool False for failure. True for success.
  957. */
  958. function delete_post_meta($post_id, $meta_key, $meta_value = '') {
  959. // make sure meta is added to the post, not a revision
  960. if ( $the_post = wp_is_post_revision($post_id) )
  961. $post_id = $the_post;
  962. return delete_metadata('post', $post_id, $meta_key, $meta_value);
  963. }
  964. /**
  965. * Retrieve post meta field for a post.
  966. *
  967. * @since 1.5.0
  968. * @uses $wpdb
  969. * @link http://codex.wordpress.org/Function_Reference/get_post_meta
  970. *
  971. * @param int $post_id Post ID.
  972. * @param string $key The meta key to retrieve.
  973. * @param bool $single Whether to return a single value.
  974. * @return mixed Will be an array if $single is false. Will be value of meta data field if $single
  975. * is true.
  976. */
  977. function get_post_meta($post_id, $key, $single = false) {
  978. return get_metadata('post', $post_id, $key, $single);
  979. }
  980. /**
  981. * Update post meta field based on post ID.
  982. *
  983. * Use the $prev_value parameter to differentiate between meta fields with the
  984. * same key and post ID.
  985. *
  986. * If the meta field for the post does not exist, it will be added.
  987. *
  988. * @since 1.5
  989. * @uses $wpdb
  990. * @link http://codex.wordpress.org/Function_Reference/update_post_meta
  991. *
  992. * @param int $post_id Post ID.
  993. * @param string $key Metadata key.
  994. * @param mixed $value Metadata value.
  995. * @param mixed $prev_value Optional. Previous value to check before removing.
  996. * @return bool False on failure, true if success.
  997. */
  998. function update_post_meta($post_id, $meta_key, $meta_value, $prev_value = '') {
  999. // make sure meta is added to the post, not a revision
  1000. if ( $the_post = wp_is_post_revision($post_id) )
  1001. $post_id = $the_post;
  1002. return update_metadata('post', $post_id, $meta_key, $meta_value, $prev_value);
  1003. }
  1004. /**
  1005. * Delete everything from post meta matching meta key.
  1006. *
  1007. * @since 2.3.0
  1008. * @uses $wpdb
  1009. *
  1010. * @param string $post_meta_key Key to search for when deleting.
  1011. * @return bool Whether the post meta key was deleted from the database
  1012. */
  1013. function delete_post_meta_by_key($post_meta_key) {
  1014. if ( !$post_meta_key )
  1015. return false;
  1016. global $wpdb;
  1017. $post_ids = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key));
  1018. if ( $post_ids ) {
  1019. $postmetaids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = %s", $post_meta_key ) );
  1020. $in = implode( ',', array_fill(1, count($postmetaids), '%d'));
  1021. do_action( 'delete_postmeta', $postmetaids );
  1022. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id IN($in)", $postmetaids ));
  1023. do_action( 'deleted_postmeta', $postmetaids );
  1024. foreach ( $post_ids as $post_id )
  1025. wp_cache_delete($post_id, 'post_meta');
  1026. return true;
  1027. }
  1028. return false;
  1029. }
  1030. /**
  1031. * Retrieve post meta fields, based on post ID.
  1032. *
  1033. * The post meta fields are retrieved from the cache, so the function is
  1034. * optimized to be called more than once. It also applies to the functions, that
  1035. * use this function.
  1036. *
  1037. * @since 1.2.0
  1038. * @link http://codex.wordpress.org/Function_Reference/get_post_custom
  1039. *
  1040. * @uses $id Current Loop Post ID
  1041. *
  1042. * @param int $post_id post ID
  1043. * @return array
  1044. */
  1045. function get_post_custom($post_id = 0) {
  1046. global $id;
  1047. if ( !$post_id )
  1048. $post_id = (int) $id;
  1049. $post_id = (int) $post_id;
  1050. if ( ! wp_cache_get($post_id, 'post_meta') )
  1051. update_postmeta_cache($post_id);
  1052. return wp_cache_get($post_id, 'post_meta');
  1053. }
  1054. /**
  1055. * Retrieve meta field names for a post.
  1056. *
  1057. * If there are no meta fields, then nothing (null) will be returned.
  1058. *
  1059. * @since 1.2.0
  1060. * @link http://codex.wordpress.org/Function_Reference/get_post_custom_keys
  1061. *
  1062. * @param int $post_id post ID
  1063. * @return array|null Either array of the keys, or null if keys could not be retrieved.
  1064. */
  1065. function get_post_custom_keys( $post_id = 0 ) {
  1066. $custom = get_post_custom( $post_id );
  1067. if ( !is_array($custom) )
  1068. return;
  1069. if ( $keys = array_keys($custom) )
  1070. return $keys;
  1071. }
  1072. /**
  1073. * Retrieve values for a custom post field.
  1074. *
  1075. * The parameters must not be considered optional. All of the post meta fields
  1076. * will be retrieved and only the meta field key values returned.
  1077. *
  1078. * @since 1.2.0
  1079. * @link http://codex.wordpress.org/Function_Reference/get_post_custom_values
  1080. *
  1081. * @param string $key Meta field key.
  1082. * @param int $post_id Post ID
  1083. * @return array Meta field values.
  1084. */
  1085. function get_post_custom_values( $key = '', $post_id = 0 ) {
  1086. if ( !$key )
  1087. return null;
  1088. $custom = get_post_custom($post_id);
  1089. return isset($custom[$key]) ? $custom[$key] : null;
  1090. }
  1091. /**
  1092. * Check if post is sticky.
  1093. *
  1094. * Sticky posts should remain at the top of The Loop. If the post ID is not
  1095. * given, then The Loop ID for the current post will be used.
  1096. *
  1097. * @since 2.7.0
  1098. *
  1099. * @param int $post_id Optional. Post ID.
  1100. * @return bool Whether post is sticky.
  1101. */
  1102. function is_sticky($post_id = null) {
  1103. global $id;
  1104. $post_id = absint($post_id);
  1105. if ( !$post_id )
  1106. $post_id = absint($id);
  1107. $stickies = get_option('sticky_posts');
  1108. if ( !is_array($stickies) )
  1109. return false;
  1110. if ( in_array($post_id, $stickies) )
  1111. return true;
  1112. return false;
  1113. }
  1114. /**
  1115. * Sanitize every post field.
  1116. *
  1117. * If the context is 'raw', then the post object or array will get minimal santization of the int fields.
  1118. *
  1119. * @since 2.3.0
  1120. * @uses sanitize_post_field() Used to sanitize the fields.
  1121. *
  1122. * @param object|array $post The Post Object or Array
  1123. * @param string $context Optional, default is 'display'. How to sanitize post fields.
  1124. * @return object|array The now sanitized Post Object or Array (will be the same type as $post)
  1125. */
  1126. function sanitize_post($post, $context = 'display') {
  1127. if ( is_object($post) ) {
  1128. // Check if post already filtered for this context
  1129. if ( isset($post->filter) && $context == $post->filter )
  1130. return $post;
  1131. if ( !isset($post->ID) )
  1132. $post->ID = 0;
  1133. foreach ( array_keys(get_object_vars($post)) as $field )
  1134. $post->$field = sanitize_post_field($field, $post->$field, $post->ID, $context);
  1135. $post->filter = $context;
  1136. } else {
  1137. // Check if post already filtered for this context
  1138. if ( isset($post['filter']) && $context == $post['filter'] )
  1139. return $post;
  1140. if ( !isset($post['ID']) )
  1141. $post['ID'] = 0;
  1142. foreach ( array_keys($post) as $field )
  1143. $post[$field] = sanitize_post_field($field, $post[$field], $post['ID'], $context);
  1144. $post['filter'] = $context;
  1145. }
  1146. return $post;
  1147. }
  1148. /**
  1149. * Sanitize post field based on context.
  1150. *
  1151. * Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and 'js'. The
  1152. * 'display' context is used by default. 'attribute' and 'js' contexts are treated like 'display'
  1153. * when calling filters.
  1154. *
  1155. * @since 2.3.0
  1156. * @uses apply_filters() Calls 'edit_$field' and '${field_no_prefix}_edit_pre' passing $value and
  1157. * $post_id if $context == 'edit' and field name prefix == 'post_'.
  1158. *
  1159. * @uses apply_filters() Calls 'edit_post_$field' passing $value and $post_id if $context == 'db'.
  1160. * @uses apply_filters() Calls 'pre_$field' passing $value if $context == 'db' and field name prefix == 'post_'.
  1161. * @uses apply_filters() Calls '${field}_pre' passing $value if $context == 'db' and field name prefix != 'post_'.
  1162. *
  1163. * @uses apply_filters() Calls '$field' passing $value, $post_id and $context if $context == anything
  1164. * other than 'raw', 'edit' and 'db' and field name prefix == 'post_'.
  1165. * @uses apply_filters() Calls 'post_$field' passing $value if $context == anything other than 'raw',
  1166. * 'edit' and 'db' and field name prefix != 'post_'.
  1167. *
  1168. * @param string $field The Post Object field name.
  1169. * @param mixed $value The Post Object value.
  1170. * @param int $post_id Post ID.
  1171. * @param string $context How to sanitize post fields. Looks for 'raw', 'edit', 'db', 'display',
  1172. * 'attribute' and 'js'.
  1173. * @return mixed Sanitized value.
  1174. */
  1175. function sanitize_post_field($field, $value, $post_id, $context) {
  1176. $int_fields = array('ID', 'post_parent', 'menu_order');
  1177. if ( in_array($field, $int_fields) )
  1178. $value = (int) $value;
  1179. // Fields which contain arrays of ints.
  1180. $array_int_fields = array( 'ancestors' );
  1181. if ( in_array($field, $array_int_fields) ) {
  1182. $value = array_map( 'absint', $value);
  1183. return $value;
  1184. }
  1185. if ( 'raw' == $context )
  1186. return $value;
  1187. $prefixed = false;
  1188. if ( false !== strpos($field, 'post_') ) {
  1189. $prefixed = true;
  1190. $field_no_prefix = str_replace('post_', '', $field);
  1191. }
  1192. if ( 'edit' == $context ) {
  1193. $format_to_edit = array('post_content', 'post_excerpt', 'post_title', 'post_password');
  1194. if ( $prefixed ) {
  1195. $value = apply_filters("edit_$field", $value, $post_id);
  1196. // Old school
  1197. $value = apply_filters("${field_no_prefix}_edit_pre", $value, $post_id);
  1198. } else {
  1199. $value = apply_filters("edit_post_$field", $value, $post_id);
  1200. }
  1201. if ( in_array($field, $format_to_edit) ) {
  1202. if ( 'post_content' == $field )
  1203. $value = format_to_edit($value, user_can_richedit());
  1204. else
  1205. $value = format_to_edit($value);
  1206. } else {
  1207. $value = esc_attr($value);
  1208. }
  1209. } else if ( 'db' == $context ) {
  1210. if ( $prefixed ) {
  1211. $value = apply_filters("pre_$field", $value);
  1212. $value = apply_filters("${field_no_prefix}_save_pre", $value);
  1213. } else {
  1214. $value = apply_filters("pre_post_$field", $value);
  1215. $value = apply_filters("${field}_pre", $value);
  1216. }
  1217. } else {
  1218. // Use display filters by default.
  1219. if ( $prefixed )
  1220. $value = apply_filters($field, $value, $post_id, $context);
  1221. else
  1222. $value = apply_filters("post_$field", $value, $post_id, $context);
  1223. }
  1224. if ( 'attribute' == $context )
  1225. $value = esc_attr($value);
  1226. else if ( 'js' == $context )
  1227. $value = esc_js($value);
  1228. return $value;
  1229. }
  1230. /**
  1231. * Make a post sticky.
  1232. *
  1233. * Sticky posts should be displayed at the top of the front page.
  1234. *
  1235. * @since 2.7.0
  1236. *
  1237. * @param int $post_id Post ID.
  1238. */
  1239. function stick_post($post_id) {
  1240. $stickies = get_option('sticky_posts');
  1241. if ( !is_array($stickies) )
  1242. $stickies = array($post_id);
  1243. if ( ! in_array($post_id, $stickies) )
  1244. $stickies[] = $post_id;
  1245. update_option('sticky_posts', $stickies);
  1246. }
  1247. /**
  1248. * Unstick a post.
  1249. *
  1250. * Sticky posts should be displayed at the top of the front page.
  1251. *
  1252. * @since 2.7.0
  1253. *
  1254. * @param int $post_id Post ID.
  1255. */
  1256. function unstick_post($post_id) {
  1257. $stickies = get_option('sticky_posts');
  1258. if ( !is_array($stickies) )
  1259. return;
  1260. if ( ! in_array($post_id, $stickies) )
  1261. return;
  1262. $offset = array_search($post_id, $stickies);
  1263. if ( false === $offset )
  1264. return;
  1265. array_splice($stickies, $offset, 1);
  1266. update_option('sticky_posts', $stickies);
  1267. }
  1268. /**
  1269. * Count number of posts of a post type and is user has permissions to view.
  1270. *
  1271. * This function provides an efficient method of finding the amount of post's
  1272. * type a blog has. Another method is to count the amount of items in
  1273. * get_posts(), but that method has a lot of overhead with doing so. Therefore,
  1274. * when developing for 2.5+, use this function instead.
  1275. *
  1276. * The $perm parameter checks for 'readable' value and if the user can read
  1277. * private posts, it will display that for the user that is signed in.
  1278. *
  1279. * @since 2.5.0
  1280. * @link http://codex.wordpress.org/Template_Tags/wp_count_posts
  1281. *
  1282. * @param string $type Optional. Post type to retrieve count
  1283. * @param string $perm Optional. 'readable' or empty.
  1284. * @return object Number of posts for each status
  1285. */
  1286. function wp_count_posts( $type = 'post', $perm = '' ) {
  1287. global $wpdb;
  1288. $user = wp_get_current_user();
  1289. $cache_key = $type;
  1290. $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s";
  1291. if ( 'readable' == $perm && is_user_logged_in() ) {
  1292. $post_type_object = get_post_type_object($type);
  1293. if ( !current_user_can("read_private_{$post_type_object->capability_type}s") ) {
  1294. $cache_key .= '_' . $perm . '_' . $user->ID;
  1295. $query .= " AND (post_status != 'private' OR ( post_author = '$user->ID' AND post_status = 'private' ))";
  1296. }
  1297. }
  1298. $query .= ' GROUP BY post_status';
  1299. $count = wp_cache_get($cache_key, 'counts');
  1300. if ( false !== $count )
  1301. return $count;
  1302. $count = $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A );
  1303. $stats = array();
  1304. foreach ( get_post_stati() as $state )
  1305. $stats[$state] = 0;
  1306. foreach ( (array) $count as

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