-
Notifications
You must be signed in to change notification settings - Fork 799
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
Podcast Player: handle dual-behaviour of render_callback fn #15515
Conversation
Caution: This PR has changes that must be merged to WordPress.com |
This is an automated check which relies on |
The goal is for WP_Block class to be backwards compatible with the $attributes array, so ideally this code wouldn't have to change.
I did a quick bit of research and an option might be for WP_Block to implement the JsonSerializable interface in the same way that it implements ArrayAccess: That would allow this part of the block's code to continue working.
I'm not completely familiar with how this works, but it seems like an unsafe practice to pass all the attributes into
While it might be fine with the current set of attributes, it doesn't seem clear to anyone extending this block in the future that adding additional attributes could have consequences. That said, it's interesting that the intended backwards compatibility for WP_Block doesn't seem to work here (cc @noisysocks, @aduth). |
Thanks, @talldan for your feedback.
It won't work in all cases where the callback argument is called implicitly, without trying to access to either a property (defines object behavior) or tries to access an item (array behavior). The default behavior will be as a class instance.
That's smart. Something to integrate in core, right?
Yes, right. it's something that we would consider to change?
We can improve the documentation in order to let folks be aware of this. |
Co-Authored-By: Konstantin Obenland <obenland@gmx.de>
It's an interesting idea. I guess it speaks to a naivety in thinking that the value was only being used in very predictable array access patterns. It would be interesting to see how far down this road we'd have to go to preserve expected behaviors, both in terms of coverage possible and the remaining risk. For example, another one could be |
That sounds like something that would happen more often than the cases that appeared on this issue. |
I'll probably plan to make an issue or pull request in Gutenberg. I was experimenting with this some more today. There's quite a few other interfaces to consider as well ( |
🤚 Pretty sure Navigation block checks the attribute key using |
confirmed :-/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is triggering quite a few notices for me now:
Undefined index: showEpisodeDescription
Type: PHP Notice Line: 25
File: wp-content/plugins/jetpack-dev/extensions/blocks/podcast-player/templates/podcast-header.php
Undefined index: showCoverArt
Type: PHP Notice Line: 24
File: wp-content/plugins/jetpack-dev/extensions/blocks/podcast-player/templates/podcast-header.php
Undefined variable: block_attributes
Type: PHP Notice Line: 94
File: wp-content/plugins/jetpack-dev/extensions/blocks/podcast-player/podcast-player.php
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cf9902c fixed the block for me.
I created an issue at WordPress/gutenberg#21797 to track the problem and potential solutions. I also created a pull request at WordPress/gutenberg#21798 which explores potential further enhancements to this approach to backward-compatibility. I am hopeful to find some time tomorrow to be able to dedicate to this. It may be worth stating: Because of the incompatibilities observed here, I don't have high confidence that the changes implemented in WordPress/gutenberg#21467 (at least in how they impact |
To clarify, the behavior implemented in WordPress/gutenberg#21467 is not part of Gutenberg 7.9.1, and not (yet) part of any public stable release of the plugin. It is only present in the |
Thanks for the updates 👍 |
WordPress/gutenberg#21868 was merged, which should restore the original behavior of the first argument being passed as a true associative array. In other words, you shouldn't need the changes here anymore. |
I was trying to reproduce the problem and I couldn't. Now I understand why. Thanks, Andrew! |
Closing because of this. |
This PR handles a bug that happens in
7.9.1
version of Gutenberg. These changes were introduced in this PR.In terms of the functionality and how it affects the Podcast Player, what the PR changed is the
valuebehavior of the first argument the callback render function. Now it supports a dual-behavior which could be aWP_Block
object instance, or simply the$attributes
array.It means that you can pick up the block name through of the object property, for instance:
Or use this param as an array (block attributes):
It works as expected as long as the argument is treated explicitly as an array or as an object. The issue happens when we use this value expecting it acts as the attributes array (the current, and unique, the behavior of it so far) but it acts as a WP_Block object instance.
It happens in parts of our code. When it encodes the data to be consumed bt the client, and when it extracts the array items to be used in the server-side templates.
To deal with this, what I suggest is checking if the $attributes is in fact an array, and also checks if the
attributes
property is defined. Take a look at the code to get the idea:Changes proposed in this Pull Request:
Testing instructions:
7.9.1
or laterAlso, you could print the
$attributes
on the server side for different versions of Gutenberg. It should shown something like the following: