AdvancedCustomFields / acf

Advanced Custom Fields
http://advancedcustomfields.com/
823 stars 168 forks source link

ACF post preview and revisions completely broken #903

Open tripflex opened 3 months ago

tripflex commented 3 months ago

Describe the bug The internal handling of ACF for revisions and previews seems to be completely broken in numerous ways when using the default WordPress Gutenberg editor. Any field groups created and attached to a post, either do not show at all when attempting to preview, or if using a workaround to remove the filter, they show values from the last "published" post.

TLDR; Preview does not work correctly for ACF fields if using the default WordPress Gutenburg editor

1.) ACF does not handle revisions correctly, and is using the revision ID when you call get_field (see end of issue for details), ultimately causing nothing to show because metadata does not exist on the revision post ID.

2.) Even with revisions disabled, preview does not work correctly which probably stems from the same issue above, and does not show anything on preview either.

To Reproduce Steps to reproduce the behavior:

  1. Create a blank WordPress install
  2. Create a new field group, leave the defaults to show when post type is Post
  3. Add any kind of field (text, repeater, etc)
  4. Create a new post
  5. Add [acf field="some_field_value"] shortcode to output on the page
  6. Set any random value in the ACF field created above
  7. Publish
  8. Edit post, change the ACF field value to something different
  9. Click Computer icon on top right and select Preview in new tab
  10. Preview will load, and either show nothing or show the old value

Expected behavior The value updated when editing to show correctly on the preview

Basic setup and functional when published: image image image

Now change the field value: image

Click preview in new tab: image

Nothing shows: image

Version Information:

Additional context

As a WordPress expert and seasoned plugin developer, here's what I found.

The problem exists in acf_get_valid_post_id which calls the acf/validate_post_id filter, which then is filtered in acf_revisions::acf_validate_post_id who then calls acf_get_post_latest_revision which returns the revision ID.

The problems stems from the fact that ACF does not store meta on the revision, but for some reason, it's specifically changing Post ID's to the revision ID, ultimately causing the function call to get_field to call get_metadata using the revision post ID, but the only meta that will ever exist on it is something like this:

[ '_acf_changed' => [ 0 => "1" ] ]

My assumption is this code was meant for the pre-Gutenburg era, as I also found that when WordPress automatically calls the autosaves REST endpoint (every 60 seconds or when you click Preview in new tab), ACF does not have any handling in there to pass any values that have been changed, and since they are not one of the default fields post_title, post_content, excerpt, etc nothing gets saved, and as such, ACF is trying to pull the value from the revision (or autosave if revisions disabled) that will never have anything on it, as ACF has specifically set to only allow _acf_changed as the only valid meta for revisions/autosaves.

Basically it seems that preview only works with ACF if you specifically force the site to use the classic editor. I've tested this on a completely blank install and replicated it numerous times.

Numerous previous issues opened regarding this: https://github.com/AdvancedCustomFields/acf/issues/411 https://github.com/AdvancedCustomFields/acf/issues/186 https://github.com/AdvancedCustomFields/acf/issues/184

Gutenburg related issue: https://github.com/WordPress/gutenberg/issues/16006

Regarding Gutenburg issue ... it seems they have no plans to support passing meta fields, so this should be something added/handled via ACF as it's been YEARS and still no resolution. Being as though ACF is so widely used, it makes the most sense that this should be resolved in ACF now that Gutenburg is the default editor -- switching to classic editor is just not an option for some.

lgladdy commented 3 months ago

Hey there,

We're aware of this issue, and there are a few long standing issues in this repo for it.

We have a plan to resolve this issue, but it requires us to remove the legacy meta box and transition to a whole new system for updating ACF fields in the block editor, this is a long term project for us.

We'll keep you updated on our progress over the next several releases of ACF.

tripflex commented 3 months ago

Hey there,

We're aware of this issue, and there are a few long standing issues in this repo for it.

We have a plan to resolve this issue, but it requires us to remove the legacy meta box and transition to a whole new system for updating ACF fields in the block editor, this is a long term project for us.

We'll keep you updated on our progress over the next several releases of ACF.

What can I do to help? I'm happy to do some development work if you can point me in the right direction. Thanks!

ConnorWedia commented 2 months ago

I am also experiencing an issue similar to the above where if I add some text and let it autosave (without modifying any ACF fields), when I go to revisions and view the autosave, ALL ACF fields get removed.

Paragraph added for autosave test:

added_paragraph

Autosave Response result: autosave_response

Revision of the autosave, showing the removed ACF fields:

removed_acf_fields

Is there ANY fix for this please as our content writers are constantly re-writing & re-adding ACF content whenever the post autosaves.

Thanks

lgladdy commented 2 months ago

@ConnorWedia Just to clarify, is this a recent change with an ACF or WordPress update, or has this always been the case for you in the block editor?

ConnorWedia commented 2 months ago

@lgladdy The issue has only been happening for a couple of weeks now, and we have since updated Wordpress to 6.5 (thinking it was related), PHP to 8.3, and obviously latest version of ACF.

lgladdy commented 2 months ago

Thanks @ConnorWedia - I'll raise it with the team here today and see the updates we made to fix (some) revisions made it worse for autosaves.

I will also note, we're prioritising a more immediate fix to this issue now given how recent WordPress updates have made things much worse for revisions and autosaves with ACF metadata.

phil-sola commented 1 month ago

@lgladdy is there any movement on previews and revisions with ACF Fields?

It works for me always after the first time, but the first time I hit preview after changing some ACF flexible fields, it never displays. Super frustrating!