[wp-trac] [WordPress Trac] #60575: Refactor: `data_wp_context` function does not follow WP standards.

WordPress Trac noreply at wordpress.org
Mon Feb 26 01:36:04 UTC 2024


#60575: Refactor: `data_wp_context` function does not follow WP standards.
--------------------------------------+---------------------
 Reporter:  cbravobernal              |       Owner:  (none)
     Type:  defect (bug)              |      Status:  new
 Priority:  normal                    |   Milestone:  6.5
Component:  Editor                    |     Version:  trunk
 Severity:  normal                    |  Resolution:
 Keywords:  needs-patch dev-feedback  |     Focuses:
--------------------------------------+---------------------

Comment (by luisherranz):

 > I'm sure you understand that there can be typos in some quick example
 code

 Maybe it was a typo to you, but that's exactly what I would expect someone
 to write. And it has already happened on several occasions to people
 experimenting with the Interactivity API.

 It's just not obvious that the result of `wp_interactivity_encode_context(
 array( 'someValue' => $some_value ) )` will contain double quotes. Nothing
 indicates that. It's also not obvious that when an attribute value
 contains double quotes, it needs to be surrounded by single quotes. People
 are not usually exposed to that because it's a very rare case. We cannot
 assume that because we know those things, everybody will know. And we
 can't expect that everybody will carefully read the documentation.

 > If someone uses the wrong quotes and their code breaks, they'll realize
 quickly, no?

 I don't think so. It's not that straightforward to discover. And again,
 we've already seen that with people experimenting with the Interactivity
 API who didn't manage to figure out the problem until they showed us the
 code.

 > Why only a helper for data-wp-context and not for others?

 Each directive that contains a JSON string in its value will need one of
 these. Initially, we were going to add `data_wp_interactive` as well, but
 as there is only one available key (`"namespace"`), we added the ability
 to use strings instead of JSON strings.

 These helpers don't need to be exclusive to directives using JSON strings.
 Other future directives that might need some sort of special server
 validation could have helpers as well.

 > Echoing by default doesn't really make sense to me as a) you can easily
 echo a return value but not the other way around, and b) the return use
 case is more common, as existing examples show

 I agree!

 > Someone can do data-wp-context='{ "someValue": "<?php echo $some_value;
 ?>" }' even if there is a nice data_wp_context filter. People will always
 find a way.

 Absolutely. That's the reason I think this should be as easy and
 straightforward as possible, to maximize the chances of people using it.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/60575#comment:15>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list