PageRenderTime 537ms CodeModel.GetById 141ms app.highlight 170ms RepoModel.GetById 131ms app.codeStats 3ms

/wp-includes/post.php

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

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