Allow metadata to be attached to comment at time of creation.

The new `$comment_meta` parameter of `wp_insert_comment()` allows an array of
key/value pairs to be passed when creating a comment. These pairs are then
stored as commentmeta when the comment has been created.

Props tellyworth, wonderboymusic.
Fixes #12431.
Built from https://develop.svn.wordpress.org/trunk@34533


git-svn-id: http://core.svn.wordpress.org/trunk@34497 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Boone Gorges 2015-09-25 04:41:25 +00:00
parent 7c0ff60cb8
commit 2370a81286
2 changed files with 11 additions and 1 deletions

View File

@ -1377,6 +1377,7 @@ function wp_get_current_commenter() {
* Inserts a comment into the database. * Inserts a comment into the database.
* *
* @since 2.0.0 * @since 2.0.0
* @since 4.4.0 Introduced `$comment_meta` argument.
* *
* @global wpdb $wpdb WordPress database abstraction object. * @global wpdb $wpdb WordPress database abstraction object.
* *
@ -1401,6 +1402,8 @@ function wp_get_current_commenter() {
* @type int $comment_post_ID ID of the post that relates to the comment, if any. * @type int $comment_post_ID ID of the post that relates to the comment, if any.
* Default empty. * Default empty.
* @type string $comment_type Comment type. Default empty. * @type string $comment_type Comment type. Default empty.
* @type array $comment_meta Optional. Array of key/value pairs to be stored in commentmeta for the
* new comment.
* @type int $user_id ID of the user who submitted the comment. Default 0. * @type int $user_id ID of the user who submitted the comment. Default 0.
* } * }
* @return int|false The new comment's ID on success, false on failure. * @return int|false The new comment's ID on success, false on failure.
@ -1439,6 +1442,13 @@ function wp_insert_comment( $commentdata ) {
} }
$comment = get_comment( $id ); $comment = get_comment( $id );
// If metadata is provided, store it.
if ( isset( $commentdata['comment_meta'] ) && is_array( $commentdata['comment_meta'] ) ) {
foreach ( $commentdata['comment_meta'] as $meta_key => $meta_value ) {
add_comment_meta( $comment->comment_ID, $meta_key, $meta_value, true );
}
}
/** /**
* Fires immediately after a comment is inserted into the database. * Fires immediately after a comment is inserted into the database.
* *

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.4-alpha-34532'; $wp_version = '4.4-alpha-34533';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.