-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Only display featured image UI when theme supports it too #6510
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,37 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { get, some, castArray } from 'lodash'; | ||
import { get, includes, some, castArray } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { withSelect } from '@wordpress/data'; | ||
|
||
function PostTypeSupportCheck( { postType, children, supportKeys } ) { | ||
export function PostTypeSupportCheck( { postType, children, supportKeys, themeSupports } ) { | ||
supportKeys = castArray( supportKeys ); | ||
const isSupported = some( | ||
castArray( supportKeys ), ( key ) => get( postType, [ 'supports', key ], false ) | ||
supportKeys, ( key ) => get( postType, [ 'supports', key ], false ) | ||
); | ||
|
||
if ( ! isSupported ) { | ||
return null; | ||
} | ||
|
||
// 'thumbnail' and 'post-thumbnails' are intentionally different. | ||
if ( includes( supportKeys, 'thumbnail' ) && | ||
! get( themeSupports, 'post-thumbnails', false ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This must be updated to pass the second argument as an array, or will fail master build. (#6247) |
||
return null; | ||
} | ||
|
||
return children; | ||
} | ||
|
||
export default withSelect( ( select ) => { | ||
const { getEditedPostAttribute } = select( 'core/editor' ); | ||
const { getPostType } = select( 'core' ); | ||
const { getPostType, getThemeSupports } = select( 'core' ); | ||
return { | ||
themeSupports: getThemeSupports(), | ||
postType: getPostType( getEditedPostAttribute( 'type' ) ), | ||
}; | ||
} )( PostTypeSupportCheck ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { shallow } from 'enzyme'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { PostTypeSupportCheck } from '../index'; | ||
|
||
describe( 'PostTypeSupportCheck', () => { | ||
it( 'should not render if there\'s no support check provided', () => { | ||
const wrapper = shallow( <PostTypeSupportCheck>foobar</PostTypeSupportCheck> ); | ||
expect( wrapper.type() ).toBe( null ); | ||
} ); | ||
|
||
it( 'should render if both thumbnail and post-thumbnails are supported', () => { | ||
const themeSupports = { | ||
'post-thumbnails': true, | ||
}; | ||
const postType = { | ||
supports: { | ||
thumbnail: true, | ||
}, | ||
}; | ||
const supportKeys = 'thumbnail'; | ||
const wrapper = shallow( <PostTypeSupportCheck | ||
supportKeys={ supportKeys } | ||
postType={ postType } | ||
themeSupports={ themeSupports }>foobar</PostTypeSupportCheck> ); | ||
expect( wrapper.type() ).not.toBe( null ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use more targeted expectation in here to ensure it properly renders |
||
} ); | ||
|
||
it( 'should not render if theme doesn\'t support thumbnails', () => { | ||
const themeSupports = { | ||
'post-thumbnails': false, | ||
}; | ||
const postType = { | ||
supports: { | ||
thumbnail: true, | ||
}, | ||
}; | ||
const supportKeys = 'thumbnail'; | ||
const wrapper = shallow( <PostTypeSupportCheck | ||
supportKeys={ supportKeys } | ||
postType={ postType } | ||
themeSupports={ themeSupports }>foobar</PostTypeSupportCheck> ); | ||
expect( wrapper.type() ).toBe( null ); | ||
} ); | ||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does
PostTypeSupportCheck
need to be handling this exception? If they're truly intentionally different, why lump them to be considered together? Should it be up to the consuming component to check for both the post type and theme supports? Notably, I see this as being part of the reasonPostFeaturedImageCheck
exists as a standalone component, so it can become:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ThemeSupportCheck
is a good suggestion. I'll open a new PR with a different implementation.