Skip to content

Commit d290d99

Browse files
ntsekourasyouknowriad
authored andcommitted
[Block Library - Site Logo]: Add permissions handling (#32919)
* [Block Library - Site Logo]: Add permissions handling * Add only logo id to rest index * add link to logo in rest index * use context 'view' only * fallback to logo id from rest index
1 parent b975705 commit d290d99

File tree

3 files changed

+112
-53
lines changed

3 files changed

+112
-53
lines changed

lib/init.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,33 @@ function register_site_icon_url( $response ) {
187187

188188
add_filter( 'rest_index', 'register_site_icon_url' );
189189

190+
/**
191+
* Exposes the site logo to the Gutenberg editor through the WordPress REST
192+
* API. This is used for fetching this information when user has no rights
193+
* to update settings.
194+
*
195+
* @since 10.9
196+
*
197+
* @param WP_REST_Response $response Response data served by the WordPress REST index endpoint.
198+
* @return WP_REST_Response
199+
*/
200+
function register_site_logo_to_rest_index( $response ) {
201+
$site_logo_id = get_theme_mod( 'custom_logo' );
202+
$response->data['site_logo'] = $site_logo_id;
203+
if ( $site_logo_id ) {
204+
$response->add_link(
205+
'https://api.w.org/featuredmedia',
206+
rest_url( 'wp/v2/media/' . $site_logo_id ),
207+
array(
208+
'embeddable' => true,
209+
)
210+
);
211+
}
212+
return $response;
213+
}
214+
215+
add_filter( 'rest_index', 'register_site_logo_to_rest_index' );
216+
190217
add_theme_support( 'widgets-block-editor' );
191218

192219
/**

packages/block-library/src/site-logo/edit.js

Lines changed: 65 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
ResizableBox,
1818
Spinner,
1919
ToggleControl,
20+
Icon,
2021
} from '@wordpress/components';
2122
import { useViewportMatch } from '@wordpress/compose';
2223
import {
@@ -73,7 +74,7 @@ const SiteLogo = ( {
7374
title: siteEntities.title,
7475
...pick( getSettings(), [ 'imageSizes', 'maxWidth' ] ),
7576
};
76-
} );
77+
}, [] );
7778

7879
function onResizeStart() {
7980
toggleSelection( false );
@@ -255,27 +256,38 @@ export default function LogoEdit( {
255256
const [ logoUrl, setLogoUrl ] = useState();
256257
const [ error, setError ] = useState();
257258
const ref = useRef();
258-
const { mediaItemData, siteLogo, url } = useSelect( ( select ) => {
259-
const siteSettings = select( coreStore ).getEditedEntityRecord(
260-
'root',
261-
'site'
262-
);
263-
const mediaItem = siteSettings.site_logo
264-
? select( coreStore ).getEntityRecord(
259+
260+
const { siteLogoId, canUserEdit, url, mediaItemData } = useSelect(
261+
( select ) => {
262+
const { canUser, getEntityRecord, getEditedEntityRecord } = select(
263+
coreStore
264+
);
265+
const siteSettings = getEditedEntityRecord( 'root', 'site' );
266+
const siteData = getEntityRecord( 'root', '__unstableBase' );
267+
const _siteLogo = siteSettings?.site_logo;
268+
const _readOnlyLogo = siteData?.site_logo;
269+
const _canUserEdit = canUser( 'update', 'settings' );
270+
const _siteLogoId = _siteLogo || _readOnlyLogo;
271+
const mediaItem =
272+
_siteLogoId &&
273+
select( coreStore ).getEntityRecord(
265274
'root',
266275
'media',
267-
siteSettings.site_logo
268-
)
269-
: null;
270-
return {
271-
mediaItemData: mediaItem && {
272-
url: mediaItem.source_url,
273-
alt: mediaItem.alt_text,
274-
},
275-
siteLogo: siteSettings.site_logo,
276-
url: siteSettings.url,
277-
};
278-
}, [] );
276+
_siteLogoId,
277+
{ context: 'view' }
278+
);
279+
return {
280+
siteLogoId: _siteLogoId,
281+
canUserEdit: _canUserEdit,
282+
url: siteData?.url,
283+
mediaItemData: mediaItem && {
284+
url: mediaItem.source_url,
285+
alt: mediaItem.alt_text,
286+
},
287+
};
288+
},
289+
[]
290+
);
279291

280292
const { editEntityRecord } = useDispatch( coreStore );
281293
const setLogo = ( newValue ) =>
@@ -290,7 +302,6 @@ export default function LogoEdit( {
290302
setLogoUrl( mediaItemData.url );
291303
}
292304
}
293-
294305
const onSelectLogo = ( media ) => {
295306
if ( ! media ) {
296307
return;
@@ -311,7 +322,7 @@ export default function LogoEdit( {
311322
setError( message[ 2 ] ? message[ 2 ] : null );
312323
};
313324

314-
const controls = logoUrl && (
325+
const controls = canUserEdit && logoUrl && (
315326
<BlockControls group="other">
316327
<MediaReplaceFlow
317328
mediaURL={ logoUrl }
@@ -325,10 +336,10 @@ export default function LogoEdit( {
325336

326337
const label = __( 'Site Logo' );
327338
let logoImage;
328-
if ( siteLogo === undefined ) {
339+
const isLoading = siteLogoId === undefined || ( siteLogoId && ! logoUrl );
340+
if ( isLoading ) {
329341
logoImage = <Spinner />;
330342
}
331-
332343
if ( !! logoUrl ) {
333344
logoImage = (
334345
<SiteLogo
@@ -343,45 +354,46 @@ export default function LogoEdit( {
343354
/>
344355
);
345356
}
346-
347-
const mediaPlaceholder = (
348-
<MediaPlaceholder
349-
icon={ <BlockIcon icon={ icon } /> }
350-
labels={ {
351-
title: label,
352-
instructions: __(
353-
'Upload an image, or pick one from your media library, to be your site logo'
354-
),
355-
} }
356-
onSelect={ onSelectLogo }
357-
accept={ ACCEPT_MEDIA_STRING }
358-
allowedTypes={ ALLOWED_MEDIA_TYPES }
359-
mediaPreview={ logoImage }
360-
notices={
361-
error && (
362-
<Notice status="error" isDismissible={ false }>
363-
{ error }
364-
</Notice>
365-
)
366-
}
367-
onError={ onUploadError }
368-
/>
369-
);
370-
371357
const classes = classnames( className, {
372358
'is-default-size': ! width,
373359
} );
374-
375360
const blockProps = useBlockProps( {
376361
ref,
377362
className: classes,
378363
} );
379-
380364
return (
381365
<div { ...blockProps }>
382366
{ controls }
383-
{ logoUrl && logoImage }
384-
{ ! logoUrl && mediaPlaceholder }
367+
{ !! logoUrl && logoImage }
368+
{ ! logoUrl && ! canUserEdit && (
369+
<div className="site-logo_placeholder">
370+
<Icon icon={ icon } />
371+
<p> { __( 'Site Logo' ) }</p>
372+
</div>
373+
) }
374+
{ ! logoUrl && canUserEdit && (
375+
<MediaPlaceholder
376+
icon={ <BlockIcon icon={ icon } /> }
377+
labels={ {
378+
title: label,
379+
instructions: __(
380+
'Upload an image, or pick one from your media library, to be your site logo'
381+
),
382+
} }
383+
onSelect={ onSelectLogo }
384+
accept={ ACCEPT_MEDIA_STRING }
385+
allowedTypes={ ALLOWED_MEDIA_TYPES }
386+
mediaPreview={ logoImage }
387+
notices={
388+
error && (
389+
<Notice status="error" isDismissible={ false }>
390+
{ error }
391+
</Notice>
392+
)
393+
}
394+
onError={ onUploadError }
395+
/>
396+
) }
385397
</div>
386398
);
387399
}

packages/block-library/src/site-logo/editor.scss

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,23 @@
8080
}
8181
}
8282
}
83+
.editor-styles-wrapper {
84+
.site-logo_placeholder {
85+
display: flex;
86+
flex-direction: row;
87+
align-items: flex-start;
88+
border-radius: $radius-block-ui;
89+
background-color: $white;
90+
box-shadow: inset 0 0 0 $border-width $gray-900;
91+
padding: $grid-unit-15;
92+
svg {
93+
margin-right: $grid-unit-15;
94+
}
95+
p {
96+
font-family: $default-font;
97+
font-size: $default-font-size;
98+
margin: 0;
99+
line-height: initial;
100+
}
101+
}
102+
}

0 commit comments

Comments
 (0)