Changeset 52275 for trunk/src/wp-includes/blocks/navigation.php
- Timestamp:
- 11/30/2021 12:22:30 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/navigation.php
r52273 r52275 10 10 * which will be applied to the navigation markup in the front-end. 11 11 * 12 * @param array $attributes Navigation block attributes. 12 * @param array $attributes Navigation block attributes. 13 * 13 14 * @return array Colors CSS classes and inline styles. 14 15 */ … … 100 101 * which will be applied to the navigation markup in the front-end. 101 102 * 102 * @param array $attributes Navigation block attributes. 103 * @param array $attributes Navigation block attributes. 104 * 103 105 * @return array Font size CSS classes and inline styles. 104 106 */ … … 133 135 } 134 136 137 138 /** 139 * Finds the first non-empty `wp_navigation` Post. 140 * 141 * @return WP_Post|null the first non-empty Navigation or null. 142 */ 143 function block_core_navigation_get_first_non_empty_navigation() { 144 // Order and orderby args set to mirror those in `wp_get_nav_menus` 145 // see: 146 // - https://github.com/WordPress/wordpress-develop/blob/ba943e113d3b31b121f77a2d30aebe14b047c69d/src/wp-includes/nav-menu.php#L613-L619. 147 // - https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters. 148 $navigation_posts = get_posts( 149 array( 150 'post_type' => 'wp_navigation', 151 'order' => 'ASC', 152 'orderby' => 'name', 153 'posts_per_page' => 1, // only the first post. 154 's' => '<!-- wp:', // look for block indicators to ensure we only include non-empty Navigations. 155 ) 156 ); 157 return count( $navigation_posts ) ? $navigation_posts[0] : null; 158 159 } 160 161 /** 162 * Filter out empty "null" blocks from the block list. 163 * 'parse_blocks' includes a null block with '\n\n' as the content when 164 * it encounters whitespace. This is not a bug but rather how the parser 165 * is designed. 166 * 167 * @param array $parsed_blocks the parsed blocks to be normalized. 168 * @return array the normalized parsed blocks. 169 */ 170 function block_core_navigation_filter_out_empty_blocks( $parsed_blocks ) { 171 $filtered = array_filter( 172 $parsed_blocks, 173 function( $block ) { 174 return isset( $block['blockName'] ); 175 } 176 ); 177 178 // Reset keys. 179 return array_values( $filtered ); 180 } 181 182 /** 183 * Retrieves the appropriate fallback to be used on the front of the 184 * site when there is no menu assigned to the Nav block. 185 * 186 * This aims to mirror how the fallback mechanic for wp_nav_menu works. 187 * See https://developer.wordpress.org/reference/functions/wp_nav_menu/#more-information. 188 * 189 * @return array the array of blocks to be used as a fallback. 190 */ 191 function block_core_navigation_get_fallback_blocks() { 192 $page_list_fallback = array( 193 array( 194 'blockName' => 'core/page-list', 195 'attrs' => array( 196 '__unstableMaxPages' => 4, 197 ), 198 ), 199 ); 200 201 $registry = WP_Block_Type_Registry::get_instance(); 202 203 // If `core/page-list` is not registered then return empty blocks. 204 $fallback_blocks = $registry->is_registered( 'core/page-list' ) ? $page_list_fallback : array(); 205 206 // Default to a list of Pages. 207 208 $navigation_post = block_core_navigation_get_first_non_empty_navigation(); 209 210 // Prefer using the first non-empty Navigation as fallback if available. 211 if ( $navigation_post ) { 212 $maybe_fallback = block_core_navigation_filter_out_empty_blocks( parse_blocks( $navigation_post->post_content ) ); 213 214 // Normalizing blocks may result in an empty array of blocks if they were all `null` blocks. 215 // In this case default to the (Page List) fallback. 216 $fallback_blocks = ! empty( $maybe_fallback ) ? $maybe_fallback : $fallback_blocks; 217 } 218 219 /** 220 * Filters the fallback experience for the Navigation block. 221 * 222 * Returning a falsey value will opt out of the fallback and cause the block not to render. 223 * To customise the blocks provided return an array of blocks - these should be valid 224 * children of the `core/navigation` block. 225 * 226 * @param array[] default fallback blocks provided by the default block mechanic. 227 */ 228 return apply_filters( 'block_core_navigation_render_fallback', $fallback_blocks ); 229 } 230 135 231 /** 136 232 * Renders the `core/navigation` block on server. … … 143 239 */ 144 240 function render_block_core_navigation( $attributes, $content, $block ) { 241 242 // Flag used to indicate whether the rendered output is considered to be 243 // a fallback (i.e. the block has no menu associated with it). 244 $is_fallback = false; 245 145 246 /** 146 247 * Deprecated: … … 188 289 $mapping = get_option( 'wp_navigation_areas', array() ); 189 290 if ( ! empty( $mapping[ $area ] ) ) { 190 $attributes['navigationMenuId'] = $mapping[ $area ]; 191 } 192 } 193 291 $attributes['ref'] = $mapping[ $area ]; 292 } 293 } 294 295 // Ensure that blocks saved with the legacy ref attribute name (navigationMenuId) continue to render. 296 if ( array_key_exists( 'navigationMenuId', $attributes ) ) { 297 $attributes['ref'] = $attributes['navigationMenuId']; 298 } 194 299 // Load inner blocks from the navigation post. 195 if ( array_key_exists( ' navigationMenuId', $attributes ) ) {196 $navigation_post = get_post( $attributes[' navigationMenuId'] );300 if ( array_key_exists( 'ref', $attributes ) ) { 301 $navigation_post = get_post( $attributes['ref'] ); 197 302 if ( ! isset( $navigation_post ) ) { 198 303 return ''; … … 203 308 // 'parse_blocks' includes a null block with '\n\n' as the content when 204 309 // it encounters whitespace. This code strips it. 205 $compacted_blocks = array_filter( 206 $parsed_blocks, 207 function( $block ) { 208 return isset( $block['blockName'] ); 209 } 210 ); 310 $compacted_blocks = block_core_navigation_filter_out_empty_blocks( $parsed_blocks ); 211 311 212 312 // TODO - this uses the full navigation block attributes for the … … 215 315 } 216 316 317 // If there are no inner blocks then fallback to rendering an appropriate fallback. 217 318 if ( empty( $inner_blocks ) ) { 218 return ''; 319 $is_fallback = true; // indicate we are rendering the fallback. 320 321 $fallback_blocks = block_core_navigation_get_fallback_blocks(); 322 323 // Fallback my have been filtered so do basic test for validity. 324 if ( empty( $fallback_blocks ) || ! is_array( $fallback_blocks ) ) { 325 return ''; 326 } 327 328 $inner_blocks = new WP_Block_List( $fallback_blocks, $attributes ); 329 219 330 } 220 331 … … 235 346 $font_sizes['css_classes'], 236 347 $is_responsive_menu ? array( 'is-responsive' ) : array(), 237 $layout_class ? array( $layout_class ) : array() 348 $layout_class ? array( $layout_class ) : array(), 349 $is_fallback ? array( 'is-fallback' ) : array() 238 350 ); 239 351 … … 324 436 * Register the navigation block. 325 437 * 438 * @throws WP_Error An WP_Error exception parsing the block definition. 326 439 * @uses render_block_core_navigation() 327 * @throws WP_Error An WP_Error exception parsing the block definition.328 440 */ 329 441 function register_block_core_navigation() { … … 342 454 * 343 455 * @param array $parsed_block The block being rendered. 456 * 344 457 * @return array The block being rendered without typographic presets. 345 458 */ … … 365 478 } 366 479 } 480 367 481 return $parsed_block; 368 482 }
Note: See TracChangeset
for help on using the changeset viewer.