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

5.8.0-beta4: Plugin calls get_option() and performs DB queries for empty/missing fields assigned to a block during registration #150

Closed
macemmek opened this issue Mar 14, 2019 · 4 comments

Comments

Projects
None yet
2 participants
@macemmek
Copy link

commented Mar 14, 2019

Scared with hundreds of database queries reported by Query Monitor on my website I have made some tests and it seems that ACF is calling get_option() every time when the block inserted into the content does not actually contain the field keys predicted during registration via acf_add_local_field_group().
It happens, for example, when I save an empty block (without filling inputs) or a block with hidden/filtered-out fields.

So when I add an empty block which looks within the code like:

<!-- wp:acf/header {"id":"block_5c8ac410e890e","name":"acf/header","align":"","mode":"edit"} /-->

the plugin looks for the missing fields in wp_options table, for example:

SELECT option_value
FROM wp_options
WHERE option_name = 'block_5c8ac410e890e_my-header-content'
LIMIT 1

where my-header-content is the name of a field type group in this case.

It's quite surprising for me - why ACF thinks that I store any block data in wp_options?

Then, when I add some text into the subfield of my-header-content field, the plugin no longer queries wp_options for my-header-content and the block structure grows into:

<!-- wp:acf/header {"id":"block_5c8ac410e890e","data":{"field_my-header-content":{"field_my-header-content-lead":"This is our lead","field_my-header-content-text":""},"field_my-header-aside":{"field_my-header-aside-settings":""},"field_my-header-background":{"field_my-header-background-image":"","field_my-header-background-video":"","field_my-header-background-color":"white","field_my-header-background-has-pattern":"0"}},"name":"acf/header","align":"","mode":"edit"} /-->

which is valid. (Actually, it calls get_option() for other missing subfields like those under my-header-aside-settings, until I fill them)

@macemmek macemmek changed the title 5.8.0-beta4: When the block inserted into the content does not contain all its registered fields, get_option() is called and DB is queried in search of such fields 5.8.0-beta4: Plugin calls get_option() and performs DB queries for empty/missing fields assigned to a block during registration Mar 14, 2019

@elliotcondon

This comment has been minimized.

Copy link
Contributor

commented Mar 15, 2019

Hi @macemmek

Thanks for the report. This will be due to an oversight in the "local-meta" class.
I'll add this to the to-do list and get this fixed shortly :)

@elliotcondon

This comment has been minimized.

Copy link
Contributor

commented Mar 18, 2019

I've found and fixed the issue, and will release an update shortly.

@macemmek

This comment has been minimized.

Copy link
Author

commented Mar 18, 2019

Thank you @elliotcondon for the fix.

I wonder if I can apply a quick patch to the local code before the next version is released or the changes are too broad?

@elliotcondon

This comment has been minimized.

Copy link
Contributor

commented Mar 18, 2019

@macemmek Sure thing. The fix is simple.

Please edit the "includes/local-meta.php" file on line 204 and change

if( isset($this->meta[ $post_id ][ $name ]) ) {
    return $this->meta[ $post_id ][ $name ];
}

to:

if( isset($this->meta[ $post_id ]) ) {
	if( isset($this->meta[ $post_id ][ $name ]) ) {
		return $this->meta[ $post_id ][ $name ];
	} else {
		return false;
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.