wp/wp-admin/includes/post.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
    77 			else
    77 			else
    78 				return new WP_Error( 'edit_others_posts', __( 'You are not allowed to create posts as this user.' ) );
    78 				return new WP_Error( 'edit_others_posts', __( 'You are not allowed to create posts as this user.' ) );
    79 		}
    79 		}
    80 	}
    80 	}
    81 
    81 
    82 	if ( ! empty( $post_data['post_status'] ) )
    82 	if ( ! empty( $post_data['post_status'] ) ) {
    83 		$post_data['post_status'] = sanitize_key( $post_data['post_status'] );
    83 		$post_data['post_status'] = sanitize_key( $post_data['post_status'] );
       
    84 
       
    85 		// No longer an auto-draft
       
    86 		if ( 'auto-draft' === $post_data['post_status'] ) {
       
    87 			$post_data['post_status'] = 'draft';
       
    88 		}
       
    89 
       
    90 		if ( ! get_post_status_object( $post_data['post_status'] ) ) {
       
    91 			unset( $post_data['post_status'] );
       
    92 		}
       
    93 	}
    84 
    94 
    85 	// What to do based on which button they pressed
    95 	// What to do based on which button they pressed
    86 	if ( isset($post_data['saveasdraft']) && '' != $post_data['saveasdraft'] )
    96 	if ( isset($post_data['saveasdraft']) && '' != $post_data['saveasdraft'] )
    87 		$post_data['post_status'] = 'draft';
    97 		$post_data['post_status'] = 'draft';
    88 	if ( isset($post_data['saveasprivate']) && '' != $post_data['saveasprivate'] )
    98 	if ( isset($post_data['saveasprivate']) && '' != $post_data['saveasprivate'] )
    98 		$post_id = $post_data['ID'];
   108 		$post_id = $post_data['ID'];
    99 	else
   109 	else
   100 		$post_id = false;
   110 		$post_id = false;
   101 	$previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false;
   111 	$previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false;
   102 
   112 
       
   113 	if ( isset( $post_data['post_status'] ) && 'private' == $post_data['post_status'] && ! current_user_can( $ptype->cap->publish_posts ) ) {
       
   114 		$post_data['post_status'] = $previous_status ? $previous_status : 'pending';
       
   115 	}
       
   116 
   103 	$published_statuses = array( 'publish', 'future' );
   117 	$published_statuses = array( 'publish', 'future' );
   104 
   118 
   105 	// Posts 'submitted for approval' present are submitted to $_POST the same as if they were being published.
   119 	// Posts 'submitted for approval' present are submitted to $_POST the same as if they were being published.
   106 	// Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts.
   120 	// Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts.
   107 	if ( isset($post_data['post_status']) && (in_array( $post_data['post_status'], $published_statuses ) && !current_user_can( $ptype->cap->publish_posts )) )
   121 	if ( isset($post_data['post_status']) && (in_array( $post_data['post_status'], $published_statuses ) && !current_user_can( $ptype->cap->publish_posts )) )
   108 		if ( ! in_array( $previous_status, $published_statuses ) || !current_user_can( 'edit_post', $post_id ) )
   122 		if ( ! in_array( $previous_status, $published_statuses ) || !current_user_can( 'edit_post', $post_id ) )
   109 			$post_data['post_status'] = 'pending';
   123 			$post_data['post_status'] = 'pending';
   110 
   124 
   111 	if ( ! isset($post_data['post_status']) )
   125 	if ( ! isset( $post_data['post_status'] ) ) {
   112 		$post_data['post_status'] = $previous_status;
   126 		$post_data['post_status'] = 'auto-draft' === $previous_status ? 'draft' : $previous_status;
       
   127 	}
       
   128 
       
   129 	if ( isset( $post_data['post_password'] ) && ! current_user_can( $ptype->cap->publish_posts ) ) {
       
   130 		unset( $post_data['post_password'] );
       
   131 	}
   113 
   132 
   114 	if (!isset( $post_data['comment_status'] ))
   133 	if (!isset( $post_data['comment_status'] ))
   115 		$post_data['comment_status'] = 'closed';
   134 		$post_data['comment_status'] = 'closed';
   116 
   135 
   117 	if (!isset( $post_data['ping_status'] ))
   136 	if (!isset( $post_data['ping_status'] ))
   156  *
   175  *
   157  * @param array $post_data Optional.
   176  * @param array $post_data Optional.
   158  * @return int Post ID.
   177  * @return int Post ID.
   159  */
   178  */
   160 function edit_post( $post_data = null ) {
   179 function edit_post( $post_data = null ) {
       
   180 	global $wpdb;
   161 
   181 
   162 	if ( empty($post_data) )
   182 	if ( empty($post_data) )
   163 		$post_data = &$_POST;
   183 		$post_data = &$_POST;
   164 
   184 
   165 	// Clear out any data in internal vars.
   185 	// Clear out any data in internal vars.
   167 
   187 
   168 	$post_ID = (int) $post_data['post_ID'];
   188 	$post_ID = (int) $post_data['post_ID'];
   169 	$post = get_post( $post_ID );
   189 	$post = get_post( $post_ID );
   170 	$post_data['post_type'] = $post->post_type;
   190 	$post_data['post_type'] = $post->post_type;
   171 	$post_data['post_mime_type'] = $post->post_mime_type;
   191 	$post_data['post_mime_type'] = $post->post_mime_type;
       
   192 
       
   193 	if ( ! empty( $post_data['post_status'] ) ) {
       
   194 		$post_data['post_status'] = sanitize_key( $post_data['post_status'] );
       
   195 
       
   196 		if ( 'inherit' == $post_data['post_status'] ) {
       
   197 			unset( $post_data['post_status'] );
       
   198 		}
       
   199 	}
   172 
   200 
   173 	$ptype = get_post_type_object($post_data['post_type']);
   201 	$ptype = get_post_type_object($post_data['post_type']);
   174 	if ( !current_user_can( 'edit_post', $post_ID ) ) {
   202 	if ( !current_user_can( 'edit_post', $post_ID ) ) {
   175 		if ( 'page' == $post_data['post_type'] )
   203 		if ( 'page' == $post_data['post_type'] )
   176 			wp_die( __('You are not allowed to edit this page.' ));
   204 			wp_die( __('You are not allowed to edit this page.' ));
   183 		$revision = current( $revisions );
   211 		$revision = current( $revisions );
   184 
   212 
   185 		// Check if the revisions have been upgraded
   213 		// Check if the revisions have been upgraded
   186 		if ( $revisions && _wp_get_post_revision_version( $revision ) < 1 )
   214 		if ( $revisions && _wp_get_post_revision_version( $revision ) < 1 )
   187 			_wp_upgrade_revisions_of_post( $post, wp_get_post_revisions( $post_ID ) );
   215 			_wp_upgrade_revisions_of_post( $post, wp_get_post_revisions( $post_ID ) );
   188 	}
       
   189 
       
   190 	$post_data = _wp_translate_postdata( true, $post_data );
       
   191 	if ( is_wp_error($post_data) )
       
   192 		wp_die( $post_data->get_error_message() );
       
   193 	if ( ( empty( $post_data['action'] ) || 'autosave' != $post_data['action'] ) && 'auto-draft' == $post_data['post_status'] ) {
       
   194 		$post_data['post_status'] = 'draft';
       
   195 	}
   216 	}
   196 
   217 
   197 	if ( isset($post_data['visibility']) ) {
   218 	if ( isset($post_data['visibility']) ) {
   198 		switch ( $post_data['visibility'] ) {
   219 		switch ( $post_data['visibility'] ) {
   199 			case 'public' :
   220 			case 'public' :
   208 				unset( $post_data['sticky'] );
   229 				unset( $post_data['sticky'] );
   209 				break;
   230 				break;
   210 		}
   231 		}
   211 	}
   232 	}
   212 
   233 
       
   234 	$post_data = _wp_translate_postdata( true, $post_data );
       
   235 	if ( is_wp_error($post_data) )
       
   236 		wp_die( $post_data->get_error_message() );
       
   237 
   213 	// Post Formats
   238 	// Post Formats
   214 	if ( isset( $post_data['post_format'] ) )
   239 	if ( isset( $post_data['post_format'] ) )
   215 		set_post_format( $post_ID, $post_data['post_format'] );
   240 		set_post_format( $post_ID, $post_data['post_format'] );
   216 
   241 
   217 	$format_meta_urls = array( 'url', 'link_url', 'quote_source_url' );
   242 	$format_meta_urls = array( 'url', 'link_url', 'quote_source_url' );
   229 			if ( current_user_can( 'unfiltered_html' ) )
   254 			if ( current_user_can( 'unfiltered_html' ) )
   230 				update_post_meta( $post_ID, $keyed, $post_data[ $keyed ] );
   255 				update_post_meta( $post_ID, $keyed, $post_data[ $keyed ] );
   231 			else
   256 			else
   232 				update_post_meta( $post_ID, $keyed, wp_filter_post_kses( $post_data[ $keyed ] ) );
   257 				update_post_meta( $post_ID, $keyed, wp_filter_post_kses( $post_data[ $keyed ] ) );
   233 		}
   258 		}
       
   259 	}
       
   260 
       
   261 	if ( 'attachment' === $post_data['post_type'] && preg_match( '#^(audio|video)/#', $post_data['post_mime_type'] ) ) {
       
   262 		$id3data = wp_get_attachment_metadata( $post_ID );
       
   263 		if ( ! is_array( $id3data ) ) {
       
   264 			$id3data = array();
       
   265 		}
       
   266 
       
   267 		foreach ( wp_get_attachment_id3_keys( $post, 'edit' ) as $key => $label ) {
       
   268 			if ( isset( $post_data[ 'id3_' . $key ] ) ) {
       
   269 				$id3data[ $key ] = sanitize_text_field( wp_unslash( $post_data[ 'id3_' . $key ] ) );
       
   270 			}
       
   271 		}
       
   272 		wp_update_attachment_metadata( $post_ID, $id3data );
   234 	}
   273 	}
   235 
   274 
   236 	// Meta Stuff
   275 	// Meta Stuff
   237 	if ( isset($post_data['meta']) && $post_data['meta'] ) {
   276 	if ( isset($post_data['meta']) && $post_data['meta'] ) {
   238 		foreach ( $post_data['meta'] as $key => $value ) {
   277 		foreach ( $post_data['meta'] as $key => $value ) {
   262 	if ( 'attachment' == $post_data['post_type'] ) {
   301 	if ( 'attachment' == $post_data['post_type'] ) {
   263 		if ( isset( $post_data[ '_wp_attachment_image_alt' ] ) ) {
   302 		if ( isset( $post_data[ '_wp_attachment_image_alt' ] ) ) {
   264 			$image_alt = wp_unslash( $post_data['_wp_attachment_image_alt'] );
   303 			$image_alt = wp_unslash( $post_data['_wp_attachment_image_alt'] );
   265 			if ( $image_alt != get_post_meta( $post_ID, '_wp_attachment_image_alt', true ) ) {
   304 			if ( $image_alt != get_post_meta( $post_ID, '_wp_attachment_image_alt', true ) ) {
   266 				$image_alt = wp_strip_all_tags( $image_alt, true );
   305 				$image_alt = wp_strip_all_tags( $image_alt, true );
   267 				// update_meta expects slashed
   306 				// update_meta expects slashed.
   268 				update_post_meta( $post_ID, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
   307 				update_post_meta( $post_ID, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
   269 			}
   308 			}
   270 		}
   309 		}
   271 
   310 
   272 		$attachment_data = isset( $post_data['attachments'][ $post_ID ] ) ? $post_data['attachments'][ $post_ID ] : array();
   311 		$attachment_data = isset( $post_data['attachments'][ $post_ID ] ) ? $post_data['attachments'][ $post_ID ] : array();
       
   312 
   273 		/** This filter is documented in wp-admin/includes/media.php */
   313 		/** This filter is documented in wp-admin/includes/media.php */
   274 		$post_data = apply_filters( 'attachment_fields_to_save', $post_data, $attachment_data );
   314 		$post_data = apply_filters( 'attachment_fields_to_save', $post_data, $attachment_data );
   275 	}
   315 	}
   276 
   316 
       
   317 	// Convert taxonomy input to term IDs, to avoid ambiguity.
       
   318 	if ( isset( $post_data['tax_input'] ) ) {
       
   319 		foreach ( (array) $post_data['tax_input'] as $taxonomy => $terms ) {
       
   320 			// Hierarchical taxonomy data is already sent as term IDs, so no conversion is necessary.
       
   321 			if ( is_taxonomy_hierarchical( $taxonomy ) ) {
       
   322 				continue;
       
   323 			}
       
   324 
       
   325 			/*
       
   326 			 * Assume that a 'tax_input' string is a comma-separated list of term names.
       
   327 			 * Some languages may use a character other than a comma as a delimiter, so we standardize on
       
   328 			 * commas before parsing the list.
       
   329 			 */
       
   330 			if ( ! is_array( $terms ) ) {
       
   331 				$comma = _x( ',', 'tag delimiter' );
       
   332 				if ( ',' !== $comma ) {
       
   333 					$terms = str_replace( $comma, ',', $terms );
       
   334 				}
       
   335 				$terms = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) );
       
   336 			}
       
   337 
       
   338 			$clean_terms = array();
       
   339 			foreach ( $terms as $term ) {
       
   340 				// Empty terms are invalid input.
       
   341 				if ( empty( $term ) ) {
       
   342 					continue;
       
   343 				}
       
   344 
       
   345 				$_term = get_terms( $taxonomy, array(
       
   346 					'name' => $term,
       
   347 					'fields' => 'ids',
       
   348 					'hide_empty' => false,
       
   349 				) );
       
   350 
       
   351 				if ( ! empty( $_term ) ) {
       
   352 					$clean_terms[] = intval( $_term[0] );
       
   353 				} else {
       
   354 					// No existing term was found, so pass the string. A new term will be created.
       
   355 					$clean_terms[] = $term;
       
   356 				}
       
   357 			}
       
   358 
       
   359 			$post_data['tax_input'][ $taxonomy ] = $clean_terms;
       
   360 		}
       
   361 	}
       
   362 
   277 	add_meta( $post_ID );
   363 	add_meta( $post_ID );
   278 
   364 
   279 	update_post_meta( $post_ID, '_edit_last', get_current_user_id() );
   365 	update_post_meta( $post_ID, '_edit_last', get_current_user_id() );
   280 
   366 
   281 	wp_update_post( $post_data );
   367 	$success = wp_update_post( $post_data );
       
   368 	// 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' ) ) ) {
       
   370 		$fields = array( 'post_title', 'post_content', 'post_excerpt' );
       
   371 
       
   372 		foreach( $fields as $field ) {
       
   373 			if ( isset( $post_data[ $field ] ) ) {
       
   374 				$post_data[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->posts, $field, $post_data[ $field ] );
       
   375 			}
       
   376 		}
       
   377 
       
   378 		wp_update_post( $post_data );
       
   379 	}
   282 
   380 
   283 	// Now that we have an ID we can fix any attachment anchor hrefs
   381 	// Now that we have an ID we can fix any attachment anchor hrefs
   284 	_fix_attachment_links( $post_ID );
   382 	_fix_attachment_links( $post_ID );
   285 
   383 
   286 	wp_set_post_lock( $post_ID );
   384 	wp_set_post_lock( $post_ID );
   329 		unset($post_data['post_status']);
   427 		unset($post_data['post_status']);
   330 	} else {
   428 	} else {
   331 		$post_data['post_status'] = $post_data['_status'];
   429 		$post_data['post_status'] = $post_data['_status'];
   332 	}
   430 	}
   333 	unset($post_data['_status']);
   431 	unset($post_data['_status']);
       
   432 
       
   433 	if ( ! empty( $post_data['post_status'] ) ) {
       
   434 		$post_data['post_status'] = sanitize_key( $post_data['post_status'] );
       
   435 
       
   436 		if ( 'inherit' == $post_data['post_status'] ) {
       
   437 			unset( $post_data['post_status'] );
       
   438 		}
       
   439 	}
   334 
   440 
   335 	$post_IDs = array_map( 'intval', (array) $post_data['post'] );
   441 	$post_IDs = array_map( 'intval', (array) $post_data['post'] );
   336 
   442 
   337 	$reset = array(
   443 	$reset = array(
   338 		'post_author', 'post_status', 'post_password',
   444 		'post_author', 'post_status', 'post_password',
   384 			}
   490 			}
   385 		}
   491 		}
   386 	}
   492 	}
   387 
   493 
   388 	$updated = $skipped = $locked = array();
   494 	$updated = $skipped = $locked = array();
       
   495 	$shared_post_data = $post_data;
       
   496 
   389 	foreach ( $post_IDs as $post_ID ) {
   497 	foreach ( $post_IDs as $post_ID ) {
       
   498 		// Start with fresh post data with each iteration.
       
   499 		$post_data = $shared_post_data;
       
   500 
   390 		$post_type_object = get_post_type_object( get_post_type( $post_ID ) );
   501 		$post_type_object = get_post_type_object( get_post_type( $post_ID ) );
   391 
   502 
   392 		if ( !isset( $post_type_object ) || ( isset($children) && in_array($post_ID, $children) ) || !current_user_can( 'edit_post', $post_ID ) ) {
   503 		if ( !isset( $post_type_object ) || ( isset($children) && in_array($post_ID, $children) ) || !current_user_can( 'edit_post', $post_ID ) ) {
   393 			$skipped[] = $post_ID;
   504 			$skipped[] = $post_ID;
   394 			continue;
   505 			continue;
   420 			$cats = (array) wp_get_post_categories($post_ID);
   531 			$cats = (array) wp_get_post_categories($post_ID);
   421 			$post_data['post_category'] = array_unique( array_merge($cats, $new_cats) );
   532 			$post_data['post_category'] = array_unique( array_merge($cats, $new_cats) );
   422 			unset( $post_data['tax_input']['category'] );
   533 			unset( $post_data['tax_input']['category'] );
   423 		}
   534 		}
   424 
   535 
       
   536 		$post_data['post_type'] = $post->post_type;
   425 		$post_data['post_mime_type'] = $post->post_mime_type;
   537 		$post_data['post_mime_type'] = $post->post_mime_type;
   426 		$post_data['guid'] = $post->guid;
   538 		$post_data['guid'] = $post->guid;
   427 
   539 
       
   540 		foreach ( array( 'comment_status', 'ping_status', 'post_author' ) as $field ) {
       
   541 			if ( ! isset( $post_data[ $field ] ) ) {
       
   542 				$post_data[ $field ] = $post->$field;
       
   543 			}
       
   544 		}
       
   545 
   428 		$post_data['ID'] = $post_ID;
   546 		$post_data['ID'] = $post_ID;
       
   547 		$post_data['post_ID'] = $post_ID;
       
   548 
       
   549 		$post_data = _wp_translate_postdata( true, $post_data );
       
   550 		if ( is_wp_error( $post_data ) ) {
       
   551 			$skipped[] = $post_ID;
       
   552 			continue;
       
   553 		}
       
   554 
   429 		$updated[] = wp_update_post( $post_data );
   555 		$updated[] = wp_update_post( $post_data );
   430 
   556 
   431 		if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) {
   557 		if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) {
   432 			if ( 'sticky' == $post_data['sticky'] )
   558 			if ( 'sticky' == $post_data['sticky'] )
   433 				stick_post( $post_ID );
   559 				stick_post( $post_ID );
   449  *
   575  *
   450  * @param string $post_type A post type string, defaults to 'post'.
   576  * @param string $post_type A post type string, defaults to 'post'.
   451  * @return WP_Post Post object containing all the default post data as attributes
   577  * @return WP_Post Post object containing all the default post data as attributes
   452  */
   578  */
   453 function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) {
   579 function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) {
   454 	global $wpdb;
       
   455 
       
   456 	$post_title = '';
   580 	$post_title = '';
   457 	if ( !empty( $_REQUEST['post_title'] ) )
   581 	if ( !empty( $_REQUEST['post_title'] ) )
   458 		$post_title = esc_html( wp_unslash( $_REQUEST['post_title'] ));
   582 		$post_title = esc_html( wp_unslash( $_REQUEST['post_title'] ));
   459 
   583 
   460 	$post_content = '';
   584 	$post_content = '';
   475 		$post->ID = 0;
   599 		$post->ID = 0;
   476 		$post->post_author = '';
   600 		$post->post_author = '';
   477 		$post->post_date = '';
   601 		$post->post_date = '';
   478 		$post->post_date_gmt = '';
   602 		$post->post_date_gmt = '';
   479 		$post->post_password = '';
   603 		$post->post_password = '';
       
   604 		$post->post_name = '';
   480 		$post->post_type = $post_type;
   605 		$post->post_type = $post_type;
   481 		$post->post_status = 'draft';
   606 		$post->post_status = 'draft';
   482 		$post->to_ping = '';
   607 		$post->to_ping = '';
   483 		$post->pinged = '';
   608 		$post->pinged = '';
   484 		$post->comment_status = get_option( 'default_comment_status' );
   609 		$post->comment_status = get_option( 'default_comment_status' );
   489 		$post->post_parent = 0;
   614 		$post->post_parent = 0;
   490 		$post->menu_order = 0;
   615 		$post->menu_order = 0;
   491 		$post = new WP_Post( $post );
   616 		$post = new WP_Post( $post );
   492 	}
   617 	}
   493 
   618 
       
   619 	/**
       
   620 	 * Filter the default post content initially used in the "Write Post" form.
       
   621 	 *
       
   622 	 * @since 1.5.0
       
   623 	 *
       
   624 	 * @param string  $post_content Default post content.
       
   625 	 * @param WP_Post $post         Post object.
       
   626 	 */
   494 	$post->post_content = apply_filters( 'default_content', $post_content, $post );
   627 	$post->post_content = apply_filters( 'default_content', $post_content, $post );
   495 	$post->post_title   = apply_filters( 'default_title',   $post_title, $post   );
   628 
       
   629 	/**
       
   630 	 * Filter the default post title initially used in the "Write Post" form.
       
   631 	 *
       
   632 	 * @since 1.5.0
       
   633 	 *
       
   634 	 * @param string  $post_title Default post title.
       
   635 	 * @param WP_Post $post       Post object.
       
   636 	 */
       
   637 	$post->post_title = apply_filters( 'default_title', $post_title, $post );
       
   638 
       
   639 	/**
       
   640 	 * Filter the default post excerpt initially used in the "Write Post" form.
       
   641 	 *
       
   642 	 * @since 1.5.0
       
   643 	 *
       
   644 	 * @param string  $post_excerpt Default post excerpt.
       
   645 	 * @param WP_Post $post         Post object.
       
   646 	 */
   496 	$post->post_excerpt = apply_filters( 'default_excerpt', $post_excerpt, $post );
   647 	$post->post_excerpt = apply_filters( 'default_excerpt', $post_excerpt, $post );
   497 	$post->post_name = '';
       
   498 
   648 
   499 	return $post;
   649 	return $post;
   500 }
   650 }
   501 
   651 
   502 /**
   652 /**
   543 /**
   693 /**
   544  * Creates a new post from the "Write Post" form using $_POST information.
   694  * Creates a new post from the "Write Post" form using $_POST information.
   545  *
   695  *
   546  * @since 2.1.0
   696  * @since 2.1.0
   547  *
   697  *
   548  * @return unknown
   698  * @return int|WP_Error
   549  */
   699  */
   550 function wp_write_post() {
   700 function wp_write_post() {
   551 	if ( isset($_POST['post_type']) )
   701 	if ( isset($_POST['post_type']) )
   552 		$ptype = get_post_type_object($_POST['post_type']);
   702 		$ptype = get_post_type_object($_POST['post_type']);
   553 	else
   703 	else
   566 	unset( $_POST['filter'] );
   716 	unset( $_POST['filter'] );
   567 
   717 
   568 	// Edit don't write if we have a post id.
   718 	// Edit don't write if we have a post id.
   569 	if ( isset( $_POST['post_ID'] ) )
   719 	if ( isset( $_POST['post_ID'] ) )
   570 		return edit_post();
   720 		return edit_post();
   571 
       
   572 	$translated = _wp_translate_postdata( false );
       
   573 	if ( is_wp_error($translated) )
       
   574 		return $translated;
       
   575 
   721 
   576 	if ( isset($_POST['visibility']) ) {
   722 	if ( isset($_POST['visibility']) ) {
   577 		switch ( $_POST['visibility'] ) {
   723 		switch ( $_POST['visibility'] ) {
   578 			case 'public' :
   724 			case 'public' :
   579 				$_POST['post_password'] = '';
   725 				$_POST['post_password'] = '';
   587 				unset( $_POST['sticky'] );
   733 				unset( $_POST['sticky'] );
   588 				break;
   734 				break;
   589 		}
   735 		}
   590 	}
   736 	}
   591 
   737 
       
   738 	$translated = _wp_translate_postdata( false );
       
   739 	if ( is_wp_error($translated) )
       
   740 		return $translated;
       
   741 
   592 	// Create the post.
   742 	// Create the post.
   593 	$post_ID = wp_insert_post( $_POST );
   743 	$post_ID = wp_insert_post( $_POST );
   594 	if ( is_wp_error( $post_ID ) )
   744 	if ( is_wp_error( $post_ID ) )
   595 		return $post_ID;
   745 		return $post_ID;
   596 
   746 
   611 
   761 
   612 /**
   762 /**
   613  * Calls wp_write_post() and handles the errors.
   763  * Calls wp_write_post() and handles the errors.
   614  *
   764  *
   615  * @since 2.0.0
   765  * @since 2.0.0
   616 
   766  *
   617  * @uses wp_write_post()
   767  * @return int|null
   618  * @uses is_wp_error()
       
   619  * @uses wp_die()
       
   620  * @return unknown
       
   621  */
   768  */
   622 function write_post() {
   769 function write_post() {
   623 	$result = wp_write_post();
   770 	$result = wp_write_post();
   624 	if ( is_wp_error( $result ) )
   771 	if ( is_wp_error( $result ) )
   625 		wp_die( $result->get_error_message() );
   772 		wp_die( $result->get_error_message() );
   630 //
   777 //
   631 // Post Meta
   778 // Post Meta
   632 //
   779 //
   633 
   780 
   634 /**
   781 /**
   635  * {@internal Missing Short Description}}
   782  * Add post meta data defined in $_POST superglobal for post with given ID.
   636  *
   783  *
   637  * @since 1.2.0
   784  * @since 1.2.0
   638  *
   785  *
   639  * @param unknown_type $post_ID
   786  * @param int $post_ID
   640  * @return unknown
   787  * @return int|bool
   641  */
   788  */
   642 function add_meta( $post_ID ) {
   789 function add_meta( $post_ID ) {
   643 	global $wpdb;
       
   644 	$post_ID = (int) $post_ID;
   790 	$post_ID = (int) $post_ID;
   645 
   791 
   646 	$metakeyselect = isset($_POST['metakeyselect']) ? wp_unslash( trim( $_POST['metakeyselect'] ) ) : '';
   792 	$metakeyselect = isset($_POST['metakeyselect']) ? wp_unslash( trim( $_POST['metakeyselect'] ) ) : '';
   647 	$metakeyinput = isset($_POST['metakeyinput']) ? wp_unslash( trim( $_POST['metakeyinput'] ) ) : '';
   793 	$metakeyinput = isset($_POST['metakeyinput']) ? wp_unslash( trim( $_POST['metakeyinput'] ) ) : '';
   648 	$metavalue = isset($_POST['metavalue']) ? $_POST['metavalue'] : '';
   794 	$metavalue = isset($_POST['metavalue']) ? $_POST['metavalue'] : '';
   649 	if ( is_string( $metavalue ) )
   795 	if ( is_string( $metavalue ) )
   650 		$metavalue = trim( $metavalue );
   796 		$metavalue = trim( $metavalue );
   651 
   797 
   652 	if ( ('0' === $metavalue || ! empty ( $metavalue ) ) && ( ( ( '#NONE#' != $metakeyselect ) && !empty ( $metakeyselect) ) || !empty ( $metakeyinput ) ) ) {
   798 	if ( ('0' === $metavalue || ! empty ( $metavalue ) ) && ( ( ( '#NONE#' != $metakeyselect ) && !empty ( $metakeyselect) ) || !empty ( $metakeyinput ) ) ) {
   653 		// We have a key/value pair. If both the select and the
   799 		/*
   654 		// input for the key have data, the input takes precedence:
   800 		 * We have a key/value pair. If both the select and the input
   655 
   801 		 * for the key have data, the input takes precedence.
       
   802 		 */
   656  		if ( '#NONE#' != $metakeyselect )
   803  		if ( '#NONE#' != $metakeyselect )
   657 			$metakey = $metakeyselect;
   804 			$metakey = $metakeyselect;
   658 
   805 
   659 		if ( $metakeyinput )
   806 		if ( $metakeyinput )
   660 			$metakey = $metakeyinput; // default
   807 			$metakey = $metakeyinput; // default
   669 
   816 
   670 	return false;
   817 	return false;
   671 } // add_meta
   818 } // add_meta
   672 
   819 
   673 /**
   820 /**
   674  * {@internal Missing Short Description}}
   821  * Delete post meta data by meta ID.
   675  *
   822  *
   676  * @since 1.2.0
   823  * @since 1.2.0
   677  *
   824  *
   678  * @param unknown_type $mid
   825  * @param int $mid
   679  * @return unknown
   826  * @return bool
   680  */
   827  */
   681 function delete_meta( $mid ) {
   828 function delete_meta( $mid ) {
   682 	return delete_metadata_by_mid( 'post' , $mid );
   829 	return delete_metadata_by_mid( 'post' , $mid );
   683 }
   830 }
   684 
   831 
   685 /**
   832 /**
   686  * Get a list of previously defined keys.
   833  * Get a list of previously defined keys.
   687  *
   834  *
   688  * @since 1.2.0
   835  * @since 1.2.0
   689  *
   836  *
   690  * @return unknown
   837  * @return mixed
   691  */
   838  */
   692 function get_meta_keys() {
   839 function get_meta_keys() {
   693 	global $wpdb;
   840 	global $wpdb;
   694 
   841 
   695 	$keys = $wpdb->get_col( "
   842 	$keys = $wpdb->get_col( "
   700 
   847 
   701 	return $keys;
   848 	return $keys;
   702 }
   849 }
   703 
   850 
   704 /**
   851 /**
   705  * {@internal Missing Short Description}}
   852  * Get post meta data by meta ID.
   706  *
   853  *
   707  * @since 2.1.0
   854  * @since 2.1.0
   708  *
   855  *
   709  * @param unknown_type $mid
   856  * @param int $mid
   710  * @return unknown
   857  * @return object|bool
   711  */
   858  */
   712 function get_post_meta_by_id( $mid ) {
   859 function get_post_meta_by_id( $mid ) {
   713 	return get_metadata_by_mid( 'post', $mid );
   860 	return get_metadata_by_mid( 'post', $mid );
   714 }
   861 }
   715 
   862 
   716 /**
   863 /**
   717  * {@internal Missing Short Description}}
   864  * Get meta data for the given post ID.
   718  *
       
   719  * Some postmeta stuff.
       
   720  *
   865  *
   721  * @since 1.2.0
   866  * @since 1.2.0
   722  *
   867  *
   723  * @param unknown_type $postid
   868  * @param int $postid
   724  * @return unknown
   869  * @return mixed
   725  */
   870  */
   726 function has_meta( $postid ) {
   871 function has_meta( $postid ) {
   727 	global $wpdb;
   872 	global $wpdb;
   728 
   873 
   729 	return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, post_id
   874 	return $wpdb->get_results( $wpdb->prepare("SELECT meta_key, meta_value, meta_id, post_id
   730 			FROM $wpdb->postmeta WHERE post_id = %d
   875 			FROM $wpdb->postmeta WHERE post_id = %d
   731 			ORDER BY meta_key,meta_id", $postid), ARRAY_A );
   876 			ORDER BY meta_key,meta_id", $postid), ARRAY_A );
   732 }
   877 }
   733 
   878 
   734 /**
   879 /**
   735  * {@internal Missing Short Description}}
   880  * Update post meta data by meta ID.
   736  *
   881  *
   737  * @since 1.2.0
   882  * @since 1.2.0
   738  *
   883  *
   739  * @param unknown_type $meta_id
   884  * @param int    $meta_id
   740  * @param unknown_type $meta_key Expect Slashed
   885  * @param string $meta_key Expect Slashed
   741  * @param unknown_type $meta_value Expect Slashed
   886  * @param string $meta_value Expect Slashed
   742  * @return unknown
   887  * @return bool
   743  */
   888  */
   744 function update_meta( $meta_id, $meta_key, $meta_value ) {
   889 function update_meta( $meta_id, $meta_key, $meta_value ) {
   745 	$meta_key = wp_unslash( $meta_key );
   890 	$meta_key = wp_unslash( $meta_key );
   746 	$meta_value = wp_unslash( $meta_value );
   891 	$meta_value = wp_unslash( $meta_value );
   747 
   892 
   800 		$post['post_content'] = $content;
   945 		$post['post_content'] = $content;
   801 		// Escape data pulled from DB.
   946 		// Escape data pulled from DB.
   802 		$post = add_magic_quotes($post);
   947 		$post = add_magic_quotes($post);
   803 
   948 
   804 		return wp_update_post($post);
   949 		return wp_update_post($post);
   805 	}
       
   806 }
       
   807 
       
   808 /**
       
   809  * Move child posts to a new parent.
       
   810  *
       
   811  * @since 2.3.0
       
   812  * @access private
       
   813  *
       
   814  * @param unknown_type $old_ID
       
   815  * @param unknown_type $new_ID
       
   816  * @return unknown
       
   817  */
       
   818 function _relocate_children( $old_ID, $new_ID ) {
       
   819 	global $wpdb;
       
   820 	$old_ID = (int) $old_ID;
       
   821 	$new_ID = (int) $new_ID;
       
   822 
       
   823 	$children = $wpdb->get_col( $wpdb->prepare("
       
   824 		SELECT post_id
       
   825 		FROM $wpdb->postmeta
       
   826 		WHERE meta_key = '_wp_attachment_temp_parent'
       
   827 		AND meta_value = %d", $old_ID) );
       
   828 
       
   829 	foreach ( $children as $child_id ) {
       
   830 		$wpdb->update($wpdb->posts, array('post_parent' => $new_ID), array('ID' => $child_id) );
       
   831 		delete_post_meta($child_id, '_wp_attachment_temp_parent');
       
   832 	}
   950 	}
   833 }
   951 }
   834 
   952 
   835 /**
   953 /**
   836  * Get all the possible statuses for a post_type
   954  * Get all the possible statuses for a post_type
   881 	if ( isset($q['order']) )
   999 	if ( isset($q['order']) )
   882 		$order = $q['order'];
  1000 		$order = $q['order'];
   883 	elseif ( isset($q['post_status']) && 'pending' == $q['post_status'] )
  1001 	elseif ( isset($q['post_status']) && 'pending' == $q['post_status'] )
   884 		$order = 'ASC';
  1002 		$order = 'ASC';
   885 
  1003 
   886 	$per_page = 'edit_' . $post_type . '_per_page';
  1004 	$per_page = "edit_{$post_type}_per_page";
   887 	$posts_per_page = (int) get_user_option( $per_page );
  1005 	$posts_per_page = (int) get_user_option( $per_page );
   888 	if ( empty( $posts_per_page ) || $posts_per_page < 1 )
  1006 	if ( empty( $posts_per_page ) || $posts_per_page < 1 )
   889 		$posts_per_page = 20;
  1007 		$posts_per_page = 20;
   890 
  1008 
   891 	$posts_per_page = apply_filters( $per_page, $posts_per_page );
  1009 	/**
       
  1010 	 * Filter the number of items per page to show for a specific 'per_page' type.
       
  1011 	 *
       
  1012 	 * The dynamic portion of the hook name, `$post_type`, refers to the post type.
       
  1013 	 *
       
  1014 	 * Some examples of filter hooks generated here include: 'edit_attachment_per_page',
       
  1015 	 * 'edit_post_per_page', 'edit_page_per_page', etc.
       
  1016 	 *
       
  1017 	 * @since 3.0.0
       
  1018 	 *
       
  1019 	 * @param int $posts_per_page Number of posts to display per page for the given post
       
  1020 	 *                            type. Default 20.
       
  1021 	 */
       
  1022 	$posts_per_page = apply_filters( "edit_{$post_type}_per_page", $posts_per_page );
       
  1023 
       
  1024 	/**
       
  1025 	 * Filter the number of posts displayed per page when specifically listing "posts".
       
  1026 	 *
       
  1027 	 * @since 2.8.0
       
  1028 	 *
       
  1029 	 * @param int    $posts_per_page Number of posts to be displayed. Default 20.
       
  1030 	 * @param string $post_type      The post type.
       
  1031 	 */
   892 	$posts_per_page = apply_filters( 'edit_posts_per_page', $posts_per_page, $post_type );
  1032 	$posts_per_page = apply_filters( 'edit_posts_per_page', $posts_per_page, $post_type );
   893 
  1033 
   894 	$query = compact('post_type', 'post_status', 'perm', 'order', 'orderby', 'posts_per_page');
  1034 	$query = compact('post_type', 'post_status', 'perm', 'order', 'orderby', 'posts_per_page');
   895 
  1035 
   896 	// Hierarchical types require special args.
  1036 	// Hierarchical types require special args.
   897 	if ( is_post_type_hierarchical( $post_type ) && !isset($orderby) ) {
  1037 	if ( is_post_type_hierarchical( $post_type ) && !isset($orderby) ) {
   898 		$query['orderby'] = 'menu_order title';
  1038 		$query['orderby'] = 'menu_order title';
   899 		$query['order'] = 'asc';
  1039 		$query['order'] = 'asc';
   900 		$query['posts_per_page'] = -1;
  1040 		$query['posts_per_page'] = -1;
   901 		$query['posts_per_archive_page'] = -1;
  1041 		$query['posts_per_archive_page'] = -1;
       
  1042 		$query['fields'] = 'id=>parent';
   902 	}
  1043 	}
   903 
  1044 
   904 	if ( ! empty( $q['show_sticky'] ) )
  1045 	if ( ! empty( $q['show_sticky'] ) )
   905 		$query['post__in'] = (array) get_option( 'sticky_posts' );
  1046 		$query['post__in'] = (array) get_option( 'sticky_posts' );
   906 
  1047 
   908 
  1049 
   909 	return $avail_post_stati;
  1050 	return $avail_post_stati;
   910 }
  1051 }
   911 
  1052 
   912 /**
  1053 /**
   913  * {@internal Missing Short Description}}
  1054  * Get all available post MIME types for a given post type.
   914  *
  1055  *
   915  * @since 2.5.0
  1056  * @since 2.5.0
   916  *
  1057  *
   917  * @param unknown_type $type
  1058  * @param string $type
   918  * @return unknown
  1059  * @return mixed
   919  */
  1060  */
   920 function get_available_post_mime_types($type = 'attachment') {
  1061 function get_available_post_mime_types($type = 'attachment') {
   921 	global $wpdb;
  1062 	global $wpdb;
   922 
  1063 
   923 	$types = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type));
  1064 	$types = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type));
   924 	return $types;
  1065 	return $types;
   925 }
  1066 }
   926 
  1067 
   927 /**
  1068 /**
   928  * Executes a query for attachments. An array of WP_Query arguments
  1069  * Get the query variables for the current attachments request.
   929  * can be passed in, which will override the arguments set by this function.
  1070  *
   930  *
  1071  * @since 4.2.0
   931  * @since 2.5.0
  1072  *
   932  * @uses apply_filters() Calls 'upload_per_page' on posts_per_page argument
  1073  * @param array|false $q Optional. Array of query variables to use to build the query or false
   933  *
  1074  *                       to use $_GET superglobal. Default false.
   934  * @param array|bool $q Array of query variables to use to build the query or false to use $_GET superglobal.
  1075  * @return array The parsed query vars.
   935  * @return array
  1076  */
   936  */
  1077 function wp_edit_attachments_query_vars( $q = false ) {
   937 function wp_edit_attachments_query( $q = false ) {
  1078 	if ( false === $q ) {
   938 	if ( false === $q )
       
   939 		$q = $_GET;
  1079 		$q = $_GET;
   940 
  1080 	}
   941 	$q['m']   = isset( $q['m'] ) ? (int) $q['m'] : 0;
  1081 	$q['m']   = isset( $q['m'] ) ? (int) $q['m'] : 0;
   942 	$q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0;
  1082 	$q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0;
   943 	$q['post_type'] = 'attachment';
  1083 	$q['post_type'] = 'attachment';
   944 	$post_type = get_post_type_object( 'attachment' );
  1084 	$post_type = get_post_type_object( 'attachment' );
   945 	$states = 'inherit';
  1085 	$states = 'inherit';
   946 	if ( current_user_can( $post_type->cap->read_private_posts ) )
  1086 	if ( current_user_can( $post_type->cap->read_private_posts ) ) {
   947 		$states .= ',private';
  1087 		$states .= ',private';
       
  1088 	}
   948 
  1089 
   949 	$q['post_status'] = isset( $q['status'] ) && 'trash' == $q['status'] ? 'trash' : $states;
  1090 	$q['post_status'] = isset( $q['status'] ) && 'trash' == $q['status'] ? 'trash' : $states;
       
  1091 	$q['post_status'] = isset( $q['attachment-filter'] ) && 'trash' == $q['attachment-filter'] ? 'trash' : $states;
       
  1092 
   950 	$media_per_page = (int) get_user_option( 'upload_per_page' );
  1093 	$media_per_page = (int) get_user_option( 'upload_per_page' );
   951 	if ( empty( $media_per_page ) || $media_per_page < 1 )
  1094 	if ( empty( $media_per_page ) || $media_per_page < 1 ) {
   952 		$media_per_page = 20;
  1095 		$media_per_page = 20;
       
  1096 	}
       
  1097 
       
  1098 	/**
       
  1099 	 * Filter the number of items to list per page when listing media items.
       
  1100 	 *
       
  1101 	 * @since 2.9.0
       
  1102 	 *
       
  1103 	 * @param int $media_per_page Number of media to list. Default 20.
       
  1104 	 */
   953 	$q['posts_per_page'] = apply_filters( 'upload_per_page', $media_per_page );
  1105 	$q['posts_per_page'] = apply_filters( 'upload_per_page', $media_per_page );
   954 
  1106 
   955 	$post_mime_types = get_post_mime_types();
  1107 	$post_mime_types = get_post_mime_types();
   956 	$avail_post_mime_types = get_available_post_mime_types('attachment');
  1108 	if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) ) {
   957 
       
   958 	if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) )
       
   959 		unset($q['post_mime_type']);
  1109 		unset($q['post_mime_type']);
   960 
  1110 	}
   961 	if ( isset($q['detached']) )
  1111 
   962 		add_filter('posts_where', '_edit_attachments_query_helper');
  1112 	foreach( array_keys( $post_mime_types ) as $type ) {
   963 
  1113 		if ( isset( $q['attachment-filter'] ) && "post_mime_type:$type" == $q['attachment-filter'] ) {
   964 	wp( $q );
  1114 			$q['post_mime_type'] = $type;
   965 
  1115 			break;
   966 	if ( isset($q['detached']) )
  1116 		}
   967 		remove_filter('posts_where', '_edit_attachments_query_helper');
  1117 	}
   968 
  1118 
   969 	return array($post_mime_types, $avail_post_mime_types);
  1119 	if ( isset( $q['detached'] ) || ( isset( $q['attachment-filter'] ) && 'detached' == $q['attachment-filter'] ) ) {
   970 }
  1120 		$q['post_parent'] = 0;
   971 
  1121 	}
   972 function _edit_attachments_query_helper($where) {
  1122 
   973 	global $wpdb;
  1123 	return $q;
   974 	return $where .= " AND {$wpdb->posts}.post_parent < 1";
  1124 }
       
  1125 
       
  1126 /**
       
  1127  * Executes a query for attachments. An array of WP_Query arguments
       
  1128  * can be passed in, which will override the arguments set by this function.
       
  1129  *
       
  1130  * @since 2.5.0
       
  1131  *
       
  1132  * @param array|false $q Array of query variables to use to build the query or false to use $_GET superglobal.
       
  1133  * @return array
       
  1134  */
       
  1135 function wp_edit_attachments_query( $q = false ) {
       
  1136 	wp( wp_edit_attachments_query_vars( $q ) );
       
  1137 
       
  1138 	$post_mime_types = get_post_mime_types();
       
  1139 	$avail_post_mime_types = get_available_post_mime_types( 'attachment' );
       
  1140 
       
  1141 	return array( $post_mime_types, $avail_post_mime_types );
   975 }
  1142 }
   976 
  1143 
   977 /**
  1144 /**
   978  * Returns the list of classes to be used by a metabox
  1145  * Returns the list of classes to be used by a metabox
   979  *
  1146  *
   980  * @uses get_user_option()
       
   981  * @since 2.5.0
  1147  * @since 2.5.0
   982  *
  1148  *
   983  * @param unknown_type $id
  1149  * @param string $id
   984  * @param unknown_type $page
  1150  * @param string $page
   985  * @return unknown
  1151  * @return string
   986  */
  1152  */
   987 function postbox_classes( $id, $page ) {
  1153 function postbox_classes( $id, $page ) {
   988 	if ( isset( $_GET['edit'] ) && $_GET['edit'] == $id ) {
  1154 	if ( isset( $_GET['edit'] ) && $_GET['edit'] == $id ) {
   989 		$classes = array( '' );
  1155 		$classes = array( '' );
   990 	} elseif ( $closed = get_user_option('closedpostboxes_'.$page ) ) {
  1156 	} elseif ( $closed = get_user_option('closedpostboxes_'.$page ) ) {
   995 		}
  1161 		}
   996 	} else {
  1162 	} else {
   997 		$classes = array( '' );
  1163 		$classes = array( '' );
   998 	}
  1164 	}
   999 
  1165 
       
  1166 	/**
       
  1167 	 * Filter the postbox classes for a specific screen and screen ID combo.
       
  1168 	 *
       
  1169 	 * The dynamic portions of the hook name, `$page` and `$id`, refer to
       
  1170 	 * the screen and screen ID, respectively.
       
  1171 	 *
       
  1172 	 * @since 3.2.0
       
  1173 	 *
       
  1174 	 * @param array $classes An array of postbox classes.
       
  1175 	 */
  1000 	$classes = apply_filters( "postbox_classes_{$page}_{$id}", $classes );
  1176 	$classes = apply_filters( "postbox_classes_{$page}_{$id}", $classes );
  1001 	return implode( ' ', $classes );
  1177 	return implode( ' ', $classes );
  1002 }
  1178 }
  1003 
  1179 
  1004 /**
  1180 /**
  1005  * {@internal Missing Short Description}}
  1181  * Get a sample permalink based off of the post name.
  1006  *
  1182  *
  1007  * @since 2.5.0
  1183  * @since 2.5.0
  1008  *
  1184  *
  1009  * @param int|object $id    Post ID or post object.
  1185  * @param int    $id    Post ID or post object.
  1010  * @param string $title (optional) Title
  1186  * @param string $title Optional. Title. Default null.
  1011  * @param string $name (optional) Name
  1187  * @param string $name  Optional. Name. Default null.
  1012  * @return array With two entries of type string
  1188  * @return array Array with two entries of type string.
  1013  */
  1189  */
  1014 function get_sample_permalink($id, $title = null, $name = null) {
  1190 function get_sample_permalink($id, $title = null, $name = null) {
  1015 	$post = get_post( $id );
  1191 	$post = get_post( $id );
  1016 	if ( ! $post )
  1192 	if ( ! $post )
  1017 		return array( '', '' );
  1193 		return array( '', '' );
  1021 	$original_status = $post->post_status;
  1197 	$original_status = $post->post_status;
  1022 	$original_date = $post->post_date;
  1198 	$original_date = $post->post_date;
  1023 	$original_name = $post->post_name;
  1199 	$original_name = $post->post_name;
  1024 
  1200 
  1025 	// Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published.
  1201 	// Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published.
  1026 	if ( in_array( $post->post_status, array( 'draft', 'pending' ) ) ) {
  1202 	if ( in_array( $post->post_status, array( 'draft', 'pending', 'future' ) ) ) {
  1027 		$post->post_status = 'publish';
  1203 		$post->post_status = 'publish';
  1028 		$post->post_name = sanitize_title($post->post_name ? $post->post_name : $post->post_title, $post->ID);
  1204 		$post->post_name = sanitize_title($post->post_name ? $post->post_name : $post->post_title, $post->ID);
  1029 	}
  1205 	}
  1030 
  1206 
  1031 	// If the user wants to set a new name -- override the current one
  1207 	// If the user wants to set a new name -- override the current one
  1043 	$permalink = str_replace("%$post->post_type%", '%pagename%', $permalink);
  1219 	$permalink = str_replace("%$post->post_type%", '%pagename%', $permalink);
  1044 
  1220 
  1045 	// Handle page hierarchy
  1221 	// Handle page hierarchy
  1046 	if ( $ptype->hierarchical ) {
  1222 	if ( $ptype->hierarchical ) {
  1047 		$uri = get_page_uri($post);
  1223 		$uri = get_page_uri($post);
  1048 		$uri = untrailingslashit($uri);
  1224 		if ( $uri ) {
  1049 		$uri = strrev( stristr( strrev( $uri ), '/' ) );
  1225 			$uri = untrailingslashit($uri);
  1050 		$uri = untrailingslashit($uri);
  1226 			$uri = strrev( stristr( strrev( $uri ), '/' ) );
       
  1227 			$uri = untrailingslashit($uri);
       
  1228 		}
       
  1229 
       
  1230 		/** This filter is documented in wp-admin/edit-tag-form.php */
  1051 		$uri = apply_filters( 'editable_slug', $uri );
  1231 		$uri = apply_filters( 'editable_slug', $uri );
  1052 		if ( !empty($uri) )
  1232 		if ( !empty($uri) )
  1053 			$uri .= '/';
  1233 			$uri .= '/';
  1054 		$permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink);
  1234 		$permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink);
  1055 	}
  1235 	}
  1056 
  1236 
  1057 	$permalink = array($permalink, apply_filters('editable_slug', $post->post_name));
  1237 	/** This filter is documented in wp-admin/edit-tag-form.php */
       
  1238 	$permalink = array( $permalink, apply_filters( 'editable_slug', $post->post_name ) );
  1058 	$post->post_status = $original_status;
  1239 	$post->post_status = $original_status;
  1059 	$post->post_date = $original_date;
  1240 	$post->post_date = $original_date;
  1060 	$post->post_name = $original_name;
  1241 	$post->post_name = $original_name;
  1061 	unset($post->filter);
  1242 	unset($post->filter);
  1062 
  1243 
  1066 /**
  1247 /**
  1067  * Returns the HTML of the sample permalink slug editor.
  1248  * Returns the HTML of the sample permalink slug editor.
  1068  *
  1249  *
  1069  * @since 2.5.0
  1250  * @since 2.5.0
  1070  *
  1251  *
  1071  * @param int|object $id Post ID or post object.
  1252  * @param int    $id        Post ID or post object.
  1072  * @param string $new_title Optional. New title.
  1253  * @param string $new_title Optional. New title. Default null.
  1073  * @param string $new_slug Optional. New slug.
  1254  * @param string $new_slug  Optional. New slug. Default null.
  1074  * @return string The HTML of the sample permalink slug editor.
  1255  * @return string The HTML of the sample permalink slug editor.
  1075  */
  1256  */
  1076 function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
  1257 function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
  1077 	$post = get_post( $id );
  1258 	$post = get_post( $id );
  1078 	if ( ! $post )
  1259 	if ( ! $post )
  1079 		return '';
  1260 		return '';
  1080 
  1261 
  1081 	list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
  1262 	list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
  1082 
  1263 
       
  1264 	if ( current_user_can( 'read_post', $post->ID ) ) {
       
  1265 		$ptype = get_post_type_object( $post->post_type );
       
  1266 		$view_post = $ptype->labels->view_item;
       
  1267 	}
       
  1268 
  1083 	if ( 'publish' == get_post_status( $post ) ) {
  1269 	if ( 'publish' == get_post_status( $post ) ) {
  1084 		$ptype = get_post_type_object($post->post_type);
       
  1085 		$view_post = $ptype->labels->view_item;
       
  1086 		$title = __('Click to edit this part of the permalink');
  1270 		$title = __('Click to edit this part of the permalink');
  1087 	} else {
  1271 	} else {
  1088 		$title = __('Temporary permalink. Click to edit this part.');
  1272 		$title = __('Temporary permalink. Click to edit this part.');
  1089 	}
  1273 	}
  1090 
  1274 
  1091 	if ( false === strpos($permalink, '%postname%') && false === strpos($permalink, '%pagename%') ) {
  1275 	if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) {
  1092 		$return = '<strong>' . __('Permalink:') . "</strong>\n" . '<span id="sample-permalink" tabindex="-1">' . $permalink . "</span>\n";
  1276 		$return = '<strong>' . __('Permalink:') . "</strong>\n" . '<span id="sample-permalink" tabindex="-1">' . $permalink . "</span>\n";
  1093 		if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) )
  1277 		if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) ) {
  1094 			$return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __('Change Permalinks') . "</a></span>\n";
  1278 			$return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __('Change Permalinks') . "</a></span>\n";
  1095 		if ( isset( $view_post ) )
  1279 		}
  1096 			$return .= "<span id='view-post-btn'><a href='$permalink' class='button button-small'>$view_post</a></span>\n";
  1280 	} else {
  1097 
  1281 		if ( function_exists( 'mb_strlen' ) ) {
  1098 		$return = apply_filters('get_sample_permalink_html', $return, $id, $new_title, $new_slug);
  1282 			if ( mb_strlen( $post_name ) > 30 ) {
  1099 
  1283 				$post_name_abridged = mb_substr( $post_name, 0, 14 ) . '&hellip;' . mb_substr( $post_name, -14 );
  1100 		return $return;
  1284 			} else {
  1101 	}
  1285 				$post_name_abridged = $post_name;
  1102 
  1286 			}
  1103 	if ( function_exists('mb_strlen') ) {
       
  1104 		if ( mb_strlen($post_name) > 30 ) {
       
  1105 			$post_name_abridged = mb_substr($post_name, 0, 14). '&hellip;' . mb_substr($post_name, -14);
       
  1106 		} else {
  1287 		} else {
  1107 			$post_name_abridged = $post_name;
  1288 			if ( strlen( $post_name ) > 30 ) {
  1108 		}
  1289 				$post_name_abridged = substr( $post_name, 0, 14 ) . '&hellip;' . substr( $post_name, -14 );
  1109 	} else {
  1290 			} else {
  1110 		if ( strlen($post_name) > 30 ) {
  1291 				$post_name_abridged = $post_name;
  1111 			$post_name_abridged = substr($post_name, 0, 14). '&hellip;' . substr($post_name, -14);
  1292 			}
       
  1293 		}
       
  1294 
       
  1295 		$post_name_html = '<span id="editable-post-name" title="' . $title . '">' . $post_name_abridged . '</span>';
       
  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.
       
  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";
       
  1303 		$return .= '<span id="editable-post-name-full">' . $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";
  1112 		} else {
  1312 		} else {
  1113 			$post_name_abridged = $post_name;
  1313 			if ( empty( $pretty_permalink ) ) {
  1114 		}
  1314 				$pretty_permalink = $permalink;
  1115 	}
  1315 			}
  1116 
  1316 
  1117 	$post_name_html = '<span id="editable-post-name" title="' . $title . '">' . $post_name_abridged . '</span>';
  1317 			$return .= "<span id='view-post-btn'><a href='" . $pretty_permalink . "' class='button button-small'>$view_post</a></span>\n";
  1118 	$display_link = str_replace(array('%pagename%','%postname%'), $post_name_html, $permalink);
  1318 		}
  1119 	$view_link = str_replace(array('%pagename%','%postname%'), $post_name, $permalink);
  1319 	}
  1120 	$return =  '<strong>' . __('Permalink:') . "</strong>\n";
  1320 
  1121 	$return .= '<span id="sample-permalink" tabindex="-1">' . $display_link . "</span>\n";
  1321 	/**
  1122 	$return .= '&lrm;'; // Fix bi-directional text display defect in RTL languages.
  1322 	 * Filter the sample permalink HTML markup.
  1123 	$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";
  1323 	 *
  1124 	$return .= '<span id="editable-post-name-full">' . $post_name . "</span>\n";
  1324 	 * @since 2.9.0
  1125 	if ( isset($view_post) )
  1325 	 *
  1126 		$return .= "<span id='view-post-btn'><a href='$view_link' class='button button-small'>$view_post</a></span>\n";
  1326 	 * @param string      $return    Sample permalink HTML markup.
  1127 
  1327 	 * @param int|WP_Post $id        Post object or ID.
  1128 	$return = apply_filters('get_sample_permalink_html', $return, $id, $new_title, $new_slug);
  1328 	 * @param string      $new_title New sample permalink title.
       
  1329 	 * @param string      $new_slug  New sample permalink slug.
       
  1330 	 */
       
  1331 	$return = apply_filters( 'get_sample_permalink_html', $return, $id, $new_title, $new_slug );
  1129 
  1332 
  1130 	return $return;
  1333 	return $return;
  1131 }
  1334 }
  1132 
  1335 
  1133 /**
  1336 /**
  1161 			$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>';
  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>';
  1162 		}
  1365 		}
  1163 		$content_width = $old_content_width;
  1366 		$content_width = $old_content_width;
  1164 	}
  1367 	}
  1165 
  1368 
       
  1369 	/**
       
  1370 	 * Filter the admin post thumbnail HTML markup to return.
       
  1371 	 *
       
  1372 	 * @since 2.9.0
       
  1373 	 *
       
  1374 	 * @param string $content Admin post thumbnail HTML markup.
       
  1375 	 * @param int    $post_id Post ID.
       
  1376 	 */
  1166 	return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID );
  1377 	return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID );
  1167 }
  1378 }
  1168 
  1379 
  1169 /**
  1380 /**
  1170  * Check to see if the post is currently being edited by another user.
  1381  * Check to see if the post is currently being edited by another user.
  1171  *
  1382  *
  1172  * @since 2.5.0
  1383  * @since 2.5.0
  1173  *
  1384  *
  1174  * @param int $post_id ID of the post to check for editing
  1385  * @param int $post_id ID of the post to check for editing
  1175  * @return bool|int False: not locked or locked by current user. Int: user ID of user with lock.
  1386  * @return integer False: not locked or locked by current user. Int: user ID of user with lock.
  1176  */
  1387  */
  1177 function wp_check_post_lock( $post_id ) {
  1388 function wp_check_post_lock( $post_id ) {
  1178 	if ( !$post = get_post( $post_id ) )
  1389 	if ( !$post = get_post( $post_id ) )
  1179 		return false;
  1390 		return false;
  1180 
  1391 
  1183 
  1394 
  1184 	$lock = explode( ':', $lock );
  1395 	$lock = explode( ':', $lock );
  1185 	$time = $lock[0];
  1396 	$time = $lock[0];
  1186 	$user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
  1397 	$user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
  1187 
  1398 
  1188 	$time_window = apply_filters( 'wp_check_post_lock_window', 120 );
  1399 	/** This filter is documented in wp-admin/includes/ajax-actions.php */
       
  1400 	$time_window = apply_filters( 'wp_check_post_lock_window', 150 );
  1189 
  1401 
  1190 	if ( $time && $time > time() - $time_window && $user != get_current_user_id() )
  1402 	if ( $time && $time > time() - $time_window && $user != get_current_user_id() )
  1191 		return $user;
  1403 		return $user;
  1192 	return false;
  1404 	return false;
  1193 }
  1405 }
  1227 	$user = null;
  1439 	$user = null;
  1228 	if (  $user_id = wp_check_post_lock( $post->ID ) )
  1440 	if (  $user_id = wp_check_post_lock( $post->ID ) )
  1229 		$user = get_userdata( $user_id );
  1441 		$user = get_userdata( $user_id );
  1230 
  1442 
  1231 	if ( $user ) {
  1443 	if ( $user ) {
       
  1444 
       
  1445 		/**
       
  1446 		 * Filter whether to show the post locked dialog.
       
  1447 		 *
       
  1448 		 * Returning a falsey value to the filter will short-circuit displaying the dialog.
       
  1449 		 *
       
  1450 		 * @since 3.6.0
       
  1451 		 *
       
  1452 		 * @param bool         $display Whether to display the dialog. Default true.
       
  1453 		 * @param WP_User|bool $user    WP_User object on success, false otherwise.
       
  1454 		 */
  1232 		if ( ! apply_filters( 'show_post_locked_dialog', true, $post, $user ) )
  1455 		if ( ! apply_filters( 'show_post_locked_dialog', true, $post, $user ) )
  1233 			return;
  1456 			return;
  1234 
  1457 
  1235 		$locked = true;
  1458 		$locked = true;
  1236 	} else {
  1459 	} else {
  1269 			}
  1492 			}
  1270 		} else {
  1493 		} else {
  1271 			$preview_link = '';
  1494 			$preview_link = '';
  1272 		}
  1495 		}
  1273 
  1496 
  1274 		$preview_link = apply_filters( 'preview_post_link', $preview_link );
  1497 		/** This filter is documented in wp-admin/includes/meta-boxes.php */
       
  1498 		$preview_link = apply_filters( 'preview_post_link', $preview_link, $post );
       
  1499 
       
  1500 		/**
       
  1501 		 * Filter whether to allow the post lock to be overridden.
       
  1502 		 *
       
  1503 		 * Returning a falsey value to the filter will disable the ability
       
  1504 		 * to override the post lock.
       
  1505 		 *
       
  1506 		 * @since 3.6.0
       
  1507 		 *
       
  1508 		 * @param bool    $override Whether to allow overriding post locks. Default true.
       
  1509 		 * @param WP_Post $post     Post object.
       
  1510 		 * @param WP_User $user     User object.
       
  1511 		 */
  1275 		$override = apply_filters( 'override_post_lock', true, $post, $user );
  1512 		$override = apply_filters( 'override_post_lock', true, $post, $user );
  1276 		$tab_last = $override ? '' : ' wp-tab-last';
  1513 		$tab_last = $override ? '' : ' wp-tab-last';
  1277 
  1514 
  1278 		?>
  1515 		?>
  1279 		<div class="post-locked-message">
  1516 		<div class="post-locked-message">
  1283 			_e( 'This content is currently locked.' );
  1520 			_e( 'This content is currently locked.' );
  1284 			if ( $override )
  1521 			if ( $override )
  1285 				printf( ' ' . __( 'If you take over, %s will be blocked from continuing to edit.' ), esc_html( $user->display_name ) );
  1522 				printf( ' ' . __( 'If you take over, %s will be blocked from continuing to edit.' ), esc_html( $user->display_name ) );
  1286 		?>
  1523 		?>
  1287 		</p>
  1524 		</p>
  1288 		<?php do_action( 'post_locked_dialog', $post ); ?>
  1525 		<?php
       
  1526 		/**
       
  1527 		 * Fires inside the post locked dialog before the buttons are displayed.
       
  1528 		 *
       
  1529 		 * @since 3.6.0
       
  1530 		 *
       
  1531 		 * @param WP_Post $post Post object.
       
  1532 		 */
       
  1533 		do_action( 'post_locked_dialog', $post );
       
  1534 		?>
  1289 		<p>
  1535 		<p>
  1290 		<a class="button" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a>
  1536 		<a class="button" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a>
  1291 		<?php if ( $preview_link ) { ?>
  1537 		<?php if ( $preview_link ) { ?>
  1292 		<a class="button<?php echo $tab_last; ?>" href="<?php echo esc_url( $preview_link ); ?>"><?php _e('Preview'); ?></a>
  1538 		<a class="button<?php echo $tab_last; ?>" href="<?php echo esc_url( $preview_link ); ?>"><?php _e('Preview'); ?></a>
  1293 		<?php
  1539 		<?php
  1307 	} else {
  1553 	} else {
  1308 		?>
  1554 		?>
  1309 		<div class="post-taken-over">
  1555 		<div class="post-taken-over">
  1310 			<div class="post-locked-avatar"></div>
  1556 			<div class="post-locked-avatar"></div>
  1311 			<p class="wp-tab-first" tabindex="0">
  1557 			<p class="wp-tab-first" tabindex="0">
  1312 			<span class="currently-editing"></span><br>
  1558 			<span class="currently-editing"></span><br />
  1313 			<span class="locked-saving hidden"><img src="images/wpspin_light-2x.gif" width="16" height="16" /> <?php _e('Saving revision...'); ?></span>
  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>
  1314 			<span class="locked-saved hidden"><?php _e('Your latest changes were saved as a revision.'); ?></span>
  1560 			<span class="locked-saved hidden"><?php _e('Your latest changes were saved as a revision.'); ?></span>
  1315 			</p>
  1561 			</p>
  1316 			<?php do_action( 'post_lock_lost_dialog', $post ); ?>
  1562 			<?php
       
  1563 			/**
       
  1564 			 * Fires inside the dialog displayed when a user has lost the post lock.
       
  1565 			 *
       
  1566 			 * @since 3.6.0
       
  1567 			 *
       
  1568 			 * @param WP_Post $post Post object.
       
  1569 			 */
       
  1570 			do_action( 'post_lock_lost_dialog', $post );
       
  1571 			?>
  1317 			<p><a class="button button-primary wp-tab-last" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a></p>
  1572 			<p><a class="button button-primary wp-tab-last" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a></p>
  1318 		</div>
  1573 		</div>
  1319 		<?php
  1574 		<?php
  1320 	}
  1575 	}
  1321 
  1576 
  1330  *
  1585  *
  1331  * @package WordPress
  1586  * @package WordPress
  1332  * @subpackage Post_Revisions
  1587  * @subpackage Post_Revisions
  1333  * @since 2.6.0
  1588  * @since 2.6.0
  1334  *
  1589  *
  1335  * @uses _wp_translate_postdata()
  1590  * @param mixed $post_data Associative array containing the post data or int post ID.
  1336  * @uses _wp_post_revision_fields()
  1591  * @return mixed The autosave revision ID. WP_Error or 0 on error.
  1337  *
  1592  */
  1338  * @return unknown
  1593 function wp_create_post_autosave( $post_data ) {
  1339  */
  1594 	if ( is_numeric( $post_data ) ) {
  1340 function wp_create_post_autosave( $post_id ) {
  1595 		$post_id = $post_data;
  1341 	$translated = _wp_translate_postdata( true );
  1596 		$post_data = &$_POST;
  1342 	if ( is_wp_error( $translated ) )
  1597 	} else {
  1343 		return $translated;
  1598 		$post_id = (int) $post_data['post_ID'];
       
  1599 	}
       
  1600 
       
  1601 	$post_data = _wp_translate_postdata( true, $post_data );
       
  1602 	if ( is_wp_error( $post_data ) )
       
  1603 		return $post_data;
  1344 
  1604 
  1345 	$post_author = get_current_user_id();
  1605 	$post_author = get_current_user_id();
  1346 
  1606 
  1347 	// Store one autosave per author. If there is already an autosave, overwrite it.
  1607 	// Store one autosave per author. If there is already an autosave, overwrite it.
  1348 	if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) {
  1608 	if ( $old_autosave = wp_get_post_autosave( $post_id, $post_author ) ) {
  1349 		$new_autosave = _wp_post_revision_fields( $_POST, true );
  1609 		$new_autosave = _wp_post_revision_fields( $post_data, true );
  1350 		$new_autosave['ID'] = $old_autosave->ID;
  1610 		$new_autosave['ID'] = $old_autosave->ID;
  1351 		$new_autosave['post_author'] = $post_author;
  1611 		$new_autosave['post_author'] = $post_author;
  1352 
  1612 
  1353 		// If the new autosave is the same content as the post, delete the old autosave.
  1613 		// If the new autosave has the same content as the post, delete the autosave.
  1354 		$post = get_post( $post_id );
  1614 		$post = get_post( $post_id );
  1355 		$autosave_is_different = false;
  1615 		$autosave_is_different = false;
  1356 		foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {
  1616 		foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields() ) ) as $field ) {
  1357 			if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) {
  1617 			if ( normalize_whitespace( $new_autosave[ $field ] ) != normalize_whitespace( $post->$field ) ) {
  1358 				$autosave_is_different = true;
  1618 				$autosave_is_different = true;
  1359 				break;
  1619 				break;
  1360 			}
  1620 			}
  1361 		}
  1621 		}
  1362 
  1622 
  1363 		if ( ! $autosave_is_different ) {
  1623 		if ( ! $autosave_is_different ) {
  1364 			wp_delete_post_revision( $old_autosave->ID );
  1624 			wp_delete_post_revision( $old_autosave->ID );
  1365 			return;
  1625 			return 0;
  1366 		}
  1626 		}
       
  1627 
       
  1628 		/**
       
  1629 		 * Fires before an autosave is stored.
       
  1630 		 *
       
  1631 		 * @since 4.1.0
       
  1632 		 *
       
  1633 		 * @param array $new_autosave Post array - the autosave that is about to be saved.
       
  1634 		 */
       
  1635 		do_action( 'wp_creating_autosave', $new_autosave );
  1367 
  1636 
  1368 		return wp_update_post( $new_autosave );
  1637 		return wp_update_post( $new_autosave );
  1369 	}
  1638 	}
  1370 
  1639 
  1371 	// _wp_put_post_revision() expects unescaped.
  1640 	// _wp_put_post_revision() expects unescaped.
  1372 	$post_data = wp_unslash( $_POST );
  1641 	$post_data = wp_unslash( $post_data );
  1373 
  1642 
  1374 	// Otherwise create the new autosave as a special post revision
  1643 	// Otherwise create the new autosave as a special post revision
  1375 	return _wp_put_post_revision( $post_data, true );
  1644 	return _wp_put_post_revision( $post_data, true );
  1376 }
  1645 }
  1377 
  1646 
  1379  * Save draft or manually autosave for showing preview.
  1648  * Save draft or manually autosave for showing preview.
  1380  *
  1649  *
  1381  * @package WordPress
  1650  * @package WordPress
  1382  * @since 2.7.0
  1651  * @since 2.7.0
  1383  *
  1652  *
  1384  * @uses get_post_status()
       
  1385  * @uses edit_post()
       
  1386  * @uses get_post()
       
  1387  * @uses current_user_can()
       
  1388  * @uses wp_die()
       
  1389  * @uses wp_create_post_autosave()
       
  1390  * @uses add_query_arg()
       
  1391  * @uses wp_create_nonce()
       
  1392  *
       
  1393  * @return str URL to redirect to show the preview
  1653  * @return str URL to redirect to show the preview
  1394  */
  1654  */
  1395 function post_preview() {
  1655 function post_preview() {
  1396 
  1656 
  1397 	$post_ID = (int) $_POST['post_ID'];
  1657 	$post_ID = (int) $_POST['post_ID'];
  1398 	$status = get_post_status( $post_ID );
       
  1399 	if ( 'auto-draft' == $status )
       
  1400 		wp_die( __('Preview not available. Please save as a draft first.') );
       
  1401 
       
  1402 	if ( isset($_POST['catslist']) )
       
  1403 		$_POST['post_category'] = explode(",", $_POST['catslist']);
       
  1404 
       
  1405 	if ( isset($_POST['tags_input']) )
       
  1406 		$_POST['tags_input'] = explode(",", $_POST['tags_input']);
       
  1407 
       
  1408 	if ( $_POST['post_type'] == 'page' || empty($_POST['post_category']) )
       
  1409 		unset($_POST['post_category']);
       
  1410 
       
  1411 	$_POST['ID'] = $post_ID;
  1658 	$_POST['ID'] = $post_ID;
  1412 	$post = get_post($post_ID);
  1659 
  1413 
  1660 	if ( ! $post = get_post( $post_ID ) ) {
  1414 	if ( 'page' == $post->post_type ) {
  1661 		wp_die( __( 'You are not allowed to edit this post.' ) );
  1415 		if ( ! current_user_can('edit_page', $post_ID) )
  1662 	}
  1416 			wp_die( __('You are not allowed to edit this page.') );
  1663 
       
  1664 	if ( ! current_user_can( 'edit_post', $post->ID ) ) {
       
  1665 		wp_die( __( 'You are not allowed to edit this post.' ) );
       
  1666 	}
       
  1667 
       
  1668 	$is_autosave = false;
       
  1669 
       
  1670 	if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'draft' == $post->post_status || 'auto-draft' == $post->post_status ) ) {
       
  1671 		$saved_post_id = edit_post();
  1417 	} else {
  1672 	} else {
  1418 		if ( ! current_user_can('edit_post', $post_ID) )
  1673 		$is_autosave = true;
  1419 			wp_die( __('You are not allowed to edit this post.') );
  1674 
  1420 	}
  1675 		if ( isset( $_POST['post_status'] ) && 'auto-draft' == $_POST['post_status'] )
  1421 
  1676 			$_POST['post_status'] = 'draft';
  1422 	$user_id = get_current_user_id();
  1677 
  1423 	$locked = wp_check_post_lock( $post->ID );
  1678 		$saved_post_id = wp_create_post_autosave( $post->ID );
  1424 	if ( ! $locked && 'draft' == $post->post_status && $user_id == $post->post_author ) {
  1679 	}
  1425 		$id = edit_post();
  1680 
  1426 	} else { // Non drafts are not overwritten. The autosave is stored in a special post revision.
  1681 	if ( is_wp_error( $saved_post_id ) )
  1427 		$id = wp_create_post_autosave( $post->ID );
  1682 		wp_die( $saved_post_id->get_error_message() );
  1428 		if ( ! is_wp_error($id) )
  1683 
  1429 			$id = $post->ID;
  1684 	$query_args = array( 'preview' => 'true' );
  1430 	}
  1685 
  1431 
  1686 	if ( $is_autosave && $saved_post_id ) {
  1432 	if ( is_wp_error($id) )
  1687 		$query_args['preview_id'] = $post->ID;
  1433 		wp_die( $id->get_error_message() );
  1688 		$query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $post->ID );
  1434 
  1689 
  1435 	if ( ! $locked && $_POST['post_status'] == 'draft' && $user_id == $post->post_author ) {
  1690 		if ( isset( $_POST['post_format'] ) )
  1436 		$url = add_query_arg( 'preview', 'true', get_permalink($id) );
  1691 			$query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
       
  1692 	}
       
  1693 
       
  1694 	$url = add_query_arg( $query_args, get_permalink( $post->ID ) );
       
  1695 
       
  1696 	/** This filter is documented in wp-admin/includes/meta-boxes.php */
       
  1697 	return apply_filters( 'preview_post_link', $url, $post );
       
  1698 }
       
  1699 
       
  1700 /**
       
  1701  * Save a post submitted with XHR
       
  1702  *
       
  1703  * Intended for use with heartbeat and autosave.js
       
  1704  *
       
  1705  * @since 3.9.0
       
  1706  *
       
  1707  * @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.
       
  1709  *               Te ID can be the draft post_id or the autosave revision post_id.
       
  1710  */
       
  1711 function wp_autosave( $post_data ) {
       
  1712 	// Back-compat
       
  1713 	if ( ! defined( 'DOING_AUTOSAVE' ) )
       
  1714 		define( 'DOING_AUTOSAVE', true );
       
  1715 
       
  1716 	$post_id = (int) $post_data['post_id'];
       
  1717 	$post_data['ID'] = $post_data['post_ID'] = $post_id;
       
  1718 
       
  1719 	if ( false === wp_verify_nonce( $post_data['_wpnonce'], 'update-post_' . $post_id ) ) {
       
  1720 		return new WP_Error( 'invalid_nonce', __( 'Error while saving.' ) );
       
  1721 	}
       
  1722 
       
  1723 	$post = get_post( $post_id );
       
  1724 
       
  1725 	if ( ! current_user_can( 'edit_post', $post->ID ) ) {
       
  1726 		return new WP_Error( 'edit_posts', __( 'You are not allowed to edit this item.' ) );
       
  1727 	}
       
  1728 
       
  1729 	if ( 'auto-draft' == $post->post_status )
       
  1730 		$post_data['post_status'] = 'draft';
       
  1731 
       
  1732 	if ( $post_data['post_type'] != 'page' && ! empty( $post_data['catslist'] ) )
       
  1733 		$post_data['post_category'] = explode( ',', $post_data['catslist'] );
       
  1734 
       
  1735 	if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author && ( 'auto-draft' == $post->post_status || 'draft' == $post->post_status ) ) {
       
  1736 		// Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked
       
  1737 		return edit_post( wp_slash( $post_data ) );
  1437 	} else {
  1738 	} else {
  1438 		$nonce = wp_create_nonce('post_preview_' . $id);
  1739 		// Non drafts or other users drafts are not overwritten. The autosave is stored in a special post revision for each user.
  1439 		$args = array(
  1740 		return wp_create_post_autosave( wp_slash( $post_data ) );
  1440 			'preview' => 'true',
  1741 	}
  1441 			'preview_id' => $id,
  1742 }
  1442 			'preview_nonce' => $nonce,
       
  1443 		);
       
  1444 
       
  1445 		if ( isset( $_POST['post_format'] ) )
       
  1446 			$args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] );
       
  1447 
       
  1448 		$url = add_query_arg( $args, get_permalink($id) );
       
  1449 	}
       
  1450 
       
  1451 	return apply_filters( 'preview_post_link', $url );
       
  1452 }