Edit File by line
/home/zeestwma/redstone.../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
$has_submenu = count( $block->inner_blocks ) > 0;
[84] Fix | Delete
$kind = empty( $attributes['kind'] ) ? 'post_type' : str_replace( '-', '_', $attributes['kind'] );
[85] Fix | Delete
$is_active = ! empty( $attributes['id'] ) && get_queried_object_id() === (int) $attributes['id'] && ! empty( get_queried_object()->$kind );
[86] Fix | Delete
[87] Fix | Delete
if ( is_post_type_archive() && ! empty( $attributes['url'] ) ) {
[88] Fix | Delete
$queried_archive_link = get_post_type_archive_link( get_queried_object()->name );
[89] Fix | Delete
if ( $attributes['url'] === $queried_archive_link ) {
[90] Fix | Delete
$is_active = true;
[91] Fix | Delete
}
[92] Fix | Delete
}
[93] Fix | Delete
[94] Fix | Delete
$show_submenu_indicators = isset( $block->context['showSubmenuIcon'] ) && $block->context['showSubmenuIcon'];
[95] Fix | Delete
$open_on_click = isset( $block->context['openSubmenusOnClick'] ) && $block->context['openSubmenusOnClick'];
[96] Fix | Delete
$open_on_hover_and_click = isset( $block->context['openSubmenusOnClick'] ) && ! $block->context['openSubmenusOnClick'] &&
[97] Fix | Delete
$show_submenu_indicators;
[98] Fix | Delete
[99] Fix | Delete
$classes = array(
[100] Fix | Delete
'wp-block-navigation-item',
[101] Fix | Delete
);
[102] Fix | Delete
$classes = array_merge(
[103] Fix | Delete
$classes,
[104] Fix | Delete
$font_sizes['css_classes']
[105] Fix | Delete
);
[106] Fix | Delete
if ( $has_submenu ) {
[107] Fix | Delete
$classes[] = 'has-child';
[108] Fix | Delete
}
[109] Fix | Delete
if ( $open_on_click ) {
[110] Fix | Delete
$classes[] = 'open-on-click';
[111] Fix | Delete
}
[112] Fix | Delete
if ( $open_on_hover_and_click ) {
[113] Fix | Delete
$classes[] = 'open-on-hover-click';
[114] Fix | Delete
}
[115] Fix | Delete
if ( $is_active ) {
[116] Fix | Delete
$classes[] = 'current-menu-item';
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
$wrapper_attributes = get_block_wrapper_attributes(
[120] Fix | Delete
array(
[121] Fix | Delete
'class' => implode( ' ', $classes ),
[122] Fix | Delete
'style' => $style_attribute,
[123] Fix | Delete
)
[124] Fix | Delete
);
[125] Fix | Delete
[126] Fix | Delete
$label = '';
[127] Fix | Delete
[128] Fix | Delete
if ( isset( $attributes['label'] ) ) {
[129] Fix | Delete
$label .= wp_kses_post( $attributes['label'] );
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
$aria_label = sprintf(
[133] Fix | Delete
/* translators: Accessibility text. %s: Parent page title. */
[134] Fix | Delete
__( '%s submenu' ),
[135] Fix | Delete
wp_strip_all_tags( $label )
[136] Fix | Delete
);
[137] Fix | Delete
[138] Fix | Delete
$html = '<li ' . $wrapper_attributes . '>';
[139] Fix | Delete
[140] Fix | Delete
// If Submenus open on hover, we render an anchor tag with attributes.
[141] Fix | Delete
// If submenu icons are set to show, we also render a submenu button, so the submenu can be opened on click.
[142] Fix | Delete
if ( ! $open_on_click ) {
[143] Fix | Delete
$item_url = isset( $attributes['url'] ) ? $attributes['url'] : '';
[144] Fix | Delete
// Start appending HTML attributes to anchor tag.
[145] Fix | Delete
$html .= '<a class="wp-block-navigation-item__content"';
[146] Fix | Delete
[147] Fix | Delete
// The href attribute on a and area elements is not required;
[148] Fix | Delete
// when those elements do not have href attributes they do not create hyperlinks.
[149] Fix | Delete
// But also The href attribute must have a value that is a valid URL potentially
[150] Fix | Delete
// surrounded by spaces.
[151] Fix | Delete
// see: https://html.spec.whatwg.org/multipage/links.html#links-created-by-a-and-area-elements.
[152] Fix | Delete
if ( ! empty( $item_url ) ) {
[153] Fix | Delete
$html .= ' href="' . esc_url( $item_url ) . '"';
[154] Fix | Delete
}
[155] Fix | Delete
[156] Fix | Delete
if ( $is_active ) {
[157] Fix | Delete
$html .= ' aria-current="page"';
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
if ( isset( $attributes['opensInNewTab'] ) && true === $attributes['opensInNewTab'] ) {
[161] Fix | Delete
$html .= ' target="_blank" ';
[162] Fix | Delete
}
[163] Fix | Delete
[164] Fix | Delete
if ( isset( $attributes['rel'] ) ) {
[165] Fix | Delete
$html .= ' rel="' . esc_attr( $attributes['rel'] ) . '"';
[166] Fix | Delete
} elseif ( isset( $attributes['nofollow'] ) && $attributes['nofollow'] ) {
[167] Fix | Delete
$html .= ' rel="nofollow"';
[168] Fix | Delete
}
[169] Fix | Delete
[170] Fix | Delete
if ( isset( $attributes['title'] ) ) {
[171] Fix | Delete
$html .= ' title="' . esc_attr( $attributes['title'] ) . '"';
[172] Fix | Delete
}
[173] Fix | Delete
[174] Fix | Delete
$html .= '>';
[175] Fix | Delete
// End appending HTML attributes to anchor tag.
[176] Fix | Delete
[177] Fix | Delete
$html .= '<span class="wp-block-navigation-item__label">';
[178] Fix | Delete
$html .= $label;
[179] Fix | Delete
$html .= '</span>';
[180] Fix | Delete
[181] Fix | Delete
// Add description if available.
[182] Fix | Delete
if ( ! empty( $attributes['description'] ) ) {
[183] Fix | Delete
$html .= '<span class="wp-block-navigation-item__description">';
[184] Fix | Delete
$html .= wp_kses_post( $attributes['description'] );
[185] Fix | Delete
$html .= '</span>';
[186] Fix | Delete
}
[187] Fix | Delete
[188] Fix | Delete
$html .= '</a>';
[189] Fix | Delete
// End anchor tag content.
[190] Fix | Delete
[191] Fix | Delete
if ( $show_submenu_indicators ) {
[192] Fix | Delete
// The submenu icon is rendered in a button here
[193] Fix | Delete
// so that there's a clickable element to open the submenu.
[194] 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>';
[195] Fix | Delete
}
[196] Fix | Delete
} else {
[197] Fix | Delete
// If menus open on click, we render the parent as a button.
[198] Fix | Delete
$html .= '<button aria-label="' . esc_attr( $aria_label ) . '" class="wp-block-navigation-item__content wp-block-navigation-submenu__toggle" aria-expanded="false">';
[199] Fix | Delete
[200] Fix | Delete
// Wrap title with span to isolate it from submenu icon.
[201] Fix | Delete
$html .= '<span class="wp-block-navigation-item__label">';
[202] Fix | Delete
[203] Fix | Delete
$html .= $label;
[204] Fix | Delete
[205] Fix | Delete
$html .= '</span>';
[206] Fix | Delete
[207] Fix | Delete
// Add description if available.
[208] Fix | Delete
if ( ! empty( $attributes['description'] ) ) {
[209] Fix | Delete
$html .= '<span class="wp-block-navigation-item__description">';
[210] Fix | Delete
$html .= wp_kses_post( $attributes['description'] );
[211] Fix | Delete
$html .= '</span>';
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
$html .= '</button>';
[215] Fix | Delete
[216] Fix | Delete
$html .= '<span class="wp-block-navigation__submenu-icon">' . block_core_navigation_submenu_render_submenu_icon() . '</span>';
[217] Fix | Delete
[218] Fix | Delete
}
[219] Fix | Delete
[220] Fix | Delete
if ( $has_submenu ) {
[221] Fix | Delete
// Copy some attributes from the parent block to this one.
[222] Fix | Delete
// Ideally this would happen in the client when the block is created.
[223] Fix | Delete
if ( array_key_exists( 'overlayTextColor', $block->context ) ) {
[224] Fix | Delete
$attributes['textColor'] = $block->context['overlayTextColor'];
[225] Fix | Delete
}
[226] Fix | Delete
if ( array_key_exists( 'overlayBackgroundColor', $block->context ) ) {
[227] Fix | Delete
$attributes['backgroundColor'] = $block->context['overlayBackgroundColor'];
[228] Fix | Delete
}
[229] Fix | Delete
if ( array_key_exists( 'customOverlayTextColor', $block->context ) ) {
[230] Fix | Delete
$attributes['style']['color']['text'] = $block->context['customOverlayTextColor'];
[231] Fix | Delete
}
[232] Fix | Delete
if ( array_key_exists( 'customOverlayBackgroundColor', $block->context ) ) {
[233] Fix | Delete
$attributes['style']['color']['background'] = $block->context['customOverlayBackgroundColor'];
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
// This allows us to be able to get a response from wp_apply_colors_support.
[237] Fix | Delete
$block->block_type->supports['color'] = true;
[238] Fix | Delete
$colors_supports = wp_apply_colors_support( $block->block_type, $attributes );
[239] Fix | Delete
$css_classes = 'wp-block-navigation__submenu-container';
[240] Fix | Delete
if ( array_key_exists( 'class', $colors_supports ) ) {
[241] Fix | Delete
$css_classes .= ' ' . $colors_supports['class'];
[242] Fix | Delete
}
[243] Fix | Delete
[244] Fix | Delete
$style_attribute = '';
[245] Fix | Delete
if ( array_key_exists( 'style', $colors_supports ) ) {
[246] Fix | Delete
$style_attribute = $colors_supports['style'];
[247] Fix | Delete
}
[248] Fix | Delete
[249] Fix | Delete
$inner_blocks_html = '';
[250] Fix | Delete
foreach ( $block->inner_blocks as $inner_block ) {
[251] Fix | Delete
$inner_blocks_html .= $inner_block->render();
[252] Fix | Delete
}
[253] Fix | Delete
[254] Fix | Delete
if ( strpos( $inner_blocks_html, 'current-menu-item' ) ) {
[255] Fix | Delete
$tag_processor = new WP_HTML_Tag_Processor( $html );
[256] Fix | Delete
while ( $tag_processor->next_tag( array( 'class_name' => 'wp-block-navigation-item' ) ) ) {
[257] Fix | Delete
$tag_processor->add_class( 'current-menu-ancestor' );
[258] Fix | Delete
}
[259] Fix | Delete
$html = $tag_processor->get_updated_html();
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
$wrapper_attributes = get_block_wrapper_attributes(
[263] Fix | Delete
array(
[264] Fix | Delete
'class' => $css_classes,
[265] Fix | Delete
'style' => $style_attribute,
[266] Fix | Delete
)
[267] Fix | Delete
);
[268] Fix | Delete
[269] Fix | Delete
$html .= sprintf(
[270] Fix | Delete
'<ul %s>%s</ul>',
[271] Fix | Delete
$wrapper_attributes,
[272] Fix | Delete
$inner_blocks_html
[273] Fix | Delete
);
[274] Fix | Delete
[275] Fix | Delete
}
[276] Fix | Delete
[277] Fix | Delete
$html .= '</li>';
[278] Fix | Delete
[279] Fix | Delete
return $html;
[280] Fix | Delete
}
[281] Fix | Delete
[282] Fix | Delete
/**
[283] Fix | Delete
* Register the navigation submenu block.
[284] Fix | Delete
*
[285] Fix | Delete
* @since 5.9.0
[286] Fix | Delete
*
[287] Fix | Delete
* @uses render_block_core_navigation_submenu()
[288] Fix | Delete
* @throws WP_Error An WP_Error exception parsing the block definition.
[289] Fix | Delete
*/
[290] Fix | Delete
function register_block_core_navigation_submenu() {
[291] Fix | Delete
register_block_type_from_metadata(
[292] Fix | Delete
__DIR__ . '/navigation-submenu',
[293] Fix | Delete
array(
[294] Fix | Delete
'render_callback' => 'render_block_core_navigation_submenu',
[295] Fix | Delete
)
[296] Fix | Delete
);
[297] Fix | Delete
}
[298] Fix | Delete
add_action( 'init', 'register_block_core_navigation_submenu' );
[299] Fix | Delete
[300] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function