Skip to content

Commit

Permalink
Only display featured image UI when theme supports it too (#6541)
Browse files Browse the repository at this point in the history
* Only display featured image UI when theme supports it too

* JSX formatting style
  • Loading branch information
danielbachhuber authored May 3, 2018
1 parent 9870e81 commit dc7ee12
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
7 changes: 6 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,14 @@
* 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">
<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

0 comments on commit dc7ee12

Please sign in to comment.