Make WordPress Core

Opened 5 weeks ago

Last modified 2 days ago

#61039 new enhancement

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

Reported by: jonsurrell's profile jonsurrell Owned by:
Milestone: 6.6 Priority: normal
Severity: normal Version: 6.5
Component: Interactivity API Keywords: has-patch
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 (6)

#1 @jonsurrell
5 weeks 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
5 weeks 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
4 weeks 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
3 weeks ago

  • Component changed from Editor to Interactivity API

#5 @oglekler
4 days ago

  • Keywords needs-patch added

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


2 days ago
#6

  • Keywords has-patch added; needs-patch removed
Note: See TracTickets for help on using tickets.