WordPress.org

Make WordPress Core

Opened 3 years ago

Closed 20 months ago

Last modified 20 months ago

#43561 closed defect (bug) (fixed)

$object_id parameter is string instead of integer in delete_{$meta_type}_meta inside delete_metadata_by_mid()

Reported by: salcode Owned by: johnbillion
Milestone: 5.2 Priority: normal
Severity: normal Version:
Component: Options, Meta APIs Keywords: has-patch
Focuses: Cc:

Description

In wp-includes/meta.php inside the function delete_metadata_by_mid() the hook do_action( "delete_{$meta_type}_meta" ) is called.

While not documented here, earlier in the same same file this hook also appears and is documented as

 * @param null|bool $delete     Whether to allow metadata deletion of the given type.
 * @param int       $object_id  Object ID.
 * @param string    $meta_key   Meta key.
 * @param mixed     $meta_value Meta value. Must be serializable if non-scalar.
 * @param bool      $delete_all Whether to delete the matching metadata entries
 *                              for all objects, ignoring the specified $object_id.
 *                              Default false.

however when the hook is called inside delete_metadata_by_mid() the $object_id is passed as a string not an int.

It looks like this happens inside delete_metadata_by_mid() because $object_id is defined in these lines

if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
	$object_id = $meta->{$column};

Attachments (1)

43561.diff (1.5 KB) - added by salcode 3 years ago.
Patch with test.

Download all attachments as: .zip

Change History (8)

#1 @salcode
3 years ago

I've made this branch on GitHub we're I'm looking into this, sf/object-id-string-not-int-43561

I've added a failing test in b848118 to illustrate the problem.

Here is the result of the test

1) Tests_Meta_DeleteMetadata::test_object_id_is_int_inside_delete_post_meta
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'integer'
+'string'

And I've added a patch to cast $object_id to an int in d70e520eea

@salcode
3 years ago

Patch with test.

#2 @SergeyBiryukov
3 years ago

  • Component changed from General to Options, Meta APIs
  • Keywords has-patch added
  • Milestone changed from Awaiting Review to 5.0

#3 @pento
2 years ago

  • Milestone changed from 5.0 to 5.1

#4 @pento
23 months ago

  • Milestone changed from 5.1 to 5.2
  • Version trunk deleted

This patch needs further investigation and a decision. We've had weird side effect issues in the past when changing the type of a parameter.

#5 @johnbillion
20 months ago

  • Owner set to johnbillion
  • Status changed from new to accepted

This change makes sense.

The delete_{$meta_type}_meta action documents its $object_id parameter as an integer. Inside delete_metadata() it gets cast to an integer, but inside delete_metadata_by_mid() it doesn't, and remains a string. The same goes for the subsequent deleted_{$meta_type}_meta action.

The only place the $object_id variable gets used in delete_metadata_by_mid() is in the call to wp_cache_delete(), in which case it's a benefit to ensure that the object key is also an integer so its type matches the key used in delete_metadata().

This means that casting $object_id to an integer inside delete_metadata_by_mid() is safe and correct.

#6 @johnbillion
20 months ago

  • Resolution set to fixed
  • Status changed from accepted to closed

In 45064:

Options, Meta APIs: Ensure the $object_id parameter passed to the delete_{$meta_type}_meta and deleted_{$meta_type}_meta filters is always an integer.

Props salcode

Fixes #43561

#7 @johnbillion
20 months ago

In 45065:

Options, Meta APIs: Remove an accidental short array syntax in the tests.

See #43561

Note: See TracTickets for help on using tickets.