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

Page for Posts: Display notice in template panel #38607

Merged
merged 3 commits into from
Feb 8, 2022
Merged
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
10 changes: 10 additions & 0 deletions lib/full-site-editing/edit-site-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,16 @@ function register_site_editor_homepage_settings() {
'description' => __( 'The ID of the page that should be displayed on the front page', 'gutenberg' ),
)
);

register_setting(
'reading',
'page_for_posts',
array(
'show_in_rest' => true,
'type' => 'number',
'description' => __( 'The ID of the page that should display the latest posts', 'gutenberg' ),
)
);
Copy link
Member Author

Choose a reason for hiding this comment

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

I noticed that static page settings aren't available when the plugin is deactivated. I think we forgot to expose these settings in REST API for 5.9.

}
add_action( 'init', 'register_site_editor_homepage_settings', 10 );

Expand Down
19 changes: 12 additions & 7 deletions packages/edit-post/src/components/sidebar/template/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { store as coreStore } from '@wordpress/core-data';
import { store as editPostStore } from '../../../store';
import { createBlock, serialize } from '@wordpress/blocks';

function PostTemplateActions() {
function PostTemplateActions( { isPostsPage } ) {
const [ isModalOpen, setIsModalOpen ] = useState( false );
const [ isBusy, setIsBusy ] = useState( false );
const [ title, setTitle ] = useState( '' );
Expand Down Expand Up @@ -128,12 +128,17 @@ function PostTemplateActions() {
{ __( 'Edit' ) }
</Button>
) }
<Button variant="link" onClick={ () => setIsModalOpen( true ) }>
{
/* translators: button to create a new template */
_x( 'New', 'action' )
}
</Button>
{ ! isPostsPage && (
<Button
variant="link"
onClick={ () => setIsModalOpen( true ) }
>
{
/* translators: button to create a new template */
_x( 'New', 'action' )
}
</Button>
) }
</div>
{ isModalOpen && (
<Modal
Expand Down
68 changes: 47 additions & 21 deletions packages/edit-post/src/components/sidebar/template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { partial, isEmpty, map, fromPairs } from 'lodash';
*/
import { __, sprintf } from '@wordpress/i18n';
import { useMemo } from '@wordpress/element';
import { PanelBody, SelectControl } from '@wordpress/components';
import { Notice, PanelBody, SelectControl } from '@wordpress/components';
import { store as editorStore } from '@wordpress/editor';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
Expand All @@ -28,6 +28,7 @@ export function TemplatePanel() {
const {
isEnabled,
isOpened,
isPostsPage,
selectedTemplate,
availableTemplates,
fetchedTemplates,
Expand All @@ -44,10 +45,19 @@ export function TemplatePanel() {
const {
getEditedPostAttribute,
getEditorSettings,
getCurrentPostId,
getCurrentPostType,
} = select( editorStore );
const { getPostType, getEntityRecords, canUser } = select( coreStore );
const {
getPostType,
getEntityRecord,
getEntityRecords,
canUser,
} = select( coreStore );

const currentPostId = getCurrentPostId();
const currentPostType = getCurrentPostType();
const settings = getEntityRecord( 'root', 'site' );
const _isViewable = getPostType( currentPostType )?.viewable ?? false;
const _supportsTemplateMode =
select( editorStore ).getEditorSettings().supportsTemplateMode &&
Expand All @@ -61,6 +71,7 @@ export function TemplatePanel() {
return {
isEnabled: isEditorPanelEnabled( PANEL_NAME ),
isOpened: isEditorPanelOpened( PANEL_NAME ),
isPostsPage: currentPostId === settings?.page_for_posts,
selectedTemplate: getEditedPostAttribute( 'template' ),
availableTemplates: getEditorSettings().availableTemplates,
fetchedTemplates: templateRecords,
Expand Down Expand Up @@ -112,25 +123,40 @@ export function TemplatePanel() {
opened={ isOpened }
onToggle={ onTogglePanel }
>
<SelectControl
hideLabelFromVision
label={ __( 'Template:' ) }
value={
Object.keys( templates ).includes( selectedTemplate )
? selectedTemplate
: ''
}
onChange={ ( templateSlug ) => {
editPost( {
template: templateSlug || '',
} );
} }
options={ map( templates, ( templateName, templateSlug ) => ( {
value: templateSlug,
label: templateName,
} ) ) }
/>
{ canUserCreate && <PostTemplateActions /> }
{ isPostsPage ? (
<Notice
className="edit-post-template__notice"
status="warning"
isDismissible={ false }
>
{ __( 'The posts page template cannot be changed.' ) }
</Notice>
) : (
<SelectControl
hideLabelFromVision
label={ __( 'Template:' ) }
value={
Object.keys( templates ).includes( selectedTemplate )
? selectedTemplate
: ''
}
onChange={ ( templateSlug ) => {
editPost( {
template: templateSlug || '',
} );
} }
options={ map(
templates,
( templateName, templateSlug ) => ( {
value: templateSlug,
label: templateName,
} )
) }
/>
) }
{ canUserCreate && (
<PostTemplateActions isPostsPage={ isPostsPage } />
) }
</PanelBody>
);
}
Expand Down
8 changes: 8 additions & 0 deletions packages/edit-post/src/components/sidebar/template/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
}
}

.edit-post-template__notice {
margin: 0 0 $grid-unit-10 0;

.components-notice__content {
margin: 0;
}
}

.edit-post-template__actions {
button:not(:last-child) {
margin-right: $grid-unit-10;
Expand Down