Skip to content

Commit ddb3d66

Browse files
committed
Block editor: ensure meta data is saved before publishing.
Internal reference: p1749025385229489-slack-C04TJ8P900J
1 parent 2a98002 commit ddb3d66

File tree

3 files changed

+48
-33
lines changed

3 files changed

+48
-33
lines changed

build/editor-plugin/plugin.asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('react', 'wp-components', 'wp-core-data', 'wp-data', 'wp-editor', 'wp-element', 'wp-i18n', 'wp-plugins', 'wp-primitives', 'wp-url'), 'version' => '1bd009e5ff8d85217279');
1+
<?php return array('dependencies' => array('react', 'wp-components', 'wp-core-data', 'wp-data', 'wp-editor', 'wp-element', 'wp-i18n', 'wp-plugins', 'wp-primitives', 'wp-url'), 'version' => 'f8f825f764e20b51bf8c');

build/editor-plugin/plugin.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/editor-plugin/plugin.js

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,61 @@
1-
import { PluginDocumentSettingPanel, PluginPreviewMenuItem } from '@wordpress/editor';
1+
import { PluginDocumentSettingPanel, PluginPreviewMenuItem, store as editorStore } from '@wordpress/editor';
22
import { registerPlugin } from '@wordpress/plugins';
33
import { TextControl, RadioControl, RangeControl, __experimentalText as Text, Tooltip } from '@wordpress/components';
4-
import { Icon, globe, people, external } from '@wordpress/icons';
4+
import { Icon, globe, people, external, notAllowed } from '@wordpress/icons';
55
import { useSelect, select } from '@wordpress/data';
66
import { useEntityProp } from '@wordpress/core-data';
77
import { addQueryArgs } from '@wordpress/url';
88
import { __ } from '@wordpress/i18n';
9-
import { SVG, Path } from '@wordpress/primitives';
109
import { useOptions } from '../shared/use-options';
1110

12-
// Defining our own because it's too new in @wordpress/icons
13-
// https://github.com/WordPress/gutenberg/blob/trunk/packages/icons/src/library/not-allowed.js
14-
const notAllowed = (
15-
<SVG xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
16-
<Path
17-
fillRule="evenodd"
18-
clipRule="evenodd"
19-
d="M12 18.5A6.5 6.5 0 0 1 6.93 7.931l9.139 9.138A6.473 6.473 0 0 1 12 18.5Zm5.123-2.498a6.5 6.5 0 0 0-9.124-9.124l9.124 9.124ZM4 12a8 8 0 1 1 16 0 8 8 0 0 1-16 0Z"
20-
/>
21-
</SVG>
22-
);
11+
/**
12+
* Custom hook to update metadata in the post editor.
13+
*
14+
* @param {string} metaKey The key of the metadata to update.
15+
* @param {string} postType The type of post to update the metadata for.
16+
* @returns {[string, (value: string) => void]} The current value of the metadata and a function to update it.
17+
*/
18+
function useSetMeta( metaKey, postType ) {
19+
const [ meta, setMeta ] = useEntityProp( 'postType', postType || 'default', 'meta' );
20+
21+
const setValue = ( value ) => {
22+
setMeta( { ...meta, [ metaKey ]: value } );
23+
};
24+
25+
return [ meta?.[ metaKey ], setValue ];
26+
}
2327

