Skip to content

Commit

Permalink
Refactor the MediaUpload components to check upload permissions by ch…
Browse files Browse the repository at this point in the history
…ecking the upload handler existence
  • Loading branch information
youknowriad committed Mar 21, 2019
1 parent d0a11c9 commit da0bb10
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 53 deletions.
67 changes: 25 additions & 42 deletions packages/block-editor/src/components/media-placeholder/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { every, get, noop, startsWith, defaultTo } from 'lodash';
import { every, get, noop, startsWith } from 'lodash';
import classnames from 'classnames';

/**
Expand Down Expand Up @@ -134,7 +134,6 @@ export class MediaPlaceholder extends Component {
multiple = false,
notices,
allowedTypes = [],
hasUploadPermissions,
mediaUpload,
} = this.props;

Expand All @@ -146,7 +145,7 @@ export class MediaPlaceholder extends Component {
let instructions = labels.instructions || '';
let title = labels.title || '';

if ( ! hasUploadPermissions && ! onSelectURL ) {
if ( ! mediaUpload && ! onSelectURL ) {
instructions = __( 'To edit this block, you need permission to upload media.' );
}

Expand All @@ -156,27 +155,15 @@ export class MediaPlaceholder extends Component {
const isImage = isOneType && 'image' === allowedTypes[ 0 ];
const isVideo = isOneType && 'video' === allowedTypes[ 0 ];

if ( ! instructions ) {
if ( hasUploadPermissions ) {
instructions = __( 'Drag a media file, upload a new one or select a file from your library.' );
if ( ! instructions && mediaUpload ) {
instructions = __( 'Drag a media file, upload a new one or select a file from your library.' );

if ( isAudio ) {
instructions = __( 'Drag an audio, upload a new one or select a file from your library.' );
} else if ( isImage ) {
instructions = __( 'Drag an image, upload a new one or select a file from your library.' );
} else if ( isVideo ) {
instructions = __( 'Drag a video, upload a new one or select a file from your library.' );
}
} else if ( ! hasUploadPermissions && onSelectURL ) {
instructions = __( 'Given your current role, you can only link a media file, you cannot upload.' );

if ( isAudio ) {
instructions = __( 'Given your current role, you can only link an audio, you cannot upload.' );
} else if ( isImage ) {
instructions = __( 'Given your current role, you can only link an image, you cannot upload.' );
} else if ( isVideo ) {
instructions = __( 'Given your current role, you can only link a video, you cannot upload.' );
}
if ( isAudio ) {
instructions = __( 'Drag an audio, upload a new one or select a file from your library.' );
} else if ( isImage ) {
instructions = __( 'Drag an image, upload a new one or select a file from your library.' );
} else if ( isVideo ) {
instructions = __( 'Drag a video, upload a new one or select a file from your library.' );
}
}

Expand All @@ -202,23 +189,21 @@ export class MediaPlaceholder extends Component {
notices={ notices }
>
<MediaUploadCheck>
{ !! mediaUpload && (
<Fragment>
<DropZone
onFilesDrop={ this.onFilesUpload }
onHTMLDrop={ onHTMLDrop }
/>
<FormFileUpload
isLarge
className="editor-media-placeholder__button block-editor-media-placeholder__button"
onChange={ this.onUpload }
accept={ accept }
multiple={ multiple }
>
{ __( 'Upload' ) }
</FormFileUpload>
</Fragment>
) }
<Fragment>
<DropZone
onFilesDrop={ this.onFilesUpload }
onHTMLDrop={ onHTMLDrop }
/>
<FormFileUpload
isLarge
className="editor-media-placeholder__button block-editor-media-placeholder__button"
onChange={ this.onUpload }
accept={ accept }
multiple={ multiple }
>
{ __( 'Upload' ) }
</FormFileUpload>
</Fragment>
<MediaUpload
gallery={ multiple && this.onlyAllowsImages() }
multiple={ multiple }
Expand Down Expand Up @@ -262,11 +247,9 @@ export class MediaPlaceholder extends Component {
}

const applyWithSelect = withSelect( ( select ) => {
const { canUser } = select( 'core' );
const { getSettings } = select( 'core/block-editor' );

return {
hasUploadPermissions: defaultTo( canUser( 'create', 'media' ), true ),
mediaUpload: getSettings().__experimentalMediaUpload,
};
} );
Expand Down
9 changes: 2 additions & 7 deletions packages/block-editor/src/components/media-upload/check.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { defaultTo } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -13,9 +8,9 @@ export function MediaUploadCheck( { hasUploadPermissions, fallback = null, child
}

export default withSelect( ( select ) => {
const { canUser } = select( 'core' );
const { getSettings } = select( 'core/block-editor' );

return {
hasUploadPermissions: defaultTo( canUser( 'create', 'media' ), true ),
hasUploadPermissions: !! getSettings().__experimentalMediaUpload,
};
} )( MediaUploadCheck );
12 changes: 8 additions & 4 deletions packages/editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { map, pick } from 'lodash';
import { map, pick, defaultTo } from 'lodash';
import memize from 'memize';

/**
Expand Down Expand Up @@ -51,7 +51,7 @@ class EditorProvider extends Component {
}
}

getBlockEditorSettings( settings, meta, onMetaChange, reusableBlocks ) {
getBlockEditorSettings( settings, meta, onMetaChange, reusableBlocks, hasUploadPermissions ) {
return {
...pick( settings, [
'alignWide',
Expand All @@ -77,7 +77,7 @@ class EditorProvider extends Component {
onChange: onMetaChange,
},
__experimentalReusableBlocks: reusableBlocks,
__experimentalMediaUpload: mediaUpload,
__experimentalMediaUpload: hasUploadPermissions ? mediaUpload : undefined,
};
}

Expand Down Expand Up @@ -116,14 +116,15 @@ class EditorProvider extends Component {
onMetaChange,
reusableBlocks,
resetEditorBlocksWithoutUndoLevel,
hasUploadPermissions,
} = this.props;

if ( ! isReady ) {
return null;
}

const editorSettings = this.getBlockEditorSettings(
settings, meta, onMetaChange, reusableBlocks
settings, meta, onMetaChange, reusableBlocks, hasUploadPermissions
);

return (
Expand All @@ -147,11 +148,14 @@ export default compose( [
getEditedPostAttribute,
__experimentalGetReusableBlocks,
} = select( 'core/editor' );
const { canUser } = select( 'core' );

return {
isReady: isEditorReady(),
blocks: getEditorBlocks(),
meta: getEditedPostAttribute( 'meta' ),
reusableBlocks: __experimentalGetReusableBlocks(),
hasUploadPermissions: defaultTo( canUser( 'create', 'media' ), true ),
};
} ),
withDispatch( ( dispatch ) => {
Expand Down

0 comments on commit da0bb10

Please sign in to comment.