wp/wp-admin/includes/post.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     9 /**
     9 /**
    10  * Rename $_POST data from form names to DB post columns.
    10  * Rename $_POST data from form names to DB post columns.
    11  *
    11  *
    12  * Manipulates $_POST directly.
    12  * Manipulates $_POST directly.
    13  *
    13  *
    14  * @package WordPress
       
    15  * @since 2.6.0
    14  * @since 2.6.0
    16  *
    15  *
    17  * @param bool $update Are we updating a pre-existing post?
    16  * @param bool $update Are we updating a pre-existing post?
    18  * @param array $post_data Array of post data. Defaults to the contents of $_POST.
    17  * @param array $post_data Array of post data. Defaults to the contents of $_POST.
    19  * @return object|bool WP_Error on failure, true on success.
    18  * @return object|bool WP_Error on failure, true on success.
    28 
    27 
    29 	$ptype = get_post_type_object( $post_data['post_type'] );
    28 	$ptype = get_post_type_object( $post_data['post_type'] );
    30 
    29 
    31 	if ( $update && ! current_user_can( 'edit_post', $post_data['ID'] ) ) {
    30 	if ( $update && ! current_user_can( 'edit_post', $post_data['ID'] ) ) {
    32 		if ( 'page' == $post_data['post_type'] )
    31 		if ( 'page' == $post_data['post_type'] )
    33 			return new WP_Error( 'edit_others_pages', __( 'You are not allowed to edit pages as this user.' ) );
    32 			return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) );
    34 		else
    33 		else
    35 			return new WP_Error( 'edit_others_posts', __( 'You are not allowed to edit posts as this user.' ) );
    34 			return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) );
    36 	} elseif ( ! $update && ! current_user_can( $ptype->cap->create_posts ) ) {
    35 	} elseif ( ! $update && ! current_user_can( $ptype->cap->create_posts ) ) {
    37 		if ( 'page' == $post_data['post_type'] )
    36 		if ( 'page' == $post_data['post_type'] )
    38 			return new WP_Error( 'edit_others_pages', __( 'You are not allowed to create pages as this user.' ) );
    37 			return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) );
    39 		else
    38 		else
    40 			return new WP_Error( 'edit_others_posts', __( 'You are not allowed to create posts as this user.' ) );
    39 			return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) );
    41 	}
    40 	}
    42 
    41 
    43 	if ( isset( $post_data['content'] ) )
    42 	if ( isset( $post_data['content'] ) )
    44 		$post_data['post_content'] = $post_data['content'];
    43 		$post_data['post_content'] = $post_data['content'];
    45 
    44 
    66 
    65 
    67 	if ( isset( $post_data['user_ID'] ) && ( $post_data['post_author'] != $post_data['user_ID'] )
    66 	if ( isset( $post_data['user_ID'] ) && ( $post_data['post_author'] != $post_data['user_ID'] )
    68 		 && ! current_user_can( $ptype->cap->edit_others_posts ) ) {
    67 		 && ! current_user_can( $ptype->cap->edit_others_posts ) ) {
    69 		if ( $update ) {
    68 		if ( $update ) {
    70 			if ( 'page' == $post_data['post_type'] )
    69 			if ( 'page' == $post_data['post_type'] )
    71 				return new WP_Error( 'edit_others_pages', __( 'You are not allowed to edit pages as this user.' ) );
    70 				return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) );
    72 			else
    71 			else
    73 				return new WP_Error( 'edit_others_posts', __( 'You are not allowed to edit posts as this user.' ) );
    72 				return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) );
    74 		} else {
    73 		} else {
    75 			if ( 'page' == $post_data['post_type'] )
    74 			if ( 'page' == $post_data['post_type'] )
    76 				return new WP_Error( 'edit_others_pages', __( 'You are not allowed to create pages as this user.' ) );
    75 				return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) );
    77 			else
    76 			else
    78 				return new WP_Error( 'edit_others_posts', __( 'You are not allowed to create posts as this user.' ) );
    77 				return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) );
    79 		}
    78 		}
    80 	}
    79 	}
    81 
    80 
    82 	if ( ! empty( $post_data['post_status'] ) ) {
    81 	if ( ! empty( $post_data['post_status'] ) ) {
    83 		$post_data['post_status'] = sanitize_key( $post_data['post_status'] );
    82 		$post_data['post_status'] = sanitize_key( $post_data['post_status'] );
   158 		$mn = ($mn > 59 ) ? $mn -60 : $mn;
   157 		$mn = ($mn > 59 ) ? $mn -60 : $mn;
   159 		$ss = ($ss > 59 ) ? $ss -60 : $ss;
   158 		$ss = ($ss > 59 ) ? $ss -60 : $ss;
   160 		$post_data['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
   159 		$post_data['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
   161 		$valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] );
   160 		$valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] );
   162 		if ( !$valid_date ) {
   161 		if ( !$valid_date ) {
   163 			return new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.' ) );
   162 			return new WP_Error( 'invalid_date', __( 'Invalid date.' ) );
   164 		}
   163 		}
   165 		$post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] );
   164 		$post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] );
   166 	}
   165 	}
   167 
   166 
       
   167 	if ( isset( $post_data['post_category'] ) ) {
       
   168 		$category_object = get_taxonomy( 'category' );
       
   169 		if ( ! current_user_can( $category_object->cap->assign_terms ) ) {
       
   170 			unset( $post_data['post_category'] );
       
   171 		}
       
   172 	}
       
   173 
   168 	return $post_data;
   174 	return $post_data;
   169 }
   175 }
   170 
   176 
   171 /**
   177 /**
   172  * Update an existing post with values provided in $_POST.
   178  * Update an existing post with values provided in $_POST.
   173  *
   179  *
   174  * @since 1.5.0
   180  * @since 1.5.0
       
   181  *
       
   182  * @global wpdb $wpdb WordPress database abstraction object.
   175  *
   183  *
   176  * @param array $post_data Optional.
   184  * @param array $post_data Optional.
   177  * @return int Post ID.
   185  * @return int Post ID.
   178  */
   186  */
   179 function edit_post( $post_data = null ) {
   187 function edit_post( $post_data = null ) {
   199 	}
   207 	}
   200 
   208 
   201 	$ptype = get_post_type_object($post_data['post_type']);
   209 	$ptype = get_post_type_object($post_data['post_type']);
   202 	if ( !current_user_can( 'edit_post', $post_ID ) ) {
   210 	if ( !current_user_can( 'edit_post', $post_ID ) ) {
   203 		if ( 'page' == $post_data['post_type'] )
   211 		if ( 'page' == $post_data['post_type'] )
   204 			wp_die( __('You are not allowed to edit this page.' ));
   212 			wp_die( __('Sorry, you are not allowed to edit this page.' ));
   205 		else
   213 		else
   206 			wp_die( __('You are not allowed to edit this post.' ));
   214 			wp_die( __('Sorry, you are not allowed to edit this post.' ));
   207 	}
   215 	}
   208 
   216 
   209 	if ( post_type_supports( $ptype->name, 'revisions' ) ) {
   217 	if ( post_type_supports( $ptype->name, 'revisions' ) ) {
   210 		$revisions = wp_get_post_revisions( $post_ID, array( 'order' => 'ASC', 'posts_per_page' => 1 ) );
   218 		$revisions = wp_get_post_revisions( $post_ID, array( 'order' => 'ASC', 'posts_per_page' => 1 ) );
   211 		$revision = current( $revisions );
   219 		$revision = current( $revisions );
   277 		foreach ( $post_data['meta'] as $key => $value ) {
   285 		foreach ( $post_data['meta'] as $key => $value ) {
   278 			if ( !$meta = get_post_meta_by_id( $key ) )
   286 			if ( !$meta = get_post_meta_by_id( $key ) )
   279 				continue;
   287 				continue;
   280 			if ( $meta->post_id != $post_ID )
   288 			if ( $meta->post_id != $post_ID )
   281 				continue;
   289 				continue;
       
   290 			if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $meta->meta_key ) )
       
   291 				continue;
   282 			if ( is_protected_meta( $value['key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $value['key'] ) )
   292 			if ( is_protected_meta( $value['key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $value['key'] ) )
   283 				continue;
   293 				continue;
   284 			update_meta( $key, $value['key'], $value['value'] );
   294 			update_meta( $key, $value['key'], $value['value'] );
   285 		}
   295 		}
   286 	}
   296 	}
   367 	$success = wp_update_post( $post_data );
   377 	$success = wp_update_post( $post_data );
   368 	// If the save failed, see if we can sanity check the main fields and try again
   378 	// If the save failed, see if we can sanity check the main fields and try again
   369 	if ( ! $success && is_callable( array( $wpdb, 'strip_invalid_text_for_column' ) ) ) {
   379 	if ( ! $success && is_callable( array( $wpdb, 'strip_invalid_text_for_column' ) ) ) {
   370 		$fields = array( 'post_title', 'post_content', 'post_excerpt' );
   380 		$fields = array( 'post_title', 'post_content', 'post_excerpt' );
   371 
   381 
   372 		foreach( $fields as $field ) {
   382 		foreach ( $fields as $field ) {
   373 			if ( isset( $post_data[ $field ] ) ) {
   383 			if ( isset( $post_data[ $field ] ) ) {
   374 				$post_data[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->posts, $field, $post_data[ $field ] );
   384 				$post_data[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->posts, $field, $post_data[ $field ] );
   375 			}
   385 			}
   376 		}
   386 		}
   377 
   387 
   381 	// Now that we have an ID we can fix any attachment anchor hrefs
   391 	// Now that we have an ID we can fix any attachment anchor hrefs
   382 	_fix_attachment_links( $post_ID );
   392 	_fix_attachment_links( $post_ID );
   383 
   393 
   384 	wp_set_post_lock( $post_ID );
   394 	wp_set_post_lock( $post_ID );
   385 
   395 
   386 	if ( current_user_can( $ptype->cap->edit_others_posts ) ) {
   396 	if ( current_user_can( $ptype->cap->edit_others_posts ) && current_user_can( $ptype->cap->publish_posts ) ) {
   387 		if ( ! empty( $post_data['sticky'] ) )
   397 		if ( ! empty( $post_data['sticky'] ) )
   388 			stick_post( $post_ID );
   398 			stick_post( $post_ID );
   389 		else
   399 		else
   390 			unstick_post( $post_ID );
   400 			unstick_post( $post_ID );
   391 	}
   401 	}
   399  * Updates all bulk edited posts/pages, adding (but not removing) tags and
   409  * Updates all bulk edited posts/pages, adding (but not removing) tags and
   400  * categories. Skips pages when they would be their own parent or child.
   410  * categories. Skips pages when they would be their own parent or child.
   401  *
   411  *
   402  * @since 2.7.0
   412  * @since 2.7.0
   403  *
   413  *
       
   414  * @global wpdb $wpdb WordPress database abstraction object.
       
   415  *
   404  * @param array $post_data Optional, the array of post data to process if not provided will use $_POST superglobal.
   416  * @param array $post_data Optional, the array of post data to process if not provided will use $_POST superglobal.
   405  * @return array
   417  * @return array
   406  */
   418  */
   407 function bulk_edit_posts( $post_data = null ) {
   419 function bulk_edit_posts( $post_data = null ) {
   408 	global $wpdb;
   420 	global $wpdb;
   415 	else
   427 	else
   416 		$ptype = get_post_type_object('post');
   428 		$ptype = get_post_type_object('post');
   417 
   429 
   418 	if ( !current_user_can( $ptype->cap->edit_posts ) ) {
   430 	if ( !current_user_can( $ptype->cap->edit_posts ) ) {
   419 		if ( 'page' == $ptype->name )
   431 		if ( 'page' == $ptype->name )
   420 			wp_die( __('You are not allowed to edit pages.'));
   432 			wp_die( __('Sorry, you are not allowed to edit pages.'));
   421 		else
   433 		else
   422 			wp_die( __('You are not allowed to edit posts.'));
   434 			wp_die( __('Sorry, you are not allowed to edit posts.'));
   423 	}
   435 	}
   424 
   436 
   425 	if ( -1 == $post_data['_status'] ) {
   437 	if ( -1 == $post_data['_status'] ) {
   426 		$post_data['post_status'] = null;
   438 		$post_data['post_status'] = null;
   427 		unset($post_data['post_status']);
   439 		unset($post_data['post_status']);
   550 		if ( is_wp_error( $post_data ) ) {
   562 		if ( is_wp_error( $post_data ) ) {
   551 			$skipped[] = $post_ID;
   563 			$skipped[] = $post_ID;
   552 			continue;
   564 			continue;
   553 		}
   565 		}
   554 
   566 
       
   567 		if ( isset( $post_data['post_format'] ) ) {
       
   568 			set_post_format( $post_ID, $post_data['post_format'] );
       
   569 			unset( $post_data['tax_input']['post_format'] );
       
   570 		}
       
   571 
   555 		$updated[] = wp_update_post( $post_data );
   572 		$updated[] = wp_update_post( $post_data );
   556 
   573 
   557 		if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) {
   574 		if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) {
   558 			if ( 'sticky' == $post_data['sticky'] )
   575 			if ( 'sticky' == $post_data['sticky'] )
   559 				stick_post( $post_ID );
   576 				stick_post( $post_ID );
   560 			else
   577 			else
   561 				unstick_post( $post_ID );
   578 				unstick_post( $post_ID );
   562 		}
   579 		}
   563 
       
   564 		if ( isset( $post_data['post_format'] ) )
       
   565 			set_post_format( $post_ID, $post_data['post_format'] );
       
   566 	}
   580 	}
   567 
   581 
   568 	return array( 'updated' => $updated, 'skipped' => $skipped, 'locked' => $locked );
   582 	return array( 'updated' => $updated, 'skipped' => $skipped, 'locked' => $locked );
   569 }
   583 }
   570 
   584 
   571 /**
   585 /**
   572  * Default post information to use when populating the "Write Post" form.
   586  * Default post information to use when populating the "Write Post" form.
   573  *
   587  *
   574  * @since 2.0.0
   588  * @since 2.0.0
   575  *
   589  *
   576  * @param string $post_type A post type string, defaults to 'post'.
   590  * @param string $post_type    Optional. A post type string. Default 'post'.
       
   591  * @param bool   $create_in_db Optional. Whether to insert the post into database. Default false.
   577  * @return WP_Post Post object containing all the default post data as attributes
   592  * @return WP_Post Post object containing all the default post data as attributes
   578  */
   593  */
   579 function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) {
   594 function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) {
   580 	$post_title = '';
   595 	$post_title = '';
   581 	if ( !empty( $_REQUEST['post_title'] ) )
   596 	if ( !empty( $_REQUEST['post_title'] ) )
   604 		$post->post_name = '';
   619 		$post->post_name = '';
   605 		$post->post_type = $post_type;
   620 		$post->post_type = $post_type;
   606 		$post->post_status = 'draft';
   621 		$post->post_status = 'draft';
   607 		$post->to_ping = '';
   622 		$post->to_ping = '';
   608 		$post->pinged = '';
   623 		$post->pinged = '';
   609 		$post->comment_status = get_option( 'default_comment_status' );
   624 		$post->comment_status = get_default_comment_status( $post_type );
   610 		$post->ping_status = get_option( 'default_ping_status' );
   625 		$post->ping_status = get_default_comment_status( $post_type, 'pingback' );
   611 		$post->post_pingback = get_option( 'default_pingback_flag' );
   626 		$post->post_pingback = get_option( 'default_pingback_flag' );
   612 		$post->post_category = get_option( 'default_category' );
   627 		$post->post_category = get_option( 'default_category' );
   613 		$post->page_template = 'default';
   628 		$post->page_template = 'default';
   614 		$post->post_parent = 0;
   629 		$post->post_parent = 0;
   615 		$post->menu_order = 0;
   630 		$post->menu_order = 0;
   616 		$post = new WP_Post( $post );
   631 		$post = new WP_Post( $post );
   617 	}
   632 	}
   618 
   633 
   619 	/**
   634 	/**
   620 	 * Filter the default post content initially used in the "Write Post" form.
   635 	 * Filters the default post content initially used in the "Write Post" form.
   621 	 *
   636 	 *
   622 	 * @since 1.5.0
   637 	 * @since 1.5.0
   623 	 *
   638 	 *
   624 	 * @param string  $post_content Default post content.
   639 	 * @param string  $post_content Default post content.
   625 	 * @param WP_Post $post         Post object.
   640 	 * @param WP_Post $post         Post object.
   626 	 */
   641 	 */
   627 	$post->post_content = apply_filters( 'default_content', $post_content, $post );
   642 	$post->post_content = apply_filters( 'default_content', $post_content, $post );
   628 
   643 
   629 	/**
   644 	/**
   630 	 * Filter the default post title initially used in the "Write Post" form.
   645 	 * Filters the default post title initially used in the "Write Post" form.
   631 	 *
   646 	 *
   632 	 * @since 1.5.0
   647 	 * @since 1.5.0
   633 	 *
   648 	 *
   634 	 * @param string  $post_title Default post title.
   649 	 * @param string  $post_title Default post title.
   635 	 * @param WP_Post $post       Post object.
   650 	 * @param WP_Post $post       Post object.
   636 	 */
   651 	 */
   637 	$post->post_title = apply_filters( 'default_title', $post_title, $post );
   652 	$post->post_title = apply_filters( 'default_title', $post_title, $post );
   638 
   653 
   639 	/**
   654 	/**
   640 	 * Filter the default post excerpt initially used in the "Write Post" form.
   655 	 * Filters the default post excerpt initially used in the "Write Post" form.
   641 	 *
   656 	 *
   642 	 * @since 1.5.0
   657 	 * @since 1.5.0
   643 	 *
   658 	 *
   644 	 * @param string  $post_excerpt Default post excerpt.
   659 	 * @param string  $post_excerpt Default post excerpt.
   645 	 * @param WP_Post $post         Post object.
   660 	 * @param WP_Post $post         Post object.
   652 /**
   667 /**
   653  * Determine if a post exists based on title, content, and date
   668  * Determine if a post exists based on title, content, and date
   654  *
   669  *
   655  * @since 2.0.0
   670  * @since 2.0.0
   656  *
   671  *
       
   672  * @global wpdb $wpdb WordPress database abstraction object.
       
   673  *
   657  * @param string $title Post title
   674  * @param string $title Post title
   658  * @param string $content Optional post content
   675  * @param string $content Optional post content
   659  * @param string $date Optional post date
   676  * @param string $date Optional post date
   660  * @return int Post ID if post exists, 0 otherwise.
   677  * @return int Post ID if post exists, 0 otherwise.
   661  */
   678  */
   678 		$query .= ' AND post_title = %s';
   695 		$query .= ' AND post_title = %s';
   679 		$args[] = $post_title;
   696 		$args[] = $post_title;
   680 	}
   697 	}
   681 
   698 
   682 	if ( !empty ( $content ) ) {
   699 	if ( !empty ( $content ) ) {
   683 		$query .= 'AND post_content = %s';
   700 		$query .= ' AND post_content = %s';
   684 		$args[] = $post_content;
   701 		$args[] = $post_content;
   685 	}
   702 	}
   686 
   703 
   687 	if ( !empty ( $args ) )
   704 	if ( !empty ( $args ) )
   688 		return (int) $wpdb->get_var( $wpdb->prepare($query, $args) );
   705 		return (int) $wpdb->get_var( $wpdb->prepare($query, $args) );
   692 
   709 
   693 /**
   710 /**
   694  * Creates a new post from the "Write Post" form using $_POST information.
   711  * Creates a new post from the "Write Post" form using $_POST information.
   695  *
   712  *
   696  * @since 2.1.0
   713  * @since 2.1.0
       
   714  *
       
   715  * @global WP_User $current_user
   697  *
   716  *
   698  * @return int|WP_Error
   717  * @return int|WP_Error
   699  */
   718  */
   700 function wp_write_post() {
   719 function wp_write_post() {
   701 	if ( isset($_POST['post_type']) )
   720 	if ( isset($_POST['post_type']) )
   703 	else
   722 	else
   704 		$ptype = get_post_type_object('post');
   723 		$ptype = get_post_type_object('post');
   705 
   724 
   706 	if ( !current_user_can( $ptype->cap->edit_posts ) ) {
   725 	if ( !current_user_can( $ptype->cap->edit_posts ) ) {
   707 		if ( 'page' == $ptype->name )
   726 		if ( 'page' == $ptype->name )
   708 			return new WP_Error( 'edit_pages', __( 'You are not allowed to create pages on this site.' ) );
   727 			return new WP_Error( 'edit_pages', __( 'Sorry, you are not allowed to create pages on this site.' ) );
   709 		else
   728 		else
   710 			return new WP_Error( 'edit_posts', __( 'You are not allowed to create posts or drafts on this site.' ) );
   729 			return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to create posts or drafts on this site.' ) );
   711 	}
   730 	}
   712 
   731 
   713 	$_POST['post_mime_type'] = '';
   732 	$_POST['post_mime_type'] = '';
   714 
   733 
   715 	// Clear out any data in internal vars.
   734 	// Clear out any data in internal vars.
   832 /**
   851 /**
   833  * Get a list of previously defined keys.
   852  * Get a list of previously defined keys.
   834  *
   853  *
   835  * @since 1.2.0
   854  * @since 1.2.0
   836  *
   855  *
       
   856  * @global wpdb $wpdb WordPress database abstraction object.
       
   857  *
   837  * @return mixed
   858  * @return mixed
   838  */
   859  */
   839 function get_meta_keys() {
   860 function get_meta_keys() {
   840 	global $wpdb;
   861 	global $wpdb;
   841 
   862 
   862 
   883 
   863 /**
   884 /**
   864  * Get meta data for the given post ID.
   885  * Get meta data for the given post ID.
   865  *
   886  *
   866  * @since 1.2.0
   887  * @since 1.2.0
       
   888  *
       
   889  * @global wpdb $wpdb WordPress database abstraction object.
   867  *
   890  *
   868  * @param int $postid
   891  * @param int $postid
   869  * @return mixed
   892  * @return mixed
   870  */
   893  */
   871 function has_meta( $postid ) {
   894 function has_meta( $postid ) {
   989 	if ( isset($q['post_status']) && in_array( $q['post_status'], $post_stati ) ) {
  1012 	if ( isset($q['post_status']) && in_array( $q['post_status'], $post_stati ) ) {
   990 		$post_status = $q['post_status'];
  1013 		$post_status = $q['post_status'];
   991 		$perm = 'readable';
  1014 		$perm = 'readable';
   992 	}
  1015 	}
   993 
  1016 
   994 	if ( isset($q['orderby']) )
  1017 	if ( isset( $q['orderby'] ) ) {
   995 		$orderby = $q['orderby'];
  1018 		$orderby = $q['orderby'];
   996 	elseif ( isset($q['post_status']) && in_array($q['post_status'], array('pending', 'draft')) )
  1019 	} elseif ( isset( $q['post_status'] ) && in_array( $q['post_status'], array( 'pending', 'draft' ) ) ) {
   997 		$orderby = 'modified';
  1020 		$orderby = 'modified';
   998 
  1021 	}
   999 	if ( isset($q['order']) )
  1022 
       
  1023 	if ( isset( $q['order'] ) ) {
  1000 		$order = $q['order'];
  1024 		$order = $q['order'];
  1001 	elseif ( isset($q['post_status']) && 'pending' == $q['post_status'] )
  1025 	} elseif ( isset( $q['post_status'] ) && 'pending' == $q['post_status'] ) {
  1002 		$order = 'ASC';
  1026 		$order = 'ASC';
       
  1027 	}
  1003 
  1028 
  1004 	$per_page = "edit_{$post_type}_per_page";
  1029 	$per_page = "edit_{$post_type}_per_page";
  1005 	$posts_per_page = (int) get_user_option( $per_page );
  1030 	$posts_per_page = (int) get_user_option( $per_page );
  1006 	if ( empty( $posts_per_page ) || $posts_per_page < 1 )
  1031 	if ( empty( $posts_per_page ) || $posts_per_page < 1 )
  1007 		$posts_per_page = 20;
  1032 		$posts_per_page = 20;
  1008 
  1033 
  1009 	/**
  1034 	/**
  1010 	 * Filter the number of items per page to show for a specific 'per_page' type.
  1035 	 * Filters the number of items per page to show for a specific 'per_page' type.
  1011 	 *
  1036 	 *
  1012 	 * The dynamic portion of the hook name, `$post_type`, refers to the post type.
  1037 	 * The dynamic portion of the hook name, `$post_type`, refers to the post type.
  1013 	 *
  1038 	 *
  1014 	 * Some examples of filter hooks generated here include: 'edit_attachment_per_page',
  1039 	 * Some examples of filter hooks generated here include: 'edit_attachment_per_page',
  1015 	 * 'edit_post_per_page', 'edit_page_per_page', etc.
  1040 	 * 'edit_post_per_page', 'edit_page_per_page', etc.
  1020 	 *                            type. Default 20.
  1045 	 *                            type. Default 20.
  1021 	 */
  1046 	 */
  1022 	$posts_per_page = apply_filters( "edit_{$post_type}_per_page", $posts_per_page );
  1047 	$posts_per_page = apply_filters( "edit_{$post_type}_per_page", $posts_per_page );
  1023 
  1048 
  1024 	/**
  1049 	/**
  1025 	 * Filter the number of posts displayed per page when specifically listing "posts".
  1050 	 * Filters the number of posts displayed per page when specifically listing "posts".
  1026 	 *
  1051 	 *
  1027 	 * @since 2.8.0
  1052 	 * @since 2.8.0
  1028 	 *
  1053 	 *
  1029 	 * @param int    $posts_per_page Number of posts to be displayed. Default 20.
  1054 	 * @param int    $posts_per_page Number of posts to be displayed. Default 20.
  1030 	 * @param string $post_type      The post type.
  1055 	 * @param string $post_type      The post type.
  1053 /**
  1078 /**
  1054  * Get all available post MIME types for a given post type.
  1079  * Get all available post MIME types for a given post type.
  1055  *
  1080  *
  1056  * @since 2.5.0
  1081  * @since 2.5.0
  1057  *
  1082  *
       
  1083  * @global wpdb $wpdb WordPress database abstraction object.
       
  1084  *
  1058  * @param string $type
  1085  * @param string $type
  1059  * @return mixed
  1086  * @return mixed
  1060  */
  1087  */
  1061 function get_available_post_mime_types($type = 'attachment') {
  1088 function get_available_post_mime_types($type = 'attachment') {
  1062 	global $wpdb;
  1089 	global $wpdb;
  1094 	if ( empty( $media_per_page ) || $media_per_page < 1 ) {
  1121 	if ( empty( $media_per_page ) || $media_per_page < 1 ) {
  1095 		$media_per_page = 20;
  1122 		$media_per_page = 20;
  1096 	}
  1123 	}
  1097 
  1124 
  1098 	/**
  1125 	/**
  1099 	 * Filter the number of items to list per page when listing media items.
  1126 	 * Filters the number of items to list per page when listing media items.
  1100 	 *
  1127 	 *
  1101 	 * @since 2.9.0
  1128 	 * @since 2.9.0
  1102 	 *
  1129 	 *
  1103 	 * @param int $media_per_page Number of media to list. Default 20.
  1130 	 * @param int $media_per_page Number of media to list. Default 20.
  1104 	 */
  1131 	 */
  1107 	$post_mime_types = get_post_mime_types();
  1134 	$post_mime_types = get_post_mime_types();
  1108 	if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) ) {
  1135 	if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) ) {
  1109 		unset($q['post_mime_type']);
  1136 		unset($q['post_mime_type']);
  1110 	}
  1137 	}
  1111 
  1138 
  1112 	foreach( array_keys( $post_mime_types ) as $type ) {
  1139 	foreach ( array_keys( $post_mime_types ) as $type ) {
  1113 		if ( isset( $q['attachment-filter'] ) && "post_mime_type:$type" == $q['attachment-filter'] ) {
  1140 		if ( isset( $q['attachment-filter'] ) && "post_mime_type:$type" == $q['attachment-filter'] ) {
  1114 			$q['post_mime_type'] = $type;
  1141 			$q['post_mime_type'] = $type;
  1115 			break;
  1142 			break;
  1116 		}
  1143 		}
  1117 	}
  1144 	}
  1118 
  1145 
  1119 	if ( isset( $q['detached'] ) || ( isset( $q['attachment-filter'] ) && 'detached' == $q['attachment-filter'] ) ) {
  1146 	if ( isset( $q['detached'] ) || ( isset( $q['attachment-filter'] ) && 'detached' == $q['attachment-filter'] ) ) {
  1120 		$q['post_parent'] = 0;
  1147 		$q['post_parent'] = 0;
  1121 	}
  1148 	}
  1122 
  1149 
       
  1150 	if ( isset( $q['mine'] ) || ( isset( $q['attachment-filter'] ) && 'mine' == $q['attachment-filter'] ) ) {
       
  1151 		$q['author'] = get_current_user_id();
       
  1152 	}
       
  1153 
       
  1154 	// Filter query clauses to include filenames.
       
  1155 	if ( isset( $q['s'] ) ) {
       
  1156 		add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
       
  1157 	}
       
  1158 
  1123 	return $q;
  1159 	return $q;
  1124 }
  1160 }
  1125 
  1161 
  1126 /**
  1162 /**
  1127  * Executes a query for attachments. An array of WP_Query arguments
  1163  * Executes a query for attachments. An array of WP_Query arguments
  1140 
  1176 
  1141 	return array( $post_mime_types, $avail_post_mime_types );
  1177 	return array( $post_mime_types, $avail_post_mime_types );
  1142 }
  1178 }
  1143 
  1179 
  1144 /**
  1180 /**
  1145  * Returns the list of classes to be used by a metabox
  1181  * Returns the list of classes to be used by a meta box.
  1146  *
  1182  *
  1147  * @since 2.5.0
  1183  * @since 2.5.0
  1148  *
  1184  *
  1149  * @param string $id
  1185  * @param string $id
  1150  * @param string $page
  1186  * @param string $page
  1162 	} else {
  1198 	} else {
  1163 		$classes = array( '' );
  1199 		$classes = array( '' );
  1164 	}
  1200 	}
  1165 
  1201 
  1166 	/**
  1202 	/**
  1167 	 * Filter the postbox classes for a specific screen and screen ID combo.
  1203 	 * Filters the postbox classes for a specific screen and screen ID combo.
  1168 	 *
  1204 	 *
  1169 	 * The dynamic portions of the hook name, `$page` and `$id`, refer to
  1205 	 * The dynamic portions of the hook name, `$page` and `$id`, refer to
  1170 	 * the screen and screen ID, respectively.
  1206 	 * the screen and screen ID, respectively.
  1171 	 *
  1207 	 *
  1172 	 * @since 3.2.0
  1208 	 * @since 3.2.0
  1181  * Get a sample permalink based off of the post name.
  1217  * Get a sample permalink based off of the post name.
  1182  *
  1218  *
  1183  * @since 2.5.0
  1219  * @since 2.5.0
  1184  *
  1220  *
  1185  * @param int    $id    Post ID or post object.
  1221  * @param int    $id    Post ID or post object.
  1186  * @param string $title Optional. Title. Default null.
  1222  * @param string $title Optional. Title to override the post's current title when generating the post name. Default null.
  1187  * @param string $name  Optional. Name. Default null.
  1223  * @param string $name  Optional. Name to override the post name. Default null.
  1188  * @return array Array with two entries of type string.
  1224  * @return array Array containing the sample permalink with placeholder for the post name, and the post name.
  1189  */
  1225  */
  1190 function get_sample_permalink($id, $title = null, $name = null) {
  1226 function get_sample_permalink($id, $title = null, $name = null) {
  1191 	$post = get_post( $id );
  1227 	$post = get_post( $id );
  1192 	if ( ! $post )
  1228 	if ( ! $post )
  1193 		return array( '', '' );
  1229 		return array( '', '' );
  1226 			$uri = strrev( stristr( strrev( $uri ), '/' ) );
  1262 			$uri = strrev( stristr( strrev( $uri ), '/' ) );
  1227 			$uri = untrailingslashit($uri);
  1263 			$uri = untrailingslashit($uri);
  1228 		}
  1264 		}
  1229 
  1265 
  1230 		/** This filter is documented in wp-admin/edit-tag-form.php */
  1266 		/** This filter is documented in wp-admin/edit-tag-form.php */
  1231 		$uri = apply_filters( 'editable_slug', $uri );
  1267 		$uri = apply_filters( 'editable_slug', $uri, $post );
  1232 		if ( !empty($uri) )
  1268 		if ( !empty($uri) )
  1233 			$uri .= '/';
  1269 			$uri .= '/';
  1234 		$permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink);
  1270 		$permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink);
  1235 	}
  1271 	}
  1236 
  1272 
  1237 	/** This filter is documented in wp-admin/edit-tag-form.php */
  1273 	/** This filter is documented in wp-admin/edit-tag-form.php */
  1238 	$permalink = array( $permalink, apply_filters( 'editable_slug', $post->post_name ) );
  1274 	$permalink = array( $permalink, apply_filters( 'editable_slug', $post->post_name, $post ) );
  1239 	$post->post_status = $original_status;
  1275 	$post->post_status = $original_status;
  1240 	$post->post_date = $original_date;
  1276 	$post->post_date = $original_date;
  1241 	$post->post_name = $original_name;
  1277 	$post->post_name = $original_name;
  1242 	unset($post->filter);
  1278 	unset($post->filter);
  1243 
  1279 
  1244 	return $permalink;
  1280 	/**
       
  1281 	 * Filters the sample permalink.
       
  1282 	 *
       
  1283 	 * @since 4.4.0
       
  1284 	 *
       
  1285 	 * @param array   $permalink Array containing the sample permalink with placeholder for the post name, and the post name.
       
  1286 	 * @param int     $post_id   Post ID.
       
  1287 	 * @param string  $title     Post title.
       
  1288 	 * @param string  $name      Post name (slug).
       
  1289 	 * @param WP_Post $post      Post object.
       
  1290 	 */
       
  1291 	return apply_filters( 'get_sample_permalink', $permalink, $post->ID, $title, $name, $post );
  1245 }
  1292 }
  1246 
  1293 
  1247 /**
  1294 /**
  1248  * Returns the HTML of the sample permalink slug editor.
  1295  * Returns the HTML of the sample permalink slug editor.
  1249  *
  1296  *
  1259 	if ( ! $post )
  1306 	if ( ! $post )
  1260 		return '';
  1307 		return '';
  1261 
  1308 
  1262 	list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
  1309 	list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
  1263 
  1310 
       
  1311 	$view_link = false;
       
  1312 	$preview_target = '';
       
  1313 
  1264 	if ( current_user_can( 'read_post', $post->ID ) ) {
  1314 	if ( current_user_can( 'read_post', $post->ID ) ) {
  1265 		$ptype = get_post_type_object( $post->post_type );
  1315 		if ( 'draft' === $post->post_status || empty( $post->post_name ) ) {
  1266 		$view_post = $ptype->labels->view_item;
  1316 			$view_link = get_preview_post_link( $post );
  1267 	}
  1317 			$preview_target = " target='wp-preview-{$post->ID}'";
  1268 
  1318 		} else {
  1269 	if ( 'publish' == get_post_status( $post ) ) {
  1319 			if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) {
  1270 		$title = __('Click to edit this part of the permalink');
  1320 				$view_link = get_permalink( $post );
  1271 	} else {
  1321 			} else {
  1272 		$title = __('Temporary permalink. Click to edit this part.');
  1322 				// Allow non-published (private, future) to be viewed at a pretty permalink, in case $post->post_name is set
  1273 	}
  1323 				$view_link = str_replace( array( '%pagename%', '%postname%' ), $post->post_name, $permalink );
  1274 
  1324 			}
       
  1325 		}
       
  1326 	}
       
  1327 
       
  1328 	// Permalinks without a post/page name placeholder don't have anything to edit
  1275 	if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) {
  1329 	if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) {
  1276 		$return = '<strong>' . __('Permalink:') . "</strong>\n" . '<span id="sample-permalink" tabindex="-1">' . $permalink . "</span>\n";
  1330 		$return = '<strong>' . __( 'Permalink:' ) . "</strong>\n";
       
  1331 
       
  1332 		if ( false !== $view_link ) {
       
  1333 			$display_link = urldecode( $view_link );
       
  1334 			$return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . esc_html( $display_link ) . "</a>\n";
       
  1335 		} else {
       
  1336 			$return .= '<span id="sample-permalink">' . $permalink . "</span>\n";
       
  1337 		}
       
  1338 
       
  1339 		// Encourage a pretty permalink setting
  1277 		if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) ) {
  1340 		if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) ) {
  1278 			$return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __('Change Permalinks') . "</a></span>\n";
  1341 			$return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __('Change Permalinks') . "</a></span>\n";
  1279 		}
  1342 		}
  1280 	} else {
  1343 	} else {
  1281 		if ( function_exists( 'mb_strlen' ) ) {
  1344 		if ( mb_strlen( $post_name ) > 34 ) {
  1282 			if ( mb_strlen( $post_name ) > 30 ) {
  1345 			$post_name_abridged = mb_substr( $post_name, 0, 16 ) . '&hellip;' . mb_substr( $post_name, -16 );
  1283 				$post_name_abridged = mb_substr( $post_name, 0, 14 ) . '&hellip;' . mb_substr( $post_name, -14 );
       
  1284 			} else {
       
  1285 				$post_name_abridged = $post_name;
       
  1286 			}
       
  1287 		} else {
  1346 		} else {
  1288 			if ( strlen( $post_name ) > 30 ) {
  1347 			$post_name_abridged = $post_name;
  1289 				$post_name_abridged = substr( $post_name, 0, 14 ) . '&hellip;' . substr( $post_name, -14 );
  1348 		}
  1290 			} else {
  1349 
  1291 				$post_name_abridged = $post_name;
  1350 		$post_name_html = '<span id="editable-post-name">' . esc_html( $post_name_abridged ) . '</span>';
  1292 			}
  1351 		$display_link = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, esc_html( urldecode( $permalink ) ) );
  1293 		}
  1352 
  1294 
  1353 		$return = '<strong>' . __( 'Permalink:' ) . "</strong>\n";
  1295 		$post_name_html = '<span id="editable-post-name" title="' . $title . '">' . $post_name_abridged . '</span>';
  1354 		$return .= '<span id="sample-permalink"><a href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $display_link . "</a></span>\n";
  1296 		$display_link = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, urldecode( $permalink ) );
       
  1297 		$pretty_permalink = str_replace( array( '%pagename%', '%postname%' ), $post_name, urldecode( $permalink ) );
       
  1298 
       
  1299 		$return =  '<strong>' . __( 'Permalink:' ) . "</strong>\n";
       
  1300 		$return .= '<span id="sample-permalink" tabindex="-1">' . $display_link . "</span>\n";
       
  1301 		$return .= '&lrm;'; // Fix bi-directional text display defect in RTL languages.
  1355 		$return .= '&lrm;'; // Fix bi-directional text display defect in RTL languages.
  1302 		$return .= '<span id="edit-slug-buttons"><a href="#post_name" class="edit-slug button button-small hide-if-no-js" onclick="editPermalink(' . $id . '); return false;">' . __( 'Edit' ) . "</a></span>\n";
  1356 		$return .= '<span id="edit-slug-buttons"><button type="button" class="edit-slug button button-small hide-if-no-js" aria-label="' . __( 'Edit permalink' ) . '">' . __( 'Edit' ) . "</button></span>\n";
  1303 		$return .= '<span id="editable-post-name-full">' . $post_name . "</span>\n";
  1357 		$return .= '<span id="editable-post-name-full">' . esc_html( $post_name ) . "</span>\n";
  1304 	}
       
  1305 
       
  1306 	if ( isset( $view_post ) ) {
       
  1307 		if( 'draft' == $post->post_status ) {
       
  1308 			$preview_link = set_url_scheme( get_permalink( $post->ID ) );
       
  1309 			/** This filter is documented in wp-admin/includes/meta-boxes.php */
       
  1310 			$preview_link = apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ), $post );
       
  1311 			$return .= "<span id='view-post-btn'><a href='" . esc_url( $preview_link ) . "' class='button button-small' target='wp-preview-{$post->ID}'>$view_post</a></span>\n";
       
  1312 		} else {
       
  1313 			if ( empty( $pretty_permalink ) ) {
       
  1314 				$pretty_permalink = $permalink;
       
  1315 			}
       
  1316 
       
  1317 			$return .= "<span id='view-post-btn'><a href='" . $pretty_permalink . "' class='button button-small'>$view_post</a></span>\n";
       
  1318 		}
       
  1319 	}
  1358 	}
  1320 
  1359 
  1321 	/**
  1360 	/**
  1322 	 * Filter the sample permalink HTML markup.
  1361 	 * Filters the sample permalink HTML markup.
  1323 	 *
  1362 	 *
  1324 	 * @since 2.9.0
  1363 	 * @since 2.9.0
  1325 	 *
  1364 	 * @since 4.4.0 Added `$post` parameter.
  1326 	 * @param string      $return    Sample permalink HTML markup.
  1365 	 *
  1327 	 * @param int|WP_Post $id        Post object or ID.
  1366 	 * @param string  $return    Sample permalink HTML markup.
  1328 	 * @param string      $new_title New sample permalink title.
  1367 	 * @param int     $post_id   Post ID.
  1329 	 * @param string      $new_slug  New sample permalink slug.
  1368 	 * @param string  $new_title New sample permalink title.
       
  1369 	 * @param string  $new_slug  New sample permalink slug.
       
  1370 	 * @param WP_Post $post      Post object.
  1330 	 */
  1371 	 */
  1331 	$return = apply_filters( 'get_sample_permalink_html', $return, $id, $new_title, $new_slug );
  1372 	$return = apply_filters( 'get_sample_permalink_html', $return, $post->ID, $new_title, $new_slug, $post );
  1332 
  1373 
  1333 	return $return;
  1374 	return $return;
  1334 }
  1375 }
  1335 
  1376 
  1336 /**
  1377 /**
  1341  * @param int $thumbnail_id ID of the attachment used for thumbnail
  1382  * @param int $thumbnail_id ID of the attachment used for thumbnail
  1342  * @param mixed $post The post ID or object associated with the thumbnail, defaults to global $post.
  1383  * @param mixed $post The post ID or object associated with the thumbnail, defaults to global $post.
  1343  * @return string html
  1384  * @return string html
  1344  */
  1385  */
  1345 function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
  1386 function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
  1346 	global $content_width, $_wp_additional_image_sizes;
  1387 	$_wp_additional_image_sizes = wp_get_additional_image_sizes();
  1347 
  1388 
  1348 	$post = get_post( $post );
  1389 	$post               = get_post( $post );
  1349 
  1390 	$post_type_object   = get_post_type_object( $post->post_type );
  1350 	$upload_iframe_src = esc_url( get_upload_iframe_src('image', $post->ID ) );
  1391 	$set_thumbnail_link = '<p class="hide-if-no-js"><a href="%s" id="set-post-thumbnail"%s class="thickbox">%s</a></p>';
  1351 	$set_thumbnail_link = '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set featured image' ) . '" href="%s" id="set-post-thumbnail" class="thickbox">%s</a></p>';
  1392 	$upload_iframe_src  = get_upload_iframe_src( 'image', $post->ID );
  1352 	$content = sprintf( $set_thumbnail_link, $upload_iframe_src, esc_html__( 'Set featured image' ) );
  1393 
       
  1394 	$content = sprintf( $set_thumbnail_link,
       
  1395 		esc_url( $upload_iframe_src ),
       
  1396 		'', // Empty when there's no featured image set, `aria-describedby` attribute otherwise.
       
  1397 		esc_html( $post_type_object->labels->set_featured_image )
       
  1398 	);
  1353 
  1399 
  1354 	if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
  1400 	if ( $thumbnail_id && get_post( $thumbnail_id ) ) {
  1355 		$old_content_width = $content_width;
  1401 		$size = isset( $_wp_additional_image_sizes['post-thumbnail'] ) ? 'post-thumbnail' : array( 266, 266 );
  1356 		$content_width = 266;
  1402 
  1357 		if ( !isset( $_wp_additional_image_sizes['post-thumbnail'] ) )
  1403 		/**
  1358 			$thumbnail_html = wp_get_attachment_image( $thumbnail_id, array( $content_width, $content_width ) );
  1404 		 * Filters the size used to display the post thumbnail image in the 'Featured Image' meta box.
  1359 		else
  1405 		 *
  1360 			$thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'post-thumbnail' );
  1406 		 * Note: When a theme adds 'post-thumbnail' support, a special 'post-thumbnail'
  1361 		if ( !empty( $thumbnail_html ) ) {
  1407 		 * image size is registered, which differs from the 'thumbnail' image size
  1362 			$ajax_nonce = wp_create_nonce( 'set_post_thumbnail-' . $post->ID );
  1408 		 * managed via the Settings > Media screen. See the `$size` parameter description
  1363 			$content = sprintf( $set_thumbnail_link, $upload_iframe_src, $thumbnail_html );
  1409 		 * for more information on default values.
  1364 			$content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\'' . $ajax_nonce . '\');return false;">' . esc_html__( 'Remove featured image' ) . '</a></p>';
  1410 		 *
  1365 		}
  1411 		 * @since 4.4.0
  1366 		$content_width = $old_content_width;
  1412 		 *
  1367 	}
  1413 		 * @param string|array $size         Post thumbnail image size to display in the meta box. Accepts any valid
       
  1414 		 *                                   image size, or an array of width and height values in pixels (in that order).
       
  1415 		 *                                   If the 'post-thumbnail' size is set, default is 'post-thumbnail'. Otherwise,
       
  1416 		 *                                   default is an array with 266 as both the height and width values.
       
  1417 		 * @param int          $thumbnail_id Post thumbnail attachment ID.
       
  1418 		 * @param WP_Post      $post         The post object associated with the thumbnail.
       
  1419 		 */
       
  1420 		$size = apply_filters( 'admin_post_thumbnail_size', $size, $thumbnail_id, $post );
       
  1421 
       
  1422 		$thumbnail_html = wp_get_attachment_image( $thumbnail_id, $size );
       
  1423 
       
  1424 		if ( ! empty( $thumbnail_html ) ) {
       
  1425 			$content = sprintf( $set_thumbnail_link,
       
  1426 				esc_url( $upload_iframe_src ),
       
  1427 				' aria-describedby="set-post-thumbnail-desc"',
       
  1428 				$thumbnail_html
       
  1429 			);
       
  1430 			$content .= '<p class="hide-if-no-js howto" id="set-post-thumbnail-desc">' . __( 'Click the image to edit or update' ) . '</p>';
       
  1431 			$content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail">' . esc_html( $post_type_object->labels->remove_featured_image ) . '</a></p>';
       
  1432 		}
       
  1433 	}
       
  1434 
       
  1435 	$content .= '<input type="hidden" id="_thumbnail_id" name="_thumbnail_id" value="' . esc_attr( $thumbnail_id ? $thumbnail_id : '-1' ) . '" />';
  1368 
  1436 
  1369 	/**
  1437 	/**
  1370 	 * Filter the admin post thumbnail HTML markup to return.
  1438 	 * Filters the admin post thumbnail HTML markup to return.
  1371 	 *
  1439 	 *
  1372 	 * @since 2.9.0
  1440 	 * @since 2.9.0
  1373 	 *
  1441 	 * @since 3.5.0 Added the `$post_id` parameter.
  1374 	 * @param string $content Admin post thumbnail HTML markup.
  1442 	 * @since 4.6.0 Added the `$thumbnail_id` parameter.
  1375 	 * @param int    $post_id Post ID.
  1443 	 *
       
  1444 	 * @param string $content      Admin post thumbnail HTML markup.
       
  1445 	 * @param int    $post_id      Post ID.
       
  1446 	 * @param int    $thumbnail_id Thumbnail ID.
  1376 	 */
  1447 	 */
  1377 	return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID );
  1448 	return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID, $thumbnail_id );
  1378 }
  1449 }
  1379 
  1450 
  1380 /**
  1451 /**
  1381  * Check to see if the post is currently being edited by another user.
  1452  * Check to see if the post is currently being edited by another user.
  1382  *
  1453  *
  1383  * @since 2.5.0
  1454  * @since 2.5.0
  1384  *
  1455  *
  1385  * @param int $post_id ID of the post to check for editing
  1456  * @param int $post_id ID of the post to check for editing.
  1386  * @return integer False: not locked or locked by current user. Int: user ID of user with lock.
  1457  * @return int|false ID of the user with lock. False if the post does not exist, post is not locked,
       
  1458  *                   the user with lock does not exist, or the post is locked by current user.
  1387  */
  1459  */
  1388 function wp_check_post_lock( $post_id ) {
  1460 function wp_check_post_lock( $post_id ) {
  1389 	if ( !$post = get_post( $post_id ) )
  1461 	if ( ! $post = get_post( $post_id ) ) {
  1390 		return false;
  1462 		return false;
  1391 
  1463 	}
  1392 	if ( !$lock = get_post_meta( $post->ID, '_edit_lock', true ) )
  1464 
       
  1465 	if ( ! $lock = get_post_meta( $post->ID, '_edit_lock', true ) ) {
  1393 		return false;
  1466 		return false;
       
  1467 	}
  1394 
  1468 
  1395 	$lock = explode( ':', $lock );
  1469 	$lock = explode( ':', $lock );
  1396 	$time = $lock[0];
  1470 	$time = $lock[0];
  1397 	$user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
  1471 	$user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
  1398 
  1472 
       
  1473 	if ( ! get_userdata( $user ) ) {
       
  1474 		return false;
       
  1475 	}
       
  1476 
  1399 	/** This filter is documented in wp-admin/includes/ajax-actions.php */
  1477 	/** This filter is documented in wp-admin/includes/ajax-actions.php */
  1400 	$time_window = apply_filters( 'wp_check_post_lock_window', 150 );
  1478 	$time_window = apply_filters( 'wp_check_post_lock_window', 150 );
  1401 
  1479 
  1402 	if ( $time && $time > time() - $time_window && $user != get_current_user_id() )
  1480 	if ( $time && $time > time() - $time_window && $user != get_current_user_id() ) {
  1403 		return $user;
  1481 		return $user;
       
  1482 	}
       
  1483 
  1404 	return false;
  1484 	return false;
  1405 }
  1485 }
  1406 
  1486 
  1407 /**
  1487 /**
  1408  * Mark the post as currently being edited by the current user
  1488  * Mark the post as currently being edited by the current user
  1409  *
  1489  *
  1410  * @since 2.5.0
  1490  * @since 2.5.0
  1411  *
  1491  *
  1412  * @param int $post_id ID of the post to being edited
  1492  * @param int $post_id ID of the post being edited.
  1413  * @return bool|array Returns false if the post doesn't exist of there is no current user, or
  1493  * @return array|false Array of the lock time and user ID. False if the post does not exist, or
  1414  * 	an array of the lock time and the user ID.
  1494  *                     there is no current user.
  1415  */
  1495  */
  1416 function wp_set_post_lock( $post_id ) {
  1496 function wp_set_post_lock( $post_id ) {
  1417 	if ( !$post = get_post( $post_id ) )
  1497 	if ( ! $post = get_post( $post_id ) ) {
  1418 		return false;
  1498 		return false;
  1419 	if ( 0 == ($user_id = get_current_user_id()) )
  1499 	}
       
  1500 
       
  1501 	if ( 0 == ( $user_id = get_current_user_id() ) ) {
  1420 		return false;
  1502 		return false;
       
  1503 	}
  1421 
  1504 
  1422 	$now = time();
  1505 	$now = time();
  1423 	$lock = "$now:$user_id";
  1506 	$lock = "$now:$user_id";
  1424 
  1507 
  1425 	update_post_meta( $post->ID, '_edit_lock', $lock );
  1508 	update_post_meta( $post->ID, '_edit_lock', $lock );
       
  1509 
  1426 	return array( $now, $user_id );
  1510 	return array( $now, $user_id );
  1427 }
  1511 }
  1428 
  1512 
  1429 /**
  1513 /**
  1430  * Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post.
  1514  * Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post.
  1441 		$user = get_userdata( $user_id );
  1525 		$user = get_userdata( $user_id );
  1442 
  1526 
  1443 	if ( $user ) {
  1527 	if ( $user ) {
  1444 
  1528 
  1445 		/**
  1529 		/**
  1446 		 * Filter whether to show the post locked dialog.
  1530 		 * Filters whether to show the post locked dialog.
  1447 		 *
  1531 		 *
  1448 		 * Returning a falsey value to the filter will short-circuit displaying the dialog.
  1532 		 * Returning a falsey value to the filter will short-circuit displaying the dialog.
  1449 		 *
  1533 		 *
  1450 		 * @since 3.6.0
  1534 		 * @since 3.6.0
  1451 		 *
  1535 		 *
  1452 		 * @param bool         $display Whether to display the dialog. Default true.
  1536 		 * @param bool         $display Whether to display the dialog. Default true.
       
  1537 		 * @param WP_Post      $post    Post object.
  1453 		 * @param WP_User|bool $user    WP_User object on success, false otherwise.
  1538 		 * @param WP_User|bool $user    WP_User object on success, false otherwise.
  1454 		 */
  1539 		 */
  1455 		if ( ! apply_filters( 'show_post_locked_dialog', true, $post, $user ) )
  1540 		if ( ! apply_filters( 'show_post_locked_dialog', true, $post, $user ) )
  1456 			return;
  1541 			return;
  1457 
  1542 
  1480 	<div class="notification-dialog-background"></div>
  1565 	<div class="notification-dialog-background"></div>
  1481 	<div class="notification-dialog">
  1566 	<div class="notification-dialog">
  1482 	<?php
  1567 	<?php
  1483 
  1568 
  1484 	if ( $locked ) {
  1569 	if ( $locked ) {
       
  1570 		$query_args = array();
  1485 		if ( get_post_type_object( $post->post_type )->public ) {
  1571 		if ( get_post_type_object( $post->post_type )->public ) {
  1486 			$preview_link = set_url_scheme( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) );
       
  1487 
       
  1488 			if ( 'publish' == $post->post_status || $user->ID != $post->post_author ) {
  1572 			if ( 'publish' == $post->post_status || $user->ID != $post->post_author ) {
  1489 				// Latest content is in autosave
  1573 				// Latest content is in autosave
  1490 				$nonce = wp_create_nonce( 'post_preview_' . $post->ID );
  1574 				$nonce = wp_create_nonce( 'post_preview_' . $post->ID );
  1491 				$preview_link = add_query_arg( array( 'preview_id' => $post->ID, 'preview_nonce' => $nonce ), $preview_link );
  1575 				$query_args['preview_id'] = $post->ID;
       
  1576 				$query_args['preview_nonce'] = $nonce;
  1492 			}
  1577 			}
  1493 		} else {
  1578 		}
  1494 			$preview_link = '';
  1579 
  1495 		}
  1580 		$preview_link = get_preview_post_link( $post->ID, $query_args );
  1496 
       
  1497 		/** This filter is documented in wp-admin/includes/meta-boxes.php */
       
  1498 		$preview_link = apply_filters( 'preview_post_link', $preview_link, $post );
       
  1499 
  1581 
  1500 		/**
  1582 		/**
  1501 		 * Filter whether to allow the post lock to be overridden.
  1583 		 * Filters whether to allow the post lock to be overridden.
  1502 		 *
  1584 		 *
  1503 		 * Returning a falsey value to the filter will disable the ability
  1585 		 * Returning a falsey value to the filter will disable the ability
  1504 		 * to override the post lock.
  1586 		 * to override the post lock.
  1505 		 *
  1587 		 *
  1506 		 * @since 3.6.0
  1588 		 * @since 3.6.0
  1515 		?>
  1597 		?>
  1516 		<div class="post-locked-message">
  1598 		<div class="post-locked-message">
  1517 		<div class="post-locked-avatar"><?php echo get_avatar( $user->ID, 64 ); ?></div>
  1599 		<div class="post-locked-avatar"><?php echo get_avatar( $user->ID, 64 ); ?></div>
  1518 		<p class="currently-editing wp-tab-first" tabindex="0">
  1600 		<p class="currently-editing wp-tab-first" tabindex="0">
  1519 		<?php
  1601 		<?php
  1520 			_e( 'This content is currently locked.' );
  1602 			if ( $override ) {
  1521 			if ( $override )
  1603 				/* translators: %s: user's display name */
  1522 				printf( ' ' . __( 'If you take over, %s will be blocked from continuing to edit.' ), esc_html( $user->display_name ) );
  1604 				printf( __( '%s is already editing this post. Do you want to take over?' ), esc_html( $user->display_name ) );
       
  1605 			} else {
       
  1606 				/* translators: %s: user's display name */
       
  1607 				printf( __( '%s is already editing this post.' ), esc_html( $user->display_name ) );
       
  1608 			}
  1523 		?>
  1609 		?>
  1524 		</p>
  1610 		</p>
  1525 		<?php
  1611 		<?php
  1526 		/**
  1612 		/**
  1527 		 * Fires inside the post locked dialog before the buttons are displayed.
  1613 		 * Fires inside the post locked dialog before the buttons are displayed.
  1540 		}
  1626 		}
  1541 
  1627 
  1542 		// Allow plugins to prevent some users overriding the post lock
  1628 		// Allow plugins to prevent some users overriding the post lock
  1543 		if ( $override ) {
  1629 		if ( $override ) {
  1544 			?>
  1630 			?>
  1545 			<a class="button button-primary wp-tab-last" href="<?php echo esc_url( add_query_arg( 'get-post-lock', '1', get_edit_post_link( $post->ID, 'url' ) ) ); ?>"><?php _e('Take over'); ?></a>
  1631 			<a class="button button-primary wp-tab-last" href="<?php echo esc_url( add_query_arg( 'get-post-lock', '1', wp_nonce_url( get_edit_post_link( $post->ID, 'url' ), 'lock-post_' . $post->ID ) ) ); ?>"><?php _e('Take over'); ?></a>
  1546 			<?php
  1632 			<?php
  1547 		}
  1633 		}
  1548 
  1634 
  1549 		?>
  1635 		?>
  1550 		</p>
  1636 		</p>
  1554 		?>
  1640 		?>
  1555 		<div class="post-taken-over">
  1641 		<div class="post-taken-over">
  1556 			<div class="post-locked-avatar"></div>
  1642 			<div class="post-locked-avatar"></div>
  1557 			<p class="wp-tab-first" tabindex="0">
  1643 			<p class="wp-tab-first" tabindex="0">
  1558 			<span class="currently-editing"></span><br />
  1644 			<span class="currently-editing"></span><br />
  1559 			<span class="locked-saving hidden"><img src="<?php echo esc_url( admin_url( 'images/spinner-2x.gif' ) ); ?>" width="16" height="16" /> <?php _e('Saving revision...'); ?></span>
  1645 			<span class="locked-saving hidden"><img src="<?php echo esc_url( admin_url( 'images/spinner-2x.gif' ) ); ?>" width="16" height="16" alt="" /> <?php _e( 'Saving revision&hellip;' ); ?></span>
  1560 			<span class="locked-saved hidden"><?php _e('Your latest changes were saved as a revision.'); ?></span>
  1646 			<span class="locked-saved hidden"><?php _e('Your latest changes were saved as a revision.'); ?></span>
  1561 			</p>
  1647 			</p>
  1562 			<?php
  1648 			<?php
  1563 			/**
  1649 			/**
  1564 			 * Fires inside the dialog displayed when a user has lost the post lock.
  1650 			 * Fires inside the dialog displayed when a user has lost the post lock.
  1581 }
  1667 }
  1582 
  1668 
  1583 /**
  1669 /**
  1584  * Creates autosave data for the specified post from $_POST data.
  1670  * Creates autosave data for the specified post from $_POST data.
  1585  *
  1671  *
  1586  * @package WordPress
       
  1587  * @subpackage Post_Revisions
       
  1588  * @since 2.6.0
  1672  * @since 2.6.0
  1589  *
  1673  *
  1590  * @param mixed $post_data Associative array containing the post data or int post ID.
  1674  * @param mixed $post_data Associative array containing the post data or int post ID.
  1591  * @return mixed The autosave revision ID. WP_Error or 0 on error.
  1675  * @return mixed The autosave revision ID. WP_Error or 0 on error.
  1592  */
  1676  */
  1593 function wp_create_post_autosave( $post_data ) {
  1677 function wp_create_post_autosave( $post_data ) {
  1594 	if ( is_numeric( $post_data ) ) {
  1678 	if ( is_numeric( $post_data ) ) {
  1595 		$post_id = $post_data;
  1679 		$post_id = $post_data;
  1596 		$post_data = &$_POST;
  1680 		$post_data = $_POST;
  1597 	} else {
  1681 	} else {
  1598 		$post_id = (int) $post_data['post_ID'];
  1682 		$post_id = (int) $post_data['post_ID'];
  1599 	}
  1683 	}
  1600 
  1684 
  1601 	$post_data = _wp_translate_postdata( true, $post_data );
  1685 	$post_data = _wp_translate_postdata( true, $post_data );
  1604 
  1688 
  1605 	$post_author = get_current_user_id();
  1689 	$post_author = get_current_user_id();
  1606 
  1690 
  1607 	// Store one autosave per author. If there is already an autosave, overwrite it.
  1691 	// Store one autosave per author. If there is already an autosave, overwrite it.
  1608 	if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) {
  1692 	if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) {
  1609 		$new_autosave = _wp_post_revision_fields( $post_data, true );
  1693 		$new_autosave = _wp_post_revision_data( $post_data, true );
  1610 		$new_autosave['ID'] = $old_autosave->ID;
  1694 		$new_autosave['ID'] = $old_autosave->ID;
  1611 		$new_autosave['post_author'] = $post_author;
  1695 		$new_autosave['post_author'] = $post_author;
  1612 
  1696 
  1613 		// If the new autosave has the same content as the post, delete the autosave.
  1697 		// If the new autosave has the same content as the post, delete the autosave.
  1614 		$post = get_post( $post_id );
  1698 		$post = get_post( $post_id );
  1615 		$autosave_is_different = false;
  1699 		$autosave_is_different = false;
  1616 		foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields() ) ) as $field ) {
  1700 		foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) {
  1617 			if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) {
  1701 			if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) {
  1618 				$autosave_is_different = true;
  1702 				$autosave_is_different = true;
  1619 				break;
  1703 				break;
  1620 			}
  1704 			}
  1621 		}
  1705 		}
  1643 	// Otherwise create the new autosave as a special post revision
  1727 	// Otherwise create the new autosave as a special post revision
  1644 	return _wp_put_post_revision( $post_data, true );
  1728 	return _wp_put_post_revision( $post_data, true );
  1645 }
  1729 }
  1646 
  1730 
  1647 /**
  1731 /**
  1648  * Save draft or manually autosave for showing preview.
  1732  * Saves a draft or manually autosaves for the purpose of showing a post preview.
  1649  *
  1733  *
  1650  * @package WordPress
       
  1651  * @since 2.7.0
  1734  * @since 2.7.0
  1652  *
  1735  *
  1653  * @return str URL to redirect to show the preview
  1736  * @return string URL to redirect to show the preview.
  1654  */
  1737  */
  1655 function post_preview() {
  1738 function post_preview() {
  1656 
  1739 
  1657 	$post_ID = (int) $_POST['post_ID'];
  1740 	$post_ID = (int) $_POST['post_ID'];
  1658 	$_POST['ID'] = $post_ID;
  1741 	$_POST['ID'] = $post_ID;
  1659 
  1742 
  1660 	if ( ! $post = get_post( $post_ID ) ) {
  1743 	if ( ! $post = get_post( $post_ID ) ) {
  1661 		wp_die( __( 'You are not allowed to edit this post.' ) );
  1744 		wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
  1662 	}
  1745 	}
  1663 
  1746 
  1664 	if ( ! current_user_can( 'edit_post', $post->ID ) ) {
  1747 	if ( ! current_user_can( 'edit_post', $post->ID ) ) {
  1665 		wp_die( __( 'You are not allowed to edit this post.' ) );
  1748 		wp_die( __( 'Sorry, you are not allowed to edit this post.' ) );
  1666 	}
  1749 	}
  1667 
  1750 
  1668 	$is_autosave = false;
  1751 	$is_autosave = false;
  1669 
  1752 
  1670 	if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'draft' == $post->post_status || 'auto-draft' == $post->post_status ) ) {
  1753 	if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'draft' == $post->post_status || 'auto-draft' == $post->post_status ) ) {
  1679 	}
  1762 	}
  1680 
  1763 
  1681 	if ( is_wp_error( $saved_post_id ) )
  1764 	if ( is_wp_error( $saved_post_id ) )
  1682 		wp_die( $saved_post_id->get_error_message() );
  1765 		wp_die( $saved_post_id->get_error_message() );
  1683 
  1766 
  1684 	$query_args = array( 'preview' => 'true' );
  1767 	$query_args = array();
  1685 
  1768 
  1686 	if ( $is_autosave && $saved_post_id ) {
  1769 	if ( $is_autosave && $saved_post_id ) {
  1687 		$query_args['preview_id'] = $post->ID;
  1770 		$query_args['preview_id'] = $post->ID;
  1688 		$query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $post->ID );
  1771 		$query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $post->ID );
  1689 
  1772 
  1690 		if ( isset( $_POST['post_format'] ) )
  1773 		if ( isset( $_POST['post_format'] ) ) {
  1691 			$query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
  1774 			$query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
  1692 	}
  1775 		}
  1693 
  1776 
  1694 	$url = add_query_arg( $query_args, get_permalink( $post->ID ) );
  1777 		if ( isset( $_POST['_thumbnail_id'] ) ) {
  1695 
  1778 			$query_args['_thumbnail_id'] = ( intval( $_POST['_thumbnail_id'] ) <= 0 ) ? '-1' : intval( $_POST['_thumbnail_id'] );
  1696 	/** This filter is documented in wp-admin/includes/meta-boxes.php */
  1779 		}
  1697 	return apply_filters( 'preview_post_link', $url, $post );
  1780 	}
       
  1781 
       
  1782 	return get_preview_post_link( $post, $query_args );
  1698 }
  1783 }
  1699 
  1784 
  1700 /**
  1785 /**
  1701  * Save a post submitted with XHR
  1786  * Save a post submitted with XHR
  1702  *
  1787  *
  1704  *
  1789  *
  1705  * @since 3.9.0
  1790  * @since 3.9.0
  1706  *
  1791  *
  1707  * @param array $post_data Associative array of the submitted post data.
  1792  * @param array $post_data Associative array of the submitted post data.
  1708  * @return mixed The value 0 or WP_Error on failure. The saved post ID on success.
  1793  * @return mixed The value 0 or WP_Error on failure. The saved post ID on success.
  1709  *               Te ID can be the draft post_id or the autosave revision post_id.
  1794  *               The ID can be the draft post_id or the autosave revision post_id.
  1710  */
  1795  */
  1711 function wp_autosave( $post_data ) {
  1796 function wp_autosave( $post_data ) {
  1712 	// Back-compat
  1797 	// Back-compat
  1713 	if ( ! defined( 'DOING_AUTOSAVE' ) )
  1798 	if ( ! defined( 'DOING_AUTOSAVE' ) )
  1714 		define( 'DOING_AUTOSAVE', true );
  1799 		define( 'DOING_AUTOSAVE', true );
  1721 	}
  1806 	}
  1722 
  1807 
  1723 	$post = get_post( $post_id );
  1808 	$post = get_post( $post_id );
  1724 
  1809 
  1725 	if ( ! current_user_can( 'edit_post', $post->ID ) ) {
  1810 	if ( ! current_user_can( 'edit_post', $post->ID ) ) {
  1726 		return new WP_Error( 'edit_posts', __( 'You are not allowed to edit this item.' ) );
  1811 		return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to edit this item.' ) );
  1727 	}
  1812 	}
  1728 
  1813 
  1729 	if ( 'auto-draft' == $post->post_status )
  1814 	if ( 'auto-draft' == $post->post_status )
  1730 		$post_data['post_status'] = 'draft';
  1815 		$post_data['post_status'] = 'draft';
  1731 
  1816 
  1738 	} else {
  1823 	} else {
  1739 		// Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user.
  1824 		// Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user.
  1740 		return wp_create_post_autosave( wp_slash( $post_data ) );
  1825 		return wp_create_post_autosave( wp_slash( $post_data ) );
  1741 	}
  1826 	}
  1742 }
  1827 }
       
  1828 
       
  1829 /**
       
  1830  * Redirect to previous page.
       
  1831  *
       
  1832  * @param int $post_id Optional. Post ID.
       
  1833  */
       
  1834 function redirect_post($post_id = '') {
       
  1835 	if ( isset($_POST['save']) || isset($_POST['publish']) ) {
       
  1836 		$status = get_post_status( $post_id );
       
  1837 
       
  1838 		if ( isset( $_POST['publish'] ) ) {
       
  1839 			switch ( $status ) {
       
  1840 				case 'pending':
       
  1841 					$message = 8;
       
  1842 					break;
       
  1843 				case 'future':
       
  1844 					$message = 9;
       
  1845 					break;
       
  1846 				default:
       
  1847 					$message = 6;
       
  1848 			}
       
  1849 		} else {
       
  1850 			$message = 'draft' == $status ? 10 : 1;
       
  1851 		}
       
  1852 
       
  1853 		$location = add_query_arg( 'message', $message, get_edit_post_link( $post_id, 'url' ) );
       
  1854 	} elseif ( isset($_POST['addmeta']) && $_POST['addmeta'] ) {
       
  1855 		$location = add_query_arg( 'message', 2, wp_get_referer() );
       
  1856 		$location = explode('#', $location);
       
  1857 		$location = $location[0] . '#postcustom';
       
  1858 	} elseif ( isset($_POST['deletemeta']) && $_POST['deletemeta'] ) {
       
  1859 		$location = add_query_arg( 'message', 3, wp_get_referer() );
       
  1860 		$location = explode('#', $location);
       
  1861 		$location = $location[0] . '#postcustom';
       
  1862 	} else {
       
  1863 		$location = add_query_arg( 'message', 4, get_edit_post_link( $post_id, 'url' ) );
       
  1864 	}
       
  1865 
       
  1866 	/**
       
  1867 	 * Filters the post redirect destination URL.
       
  1868 	 *
       
  1869 	 * @since 2.9.0
       
  1870 	 *
       
  1871 	 * @param string $location The destination URL.
       
  1872 	 * @param int    $post_id  The post ID.
       
  1873 	 */
       
  1874 	wp_redirect( apply_filters( 'redirect_post_location', $location, $post_id ) );
       
  1875 	exit;
       
  1876 }