Skip to content
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

Post Title: Implement debounced title saving and fix title retrieval for post context #68431

Closed

Conversation

yogeshbhutkar
Copy link
Contributor

What, Why & How?

As noted in the file packages/block-library/src/post-title/index.php, the context's Post ID was removed from the get_the_title() function. However, I believe the context's Post ID should be utilized to make the block functional even outside the Query Loop block.

To address the issue of unsynced previews, I’ve implemented a solution that directly dispatches changes to the post-title block. This ensures that updates appear in the preview tab, providing a consistent and synced experience.

That said, directly dispatching changes in previews might not always align with the purpose of previews, which are meant to reflect unsaved changes. However, given that significant modifications to the core/post-title block are rare, this approach could be acceptable as it prioritizes a fully synced and cohesive look.

Testing Instructions

  1. Create a new block that accepts inner blocks.
  2. Copy this to its block.json
"attributes": {
    "postId": {
	    "type": "integer",
	    "default": 165 // A post ID for a post that already exists.
    },
    "postType": {
	    "type": "string",
	    "default": "post"
    }
  },
"providesContext": {
  "postId": "postId",
  "postType": "postType"
},
  1. Copy this to edit.js
/**
 * WordPress dependencies
 */
import { useBlockProps, useInnerBlocksProps } from '@wordpress/block-editor';

function Edit( { attributes, setAttributes } ) {
	const blockProps = useBlockProps();
	const innerBlocksProps = useInnerBlocksProps();

	return (
		<div { ...blockProps }>
			<div { ...innerBlocksProps } />
		</div>
	);
}

export default Edit;
  1. Copy this to save.js
/**
 * WordPress dependencies
 */
import { useBlockProps, InnerBlocks } from '@wordpress/block-editor';

export default function save() {
	const blockProps = useBlockProps.save();
	return (
		<div { ...blockProps }>
			<InnerBlocks.Content />
		</div>
	);
}
  1. Try inserting core/post-title block as an inner block.
  2. Verify it functions normally in the preview and works outside the Query Loop.

Screencast

Before

Screen.Recording.2024-12-31.at.12.04.52.PM.mov

After

Screen.Recording.2024-12-31.at.12.02.25.PM.mov

Closes: #68369

@yogeshbhutkar yogeshbhutkar changed the title Editor: Implement debounced title saving and fix title retrieval for post context Post Title: Implement debounced title saving and fix title retrieval for post context Dec 31, 2024
@yogeshbhutkar yogeshbhutkar marked this pull request as ready for review December 31, 2024 07:46
Copy link

github-actions bot commented Dec 31, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @0x8sksr, @ivarsm.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Unlinked contributors: 0x8sksr, ivarsm.

Co-authored-by: yogeshbhutkar <[email protected]>
Co-authored-by: hbhalodia <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: rinkalpagdar <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@@ -50,12 +51,20 @@ export default function PostTitleEdit( {
},
[ isDescendentOfQueryLoop, postType, postId ]
);
const [ rawTitle = '', setTitle, fullTitle ] = useEntityProp(
const [ rawTitle = '', , fullTitle ] = useEntityProp(
Copy link
Contributor

Choose a reason for hiding this comment

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

I am seeing 2 commas here. Is this intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. The second value, setTitle, is no longer needed, so it is skipped in the destructuring assignment.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh yes, got that. Thank You,

@Mamaduka Mamaduka added [Type] Bug An existing feature does not function as intended [Block] Post Title Affects the Post Title Block labels Jan 6, 2025
@Mamaduka
Copy link
Member

Mamaduka commented Jan 6, 2025

Thanks for working on this, @yogeshbhutkar!

I don't think using the saveEntityRecord action is the right choice here (or in any other block). It will persist with any changes the user makes to the post, creating an illusion of a fix. Autosaves don't work this way. IIRC, the original PR has some details on how they work.

Changes to a post or page should not persist without the user's direct action — saving a post.

@Mamaduka Mamaduka added the [Status] Blocked Used to indicate that a current effort isn't able to move forward label Jan 6, 2025
@Mamaduka
Copy link
Member

Mamaduka commented Jan 6, 2025

@yogeshbhutkar, let's continue the discussion on possible solutions in the issue.

@yogeshbhutkar
Copy link
Contributor Author

Sure, thanks @Mamaduka.

I thought this wouldn't be a practical solution as well, but, I don't think it's possible to keep it synced with the previews until and unless we manually update the post title when changed. I'll probably need to put some more thought into this, post that, I'll put my findings into the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Post Title Affects the Post Title Block [Status] Blocked Used to indicate that a current effort isn't able to move forward [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

core/post-title not showing the correct title in dynamic block when used outside loop with postId context
3 participants