2428
/**
2529
* Editor plugin for ActivityPub settings in the block editor.
2630
*
2731
* @returns {JSX.Element|null} The settings panel for ActivityPub or null for sync blocks.
2832
*/
2933
const EditorPlugin = () => {
30-
const postType = useSelect( ( select ) => select( 'core/editor' ).getCurrentPostType(), [] );
31-
const [ meta, setMeta ] = useEntityProp( 'postType', postType || 'default', 'meta' );
34+
const postType = useSelect( ( select ) => select( editorStore ).getCurrentPostType(), [] );
35+
36+
const [ contentWarning, setContentWarning ] = useSetMeta( 'activitypub_content_warning', postType );
37+
const [ maxImageAttachments, setMaxImageAttachments ] = useSetMeta( 'activitypub_max_image_attachments', postType );
38+
const [ contentVisibility, setContentVisibility ] = useSetMeta( 'activitypub_content_visibility', postType );
39+
40+
const handleContentWarningChange = ( value ) => {
41+
setContentWarning( value );
42+
};
43+
44+
const handleMaxImageAttachmentsChange = ( value ) => {
45+
setMaxImageAttachments( value );
46+
};
47+
48+
const handleVisibilityChange = ( value ) => {
49+
setContentVisibility( value );
50+
};
3251

3352
// Don't show when editing sync blocks.
3453
if ( 'wp_block' === postType ) {
3554
return null;
3655
}
37-
const { maxImageAttachments = 4 } = useOptions();
56+
57+
const { maxImageAttachments: defaultMaxImageAttachments = 4 } = useOptions();
58+
3859
const labelStyling = {
3960
verticalAlign: 'middle',
4061
gap: '4px',
@@ -69,10 +90,8 @@ const EditorPlugin = () => {
6990
>
7091
<TextControl
7192
label={ __( 'Content Warning', 'activitypub' ) }
72-
value={ meta?.activitypub_content_warning }
73-
onChange={ ( value ) => {
74-
setMeta( { ...meta, activitypub_content_warning: value } );
75-
} }
93+
value={ contentWarning ?? '' }
94+
onChange={ handleContentWarningChange }
7695
placeholder={ __( 'Optional content warning', 'activitypub' ) }
7796
help={ __(
7897
'Content warnings do not change the content on your site, only in the fediverse.',
@@ -84,10 +103,8 @@ const EditorPlugin = () => {
84103

85104
<RangeControl
86105
label={ __( 'Maximum Image Attachments', 'activitypub' ) }
87-
value={ meta?.activitypub_max_image_attachments ?? maxImageAttachments }
88-
onChange={ ( value ) => {
89-
setMeta( { ...meta, activitypub_max_image_attachments: value } );
90-
} }
106+
value={ maxImageAttachments ?? defaultMaxImageAttachments }
107+
onChange={ handleMaxImageAttachmentsChange }
91108
min={ 0 }
92109
max={ 10 }
93110
help={ __(
@@ -104,7 +121,7 @@ const EditorPlugin = () => {
104121
"This adjusts the visibility of a post in the fediverse, but note that it won't affect how the post appears on the blog.",
105122
'activitypub'
106123
) }
107-
selected={ meta?.activitypub_content_visibility || 'public' }
124+
selected={ contentVisibility || 'public' }
108125
options={ [
109126
{
110127
label: enhancedLabel(
@@ -134,9 +151,7 @@ const EditorPlugin = () => {
134151
value: 'local',
135152
},
136153
] }
137-
onChange={ ( value ) => {
138-
setMeta( { ...meta, activitypub_content_visibility: value } );
139-
} }
154+
onChange={ handleVisibilityChange }
140155
className="activitypub-visibility"
141156
/>
142157
</PluginDocumentSettingPanel>
@@ -147,7 +162,7 @@ const EditorPlugin = () => {
147162
* Opens the Fediverse preview for the current post in a new tab.
148163
*/
149164
function onActivityPubPreview() {
150-
const previewLink = select( 'core/editor' ).getEditedPostPreviewLink();
165+
const previewLink = select( editorStore ).getEditedPostPreviewLink();
151166
const fediversePreviewLink = addQueryArgs( previewLink, { activitypub: 'true' } );
152167

153168
window.open( fediversePreviewLink, '_blank' );
@@ -160,7 +175,7 @@ function onActivityPubPreview() {
160175
*/
161176
const EditorPreview = () => {
162177
// check if post was saved
163-
const post_status = useSelect( ( select ) => select( 'core/editor' ).getCurrentPost().status );
178+
const post_status = useSelect( ( select ) => select( editorStore ).getCurrentPost().status );
164179

165180
return (
166181
<>

0 commit comments

Comments
 (0)