edit_post

Definition:
function edit_post( $post_data = null ) {}

Update an existing post with values provided in $_POST.

Parameters

  • array $post_data: Optional.

Return values

returns:Post ID.

Source code

function edit_post( $post_data = null ) {



	if ( empty($post_data) )

		$post_data = &$_POST;



	// Clear out any data in internal vars.

	unset( $post_data['filter'] );



	$post_ID = (int) $post_data['post_ID'];

	$post = get_post( $post_ID );

	$post_data['post_type'] = $post->post_type;

	$post_data['post_mime_type'] = $post->post_mime_type;



	$ptype = get_post_type_object($post_data['post_type']);

	if ( !current_user_can( $ptype->cap->edit_post, $post_ID ) ) {

		if ( 'page' == $post_data['post_type'] )

			wp_die( __('You are not allowed to edit this page.' ));

		else

			wp_die( __('You are not allowed to edit this post.' ));

	}



	// Autosave shouldn't save too soon after a real save

	if ( 'autosave' == $post_data['action'] ) {

		$post =& get_post( $post_ID );

		$now = time();

		$then = strtotime($post->post_date_gmt . ' +0000');

		$delta = AUTOSAVE_INTERVAL / 2;

		if ( ($now - $then) < $delta )

			return $post_ID;

	}



	$post_data = _wp_translate_postdata( true, $post_data );

	if ( is_wp_error($post_data) )

		wp_die( $post_data->get_error_message() );

	if ( 'autosave' != $post_data['action']  && 'auto-draft' == $post_data['post_status'] )

		$post_data['post_status'] = 'draft';



	if ( isset($post_data['visibility']) ) {

		switch ( $post_data['visibility'] ) {

			case 'public' :

				$post_data['post_password'] = '';

				break;

			case 'password' :

				unset( $post_data['sticky'] );

				break;

			case 'private' :

				$post_data['post_status'] = 'private';

				$post_data['post_password'] = '';

				unset( $post_data['sticky'] );

				break;

		}

	}



	// Post Formats

	if ( current_theme_supports( 'post-formats' ) && isset( $post_data['post_format'] ) ) {

		$formats = get_theme_support( 'post-formats' );

		if ( is_array( $formats ) ) {

			$formats = $formats[0];

			if ( in_array( $post_data['post_format'], $formats ) ) {

				set_post_format( $post_ID, $post_data['post_format'] );

			} elseif ( '0' == $post_data['post_format'] ) {

				set_post_format( $post_ID, false );

			}

		}

	}



	// Meta Stuff

	if ( isset($post_data['meta']) && $post_data['meta'] ) {

		foreach ( $post_data['meta'] as $key => $value ) {

			if ( !$meta = get_post_meta_by_id( $key ) )

				continue;

			if ( $meta->post_id != $post_ID )

				continue;

			if ( is_protected_meta( $value['key'] ) )

				continue;

			update_meta( $key, $value['key'], $value['value'] );

		}

	}



	if ( isset($post_data['deletemeta']) && $post_data['deletemeta'] ) {

		foreach ( $post_data['deletemeta'] as $key => $value ) {

			if ( !$meta = get_post_meta_by_id( $key ) )

				continue;

			if ( $meta->post_id != $post_ID )

				continue;

			if ( is_protected_meta( $meta->meta_key ) )

				continue;

			delete_meta( $key );

		}

	}



	add_meta( $post_ID );



	update_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID );



	wp_update_post( $post_data );



	// Reunite any orphaned attachments with their parent

	if ( !$draft_ids = get_user_option( 'autosave_draft_ids' ) )

		$draft_ids = array();

	if ( $draft_temp_id = (int) array_search( $post_ID, $draft_ids ) )

		_relocate_children( $draft_temp_id, $post_ID );



	// Now that we have an ID we can fix any attachment anchor hrefs

	_fix_attachment_links( $post_ID );



	wp_set_post_lock( $post_ID, $GLOBALS['current_user']->ID );



	if ( current_user_can( $ptype->cap->edit_others_posts ) ) {

		if ( ! empty( $post_data['sticky'] ) )

			stick_post( $post_ID );

		else

			unstick_post( $post_ID );

	}



	return $post_ID;

}

1002

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: