Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#61039 closed enhancement (fixed)

Interactivity API: Some property access does not work well in server directives

Reported by: jonsurrell's profile jonsurrell Owned by: gziolo's profile gziolo
Milestone: 6.6 Priority: normal
Severity: normal Version: 6.5
Component: Interactivity API Keywords: has-patch has-unit-tests commit
Focuses: Cc:

Description

The Interactivity API uses .-delimited paths to describe where data lives in interactivity state and context.

In my testing, associative and numeric arrays are well handled in PHP. PHP objects can be serialized in state and used by the client as JavaScript objects, but directive processing on the server throws an error. For example:

<?php
$obj = new stdClass();
$obj->prop = 'property';
wp_interactivity_state('ns', array(
        'obj' => $obj,
));
?>
<div data-wp-interactive="ns">
        <div data-wp-text="state.obj.prop"></div>
</div>

Throws:

Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /var/www/html/wp-includes/interactivity-api/class-wp-interactivity-api.php:397
Stack trace:
#0 /var/www/html/wp-includes/interactivity-api/class-wp-interactivity-api.php(787): WP_Interactivity_API->evaluate('state.obj.prop', 'ns', false)
#1 /var/www/html/wp-includes/interactivity-api/class-wp-interactivity-api.php(351): WP_Interactivity_API->data_wp_text_processor(Object(WP_Interactivity_API_Directives_Processor), 'enter', Array, Array, Array)
#2 /var/www/html/wp-includes/interactivity-api/class-wp-interactivity-api.php(229): WP_Interactivity_API->process_directives_args('process_directives('apply_filters('render()
#9 /var/www/html/wp-includes/blocks.php(1743): render_block(Array)
#10 /var/www/html/wp-includes/class-wp-hook.php(324): do_blocks('

Arrays and objects are accessed without issue in JavaScript. It would be nice if property access in PHP would access object properties or associative array values correctly.

Change History (17)

#1 @jonsurrell
2 years ago

wpdb::get_results by default returns an array (the rows) of objects by default. That's one example where this type of property access would be handy.

Using the $wpdb->get_results( …, ARRAY_A ) does provide associative arrays for each row which can be accessed from the Interactivity API without issues.

#2 @gziolo
2 years ago

  • Component changed from General to Editor
  • Milestone changed from Awaiting Review to 6.6
  • Version set to 6.5

Would it be possible to automatically cast array to object or the other way around so directive processing can always work with one type of data mirroring how JS works? For example enconding and decoding as JSON could do the trick.

#3 @jonsurrell
2 years ago

Would it be possible to automatically cast array to object or the other way

I had an implementation that seemed to work that checked is_array() and used either $arr[$prop] or $obj->$prop access. It seemed to work well and I think that should be sufficient.

Another solution would be get_object_vars(), but I think we'd still have a conditional check and would be creating full arrays we don't really need.

#4 @jonsurrell
2 years ago

  • Component changed from Editor to Interactivity API

#5 @oglekler
2 years ago

  • Keywords needs-patch added

This ticket was mentioned in PR #6581 on WordPress/wordpress-develop by @narenin.


2 years ago
#6

  • Keywords has-patch added; needs-patch removed

This ticket was mentioned in Slack in #core by nhrrob. View the logs.


2 years ago

This ticket was mentioned in PR #6672 on WordPress/wordpress-develop by @cbravobernal.


2 years ago
#8

  • Keywords has-unit-tests added

@czapla commented on PR #6672:


2 years ago
#9

It looks good to me, too! Nice work 👍

@gziolo commented on PR #6672:


2 years ago
#10

@cbravobernal, is this patch ready to land? https://github.com/WordPress/wordpress-develop/pull/6672#discussion_r1622282621 – any actions to take here?

@cbravobernal commented on PR #6672:


2 years ago
#11

@cbravobernal[[Image(chrome-extension://hgomfjikakokcbkjlfgodhklifiplmpg/images/wp-logo.png)]], is this patch ready to land? #6672 (comment) – any actions to take here?

I removed the bool type and added #[\ReturnTypeWillChange] at the beginning of the function like you mentioned to keep consistency with other parts of the code. Let's see the tests.

#12 @gziolo
2 years ago

  • Keywords commit added

@cbravobernal commented on PR #6672:


2 years ago
#13

All green so ready to land!

#14 @gziolo
2 years ago

  • Owner set to gziolo
  • Resolution set to fixed
  • Status changed from new to closed

In 58320:

Interactivity API: Some property access does not work well in server directives

Ensures property access in PHP works for object properties or associative array values correctly when processing Interactivity API directives.

Props narenin, cbravobernal, jonsurrell, gziolo, czapla.
Fixes #61039.

@jonsurrell commented on PR #6581:


2 years ago
#16

The work here was landed as part of https://core.trac.wordpress.org/changeset/58320. This PR can be closed. Thanks @narenin!

@narenin commented on PR #6581:


2 years ago
#17

@sirreal Thanks for the update. I have closed the PR.

Note: See TracTickets for help on using tickets.