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

Add hook for block deletion so post meta can be removed #5626

Closed
diegoliv opened this issue Mar 15, 2018 · 9 comments
Closed

Add hook for block deletion so post meta can be removed #5626

diegoliv opened this issue Mar 15, 2018 · 9 comments
Labels
[Feature] Block API API that allows to express the block paradigm.
Milestone

Comments

@diegoliv
Copy link

Issue Overview

When you create a block that have a attribute set as meta, Gutenberg saves this same attribute as post meta, which is expected. However, if you remove the block and save the post, the post meta is still stored on the database.

Steps to Reproduce (for bugs)

  1. Create a custom block, and set any random attribute as source: 'meta'.
  2. Change the attribute value and save the post.
  3. Check on the frontend, using get_post_meta, the value of the post meta from that specific block.
  4. Now go back to the editor, remove the block and update the post.
  5. Go back to the frontend, and check again the value of the post meta. It is still there.

Expected Behavior

When the block is deleted, the post meta should be deleted as well. Unless there is another block of the same type for the post. On this case, post meta should be treated on a block basis, not on a post basis (related to #5599)

Current Behavior

Post meta is stored when you add one block, changes when you change the attribute on any instance of the block for the respective post, but it is not removed from the database if you remove all block instances.

Related Issues and/or PRs

#5599

@jeffpaul jeffpaul added the [Type] Enhancement A suggestion for improvement. label Mar 21, 2018
@karmatosed karmatosed added [Type] Bug An existing feature does not function as intended and removed [Type] Enhancement A suggestion for improvement. labels Apr 27, 2018
@ocean90
Copy link
Member

ocean90 commented May 2, 2018

See #4054 for previous discussion.

If the editor is not going to remove/reset them to default (see #6505) by default, there needs at least a hook on block delete that can be used to perform additional cleanups like deleting post meta.

@ocean90 ocean90 added the [Feature] Block API API that allows to express the block paradigm. label May 2, 2018
@Luehrsen
Copy link
Contributor

I would like to add importance to this issue. We are using blocks to control the existence of certain metadata (e.g. control template pieces outside of 'the_content()')

AFAIK there is currently no way to force the existance of a certain block, so the the problem is that we will have metadata existing without an interface to edit them. That is less than optimal.

@Luehrsen
Copy link
Contributor

Luehrsen commented May 25, 2018

https://gist.github.com/Luehrsen/41d27f30eaca2d997a84898271c004c7

This is my current implementation. It works, but it has some issues. Sometimes the actions fire twice, sometimes I get a strange error from the rest API. (See screenshot)

But so far I am quite happy with the progress.

bildschirmfoto 2018-05-25 um 16 16 58

@danielbachhuber danielbachhuber changed the title Post meta is not removed when block is deleted Add hook for block deletion so post meta can be removed Jun 4, 2018
@danielbachhuber danielbachhuber added this to the WordPress 5.0 milestone Jun 4, 2018
@danielbachhuber
Copy link
Member

Re-titled issue as "Add hook for block deletion so post meta can be removed" to clarify its direction.

@aduth
Copy link
Member

aduth commented Jun 18, 2018

To me, ideally this is not something which occurs in the editor itself, as it's very possible someone may remove the block without interfacing with Gutenberg (by database, or wp_update_post). With server registration of blocks (#2751), it seems reasonable that in the updating of a post's content, we could detect whether a block is being removed which sources some attribute value from post meta, then delete the associated post meta from the server (admittedly only addresses wp_update_post, and not database manipulation, but a step in the right direction).

@hgezim
Copy link

hgezim commented Dec 13, 2018

Any updates on this?

@mitogh
Copy link
Member

mitogh commented Dec 13, 2018

As a workaround of this I used componentWillUnMount and trigger an action there.

@gziolo
Copy link
Member

gziolo commented Feb 1, 2019

Indeed, it makes a lot of sense what @aduth shared. Even if we would land #7325 proposed by @Luehrsen, this issue would still be present if someone uses REST API to remove one of the blocks. Given that mobile team is working on Gutenberg integration for the mobile app this issue will become even more prominent.

I'm wondering if the root issue shouldn't be moved to WordPress core now as the logic for saving blocks is there.

@youknowriad
Copy link
Contributor

I'm going to close this now as a potential solution for this can't come from the block editor itself.
Please consider opening a trac ticket about this if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm.
Projects
None yet
Development

No branches or pull requests