[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-wp-classic-to-block-menu-converter.php (source)

   1  <?php
   2  /**
   3   * WP_Classic_To_Block_Menu_Converter class
   4   *
   5   * @package WordPress
   6   * @since 6.3.0
   7   */
   8  
   9  /**
  10   * Converts a Classic Menu to Block Menu blocks.
  11   *
  12   * @since 6.3.0
  13   */
  14  class WP_Classic_To_Block_Menu_Converter {
  15  
  16      /**
  17       * Converts a Classic Menu to blocks.
  18       *
  19       * @since 6.3.0
  20       *
  21       * @param WP_Term $menu The Menu term object of the menu to convert.
  22       * @return string|WP_Error The serialized and normalized parsed blocks on success,
  23       *                         an empty string when there are no menus to convert,
  24       *                         or WP_Error on invalid menu.
  25       */
  26  	public static function convert( $menu ) {
  27  
  28          if ( ! is_nav_menu( $menu ) ) {
  29              return new WP_Error(
  30                  'invalid_menu',
  31                  __( 'The menu provided is not a valid menu.' )
  32              );
  33          }
  34  
  35          $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
  36  
  37          if ( empty( $menu_items ) ) {
  38              return '';
  39          }
  40  
  41          // Set up the $menu_item variables.
  42          // Adds the class property classes for the current context, if applicable.
  43          _wp_menu_item_classes_by_context( $menu_items );
  44  
  45          $menu_items_by_parent_id = static::group_by_parent_id( $menu_items );
  46  
  47          $first_menu_item = $menu_items_by_parent_id[0] ?? array();
  48  
  49          $inner_blocks = static::to_blocks(
  50              $first_menu_item,
  51              $menu_items_by_parent_id
  52          );
  53  
  54          return serialize_blocks( $inner_blocks );
  55      }
  56  
  57      /**
  58       * Returns an array of menu items grouped by the id of the parent menu item.
  59       *
  60       * @since 6.3.0
  61       *
  62       * @param array $menu_items An array of menu items.
  63       * @return array
  64       */
  65  	private static function group_by_parent_id( $menu_items ) {
  66          $menu_items_by_parent_id = array();
  67  
  68          foreach ( $menu_items as $menu_item ) {
  69              $menu_items_by_parent_id[ $menu_item->menu_item_parent ][] = $menu_item;
  70          }
  71  
  72          return $menu_items_by_parent_id;
  73      }
  74  
  75      /**
  76       * Turns menu item data into a nested array of parsed blocks
  77       *
  78       * @since 6.3.0
  79       *
  80       * @param array $menu_items              An array of menu items that represent
  81       *                                       an individual level of a menu.
  82       * @param array $menu_items_by_parent_id An array keyed by the id of the
  83       *                                       parent menu where each element is an
  84       *                                       array of menu items that belong to
  85       *                                       that parent.
  86       * @return array An array of parsed block data.
  87       */
  88  	private static function to_blocks( $menu_items, $menu_items_by_parent_id ) {
  89  
  90          if ( empty( $menu_items ) ) {
  91              return array();
  92          }
  93  
  94          $blocks = array();
  95  
  96          foreach ( $menu_items as $menu_item ) {
  97              $class_name       = ! empty( $menu_item->classes ) ? implode( ' ', (array) $menu_item->classes ) : null;
  98              $id               = ( null !== $menu_item->object_id && 'custom' !== $menu_item->object ) ? $menu_item->object_id : null;
  99              $opens_in_new_tab = null !== $menu_item->target && '_blank' === $menu_item->target;
 100              $rel              = ( null !== $menu_item->xfn && '' !== $menu_item->xfn ) ? $menu_item->xfn : null;
 101              $kind             = null !== $menu_item->type ? str_replace( '_', '-', $menu_item->type ) : 'custom';
 102  
 103              $block = array(
 104                  'blockName' => isset( $menu_items_by_parent_id[ $menu_item->ID ] ) ? 'core/navigation-submenu' : 'core/navigation-link',
 105                  'attrs'     => array(
 106                      'className'     => $class_name,
 107                      'description'   => $menu_item->description,
 108                      'id'            => $id,
 109                      'kind'          => $kind,
 110                      'label'         => $menu_item->title,
 111                      'opensInNewTab' => $opens_in_new_tab,
 112                      'rel'           => $rel,
 113                      'title'         => $menu_item->attr_title,
 114                      'type'          => $menu_item->object,
 115                      'url'           => $menu_item->url,
 116                  ),
 117              );
 118  
 119              $block['innerBlocks']  = isset( $menu_items_by_parent_id[ $menu_item->ID ] )
 120              ? static::to_blocks( $menu_items_by_parent_id[ $menu_item->ID ], $menu_items_by_parent_id )
 121              : array();
 122              $block['innerContent'] = array_map( 'serialize_block', $block['innerBlocks'] );
 123  
 124              $blocks[] = $block;
 125          }
 126  
 127          return $blocks;
 128      }
 129  }


Generated : Thu Apr 23 08:20:11 2026 Cross-referenced by PHPXref