From 77c5c6b4fe6ac8a5fc17709408dd5555abe7efad Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Wed, 18 Dec 2024 23:45:45 +0000 Subject: [PATCH 1/7] Fix: Editing "Page" is broken for low capability users. --- packages/editor/src/components/provider/index.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/editor/src/components/provider/index.js b/packages/editor/src/components/provider/index.js index abdab284cfd13..4184ec943f1f8 100644 --- a/packages/editor/src/components/provider/index.js +++ b/packages/editor/src/components/provider/index.js @@ -193,9 +193,11 @@ export const ExperimentalEditorProvider = withRegistryProvider( hasLoadedPostObject: _hasLoadedPostObject, editorSettings: getEditorSettings(), isReady: __unstableIsEditorReady(), - mode: getRenderingMode(), + mode: template ? getRenderingMode() : 'post-only', defaultMode: - postTypeObject?.default_rendering_mode ?? 'post-only', + template && postTypeObject?.default_rendering_mode + ? postTypeObject?.default_rendering_mode + : 'post-only', selection: getEditorSelection(), postTypeEntities: post.type === 'wp_template' @@ -203,7 +205,7 @@ export const ExperimentalEditorProvider = withRegistryProvider( : null, }; }, - [ post.type ] + [ template, post.type ] ); const shouldRenderTemplate = !! template && mode !== 'post-only'; From efab0fdadbca4f154e6a5473ef16f3ba51951611 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Thu, 19 Dec 2024 14:33:23 +0000 Subject: [PATCH 2/7] cehck for permissions --- .../edit-post/src/components/layout/index.js | 5 +++++ .../editor/src/components/provider/index.js | 19 +++++++++++-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/edit-post/src/components/layout/index.js b/packages/edit-post/src/components/layout/index.js index acc71afe1db0a..a672ee742c566 100644 --- a/packages/edit-post/src/components/layout/index.js +++ b/packages/edit-post/src/components/layout/index.js @@ -402,6 +402,10 @@ function Layout( { kind: 'postType', name: 'wp_template', } ); + const canDiscoverTemplate = canUser( 'read', { + kind: 'root', + name: 'site', + } ); const { isZoomOut } = unlock( select( blockEditorStore ) ); const { getEditorMode, getRenderingMode } = select( editorStore ); const isRenderingPostOnly = getRenderingMode() === 'post-only'; @@ -428,6 +432,7 @@ function Layout( { supportsTemplateMode && isViewable && canViewTemplate && + canDiscoverTemplate && ! isEditingTemplate ? getTemplateId( currentPostType, currentPostId ) : null, diff --git a/packages/editor/src/components/provider/index.js b/packages/editor/src/components/provider/index.js index 4184ec943f1f8..e3545e2f89e46 100644 --- a/packages/editor/src/components/provider/index.js +++ b/packages/editor/src/components/provider/index.js @@ -163,6 +163,7 @@ export const ExperimentalEditorProvider = withRegistryProvider( BlockEditorProviderComponent = ExperimentalBlockEditorProvider, __unstableTemplate: template, } ) => { + const hasTemplate = !! template; const { editorSettings, selection, @@ -193,11 +194,8 @@ export const ExperimentalEditorProvider = withRegistryProvider( hasLoadedPostObject: _hasLoadedPostObject, editorSettings: getEditorSettings(), isReady: __unstableIsEditorReady(), - mode: template ? getRenderingMode() : 'post-only', - defaultMode: - template && postTypeObject?.default_rendering_mode - ? postTypeObject?.default_rendering_mode - : 'post-only', + mode: getRenderingMode(), + defaultMode: postTypeObject?.default_rendering_mode, selection: getEditorSelection(), postTypeEntities: post.type === 'wp_template' @@ -205,7 +203,7 @@ export const ExperimentalEditorProvider = withRegistryProvider( : null, }; }, - [ template, post.type ] + [ post.type ] ); const shouldRenderTemplate = !! template && mode !== 'post-only'; @@ -325,14 +323,19 @@ export const ExperimentalEditorProvider = withRegistryProvider( // Sets the right rendering mode when loading the editor. useEffect( () => { - setRenderingMode( defaultMode ); - }, [ defaultMode, setRenderingMode ] ); + setRenderingMode( hasTemplate ? defaultMode : 'post-only' ); + }, [ hasTemplate, defaultMode, setRenderingMode ] ); useHideBlocksFromInserter( post.type, mode ); // Register the editor commands. useCommands(); + console.log( { + notRender: ! isReady || ! mode || ! hasLoadedPostObject, + settings: ! settings.isPreviewMode, + } ); + if ( ! isReady || ! mode || ! hasLoadedPostObject ) { return null; } From 3d976b5ff69e9ce324dcc2be158f74e38bfe5985 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Thu, 19 Dec 2024 23:00:08 +0000 Subject: [PATCH 3/7] remove debug logic --- packages/editor/src/components/provider/index.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/editor/src/components/provider/index.js b/packages/editor/src/components/provider/index.js index e3545e2f89e46..7950194218fdc 100644 --- a/packages/editor/src/components/provider/index.js +++ b/packages/editor/src/components/provider/index.js @@ -195,7 +195,8 @@ export const ExperimentalEditorProvider = withRegistryProvider( editorSettings: getEditorSettings(), isReady: __unstableIsEditorReady(), mode: getRenderingMode(), - defaultMode: postTypeObject?.default_rendering_mode, + defaultMode: + postTypeObject?.default_rendering_mode ?? 'post-only', selection: getEditorSelection(), postTypeEntities: post.type === 'wp_template' @@ -331,11 +332,6 @@ export const ExperimentalEditorProvider = withRegistryProvider( // Register the editor commands. useCommands(); - console.log( { - notRender: ! isReady || ! mode || ! hasLoadedPostObject, - settings: ! settings.isPreviewMode, - } ); - if ( ! isReady || ! mode || ! hasLoadedPostObject ) { return null; } From 0c4aaa40bbda8a0b5511532d6d7706b9146114c3 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Mon, 23 Dec 2024 14:57:09 +0000 Subject: [PATCH 4/7] feedback --- packages/core-data/src/private-selectors.ts | 18 +++++++++++++++++- .../edit-post/src/components/layout/index.js | 5 ----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/core-data/src/private-selectors.ts b/packages/core-data/src/private-selectors.ts index 0d4a28ad174a1..9249da5c6a05a 100644 --- a/packages/core-data/src/private-selectors.ts +++ b/packages/core-data/src/private-selectors.ts @@ -6,7 +6,12 @@ import { createSelector, createRegistrySelector } from '@wordpress/data'; /** * Internal dependencies */ -import { getDefaultTemplateId, getEntityRecord, type State } from './selectors'; +import { + canUser, + getDefaultTemplateId, + getEntityRecord, + type State, +} from './selectors'; import { STORE_NAME } from './name'; import { unlock } from './lock-unlock'; @@ -134,6 +139,13 @@ interface SiteData { export const getHomePage = createRegistrySelector( ( select ) => createSelector( () => { + const canReadSiteData = select( STORE_NAME ).canUser( 'read', { + kind: 'root', + name: 'site', + } ); + if ( ! canReadSiteData ) { + return null; + } const siteData = select( STORE_NAME ).getEntityRecord( 'root', 'site' @@ -156,6 +168,10 @@ export const getHomePage = createRegistrySelector( ( select ) => return { postType: 'wp_template', postId: frontPageTemplateId }; }, ( state ) => [ + canUser( state, 'read', { + kind: 'root', + name: 'site', + } ), getEntityRecord( state, 'root', 'site' ), getDefaultTemplateId( state, { slug: 'front-page', diff --git a/packages/edit-post/src/components/layout/index.js b/packages/edit-post/src/components/layout/index.js index a672ee742c566..acc71afe1db0a 100644 --- a/packages/edit-post/src/components/layout/index.js +++ b/packages/edit-post/src/components/layout/index.js @@ -402,10 +402,6 @@ function Layout( { kind: 'postType', name: 'wp_template', } ); - const canDiscoverTemplate = canUser( 'read', { - kind: 'root', - name: 'site', - } ); const { isZoomOut } = unlock( select( blockEditorStore ) ); const { getEditorMode, getRenderingMode } = select( editorStore ); const isRenderingPostOnly = getRenderingMode() === 'post-only'; @@ -432,7 +428,6 @@ function Layout( { supportsTemplateMode && isViewable && canViewTemplate && - canDiscoverTemplate && ! isEditingTemplate ? getTemplateId( currentPostType, currentPostId ) : null, From e4bd237f59f6f24241a6c5ddac2348b1a32ac86b Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Mon, 23 Dec 2024 18:16:23 +0000 Subject: [PATCH 5/7] enhacements --- packages/core-data/src/private-selectors.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/core-data/src/private-selectors.ts b/packages/core-data/src/private-selectors.ts index 9249da5c6a05a..5b650b60618b8 100644 --- a/packages/core-data/src/private-selectors.ts +++ b/packages/core-data/src/private-selectors.ts @@ -171,8 +171,7 @@ export const getHomePage = createRegistrySelector( ( select ) => canUser( state, 'read', { kind: 'root', name: 'site', - } ), - getEntityRecord( state, 'root', 'site' ), + } ) && getEntityRecord( state, 'root', 'site' ), getDefaultTemplateId( state, { slug: 'front-page', } ), From f7d93f50dd51c2e3217c368c79a7c9c608294501 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 27 Dec 2024 17:39:26 +0000 Subject: [PATCH 6/7] post rebase fixes --- packages/editor/src/components/provider/index.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/editor/src/components/provider/index.js b/packages/editor/src/components/provider/index.js index 7950194218fdc..1259eae623de9 100644 --- a/packages/editor/src/components/provider/index.js +++ b/packages/editor/src/components/provider/index.js @@ -196,7 +196,9 @@ export const ExperimentalEditorProvider = withRegistryProvider( isReady: __unstableIsEditorReady(), mode: getRenderingMode(), defaultMode: - postTypeObject?.default_rendering_mode ?? 'post-only', + hasTemplate && postTypeObject?.default_rendering_mode + ? postTypeObject?.default_rendering_mode + : 'post-only', selection: getEditorSelection(), postTypeEntities: post.type === 'wp_template' @@ -204,7 +206,7 @@ export const ExperimentalEditorProvider = withRegistryProvider( : null, }; }, - [ post.type ] + [ post.type, hasTemplate ] ); const shouldRenderTemplate = !! template && mode !== 'post-only'; @@ -324,8 +326,8 @@ export const ExperimentalEditorProvider = withRegistryProvider( // Sets the right rendering mode when loading the editor. useEffect( () => { - setRenderingMode( hasTemplate ? defaultMode : 'post-only' ); - }, [ hasTemplate, defaultMode, setRenderingMode ] ); + setRenderingMode( defaultMode ); + }, [ defaultMode, setRenderingMode ] ); useHideBlocksFromInserter( post.type, mode ); From 27f695178a8d8257138fde6af0e478b442f91141 Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Fri, 27 Dec 2024 17:44:00 +0000 Subject: [PATCH 7/7] add permision check on getPostsPageId --- packages/core-data/src/private-selectors.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/core-data/src/private-selectors.ts b/packages/core-data/src/private-selectors.ts index 5b650b60618b8..fb0401509694e 100644 --- a/packages/core-data/src/private-selectors.ts +++ b/packages/core-data/src/private-selectors.ts @@ -180,6 +180,13 @@ export const getHomePage = createRegistrySelector( ( select ) => ); export const getPostsPageId = createRegistrySelector( ( select ) => () => { + const canReadSiteData = select( STORE_NAME ).canUser( 'read', { + kind: 'root', + name: 'site', + } ); + if ( ! canReadSiteData ) { + return null; + } const siteData = select( STORE_NAME ).getEntityRecord( 'root', 'site' ) as | SiteData | undefined;