PageRenderTime 70ms CodeModel.GetById 34ms app.highlight 25ms RepoModel.GetById 0ms app.codeStats 1ms

/Web/wp-includes/post.php

https://bitbucket.org/jimjenkins5/blog
PHP | 5350 lines | 2592 code | 730 blank | 2028 comment | 745 complexity | 8f2ae0631d56193fe80f48c728a995e0 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, AGPL-1.0, LGPL-2.1

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

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

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