PageRenderTime 511ms CodeModel.GetById 136ms app.highlight 190ms RepoModel.GetById 111ms app.codeStats 3ms

/APP/wp-includes/post.php

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

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