Make WordPress Core


Ignore:
Timestamp:
11/30/2021 12:22:30 AM (3 years ago)
Author:
noisysocks
Message:

Update @wordpress packages

Update packages to include these bug fixes from Gutenberg:

  • Update Pattern block category and add documentation
  • Fix non existent menu handling in nav block
  • Make Reusable blocks available in the Site Editor
  • Add caching to WP_Theme_JSON_Resolver_Gutenberg::get_user_data_from_custom_post_type()
  • theme.json: add appearanceTools flag to opt-in into appearance UI controls
  • Update the block theme folders to templates and parts
  • Remove reference to gutenberg_, swap with wp_
  • Use table layout in templates list screen
  • Update featured image placeholder graphic.
  • [Inserter]: Adjust order of theme blocks and reorder inserter items
  • Implement suitable fallback for Nav block on front end of site when no menu selected
  • Toggle Group Control: add tooltip
  • Use first non-empty Nav post as primary fallback for Nav block
  • Change .nvmrc and documentation for Node.js version (LTS to 14.18.1)
  • Update: Migrate global styles user database data on the rest endpoint
  • Update global styles public API
  • Update: Rename user preset origin to custom
  • Try always generating navigation post title
  • Show all templates and template parts on the site editor list screens
  • Highlight "Site" in the navigation panel
  • Fix template part slug generation when creating through the block placeholder
  • [Block Library - Post Title]: Fix render error when setting Page to homepage
  • Add 'Clear customizations' button to template list page
  • Gallery v1: Allow clicks within replace media placeholder state
  • Site Editor: Set the <title> on the list page to be same as the CPT name
  • Gallery: Fix stuck image size options loader
  • Cover: Fix undo trap
  • Add success and error snackbars to the templates list page
  • Fix: theme colors cannot override defaults
  • Fix: Color palette is not being stored
  • Add elements support to the typography panel in global styles
  • Make links plural in global styles
  • Add: Gradient palette editor
  • Update some small style regressions in the template list
  • Add: Transparency support on global styles colors
  • Fix: apply by slug on all origins
  • Render empty Nav block if no fallback block can be utilised
  • Allow filtering of Nav block fallback
  • Fix Nav block fallback DB query to match on full block grammar start tag
  • Remove unstable max pages attribute from Nav block
  • DateTimePicker: set PM hours correctly
  • Update delete template button
  • Site Editor: Template list add rename action
  • Fix Nav block editing wrong entity on creation of new Menu
  • [REST] Restore the missing double slash in the ID received by /templates
  • Add icons to navigation sidebar items
  • Update function names for the public global styles API functions
  • Templates Controller: Add missing 'is_custom' prop
  • Rename gutenberg_ to wp_ for some functions that land in WordPress 5.9
  • [Block Library - Template Part]:Remove support for conversion to Reusable block
  • Global Styles: Call "palettes" and not "color palettes" on panel label
  • Add button text when no colors found
  • Update: Global Styes: Count all color palette origins on the palette counter
  • Rename navigationMenuId to ref
  • Offset the parent iframe when computing Popover position
  • Fix: Failing PHPUnit test
  • Show theme, plugin or author in Added By column with appropriate icon or avatar
  • Add origin and author to template rest api

See #54487.
Props talldanwp, mamaduka, oandregal.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks/navigation.php

    r52273 r52275  
    1010 * which will be applied to the navigation markup in the front-end.
    1111 *
    12  * @param  array $attributes Navigation block attributes.
     12 * @param array $attributes Navigation block attributes.
     13 *
    1314 * @return array Colors CSS classes and inline styles.
    1415 */
     
    100101 * which will be applied to the navigation markup in the front-end.
    101102 *
    102  * @param  array $attributes Navigation block attributes.
     103 * @param array $attributes Navigation block attributes.
     104 *
    103105 * @return array Font size CSS classes and inline styles.
    104106 */
     
    133135}
    134136
     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 */
     143function 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 */
     170function 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 */
     191function 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
    135231/**
    136232 * Renders the `core/navigation` block on server.
     
    143239 */
    144240function 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
    145246    /**
    146247     * Deprecated:
     
    188289        $mapping = get_option( 'wp_navigation_areas', array() );
    189290        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    }
    194299    // 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'] );
    197302        if ( ! isset( $navigation_post ) ) {
    198303            return '';
     
    203308        // 'parse_blocks' includes a null block with '\n\n' as the content when
    204309        // 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 );
    211311
    212312        // TODO - this uses the full navigation block attributes for the
     
    215315    }
    216316
     317    // If there are no inner blocks then fallback to rendering an appropriate fallback.
    217318    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
    219330    }
    220331
     
    235346        $font_sizes['css_classes'],
    236347        $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()
    238350    );
    239351
     
    324436 * Register the navigation block.
    325437 *
     438 * @throws WP_Error An WP_Error exception parsing the block definition.
    326439 * @uses render_block_core_navigation()
    327  * @throws WP_Error An WP_Error exception parsing the block definition.
    328440 */
    329441function register_block_core_navigation() {
     
    342454 *
    343455 * @param array $parsed_block The block being rendered.
     456 *
    344457 * @return array The block being rendered without typographic presets.
    345458 */
     
    365478        }
    366479    }
     480
    367481    return $parsed_block;
    368482}
Note: See TracChangeset for help on using the changeset viewer.