Merged in feature/81-dev-dev01 (pull request #5)
auto-patch 81-dev-dev01-2023-12-05T22_45_26 * auto-patch 81-dev-dev01-2023-12-05T22_45_26
This commit is contained in:
@@ -65,87 +65,69 @@ if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) {
|
||||
|
||||
return $menu_items_by_parent_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Interactivity API directives to the navigation-submenu and page-list blocks markup using the Tag Processor
|
||||
* The final HTML of the navigation-submenu and the page-list blocks will look similar to this:
|
||||
*
|
||||
* <li
|
||||
* class="has-child"
|
||||
* data-wp-context='{ "core": { "navigation": { "isMenuOpen": false, "overlay": false } } }'
|
||||
* data-wp-effect="effects.core.navigation.initMenu"
|
||||
* data-wp-on.keydown="actions.core.navigation.handleMenuKeydown"
|
||||
* data-wp-on.focusout="actions.core.navigation.handleMenuFocusout"
|
||||
* >
|
||||
* <button
|
||||
* class="wp-block-navigation-submenu__toggle"
|
||||
* data-wp-on.click="actions.core.navigation.openMenu"
|
||||
* data-wp-bind.aria-expanded="context.core.navigation.isMenuOpen"
|
||||
* >
|
||||
* </button>
|
||||
* <span>Title</span>
|
||||
* <ul class="wp-block-navigation__submenu-container">
|
||||
* SUBMENU ITEMS
|
||||
* </ul>
|
||||
* </li>
|
||||
*
|
||||
* @param string $w Markup of the navigation block.
|
||||
* @param array $block_attributes Block attributes.
|
||||
*
|
||||
* @return string Submenu markup with the directives injected.
|
||||
*/
|
||||
function block_core_navigation_add_directives_to_submenu( $w, $block_attributes ) {
|
||||
while ( $w->next_tag(
|
||||
array(
|
||||
'tag_name' => 'LI',
|
||||
'class_name' => 'has-child',
|
||||
)
|
||||
) ) {
|
||||
// Add directives to the parent `<li>`.
|
||||
$w->set_attribute( 'data-wp-interactive', true );
|
||||
$w->set_attribute( 'data-wp-context', '{ "core": { "navigation": { "isMenuOpen": { "click": false, "hover": false }, "overlay": false } } }' );
|
||||
$w->set_attribute( 'data-wp-effect', 'effects.core.navigation.initMenu' );
|
||||
$w->set_attribute( 'data-wp-on--focusout', 'actions.core.navigation.handleMenuFocusout' );
|
||||
$w->set_attribute( 'data-wp-on--keydown', 'actions.core.navigation.handleMenuKeydown' );
|
||||
if ( ! isset( $block_attributes['openSubmenusOnClick'] ) || false === $block_attributes['openSubmenusOnClick'] ) {
|
||||
$w->set_attribute( 'data-wp-on--mouseenter', 'actions.core.navigation.openMenuOnHover' );
|
||||
$w->set_attribute( 'data-wp-on--mouseleave', 'actions.core.navigation.closeMenuOnHover' );
|
||||
}
|
||||
|
||||
// Add directives to the toggle submenu button.
|
||||
if ( $w->next_tag(
|
||||
array(
|
||||
'tag_name' => 'BUTTON',
|
||||
'class_name' => 'wp-block-navigation-submenu__toggle',
|
||||
)
|
||||
) ) {
|
||||
$w->set_attribute( 'data-wp-on--click', 'actions.core.navigation.toggleMenuOnClick' );
|
||||
$w->set_attribute( 'data-wp-bind--aria-expanded', 'selectors.core.navigation.isMenuOpen' );
|
||||
};
|
||||
|
||||
// Iterate through subitems if exist.
|
||||
block_core_navigation_add_directives_to_submenu( $w, $block_attributes );
|
||||
}
|
||||
return $w->get_updated_html();
|
||||
};
|
||||
|
||||
/**
|
||||
* Replaces view script for the Navigation block with version using Interactivity API.
|
||||
*
|
||||
* @param array $metadata Block metadata as read in via block.json.
|
||||
*
|
||||
* @return array Filtered block type metadata.
|
||||
*/
|
||||
function gutenberg_block_core_navigation_update_interactive_view_script( $metadata ) {
|
||||
if ( 'core/navigation' === $metadata['name'] ) {
|
||||
$metadata['viewScript'] = array( 'file:./interactivity.min.js' );
|
||||
}
|
||||
return $metadata;
|
||||
}
|
||||
add_filter( 'block_type_metadata', 'gutenberg_block_core_navigation_update_interactive_view_script', 10, 1 );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add Interactivity API directives to the navigation-submenu and page-list
|
||||
* blocks markup using the Tag Processor.
|
||||
*
|
||||
* @param string $w Markup of the navigation block.
|
||||
* @param array $block_attributes Block attributes.
|
||||
*
|
||||
* @return string Submenu markup with the directives injected.
|
||||
*/
|
||||
function block_core_navigation_add_directives_to_submenu( $w, $block_attributes ) {
|
||||
while ( $w->next_tag(
|
||||
array(
|
||||
'tag_name' => 'LI',
|
||||
'class_name' => 'has-child',
|
||||
)
|
||||
) ) {
|
||||
// Add directives to the parent `<li>`.
|
||||
$w->set_attribute( 'data-wp-interactive', true );
|
||||
$w->set_attribute( 'data-wp-context', '{ "core": { "navigation": { "submenuOpenedBy": {}, "type": "submenu" } } }' );
|
||||
$w->set_attribute( 'data-wp-effect', 'effects.core.navigation.initMenu' );
|
||||
$w->set_attribute( 'data-wp-on--focusout', 'actions.core.navigation.handleMenuFocusout' );
|
||||
$w->set_attribute( 'data-wp-on--keydown', 'actions.core.navigation.handleMenuKeydown' );
|
||||
|
||||
// This is a fix for Safari. Without it, Safari doesn't change the active
|
||||
// element when the user clicks on a button. It can be removed once we add
|
||||
// an overlay to capture the clicks, instead of relying on the focusout
|
||||
// event.
|
||||
$w->set_attribute( 'tabindex', '-1' );
|
||||
|
||||
if ( ! isset( $block_attributes['openSubmenusOnClick'] ) || false === $block_attributes['openSubmenusOnClick'] ) {
|
||||
$w->set_attribute( 'data-wp-on--mouseenter', 'actions.core.navigation.openMenuOnHover' );
|
||||
$w->set_attribute( 'data-wp-on--mouseleave', 'actions.core.navigation.closeMenuOnHover' );
|
||||
}
|
||||
|
||||
// Add directives to the toggle submenu button.
|
||||
if ( $w->next_tag(
|
||||
array(
|
||||
'tag_name' => 'BUTTON',
|
||||
'class_name' => 'wp-block-navigation-submenu__toggle',
|
||||
)
|
||||
) ) {
|
||||
$w->set_attribute( 'data-wp-on--click', 'actions.core.navigation.toggleMenuOnClick' );
|
||||
$w->set_attribute( 'data-wp-bind--aria-expanded', 'selectors.core.navigation.isMenuOpen' );
|
||||
// The `aria-expanded` attribute for SSR is already added in the submenu block.
|
||||
}
|
||||
// Add directives to the submenu.
|
||||
if ( $w->next_tag(
|
||||
array(
|
||||
'tag_name' => 'UL',
|
||||
'class_name' => 'wp-block-navigation__submenu-container',
|
||||
)
|
||||
) ) {
|
||||
$w->set_attribute( 'data-wp-on--focus', 'actions.core.navigation.openMenuOnFocus' );
|
||||
}
|
||||
|
||||
// Iterate through subitems if exist.
|
||||
block_core_navigation_add_directives_to_submenu( $w, $block_attributes );
|
||||
}
|
||||
return $w->get_updated_html();
|
||||
}
|
||||
|
||||
/**
|
||||
* Build an array with CSS classes and inline styles defining the colors
|
||||
@@ -276,10 +258,6 @@ function block_core_navigation_render_submenu_icon() {
|
||||
return '<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 12 12" fill="none" aria-hidden="true" focusable="false"><path d="M1.50002 4L6.00002 8L10.5 4" stroke-width="1.5"></path></svg>';
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Filter out empty "null" blocks from the block list.
|
||||
* 'parse_blocks' includes a null block with '\n\n' as the content when
|
||||
@@ -292,7 +270,7 @@ function block_core_navigation_render_submenu_icon() {
|
||||
function block_core_navigation_filter_out_empty_blocks( $parsed_blocks ) {
|
||||
$filtered = array_filter(
|
||||
$parsed_blocks,
|
||||
static function( $block ) {
|
||||
static function ( $block ) {
|
||||
return isset( $block['blockName'] );
|
||||
}
|
||||
);
|
||||
@@ -422,7 +400,7 @@ function render_block_core_navigation( $attributes, $content, $block ) {
|
||||
// a fallback (i.e. the block has no menu associated with it).
|
||||
$is_fallback = false;
|
||||
|
||||
$nav_menu_name = '';
|
||||
$nav_menu_name = $attributes['ariaLabel'] ?? '';
|
||||
|
||||
/**
|
||||
* Deprecated:
|
||||
@@ -558,7 +536,7 @@ function render_block_core_navigation( $attributes, $content, $block ) {
|
||||
}
|
||||
|
||||
// Manually add block support text decoration as CSS class.
|
||||
$text_decoration = _wp_array_get( $attributes, array( 'style', 'typography', 'textDecoration' ), null );
|
||||
$text_decoration = $attributes['style']['typography']['textDecoration'] ?? null;
|
||||
$text_decoration_class = sprintf( 'has-text-decoration-%s', $text_decoration );
|
||||
|
||||
$colors = block_core_navigation_build_css_colors( $attributes );
|
||||
@@ -658,24 +636,25 @@ function render_block_core_navigation( $attributes, $content, $block ) {
|
||||
$inner_blocks_html .= '</ul>';
|
||||
}
|
||||
|
||||
// If the script already exists, there is no point in removing it from viewScript.
|
||||
$should_load_view_script = ( $is_responsive_menu || ( $has_submenus && ( $attributes['openSubmenusOnClick'] || $attributes['showSubmenuIcon'] ) ) );
|
||||
$should_load_view_script = ( $has_submenus && ( $attributes['openSubmenusOnClick'] || $attributes['showSubmenuIcon'] ) ) || $is_responsive_menu;
|
||||
$view_js_file = 'wp-block-navigation-view';
|
||||
|
||||
// If the script already exists, there is no point in removing it from viewScript.
|
||||
if ( ! wp_script_is( $view_js_file ) ) {
|
||||
$script_handles = $block->block_type->view_script_handles;
|
||||
|
||||
// If the script is not needed, and it is still in the `view_script_handles`, remove it.
|
||||
if ( ! $should_load_view_script && in_array( $view_js_file, $script_handles, true ) ) {
|
||||
$block->block_type->view_script_handles = array_diff( $script_handles, array( $view_js_file, 'wp-block-navigation-view-2' ) );
|
||||
$block->block_type->view_script_handles = array_diff( $script_handles, array( $view_js_file ) );
|
||||
}
|
||||
// If the script is needed, but it was previously removed, add it again.
|
||||
if ( $should_load_view_script && ! in_array( $view_js_file, $script_handles, true ) ) {
|
||||
$block->block_type->view_script_handles = array_merge( $script_handles, array( $view_js_file, 'wp-block-navigation-view-2' ) );
|
||||
$block->block_type->view_script_handles = array_merge( $script_handles, array( $view_js_file ) );
|
||||
}
|
||||
}
|
||||
|
||||
// Add directives to the submenu if needed.
|
||||
if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN && $has_submenus && $should_load_view_script ) {
|
||||
if ( $has_submenus && $should_load_view_script ) {
|
||||
$w = new WP_HTML_Tag_Processor( $inner_blocks_html );
|
||||
$inner_blocks_html = block_core_navigation_add_directives_to_submenu( $w, $attributes );
|
||||
}
|
||||
@@ -723,10 +702,23 @@ function render_block_core_navigation( $attributes, $content, $block ) {
|
||||
$responsive_container_directives = '';
|
||||
$responsive_dialog_directives = '';
|
||||
$close_button_directives = '';
|
||||
if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN && $should_load_view_script ) {
|
||||
if ( $should_load_view_script ) {
|
||||
$nav_element_context = wp_json_encode(
|
||||
array(
|
||||
'core' => array(
|
||||
'navigation' => array(
|
||||
'overlayOpenedBy' => array(),
|
||||
'type' => 'overlay',
|
||||
'roleAttribute' => '',
|
||||
'ariaLabel' => __( 'Menu' ),
|
||||
),
|
||||
),
|
||||
),
|
||||
JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP
|
||||
);
|
||||
$nav_element_directives = '
|
||||
data-wp-interactive
|
||||
data-wp-context=\'{ "core": { "navigation": { "isMenuOpen": { "click": false, "hover": false }, "overlay": true, "roleAttribute": "" } } }\'
|
||||
data-wp-context=\'' . $nav_element_context . '\'
|
||||
';
|
||||
$open_button_directives = '
|
||||
data-wp-on--click="actions.core.navigation.openMenuOnClick"
|
||||
@@ -741,7 +733,8 @@ function render_block_core_navigation( $attributes, $content, $block ) {
|
||||
tabindex="-1"
|
||||
';
|
||||
$responsive_dialog_directives = '
|
||||
data-wp-bind--aria-modal="selectors.core.navigation.isMenuOpen"
|
||||
data-wp-bind--aria-modal="selectors.core.navigation.ariaModal"
|
||||
data-wp-bind--aria-label="selectors.core.navigation.ariaLabel"
|
||||
data-wp-bind--role="selectors.core.navigation.roleAttribute"
|
||||
data-wp-effect="effects.core.navigation.focusFirstElement"
|
||||
';
|
||||
@@ -751,11 +744,11 @@ function render_block_core_navigation( $attributes, $content, $block ) {
|
||||
}
|
||||
|
||||
$responsive_container_markup = sprintf(
|
||||
'<button aria-haspopup="true" %3$s class="%6$s" data-micromodal-trigger="%1$s" %11$s>%9$s</button>
|
||||
<div class="%5$s" style="%7$s" id="%1$s" %12$s>
|
||||
<div class="wp-block-navigation__responsive-close" tabindex="-1" data-micromodal-close>
|
||||
<div class="wp-block-navigation__responsive-dialog" aria-label="%8$s" %13$s>
|
||||
<button %4$s data-micromodal-close class="wp-block-navigation__responsive-container-close" %14$s>%10$s</button>
|
||||
'<button aria-haspopup="true" %3$s class="%6$s" %10$s>%8$s</button>
|
||||
<div class="%5$s" style="%7$s" id="%1$s" %11$s>
|
||||
<div class="wp-block-navigation__responsive-close" tabindex="-1">
|
||||
<div class="wp-block-navigation__responsive-dialog" %12$s>
|
||||
<button %4$s class="wp-block-navigation__responsive-container-close" %13$s>%9$s</button>
|
||||
<div class="wp-block-navigation__responsive-container-content" id="%1$s-content">
|
||||
%2$s
|
||||
</div>
|
||||
@@ -769,7 +762,6 @@ function render_block_core_navigation( $attributes, $content, $block ) {
|
||||
esc_attr( implode( ' ', $responsive_container_classes ) ),
|
||||
esc_attr( implode( ' ', $open_button_classes ) ),
|
||||
esc_attr( safecss_filter_attr( $colors['overlay_inline_styles'] ) ),
|
||||
__( 'Menu' ),
|
||||
$toggle_button_content,
|
||||
$toggle_close_button_content,
|
||||
$open_button_directives,
|
||||
@@ -837,6 +829,25 @@ function block_core_navigation_typographic_presets_backcompatibility( $parsed_bl
|
||||
|
||||
add_filter( 'render_block_data', 'block_core_navigation_typographic_presets_backcompatibility' );
|
||||
|
||||
/**
|
||||
* Ensure that the view script has the `wp-interactivity` dependency.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @global WP_Scripts $wp_scripts
|
||||
*/
|
||||
function block_core_navigation_ensure_interactivity_dependency() {
|
||||
global $wp_scripts;
|
||||
if (
|
||||
isset( $wp_scripts->registered['wp-block-navigation-view'] ) &&
|
||||
! in_array( 'wp-interactivity', $wp_scripts->registered['wp-block-navigation-view']->deps, true )
|
||||
) {
|
||||
$wp_scripts->registered['wp-block-navigation-view']->deps[] = 'wp-interactivity';
|
||||
}
|
||||
}
|
||||
|
||||
add_action( 'wp_print_scripts', 'block_core_navigation_ensure_interactivity_dependency' );
|
||||
|
||||
/**
|
||||
* Turns menu item data into a nested array of parsed blocks
|
||||
*
|
||||
@@ -930,7 +941,7 @@ function block_core_navigation_get_classic_menu_fallback() {
|
||||
// Otherwise return the most recently created classic menu.
|
||||
usort(
|
||||
$classic_nav_menus,
|
||||
static function( $a, $b ) {
|
||||
static function ( $a, $b ) {
|
||||
return $b->term_id - $a->term_id;
|
||||
}
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user