[wp-trac] [WordPress Trac] #61039: Interactivity API: Some property access does not work well in server directives

WordPress Trac noreply at wordpress.org
Thu Apr 18 15:49:04 UTC 2024


#61039: Interactivity API: Some property access does not work well in server
directives
-------------------------+-----------------------------
 Reporter:  jonsurrell   |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 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
 <?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.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/61039>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list