Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions src/wp-includes/meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ function add_metadata( $meta_type, $object_id, $meta_key, $meta_value, $unique =
* - `add_user_meta`
*
* @since 3.1.0
* @since 7.2.0 The `$unique` parameter was added.
*
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $_meta_value Metadata value.
* @param bool $unique Whether the specified meta key should be unique for the object.
*/
do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value );
do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value, $unique );

$result = $wpdb->insert(
$table,
Expand Down Expand Up @@ -159,13 +161,15 @@ function add_metadata( $meta_type, $object_id, $meta_key, $meta_value, $unique =
* - `added_user_meta`
*
* @since 2.9.0
* @since 7.2.0 The `$unique` parameter was added.
*
* @param int $mid The meta ID after successful update.
* @param int $mid The meta ID after successful addition.
* @param int $object_id ID of the object metadata is for.
* @param string $meta_key Metadata key.
* @param mixed $_meta_value Metadata value.
* @param bool $unique Whether the specified meta key should be unique for the object.
*/
do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value );
do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value, $unique );

return $mid;
}
Expand Down
43 changes: 43 additions & 0 deletions tests/phpunit/tests/meta/addMetadata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* @group meta
* @covers ::add_metadata
*/
class Tests_Meta_AddMetadata extends WP_UnitTestCase {

/**
* @ticket 39706
*
* @dataProvider data_actions_should_receive_unique
*
* @param bool $unique Whether the specified meta key should be unique for the object.
*/
public function test_actions_should_receive_unique( $unique ) {
$add_action = new MockAction();
$added_action = new MockAction();

add_action( 'add_post_meta', array( $add_action, 'action' ), 10, 4 );
add_action( 'added_post_meta', array( $added_action, 'action' ), 10, 5 );

add_metadata( 'post', 123, 'test_key', 'value', $unique );

$add_args = $add_action->get_args();
$added_args = $added_action->get_args();

$this->assertSame( $unique, $add_args[0][3], 'The add_post_meta action should receive the value of $unique.' );
$this->assertSame( $unique, $added_args[0][4], 'The added_post_meta action should receive the value of $unique.' );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_actions_should_receive_unique() {
return array(
'unique meta' => array( true ),
'non-unique meta' => array( false ),
);
}
}
Loading