Make WordPress Core

Opened 3 months ago

Last modified 3 months ago

#64215 new defect (bug)

slimImageObject drops meta fields added to attachments

Reported by: leemon's profile leemon Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Media Keywords: needs-patch
Focuses: Cc:

Description

I've opened an issue in the Gutenberg repo, but since the solution likely involves modifying core code, I'm creating a ticket here as well.

### Description

The object passed to the onSelect callback in the Gutenberg MediaUpload component calls the following function that removes all image attributes except those in the attrSet constant:

const slimImageObject = ( img ) => {
	const attrSet = [
		'sizes',
		'mime',
		'type',
		'subtype',
		'id',
		'url',
		'alt',
		'link',
		'caption',
	];
	return attrSet.reduce( ( result, key ) => {
		if ( img?.hasOwnProperty( key ) ) {
			result[ key ] = img[ key ];
		}
		return result;
	}, {} );
};

That means that meta fields added to attachments via register_post_meta and the attachment_fields_to_edit and attachment_fields_to_save filters are dropped. Is there a way to avoid this?

### Step-by-step reproduction instructions

  1. Register a meta field to the attachment post type:
register_post_meta(
	'attachment',
	'_oembed_url',
	array(
		'show_in_rest' => true,
		'type'              => 'string',
		'single'            => true,
		'sanitize_callback' => 'sanitize_url',
		'auth_callback'     => function () {
			return current_user_can( 'edit_posts' );
		},
	)
);
  1. Add a MediaUpload component to a block:
<MediaUpload
    allowedTypes={['image']}
    multiple={true}
    gallery={true}
    value={images.map((image) => image.id)}
    onSelect={setImages}
    render={({ open }) => (
        <ToolbarButton onClick={open}>
            {__('Edit images')}
        </ToolbarButton>)}
    />
  1. Check that the value param doesn't contain any meta fields:
const setImages = (value) => {
    console.log(value);
}

Change History (1)

Note: See TracTickets for help on using tickets.