| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Client Assets compatibility functions for WordPress 6.9. |
| 5 |
* |
| 6 |
* Hooks into `render_block_data` to detect blocks that support preloading |
| 7 |
* during client side navigation (used by interactivity router). This is addressed in core in: |
| 8 |
* https://github.com/WordPress/wordpress-develop/pull/10357 |
| 9 |
* |
| 10 |
* @package gutenberg |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! method_exists( 'WP_Interactivity_API', 'add_client_navigation_support_to_script_module' ) ) { |
| 14 |
/** |
| 15 |
* Access the shared static variable for interactive script modules. |
| 16 |
* |
| 17 |
* @param string|null $script_module_id The script module ID to register, or null to get the list. |
| 18 |
* @return array<string, true> Associative array of script module ID => true. |
| 19 |
*/ |
| 20 |
function gutenberg_interactive_script_modules_registry( $script_module_id = null ) { |
| 21 |
static $interactive_script_modules = array( |
| 22 |
'@wordpress/block-library/file/view-js-module' => true, |
| 23 |
'@wordpress/block-library/image/view-js-module' => true, |
| 24 |
'@wordpress/block-library/navigation/view-js-module' => true, |
| 25 |
'@wordpress/block-library/query/view-js-module' => true, |
| 26 |
'@wordpress/block-library/search/view-js-module' => true, |
| 27 |
); |
| 28 |
if ( null !== $script_module_id ) { |
| 29 |
$interactive_script_modules[ $script_module_id . '-js-module' ] = true; |
| 30 |
} |
| 31 |
return $interactive_script_modules; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Adds `data-wp-router-options` attribute to script modules registered as interactive. |
| 36 |
* |
| 37 |
* @param array<string, string|true>|mixed $attributes Script attributes. |
| 38 |
* @return array<string, string|true> Filtered script attributes. |
| 39 |
*/ |
| 40 |
function gutenberg_script_module_add_router_options_attributes( $attributes ): array { |
| 41 |
if ( ! is_array( $attributes ) ) { |
| 42 |
return $attributes; |
| 43 |
} |
| 44 |
if ( isset( $attributes['id'] ) && array_key_exists( $attributes['id'], gutenberg_interactive_script_modules_registry() ) ) { |
| 45 |
$attributes['data-wp-router-options'] = wp_json_encode( array( 'loadOnClientNavigation' => true ) ); |
| 46 |
} |
| 47 |
return $attributes; |
| 48 |
} |
| 49 |
add_filter( 'wp_script_attributes', 'gutenberg_script_module_add_router_options_attributes' ); |
| 50 |
|
| 51 |
function gutenberg_script_module_add_load_on_client_navigation_to_script_modules( $parsed_block ) { |
| 52 |
if ( ! isset( $parsed_block['blockName'] ) ) { |
| 53 |
return $parsed_block; |
| 54 |
} |
| 55 |
|
| 56 |
$block_type = WP_Block_Type_Registry::get_instance()->get_registered( $parsed_block['blockName'] ); |
| 57 |
|
| 58 |
$supports_interactivity = isset( $block_type->supports['interactivity'] ); |
| 59 |
$supports_interactivity_array = $supports_interactivity && is_array( $block_type->supports['interactivity'] ); |
| 60 |
$is_fully_interactive = $supports_interactivity && true === $block_type->supports['interactivity']; |
| 61 |
$is_supports_interactive = $supports_interactivity_array && isset( $block_type->supports['interactivity']['interactive'] ) && true === $block_type->supports['interactivity']['interactive']; |
| 62 |
$is_supports_client_navigation = $supports_interactivity_array && isset( $block_type->supports['interactivity']['clientNavigation'] ) && true === $block_type->supports['interactivity']['clientNavigation']; |
| 63 |
|
| 64 |
if ( $is_fully_interactive || ( $is_supports_interactive && $is_supports_client_navigation ) ) { |
| 65 |
foreach ( $block_type->view_script_module_ids as $script_module_id ) { |
| 66 |
gutenberg_interactive_script_modules_registry( $script_module_id ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
return $parsed_block; |
| 71 |
} |
| 72 |
add_action( 'render_block_data', 'gutenberg_script_module_add_load_on_client_navigation_to_script_modules', 10, 1 ); |
| 73 |
} |
| 74 |
|