PageRenderTime 476ms CodeModel.GetById 201ms app.highlight 129ms RepoModel.GetById 72ms app.codeStats 3ms

/wp-includes/post.php

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

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