Automattic / wp-calypso

The JavaScript and API powered WordPress.com
https://developer.wordpress.com
GNU General Public License v2.0
12.4k stars 1.98k forks source link

Patterns: Update serialized pattern content #49404

Open sirreal opened 3 years ago

sirreal commented 3 years ago

When the patterns panel is loaded in the block editor, numerous patterns are updated like in the screenshot below. Stale patterns should be updated regularly so that they're using the most current block markup.

image

Needing to update the blocks every time a stale pattern is rendered is wasteful and bad for performance. Ideally, tool should exist to make it easy to update patterns.

Migrated from PR https://github.com/Automattic/wp-calypso/pull/45420 whose changes are stale. The problem of stale patterns continues to be an issue.

@Automattic/view-engineering

ianstewart commented 3 years ago

Stale patterns should be updated regularly so that they're using the most current block markup.

Given that we use wpcom sites as "source sites" via our pattern toolkit to deliver patterns to the Editor on wpcom … is it possible to do an automated update/publish event on all posts on a source site? Whether that's through a job or command-line trigger?

cc @andrewserong as I think we may have discussed this at some point as a future idea?

ianstewart commented 3 years ago

Stale patterns should be updated regularly so that they're using the most current block markup.

I believe when Gutenbreg block syntax is updated in a publish/update event that the Editor throws alerts to the console. If this is the case it would be good to track that via our Pattern source sites. It may be useful for catching regressions or issues in Gutenberg that we can help fix. In pseudo-code what I mean is …

andrewserong commented 3 years ago

That's a good question @ianstewart — the idea we'd discussed previously was for a scheduled job in PHP to automate sending strings for translation. I'm not too sure what a tool would look like for updating the block content, given that it needs to run in a front-end JS context to run through the deprecations and update the block markup. It'd be great to come up with a solution for that, though, particularly as we scale up the number of patterns available.

@sirreal do you have any thoughts or ideas for how a tool to automate updating a large number of posts would work? Possibly a JS plugin that iteratively loads and updates the blocks?

sirreal commented 3 years ago

The only way I know of updating posts is to load them and save them again in the editor.

Unfortunately, the editor doesn't seem to become "dirty" (save button is disabled) when block updates are applied. So the content changes, but you can't save it until the user makes a change. An alternative may be to save programmatically by dispatching from the browser console.

That sounds like a pretty silly task to do once and it's something that should be repeated so if this is how patterns are expected to be managed for the time being, it makes sense to work on a tool.

Given that everything is browser based, it seems like a Puppeteer or other browser control tool should be used to script the process:

Maybe a minimal browser control script could be combined with some wp-cli output to get rid of a lot of navigating around wp-admin and simply operate on a list of post IDs to load and save them.

Backup the site first 😅

glendaviesnz commented 3 years ago

In theory we could use something similar to the block fixture parsing that we are adding to jetpack blocks

https://github.com/Automattic/jetpack/pull/18643/files#diff-3f90ae76bffe8f72fd44fb123a91f24295beef7c23bc313b577786540456945c

Which can can source post post content, parses it and outputs the migrated version fo the block. A could iterated through post content in a node.js cli via the WP api, parse the post content and then update the post via the api.

.... but the only gotcha is that every block that might be in the content needs to be registered by the setup script, which I am picking might be impossible to achieve with all of the various block plugins that go into wpcom ... so probably not a goer, but might at least explore a little further.

andrewserong commented 3 years ago

Sounds like a good idea to explore! If we need to run in a web context that has access to everything that wpcom registers, another place to put the tool could be a page in MC under patterns-toolkit, and then build an ultra-simple UI for it.

I was wondering how much we can get away with if we set up the editor store, then grab the posts via calling apiFetch or getEntityRecords, then pass it to setupEditor before then calling savePost, and iterate over the list? Even a tool that doesn't update them, but just lists the posts that need updating could be a useful thing to help track which ones need updating.

ramonjd commented 3 years ago

put the tool could be a page in MC under patterns-toolkit, and then build an ultra-simple UI for it.

I can't find anywhere that tell me there's a PHP function that does block validation (parse_blocks) seems to parse any old block. This does not mean that one doesn't exist :)

So if we do it clientside to make use of block validation methods, an MC might be a great way to get started. No idea how much of a time sink it would be. Maybe we could pinch the code from https://github.com/getdave/standalone-block-editor :D ?

glendaviesnz commented 3 years ago

This also needs to include: