Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
215 changes: 121 additions & 94 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -3345,12 +3345,12 @@ function wp_insert_post( $postarr, $wp_error = false ) {

unset( $postarr['filter'] );

$postarr = sanitize_post( $postarr, 'db' );
$postarr = sanitize_post( $postarr, 'db' );
$original_postarr = $postarr;

// Are we updating or creating?
$post_ID = 0;
$update = false;
$guid = $postarr['guid'];

if ( ! empty( $postarr['ID'] ) ) {
$update = true;
Expand All @@ -3365,29 +3365,26 @@ function wp_insert_post( $postarr, $wp_error = false ) {
return 0;
}

$guid = get_post_field( 'guid', $post_ID );
$postarr['guid'] = get_post_field( 'guid', $post_ID );
$previous_status = get_post_field( 'post_status', $post_ID );
} else {
$previous_status = 'new';
}

$post_type = empty( $postarr['post_type'] ) ? 'post' : $postarr['post_type'];
if ( empty( $postarr['post_type'] ) ) {
$postarr['post_type'] = 'post';
}

$post_title = $postarr['post_title'];
$post_content = $postarr['post_content'];
$post_excerpt = $postarr['post_excerpt'];
if ( isset( $postarr['post_name'] ) ) {
$post_name = $postarr['post_name'];
} elseif ( $update ) {
if ( ! isset( $postarr['post_name'] ) && $update ) {
// For an update, don't modify the post_name if it wasn't supplied as an argument.
$post_name = $post_before->post_name;
$postarr['post_name'] = $post_before->post_name;
}

$maybe_empty = 'attachment' !== $post_type
&& ! $post_content && ! $post_title && ! $post_excerpt
&& post_type_supports( $post_type, 'editor' )
&& post_type_supports( $post_type, 'title' )
&& post_type_supports( $post_type, 'excerpt' );
$maybe_empty = 'attachment' !== $postarr['post_type']
&& ! $postarr['post_content'] && ! $postarr['post_title'] && ! $postarr['post_excerpt']
&& post_type_supports( $postarr['post_type'], 'editor' )
&& post_type_supports( $postarr['post_type'], 'title' )
&& post_type_supports( $postarr['post_type'], 'excerpt' );

/**
* Filters whether the post should be considered "empty".
Expand All @@ -3413,23 +3410,26 @@ function wp_insert_post( $postarr, $wp_error = false ) {
}
}

$post_status = empty( $postarr['post_status'] ) ? 'draft' : $postarr['post_status'];
if ( 'attachment' === $post_type && ! in_array( $post_status, array( 'inherit', 'private', 'trash', 'auto-draft' ), true ) ) {
$post_status = 'inherit';
if ( empty( $postarr['post_status'] ) ) {
$postarr['post_status'] = 'draft';
}

if ( 'attachment' === $postarr['post_type'] && ! in_array( $postarr['post_status'], array( 'inherit', 'private', 'trash', 'auto-draft' ), true ) ) {
$postarr['post_status'] = 'inherit';
}

if ( ! empty( $postarr['post_category'] ) ) {
// Filter out empty terms.
$post_category = array_filter( $postarr['post_category'] );
$postarr['post_category'] = array_filter( $postarr['post_category'] );
}

// Make sure we set a valid category.
if ( empty( $post_category ) || 0 == count( $post_category ) || ! is_array( $post_category ) ) {
if ( empty( $postarr['post_category'] ) || 0 == count( $postarr['post_category'] ) || ! is_array( $postarr['post_category'] ) ) {
// 'post' requires at least one category.
if ( 'post' == $post_type && 'auto-draft' != $post_status ) {
$post_category = array( get_option( 'default_category' ) );
if ( 'post' == $postarr['post_type'] && 'auto-draft' != $postarr['post_status'] ) {
$postarr['post_category'] = array( get_option( 'default_category' ) );
} else {
$post_category = array();
$postarr['post_category'] = array();
}
}

Expand All @@ -3438,31 +3438,31 @@ function wp_insert_post( $postarr, $wp_error = false ) {
*
* For new posts check the primitive capability, for updates check the meta capability.
*/
$post_type_object = get_post_type_object( $post_type );
$post_type_object = get_post_type_object( $postarr['post_type'] );

if ( ! $update && 'pending' === $post_status && ! current_user_can( $post_type_object->cap->publish_posts ) ) {
$post_name = '';
} elseif ( $update && 'pending' === $post_status && ! current_user_can( 'publish_post', $post_ID ) ) {
$post_name = '';
if ( ! $update && 'pending' === $postarr['post_status'] && ! current_user_can( $post_type_object->cap->publish_posts ) ) {
$postarr['post_name'] = '';
} elseif ( $update && 'pending' === $postarr['post_status'] && ! current_user_can( 'publish_post', $post_ID ) ) {
$postarr['post_name'] = '';
}

/*
* Create a valid post name. Drafts and pending posts are allowed to have
* an empty post name.
*/
if ( empty( $post_name ) ) {
if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
$post_name = sanitize_title( $post_title );
if ( empty( $postarr['post_name'] ) ) {
if ( ! in_array( $postarr['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
$postarr['post_name'] = sanitize_title( $postarr['post_title'] );
} else {
$post_name = '';
$postarr['post_name'] = '';
}
} else {
// On updates, we need to check to see if it's using the old, fixed sanitization context.
$check_name = sanitize_title( $post_name, '', 'old-save' );
if ( $update && strtolower( urlencode( $post_name ) ) == $check_name && get_post_field( 'post_name', $post_ID ) == $check_name ) {
$post_name = $check_name;
$check_name = sanitize_title( $postarr['post_name'], '', 'old-save' );
if ( $update && strtolower( urlencode( $postarr['post_name'] ) ) == $check_name && get_post_field( 'post_name', $post_ID ) == $check_name ) {
$postarr['post_name'] = $check_name;
} else { // new post, or slug has changed.
$post_name = sanitize_title( $post_name );
$postarr['post_name'] = sanitize_title( $postarr['post_name'] );
}
}

Expand All @@ -3472,19 +3472,18 @@ function wp_insert_post( $postarr, $wp_error = false ) {
*/
if ( empty( $postarr['post_date'] ) || '0000-00-00 00:00:00' == $postarr['post_date'] ) {
if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' == $postarr['post_date_gmt'] ) {
$post_date = current_time( 'mysql' );
$postarr['post_date'] = current_time( 'mysql' );
} else {
$post_date = get_date_from_gmt( $postarr['post_date_gmt'] );
$postarr['post_date'] = get_date_from_gmt( $postarr['post_date_gmt'] );
}
} else {
$post_date = $postarr['post_date'];
}

// Validate the date.
$mm = substr( $post_date, 5, 2 );
$jj = substr( $post_date, 8, 2 );
$aa = substr( $post_date, 0, 4 );
$valid_date = wp_checkdate( $mm, $jj, $aa, $post_date );
$mm = substr( $postarr['post_date'], 5, 2 );
$jj = substr( $postarr['post_date'], 8, 2 );
$aa = substr( $postarr['post_date'], 0, 4 );
$valid_date = wp_checkdate( $mm, $jj, $aa, $postarr['post_date'] );

if ( ! $valid_date ) {
if ( $wp_error ) {
return new WP_Error( 'invalid_date', __( 'Invalid date.' ) );
Expand All @@ -3494,55 +3493,55 @@ function wp_insert_post( $postarr, $wp_error = false ) {
}

if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' == $postarr['post_date_gmt'] ) {
if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) {
$post_date_gmt = get_gmt_from_date( $post_date );
if ( ! in_array( $postarr['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
$postarr['post_date_gmt'] = get_gmt_from_date( $postarr['post_date'] );
} else {
$post_date_gmt = '0000-00-00 00:00:00';
$postarr['post_date_gmt'] = '0000-00-00 00:00:00';
}
} else {
$post_date_gmt = $postarr['post_date_gmt'];
}

if ( $update || '0000-00-00 00:00:00' == $post_date ) {
$post_modified = current_time( 'mysql' );
$post_modified_gmt = current_time( 'mysql', 1 );
if ( $update || '0000-00-00 00:00:00' == $postarr['post_date'] ) {
$postarr['post_modified'] = current_time( 'mysql' );
$postarr['post_modified_gmt'] = current_time( 'mysql', 1 );
} else {
$post_modified = $post_date;
$post_modified_gmt = $post_date_gmt;
$postarr['post_modified'] = $postarr['post_date'];
$postarr['post_modified_gmt'] = $postarr['post_date_gmt'];
}

if ( 'attachment' !== $post_type ) {
if ( 'publish' == $post_status ) {
if ( 'attachment' !== $postarr['post_type'] ) {
if ( 'publish' == $postarr['post_status'] ) {
$now = gmdate( 'Y-m-d H:i:59' );
if ( mysql2date( 'U', $post_date_gmt, false ) > mysql2date( 'U', $now, false ) ) {
$post_status = 'future';
if ( mysql2date( 'U', $postarr['post_date_gmt'], false ) > mysql2date( 'U', $now, false ) ) {
$postarr['post_status'] = 'future';
}
} elseif ( 'future' == $post_status ) {
} elseif ( 'future' == $postarr['post_status'] ) {
$now = gmdate( 'Y-m-d H:i:59' );
if ( mysql2date( 'U', $post_date_gmt, false ) <= mysql2date( 'U', $now, false ) ) {
$post_status = 'publish';
if ( mysql2date( 'U', $postarr['post_date_gmt'], false ) <= mysql2date( 'U', $now, false ) ) {
$postarr['post_status'] = 'publish';
}
}
}

// Comment status.
if ( empty( $postarr['comment_status'] ) ) {
if ( $update ) {
$comment_status = 'closed';
$postarr['comment_status'] = 'closed';
} else {
$comment_status = get_default_comment_status( $post_type );
$postarr['comment_status'] = get_default_comment_status( $postarr['post_type'] );
}
} else {
$comment_status = $postarr['comment_status'];
}

// These variables are needed by compact() later.
$post_content_filtered = $postarr['post_content_filtered'];
$post_author = isset( $postarr['post_author'] ) ? $postarr['post_author'] : $user_id;
$ping_status = empty( $postarr['ping_status'] ) ? get_default_comment_status( $post_type, 'pingback' ) : $postarr['ping_status'];
$to_ping = isset( $postarr['to_ping'] ) ? sanitize_trackback_urls( $postarr['to_ping'] ) : '';
$pinged = isset( $postarr['pinged'] ) ? $postarr['pinged'] : '';
$import_id = isset( $postarr['import_id'] ) ? $postarr['import_id'] : 0;
if ( ! isset( $postarr['post_author'] ) ) {
$postarr['post_author'] = $user_id;
}

if ( empty( $postarr['ping_status'] ) ) {
$postarr['ping_status'] = get_default_comment_status( $postarr['post_type'], 'pingback' );
}

if ( ! empty( $postarr['to_ping'] ) ) {
$postarr['to_ping'] = sanitize_trackback_urls( $postarr['to_ping'] );
}

/*
* The 'wp_insert_post_parent' filter expects all variables to be present.
Expand All @@ -3555,14 +3554,14 @@ function wp_insert_post( $postarr, $wp_error = false ) {
}

$post_password = isset( $postarr['post_password'] ) ? $postarr['post_password'] : '';
if ( 'private' == $post_status ) {
if ( 'private' == $postarr['post_status'] ) {
$post_password = '';
}

if ( isset( $postarr['post_parent'] ) ) {
$post_parent = (int) $postarr['post_parent'];
$postarr['post_parent'] = (int) $postarr['post_parent'];
} else {
$post_parent = 0;
$postarr['post_parent'] = 0;
}

/**
Expand All @@ -3575,37 +3574,65 @@ function wp_insert_post( $postarr, $wp_error = false ) {
* @param array $new_postarr Array of parsed post data.
* @param array $postarr Array of sanitized, but otherwise unmodified post data.
*/
$post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, compact( array_keys( $postarr ) ), $postarr );
$postarr['post_parent'] = apply_filters( 'wp_insert_post_parent', $postarr['post_parent'], $post_ID, $postarr, $original_postarr );

/*
* If the post is being untrashed and it has a desired slug stored in post meta,
* reassign it.
*/
if ( 'trash' === $previous_status && 'trash' !== $post_status ) {
if ( 'trash' === $previous_status && 'trash' !== $postarr['post_status'] ) {
$desired_post_slug = get_post_meta( $post_ID, '_wp_desired_post_slug', true );
if ( $desired_post_slug ) {
delete_post_meta( $post_ID, '_wp_desired_post_slug' );
$post_name = $desired_post_slug;
$postarr['post_name'] = $desired_post_slug;
}
}

// If a trashed post has the desired slug, change it and let this post have it.
if ( 'trash' !== $post_status && $post_name ) {
wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID );
if ( 'trash' !== $postarr['post_status'] && $postarr['post_name'] ) {
wp_add_trashed_suffix_to_post_name_for_trashed_posts( $postarr['post_name'], $post_ID );
}

// When trashing an existing post, change its slug to allow non-trashed posts to use it.
if ( 'trash' === $post_status && 'trash' !== $previous_status && 'new' !== $previous_status ) {
$post_name = wp_add_trashed_suffix_to_post_name_for_post( $post_ID );
if ( 'trash' === $postarr['post_status'] && 'trash' !== $previous_status && 'new' !== $previous_status ) {
$postarr['post_name'] = wp_add_trashed_suffix_to_post_name_for_post( $post_ID );
}

$post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent );
$postarr['post_name'] = wp_unique_post_slug( $postarr['post_name'], $post_ID, $postarr['post_status'], $postarr['post_type'], $postarr['post_parent'] );

// Don't unslash.
$post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : '';

// Expected_slashed (everything!).
$data = compact( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'post_mime_type', 'guid' );
$data = array_intersect_key(
$postarr,
array_fill_keys(
array(
'post_author',
'post_date',
'post_date_gmt',
'post_content',
'post_content_filtered',
'post_title',
'post_excerpt',
'post_status',
'post_type',
'comment_status',
'ping_status',
'post_password',
'post_name',
'to_ping',
'pinged',
'post_modified',
'post_modified_gmt',
'post_parent',
'menu_order',
'post_mime_type',
'guid',
),
''
)
);

$emoji_fields = array( 'post_title', 'post_content', 'post_excerpt' );

Expand All @@ -3618,7 +3645,7 @@ function wp_insert_post( $postarr, $wp_error = false ) {
}
}

if ( 'attachment' === $post_type ) {
if ( 'attachment' === $postarr['post_type'] ) {
/**
* Filters attachment post data before it is updated in or added to the database.
*
Expand All @@ -3627,7 +3654,7 @@ function wp_insert_post( $postarr, $wp_error = false ) {
* @param array $data An array of sanitized attachment post data.
* @param array $postarr An array of unsanitized attachment post data.
*/
$data = apply_filters( 'wp_insert_attachment_data', $data, $postarr );
$data = apply_filters( 'wp_insert_attachment_data', $data, $original_postarr );
} else {
/**
* Filters slashed post data just before it is inserted into the database.
Expand All @@ -3637,7 +3664,7 @@ function wp_insert_post( $postarr, $wp_error = false ) {
* @param array $data An array of slashed post data.
* @param array $postarr An array of sanitized, but otherwise unmodified post data.
*/
$data = apply_filters( 'wp_insert_post_data', $data, $postarr );
$data = apply_filters( 'wp_insert_post_data', $data, $original_postarr );
}
$data = wp_unslash( $data );
$where = array( 'ID' => $post_ID );
Expand Down Expand Up @@ -3681,16 +3708,16 @@ function wp_insert_post( $postarr, $wp_error = false ) {
}

if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) {
$data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $post_ID ), $post_ID, $data['post_status'], $post_type, $post_parent );
$data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $post_ID ), $post_ID, $data['post_status'], $postarr['post_type'], $postarr['post_parent'] );
$wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where );
clean_post_cache( $post_ID );
}

if ( is_object_in_taxonomy( $post_type, 'category' ) ) {
wp_set_post_categories( $post_ID, $post_category );
if ( is_object_in_taxonomy( $postarr['post_type'], 'category' ) ) {
wp_set_post_categories( $post_ID, $postarr['post_category'] );
}

if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $post_type, 'post_tag' ) ) {
if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $postarr['post_type'], 'post_tag' ) ) {
wp_set_post_tags( $post_ID, $postarr['tags_input'] );
}

Expand Down Expand Up @@ -3739,8 +3766,8 @@ function wp_insert_post( $postarr, $wp_error = false ) {

// Set or remove featured image.
if ( isset( $postarr['_thumbnail_id'] ) ) {
$thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ) || 'revision' === $post_type;
if ( ! $thumbnail_support && 'attachment' === $post_type && $post_mime_type ) {
$thumbnail_support = current_theme_supports( 'post-thumbnails', $postarr['post_type'] ) && post_type_supports( $postarr['post_type'], 'thumbnail' ) || 'revision' === $postarr['post_type'];
if ( ! $thumbnail_support && 'attachment' === $postarr['post_type'] && $post_mime_type ) {
if ( wp_attachment_is( 'audio', $post_ID ) ) {
$thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' );
} elseif ( wp_attachment_is( 'video', $post_ID ) ) {
Expand Down