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

Checkbox/toggle control values not returned correctly in get_lzb_meta #185

Closed
edwinwills opened this issue Jan 23, 2021 · 4 comments
Closed

Comments

@edwinwills
Copy link

Issue description:

This is my first time using Lazy Blocks (and it's been a while since I've worked with Wordpress), so apologies if I've missed something obvious here, but I'm getting a bit of an odd issue when I try to use checkboxes/toggles and include their value in a template I'm building - I can't get get_lzb_meta to return the correct checked/unchecked value of a toggle or checkbox. Instead, it seems to return the value of the set up option (should the control be checked/toggled by default).

Lazy Blocks Version:

2.3.0

WordPress Version:

5.6

Any helpful information to reproduce the issue (screenshots, code parts)

To reproduce:

Screenshot 2021-01-23 at 13 57 24
I create a new field "show-down-arrow" as a toggle. I make sure I've checked to include this in Meta. I then use the block on a page, and set the value to checked/toggled.

When I try to inspect the value in PHP in a template file, e.g.

if (get_lzb_meta( 'show-down-arrow' )) {
    echo "TRUE";
} else {
    echo "FALSE";
}

I always get the value of $control['checked'] returned, which seems to just be what the default value of the control should be.

I tried to do some debugging, and it looks like the filter_get_lzb_meta_default function in controls/toggle/index.php is returning the wrong result, specifically:

if ( ! isset( $result ) || ! is_bool( $result ) ) {
    $result = 'true' === $control['checked'];
}

As far as I can tell, this should be checking whether $result is either blank (unchecked) or '1' (checked). Checking the $control['checked'] seems like it's just checking whether the toggle should be "toggled" by default:

Screenshot 2021-01-23 at 14 05 05

I'm happy to have a go at writing a PR if this looks like a bug from your end as well. Thanks

@nk-o
Copy link
Collaborator

nk-o commented Jan 28, 2021

Hey.

If I understand correctly, post meta is not saved properly? Maybe these issues related to yours?

  1. Bug report: Lazyblocks does not update meta fields on publish / update. #137
  2. Block meta attributes and Custom Fields panel conflict WordPress/gutenberg#23078

Please, check it and let me know helped it or not

@edwinwills
Copy link
Author

Afraid not - the data is saved correctly in the database, but the get_lzb_meta function is returning incorrect data (because it's looking to the incorrect database attribute) when returning a value for the checkbox/toggle field.

E.g. with the fix I added above, I got the correct data without having to re-save anything to the database.

@nk-o
Copy link
Collaborator

nk-o commented Jan 28, 2021

This is strange because when I tested it, meta returned correctly for controls with default checked value and without default.

Anyway, this code part (https://github.com/nk-o/lazy-blocks/blob/master/src/controls/toggle/index.php#L87-L108) will be removed in the coming plugin update and your issue should be resolved.

This code part was as a workaround for default meta values. Since WordPress 5.5 we can define a default value for meta using official API and our hack no more needed.

@edwinwills
Copy link
Author

Ah got it - thanks for the help, I'll leave my fix in for now until the update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants