Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chrome: Hide several Sidebar Panels if the CPT doesn't support it #4161

Merged
merged 5 commits into from
Dec 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions editor/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export { default as PostComments } from './post-comments';
export { default as PostExcerpt } from './post-excerpt';
export { default as PostExcerptCheck } from './post-excerpt/check';
export { default as PostFeaturedImage } from './post-featured-image';
export { default as PostFeaturedImageCheck } from './post-featured-image/check';
export { default as PostFormat } from './post-format';
export { default as PostFormatCheck } from './post-format/check';
export { default as PostLastRevision } from './post-last-revision';
Expand All @@ -38,6 +39,7 @@ export { default as PostTextEditorToolbar } from './post-text-editor/toolbar';
export { default as PostTitle } from './post-title';
export { default as PostTrash } from './post-trash';
export { default as PostTrashCheck } from './post-trash/check';
export { default as PostTypeSupportCheck } from './post-type-support-check';
export { default as PostVisibility } from './post-visibility';
export { default as PostVisibilityLabel } from './post-visibility/label';
export { default as TableOfContents } from './table-of-contents';
Expand Down
3 changes: 2 additions & 1 deletion editor/components/post-author/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { compose } from '@wordpress/element';
/**
* Internal dependencies
*/
import PostTypeSupportCheck from '../post-type-support-check';
import { getCurrentPostType } from '../../store/selectors';

export function PostAuthorCheck( { user, users, children } ) {
Expand All @@ -23,7 +24,7 @@ export function PostAuthorCheck( { user, users, children } ) {
return null;
}

return children;
return <PostTypeSupportCheck supportKeys="author">{ children }</PostTypeSupportCheck>;
}

const applyConnect = connect(
Expand Down
2 changes: 1 addition & 1 deletion editor/components/post-excerpt/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import PostTypeSupportCheck from '../post-type-support-check';

function PostExcerptCheck( props ) {
return <PostTypeSupportCheck { ...props } supportKey="excerpt" />;
return <PostTypeSupportCheck { ...props } supportKeys="excerpt" />;
}

export default PostExcerptCheck;
10 changes: 10 additions & 0 deletions editor/components/post-featured-image/check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Internal dependencies
*/
import PostTypeSupportCheck from '../post-type-support-check';

function PostFeaturedImageCheck( props ) {
return <PostTypeSupportCheck { ...props } supportKeys="thumbnail" />;
}

export default PostFeaturedImageCheck;
84 changes: 44 additions & 40 deletions editor/components/post-featured-image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { compose } from '@wordpress/element';
* Internal dependencies
*/
import './style.scss';
import PostFeaturedImageCheck from './check';
import { getCurrentPostType, getEditedPostAttribute } from '../../store/selectors';
import { editPost } from '../../store/actions';

Expand All @@ -25,47 +26,50 @@ const DEFAULT_REMOVE_FEATURE_IMAGE_LABEL = __( 'Remove featured image' );

function PostFeaturedImage( { featuredImageId, onUpdateImage, onRemoveImage, media, postType } ) {
const postLabel = get( postType, 'data.labels', {} );

return (
<div className="editor-post-featured-image">
{ !! featuredImageId &&
<MediaUploadButton
title={ postLabel.set_featured_image }
buttonProps={ { className: 'button-link editor-post-featured-image__preview' } }
onSelect={ onUpdateImage }
type="image"
>
{ media && !! media.data &&
<ResponsiveWrapper
naturalWidth={ media.data.media_details.width }
naturalHeight={ media.data.media_details.height }
>
<img src={ media.data.source_url } alt={ __( 'Featured image' ) } />
</ResponsiveWrapper>
}
{ media && media.isLoading && <Spinner /> }
</MediaUploadButton>
}
{ !! featuredImageId && media && ! media.isLoading &&
<p className="editor-post-featured-image__howto">
{ __( 'Click the image to edit or update' ) }
</p>
}
{ ! featuredImageId &&
<MediaUploadButton
title={ postLabel.set_featured_image || DEFAULT_SET_FEATURE_IMAGE_LABEL }
buttonProps={ { className: 'editor-post-featured-image__toggle button-link' } }
onSelect={ onUpdateImage }
type="image"
>
{ postLabel.set_featured_image || DEFAULT_SET_FEATURE_IMAGE_LABEL }
</MediaUploadButton>
}
{ !! featuredImageId &&
<Button className="editor-post-featured-image__toggle button-link" onClick={ onRemoveImage }>
{ postLabel.remove_featured_image || DEFAULT_REMOVE_FEATURE_IMAGE_LABEL }
</Button>
}
</div>
<PostFeaturedImageCheck>
<div className="editor-post-featured-image">
{ !! featuredImageId &&
<MediaUploadButton
title={ postLabel.set_featured_image }
buttonProps={ { className: 'button-link editor-post-featured-image__preview' } }
onSelect={ onUpdateImage }
type="image"
>
{ media && !! media.data &&
<ResponsiveWrapper
naturalWidth={ media.data.media_details.width }
naturalHeight={ media.data.media_details.height }
>
<img src={ media.data.source_url } alt={ __( 'Featured image' ) } />
</ResponsiveWrapper>
}
{ media && media.isLoading && <Spinner /> }
</MediaUploadButton>
}
{ !! featuredImageId && media && ! media.isLoading &&
<p className="editor-post-featured-image__howto">
{ __( 'Click the image to edit or update' ) }
</p>
}
{ ! featuredImageId &&
<MediaUploadButton
title={ postLabel.set_featured_image || DEFAULT_SET_FEATURE_IMAGE_LABEL }
buttonProps={ { className: 'editor-post-featured-image__toggle button-link' } }
onSelect={ onUpdateImage }
type="image"
>
{ postLabel.set_featured_image || DEFAULT_SET_FEATURE_IMAGE_LABEL }
</MediaUploadButton>
}
{ !! featuredImageId &&
<Button className="editor-post-featured-image__toggle button-link" onClick={ onRemoveImage }>
{ postLabel.remove_featured_image || DEFAULT_REMOVE_FEATURE_IMAGE_LABEL }
</Button>
}
</div>
</PostFeaturedImageCheck>
);
}

Expand Down
39 changes: 4 additions & 35 deletions editor/components/post-format/check.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,10 @@
/**
* External dependencies
*/
import { connect } from 'react-redux';
import { get } from 'lodash';

/**
* WordPress dependencies
*/
import { withAPIData } from '@wordpress/components';
import { compose } from '@wordpress/element';

/**
* Internal dependencies
*/
import { getCurrentPostType } from '../../store/selectors';
import PostTypeSupportCheck from '../post-type-support-check';

function PostFormatCheck( { postType, children } ) {
if ( ! get( postType.data, [ 'supports', 'post-formats' ] ) ) {
return null;
}

return children;
function PostFormatCheck( props ) {
return <PostTypeSupportCheck { ...props } supportKeys="post-formats" />;
}

export default compose( [
connect(
( state ) => {
return {
postTypeSlug: getCurrentPostType( state ),
};
},
),
withAPIData( ( props ) => {
const { postTypeSlug } = props;

return {
postType: `/wp/v2/types/${ postTypeSlug }?context=edit`,
};
} ),
] )( PostFormatCheck );
export default PostFormatCheck;
3 changes: 2 additions & 1 deletion editor/components/post-last-revision/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import { connect } from 'react-redux';
* Internal dependencies
*/
import { getCurrentPostLastRevisionId, getCurrentPostRevisionsCount } from '../../store/selectors';
import PostTypeSupportCheck from '../post-type-support-check';

export function PostLastRevisionCheck( { lastRevisionId, revisionsCount, children } ) {
if ( ! lastRevisionId || revisionsCount < 2 ) {
return null;
}

return children;
return <PostTypeSupportCheck supportKeys="revisions" >{ children }</PostTypeSupportCheck>;
}

export default connect(
Expand Down
2 changes: 1 addition & 1 deletion editor/components/post-last-revision/test/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ describe( 'PostLastRevisionCheck', () => {
</PostLastRevisionCheck>
);

expect( wrapper.text() ).toEqual( 'Children' );
expect( wrapper.text() ).not.toBe( null );
} );
} );
8 changes: 5 additions & 3 deletions editor/components/post-type-support-check/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { connect } from 'react-redux';
import { get } from 'lodash';
import { get, some, castArray } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -15,8 +15,10 @@ import { compose } from '@wordpress/element';
*/
import { getCurrentPostType } from '../../store/selectors';

function PostTypeSupportCheck( { postType, children, supportKey } ) {
const isSupported = get( postType, [ 'data', 'supports', supportKey ], false );
function PostTypeSupportCheck( { postType, children, supportKeys } ) {
const isSupported = some(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be every? I could imagine a need for both cases, i.e. a component that requires post type support for both X and Y, but alternatively X or Y.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hesitated on adding an "operator". For now we only have the "or" use-case.

castArray( supportKeys ), key => get( postType, [ 'data', 'supports', key ], false )
);

if ( ! isSupported ) {
return null;
Expand Down
25 changes: 16 additions & 9 deletions editor/edit-post/sidebar/discussion-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { PanelBody, PanelRow } from '@wordpress/components';
/**
* Internal Dependencies
*/
import { PostComments, PostPingbacks } from '../../../components';
import { PostComments, PostPingbacks, PostTypeSupportCheck } from '../../../components';
import { isEditorSidebarPanelOpened } from '../../../store/selectors';
import { toggleSidebarPanel } from '../../../store/actions';

Expand All @@ -23,14 +23,21 @@ const PANEL_NAME = 'discussion-panel';

function DiscussionPanel( { isOpened, onTogglePanel } ) {
return (
<PanelBody title={ __( 'Discussion' ) } opened={ isOpened } onToggle={ onTogglePanel }>
<PanelRow>
<PostComments />
</PanelRow>
<PanelRow>
<PostPingbacks />
</PanelRow>
</PanelBody>
<PostTypeSupportCheck supportKeys={ [ 'comments', 'trackbacks' ] }>
<PanelBody title={ __( 'Discussion' ) } opened={ isOpened } onToggle={ onTogglePanel }>
<PostTypeSupportCheck supportKeys="comments">
<PanelRow>
<PostComments />
</PanelRow>
</PostTypeSupportCheck>

<PostTypeSupportCheck supportKeys="trackbacks">
<PanelRow>
<PostPingbacks />
</PanelRow>
</PostTypeSupportCheck>
</PanelBody>
</PostTypeSupportCheck>
);
}

Expand Down
10 changes: 6 additions & 4 deletions editor/edit-post/sidebar/featured-image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { PanelBody } from '@wordpress/components';
/**
* Internal dependencies
*/
import { PostFeaturedImage } from '../../../components';
import { PostFeaturedImage, PostFeaturedImageCheck } from '../../../components';
import { isEditorSidebarPanelOpened } from '../../../store/selectors';
import { toggleSidebarPanel } from '../../../store/actions';

Expand All @@ -23,9 +23,11 @@ const PANEL_NAME = 'featured-image';

function FeaturedImage( { isOpened, onTogglePanel } ) {
return (
<PanelBody title={ __( 'Featured Image' ) } opened={ isOpened } onToggle={ onTogglePanel }>
<PostFeaturedImage />
</PanelBody>
<PostFeaturedImageCheck>
<PanelBody title={ __( 'Featured Image' ) } opened={ isOpened } onToggle={ onTogglePanel }>
<PostFeaturedImage />
</PanelBody>
</PostFeaturedImageCheck>
);
}

Expand Down