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 26, 2019
1 parent 59e56dc commit c28f11f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 37 deletions.
37 changes: 11 additions & 26 deletions packages/block-editor/src/components/media-placeholder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import {
defaultTo,
every,
get,
isArray,
Expand Down Expand Up @@ -152,18 +151,18 @@ export class MediaPlaceholder extends Component {
const {
allowedTypes = [],
className,
hasUploadPermissions,
icon,
isAppender,
labels = {},
notices,
onSelectURL,
mediaUpload,
} = this.props;

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 @@ -173,27 +172,15 @@ export class MediaPlaceholder extends Component {
const isImage = isOneType && 'image' === allowedTypes[ 0 ];
const isVideo = isOneType && 'video' === allowedTypes[ 0 ];

if ( instructions === undefined ) {
if ( hasUploadPermissions ) {
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 ( instructions === undefined && 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.' );
}
}

Expand Down Expand Up @@ -400,11 +387,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 @@ -71,7 +71,7 @@ class EditorProvider extends Component {
}
}

getBlockEditorSettings( settings, meta, onMetaChange, reusableBlocks ) {
getBlockEditorSettings( settings, meta, onMetaChange, reusableBlocks, hasUploadPermissions ) {
return {
...pick( settings, [
'alignWide',
Expand All @@ -97,7 +97,7 @@ class EditorProvider extends Component {
onChange: onMetaChange,
},
__experimentalReusableBlocks: reusableBlocks,
__experimentalMediaUpload: mediaUpload,
__experimentalMediaUpload: hasUploadPermissions ? mediaUpload : undefined,
__experimentalFetchLinkSuggestions: fetchLinkSuggestions,
};
}
Expand Down Expand Up @@ -137,14 +137,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 @@ -169,11 +170,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 c28f11f

Please sign in to comment.