+ $post_data['post_date'] = sprintf( "%04d-%02d-%02d %02d:%02d:%02d", $aa, $mm, $jj, $hh, $mn, $ss );
+ $valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] );
+ if ( !$valid_date ) {
+ return new WP_Error( 'invalid_date', __( 'Whoops, the provided date is invalid.' ) );
+ }
+ $post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] );
+ }
+
+ return $post_data;
+}
+
+/**
+ * Update an existing post with values provided in $_POST.
+ *
+ * @since 1.5.0
+ *
+ * @param array $post_data Optional.
+ * @return int Post ID.
+ */
+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;
+
+ if ( ! empty( $post_data['post_status'] ) ) {
+ $post_data['post_status'] = sanitize_key( $post_data['post_status'] );
+
+ if ( 'inherit' == $post_data['post_status'] ) {
+ unset( $post_data['post_status'] );
+ }
+ }
+
+ $ptype = get_post_type_object($post_data['post_type']);
+ if ( !current_user_can( '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.' ));
+ }
+
+ if ( post_type_supports( $ptype->name, 'revisions' ) ) {
+ $revisions = wp_get_post_revisions( $post_ID, array( 'order' => 'ASC', 'posts_per_page' => 1 ) );
+ $revision = current( $revisions );
+
+ // Check if the revisions have been upgraded
+ if ( $revisions && _wp_get_post_revision_version( $revision ) < 1 )
+ _wp_upgrade_revisions_of_post( $post, wp_get_post_revisions( $post_ID ) );
+ }
+
+ if ( ( empty( $post_data['action'] ) || '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_data = _wp_translate_postdata( true, $post_data );
+ if ( is_wp_error($post_data) )
+ wp_die( $post_data->get_error_message() );
+
+ // Post Formats
+ if ( isset( $post_data['post_format'] ) )
+ set_post_format( $post_ID, $post_data['post_format'] );
+
+ $format_meta_urls = array( 'url', 'link_url', 'quote_source_url' );
+ foreach ( $format_meta_urls as $format_meta_url ) {
+ $keyed = '_format_' . $format_meta_url;
+ if ( isset( $post_data[ $keyed ] ) )
+ update_post_meta( $post_ID, $keyed, wp_slash( esc_url_raw( wp_unslash( $post_data[ $keyed ] ) ) ) );
+ }
+
+ $format_keys = array( 'quote', 'quote_source_name', 'image', 'gallery', 'audio_embed', 'video_embed' );
+
+ foreach ( $format_keys as $key ) {
+ $keyed = '_format_' . $key;
+ if ( isset( $post_data[ $keyed ] ) ) {
+ if ( current_user_can( 'unfiltered_html' ) )
+ update_post_meta( $post_ID, $keyed, $post_data[ $keyed ] );
+ else
+ update_post_meta( $post_ID, $keyed, wp_filter_post_kses( $post_data[ $keyed ] ) );
+ }