Changeset 47199 for trunk/src/wp-includes/blocks/navigation.php
- Timestamp:
- 02/06/2020 09:01:16 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/navigation.php
r47176 r47199 14 14 */ 15 15 function build_css_colors( $attributes ) { 16 // CSS classes.17 16 $colors = array( 18 17 'css_classes' => array(), … … 20 19 ); 21 20 21 // Text color. 22 22 $has_named_text_color = array_key_exists( 'textColor', $attributes ); 23 23 $has_custom_text_color = array_key_exists( 'customTextColor', $attributes ); … … 34 34 } elseif ( $has_custom_text_color ) { 35 35 // Add the custom color inline style. 36 $colors['inline_styles'] = sprintf( 'color: %s;', $attributes['customTextColor'] ); 36 $colors['inline_styles'] .= sprintf( 'color: %s;', $attributes['customTextColor'] ); 37 } 38 39 // Background color. 40 $has_named_background_color = array_key_exists( 'backgroundColor', $attributes ); 41 $has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes ); 42 43 // If has background color. 44 if ( $has_custom_background_color || $has_named_background_color ) { 45 // Add has-background-color class. 46 $colors['css_classes'][] = 'has-background-color'; 47 } 48 49 if ( $has_named_background_color ) { 50 // Add the background-color class. 51 $colors['css_classes'][] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] ); 52 } elseif ( $has_custom_background_color ) { 53 // Add the custom background-color inline style. 54 $colors['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['customBackgroundColor'] ); 37 55 } 38 56 … … 69 87 70 88 /** 89 * Recursively filters out links with no labels to build a clean navigation block structure. 90 * 91 * @param array $blocks Navigation link inner blocks from the Navigation block. 92 * @return array Blocks that had valid labels 93 */ 94 function gutenberg_remove_empty_navigation_links_recursive( $blocks ) { 95 $blocks = array_filter( 96 $blocks, 97 function( $block ) { 98 return ! empty( $block['attrs']['label'] ); 99 } 100 ); 101 102 if ( ! empty( $blocks ) ) { 103 foreach ( $blocks as $key => $block ) { 104 if ( ! empty( $block['innerBlocks'] ) ) { 105 $blocks[ $key ]['innerBlocks'] = gutenberg_remove_empty_navigation_links_recursive( $block['innerBlocks'] ); 106 } 107 } 108 } 109 110 return $blocks; 111 } 112 113 /** 114 * Returns the top-level submenu SVG chevron icon. 115 * 116 * @return string 117 */ 118 function render_submenu_icon() { 119 return '<svg width="18" height="18" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 18 18" role="img" aria-hidden="true" focusable="false"><polygon points="9,13.5 14.7,7.9 13.2,6.5 9,10.7 4.8,6.5 3.3,7.9 "></polygon></svg>'; 120 } 121 122 /** 71 123 * Renders the `core/navigation` block on server. 72 124 * 73 * @param array $attributes The block attributes.74 125 * @param array $content The saved content. 75 126 * @param array $block The parsed block. … … 77 128 * @return string Returns the post content with the legacy widget added. 78 129 */ 79 function render_block_navigation( $attributes, $content, $block ) { 130 function render_block_navigation( $content, $block ) { 131 132 if ( 'core/navigation' !== $block['blockName'] ) { 133 return $content; 134 } 135 136 $attributes = $block['attrs']; 137 $block['innerBlocks'] = gutenberg_remove_empty_navigation_links_recursive( $block['innerBlocks'] ); 138 139 if ( empty( $block['innerBlocks'] ) ) { 140 return ''; 141 } 142 80 143 $colors = build_css_colors( $attributes ); 81 144 $font_sizes = build_css_font_sizes( $attributes ); … … 97 160 $class_attribute, 98 161 $style_attribute, 99 build_navigation_html( $ block, $colors, $font_sizes)162 build_navigation_html( $attributes, $block, $colors, $font_sizes, true ) 100 163 ); 101 164 } … … 104 167 * Walks the inner block structure and returns an HTML list for it. 105 168 * 106 * @param array $block The block. 107 * @param array $colors Contains inline styles and CSS classes to apply to navigation item. 108 * @param array $font_sizes Contains inline styles and CSS classes to apply to navigation item. 169 * @param array $attributes The Navigation block attributes. 170 * @param array $block The NavigationItem block. 171 * @param array $colors Contains inline styles and CSS classes to apply to navigation item. 172 * @param array $font_sizes Contains inline styles and CSS classes to apply to navigation item. 173 * @param bool $is_level_zero True whether is main menu (level zero). Otherwise, False. 109 174 * 110 175 * @return string Returns an HTML list from innerBlocks. 111 176 */ 112 function build_navigation_html( $ block, $colors, $font_sizes) {177 function build_navigation_html( $attributes, $block, $colors, $font_sizes, $is_level_zero = true ) { 113 178 $html = ''; 114 179 $classes = array_merge( … … 123 188 124 189 foreach ( (array) $block['innerBlocks'] as $key => $block ) { 125 126 $html .= '<li class="wp-block-navigation-link">' . 127 '<a' . $class_attribute . $style_attribute; 190 $has_submenu = count( (array) $block['innerBlocks'] ) > 0; 191 192 $html .= '<li class="wp-block-navigation-link' . ( $has_submenu ? ' has-submenu' : '' ) . '">' . 193 '<a'; 194 195 if ( $is_level_zero ) { 196 $html .= $class_attribute . $style_attribute; 197 } 128 198 129 199 // Start appending HTML attributes to anchor tag. … … 141 211 142 212 // Start anchor tag content. 143 $html .= '>'; 213 $html .= '>' . 214 // Wrap title with span to isolate it from submenu icon. 215 '<span class="wp-block-navigation-link__label">'; 216 144 217 if ( isset( $block['attrs']['label'] ) ) { 145 $html .= esc_html( $block['attrs']['label'] ); 146 } 218 $html .= wp_kses( 219 $block['attrs']['label'], 220 array( 221 'code' => array(), 222 'em' => array(), 223 'img' => array( 224 'scale' => array(), 225 'class' => array(), 226 'style' => array(), 227 'src' => array(), 228 'alt' => array(), 229 ), 230 's' => array(), 231 'span' => array( 232 'style' => array(), 233 ), 234 'strong' => array(), 235 ) 236 ); 237 } 238 239 $html .= '</span>'; 240 241 // Append submenu icon to top-level item. 242 if ( ! empty( $attributes['showSubmenuIcon'] ) && $is_level_zero && $has_submenu ) { 243 $html .= '<span class="wp-block-navigation-link__submenu-icon">' . render_submenu_icon() . '</span>'; 244 } 245 147 246 $html .= '</a>'; 148 247 // End anchor tag content. 149 248 150 if ( count( (array) $block['innerBlocks'] ) > 0) {151 $html .= build_navigation_html( $ block, $colors, $font_sizes);249 if ( $has_submenu ) { 250 $html .= build_navigation_html( $attributes, $block, $colors, $font_sizes, false ); 152 251 } 153 252 … … 168 267 'core/navigation', 169 268 array( 170 'attributes' => array( 171 'className' => array( 172 'type' => 'string', 173 ), 174 'textColor' => array( 175 'type' => 'string', 176 ), 177 'customTextColor' => array( 178 'type' => 'string', 179 ), 180 'fontSize' => array( 181 'type' => 'string', 182 ), 183 'customFontSize' => array( 269 'attributes' => array( 270 'className' => array( 271 'type' => 'string', 272 ), 273 'textColor' => array( 274 'type' => 'string', 275 ), 276 'customTextColor' => array( 277 'type' => 'string', 278 ), 279 'backgroundColor' => array( 280 'type' => 'string', 281 ), 282 'customBackgroundColor' => array( 283 'type' => 'string', 284 ), 285 'fontSize' => array( 286 'type' => 'string', 287 ), 288 'customFontSize' => array( 184 289 'type' => 'number', 185 290 ), 186 'itemsJustification' => array( 187 'type' => 'string', 291 'itemsJustification' => array( 292 'type' => 'string', 293 ), 294 'showSubmenuIcon' => array( 295 'type' => 'boolean', 296 'default' => false, 188 297 ), 189 298 ), 190 191 'render_callback' => 'render_block_navigation',192 299 ) 193 300 ); 194 301 } 195 302 add_action( 'init', 'register_block_core_navigation' ); 303 add_filter( 'render_block', 'render_block_navigation', 10, 2 );
Note: See TracChangeset
for help on using the changeset viewer.