PageRenderTime 336ms CodeModel.GetById 221ms app.highlight 54ms RepoModel.GetById 16ms app.codeStats 2ms

/wp-includes/post.php

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

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