-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathpanel.js
83 lines (74 loc) · 2.1 KB
/
panel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import { store as editorStore } from '../../store';
import ClassicThemeControl from './classic-theme';
import BlockThemeControl from './block-theme';
import PostPanelRow from '../post-panel-row';
/**
* Displays the template controls based on the current editor settings and user permissions.
*
* @return {React.ReactNode} The rendered PostTemplatePanel component.
*/
export default function PostTemplatePanel() {
const { templateId, isBlockTheme } = useSelect( ( select ) => {
const { getCurrentTemplateId, getEditorSettings } =
select( editorStore );
return {
templateId: getCurrentTemplateId(),
isBlockTheme: getEditorSettings().__unstableIsBlockBasedTheme,
};
}, [] );
const isVisible = useSelect( ( select ) => {
const postTypeSlug = select( editorStore ).getCurrentPostType();
const postType = select( coreStore ).getPostType( postTypeSlug );
if ( ! postType?.viewable ) {
return false;
}
const settings = select( editorStore ).getEditorSettings();
const hasTemplates =
!! settings.availableTemplates &&
Object.keys( settings.availableTemplates ).length > 0;
if ( hasTemplates ) {
return true;
}
if ( ! settings.supportsTemplateMode ) {
return false;
}
const canCreateTemplates =
select( coreStore ).canUser( 'create', {
kind: 'postType',
name: 'wp_template',
} ) ?? false;
return canCreateTemplates;
}, [] );
const canViewTemplates = useSelect( ( select ) => {
return (
select( coreStore ).canUser( 'read', {
kind: 'postType',
name: 'wp_template',
} ) ?? false
);
}, [] );
if ( ( ! isBlockTheme || ! canViewTemplates ) && isVisible ) {
return (
<PostPanelRow label={ __( 'Template' ) }>
<ClassicThemeControl />
</PostPanelRow>
);
}
if ( isBlockTheme && !! templateId ) {
return (
<PostPanelRow label={ __( 'Template' ) }>
<BlockThemeControl id={ templateId } />
</PostPanelRow>
);
}
return null;
}