Skip to content

Block editor: ensure meta data is saved before publishing. #1763

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

Closed
wants to merge 6 commits into from
Closed
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
4 changes: 4 additions & 0 deletions .github/changelog/fix-conflict-sidebar-meta-updates
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Block editor: avoid conflicts with other plugins that save meta data.
2 changes: 1 addition & 1 deletion build/editor-plugin/plugin.asset.php
Original file line number Diff line number Diff line change
@@ -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');
<?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' => 'fc80a746c1daa1dc0de8');
2 changes: 1 addition & 1 deletion build/editor-plugin/plugin.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 46 additions & 19 deletions src/editor-plugin/plugin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PluginDocumentSettingPanel, PluginPreviewMenuItem } from '@wordpress/editor';
import { PluginDocumentSettingPanel, PluginPreviewMenuItem, store as editorStore } from '@wordpress/editor';
import { registerPlugin } from '@wordpress/plugins';
import { TextControl, RadioControl, RangeControl, __experimentalText as Text, Tooltip } from '@wordpress/components';
import { Icon, globe, people, external } from '@wordpress/icons';
Expand All @@ -21,20 +21,53 @@ const notAllowed = (
</SVG>
);

/**
* Custom hook to update metadata in the post editor.
*
* @param {string} metaKey The key of the metadata to update.
* @param {string} postType The type of post to update the metadata for.
* @returns {[string, (value: string) => void]} The current value of the metadata and a function to update it.
*/
function useSetMeta( metaKey, postType ) {
const [ meta, setMeta ] = useEntityProp( 'postType', postType, 'meta' );

const setValue = ( value ) => {
setMeta( { ...meta, [ metaKey ]: value } );
};

return [ meta?.[ metaKey ], setValue ];
}

/**
* Editor plugin for ActivityPub settings in the block editor.
*
* @returns {JSX.Element|null} The settings panel for ActivityPub or null for sync blocks.
*/
const EditorPlugin = () => {
const postType = useSelect( ( select ) => select( 'core/editor' ).getCurrentPostType(), [] );
const [ meta, setMeta ] = useEntityProp( 'postType', postType || 'default', 'meta' );

const postType = useSelect( ( select ) => select( editorStore ).getCurrentPostType(), [] );
// Don't show when editing sync blocks.
if ( 'wp_block' === postType ) {
return null;
}
const { maxImageAttachments = 4 } = useOptions();

const [ contentWarning, setContentWarning ] = useSetMeta( 'activitypub_content_warning', postType );
const [ maxImageAttachments, setMaxImageAttachments ] = useSetMeta( 'activitypub_max_image_attachments', postType );
const [ contentVisibility, setContentVisibility ] = useSetMeta( 'activitypub_content_visibility', postType );

const handleContentWarningChange = ( value ) => {
setContentWarning( value );
};

const handleMaxImageAttachmentsChange = ( value ) => {
setMaxImageAttachments( value );
};

const handleVisibilityChange = ( value ) => {
setContentVisibility( value );
};

Comment on lines +57 to +67
Copy link
Member

@obenland obenland Jun 4, 2025

Choose a reason for hiding this comment

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

Can we use the setter functions in the onChange handlers directly? I'm not sure the wrappers are needed? The setters' function names are also less ambiguous.

const { maxImageAttachments: defaultMaxImageAttachments = 4 } = useOptions();

const labelStyling = {
verticalAlign: 'middle',
gap: '4px',
Expand Down Expand Up @@ -69,10 +102,8 @@ const EditorPlugin = () => {
>
<TextControl
label={ __( 'Content Warning', 'activitypub' ) }
value={ meta?.activitypub_content_warning }
onChange={ ( value ) => {
setMeta( { ...meta, activitypub_content_warning: value } );
} }
value={ contentWarning ?? '' }
onChange={ handleContentWarningChange }
placeholder={ __( 'Optional content warning', 'activitypub' ) }
help={ __(
'Content warnings do not change the content on your site, only in the fediverse.',
Expand All @@ -84,10 +115,8 @@ const EditorPlugin = () => {

<RangeControl
label={ __( 'Maximum Image Attachments', 'activitypub' ) }
value={ meta?.activitypub_max_image_attachments ?? maxImageAttachments }
onChange={ ( value ) => {
setMeta( { ...meta, activitypub_max_image_attachments: value } );
} }
value={ maxImageAttachments ?? defaultMaxImageAttachments }
onChange={ handleMaxImageAttachmentsChange }
min={ 0 }
max={ 10 }
help={ __(
Expand All @@ -104,7 +133,7 @@ const EditorPlugin = () => {
"This adjusts the visibility of a post in the fediverse, but note that it won't affect how the post appears on the blog.",
'activitypub'
) }
selected={ meta?.activitypub_content_visibility || 'public' }
selected={ contentVisibility || 'public' }
options={ [
{
label: enhancedLabel(
Expand Down Expand Up @@ -134,9 +163,7 @@ const EditorPlugin = () => {
value: 'local',
},
] }
onChange={ ( value ) => {
setMeta( { ...meta, activitypub_content_visibility: value } );
} }
onChange={ handleVisibilityChange }
className="activitypub-visibility"
/>
</PluginDocumentSettingPanel>
Expand All @@ -147,7 +174,7 @@ const EditorPlugin = () => {
* Opens the Fediverse preview for the current post in a new tab.
*/
function onActivityPubPreview() {
const previewLink = select( 'core/editor' ).getEditedPostPreviewLink();
const previewLink = select( editorStore ).getEditedPostPreviewLink();
const fediversePreviewLink = addQueryArgs( previewLink, { activitypub: 'true' } );

window.open( fediversePreviewLink, '_blank' );
Expand All @@ -160,7 +187,7 @@ function onActivityPubPreview() {
*/
const EditorPreview = () => {
// check if post was saved
const post_status = useSelect( ( select ) => select( 'core/editor' ).getCurrentPost().status );
const post_status = useSelect( ( select ) => select( editorStore ).getCurrentPost().status );

return (
<>
Expand Down
Loading