Make WordPress Core

Opened 10 months ago

Closed 8 months ago

#64518 closed defect (bug) (fixed)

Interactivity API: `data-wp-bind` stops processing valid directives when encountering an invalid one

Reported by: luisherranz Owned by: luisherranz
Priority: normal Milestone: 7.0
Component: Interactivity API Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

When the Interactivity API is processing the data-wp-bind directive on the server using the data_wp_bind_processor method, ​a `return` statement inside a `foreach` loop causes the function to exit entirely when encountering a data-wp-bind directive with an empty suffix or a unique ID. This prevents any subsequent valid bind directives on the same element from being processed.

Steps to Reproduce:

  1. Create an element with both an invalid data-wp-bind directive (no suffix) and a valid one:
<div
  data-wp-interactive="myPlugin"
  data-wp-context='{"id":"some-id"}'
  data-wp-bind="context.id"
  data-wp-bind--id="context.id"
>
  Text
</div>
  1. Process the directives using wp_interactivity_process_directives().

Expected Behavior:

The invalid data-wp-bind (no suffix) should be skipped, and the valid data-wp-bind--id should be processed, resulting in:

<div
  data-wp-interactive="myPlugin"
  data-wp-context='{"id":"some-id"}'
  data-wp-bind="context.id"
  data-wp-bind--id="context.id"
  id="some-id"
>
  Text
</div>

Actual Behavior:

The return statement exits the function when it encounters the invalid directive, so the valid data-wp-bind--id is never processed. The id attribute is not added.

Change History (7)

This ticket was mentioned in ​PR #10746 on ​WordPress/wordpress-develop by ​@luisherranz.


10 months ago
#1

  • Keywords has-patch has-unit-tests added

## What

This PR fixes a bug in the data_wp_bind_processor method where a return statement inside a foreach loop was causing the function to exit entirely when encountering a bind directive with an empty suffix or a unique ID.

## Why

When an element has multiple data-wp-bind directives, such as:

<div data-wp-bind="myPlugin::state.id" data-wp-bind--id="myPlugin::state.id">Text</div>

The first directive (data-wp-bind with no suffix) is invalid and should be skipped. However, the return statement was causing the entire function to exit, preventing the valid data-wp-bind--id directive from being processed.

## How

Changed return to continue so that invalid entries are skipped while valid entries continue to be processed.

---

Trac ticket: https://core.trac.wordpress.org/ticket/64518

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


8 months ago

#3 @juanmaguitar
8 months ago

  • Keywords needs-testing added

From today's scrub

This ticket is in a very good place to be closed. I'll just add the needs-testing keyword and ping @luisherranz about the state of the ticket.

@luisherranz the PR is approved and seems good to go. Do you think we'll be able to close this ticket in time for WP 7.0 RC1 (happening in ~1 week)

Last edited 8 months ago by juanmaguitar (previous) (diff)

#4 @alexodiy
8 months ago

Reviewed and tested PR ​#10746 against trunk r62029, PHP 8.5.1.

The fix is straightforward: return → continue on line 1029 in data_wp_bind_processor(). With return, the entire method exits when it hits a bind directive with an empty suffix, so any valid data-wp-bind--id (or similar) on the same element never gets processed. Changing it to continue skips the bad entry and moves on.

Worth noting that data_wp_class_processor() already handles the same case with continue (line 1093), so this just brings data_wp_bind_processor() in line with the existing pattern.

Tested with a mix of invalid and valid bind directives on the same element — valid ones are now processed as expected. No regressions when all directives are valid.

+1 for commit.

#5 @ozgursar
8 months ago

  • Keywords needs-testing removed

Patch Testing Report

Patch Tested: ​https://github.com/WordPress/wordpress-develop/pull/10746

Environment

  • WordPress: 7.0-beta5-61991-src
  • PHP: 8.2.29
  • Server: nginx/1.29.4
  • Database: mysqli (Server: 8.4.7 / Client: mysqlnd 8.2.29)
  • Browser: Chrome 145.0.0.0
  • OS: macOS
  • Theme: Twenty Twenty-Five 1.4
  • MU Plugins: None activated
  • Plugins:
    • Code Snippets 3.9.5
    • Test Reports 1.2.1

Steps taken

  1. Add the following to functions.php to create a shortcode that reproduces the bug:
add_shortcode('bug_test', function() {
    wp_interactivity_state( 'myPlugin', [
        'id' => 'some-id',
    ]);

    $html = '<div 
        data-wp-bind="myPlugin::state.id" 
        data-wp-bind--id="myPlugin::state.id"
    >
        Text
    </div>';

    return wp_interactivity_process_directives( $html );
});
  1. Add the [bug_test] shortcode to any post and observe the generated HTML source code
  2. Without the patch, the outer <div> is missing the id="some-id" attribute entirely, confirming the bug (the return statement exits data_wp_bind_processor before processing the valid data-wp-bind--id directive)
  3. Apply the patch
  4. Reload the page and view page source again. id="some-id" now appears on the <div>, confirming the valid directive is processed correctly.
  5. ✅ Patch is solving the problem

Screenshots/Screencast with results

Before
https://i.imgur.com/RO4k5wN.png

After
https://i.imgur.com/iKCPgRR.png

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


8 months ago

#7 @luisherranz
8 months ago

  • Owner set to luisherranz
  • Resolution → fixed
  • Status new → closed

In 62070:

Interactivity API: fix data-wp-bind skipping valid directives after encountering an invalid one

Change return to continue in data_wp_bind_processor so that when an element has multiple data-wp-bind directives and one is invalid (empty suffix or unique ID), the invalid entry is skipped instead of causing the entire function to exit, allowing valid directives on the same element to still be processed.

Props DAreRodz, ozgursar, alexodiy.
Fixes #64518.

Note: See TracTickets for help on using tickets.