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

Only display featured image UI when theme supports it too #6510

Closed
wants to merge 3 commits into from
Closed
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
16 changes: 12 additions & 4 deletions editor/components/post-type-support-check/index.js
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' ) &&
Copy link
Member

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 reason PostFeaturedImageCheck exists as a standalone component, so it can become:

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

Copy link
Member Author

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.

! get( themeSupports, 'post-thumbnails', false ) ) {
Copy link
Member

Choose a reason for hiding this comment

The 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 );
50 changes: 50 additions & 0 deletions editor/components/post-type-support-check/test/index.js
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 );
Copy link
Member

Choose a reason for hiding this comment

The 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 foobar text? One of the ways of doing it is to use toHaveText matcher.

} );

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 );
} );
} );
5 changes: 5 additions & 0 deletions lib/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,11 @@ function gutenberg_ensure_wp_json_has_theme_supports( $response ) {

$site_info['theme_supports']['formats'] = $formats;
}
if ( ! array_key_exists( 'post-thumbnails', $site_info['theme_supports'] ) ) {
if ( get_theme_support( 'post-thumbnails' ) ) {
$site_info['theme_supports']['post-thumbnails'] = true;
}
}
$response->set_data( $site_info );
return $response;
}
Expand Down