Skip to content

Commit

Permalink
Use the MediaUploadCheck component before each Upload component (#11924)
Browse files Browse the repository at this point in the history
* Use the MediaUploadCheck component before each Upload component

This PR should fix #11910

1. It adds a MediaUploadCheck component before the forgotten MediaUpload components used directly in some blocks :
- within the edit toolbar buttons of the cover, file and image blocks,
- within the Poster image setting of the video block.
2. It also adds a MediaUploadCheck component before rendering the Block Drop Zone.

* Edit the MediaUpload documentation about Upload Permissions

Include some informations about how to make sure the current user has upload permissions (by wrapping the MediaUpload component into the MediaUploadCheck one)
  • Loading branch information
imath authored and gziolo committed Nov 17, 2018
1 parent 1b6cf07 commit 88b5033
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 86 deletions.
33 changes: 18 additions & 15 deletions packages/block-library/src/cover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
BlockAlignmentToolbar,
MediaPlaceholder,
MediaUpload,
MediaUploadCheck,
AlignmentToolbar,
PanelColorSettings,
RichText,
Expand Down Expand Up @@ -259,21 +260,23 @@ export const settings = {
setAttributes( { contentAlign: nextAlign } );
} }
/>
<Toolbar>
<MediaUpload
onSelect={ onSelectMedia }
allowedTypes={ ALLOWED_MEDIA_TYPES }
value={ id }
render={ ( { open } ) => (
<IconButton
className="components-toolbar__control"
label={ __( 'Edit media' ) }
icon="edit"
onClick={ open }
/>
) }
/>
</Toolbar>
<MediaUploadCheck>
<Toolbar>
<MediaUpload
onSelect={ onSelectMedia }
allowedTypes={ ALLOWED_MEDIA_TYPES }
value={ id }
render={ ( { open } ) => (
<IconButton
className="components-toolbar__control"
label={ __( 'Edit media' ) }
icon="edit"
onClick={ open }
/>
) }
/>
</Toolbar>
</MediaUploadCheck>
</Fragment>
) }
</BlockControls>
Expand Down
31 changes: 17 additions & 14 deletions packages/block-library/src/file/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { Component, Fragment } from '@wordpress/element';
import {
MediaUpload,
MediaPlaceholder,
MediaUploadCheck,
BlockControls,
RichText,
mediaUpload,
Expand Down Expand Up @@ -165,20 +166,22 @@ class FileEdit extends Component {
} }
/>
<BlockControls>
<Toolbar>
<MediaUpload
onSelect={ this.onSelectFile }
value={ id }
render={ ( { open } ) => (
<IconButton
className="components-toolbar__control"
label={ __( 'Edit file' ) }
onClick={ open }
icon="edit"
/>
) }
/>
</Toolbar>
<MediaUploadCheck>
<Toolbar>
<MediaUpload
onSelect={ this.onSelectFile }
value={ id }
render={ ( { open } ) => (
<IconButton
className="components-toolbar__control"
label={ __( 'Edit file' ) }
onClick={ open }
icon="edit"
/>
) }
/>
</Toolbar>
</MediaUploadCheck>
</BlockControls>
<div className={ classes }>
<div className={ `${ className }__content-wrapper` }>
Expand Down
33 changes: 18 additions & 15 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
InspectorControls,
MediaPlaceholder,
MediaUpload,
MediaUploadCheck,
BlockAlignmentToolbar,
mediaUpload,
} from '@wordpress/editor';
Expand Down Expand Up @@ -328,21 +329,23 @@ class ImageEdit extends Component {
);
} else {
toolbarEditButton = (
<Toolbar>
<MediaUpload
onSelect={ this.onSelectImage }
allowedTypes={ ALLOWED_MEDIA_TYPES }
value={ id }
render={ ( { open } ) => (
<IconButton
className="components-toolbar__control"
label={ __( 'Edit image' ) }
icon="edit"
onClick={ open }
/>
) }
/>
</Toolbar>
<MediaUploadCheck>
<Toolbar>
<MediaUpload
onSelect={ this.onSelectImage }
allowedTypes={ ALLOWED_MEDIA_TYPES }
value={ id }
render={ ( { open } ) => (
<IconButton
className="components-toolbar__control"
label={ __( 'Edit image' ) }
icon="edit"
onClick={ open }
/>
) }
/>
</Toolbar>
</MediaUploadCheck>
);
}
}
Expand Down
49 changes: 26 additions & 23 deletions packages/block-library/src/video/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
InspectorControls,
MediaPlaceholder,
MediaUpload,
MediaUploadCheck,
RichText,
mediaUpload,
} from '@wordpress/editor';
Expand Down Expand Up @@ -207,30 +208,32 @@ class VideoEdit extends Component {
{ value: 'none', label: __( 'None' ) },
] }
/>
<BaseControl
className="editor-video-poster-control"
label={ __( 'Poster Image' ) }
>
<MediaUpload
title={ __( 'Select Poster Image' ) }
onSelect={ this.onSelectPoster }
allowedTypes={ VIDEO_POSTER_ALLOWED_MEDIA_TYPES }
render={ ( { open } ) => (
<Button
isDefault
onClick={ open }
ref={ this.posterImageButton }
>
{ ! this.props.attributes.poster ? __( 'Select Poster Image' ) : __( 'Replace image' ) }
<MediaUploadCheck>
<BaseControl
className="editor-video-poster-control"
label={ __( 'Poster Image' ) }
>
<MediaUpload
title={ __( 'Select Poster Image' ) }
onSelect={ this.onSelectPoster }
allowedTypes={ VIDEO_POSTER_ALLOWED_MEDIA_TYPES }
render={ ( { open } ) => (
<Button
isDefault
onClick={ open }
ref={ this.posterImageButton }
>
{ ! this.props.attributes.poster ? __( 'Select Poster Image' ) : __( 'Replace image' ) }
</Button>
) }
/>
{ !! this.props.attributes.poster &&
<Button onClick={ this.onRemovePoster } isLink isDestructive>
{ __( 'Remove Poster Image' ) }
</Button>
) }
/>
{ !! this.props.attributes.poster &&
<Button onClick={ this.onRemovePoster } isLink isDestructive>
{ __( 'Remove Poster Image' ) }
</Button>
}
</BaseControl>
}
</BaseControl>
</MediaUploadCheck>
</PanelBody>
</InspectorControls>
<figure className={ className }>
Expand Down
23 changes: 15 additions & 8 deletions packages/editor/src/components/block-drop-zone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import { Component } from '@wordpress/element';
import { withDispatch, withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';

/**
* Internal dependencies
*/
import MediaUploadCheck from '../media-upload/check';

const parseDropEvent = ( event ) => {
let result = {
srcRootClientId: null,
Expand Down Expand Up @@ -111,14 +116,16 @@ class BlockDropZone extends Component {
const isAppender = index === undefined;

return (
<DropZone
className={ classnames( 'editor-block-drop-zone', {
'is-appender': isAppender,
} ) }
onFilesDrop={ this.onFilesDrop }
onHTMLDrop={ this.onHTMLDrop }
onDrop={ this.onDrop }
/>
<MediaUploadCheck>
<DropZone
className={ classnames( 'editor-block-drop-zone', {
'is-appender': isAppender,
} ) }
onFilesDrop={ this.onFilesDrop }
onHTMLDrop={ this.onHTMLDrop }
onDrop={ this.onDrop }
/>
</MediaUploadCheck>
);
}
}
Expand Down
25 changes: 14 additions & 11 deletions packages/editor/src/components/media-upload/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,28 @@ You can check how this component is implemented for the edit post page using `wp

## Usage

To make sure the current user has Upload permissions, you need to wrap the MediaUpload component into the MediaUploadCheck one.

```jsx
import { Button } from '@wordpress/components';
import { MediaUpload } from '@wordpress/editor';
import { MediaUpload, MediaUploadCheck } from '@wordpress/editor';

const ALLOWED_MEDIA_TYPES = [ 'audio' ];

function MyMediaUploader() {
return (
<MediaUpload
onSelect={ ( media ) => console.log( 'selected ' + media.length ) }
allowedTypes={ ALLOWED_MEDIA_TYPES }
value={ mediaId }
render={ ( { open } ) => (
<Button onClick={ open }>
Open Media Library
</Button>
) }
/>
<MediaUploadCheck>
<MediaUpload
onSelect={ ( media ) => console.log( 'selected ' + media.length ) }
allowedTypes={ ALLOWED_MEDIA_TYPES }
value={ mediaId }
render={ ( { open } ) => (
<Button onClick={ open }>
Open Media Library
</Button>
) }
/>
</MediaUploadCheck>
);
}
```
Expand Down

0 comments on commit 88b5033

Please sign in to comment.