web/wp-includes/post.php
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
    50 		'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
    50 		'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ),
    51 	) );
    51 	) );
    52 
    52 
    53 	register_post_type( 'attachment', array(
    53 	register_post_type( 'attachment', array(
    54 		'labels' => array(
    54 		'labels' => array(
    55 			'name' => __( 'Media' ),
    55 			'name' => _x('Media', 'post type general name'),
    56 			'edit_item' => __( 'Edit Media' ),
    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' ),
    57 		),
    60 		),
    58 		'public' => true,
    61 		'public' => true,
    59 		'show_ui' => false,
    62 		'show_ui' => true,
    60 		'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
    63 		'_builtin' => true, /* internal use only. don't use this when registering your own post type. */
    61 		'_edit_link' => 'media.php?attachment_id=%d', /* internal use only. don't use this when registering your own post type. */
    64 		'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */
    62 		'capability_type' => 'post',
    65 		'capability_type' => 'post',
       
    66 		'capabilities' => array(
       
    67 			'create_posts' => 'upload_files',
       
    68 		),
    63 		'map_meta_cap' => true,
    69 		'map_meta_cap' => true,
    64 		'hierarchical' => false,
    70 		'hierarchical' => false,
    65 		'rewrite' => false,
    71 		'rewrite' => false,
    66 		'query_var' => false,
    72 		'query_var' => false,
    67 		'show_in_nav_menus' => false,
    73 		'show_in_nav_menus' => false,
    68 		'delete_with_user' => true,
    74 		'delete_with_user' => true,
    69 		'supports' => array( 'comments', 'author' ),
    75 		'supports' => array( 'title', 'author', 'comments' ),
    70 	) );
    76 	) );
    71 
    77 
    72 	register_post_type( 'revision', array(
    78 	register_post_type( 'revision', array(
    73 		'labels' => array(
    79 		'labels' => array(
    74 			'name' => __( 'Revisions' ),
    80 			'name' => __( 'Revisions' ),
   202 function update_attached_file( $attachment_id, $file ) {
   208 function update_attached_file( $attachment_id, $file ) {
   203 	if ( !get_post( $attachment_id ) )
   209 	if ( !get_post( $attachment_id ) )
   204 		return false;
   210 		return false;
   205 
   211 
   206 	$file = apply_filters( 'update_attached_file', $file, $attachment_id );
   212 	$file = apply_filters( 'update_attached_file', $file, $attachment_id );
   207 	$file = _wp_relative_upload_path($file);
   213 	if ( $file = _wp_relative_upload_path( $file ) )
   208 
   214 		return update_post_meta( $attachment_id, '_wp_attached_file', $file );
   209 	return update_post_meta( $attachment_id, '_wp_attached_file', $file );
   215 	else
       
   216 		return delete_post_meta( $attachment_id, '_wp_attached_file' );
   210 }
   217 }
   211 
   218 
   212 /**
   219 /**
   213  * Return relative path to an uploaded file.
   220  * Return relative path to an uploaded file.
   214  *
   221  *
   221  * @return string relative path on success, unchanged path on failure.
   228  * @return string relative path on success, unchanged path on failure.
   222  */
   229  */
   223 function _wp_relative_upload_path( $path ) {
   230 function _wp_relative_upload_path( $path ) {
   224 	$new_path = $path;
   231 	$new_path = $path;
   225 
   232 
   226 	if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) {
   233 	$uploads = wp_upload_dir();
   227 		if ( 0 === strpos($new_path, $uploads['basedir']) ) {
   234 	if ( 0 === strpos( $new_path, $uploads['basedir'] ) ) {
   228 				$new_path = str_replace($uploads['basedir'], '', $new_path);
   235 			$new_path = str_replace( $uploads['basedir'], '', $new_path );
   229 				$new_path = ltrim($new_path, '/');
   236 			$new_path = ltrim( $new_path, '/' );
   230 		}
       
   231 	}
   237 	}
   232 
   238 
   233 	return apply_filters( '_wp_relative_upload_path', $new_path, $path );
   239 	return apply_filters( '_wp_relative_upload_path', $new_path, $path );
   234 }
   240 }
   235 
   241 
   370  *
   376  *
   371  * @since 1.5.1
   377  * @since 1.5.1
   372  * @uses $wpdb
   378  * @uses $wpdb
   373  * @link http://codex.wordpress.org/Function_Reference/get_post
   379  * @link http://codex.wordpress.org/Function_Reference/get_post
   374  *
   380  *
   375  * @param int|object $post Post ID or post object.
   381  * @param int|object $post Post ID or post object. Optional, default is the current post from the loop.
   376  * @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
   382  * @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
   377  * @param string $filter Optional, default is raw.
   383  * @param string $filter Optional, default is raw.
   378  * @return mixed Post data
   384  * @return WP_Post|null WP_Post on success or null on failure
   379  */
   385  */
   380 function &get_post(&$post, $output = OBJECT, $filter = 'raw') {
   386 function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) {
   381 	global $wpdb;
   387 	if ( empty( $post ) && isset( $GLOBALS['post'] ) )
   382 	$null = null;
   388 		$post = $GLOBALS['post'];
   383 
   389 
   384 	if ( empty($post) ) {
   390 	if ( is_a( $post, 'WP_Post' ) ) {
   385 		if ( isset($GLOBALS['post']) )
   391 		$_post = $post;
   386 			$_post = & $GLOBALS['post'];
   392 	} elseif ( is_object( $post ) ) {
       
   393 		if ( empty( $post->filter ) ) {
       
   394 			$_post = sanitize_post( $post, 'raw' );
       
   395 			$_post = new WP_Post( $_post );
       
   396 		} elseif ( 'raw' == $post->filter ) {
       
   397 			$_post = new WP_Post( $post );
       
   398 		} else {
       
   399 			$_post = WP_Post::get_instance( $post->ID );
       
   400 		}
       
   401 	} else {
       
   402 		$_post = WP_Post::get_instance( $post );
       
   403 	}
       
   404 
       
   405 	if ( ! $_post )
       
   406 		return null;
       
   407 
       
   408 	$_post = $_post->filter( $filter );
       
   409 
       
   410 	if ( $output == ARRAY_A )
       
   411 		return $_post->to_array();
       
   412 	elseif ( $output == ARRAY_N )
       
   413 		return array_values( $_post->to_array() );
       
   414 
       
   415 	return $_post;
       
   416 }
       
   417 
       
   418 /**
       
   419  * WordPress Post class.
       
   420  *
       
   421  * @since 3.5.0
       
   422  *
       
   423  */
       
   424 final class WP_Post {
       
   425 
       
   426 	/**
       
   427 	 *
       
   428 	 * @var int
       
   429 	 */
       
   430 	public $ID;
       
   431 
       
   432 	/**
       
   433 	 *
       
   434 	 * @var int
       
   435 	 */
       
   436 	public $post_author = 0;
       
   437 
       
   438 	/**
       
   439 	 *
       
   440 	 * @var string
       
   441 	 */
       
   442 	public $post_date = '0000-00-00 00:00:00';
       
   443 
       
   444 	/**
       
   445 	 *
       
   446 	 * @var string
       
   447 	 */
       
   448 	public $post_date_gmt = '0000-00-00 00:00:00';
       
   449 
       
   450 	/**
       
   451 	 *
       
   452 	 * @var string
       
   453 	 */
       
   454 	public $post_content = '';
       
   455 
       
   456 	/**
       
   457 	 *
       
   458 	 * @var string
       
   459 	 */
       
   460 	public $post_title = '';
       
   461 
       
   462 	/**
       
   463 	 *
       
   464 	 * @var string
       
   465 	 */
       
   466 	public $post_excerpt = '';
       
   467 
       
   468 	/**
       
   469 	 *
       
   470 	 * @var string
       
   471 	 */
       
   472 	public $post_status = 'publish';
       
   473 
       
   474 	/**
       
   475 	 *
       
   476 	 * @var string
       
   477 	 */
       
   478 	public $comment_status = 'open';
       
   479 
       
   480 	/**
       
   481 	 *
       
   482 	 * @var string
       
   483 	 */
       
   484 	public $ping_status = 'open';
       
   485 
       
   486 	/**
       
   487 	 *
       
   488 	 * @var string
       
   489 	 */
       
   490 	public $post_password = '';
       
   491 
       
   492 	/**
       
   493 	 *
       
   494 	 * @var string
       
   495 	 */
       
   496 	public $post_name = '';
       
   497 
       
   498 	/**
       
   499 	 *
       
   500 	 * @var string
       
   501 	 */
       
   502 	public $to_ping = '';
       
   503 
       
   504 	/**
       
   505 	 *
       
   506 	 * @var string
       
   507 	 */
       
   508 	public $pinged = '';
       
   509 
       
   510 	/**
       
   511 	 *
       
   512 	 * @var string
       
   513 	 */
       
   514 	public $post_modified = '0000-00-00 00:00:00';
       
   515 
       
   516 	/**
       
   517 	 *
       
   518 	 * @var string
       
   519 	 */
       
   520 	public $post_modified_gmt = '0000-00-00 00:00:00';
       
   521 
       
   522 	/**
       
   523 	 *
       
   524 	 * @var string
       
   525 	 */
       
   526 	public $post_content_filtered = '';
       
   527 
       
   528 	/**
       
   529 	 *
       
   530 	 * @var int
       
   531 	 */
       
   532 	public $post_parent = 0;
       
   533 
       
   534 	/**
       
   535 	 *
       
   536 	 * @var string
       
   537 	 */
       
   538 	public $guid = '';
       
   539 
       
   540 	/**
       
   541 	 *
       
   542 	 * @var int
       
   543 	 */
       
   544 	public $menu_order = 0;
       
   545 
       
   546 	/**
       
   547 	 *
       
   548 	 * @var string
       
   549 	 */
       
   550 	public $post_type = 'post';
       
   551 
       
   552 	/**
       
   553 	 *
       
   554 	 * @var string
       
   555 	 */
       
   556 	public $post_mime_type = '';
       
   557 
       
   558 	/**
       
   559 	 *
       
   560 	 * @var int
       
   561 	 */
       
   562 	public $comment_count = 0;
       
   563 
       
   564 	/**
       
   565 	 *
       
   566 	 * @var string
       
   567 	 */
       
   568 	public $filter;
       
   569 
       
   570 	public static function get_instance( $post_id ) {
       
   571 		global $wpdb;
       
   572 
       
   573 		$post_id = (int) $post_id;
       
   574 		if ( ! $post_id )
       
   575 			return false;
       
   576 
       
   577 		$_post = wp_cache_get( $post_id, 'posts' );
       
   578 
       
   579 		if ( ! $_post ) {
       
   580 			$_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id ) );
       
   581 
       
   582 			if ( ! $_post )
       
   583 				return false;
       
   584 
       
   585 			$_post = sanitize_post( $_post, 'raw' );
       
   586 			wp_cache_add( $_post->ID, $_post, 'posts' );
       
   587 		} elseif ( empty( $_post->filter ) ) {
       
   588 			$_post = sanitize_post( $_post, 'raw' );
       
   589 		}
       
   590 
       
   591 		return new WP_Post( $_post );
       
   592 	}
       
   593 
       
   594 	public function __construct( $post ) {
       
   595 		foreach ( get_object_vars( $post ) as $key => $value )
       
   596 			$this->$key = $value;
       
   597 	}
       
   598 
       
   599 	public function __isset( $key ) {
       
   600 		if ( 'ancestors' == $key )
       
   601 			return true;
       
   602 
       
   603 		if ( 'page_template' == $key )
       
   604 			return ( 'page' == $this->post_type );
       
   605 
       
   606 		if ( 'post_category' == $key )
       
   607 		   return true;
       
   608 
       
   609 		if ( 'tags_input' == $key )
       
   610 		   return true;
       
   611 
       
   612 		return metadata_exists( 'post', $this->ID, $key );
       
   613 	}
       
   614 
       
   615 	public function __get( $key ) {
       
   616 		if ( 'page_template' == $key && $this->__isset( $key ) ) {
       
   617 			return get_post_meta( $this->ID, '_wp_page_template', true );
       
   618 		}
       
   619 
       
   620 		if ( 'post_category' == $key ) {
       
   621 			if ( is_object_in_taxonomy( $this->post_type, 'category' ) )
       
   622 				$terms = get_the_terms( $this, 'category' );
       
   623 
       
   624 			if ( empty( $terms ) )
       
   625 				return array();
       
   626 
       
   627 			return wp_list_pluck( $terms, 'term_id' );
       
   628 		}
       
   629 
       
   630 		if ( 'tags_input' == $key ) {
       
   631 			if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) )
       
   632 				$terms = get_the_terms( $this, 'post_tag' );
       
   633 
       
   634 			if ( empty( $terms ) )
       
   635 				return array();
       
   636 
       
   637 			return wp_list_pluck( $terms, 'name' );
       
   638 		}
       
   639 
       
   640 		// Rest of the values need filtering
       
   641 
       
   642 		if ( 'ancestors' == $key )
       
   643 			$value = get_post_ancestors( $this );
   387 		else
   644 		else
   388 			return $null;
   645 			$value = get_post_meta( $this->ID, $key, true );
   389 	} elseif ( is_object($post) && empty($post->filter) ) {
   646 
   390 		_get_post_ancestors($post);
   647 		if ( $this->filter )
   391 		$_post = sanitize_post($post, 'raw');
   648 			$value = sanitize_post_field( $key, $value, $this->ID, $this->filter );
   392 		wp_cache_add($post->ID, $_post, 'posts');
   649 
   393 	} elseif ( is_object($post) && 'raw' == $post->filter ) {
   650 		return $value;
   394 		$_post = $post;
   651 	}
   395 	} else {
   652 
   396 		if ( is_object($post) )
   653 	public function filter( $filter ) {
   397 			$post_id = $post->ID;
   654 		if ( $this->filter == $filter )
   398 		else
   655 			return $this;
   399 			$post_id = $post;
   656 
   400 
   657 		if ( $filter == 'raw' )
   401 		$post_id = (int) $post_id;
   658 			return self::get_instance( $this->ID );
   402 		if ( ! $_post = wp_cache_get($post_id, 'posts') ) {
   659 
   403 			$_post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id));
   660 		return sanitize_post( $this, $filter );
   404 			if ( ! $_post )
   661 	}
   405 				return $null;
   662 
   406 			_get_post_ancestors($_post);
   663 	public function to_array() {
   407 			$_post = sanitize_post($_post, 'raw');
   664 		$post = get_object_vars( $this );
   408 			wp_cache_add($_post->ID, $_post, 'posts');
   665 
       
   666 		foreach ( array( 'ancestors', 'page_template', 'post_category', 'tags_input' ) as $key ) {
       
   667 			if ( $this->__isset( $key ) )
       
   668 				$post[ $key ] = $this->__get( $key );
   409 		}
   669 		}
   410 	}
   670 
   411 
   671 		return $post;
   412 	if ($filter != 'raw')
       
   413 		$_post = sanitize_post($_post, $filter);
       
   414 
       
   415 	if ( $output == OBJECT ) {
       
   416 		return $_post;
       
   417 	} elseif ( $output == ARRAY_A ) {
       
   418 		$__post = get_object_vars($_post);
       
   419 		return $__post;
       
   420 	} elseif ( $output == ARRAY_N ) {
       
   421 		$__post = array_values(get_object_vars($_post));
       
   422 		return $__post;
       
   423 	} else {
       
   424 		return $_post;
       
   425 	}
   672 	}
   426 }
   673 }
   427 
   674 
   428 /**
   675 /**
   429  * Retrieve ancestors of a post.
   676  * Retrieve ancestors of a post.
   431  * @since 2.5.0
   678  * @since 2.5.0
   432  *
   679  *
   433  * @param int|object $post Post ID or post object
   680  * @param int|object $post Post ID or post object
   434  * @return array Ancestor IDs or empty array if none are found.
   681  * @return array Ancestor IDs or empty array if none are found.
   435  */
   682  */
   436 function get_post_ancestors($post) {
   683 function get_post_ancestors( $post ) {
   437 	$post = get_post($post);
   684 	if ( ! $post )
   438 
   685 		return false;
   439 	if ( ! isset( $post->ancestors ) )
   686 
   440 		_get_post_ancestors( $post );
   687 	$post = get_post( $post );
   441 
   688 
   442 	if ( ! empty( $post->ancestors ) )
   689 	if ( empty( $post->post_parent ) || $post->post_parent == $post->ID )
   443 		return $post->ancestors;
   690 		return array();
   444 
   691 
   445 	return array();
   692 	$ancestors = array();
       
   693 
       
   694 	$id = $ancestors[] = $post->post_parent;
       
   695 
       
   696 	while ( $ancestor = get_post( $id ) ) {
       
   697 		// Loop detection: If the ancestor has been seen before, break.
       
   698 		if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors ) )
       
   699 			break;
       
   700 
       
   701 		$id = $ancestors[] = $ancestor->post_parent;
       
   702 	}
       
   703 
       
   704 	return $ancestors;
   446 }
   705 }
   447 
   706 
   448 /**
   707 /**
   449  * Retrieve data from a post field based on Post ID.
   708  * Retrieve data from a post field based on Post ID.
   450  *
   709  *
   458  * @uses sanitize_post_field() See for possible $context values.
   717  * @uses sanitize_post_field() See for possible $context values.
   459  *
   718  *
   460  * @param string $field Post field name
   719  * @param string $field Post field name
   461  * @param id $post Post ID
   720  * @param id $post Post ID
   462  * @param string $context Optional. How to filter the field. Default is display.
   721  * @param string $context Optional. How to filter the field. Default is display.
   463  * @return WP_Error|string Value in post field or WP_Error on failure
   722  * @return bool|string False on failure or returns the value in post field
   464  */
   723  */
   465 function get_post_field( $field, $post, $context = 'display' ) {
   724 function get_post_field( $field, $post, $context = 'display' ) {
   466 	$post = (int) $post;
       
   467 	$post = get_post( $post );
   725 	$post = get_post( $post );
   468 
   726 
   469 	if ( is_wp_error($post) )
   727 	if ( !$post )
   470 		return $post;
       
   471 
       
   472 	if ( !is_object($post) )
       
   473 		return '';
   728 		return '';
   474 
   729 
   475 	if ( !isset($post->$field) )
   730 	if ( !isset($post->$field) )
   476 		return '';
   731 		return '';
   477 
   732 
   488  *
   743  *
   489  * @param int $ID Optional. Post ID.
   744  * @param int $ID Optional. Post ID.
   490  * @return bool|string False on failure or returns the mime type
   745  * @return bool|string False on failure or returns the mime type
   491  */
   746  */
   492 function get_post_mime_type($ID = '') {
   747 function get_post_mime_type($ID = '') {
   493 	$post = & get_post($ID);
   748 	$post = get_post($ID);
   494 
   749 
   495 	if ( is_object($post) )
   750 	if ( is_object($post) )
   496 		return $post->post_mime_type;
   751 		return $post->post_mime_type;
   497 
   752 
   498 	return false;
   753 	return false;
   669 
   924 
   670 	if (!is_array($wp_post_statuses))
   925 	if (!is_array($wp_post_statuses))
   671 		$wp_post_statuses = array();
   926 		$wp_post_statuses = array();
   672 
   927 
   673 	// Args prefixed with an underscore are reserved for internal use.
   928 	// Args prefixed with an underscore are reserved for internal use.
   674 	$defaults = array('label' => false, 'label_count' => false, 'exclude_from_search' => null, '_builtin' => false, '_edit_link' => 'post.php?post=%d', 'capability_type' => 'post', 'hierarchical' => false, 'public' => null, 'internal' => null, 'protected' => null, 'private' => null, 'show_in_admin_all' => null, 'publicly_queryable' => null, 'show_in_admin_status_list' => null, 'show_in_admin_all_list' => null, 'single_view_cap' => null);
   929 	$defaults = array(
       
   930 		'label' => false,
       
   931 		'label_count' => false,
       
   932 		'exclude_from_search' => null,
       
   933 		'_builtin' => false,
       
   934 		'public' => null,
       
   935 		'internal' => null,
       
   936 		'protected' => null,
       
   937 		'private' => null,
       
   938 		'publicly_queryable' => null,
       
   939 		'show_in_admin_status_list' => null,
       
   940 		'show_in_admin_all_list' => null,
       
   941 	);
   675 	$args = wp_parse_args($args, $defaults);
   942 	$args = wp_parse_args($args, $defaults);
   676 	$args = (object) $args;
   943 	$args = (object) $args;
   677 
   944 
   678 	$post_status = sanitize_key($post_status);
   945 	$post_status = sanitize_key($post_status);
   679 	$args->name = $post_status;
   946 	$args->name = $post_status;
   701 
   968 
   702 	if ( null === $args->show_in_admin_all_list )
   969 	if ( null === $args->show_in_admin_all_list )
   703 		$args->show_in_admin_all_list = !$args->internal;
   970 		$args->show_in_admin_all_list = !$args->internal;
   704 
   971 
   705 	if ( null === $args->show_in_admin_status_list )
   972 	if ( null === $args->show_in_admin_status_list )
   706 			$args->show_in_admin_status_list = !$args->internal;
   973 		$args->show_in_admin_status_list = !$args->internal;
   707 
       
   708 	if ( null === $args->single_view_cap )
       
   709 		$args->single_view_cap = $args->public ? '' : 'edit';
       
   710 
   974 
   711 	if ( false === $args->label )
   975 	if ( false === $args->label )
   712 		$args->label = $post_status;
   976 		$args->label = $post_status;
   713 
   977 
   714 	if ( false === $args->label_count )
   978 	if ( false === $args->label_count )
   802  *
  1066  *
   803  * @since 2.1.0
  1067  * @since 2.1.0
   804  *
  1068  *
   805  * @uses $post The Loop current post global
  1069  * @uses $post The Loop current post global
   806  *
  1070  *
   807  * @param mixed $the_post Optional. Post object or post ID.
  1071  * @param mixed $post Optional. Post object or post ID.
   808  * @return bool|string post type or false on failure.
  1072  * @return bool|string post type or false on failure.
   809  */
  1073  */
   810 function get_post_type( $the_post = false ) {
  1074 function get_post_type( $post = null ) {
   811 	global $post;
  1075 	if ( $post = get_post( $post ) )
   812 
  1076 		return $post->post_type;
   813 	if ( false === $the_post )
       
   814 		$the_post = $post;
       
   815 	elseif ( is_numeric($the_post) )
       
   816 		$the_post = get_post($the_post);
       
   817 
       
   818 	if ( is_object($the_post) )
       
   819 		return $the_post->post_type;
       
   820 
  1077 
   821 	return false;
  1078 	return false;
   822 }
  1079 }
   823 
  1080 
   824 /**
  1081 /**
  1018 		$args->capability_type = $args->capability_type[0];
  1275 		$args->capability_type = $args->capability_type[0];
  1019 
  1276 
  1020 	if ( ! empty($args->supports) ) {
  1277 	if ( ! empty($args->supports) ) {
  1021 		add_post_type_support($post_type, $args->supports);
  1278 		add_post_type_support($post_type, $args->supports);
  1022 		unset($args->supports);
  1279 		unset($args->supports);
  1023 	} else {
  1280 	} elseif ( false !== $args->supports ) {
  1024 		// Add default features
  1281 		// Add default features
  1025 		add_post_type_support($post_type, array('title', 'editor'));
  1282 		add_post_type_support($post_type, array('title', 'editor'));
  1026 	}
  1283 	}
  1027 
  1284 
  1028 	if ( false !== $args->query_var && !empty($wp) ) {
  1285 	if ( false !== $args->query_var && !empty($wp) ) {
  1029 		if ( true === $args->query_var )
  1286 		if ( true === $args->query_var )
  1030 			$args->query_var = $post_type;
  1287 			$args->query_var = $post_type;
  1031 		$args->query_var = sanitize_title_with_dashes($args->query_var);
  1288 		else
       
  1289 			$args->query_var = sanitize_title_with_dashes($args->query_var);
  1032 		$wp->add_query_var($args->query_var);
  1290 		$wp->add_query_var($args->query_var);
  1033 	}
  1291 	}
  1034 
  1292 
  1035 	if ( false !== $args->rewrite && ( is_admin() || '' != get_option('permalink_structure') ) ) {
  1293 	if ( false !== $args->rewrite && ( is_admin() || '' != get_option('permalink_structure') ) ) {
  1036 		if ( ! is_array( $args->rewrite ) )
  1294 		if ( ! is_array( $args->rewrite ) )
  1180 		$default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
  1438 		$default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping );
  1181 	}
  1439 	}
  1182 
  1440 
  1183 	$capabilities = array_merge( $default_capabilities, $args->capabilities );
  1441 	$capabilities = array_merge( $default_capabilities, $args->capabilities );
  1184 
  1442 
       
  1443 	// Post creation capability simply maps to edit_posts by default:
       
  1444 	if ( ! isset( $capabilities['create_posts'] ) )
       
  1445 		$capabilities['create_posts'] = $capabilities['edit_posts'];
       
  1446 
  1185 	// Remember meta capabilities for future reference.
  1447 	// Remember meta capabilities for future reference.
  1186 	if ( $args->map_meta_cap )
  1448 	if ( $args->map_meta_cap )
  1187 		_post_type_meta_capabilities( $capabilities );
  1449 		_post_type_meta_capabilities( $capabilities );
  1188 
  1450 
  1189 	return (object) $capabilities;
  1451 	return (object) $capabilities;
  1243 		'not_found_in_trash' => array( __('No posts found in Trash.'), __('No pages found in Trash.') ),
  1505 		'not_found_in_trash' => array( __('No posts found in Trash.'), __('No pages found in Trash.') ),
  1244 		'parent_item_colon' => array( null, __('Parent Page:') ),
  1506 		'parent_item_colon' => array( null, __('Parent Page:') ),
  1245 		'all_items' => array( __( 'All Posts' ), __( 'All Pages' ) )
  1507 		'all_items' => array( __( 'All Posts' ), __( 'All Pages' ) )
  1246 	);
  1508 	);
  1247 	$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
  1509 	$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
  1248 	return _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
  1510 
       
  1511 	$labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults );
       
  1512 
       
  1513 	$post_type = $post_type_object->name;
       
  1514 	return apply_filters( "post_type_labels_{$post_type}", $labels );
  1249 }
  1515 }
  1250 
  1516 
  1251 /**
  1517 /**
  1252  * Builds an object with custom-something object (post type, taxonomy) labels out of a custom-something object
  1518  * Builds an object with custom-something object (post type, taxonomy) labels out of a custom-something object
  1253  *
  1519  *
  1666  * If the context is 'raw', then the post object or array will get minimal santization of the int fields.
  1932  * If the context is 'raw', then the post object or array will get minimal santization of the int fields.
  1667  *
  1933  *
  1668  * @since 2.3.0
  1934  * @since 2.3.0
  1669  * @uses sanitize_post_field() Used to sanitize the fields.
  1935  * @uses sanitize_post_field() Used to sanitize the fields.
  1670  *
  1936  *
  1671  * @param object|array $post The Post Object or Array
  1937  * @param object|WP_Post|array $post The Post Object or Array
  1672  * @param string $context Optional, default is 'display'. How to sanitize post fields.
  1938  * @param string $context Optional, default is 'display'. How to sanitize post fields.
  1673  * @return object|array The now sanitized Post Object or Array (will be the same type as $post)
  1939  * @return object|WP_Post|array The now sanitized Post Object or Array (will be the same type as $post)
  1674  */
  1940  */
  1675 function sanitize_post($post, $context = 'display') {
  1941 function sanitize_post($post, $context = 'display') {
  1676 	if ( is_object($post) ) {
  1942 	if ( is_object($post) ) {
  1677 		// Check if post already filtered for this context
  1943 		// Check if post already filtered for this context
  1678 		if ( isset($post->filter) && $context == $post->filter )
  1944 		if ( isset($post->filter) && $context == $post->filter )
  1915 
  2181 
  1916 	return (object) $stats;
  2182 	return (object) $stats;
  1917 }
  2183 }
  1918 
  2184 
  1919 /**
  2185 /**
       
  2186  * Get default post mime types
       
  2187  *
       
  2188  * @since 2.9.0
       
  2189  *
       
  2190  * @return array
       
  2191  */
       
  2192 function get_post_mime_types() {
       
  2193 	$post_mime_types = array(	//	array( adj, noun )
       
  2194 		'image' => array(__('Images'), __('Manage Images'), _n_noop('Image <span class="count">(%s)</span>', 'Images <span class="count">(%s)</span>')),
       
  2195 		'audio' => array(__('Audio'), __('Manage Audio'), _n_noop('Audio <span class="count">(%s)</span>', 'Audio <span class="count">(%s)</span>')),
       
  2196 		'video' => array(__('Video'), __('Manage Video'), _n_noop('Video <span class="count">(%s)</span>', 'Video <span class="count">(%s)</span>')),
       
  2197 	);
       
  2198 
       
  2199 	return apply_filters('post_mime_types', $post_mime_types);
       
  2200 }
       
  2201 
       
  2202 /**
  1920  * Check a MIME-Type against a list.
  2203  * Check a MIME-Type against a list.
  1921  *
  2204  *
  1922  * If the wildcard_mime_types parameter is a string, it must be comma separated
  2205  * If the wildcard_mime_types parameter is a string, it must be comma separated
  1923  * list. If the real_mime_types is a string, it is also comma separated to
  2206  * list. If the real_mime_types is a string, it is also comma separated to
  1924  * create the list.
  2207  * create the list.
  2084 	$wpdb->delete( $wpdb->posts, array( 'ID' => $postid ) );
  2367 	$wpdb->delete( $wpdb->posts, array( 'ID' => $postid ) );
  2085 	do_action( 'deleted_post', $postid );
  2368 	do_action( 'deleted_post', $postid );
  2086 
  2369 
  2087 	clean_post_cache( $post );
  2370 	clean_post_cache( $post );
  2088 
  2371 
  2089 	if ( is_post_type_hierarchical( $post->post_type ) ) {
  2372 	if ( is_post_type_hierarchical( $post->post_type ) && $children ) {
  2090 		foreach ( (array) $children as $child )
  2373 		foreach ( $children as $child )
  2091 			clean_post_cache( $child );
  2374 			clean_post_cache( $child );
  2092 	}
  2375 	}
  2093 
  2376 
  2094 	wp_clear_scheduled_hook('publish_future_post', array( $postid ) );
  2377 	wp_clear_scheduled_hook('publish_future_post', array( $postid ) );
  2095 
  2378 
  2113  */
  2396  */
  2114 function wp_trash_post($post_id = 0) {
  2397 function wp_trash_post($post_id = 0) {
  2115 	if ( !EMPTY_TRASH_DAYS )
  2398 	if ( !EMPTY_TRASH_DAYS )
  2116 		return wp_delete_post($post_id, true);
  2399 		return wp_delete_post($post_id, true);
  2117 
  2400 
  2118 	if ( !$post = wp_get_single_post($post_id, ARRAY_A) )
  2401 	if ( !$post = get_post($post_id, ARRAY_A) )
  2119 		return $post;
  2402 		return $post;
  2120 
  2403 
  2121 	if ( $post['post_status'] == 'trash' )
  2404 	if ( $post['post_status'] == 'trash' )
  2122 		return false;
  2405 		return false;
  2123 
  2406 
  2145  *
  2428  *
  2146  * @param int $post_id Post ID.
  2429  * @param int $post_id Post ID.
  2147  * @return mixed False on failure
  2430  * @return mixed False on failure
  2148  */
  2431  */
  2149 function wp_untrash_post($post_id = 0) {
  2432 function wp_untrash_post($post_id = 0) {
  2150 	if ( !$post = wp_get_single_post($post_id, ARRAY_A) )
  2433 	if ( !$post = get_post($post_id, ARRAY_A) )
  2151 		return $post;
  2434 		return $post;
  2152 
  2435 
  2153 	if ( $post['post_status'] != 'trash' )
  2436 	if ( $post['post_status'] != 'trash' )
  2154 		return false;
  2437 		return false;
  2155 
  2438 
  2373 		return $results ? $results : array();
  2656 		return $results ? $results : array();
  2374 	}
  2657 	}
  2375 
  2658 
  2376 	return $results ? $results : false;
  2659 	return $results ? $results : false;
  2377 
  2660 
  2378 }
       
  2379 
       
  2380 /**
       
  2381  * Retrieve a single post, based on post ID.
       
  2382  *
       
  2383  * Has categories in 'post_category' property or key. Has tags in 'tags_input'
       
  2384  * property or key.
       
  2385  *
       
  2386  * @since 1.0.0
       
  2387  *
       
  2388  * @param int $postid Post ID.
       
  2389  * @param string $mode How to return result, either OBJECT, ARRAY_N, or ARRAY_A.
       
  2390  * @return object|array Post object or array holding post contents and information
       
  2391  */
       
  2392 function wp_get_single_post($postid = 0, $mode = OBJECT) {
       
  2393 	$postid = (int) $postid;
       
  2394 
       
  2395 	$post = get_post($postid, $mode);
       
  2396 
       
  2397 	if (
       
  2398 		( OBJECT == $mode && empty( $post->ID ) ) ||
       
  2399 		( OBJECT != $mode && empty( $post['ID'] ) )
       
  2400 	)
       
  2401 		return ( OBJECT == $mode ? null : array() );
       
  2402 
       
  2403 	// Set categories and tags
       
  2404 	if ( $mode == OBJECT ) {
       
  2405 		$post->post_category = array();
       
  2406 		if ( is_object_in_taxonomy($post->post_type, 'category') )
       
  2407 			$post->post_category = wp_get_post_categories($postid);
       
  2408 		$post->tags_input = array();
       
  2409 		if ( is_object_in_taxonomy($post->post_type, 'post_tag') )
       
  2410 			$post->tags_input = wp_get_post_tags($postid, array('fields' => 'names'));
       
  2411 	} else {
       
  2412 		$post['post_category'] = array();
       
  2413 		if ( is_object_in_taxonomy($post['post_type'], 'category') )
       
  2414 			$post['post_category'] = wp_get_post_categories($postid);
       
  2415 		$post['tags_input'] = array();
       
  2416 		if ( is_object_in_taxonomy($post['post_type'], 'post_tag') )
       
  2417 			$post['tags_input'] = wp_get_post_tags($postid, array('fields' => 'names'));
       
  2418 	}
       
  2419 
       
  2420 	return $post;
       
  2421 }
  2661 }
  2422 
  2662 
  2423 /**
  2663 /**
  2424  * Insert a post.
  2664  * Insert a post.
  2425  *
  2665  *
  2544 	}
  2784 	}
  2545 
  2785 
  2546 	// If the post date is empty (due to having been new or a draft) and status is not 'draft' or 'pending', set date to now
  2786 	// If the post date is empty (due to having been new or a draft) and status is not 'draft' or 'pending', set date to now
  2547 	if ( empty($post_date) || '0000-00-00 00:00:00' == $post_date )
  2787 	if ( empty($post_date) || '0000-00-00 00:00:00' == $post_date )
  2548 		$post_date = current_time('mysql');
  2788 		$post_date = current_time('mysql');
       
  2789 
       
  2790 		// validate the date
       
  2791 		$mm = substr( $post_date, 5, 2 );
       
  2792 		$jj = substr( $post_date, 8, 2 );
       
  2793 		$aa = substr( $post_date, 0, 4 );
       
  2794 		$valid_date = wp_checkdate( $mm, $jj, $aa, $post_date );
       
  2795 		if ( !$valid_date ) {
       
  2796 			if ( $wp_error )
       
  2797 				return new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.' ) );
       
  2798 			else
       
  2799 				return 0;
       
  2800 		}
  2549 
  2801 
  2550 	if ( empty($post_date_gmt) || '0000-00-00 00:00:00' == $post_date_gmt ) {
  2802 	if ( empty($post_date_gmt) || '0000-00-00 00:00:00' == $post_date_gmt ) {
  2551 		if ( !in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) )
  2803 		if ( !in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) )
  2552 			$post_date_gmt = get_gmt_from_date($post_date);
  2804 			$post_date_gmt = get_gmt_from_date($post_date);
  2553 		else
  2805 		else
  2665 		}
  2917 		}
  2666 	}
  2918 	}
  2667 
  2919 
  2668 	$current_guid = get_post_field( 'guid', $post_ID );
  2920 	$current_guid = get_post_field( 'guid', $post_ID );
  2669 
  2921 
  2670 	clean_post_cache( $post_ID );
       
  2671 
       
  2672 	// Set GUID
  2922 	// Set GUID
  2673 	if ( !$update && '' == $current_guid )
  2923 	if ( !$update && '' == $current_guid )
  2674 		$wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where );
  2924 		$wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where );
  2675 
  2925 
       
  2926 	clean_post_cache( $post_ID );
       
  2927 
  2676 	$post = get_post($post_ID);
  2928 	$post = get_post($post_ID);
  2677 
  2929 
  2678 	if ( !empty($page_template) && 'page' == $data['post_type'] ) {
  2930 	if ( !empty($page_template) && 'page' == $data['post_type'] ) {
  2679 		$post->page_template = $page_template;
  2931 		$post->page_template = $page_template;
  2680 		$page_templates = get_page_templates();
  2932 		$page_templates = wp_get_theme()->get_page_templates();
  2681 		if ( 'default' != $page_template && !in_array($page_template, $page_templates) ) {
  2933 		if ( 'default' != $page_template && ! isset( $page_templates[ $page_template ] ) ) {
  2682 			if ( $wp_error )
  2934 			if ( $wp_error )
  2683 				return new WP_Error('invalid_page_template', __('The page template is invalid.'));
  2935 				return new WP_Error('invalid_page_template', __('The page template is invalid.'));
  2684 			else
  2936 			else
  2685 				return 0;
  2937 				return 0;
  2686 		}
  2938 		}
  2708  * not be overridden.
  2960  * not be overridden.
  2709  *
  2961  *
  2710  * @since 1.0.0
  2962  * @since 1.0.0
  2711  *
  2963  *
  2712  * @param array|object $postarr Post data. Arrays are expected to be escaped, objects are not.
  2964  * @param array|object $postarr Post data. Arrays are expected to be escaped, objects are not.
  2713  * @return int 0 on failure, Post ID on success.
  2965  * @param bool $wp_error Optional. Allow return of WP_Error on failure.
  2714  */
  2966  * @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success.
  2715 function wp_update_post($postarr = array()) {
  2967  */
       
  2968 function wp_update_post( $postarr = array(), $wp_error = false ) {
  2716 	if ( is_object($postarr) ) {
  2969 	if ( is_object($postarr) ) {
  2717 		// non-escaped post was passed
  2970 		// non-escaped post was passed
  2718 		$postarr = get_object_vars($postarr);
  2971 		$postarr = get_object_vars($postarr);
  2719 		$postarr = add_magic_quotes($postarr);
  2972 		$postarr = add_magic_quotes($postarr);
  2720 	}
  2973 	}
  2721 
  2974 
  2722 	// First, get all of the original fields
  2975 	// First, get all of the original fields
  2723 	$post = wp_get_single_post($postarr['ID'], ARRAY_A);
  2976 	$post = get_post($postarr['ID'], ARRAY_A);
  2724 
  2977 
  2725 	// Escape data pulled from DB.
  2978 	// Escape data pulled from DB.
  2726 	$post = add_magic_quotes($post);
  2979 	$post = add_magic_quotes($post);
  2727 
  2980 
  2728 	// Passed post category list overwrites existing category list if not empty.
  2981 	// Passed post category list overwrites existing category list if not empty.
  2748 	}
  3001 	}
  2749 
  3002 
  2750 	if ($postarr['post_type'] == 'attachment')
  3003 	if ($postarr['post_type'] == 'attachment')
  2751 		return wp_insert_attachment($postarr);
  3004 		return wp_insert_attachment($postarr);
  2752 
  3005 
  2753 	return wp_insert_post($postarr);
  3006 	return wp_insert_post( $postarr, $wp_error );
  2754 }
  3007 }
  2755 
  3008 
  2756 /**
  3009 /**
  2757  * Publish a post by transitioning the post status.
  3010  * Publish a post by transitioning the post status.
  2758  *
  3011  *
  2759  * @since 2.1.0
  3012  * @since 2.1.0
  2760  * @uses $wpdb
  3013  * @uses wp_update_post()
  2761  * @uses do_action() Calls 'edit_post', 'save_post', and 'wp_insert_post' on post_id and post data.
  3014  *
  2762  *
  3015  * @param mixed $post Post ID or object.
  2763  * @param int $post_id Post ID.
  3016  */
  2764  * @return null
  3017 function wp_publish_post( $post ) {
  2765  */
  3018 	if ( ! $post = get_post( $post ) )
  2766 function wp_publish_post($post_id) {
       
  2767 	global $wpdb;
       
  2768 
       
  2769 	$post = get_post($post_id);
       
  2770 
       
  2771 	if ( empty($post) )
       
  2772 		return;
  3019 		return;
  2773 
       
  2774 	if ( 'publish' == $post->post_status )
  3020 	if ( 'publish' == $post->post_status )
  2775 		return;
  3021 		return;
  2776 
  3022 
  2777 	$wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post_id ) );
       
  2778 
       
  2779 	$old_status = $post->post_status;
       
  2780 	$post->post_status = 'publish';
  3023 	$post->post_status = 'publish';
  2781 	wp_transition_post_status('publish', $old_status, $post);
  3024 	wp_update_post( $post );
  2782 
       
  2783 	do_action('edit_post', $post_id, $post);
       
  2784 	do_action('save_post', $post_id, $post);
       
  2785 	do_action('wp_insert_post', $post_id, $post);
       
  2786 }
  3025 }
  2787 
  3026 
  2788 /**
  3027 /**
  2789  * Publish future post and make sure post ID has future post status.
  3028  * Publish future post and make sure post ID has future post status.
  2790  *
  3029  *
  2834 function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) {
  3073 function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) {
  2835 	if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) )
  3074 	if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) )
  2836 		return $slug;
  3075 		return $slug;
  2837 
  3076 
  2838 	global $wpdb, $wp_rewrite;
  3077 	global $wpdb, $wp_rewrite;
       
  3078 
       
  3079 	$original_slug = $slug;
  2839 
  3080 
  2840 	$feeds = $wp_rewrite->feeds;
  3081 	$feeds = $wp_rewrite->feeds;
  2841 	if ( ! is_array( $feeds ) )
  3082 	if ( ! is_array( $feeds ) )
  2842 		$feeds = array();
  3083 		$feeds = array();
  2843 
  3084 
  2855 				$suffix++;
  3096 				$suffix++;
  2856 			} while ( $post_name_check );
  3097 			} while ( $post_name_check );
  2857 			$slug = $alt_post_name;
  3098 			$slug = $alt_post_name;
  2858 		}
  3099 		}
  2859 	} elseif ( in_array( $post_type, $hierarchical_post_types ) ) {
  3100 	} elseif ( in_array( $post_type, $hierarchical_post_types ) ) {
       
  3101 		if ( 'nav_menu_item' == $post_type )
       
  3102 			return $slug;
  2860 		// Page slugs must be unique within their own trees. Pages are in a separate
  3103 		// Page slugs must be unique within their own trees. Pages are in a separate
  2861 		// namespace than posts so page slugs are allowed to overlap post slugs.
  3104 		// namespace than posts so page slugs are allowed to overlap post slugs.
  2862 		$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( '" . implode( "', '", esc_sql( $hierarchical_post_types ) ) . "' ) AND ID != %d AND post_parent = %d LIMIT 1";
  3105 		$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( '" . implode( "', '", esc_sql( $hierarchical_post_types ) ) . "' ) AND ID != %d AND post_parent = %d LIMIT 1";
  2863 		$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID, $post_parent ) );
  3106 		$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID, $post_parent ) );
  2864 
  3107 
  2885 			} while ( $post_name_check );
  3128 			} while ( $post_name_check );
  2886 			$slug = $alt_post_name;
  3129 			$slug = $alt_post_name;
  2887 		}
  3130 		}
  2888 	}
  3131 	}
  2889 
  3132 
  2890 	return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent );
  3133 	return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug );
  2891 }
  3134 }
  2892 
  3135 
  2893 /**
  3136 /**
  2894  * Adds tags to a post.
  3137  * Adds tags to a post.
  2895  *
  3138  *
  2951 	}
  3194 	}
  2952 
  3195 
  2953 	// Hierarchical taxonomies must always pass IDs rather than names so that children with the same
  3196 	// Hierarchical taxonomies must always pass IDs rather than names so that children with the same
  2954 	// names but different parents aren't confused.
  3197 	// names but different parents aren't confused.
  2955 	if ( is_taxonomy_hierarchical( $taxonomy ) ) {
  3198 	if ( is_taxonomy_hierarchical( $taxonomy ) ) {
  2956 		$tags = array_map( 'intval', $tags );
  3199 		$tags = array_unique( array_map( 'intval', $tags ) );
  2957 		$tags = array_unique( $tags );
  3200 	}
  2958 	}
  3201 
  2959 
  3202 	return wp_set_object_terms( $post_id, $tags, $taxonomy, $append );
  2960 	return wp_set_object_terms($post_id, $tags, $taxonomy, $append);
       
  2961 }
  3203 }
  2962 
  3204 
  2963 /**
  3205 /**
  2964  * Set categories for a post.
  3206  * Set categories for a post.
  2965  *
  3207  *
  2984 			$post_categories = array();
  3226 			$post_categories = array();
  2985 	} else if ( 1 == count($post_categories) && '' == reset($post_categories) ) {
  3227 	} else if ( 1 == count($post_categories) && '' == reset($post_categories) ) {
  2986 		return true;
  3228 		return true;
  2987 	}
  3229 	}
  2988 
  3230 
  2989 	if ( !empty($post_categories) ) {
  3231 	return wp_set_post_terms($post_ID, $post_categories, 'category');
  2990 		$post_categories = array_map('intval', $post_categories);
       
  2991 		$post_categories = array_unique($post_categories);
       
  2992 	}
       
  2993 
       
  2994 	return wp_set_object_terms($post_ID, $post_categories, 'category');
       
  2995 }
  3232 }
  2996 
  3233 
  2997 /**
  3234 /**
  2998  * Transition the post status of a post.
  3235  * Transition the post status of a post.
  2999  *
  3236  *
  3125  * @param int $post_id Post ID
  3362  * @param int $post_id Post ID
  3126  */
  3363  */
  3127 function trackback_url_list($tb_list, $post_id) {
  3364 function trackback_url_list($tb_list, $post_id) {
  3128 	if ( ! empty( $tb_list ) ) {
  3365 	if ( ! empty( $tb_list ) ) {
  3129 		// get post data
  3366 		// get post data
  3130 		$postdata = wp_get_single_post($post_id, ARRAY_A);
  3367 		$postdata = get_post($post_id, ARRAY_A);
  3131 
  3368 
  3132 		// import postdata as variables
  3369 		// import postdata as variables
  3133 		extract($postdata, EXTR_SKIP);
  3370 		extract($postdata, EXTR_SKIP);
  3134 
  3371 
  3135 		// form an excerpt
  3372 		// form an excerpt
  3172 }
  3409 }
  3173 
  3410 
  3174 /**
  3411 /**
  3175  * Retrieves page data given a page ID or page object.
  3412  * Retrieves page data given a page ID or page object.
  3176  *
  3413  *
       
  3414  * Use get_post() instead of get_page().
       
  3415  *
  3177  * @since 1.5.1
  3416  * @since 1.5.1
       
  3417  * @deprecated 3.5.0
  3178  *
  3418  *
  3179  * @param mixed $page Page object or page ID. Passed by reference.
  3419  * @param mixed $page Page object or page ID. Passed by reference.
  3180  * @param string $output What to output. OBJECT, ARRAY_A, or ARRAY_N.
  3420  * @param string $output What to output. OBJECT, ARRAY_A, or ARRAY_N.
  3181  * @param string $filter How the return value should be filtered.
  3421  * @param string $filter How the return value should be filtered.
  3182  * @return mixed Page data.
  3422  * @return WP_Post|null WP_Post on success or null on failure
  3183  */
  3423  */
  3184 function &get_page(&$page, $output = OBJECT, $filter = 'raw') {
  3424 function get_page( $page, $output = OBJECT, $filter = 'raw') {
  3185 	$p = get_post($page, $output, $filter);
  3425 	return get_post( $page, $output, $filter );
  3186 	return $p;
       
  3187 }
  3426 }
  3188 
  3427 
  3189 /**
  3428 /**
  3190  * Retrieves a page given its path.
  3429  * Retrieves a page given its path.
  3191  *
  3430  *
  3193  * @uses $wpdb
  3432  * @uses $wpdb
  3194  *
  3433  *
  3195  * @param string $page_path Page path
  3434  * @param string $page_path Page path
  3196  * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT.
  3435  * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT.
  3197  * @param string $post_type Optional. Post type. Default page.
  3436  * @param string $post_type Optional. Post type. Default page.
  3198  * @return mixed Null when complete.
  3437  * @return WP_Post|null WP_Post on success or null on failure
  3199  */
  3438  */
  3200 function get_page_by_path($page_path, $output = OBJECT, $post_type = 'page') {
  3439 function get_page_by_path($page_path, $output = OBJECT, $post_type = 'page') {
  3201 	global $wpdb;
  3440 	global $wpdb;
  3202 
  3441 
  3203 	$page_path = rawurlencode(urldecode($page_path));
  3442 	$page_path = rawurlencode(urldecode($page_path));
  3208 	$parts = array_map( 'sanitize_title_for_query', $parts );
  3447 	$parts = array_map( 'sanitize_title_for_query', $parts );
  3209 
  3448 
  3210 	$in_string = "'". implode( "','", $parts ) . "'";
  3449 	$in_string = "'". implode( "','", $parts ) . "'";
  3211 	$post_type_sql = $post_type;
  3450 	$post_type_sql = $post_type;
  3212 	$wpdb->escape_by_ref( $post_type_sql );
  3451 	$wpdb->escape_by_ref( $post_type_sql );
  3213 	$pages = $wpdb->get_results( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name IN ($in_string) AND (post_type = '$post_type_sql' OR post_type = 'attachment')", OBJECT_K );
  3452 	$pages = $wpdb->get_results( "SELECT ID, post_name, post_parent, post_type FROM $wpdb->posts WHERE post_name IN ($in_string) AND (post_type = '$post_type_sql' OR post_type = 'attachment')", OBJECT_K );
  3214 
  3453 
  3215 	$revparts = array_reverse( $parts );
  3454 	$revparts = array_reverse( $parts );
  3216 
  3455 
  3217 	$foundid = 0;
  3456 	$foundid = 0;
  3218 	foreach ( (array) $pages as $page ) {
  3457 	foreach ( (array) $pages as $page ) {
  3227 				$p = $parent;
  3466 				$p = $parent;
  3228 			}
  3467 			}
  3229 
  3468 
  3230 			if ( $p->post_parent == 0 && $count+1 == count( $revparts ) && $p->post_name == $revparts[ $count ] ) {
  3469 			if ( $p->post_parent == 0 && $count+1 == count( $revparts ) && $p->post_name == $revparts[ $count ] ) {
  3231 				$foundid = $page->ID;
  3470 				$foundid = $page->ID;
  3232 				break;
  3471 				if ( $page->post_type == $post_type )
       
  3472 					break;
  3233 			}
  3473 			}
  3234 		}
  3474 		}
  3235 	}
  3475 	}
  3236 
  3476 
  3237 	if ( $foundid )
  3477 	if ( $foundid )
  3238 		return get_page( $foundid, $output );
  3478 		return get_post( $foundid, $output );
  3239 
  3479 
  3240 	return null;
  3480 	return null;
  3241 }
  3481 }
  3242 
  3482 
  3243 /**
  3483 /**
  3247  * @uses $wpdb
  3487  * @uses $wpdb
  3248  *
  3488  *
  3249  * @param string $page_title Page title
  3489  * @param string $page_title Page title
  3250  * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT.
  3490  * @param string $output Optional. Output type. OBJECT, ARRAY_N, or ARRAY_A. Default OBJECT.
  3251  * @param string $post_type Optional. Post type. Default page.
  3491  * @param string $post_type Optional. Post type. Default page.
  3252  * @return mixed
  3492  * @return WP_Post|null WP_Post on success or null on failure
  3253  */
  3493  */
  3254 function get_page_by_title($page_title, $output = OBJECT, $post_type = 'page' ) {
  3494 function get_page_by_title($page_title, $output = OBJECT, $post_type = 'page' ) {
  3255 	global $wpdb;
  3495 	global $wpdb;
  3256 	$page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $page_title, $post_type ) );
  3496 	$page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type= %s", $page_title, $post_type ) );
  3257 	if ( $page )
  3497 	if ( $page )
  3258 		return get_page($page, $output);
  3498 		return get_post( $page, $output );
  3259 
  3499 
  3260 	return null;
  3500 	return null;
  3261 }
  3501 }
  3262 
  3502 
  3263 /**
  3503 /**
  3271  *
  3511  *
  3272  * @param int $page_id Page ID.
  3512  * @param int $page_id Page ID.
  3273  * @param array $pages List of pages' objects.
  3513  * @param array $pages List of pages' objects.
  3274  * @return array
  3514  * @return array
  3275  */
  3515  */
  3276 function &get_page_children($page_id, $pages) {
  3516 function get_page_children($page_id, $pages) {
  3277 	$page_list = array();
  3517 	$page_list = array();
  3278 	foreach ( (array) $pages as $page ) {
  3518 	foreach ( (array) $pages as $page ) {
  3279 		if ( $page->post_parent == $page_id ) {
  3519 		if ( $page->post_parent == $page_id ) {
  3280 			$page_list[] = $page;
  3520 			$page_list[] = $page;
  3281 			if ( $children = get_page_children($page->ID, $pages) )
  3521 			if ( $children = get_page_children($page->ID, $pages) )
  3295  *
  3535  *
  3296  * @param array $pages Posts array.
  3536  * @param array $pages Posts array.
  3297  * @param int $page_id Parent page ID.
  3537  * @param int $page_id Parent page ID.
  3298  * @return array A list arranged by hierarchy. Children immediately follow their parents.
  3538  * @return array A list arranged by hierarchy. Children immediately follow their parents.
  3299  */
  3539  */
  3300 function &get_page_hierarchy( &$pages, $page_id = 0 ) {
  3540 function get_page_hierarchy( &$pages, $page_id = 0 ) {
  3301 	if ( empty( $pages ) ) {
  3541 	if ( empty( $pages ) ) {
  3302 		$result = array();
  3542 		$result = array();
  3303 		return $result;
  3543 		return $result;
  3304 	}
  3544 	}
  3305 
  3545 
  3340  * @param mixed $page Page object or page ID.
  3580  * @param mixed $page Page object or page ID.
  3341  * @return string Page URI.
  3581  * @return string Page URI.
  3342  */
  3582  */
  3343 function get_page_uri($page) {
  3583 function get_page_uri($page) {
  3344 	if ( ! is_object($page) )
  3584 	if ( ! is_object($page) )
  3345 		$page = get_page($page);
  3585 		$page = get_post( $page );
  3346 	$uri = $page->post_name;
  3586 	$uri = $page->post_name;
  3347 
  3587 
  3348 	// A page cannot be it's own parent.
  3588 	foreach ( $page->ancestors as $parent ) {
  3349 	if ( $page->post_parent == $page->ID )
  3589 		$uri = get_post( $parent )->post_name . "/" . $uri;
  3350 		return $uri;
       
  3351 
       
  3352 	while ($page->post_parent != 0) {
       
  3353 		$page = get_page($page->post_parent);
       
  3354 		$uri = $page->post_name . "/" . $uri;
       
  3355 	}
  3590 	}
  3356 
  3591 
  3357 	return $uri;
  3592 	return $uri;
  3358 }
  3593 }
  3359 
  3594 
  3368  * @uses $wpdb
  3603  * @uses $wpdb
  3369  *
  3604  *
  3370  * @param mixed $args Optional. Array or string of options that overrides defaults.
  3605  * @param mixed $args Optional. Array or string of options that overrides defaults.
  3371  * @return array List of pages matching defaults or $args
  3606  * @return array List of pages matching defaults or $args
  3372  */
  3607  */
  3373 function &get_pages($args = '') {
  3608 function get_pages($args = '') {
  3374 	global $wpdb;
  3609 	global $wpdb;
       
  3610 
       
  3611 	$pages = false;
  3375 
  3612 
  3376 	$defaults = array(
  3613 	$defaults = array(
  3377 		'child_of' => 0, 'sort_order' => 'ASC',
  3614 		'child_of' => 0, 'sort_order' => 'ASC',
  3378 		'sort_column' => 'post_title', 'hierarchical' => 1,
  3615 		'sort_column' => 'post_title', 'hierarchical' => 1,
  3379 		'exclude' => array(), 'include' => array(),
  3616 		'exclude' => array(), 'include' => array(),
  3389 	$offset = (int) $offset;
  3626 	$offset = (int) $offset;
  3390 
  3627 
  3391 	// Make sure the post type is hierarchical
  3628 	// Make sure the post type is hierarchical
  3392 	$hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) );
  3629 	$hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) );
  3393 	if ( !in_array( $post_type, $hierarchical_post_types ) )
  3630 	if ( !in_array( $post_type, $hierarchical_post_types ) )
  3394 		return false;
  3631 		return $pages;
  3395 
  3632 
  3396 	// Make sure we have a valid post status
  3633 	// Make sure we have a valid post status
  3397 	if ( !is_array( $post_status ) )
  3634 	if ( !is_array( $post_status ) )
  3398 		$post_status = explode( ',', $post_status );
  3635 		$post_status = explode( ',', $post_status );
  3399 	if ( array_diff( $post_status, get_post_stati() ) )
  3636 	if ( array_diff( $post_status, get_post_stati() ) )
  3400 		return false;
  3637 		return $pages;
  3401 
  3638 
  3402 	$cache = array();
  3639 	$cache = array();
  3403 	$key = md5( serialize( compact(array_keys($defaults)) ) );
  3640 	$key = md5( serialize( compact(array_keys($defaults)) ) );
  3404 	if ( $cache = wp_cache_get( 'get_pages', 'posts' ) ) {
  3641 	if ( $cache = wp_cache_get( 'get_pages', 'posts' ) ) {
  3405 		if ( is_array($cache) && isset( $cache[ $key ] ) ) {
  3642 		if ( is_array($cache) && isset( $cache[ $key ] ) && is_array( $cache[ $key ] ) ) {
  3406 			$pages = apply_filters('get_pages', $cache[ $key ], $r );
  3643 			// Convert to WP_Post instances
       
  3644 			$pages = array_map( 'get_post', $cache[ $key ] );
       
  3645 			$pages = apply_filters( 'get_pages', $pages, $r );
  3407 			return $pages;
  3646 			return $pages;
  3408 		}
  3647 		}
  3409 	}
  3648 	}
  3410 
  3649 
  3411 	if ( !is_array($cache) )
  3650 	if ( !is_array($cache) )
  3557 
  3796 
  3558 	// Update cache.
  3797 	// Update cache.
  3559 	update_post_cache( $pages );
  3798 	update_post_cache( $pages );
  3560 
  3799 
  3561 	if ( $child_of || $hierarchical )
  3800 	if ( $child_of || $hierarchical )
  3562 		$pages = & get_page_children($child_of, $pages);
  3801 		$pages = get_page_children($child_of, $pages);
  3563 
  3802 
  3564 	if ( !empty($exclude_tree) ) {
  3803 	if ( !empty($exclude_tree) ) {
  3565 		$exclude = (int) $exclude_tree;
  3804 		$exclude = (int) $exclude_tree;
  3566 		$children = get_page_children($exclude, $pages);
  3805 		$children = get_page_children($exclude, $pages);
  3567 		$excludes = array();
  3806 		$excludes = array();
  3576 	}
  3815 	}
  3577 
  3816 
  3578 	$cache[ $key ] = $pages;
  3817 	$cache[ $key ] = $pages;
  3579 	wp_cache_set( 'get_pages', $cache, 'posts' );
  3818 	wp_cache_set( 'get_pages', $cache, 'posts' );
  3580 
  3819 
       
  3820 	// Convert to WP_Post instances
       
  3821 	$pages = array_map( 'get_post', $pages );
       
  3822 
  3581 	$pages = apply_filters('get_pages', $pages, $r);
  3823 	$pages = apply_filters('get_pages', $pages, $r);
  3582 
  3824 
  3583 	return $pages;
  3825 	return $pages;
  3584 }
  3826 }
  3585 
  3827 
  3599 	if (strpos($url, home_url()) === false)
  3841 	if (strpos($url, home_url()) === false)
  3600 		return false;
  3842 		return false;
  3601 	if (strpos($url, home_url('/?attachment_id=')) !== false)
  3843 	if (strpos($url, home_url('/?attachment_id=')) !== false)
  3602 		return true;
  3844 		return true;
  3603 	if ( $id = url_to_postid($url) ) {
  3845 	if ( $id = url_to_postid($url) ) {
  3604 		$post = & get_post($id);
  3846 		$post = get_post($id);
  3605 		if ( 'attachment' == $post->post_type )
  3847 		if ( 'attachment' == $post->post_type )
  3606 			return true;
  3848 			return true;
  3607 	}
  3849 	}
  3608 	return false;
  3850 	return false;
  3609 }
  3851 }
  3674 	$post_type = 'attachment';
  3916 	$post_type = 'attachment';
  3675 
  3917 
  3676 	if ( ! in_array( $post_status, array( 'inherit', 'private' ) ) )
  3918 	if ( ! in_array( $post_status, array( 'inherit', 'private' ) ) )
  3677 		$post_status = 'inherit';
  3919 		$post_status = 'inherit';
  3678 
  3920 
       
  3921 	if ( !empty($post_category) )
       
  3922 		$post_category = array_filter($post_category); // Filter out empty terms
       
  3923 
  3679 	// Make sure we set a valid category.
  3924 	// Make sure we set a valid category.
  3680 	if ( !isset($post_category) || 0 == count($post_category) || !is_array($post_category) ) {
  3925 	if ( empty($post_category) || 0 == count($post_category) || !is_array($post_category) ) {
  3681 		// 'post' requires at least one category.
  3926 		$post_category = array();
  3682 		if ( 'post' == $post_type )
       
  3683 			$post_category = array( get_option('default_category') );
       
  3684 		else
       
  3685 			$post_category = array();
       
  3686 	}
  3927 	}
  3687 
  3928 
  3688 	// Are we updating or creating?
  3929 	// Are we updating or creating?
  3689 	if ( !empty($ID) ) {
  3930 	if ( !empty($ID) ) {
  3690 		$update = true;
  3931 		$update = true;
  3765 	if ( empty($post_name) ) {
  4006 	if ( empty($post_name) ) {
  3766 		$post_name = sanitize_title($post_title, $post_ID);
  4007 		$post_name = sanitize_title($post_title, $post_ID);
  3767 		$wpdb->update( $wpdb->posts, compact("post_name"), array( 'ID' => $post_ID ) );
  4008 		$wpdb->update( $wpdb->posts, compact("post_name"), array( 'ID' => $post_ID ) );
  3768 	}
  4009 	}
  3769 
  4010 
  3770 	wp_set_post_categories($post_ID, $post_category);
  4011 	if ( is_object_in_taxonomy($post_type, 'category') )
       
  4012 		wp_set_post_categories( $post_ID, $post_category );
       
  4013 
       
  4014 	if ( isset( $tags_input ) && is_object_in_taxonomy($post_type, 'post_tag') )
       
  4015 		wp_set_post_tags( $post_ID, $tags_input );
       
  4016 
       
  4017 	// support for all custom taxonomies
       
  4018 	if ( !empty($tax_input) ) {
       
  4019 		foreach ( $tax_input as $taxonomy => $tags ) {
       
  4020 			$taxonomy_obj = get_taxonomy($taxonomy);
       
  4021 			if ( is_array($tags) ) // array = hierarchical, string = non-hierarchical.
       
  4022 				$tags = array_filter($tags);
       
  4023 			if ( current_user_can($taxonomy_obj->cap->assign_terms) )
       
  4024 				wp_set_post_terms( $post_ID, $tags, $taxonomy );
       
  4025 		}
       
  4026 	}
  3771 
  4027 
  3772 	if ( $file )
  4028 	if ( $file )
  3773 		update_attached_file( $post_ID, $file );
  4029 		update_attached_file( $post_ID, $file );
  3774 
  4030 
  3775 	clean_post_cache( $post_ID );
  4031 	clean_post_cache( $post_ID );
  3895  * @param bool $unfiltered Optional, default is false. If true, filters are not run.
  4151  * @param bool $unfiltered Optional, default is false. If true, filters are not run.
  3896  * @return string|bool Attachment meta field. False on failure.
  4152  * @return string|bool Attachment meta field. False on failure.
  3897  */
  4153  */
  3898 function wp_get_attachment_metadata( $post_id = 0, $unfiltered = false ) {
  4154 function wp_get_attachment_metadata( $post_id = 0, $unfiltered = false ) {
  3899 	$post_id = (int) $post_id;
  4155 	$post_id = (int) $post_id;
  3900 	if ( !$post =& get_post( $post_id ) )
  4156 	if ( !$post = get_post( $post_id ) )
  3901 		return false;
  4157 		return false;
  3902 
  4158 
  3903 	$data = get_post_meta( $post->ID, '_wp_attachment_metadata', true );
  4159 	$data = get_post_meta( $post->ID, '_wp_attachment_metadata', true );
  3904 
  4160 
  3905 	if ( $unfiltered )
  4161 	if ( $unfiltered )
  3917  * @param array $data Attachment data.
  4173  * @param array $data Attachment data.
  3918  * @return int
  4174  * @return int
  3919  */
  4175  */
  3920 function wp_update_attachment_metadata( $post_id, $data ) {
  4176 function wp_update_attachment_metadata( $post_id, $data ) {
  3921 	$post_id = (int) $post_id;
  4177 	$post_id = (int) $post_id;
  3922 	if ( !$post =& get_post( $post_id ) )
  4178 	if ( !$post = get_post( $post_id ) )
  3923 		return false;
  4179 		return false;
  3924 
  4180 
  3925 	$data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID );
  4181 	if ( $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID ) )
  3926 
  4182 		return update_post_meta( $post->ID, '_wp_attachment_metadata', $data );
  3927 	return update_post_meta( $post->ID, '_wp_attachment_metadata', $data);
  4183 	else
       
  4184 		return delete_post_meta( $post->ID, '_wp_attachment_metadata' );
  3928 }
  4185 }
  3929 
  4186 
  3930 /**
  4187 /**
  3931  * Retrieve the URL for an attachment.
  4188  * Retrieve the URL for an attachment.
  3932  *
  4189  *
  3935  * @param int $post_id Attachment ID.
  4192  * @param int $post_id Attachment ID.
  3936  * @return string
  4193  * @return string
  3937  */
  4194  */
  3938 function wp_get_attachment_url( $post_id = 0 ) {
  4195 function wp_get_attachment_url( $post_id = 0 ) {
  3939 	$post_id = (int) $post_id;
  4196 	$post_id = (int) $post_id;
  3940 	if ( !$post =& get_post( $post_id ) )
  4197 	if ( !$post = get_post( $post_id ) )
  3941 		return false;
  4198 		return false;
  3942 
  4199 
  3943 	if ( 'attachment' != $post->post_type )
  4200 	if ( 'attachment' != $post->post_type )
  3944 		return false;
  4201 		return false;
  3945 
  4202 
  3974  * @param int $post_id Attachment ID.
  4231  * @param int $post_id Attachment ID.
  3975  * @return mixed False on failure. Thumbnail file path on success.
  4232  * @return mixed False on failure. Thumbnail file path on success.
  3976  */
  4233  */
  3977 function wp_get_attachment_thumb_file( $post_id = 0 ) {
  4234 function wp_get_attachment_thumb_file( $post_id = 0 ) {
  3978 	$post_id = (int) $post_id;
  4235 	$post_id = (int) $post_id;
  3979 	if ( !$post =& get_post( $post_id ) )
  4236 	if ( !$post = get_post( $post_id ) )
  3980 		return false;
  4237 		return false;
  3981 	if ( !is_array( $imagedata = wp_get_attachment_metadata( $post->ID ) ) )
  4238 	if ( !is_array( $imagedata = wp_get_attachment_metadata( $post->ID ) ) )
  3982 		return false;
  4239 		return false;
  3983 
  4240 
  3984 	$file = get_attached_file( $post->ID );
  4241 	$file = get_attached_file( $post->ID );
  3996  * @param int $post_id Attachment ID
  4253  * @param int $post_id Attachment ID
  3997  * @return string|bool False on failure. Thumbnail URL on success.
  4254  * @return string|bool False on failure. Thumbnail URL on success.
  3998  */
  4255  */
  3999 function wp_get_attachment_thumb_url( $post_id = 0 ) {
  4256 function wp_get_attachment_thumb_url( $post_id = 0 ) {
  4000 	$post_id = (int) $post_id;
  4257 	$post_id = (int) $post_id;
  4001 	if ( !$post =& get_post( $post_id ) )
  4258 	if ( !$post = get_post( $post_id ) )
  4002 		return false;
  4259 		return false;
  4003 	if ( !$url = wp_get_attachment_url( $post->ID ) )
  4260 	if ( !$url = wp_get_attachment_url( $post->ID ) )
  4004 		return false;
  4261 		return false;
  4005 
  4262 
  4006 	$sized = image_downsize( $post_id, 'thumbnail' );
  4263 	$sized = image_downsize( $post_id, 'thumbnail' );
  4023  * @param int $post_id Attachment ID
  4280  * @param int $post_id Attachment ID
  4024  * @return bool
  4281  * @return bool
  4025  */
  4282  */
  4026 function wp_attachment_is_image( $post_id = 0 ) {
  4283 function wp_attachment_is_image( $post_id = 0 ) {
  4027 	$post_id = (int) $post_id;
  4284 	$post_id = (int) $post_id;
  4028 	if ( !$post =& get_post( $post_id ) )
  4285 	if ( !$post = get_post( $post_id ) )
  4029 		return false;
  4286 		return false;
  4030 
  4287 
  4031 	if ( !$file = get_attached_file( $post->ID ) )
  4288 	if ( !$file = get_attached_file( $post->ID ) )
  4032 		return false;
  4289 		return false;
  4033 
  4290 
  4034 	$ext = preg_match('/\.([^.]+)$/', $file, $matches) ? strtolower($matches[1]) : false;
  4291 	$ext = preg_match('/\.([^.]+)$/', $file, $matches) ? strtolower($matches[1]) : false;
  4035 
  4292 
  4036 	$image_exts = array('jpg', 'jpeg', 'gif', 'png');
  4293 	$image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' );
  4037 
  4294 
  4038 	if ( 'image/' == substr($post->post_mime_type, 0, 6) || $ext && 'import' == $post->post_mime_type && in_array($ext, $image_exts) )
  4295 	if ( 'image/' == substr($post->post_mime_type, 0, 6) || $ext && 'import' == $post->post_mime_type && in_array($ext, $image_exts) )
  4039 		return true;
  4296 		return true;
  4040 	return false;
  4297 	return false;
  4041 }
  4298 }
  4049  * @return string|bool
  4306  * @return string|bool
  4050  */
  4307  */
  4051 function wp_mime_type_icon( $mime = 0 ) {
  4308 function wp_mime_type_icon( $mime = 0 ) {
  4052 	if ( !is_numeric($mime) )
  4309 	if ( !is_numeric($mime) )
  4053 		$icon = wp_cache_get("mime_type_icon_$mime");
  4310 		$icon = wp_cache_get("mime_type_icon_$mime");
       
  4311 
       
  4312 	$post_id = 0;
  4054 	if ( empty($icon) ) {
  4313 	if ( empty($icon) ) {
  4055 		$post_id = 0;
       
  4056 		$post_mimes = array();
  4314 		$post_mimes = array();
  4057 		if ( is_numeric($mime) ) {
  4315 		if ( is_numeric($mime) ) {
  4058 			$mime = (int) $mime;
  4316 			$mime = (int) $mime;
  4059 			if ( $post =& get_post( $mime ) ) {
  4317 			if ( $post = get_post( $mime ) ) {
  4060 				$post_id = (int) $post->ID;
  4318 				$post_id = (int) $post->ID;
  4061 				$ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $post->guid);
  4319 				$ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $post->guid);
  4062 				if ( !empty($ext) ) {
  4320 				if ( !empty($ext) ) {
  4063 					$post_mimes[] = $ext;
  4321 					$post_mimes[] = $ext;
  4064 					if ( $ext_type = wp_ext2type( $ext ) )
  4322 					if ( $ext_type = wp_ext2type( $ext ) )
  4096 						$icon_files["$dir/$file"] = "$uri/$file";
  4354 						$icon_files["$dir/$file"] = "$uri/$file";
  4097 					}
  4355 					}
  4098 					closedir($dh);
  4356 					closedir($dh);
  4099 				}
  4357 				}
  4100 			}
  4358 			}
  4101 			wp_cache_set('icon_files', $icon_files, 600);
  4359 			wp_cache_add( 'icon_files', $icon_files, 'default', 600 );
  4102 		}
  4360 		}
  4103 
  4361 
  4104 		// Icon basename - extension = MIME wildcard
  4362 		// Icon basename - extension = MIME wildcard
  4105 		foreach ( $icon_files as $file => $uri )
  4363 		foreach ( $icon_files as $file => $uri )
  4106 			$types[ preg_replace('/^([^.]*).*$/', '$1', basename($file)) ] =& $icon_files[$file];
  4364 			$types[ preg_replace('/^([^.]*).*$/', '$1', basename($file)) ] =& $icon_files[$file];
  4116 
  4374 
  4117 		foreach ( $matches as $match => $wilds ) {
  4375 		foreach ( $matches as $match => $wilds ) {
  4118 			if ( isset($types[$wilds[0]])) {
  4376 			if ( isset($types[$wilds[0]])) {
  4119 				$icon = $types[$wilds[0]];
  4377 				$icon = $types[$wilds[0]];
  4120 				if ( !is_numeric($mime) )
  4378 				if ( !is_numeric($mime) )
  4121 					wp_cache_set("mime_type_icon_$mime", $icon);
  4379 					wp_cache_add("mime_type_icon_$mime", $icon);
  4122 				break;
  4380 				break;
  4123 			}
  4381 			}
  4124 		}
  4382 		}
  4125 	}
  4383 	}
  4126 
  4384 
  4193  *
  4451  *
  4194  * @since 3.0.0
  4452  * @since 3.0.0
  4195  * @param string $post_type Post type.
  4453  * @param string $post_type Post type.
  4196  * @param bool $full Optional. Returns a full WHERE statement instead of just an 'andalso' term.
  4454  * @param bool $full Optional. Returns a full WHERE statement instead of just an 'andalso' term.
  4197  * @param int $post_author Optional. Query posts having a single author ID.
  4455  * @param int $post_author Optional. Query posts having a single author ID.
       
  4456  * @param bool $public_only Optional. Only return public posts. Skips cap checks for $current_user.  Default is false.
  4198  * @return string SQL WHERE code that can be added to a query.
  4457  * @return string SQL WHERE code that can be added to a query.
  4199  */
  4458  */
  4200 function get_posts_by_author_sql( $post_type, $full = true, $post_author = null ) {
  4459 function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) {
  4201 	global $user_ID, $wpdb;
  4460 	global $user_ID, $wpdb;
  4202 
  4461 
  4203 	// Private posts
  4462 	// Private posts
  4204 	$post_type_obj = get_post_type_object( $post_type );
  4463 	$post_type_obj = get_post_type_object( $post_type );
  4205 	if ( ! $post_type_obj )
  4464 	if ( ! $post_type_obj )
  4219 		$sql = '';
  4478 		$sql = '';
  4220 	}
  4479 	}
  4221 
  4480 
  4222 	$sql .= "(post_status = 'publish'";
  4481 	$sql .= "(post_status = 'publish'";
  4223 
  4482 
  4224 	if ( current_user_can( $cap ) ) {
  4483 	// Only need to check the cap if $public_only is false
  4225 		// Does the user have the capability to view private posts? Guess so.
  4484 	if ( false === $public_only ) {
  4226 		$sql .= " OR post_status = 'private'";
  4485 		if ( current_user_can( $cap ) ) {
  4227 	} elseif ( is_user_logged_in() ) {
  4486 			// Does the user have the capability to view private posts? Guess so.
  4228 		// Users can view their own private posts.
       
  4229 		$id = (int) $user_ID;
       
  4230 		if ( null === $post_author || ! $full ) {
       
  4231 			$sql .= " OR post_status = 'private' AND post_author = $id";
       
  4232 		} elseif ( $id == (int) $post_author ) {
       
  4233 			$sql .= " OR post_status = 'private'";
  4487 			$sql .= " OR post_status = 'private'";
       
  4488 		} elseif ( is_user_logged_in() ) {
       
  4489 			// Users can view their own private posts.
       
  4490 			$id = (int) $user_ID;
       
  4491 			if ( null === $post_author || ! $full ) {
       
  4492 				$sql .= " OR post_status = 'private' AND post_author = $id";
       
  4493 			} elseif ( $id == (int) $post_author ) {
       
  4494 				$sql .= " OR post_status = 'private'";
       
  4495 			} // else none
  4234 		} // else none
  4496 		} // else none
  4235 	} // else none
  4497 	}
  4236 
  4498 
  4237 	$sql .= ')';
  4499 	$sql .= ')';
  4238 
  4500 
  4239 	return $sql;
  4501 	return $sql;
  4240 }
  4502 }
  4349  * Will clean the post in the cache.
  4611  * Will clean the post in the cache.
  4350  *
  4612  *
  4351  * Cleaning means delete from the cache of the post. Will call to clean the term
  4613  * Cleaning means delete from the cache of the post. Will call to clean the term
  4352  * object cache associated with the post ID.
  4614  * object cache associated with the post ID.
  4353  *
  4615  *
  4354  * clean_post_cache() will call itself recursively for each child post.
       
  4355  *
       
  4356  * This function not run if $_wp_suspend_cache_invalidation is not empty. See
  4616  * This function not run if $_wp_suspend_cache_invalidation is not empty. See
  4357  * wp_suspend_cache_invalidation().
  4617  * wp_suspend_cache_invalidation().
  4358  *
  4618  *
  4359  * @package WordPress
  4619  * @package WordPress
  4360  * @subpackage Cache
  4620  * @subpackage Cache
  4381 
  4641 
  4382 	wp_cache_delete( 'wp_get_archives', 'general' );
  4642 	wp_cache_delete( 'wp_get_archives', 'general' );
  4383 
  4643 
  4384 	do_action( 'clean_post_cache', $post->ID, $post );
  4644 	do_action( 'clean_post_cache', $post->ID, $post );
  4385 
  4645 
       
  4646 	if ( is_post_type_hierarchical( $post->post_type ) )
       
  4647 		wp_cache_delete( 'get_pages', 'posts' );
       
  4648 
  4386 	if ( 'page' == $post->post_type ) {
  4649 	if ( 'page' == $post->post_type ) {
  4387 		wp_cache_delete( 'all_page_ids', 'posts' );
  4650 		wp_cache_delete( 'all_page_ids', 'posts' );
  4388 		wp_cache_delete( 'get_pages', 'posts' );
       
  4389 		do_action( 'clean_page_cache', $post->ID );
  4651 		do_action( 'clean_page_cache', $post->ID );
  4390 	}
  4652 	}
  4391 
       
  4392 	if ( $children = $wpdb->get_results( $wpdb->prepare("SELECT ID, post_type FROM $wpdb->posts WHERE post_parent = %d", $post->ID) ) ) {
       
  4393 		foreach ( $children as $child ) {
       
  4394 			// Loop detection
       
  4395 			if ( $child->ID == $post->ID )
       
  4396 				continue;
       
  4397 			clean_post_cache( $child );
       
  4398 		}
       
  4399 	}
       
  4400 
       
  4401 	if ( is_multisite() )
       
  4402 		wp_cache_delete( $wpdb->blogid . '-' . $post->ID, 'global-posts' );
       
  4403 }
  4653 }
  4404 
  4654 
  4405 /**
  4655 /**
  4406  * Call major cache updating functions for list of Post objects.
  4656  * Call major cache updating functions for list of Post objects.
  4407  *
  4657  *
  4428 
  4678 
  4429 	$post_ids = array();
  4679 	$post_ids = array();
  4430 	foreach ( $posts as $post )
  4680 	foreach ( $posts as $post )
  4431 		$post_ids[] = $post->ID;
  4681 		$post_ids[] = $post->ID;
  4432 
  4682 
  4433 	if ( empty($post_type) )
  4683 	if ( ! $post_type )
  4434 		$post_type = 'post';
  4684 		$post_type = 'any';
  4435 
  4685 
  4436 	if ( $update_term_cache ) {
  4686 	if ( $update_term_cache ) {
  4437 		if ( is_array($post_type) ) {
  4687 		if ( is_array($post_type) ) {
  4438 			$ptypes = $post_type;
  4688 			$ptypes = $post_type;
  4439 		} elseif ( 'any' == $post_type ) {
  4689 		} elseif ( 'any' == $post_type ) {
  4569  * Hook to schedule pings and enclosures when a post is published.
  4819  * Hook to schedule pings and enclosures when a post is published.
  4570  *
  4820  *
  4571  * @since 2.3.0
  4821  * @since 2.3.0
  4572  * @access private
  4822  * @access private
  4573  * @uses $wpdb
  4823  * @uses $wpdb
  4574  * @uses XMLRPC_REQUEST and APP_REQUEST constants.
  4824  * @uses XMLRPC_REQUEST constant.
  4575  * @uses do_action() Calls 'xmlprc_publish_post' on post ID if XMLRPC_REQUEST is defined.
  4825  * @uses do_action() Calls 'xmlprc_publish_post' on post ID if XMLRPC_REQUEST is defined.
  4576  * @uses do_action() Calls 'app_publish_post' on post ID if APP_REQUEST is defined.
       
  4577  *
  4826  *
  4578  * @param int $post_id The ID in the database table of the post being published
  4827  * @param int $post_id The ID in the database table of the post being published
  4579  */
  4828  */
  4580 function _publish_post_hook($post_id) {
  4829 function _publish_post_hook($post_id) {
  4581 	global $wpdb;
  4830 	global $wpdb;
  4582 
  4831 
  4583 	if ( defined('XMLRPC_REQUEST') )
  4832 	if ( defined('XMLRPC_REQUEST') )
  4584 		do_action('xmlrpc_publish_post', $post_id);
  4833 		do_action('xmlrpc_publish_post', $post_id);
  4585 	if ( defined('APP_REQUEST') )
       
  4586 		do_action('app_publish_post', $post_id);
       
  4587 
  4834 
  4588 	if ( defined('WP_IMPORTING') )
  4835 	if ( defined('WP_IMPORTING') )
  4589 		return;
  4836 		return;
  4590 
  4837 
  4591 	if ( get_option('default_pingback_flag') )
  4838 	if ( get_option('default_pingback_flag') )
  4592 		add_post_meta( $post_id, '_pingme', '1' );
  4839 		add_post_meta( $post_id, '_pingme', '1' );
  4593 	add_post_meta( $post_id, '_encloseme', '1' );
  4840 	add_post_meta( $post_id, '_encloseme', '1' );
  4594 
  4841 
  4595 	wp_schedule_single_event(time(), 'do_pings');
  4842 	wp_schedule_single_event(time(), 'do_pings');
  4596 }
       
  4597 
       
  4598 /**
       
  4599  * Hook used to prevent page/post cache from staying dirty when a post is saved.
       
  4600  *
       
  4601  * @since 2.3.0
       
  4602  * @access private
       
  4603  *
       
  4604  * @param int $post_id The ID in the database table for the $post
       
  4605  * @param object $post Object type containing the post information
       
  4606  */
       
  4607 function _save_post_hook( $post_id, $post ) {
       
  4608 	clean_post_cache( $post );
       
  4609 }
       
  4610 
       
  4611 /**
       
  4612  * Retrieve post ancestors and append to post ancestors property.
       
  4613  *
       
  4614  * Will only retrieve ancestors once, if property is already set, then nothing
       
  4615  * will be done. If there is not a parent post, or post ID and post parent ID
       
  4616  * are the same then nothing will be done.
       
  4617  *
       
  4618  * The parameter is passed by reference, so nothing needs to be returned. The
       
  4619  * property will be updated and can be referenced after the function is
       
  4620  * complete. The post parent will be an ancestor and the parent of the post
       
  4621  * parent will be an ancestor. There will only be two ancestors at the most.
       
  4622  *
       
  4623  * @since 2.5.0
       
  4624  * @access private
       
  4625  * @uses $wpdb
       
  4626  *
       
  4627  * @param object $_post Post data.
       
  4628  * @return null When nothing needs to be done.
       
  4629  */
       
  4630 function _get_post_ancestors(&$_post) {
       
  4631 	global $wpdb;
       
  4632 
       
  4633 	if ( isset($_post->ancestors) )
       
  4634 		return;
       
  4635 
       
  4636 	$_post->ancestors = array();
       
  4637 
       
  4638 	if ( empty($_post->post_parent) || $_post->ID == $_post->post_parent )
       
  4639 		return;
       
  4640 
       
  4641 	$id = $_post->ancestors[] = (int) $_post->post_parent;
       
  4642 	while ( $ancestor = $wpdb->get_var( $wpdb->prepare("SELECT `post_parent` FROM $wpdb->posts WHERE ID = %d LIMIT 1", $id) ) ) {
       
  4643 		// Loop detection: If the ancestor has been seen before, break.
       
  4644 		if ( ( $ancestor == $_post->ID ) || in_array($ancestor,  $_post->ancestors) )
       
  4645 			break;
       
  4646 		$id = $_post->ancestors[] = (int) $ancestor;
       
  4647 	}
       
  4648 }
  4843 }
  4649 
  4844 
  4650 /**
  4845 /**
  4651  * Determines which fields of posts are to be saved in revisions.
  4846  * Determines which fields of posts are to be saved in revisions.
  4652  *
  4847  *
  4724 		return;
  4919 		return;
  4725 
  4920 
  4726 	if ( !$post = get_post( $post_id, ARRAY_A ) )
  4921 	if ( !$post = get_post( $post_id, ARRAY_A ) )
  4727 		return;
  4922 		return;
  4728 
  4923 
       
  4924 	if ( 'auto-draft' == $post['post_status'] )
       
  4925 		return;
       
  4926 
  4729 	if ( !post_type_supports($post['post_type'], 'revisions') )
  4927 	if ( !post_type_supports($post['post_type'], 'revisions') )
  4730 		return;
  4928 		return;
  4731 
  4929 
  4732 	$return = _wp_put_post_revision( $post );
  4930 	$return = _wp_put_post_revision( $post );
  4733 
  4931 
  4888  * @param int|object $post Post ID or post object
  5086  * @param int|object $post Post ID or post object
  4889  * @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N.
  5087  * @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N.
  4890  * @param string $filter Optional sanitation filter. @see sanitize_post()
  5088  * @param string $filter Optional sanitation filter. @see sanitize_post()
  4891  * @return mixed Null if error or post object if success
  5089  * @return mixed Null if error or post object if success
  4892  */
  5090  */
  4893 function &wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') {
  5091 function wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') {
  4894 	$null = null;
  5092 	$null = null;
  4895 	if ( !$revision = get_post( $post, OBJECT, $filter ) )
  5093 	if ( !$revision = get_post( $post, OBJECT, $filter ) )
  4896 		return $revision;
  5094 		return $revision;
  4897 	if ( 'revision' !== $revision->post_type )
  5095 	if ( 'revision' !== $revision->post_type )
  4898 		return $null;
  5096 		return $null;
  5164  */
  5362  */
  5165 function set_post_thumbnail( $post, $thumbnail_id ) {
  5363 function set_post_thumbnail( $post, $thumbnail_id ) {
  5166 	$post = get_post( $post );
  5364 	$post = get_post( $post );
  5167 	$thumbnail_id = absint( $thumbnail_id );
  5365 	$thumbnail_id = absint( $thumbnail_id );
  5168 	if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {
  5366 	if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {
  5169 		$thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' );
  5367 		if ( $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) )
  5170 		if ( ! empty( $thumbnail_html ) ) {
       
  5171 			return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );
  5368 			return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id );
  5172 		}
  5369 		else
       
  5370 			return delete_post_meta( $post->ID, '_thumbnail_id' );
  5173 	}
  5371 	}
  5174 	return false;
  5372 	return false;
  5175 }
  5373 }
  5176 
  5374 
  5177 /**
  5375 /**