-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Try supporting post meta in block attributes #2740
Conversation
dea6042
to
8f9f372
Compare
Codecov Report
@@ Coverage Diff @@
## master #2740 +/- ##
==========================================
+ Coverage 33.77% 33.77% +<.01%
==========================================
Files 189 191 +2
Lines 5640 5930 +290
Branches 982 1053 +71
==========================================
+ Hits 1905 2003 +98
- Misses 3163 3319 +156
- Partials 572 608 +36
Continue to review full report at Codecov.
|
b21264e
to
10c412c
Compare
|
||
### Considerations | ||
|
||
By default, a meta field will be excluded from a post object's meta. This can be circumvented by explicitly making the field visible: |
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.
@pento this would be nice to address in general :)
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.
Yeah, that's annoying.
@rmccue: I don't recall the history for why postmeta is only included in a response when it has the show_in_rest
flag set. (As opposed to at least returning the visible postmeta when the user is appropriately authenticated.)
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.
Meta fields aren't guaranteed to be "safe" by default. "Safe" is a few things, namely safe for JSON serialisation (which is lossy compared to PHP), and safe for capabilities (not all meta is always available).
The current behaviour exists for those reasons. When we initially didn't require show_in_rest
, people noted that this would expose hidden fields created by plugin or users (turns out, a bunch of people use the "Custom Fields" metabox for random internal notes). Solving these problems in a consistent and safe way turned out to be essentially impossible.
show_in_rest
is an indicator that plugin developers are accepting the limitations of the API for their meta fields.
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.
These hidden fields would be shown if you do get_post_meta()
calls, no? It feels like the API should adhere to the expectations of doing such authenticated calls.
editor/selectors.js
Outdated
...block, | ||
attributes: { | ||
...block.attributes, | ||
...reduce( metaAttrs, ( acc, val ) => { |
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.
Can we use full word argument names?
This is looking good to me and opens up a lot of possibilities for block authors, specially those transitioning from meta-boxes. |
docs/attributes.md
Outdated
'single' => true, | ||
) ); | ||
} | ||
add_action( 'init', 'gutenberg_my_block_init' ); |
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.
It may also be worth mentioning that register_meta
treats the type as string
by default, which may be surprising behaviour.
464bbcc
to
27b217b
Compare
6544a10
to
e3e868e
Compare
Undo hack in `setAtttributes`.
Both as exercise and to avoid need for block tests, which would distract from the object of this PR (meta support).
Fixes tests.
That's besides the concept of meta support
29f84f7
to
f8c5a92
Compare
69e19b8
to
8a3b86c
Compare
8a3b86c
to
d6f9296
Compare
We should have added some test cases for the revised
|
}, {} ); | ||
|
||
// Avoid injecting an empty `attributes: {}` | ||
if ( ! block.attributes && ! metaAttributes.length ) { |
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.
I'm confused how this is working, since metaAttributes.length
will always be undefined
, and this will shortcut before merging in attributes. I assume this should have been Object.keys( metaAttributes ).length
.
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.
Also, shouldn't we still want to merge meta attributes even if there are no other attributes of the block?
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.
I'm confused how this is working
Ah, this one fell through the cracks. :( At some point there was a size( metaAttributes )
.
merge meta attributes even if there are no other attributes of the block?
👍
return result; | ||
}, {} ); | ||
|
||
// Avoid injecting an empty `attributes: {}` |
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.
Is there a negative impact of injecting an empty attributes object? I think I might rather we normalize on the empty object.
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.
The intent was to preserve the original behavior (returning an untouched item from blocksByUid
) unless there was any meta. Due to my lack of knowledge, this seemed preferable, in case any consumer of the selector was comparing object references.
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.
The intent was to preserve the original behavior (returning an untouched item from
blocksByUid
) unless there was any meta. Due to my lack of knowledge, this seemed preferable, in case any consumer of the selector was comparing object references.
Yeah, the intent became clearer in #2805 putting together the changes there, and I left the existing behavior. 👍
How does one use a meta attribute value in a (server-rendered) dynamic block? |
@aduth wouldn't that be just |
Wouldn't one expect this to be available in the |
I think there will likely be that expectation, yes. For the first pass, it seemed acceptable to require fetching via Mind you, this availability of data in |
Not sure if expecting from attributes is better than using the standard API for retrieving meta fields — which is more likely to have been used by a theme already. |
Experimental. Not fit to merge.See updated docs for details.