Edit File by line
/home/zeestwma/richards.../wp-inclu.../blocks
File: navigation-submenu.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Server-side rendering of the `core/navigation-submenu` block.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
*/
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* Build an array with CSS classes and inline styles defining the font sizes
[8] Fix | Delete
* which will be applied to the navigation markup in the front-end.
[9] Fix | Delete
*
[10] Fix | Delete
* @since 5.9.0
[11] Fix | Delete
*
[12] Fix | Delete
* @param array $context Navigation block context.
[13] Fix | Delete
* @return array Font size CSS classes and inline styles.
[14] Fix | Delete
*/
[15] Fix | Delete
function block_core_navigation_submenu_build_css_font_sizes( $context ) {
[16] Fix | Delete
// CSS classes.
[17] Fix | Delete
$font_sizes = array(
[18] Fix | Delete
'css_classes' => array(),
[19] Fix | Delete
'inline_styles' => '',
[20] Fix | Delete
);
[21] Fix | Delete
[22] Fix | Delete
$has_named_font_size = array_key_exists( 'fontSize', $context );
[23] Fix | Delete
$has_custom_font_size = isset( $context['style']['typography']['fontSize'] );
[24] Fix | Delete
[25] Fix | Delete
if ( $has_named_font_size ) {
[26] Fix | Delete
// Add the font size class.
[27] Fix | Delete
$font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $context['fontSize'] );
[28] Fix | Delete
} elseif ( $has_custom_font_size ) {
[29] Fix | Delete
// Add the custom font size inline style.
[30] Fix | Delete
$font_sizes['inline_styles'] = sprintf(
[31] Fix | Delete
'font-size: %s;',
[32] Fix | Delete
wp_get_typography_font_size_value(
[33] Fix | Delete
array(
[34] Fix | Delete
'size' => $context['style']['typography']['fontSize'],
[35] Fix | Delete
)
[36] Fix | Delete
)
[37] Fix | Delete
);
[38] Fix | Delete
}
[39] Fix | Delete
[40] Fix | Delete
return $font_sizes;
[41] Fix | Delete
}
[42] Fix | Delete
[43] Fix | Delete
/**
[44] Fix | Delete
* Returns the top-level submenu SVG chevron icon.
[45] Fix | Delete
*
[46] Fix | Delete
* @since 5.9.0
[47] Fix | Delete
*
[48] Fix | Delete
* @return string
[49] Fix | Delete
*/
[50] Fix | Delete
function block_core_navigation_submenu_render_submenu_icon() {
[51] Fix | Delete
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>';
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
/**
[55] Fix | Delete
* Renders the `core/navigation-submenu` block.
[56] Fix | Delete
*
[57] Fix | Delete
* @since 5.9.0
[58] Fix | Delete
*
[59] Fix | Delete
* @param array $attributes The block attributes.
[60] Fix | Delete
* @param string $content The saved content.
[61] Fix | Delete
* @param WP_Block $block The parsed block.
[62] Fix | Delete
*
[63] Fix | Delete
* @return string Returns the post content with the legacy widget added.
[64] Fix | Delete
*/
[65] Fix | Delete
function render_block_core_navigation_submenu( $attributes, $content, $block ) {
[66] Fix | Delete
$navigation_link_has_id = isset( $attributes['id'] ) && is_numeric( $attributes['id'] );
[67] Fix | Delete
$is_post_type = isset( $attributes['kind'] ) && 'post-type' === $attributes['kind'];
[68] Fix | Delete
$is_post_type = $is_post_type || isset( $attributes['type'] ) && ( 'post' === $attributes['type'] || 'page' === $attributes['type'] );
[69] Fix | Delete
[70] Fix | Delete
// Don't render the block's subtree if it is a draft.
[71] Fix | Delete
if ( $is_post_type && $navigation_link_has_id && 'publish' !== get_post_status( $attributes['id'] ) ) {
[72] Fix | Delete
return '';
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
// Don't render the block's subtree if it has no label.
[76] Fix | Delete
if ( empty( $attributes['label'] ) ) {
[77] Fix | Delete
return '';
[78] Fix | Delete
}
[79] Fix | Delete
[80] Fix | Delete
$font_sizes = block_core_navigation_submenu_build_css_font_sizes( $block->context );
[81] Fix | Delete
$style_attribute = $font_sizes['inline_styles'];
[82] Fix | Delete
[83] Fix | Delete
$css_classes = trim( implode( ' ', $font_sizes['css_classes'] ) );
[84] Fix | Delete
$has_submenu = count( $block->inner_blocks ) > 0;
[85] Fix | Delete
$kind = empty( $attributes['kind'] ) ? 'post_type' : str_replace( '-', '_', $attributes['kind'] );
[86] Fix | Delete
$is_active = ! empty( $attributes['id'] ) && get_queried_object_id() === (int) $attributes['id'] && ! empty( get_queried_object()->$kind );
[87] Fix | Delete
[88] Fix | Delete
if ( is_post_type_archive() ) {
[89] Fix | Delete
$queried_archive_link = get_post_type_archive_link( get_queried_object()->name );
[90] Fix | Delete
if ( $attributes['url'] === $queried_archive_link ) {
[91] Fix | Delete
$is_active = true;
[92] Fix | Delete
}
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
$show_submenu_indicators = isset( $block->context['showSubmenuIcon'] ) && $block->context['showSubmenuIcon'];
[96] Fix | Delete
$open_on_click = isset( $block->context['openSubmenusOnClick'] ) && $block->context['openSubmenusOnClick'];
[97] Fix | Delete
$open_on_hover_and_click = isset( $block->context['openSubmenusOnClick'] ) && ! $block->context['openSubmenusOnClick'] &&
[98] Fix | Delete
$show_submenu_indicators;
[99] Fix | Delete
[100] Fix | Delete
$wrapper_attributes = get_block_wrapper_attributes(
[101] Fix | Delete
array(
[102] Fix | Delete
'class' => $css_classes . ' wp-block-navigation-item' . ( $has_submenu ? ' has-child' : '' ) .
[103] Fix | Delete
( $open_on_click ? ' open-on-click' : '' ) . ( $open_on_hover_and_click ? ' open-on-hover-click' : '' ) .
[104] Fix | Delete
( $is_active ? ' current-menu-item' : '' ),
[105] Fix | Delete
'style' => $style_attribute,
[106] Fix | Delete
)
[107] Fix | Delete
);
[108] Fix | Delete
[109] Fix | Delete
$label = '';
[110] Fix | Delete
[111] Fix | Delete
if ( isset( $attributes['label'] ) ) {
[112] Fix | Delete
$label .= wp_kses_post( $attributes['label'] );
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
$aria_label = sprintf(
[116] Fix | Delete
/* translators: Accessibility text. %s: Parent page title. */
[117] Fix | Delete
__( '%s submenu' ),
[118] Fix | Delete
wp_strip_all_tags( $label )
[119] Fix | Delete
);
[120] Fix | Delete
[121] Fix | Delete
$html = '<li ' . $wrapper_attributes . '>';
[122] Fix | Delete
[123] Fix | Delete
// If Submenus open on hover, we render an anchor tag with attributes.
[124] Fix | Delete
// If submenu icons are set to show, we also render a submenu button, so the submenu can be opened on click.
[125] Fix | Delete
if ( ! $open_on_click ) {
[126] Fix | Delete
$item_url = isset( $attributes['url'] ) ? $attributes['url'] : '';
[127] Fix | Delete
// Start appending HTML attributes to anchor tag.
[128] Fix | Delete
$html .= '<a class="wp-block-navigation-item__content"';
[129] Fix | Delete
[130] Fix | Delete
// The href attribute on a and area elements is not required;
[131] Fix | Delete
// when those elements do not have href attributes they do not create hyperlinks.
[132] Fix | Delete
// But also The href attribute must have a value that is a valid URL potentially
[133] Fix | Delete
// surrounded by spaces.
[134] Fix | Delete
// see: https://html.spec.whatwg.org/multipage/links.html#links-created-by-a-and-area-elements.
[135] Fix | Delete
if ( ! empty( $item_url ) ) {
[136] Fix | Delete
$html .= ' href="' . esc_url( $item_url ) . '"';
[137] Fix | Delete
}
[138] Fix | Delete
[139] Fix | Delete
if ( $is_active ) {
[140] Fix | Delete
$html .= ' aria-current="page"';
[141] Fix | Delete
}
[142] Fix | Delete
[143] Fix | Delete
if ( isset( $attributes['opensInNewTab'] ) && true === $attributes['opensInNewTab'] ) {
[144] Fix | Delete
$html .= ' target="_blank" ';
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
if ( isset( $attributes['rel'] ) ) {
[148] Fix | Delete
$html .= ' rel="' . esc_attr( $attributes['rel'] ) . '"';
[149] Fix | Delete
} elseif ( isset( $attributes['nofollow'] ) && $attributes['nofollow'] ) {
[150] Fix | Delete
$html .= ' rel="nofollow"';
[151] Fix | Delete
}
[152] Fix | Delete
[153] Fix | Delete
if ( isset( $attributes['title'] ) ) {
[154] Fix | Delete
$html .= ' title="' . esc_attr( $attributes['title'] ) . '"';
[155] Fix | Delete
}
[156] Fix | Delete
[157] Fix | Delete
$html .= '>';
[158] Fix | Delete
// End appending HTML attributes to anchor tag.
[159] Fix | Delete
[160] Fix | Delete
$html .= $label;
[161] Fix | Delete
[162] Fix | Delete
$html .= '</a>';
[163] Fix | Delete
// End anchor tag content.
[164] Fix | Delete
[165] Fix | Delete
if ( $show_submenu_indicators ) {
[166] Fix | Delete
// The submenu icon is rendered in a button here
[167] Fix | Delete
// so that there's a clickable element to open the submenu.
[168] Fix | Delete
$html .= '<button aria-label="' . esc_attr( $aria_label ) . '" class="wp-block-navigation__submenu-icon wp-block-navigation-submenu__toggle" aria-expanded="false">' . block_core_navigation_submenu_render_submenu_icon() . '</button>';
[169] Fix | Delete
}
[170] Fix | Delete
} else {
[171] Fix | Delete
// If menus open on click, we render the parent as a button.
[172] Fix | Delete
$html .= '<button aria-label="' . esc_attr( $aria_label ) . '" class="wp-block-navigation-item__content wp-block-navigation-submenu__toggle" aria-expanded="false">';
[173] Fix | Delete
[174] Fix | Delete
// Wrap title with span to isolate it from submenu icon.
[175] Fix | Delete
$html .= '<span class="wp-block-navigation-item__label">';
[176] Fix | Delete
[177] Fix | Delete
$html .= $label;
[178] Fix | Delete
[179] Fix | Delete
$html .= '</span>';
[180] Fix | Delete
[181] Fix | Delete
$html .= '</button>';
[182] Fix | Delete
[183] Fix | Delete
$html .= '<span class="wp-block-navigation__submenu-icon">' . block_core_navigation_submenu_render_submenu_icon() . '</span>';
[184] Fix | Delete
[185] Fix | Delete
}
[186] Fix | Delete
[187] Fix | Delete
if ( $has_submenu ) {
[188] Fix | Delete
// Copy some attributes from the parent block to this one.
[189] Fix | Delete
// Ideally this would happen in the client when the block is created.
[190] Fix | Delete
if ( array_key_exists( 'overlayTextColor', $block->context ) ) {
[191] Fix | Delete
$attributes['textColor'] = $block->context['overlayTextColor'];
[192] Fix | Delete
}
[193] Fix | Delete
if ( array_key_exists( 'overlayBackgroundColor', $block->context ) ) {
[194] Fix | Delete
$attributes['backgroundColor'] = $block->context['overlayBackgroundColor'];
[195] Fix | Delete
}
[196] Fix | Delete
if ( array_key_exists( 'customOverlayTextColor', $block->context ) ) {
[197] Fix | Delete
$attributes['style']['color']['text'] = $block->context['customOverlayTextColor'];
[198] Fix | Delete
}
[199] Fix | Delete
if ( array_key_exists( 'customOverlayBackgroundColor', $block->context ) ) {
[200] Fix | Delete
$attributes['style']['color']['background'] = $block->context['customOverlayBackgroundColor'];
[201] Fix | Delete
}
[202] Fix | Delete
[203] Fix | Delete
// This allows us to be able to get a response from wp_apply_colors_support.
[204] Fix | Delete
$block->block_type->supports['color'] = true;
[205] Fix | Delete
$colors_supports = wp_apply_colors_support( $block->block_type, $attributes );
[206] Fix | Delete
$css_classes = 'wp-block-navigation__submenu-container';
[207] Fix | Delete
if ( array_key_exists( 'class', $colors_supports ) ) {
[208] Fix | Delete
$css_classes .= ' ' . $colors_supports['class'];
[209] Fix | Delete
}
[210] Fix | Delete
[211] Fix | Delete
$style_attribute = '';
[212] Fix | Delete
if ( array_key_exists( 'style', $colors_supports ) ) {
[213] Fix | Delete
$style_attribute = $colors_supports['style'];
[214] Fix | Delete
}
[215] Fix | Delete
[216] Fix | Delete
$inner_blocks_html = '';
[217] Fix | Delete
foreach ( $block->inner_blocks as $inner_block ) {
[218] Fix | Delete
$inner_blocks_html .= $inner_block->render();
[219] Fix | Delete
}
[220] Fix | Delete
[221] Fix | Delete
if ( strpos( $inner_blocks_html, 'current-menu-item' ) ) {
[222] Fix | Delete
$tag_processor = new WP_HTML_Tag_Processor( $html );
[223] Fix | Delete
while ( $tag_processor->next_tag( array( 'class_name' => 'wp-block-navigation-item__content' ) ) ) {
[224] Fix | Delete
$tag_processor->add_class( 'current-menu-ancestor' );
[225] Fix | Delete
}
[226] Fix | Delete
$html = $tag_processor->get_updated_html();
[227] Fix | Delete
}
[228] Fix | Delete
[229] Fix | Delete
$wrapper_attributes = get_block_wrapper_attributes(
[230] Fix | Delete
array(
[231] Fix | Delete
'class' => $css_classes,
[232] Fix | Delete
'style' => $style_attribute,
[233] Fix | Delete
)
[234] Fix | Delete
);
[235] Fix | Delete
[236] Fix | Delete
$html .= sprintf(
[237] Fix | Delete
'<ul %s>%s</ul>',
[238] Fix | Delete
$wrapper_attributes,
[239] Fix | Delete
$inner_blocks_html
[240] Fix | Delete
);
[241] Fix | Delete
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
$html .= '</li>';
[245] Fix | Delete
[246] Fix | Delete
return $html;
[247] Fix | Delete
}
[248] Fix | Delete
[249] Fix | Delete
/**
[250] Fix | Delete
* Register the navigation submenu block.
[251] Fix | Delete
*
[252] Fix | Delete
* @since 5.9.0
[253] Fix | Delete
*
[254] Fix | Delete
* @uses render_block_core_navigation_submenu()
[255] Fix | Delete
* @throws WP_Error An WP_Error exception parsing the block definition.
[256] Fix | Delete
*/
[257] Fix | Delete
function register_block_core_navigation_submenu() {
[258] Fix | Delete
register_block_type_from_metadata(
[259] Fix | Delete
__DIR__ . '/navigation-submenu',
[260] Fix | Delete
array(
[261] Fix | Delete
'render_callback' => 'render_block_core_navigation_submenu',
[262] Fix | Delete
)
[263] Fix | Delete
);
[264] Fix | Delete
}
[265] Fix | Delete
add_action( 'init', 'register_block_core_navigation_submenu' );
[266] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function