import { Component } from '@wordpress/element'; import { addFilter } from '@wordpress/hooks'; import deprecated from '@wordpress/deprecated'; import { useSelect } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; import { MediaUpload, privateApis as mediaUtilsPrivateApis, } from '@wordpress/media-utils'; import { unlock } from '../lock-unlock'; import { store as editorStore } from '../store'; const { MediaUploadModal: MediaUploadModalComponent } = unlock( mediaUtilsPrivateApis ); const EMPTY_FIELDS = []; /** * Supplies the modal with the post it was opened from, so that its "Attached to" * filter can offer media uploaded to that post, labelled with the post type's * own wording. `media-utils` has no editor dependency, so the post is injected * here. * * The post is only passed on when media can actually belong to it. Templates and * the like have no front end of their own, so an image can't be uploaded to one — * the same reasoning, and the same `viewable` check, as attaching media on save. * Their entity ID is a slug rather than a number besides, which the media query * has no use for. * * It also supplies the fields registered for attachments on the server, so * that a plugin's fields show in the modal as columns and filters. They are * requested the first time the modal opens rather than wherever a media * placeholder renders. * * @param {Object} props Props passed through to the modal. */ function MediaUploadModalWithPostContext( props ) { const { postId, postType } = useSelect( ( select ) => { const { getCurrentPostId, getCurrentPostType } = select( editorStore ); const currentPostId = getCurrentPostId(); const currentPostType = getCurrentPostType(); const postTypeObject = currentPostType ? select( coreStore ).getPostType( currentPostType ) : undefined; if ( typeof currentPostId !== 'number' || ! postTypeObject?.viewable ) { return {}; } return { postId: currentPostId, postType: currentPostType }; }, [] ); const fields = useSelect( ( select ) => props.isOpen ? unlock( select( editorStore ) ).getEntityFields( 'postType', 'attachment' ) : EMPTY_FIELDS, [ props.isOpen ] ); return ( ); } /** * Class component wrapper for MediaUploadModal to maintain compatibility * with the stable MediaUpload component API (render prop pattern). */ class MediaUploadModalWrapper extends Component { constructor( props ) { super( props ); this.state = { isOpen: false, }; this.openModal = this.openModal.bind( this ); this.closeModal = this.closeModal.bind( this ); } openModal() { this.setState( { isOpen: true } ); } closeModal() { this.setState( { isOpen: false } ); this.props.onClose?.(); } render() { const { allowedTypes, multiple, value, onSelect, title, modalClass, render, } = this.props; const { isOpen } = this.state; return ( <> { render( { open: this.openModal } ) } { onSelect( media ); this.closeModal(); } } onClose={ this.closeModal } title={ title } isOpen={ isOpen } modalClass={ modalClass } /> ); } } if ( window.__experimentalDataViewsMediaModal ) { // Use the wrapper component for editor.MediaUpload when the experimental flag is enabled addFilter( 'editor.MediaUpload', 'core/editor/components/media-upload', () => { deprecated( 'Extending MediaUpload as a class component', { since: '7.0', version: '7.2', hint: 'MediaUpload will become a function component in WordPress 7.2 Please update any custom implementations to use function components instead.', } ); return MediaUploadModalWrapper; } ); } else { addFilter( 'editor.MediaUpload', 'core/editor/components/media-upload', () => { return MediaUpload; } ); }