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 #6541

Merged
merged 2 commits into from
May 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion editor/components/post-featured-image/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
* Internal dependencies
*/
import PostTypeSupportCheck from '../post-type-support-check';
import ThemeSupportCheck from '../theme-support-check';

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

@aduth aduth May 3, 2018

Choose a reason for hiding this comment

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

Nit (Style): My preference (and predominantly throughout the codebase) is for JSX to be placed on its own lines with parentheses if multi-lined, e.g.:

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

If it's a convention we want to adopt, we could enforce with:

https://github.com/yannickcr/eslint-plugin-react/blob/master/docs/rules/jsx-wrap-multilines.md

Copy link
Member Author

Choose a reason for hiding this comment

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

My preference (and predominantly throughout the codebase) is for JSX to be placed on its own lines with parentheses if multi-lined, e.g.:

Seems reasonable to me 1916026

<PostTypeSupportCheck { ...props } supportKeys="thumbnail" />
</ThemeSupportCheck>;
}

export default PostFeaturedImageCheck;
28 changes: 28 additions & 0 deletions editor/components/theme-support-check/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* External dependencies
*/
import { get, some, castArray } from 'lodash';

/**
* WordPress dependencies
*/
import { withSelect } from '@wordpress/data';

export function ThemeSupportCheck( { themeSupports, children, supportKeys } ) {
const isSupported = some(
castArray( supportKeys ), ( key ) => get( themeSupports, [ key ], false )
);

if ( ! isSupported ) {
return null;
}

return children;
}

export default withSelect( ( select ) => {
const { getThemeSupports } = select( 'core' );
return {
themeSupports: getThemeSupports(),
};
} )( ThemeSupportCheck );
38 changes: 38 additions & 0 deletions editor/components/theme-support-check/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* External dependencies
*/
import { shallow } from 'enzyme';

/**
* Internal dependencies
*/
import { ThemeSupportCheck } from '../index';

describe( 'ThemeSupportCheck', () => {
it( 'should not render if there\'s no support check provided', () => {
const wrapper = shallow( <ThemeSupportCheck>foobar</ThemeSupportCheck> );
expect( wrapper.type() ).toBe( null );
} );

it( 'should render if post-thumbnails are supported', () => {
const themeSupports = {
'post-thumbnails': true,
};
const supportKeys = 'post-thumbnails';
const wrapper = shallow( <ThemeSupportCheck
supportKeys={ supportKeys }
themeSupports={ themeSupports }>foobar</ThemeSupportCheck> );
expect( wrapper.type() ).not.toBe( null );
} );

it( 'should not render if theme doesn\'t support post-thumbnails', () => {
const themeSupports = {
'post-thumbnails': false,
};
const supportKeys = 'post-thumbnails';
const wrapper = shallow( <ThemeSupportCheck
supportKeys={ supportKeys }
themeSupports={ themeSupports }>foobar</ThemeSupportCheck> );
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