Commit Graph

26 Commits

Author SHA1 Message Date
czapla
5e72f32f5c Interactivity API: Add wp_interactivity_get_element() function.
Introduces the `wp_interactivity_get_element()` function to the Interactivity API, analogous to the `getElement()` function in the `@wordpress/interactivity` JavaScript module. This function allows access to the current element being processed during directive processing.

The function returns an array containing the `attributes` property, which includes only the originally defined attributes present on the element. Attributes added or modified by directive processing are not included. This is intended for use in derived state properties inside `wp_interactivity_state()`, similar to how `wp_interactivity_get_context()` is used.

Example usage:

```php
wp_interactivity_state( 'myPlugin', array(
    'buttonText' => function() {
        $context = wp_interactivity_get_context();
        $element = wp_interactivity_get_element();
        return isset( $context['buttonText'] )
          ? $context['buttonText']
          : $element['attributes']['data-default-button-text'];
    },
) );
```

Includes unit tests to cover the new functionality.

Props darerodz, swissspidy, cbravobernal, czapla.
Fixes #62136.
Built from https://develop.svn.wordpress.org/trunk@59131


git-svn-id: http://core.svn.wordpress.org/trunk@58527 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-09-30 17:07:13 +00:00
czapla
245519d952 Interactivity API: Move interactivity-router i18n strings to Script Module data.
Moves the 'loading' and 'loaded' i18n strings for the `interactivity-router` to the script module data via the `script_module_data_@wordpress/interactivity-router` filter.

Key changes:

- Add the `filter_script_module_interactivity_router_data()` method, hooked into the `script_module_data_@wordpress/interactivity-router` filter, to set the `i18n` data with the 'loading' and 'loaded' messages.
- Rename the `print_router_loading_and_screen_reader_markup()` method to `print_router_markup()` and remove the screen reader markup from it because it's no longer needed.
- Deprecate the `print_router_loading_and_screen_reader_markup()` method.
- Remove the `loading` and `loaded` strings from the `core/router` store state because they're no longer needed.
- Initialize the `core/router` store with a minimal navigation object to prevent errors in the interactivity-router script module when the store is not properly initialized.
- Update corresponding unit tests to reflect these changes.

This change ensures that the `interactivity-router` i18n messages are localized in a single place and removes the need to initialize them in the `core/router` store state.

Props jonsurrell, swissspidy, czapla, gziolo.
See #60647.
Built from https://develop.svn.wordpress.org/trunk@59130


git-svn-id: http://core.svn.wordpress.org/trunk@58526 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-09-30 16:18:17 +00:00
czapla
d5c6e455e8 Revert [59097] because it was renaming a public method that should be deprecated instead.
Built from https://develop.svn.wordpress.org/trunk@59098


git-svn-id: http://core.svn.wordpress.org/trunk@58494 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-09-26 19:37:22 +00:00
czapla
8786fe5da6 Interactivity API: Move interactivity-router i18n strings to Script Module data.
Moves the 'loading' and 'loaded' i18n strings for the `interactivity-router` to the script module data via the `script_module_data_@wordpress/interactivity-router` filter.

Key changes:

- Add the `filter_script_module_interactivity_router_data()` method, hooked into the `script_module_data_@wordpress/interactivity-router` filter, to set the `i18n` data with the 'loading' and 'loaded' messages.
- Rename the `print_router_loading_and_screen_reader_markup()` method to `print_router_markup()` and remove the screen reader markup from it because it's no longer needed.
- Remove the `loading` and `loaded` strings from the `core/router` store state because they're no longer needed.
- Initialize the `core/router` store with a minimal navigation object to prevent errors in the interactivity-router script module when the store is not properly initialized.
- Update corresponding unit tests to reflect these changes.

This change ensures that the `interactivity-router` i18n messages are localized in a single place and removes the need to initialize them in the `core/router` store state.

Props jonsurrell, swissspidy, czapla. 
See #60647.

Built from https://develop.svn.wordpress.org/trunk@59097


git-svn-id: http://core.svn.wordpress.org/trunk@58493 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-09-26 17:55:13 +00:00
gziolo
0dd5a7eb4e Build: Prepare for more Script Modules
This is a companion to https://github.com/WordPress/gutenberg/pull/65460 that requires syncing in WordPress Core. Namely, the block-library changes require registration with their updated script module IDs so that the blocks continue to work correctly.

They key improvement is script modules registration is handled in one central place, and a combined asset file is used to improve the performance by avoiding multiple disk operations for every individual file.

Props jonsurrell, gziolo, wildworks, noisysocks.
See #60647, #59462.


Built from https://develop.svn.wordpress.org/trunk@59083


git-svn-id: http://core.svn.wordpress.org/trunk@58479 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-09-24 07:35:17 +00:00
luisherranz
77f5a025b2 Interactivity API: Allow server derived state to appear in non-final position
In some cases, derived state returns an associative array. Directives may wish to continue to access properties of the associative array, when using the syntax `state.arrayReturnedByClosure.property`. This patch continues evaluating the path after the associative array has been returned by the Closure.

Props jonsurrell, luisherranz.

Fixes #61741.
Built from https://develop.svn.wordpress.org/trunk@58825


git-svn-id: http://core.svn.wordpress.org/trunk@58221 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-07-29 11:10:23 +00:00
Bernhard Reiter
c89fb9823f Interactivity API: Use Script Modules filter for store & config data.
A dedicated API exists for passing data to Script Modules implemented in changeset [58579].
Use this Core API instead of a custom implementation for Interactivity API to pass data to the client.

Developed in https://github.com/WordPress/wordpress-develop/pull/6683.

Props jonsurrell, gziolo, luisherranz, cbravobernal.
Fixes #61512.
Built from https://develop.svn.wordpress.org/trunk@58729


git-svn-id: http://core.svn.wordpress.org/trunk@58131 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-07-16 09:44:14 +00:00
gziolo
70d2443a48 Interactivity API: Directives cannot derive state on the server
The Interactivity API has a concept of "derived state" but it only worked on the client (JavaScript). This is the implementation that mirrors it, so derived state has good server-side solution.

Props jonsurrell, darerodz, gziolo, luisherranz, cbravobernal.
Fixes #61037.



Built from https://develop.svn.wordpress.org/trunk@58327


git-svn-id: http://core.svn.wordpress.org/trunk@57784 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-06-04 11:01:16 +00:00
gziolo
e978cba1e5 Interactivity API: Print debug warning when server directives processing encounters errors
Aims to improve the developer experience of the Interactivity API server directives processing.

Props cbravobernal, jonsurrell, westonruter, darerodz, czapla, gziolo.
Fixes #61044.


Built from https://develop.svn.wordpress.org/trunk@58321


git-svn-id: http://core.svn.wordpress.org/trunk@57778 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-06-04 07:18:17 +00:00
gziolo
d2a459572e 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.



Built from https://develop.svn.wordpress.org/trunk@58320


git-svn-id: http://core.svn.wordpress.org/trunk@57777 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-06-04 06:58:15 +00:00
gziolo
b23d47efe7 Interactivity API: Move directive processing to WP_Block class
Integrates the directives processing into the WP_Block class. It removes the overhead of running additional hooks when rendering blocks and simplifies the way we detect whether the directive processing should run on an interactive region of the produced final HTML for the blocks.

Introduces `interactivity_process_directives` filter to offer a way to opt out from directives processing. It's needed in Gutenberg: https://github.com/WordPress/gutenberg/pull/62095.

Props gziolo, cbravobernal.
Fixes #61185.


Built from https://develop.svn.wordpress.org/trunk@58234


git-svn-id: http://core.svn.wordpress.org/trunk@57697 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-05-29 11:57:08 +00:00
dmsnell
b5894d595e HTML API: Fix token length bug in Tag Processor.
The Tag Processor stores the byte-offsets into its HTML document where
the current token starts and ends, and also for every bookmark. In some
cases for tags, the end offset has been off by one.

In this patch the offset is fixed so that a bookmark always properly
refers to the full span of the token it's bookmarking. Also the current
token byte offsets are properly recorded.

While this is a defect in the Tag Processor, it hasn't been exposed 
through the public interface and has not affected any of the working
of the processor. Only subclasses which rely on the length of a bookmark
have been potentially affected, and these are not supported environments
in the ongoing work.

This fix is important for future work and for ensuring that subclasses
performing custom behaviors remain as reliable as the public interface.

Developed in https://github.com/WordPress/wordpress-develop/pull/6625
Discussed in https://core.trac.wordpress.org/ticket/61301

Props dmsnell, gziolo, jonsurrell, westonruter.
Fixes #61301.

Built from https://develop.svn.wordpress.org/trunk@58233


git-svn-id: http://core.svn.wordpress.org/trunk@57696 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-05-29 11:42:08 +00:00
dmsnell
a3b5efe883 Improve legibility of JSON-encoded Interactivity API store data.
The Interactivity API has been rendering client data in a SCRIPT element with the
type `application/json` so that it's not executed as a script, but is available
to one. The data runs through `wp_json_encode()` and is encoded with some flags
to ensure that potentially-dangerous characters are escaped.

However, this can lead to some challenges. Eagerly escaping when not necessary
can make the data difficult to comprehend when reading the output HTML. For example,
all non-ASCII Unicode characters are escaped with their code point equivalent.
This results in `\ud83c\udd70` instead of `🅰`.

In this patch, the flags for JSON encoding are refined to ensure what's necessary
while relaxing other rules (leaving in those Unicode characters if the blog charset
is UTF-8). This makes for Interactivity API data that's quicker as a human reader
to decipher and diagnose.

In summary:

 - This data is JSON encoded and printed in a `<script type="application/json">` tag.

 - If we ensure that `<` is never printed inside the data, it should be impossible to
   break out of the script tag and the browser treats everything as the element's `textContent`.

 - All other escaping becomes unnecessary at that point, including unicode escaping 
   if the page uses the UTF-8 charset (the same encoding as JSON).

See https://github.com/WordPress/wordpress-develop/pull/6433#pullrequestreview-2043218338

Developed in https://github.com/WordPress/wordpress-develop/pull/6520
Discussed in https://core.trac.wordpress.org/ticket/61170

Fixes: #61170
Follow-up to: [57563].
Props: bjorsch, dmsnell, jonsurrell, sabernhardt, westonruter.

Built from https://develop.svn.wordpress.org/trunk@58159


git-svn-id: http://core.svn.wordpress.org/trunk@57622 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-05-15 17:42:10 +00:00
gziolo
6edbf28fec Interactivity API: Cannot be used from wp-admin
Interactivity API has hooks that run on the frontend, but not in wp-admin. This means that interactivity data was not printed to the HTML and the interactivity Script Modules were not registered.

Fixes #61087.
Props jonsurrell, cbravobernal, gziolo.


Built from https://develop.svn.wordpress.org/trunk@58127


git-svn-id: http://core.svn.wordpress.org/trunk@57592 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-05-10 09:13:05 +00:00
Pascal Birchler
bf346251a3 Interactivity API: Do not print state if it’s an empty array.
This prunes stores and configurations that are empty arrays, as stores are expected to be JSON objects.
By not printing empty configurations, less redundant data is serialized into the HTML.

Props jonsurrell, luisherranz, darerodz, gziolo, swissspidy.
Fixes #60761.
Built from https://develop.svn.wordpress.org/trunk@57841


git-svn-id: http://core.svn.wordpress.org/trunk@57342 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-03-15 12:12:09 +00:00
Pascal Birchler
78ea9f4ee6 Interactivity API: Prevent warning when using a bind directive with a short attribute name.
Adds new tests and improves existing ones by using `assertSame` to do type comparison as well.

Props jonsurrell, cbravobernal, swissspidy, gziolo.
Fixes #60758.
Built from https://develop.svn.wordpress.org/trunk@57835


git-svn-id: http://core.svn.wordpress.org/trunk@57336 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-03-14 15:30:09 +00:00
Pascal Birchler
0a308a97bb Interactivity API: Do not propagate context from void tags to its siblings.
Resolves an issue where context on a void tag element such as `<img>` was incorrectly passed to following elements.
Adds tests.

Props santosguillamot, luisherranz, cbravobernal, dmsnell, gziolo, swissspidy.
Fixes #60768.
Built from https://develop.svn.wordpress.org/trunk@57832


git-svn-id: http://core.svn.wordpress.org/trunk@57333 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-03-14 13:34:13 +00:00
Pascal Birchler
858da6cbc5 Interactivity API: Increase hook priority for processing directives.
Use a priority of 100 to ensure that other filters can add additional directives before the processing starts.
This way, directives will be processed even if the `$parsed_block` variable is edited by a filter.

Props cbravobernal, swissspidy, flixos90, joemcgill, gziolo.
Fixes #60743.
Built from https://develop.svn.wordpress.org/trunk@57826


git-svn-id: http://core.svn.wordpress.org/trunk@57327 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-03-13 10:48:11 +00:00
Pascal Birchler
9958729416 Interactivity API: Do not process directives when there aren’t any.
Short-circuits directive processing when the markup does not actually contain any `data-wp-*` attributes.
This reduces function calls and memory usage for the best case scenario due to not involving `WP_HTML_Tag_Processor`.

Props joemcgill, swissspidy, gziolo, cbravobernal, flixos90.
Fixes #60749.
Built from https://develop.svn.wordpress.org/trunk@57824


git-svn-id: http://core.svn.wordpress.org/trunk@57325 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-03-13 10:41:14 +00:00
audrasjb
31569e11d0 Docs: Add missing @global mention in wp_interactivity().
This merges a changeset updated upstream in the Gutenberg repository.
See https://github.com/WordPress/gutenberg/pull/59522.

Props wildworks.
Fixes #60677.
See #59651.




Built from https://develop.svn.wordpress.org/trunk@57774


git-svn-id: http://core.svn.wordpress.org/trunk@57275 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-03-05 13:39:13 +00:00
Pascal Birchler
f98a921c98 Interactivity API: Rename data_wp_context() to wp_interactivity_data_wp_context().
Increases clarity about where the function belongs to, bringing it in line with other related functions.

After initially merging this change in [57742] and reverting it in [57743], this reintroduces it now that the Gutenberg packages have been updated accordingly in [57760].

Props swissspidy, gziolo, cbravobernal, youknowriad, ankitmaru, westonruter, luisherranz, darerodz.
Fixes #60575.
Built from https://develop.svn.wordpress.org/trunk@57762


git-svn-id: http://core.svn.wordpress.org/trunk@57263 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-03-04 15:39:15 +00:00
Pascal Birchler
e265c6d012 Interactivity API: Revert [57742] pending a Gutenberg package update.
This function can only be renamed after updating Gutenberg npm packages, as some of the core blocks already use this function.

See #60575.
Built from https://develop.svn.wordpress.org/trunk@57743


git-svn-id: http://core.svn.wordpress.org/trunk@57244 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-29 14:30:12 +00:00
Pascal Birchler
fbeaad601c Interactivity API: Rename data_wp_context() to wp_interactivity_data_wp_context().
Increases clarity about where the function belongs to, bringing it in line with other related functions.

Props swissspidy, gziolo, cbravobernal, youknowriad, ankitmaru, westonruter, luisherranz, darerodz.
Fixes #60575.
Built from https://develop.svn.wordpress.org/trunk@57742


git-svn-id: http://core.svn.wordpress.org/trunk@57243 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-29 12:28:09 +00:00
Pascal Birchler
41532d5638 Interactivity API: Skip instead of bail out if HTML contains SVG or MATH.
Addresses an issue with server-side processing of directives when there is e.g. an SVG icon a navigation menu.

Props cbravobernal, westonruter, dmsnell, swissspidy.
Fixes #60517.
Built from https://develop.svn.wordpress.org/trunk@57649


git-svn-id: http://core.svn.wordpress.org/trunk@57150 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-17 15:28:04 +00:00
Pascal Birchler
61f8789d9f Interactivity API: Use string instead of object in data-wp-interactive attribute.
The server directive processing, integrated in [57563], supports a simplified format for passing the namespace to `data-wp-interactive`.

Props cbravobernal, gziolo.
Fixes #60542.
See #60356.
Built from https://develop.svn.wordpress.org/trunk@57646


git-svn-id: http://core.svn.wordpress.org/trunk@57147 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-17 13:19:16 +00:00
gziolo
802c251971 Interactivity API: Integrate Server Directive Processing
The Interactivity API enables WordPress developers to create dynamic and interactive web experiences with ease using a set of special HTML attributes called directives. Please refer to the [Interactivity API proposal](https://make.wordpress.org/core/2023/03/30/proposal-the-interactivity-api-a-better-developer-experience-in-building-interactive-blocks/) for further details. 

It syncs the changes from the Gutenberg plugin: https://github.com/WordPress/gutenberg/pull/58066.

Fixes #60356.
Props luisherranz, jonsurrell, swissspidy, westonruter, gziolo.


Built from https://develop.svn.wordpress.org/trunk@57563


git-svn-id: http://core.svn.wordpress.org/trunk@57064 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-02-08 09:13:09 +00:00