From 820ec9edb57c03cffd64f726334c6f727ddc1526 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 20 Oct 2022 11:56:10 +0400 Subject: [PATCH] Site Tagline: Fix user permission HTTP errors --- .../block-library/src/site-tagline/edit.js | 43 +++++++++++-------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/packages/block-library/src/site-tagline/edit.js b/packages/block-library/src/site-tagline/edit.js index 5c8257d11c3fda..1ce13c6eb43c49 100644 --- a/packages/block-library/src/site-tagline/edit.js +++ b/packages/block-library/src/site-tagline/edit.js @@ -6,8 +6,8 @@ import classnames from 'classnames'; /** * WordPress dependencies */ -import { useSelect } from '@wordpress/data'; -import { useEntityProp, store as coreStore } from '@wordpress/core-data'; +import { useDispatch, useSelect } from '@wordpress/data'; +import { store as coreStore } from '@wordpress/core-data'; import { AlignmentControl, useBlockProps, @@ -23,34 +23,43 @@ export default function SiteTaglineEdit( { insertBlocksAfter, } ) { const { textAlign } = attributes; - const [ siteTagline, setSiteTagline ] = useEntityProp( - 'root', - 'site', - 'description' - ); - const { canUserEdit, readOnlySiteTagLine } = useSelect( ( select ) => { - const { canUser, getEntityRecord } = select( coreStore ); - const siteData = getEntityRecord( 'root', '__unstableBase' ); + const { canUserEdit, tagline } = useSelect( ( select ) => { + const { canUser, getEntityRecord, getEditedEntityRecord } = + select( coreStore ); + const canEdit = canUser( 'update', 'settings' ); + const settings = canEdit ? getEditedEntityRecord( 'root', 'site' ) : {}; + const readOnlySettings = getEntityRecord( 'root', '__unstableBase' ); + return { canUserEdit: canUser( 'update', 'settings' ), - readOnlySiteTagLine: siteData?.description, + tagline: canEdit + ? settings?.description + : readOnlySettings?.description, }; }, [] ); + + const { editEntityRecord } = useDispatch( coreStore ); + + function setTagline( newTagline ) { + editEntityRecord( 'root', 'site', undefined, { + description: newTagline, + } ); + } + const blockProps = useBlockProps( { className: classnames( { [ `has-text-align-${ textAlign }` ]: textAlign, - 'wp-block-site-tagline__placeholder': - ! canUserEdit && ! readOnlySiteTagLine, + 'wp-block-site-tagline__placeholder': ! canUserEdit && ! tagline, } ), } ); const siteTaglineContent = canUserEdit ? ( insertBlocksAfter( createBlock( getDefaultBlockName() ) ) @@ -58,9 +67,7 @@ export default function SiteTaglineEdit( { { ...blockProps } /> ) : ( -

- { readOnlySiteTagLine || __( 'Site Tagline placeholder' ) } -

+

{ tagline || __( 'Site Tagline placeholder' ) }

); return ( <>