@wordpress/block-library 6.0.11 → 6.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/build/cover/edit.js +1 -0
  2. package/build/cover/edit.js.map +1 -1
  3. package/build/gallery/deprecated.js +314 -26
  4. package/build/gallery/deprecated.js.map +1 -1
  5. package/build/gallery/edit-wrapper.js +10 -27
  6. package/build/gallery/edit-wrapper.js.map +1 -1
  7. package/build/gallery/edit.js +10 -12
  8. package/build/gallery/edit.js.map +1 -1
  9. package/build/gallery/save.js +3 -3
  10. package/build/gallery/save.js.map +1 -1
  11. package/build/gallery/shared.js +24 -0
  12. package/build/gallery/shared.js.map +1 -1
  13. package/build/gallery/transforms.js +8 -22
  14. package/build/gallery/transforms.js.map +1 -1
  15. package/build/gallery/use-mobile-warning.js +1 -1
  16. package/build/gallery/use-mobile-warning.js.map +1 -1
  17. package/build/gallery/v1/edit.js +2 -21
  18. package/build/gallery/v1/edit.js.map +1 -1
  19. package/build/navigation/edit/index.js +1 -0
  20. package/build/navigation/edit/index.js.map +1 -1
  21. package/build/navigation/edit/inner-blocks.js +0 -4
  22. package/build/navigation/edit/inner-blocks.js.map +1 -1
  23. package/build/navigation-submenu/edit.js +4 -0
  24. package/build/navigation-submenu/edit.js.map +1 -1
  25. package/build/query-pagination/index.js +1 -1
  26. package/build/query-pagination-next/index.js +1 -1
  27. package/build/query-pagination-numbers/index.js +1 -1
  28. package/build/query-pagination-previous/index.js +1 -1
  29. package/build/separator/separator-settings.js +1 -0
  30. package/build/separator/separator-settings.js.map +1 -1
  31. package/build/social-links/edit.js +1 -0
  32. package/build/social-links/edit.js.map +1 -1
  33. package/build-module/cover/edit.js +1 -0
  34. package/build-module/cover/edit.js.map +1 -1
  35. package/build-module/gallery/deprecated.js +309 -27
  36. package/build-module/gallery/deprecated.js.map +1 -1
  37. package/build-module/gallery/edit-wrapper.js +7 -27
  38. package/build-module/gallery/edit-wrapper.js.map +1 -1
  39. package/build-module/gallery/edit.js +10 -12
  40. package/build-module/gallery/edit.js.map +1 -1
  41. package/build-module/gallery/save.js +2 -3
  42. package/build-module/gallery/save.js.map +1 -1
  43. package/build-module/gallery/shared.js +22 -0
  44. package/build-module/gallery/shared.js.map +1 -1
  45. package/build-module/gallery/transforms.js +9 -21
  46. package/build-module/gallery/transforms.js.map +1 -1
  47. package/build-module/gallery/use-mobile-warning.js +1 -1
  48. package/build-module/gallery/use-mobile-warning.js.map +1 -1
  49. package/build-module/gallery/v1/edit.js +4 -22
  50. package/build-module/gallery/v1/edit.js.map +1 -1
  51. package/build-module/navigation/edit/index.js +1 -0
  52. package/build-module/navigation/edit/index.js.map +1 -1
  53. package/build-module/navigation/edit/inner-blocks.js +0 -4
  54. package/build-module/navigation/edit/inner-blocks.js.map +1 -1
  55. package/build-module/navigation-submenu/edit.js +4 -0
  56. package/build-module/navigation-submenu/edit.js.map +1 -1
  57. package/build-module/query-pagination/index.js +1 -1
  58. package/build-module/query-pagination-next/index.js +1 -1
  59. package/build-module/query-pagination-numbers/index.js +1 -1
  60. package/build-module/query-pagination-previous/index.js +1 -1
  61. package/build-module/separator/separator-settings.js +1 -0
  62. package/build-module/separator/separator-settings.js.map +1 -1
  63. package/build-module/social-links/edit.js +1 -0
  64. package/build-module/social-links/edit.js.map +1 -1
  65. package/build-style/columns/editor-rtl.css +1 -1
  66. package/build-style/columns/editor.css +1 -1
  67. package/build-style/editor-rtl.css +1 -1
  68. package/build-style/editor.css +1 -1
  69. package/build-style/navigation/style-rtl.css +4 -1
  70. package/build-style/navigation/style.css +4 -1
  71. package/build-style/style-rtl.css +4 -1
  72. package/build-style/style.css +4 -1
  73. package/package.json +8 -8
  74. package/src/columns/editor.scss +3 -2
  75. package/src/cover/edit.js +1 -0
  76. package/src/gallery/deprecated.js +831 -559
  77. package/src/gallery/edit-wrapper.js +7 -27
  78. package/src/gallery/edit.js +10 -12
  79. package/src/gallery/save.js +2 -1
  80. package/src/gallery/shared.js +24 -0
  81. package/src/gallery/transforms.js +9 -27
  82. package/src/gallery/use-mobile-warning.js +1 -1
  83. package/src/gallery/v1/edit.js +1 -28
  84. package/src/navigation/edit/index.js +1 -0
  85. package/src/navigation/edit/inner-blocks.js +0 -5
  86. package/src/navigation/index.php +149 -18
  87. package/src/navigation/style.scss +4 -1
  88. package/src/navigation-submenu/edit.js +6 -0
  89. package/src/navigation-submenu/index.php +30 -45
  90. package/src/page-list/index.php +5 -9
  91. package/src/query-pagination/block.json +1 -1
  92. package/src/query-pagination-next/block.json +1 -1
  93. package/src/query-pagination-numbers/block.json +1 -1
  94. package/src/query-pagination-previous/block.json +1 -1
  95. package/src/separator/separator-settings.js +1 -0
  96. package/src/social-links/edit.js +1 -0
  97. package/src/template-part/index.php +36 -0
  98. package/build/gallery/v1/update-gallery-modal.js +0 -114
  99. package/build/gallery/v1/update-gallery-modal.js.map +0 -1
  100. package/build-module/gallery/v1/update-gallery-modal.js +0 -97
  101. package/build-module/gallery/v1/update-gallery-modal.js.map +0 -1
  102. package/src/gallery/v1/update-gallery-modal.js +0 -97
@@ -1,47 +1,27 @@
1
1
  /**
2
2
  * WordPress dependencies
3
3
  */
4
- import { store as blockEditorStore } from '@wordpress/block-editor';
5
- import { useSelect } from '@wordpress/data';
4
+ import { compose } from '@wordpress/compose';
5
+ import { withNotices } from '@wordpress/components';
6
6
 
7
7
  /**
8
8
  * Internal dependencies
9
9
  */
10
10
  import EditWithInnerBlocks from './edit';
11
11
  import EditWithoutInnerBlocks from './v1/edit';
12
+ import { isGalleryV2Enabled } from './shared';
12
13
 
13
14
  /*
14
15
  * Using a wrapper around the logic to load the edit for v1 of Gallery block
15
16
  * or the refactored version with InnerBlocks. This is to prevent conditional
16
17
  * use of hooks lint errors if adding this logic to the top of the edit component.
17
18
  */
18
- export default function GalleryEditWrapper( props ) {
19
- const { attributes, clientId } = props;
20
-
21
- const innerBlockImages = useSelect(
22
- ( select ) => {
23
- return select( blockEditorStore ).getBlock( clientId )?.innerBlocks;
24
- },
25
- [ clientId ]
26
- );
27
-
28
- const __unstableGalleryWithImageBlocks = useSelect( ( select ) => {
29
- const settings = select( blockEditorStore ).getSettings();
30
- return settings.__unstableGalleryWithImageBlocks;
31
- }, [] );
32
-
33
- // This logic is used to infer version information from content with higher
34
- // precedence than the flag. New galleries (and existing empty galleries) will
35
- // honor the flag.
36
- const hasNewVersionContent = !! innerBlockImages?.length;
37
- const hasOldVersionContent =
38
- 0 < attributes?.ids?.length || 0 < attributes?.images?.length;
39
- if (
40
- hasOldVersionContent ||
41
- ( ! hasNewVersionContent && ! __unstableGalleryWithImageBlocks )
42
- ) {
19
+ function GalleryEditWrapper( props ) {
20
+ if ( ! isGalleryV2Enabled() ) {
43
21
  return <EditWithoutInnerBlocks { ...props } />;
44
22
  }
45
23
 
46
24
  return <EditWithInnerBlocks { ...props } />;
47
25
  }
26
+
27
+ export default compose( [ withNotices ] )( GalleryEditWrapper );
@@ -144,7 +144,7 @@ function GalleryEdit( props ) {
144
144
  useEffect( () => {
145
145
  newImages?.forEach( ( newImage ) => {
146
146
  updateBlockAttributes( newImage.clientId, {
147
- ...buildImageAttributes( false, newImage.attributes ),
147
+ ...buildImageAttributes( newImage.attributes ),
148
148
  id: newImage.id,
149
149
  align: undefined,
150
150
  } );
@@ -176,26 +176,24 @@ function GalleryEdit( props ) {
176
176
  * it already existed in the gallery. If the image is in fact new, we need
177
177
  * to apply the gallery's current settings to the image.
178
178
  *
179
- * @param {Object} existingBlock Existing Image block that still exists after gallery update.
180
- * @param {Object} image Media object for the actual image.
181
- * @return {Object} Attributes to set on the new image block.
179
+ * @param {Object} imageAttributes Media object for the actual image.
180
+ * @return {Object} Attributes to set on the new image block.
182
181
  */
183
- function buildImageAttributes( existingBlock, image ) {
184
- if ( existingBlock ) {
185
- return existingBlock.attributes;
186
- }
182
+ function buildImageAttributes( imageAttributes ) {
183
+ const image = imageAttributes.id
184
+ ? find( imageData, { id: imageAttributes.id } )
185
+ : null;
187
186
 
188
187
  let newClassName;
189
- if ( image.className && image.className !== '' ) {
190
- newClassName = image.className;
188
+ if ( imageAttributes.className && imageAttributes.className !== '' ) {
189
+ newClassName = imageAttributes.className;
191
190
  } else {
192
191
  newClassName = preferredStyle
193
192
  ? `is-style-${ preferredStyle }`
194
193
  : undefined;
195
194
  }
196
-
197
195
  return {
198
- ...pickRelevantMediaFiles( image, sizeSlug ),
196
+ ...pickRelevantMediaFiles( imageAttributes, sizeSlug ),
199
197
  ...getHrefAndDestination( image, linkTo ),
200
198
  ...getUpdatedLinkTargetSettings( linkTarget, attributes ),
201
199
  className: newClassName,
@@ -16,9 +16,10 @@ import {
16
16
  * Internal dependencies
17
17
  */
18
18
  import saveWithoutInnerBlocks from './v1/save';
19
+ import { isGalleryV2Enabled } from './shared';
19
20
 
20
21
  export default function saveWithInnerBlocks( { attributes } ) {
21
- if ( attributes?.ids?.length > 0 || attributes?.images?.length > 0 ) {
22
+ if ( ! isGalleryV2Enabled() ) {
22
23
  return saveWithoutInnerBlocks( { attributes } );
23
24
  }
24
25
 
@@ -21,3 +21,27 @@ export const pickRelevantMediaFiles = ( image, sizeSlug = 'large' ) => {
21
21
  }
22
22
  return imageProps;
23
23
  };
24
+
25
+ /**
26
+ * The new gallery block format is not compatible with the use_BalanceTags option
27
+ * in WP versions <= 5.8 https://core.trac.wordpress.org/ticket/54130. The
28
+ * window.wp.galleryBlockV2Enabled flag is set in lib/compat.php. This method
29
+ * can be removed when minimum supported WP version >=5.9.
30
+ */
31
+ export function isGalleryV2Enabled() {
32
+ // Only run the Gallery version compat check if the plugin is running, otherwise
33
+ // assume we are in 5.9 core and enable by default.
34
+ if ( process.env.GUTENBERG_PHASE === 2 ) {
35
+ // We want to fail early here, at least during beta testing phase, to ensure
36
+ // there aren't instances where undefined values cause false negatives.
37
+ if (
38
+ ! window.wp ||
39
+ typeof window.wp.galleryBlockV2Enabled !== 'boolean'
40
+ ) {
41
+ throw 'window.wp.galleryBlockV2Enabled is not defined';
42
+ }
43
+ return window.wp.galleryBlockV2Enabled;
44
+ }
45
+
46
+ return true;
47
+ }
@@ -8,8 +8,6 @@ import { filter, every, toString } from 'lodash';
8
8
  */
9
9
  import { createBlock } from '@wordpress/blocks';
10
10
  import { createBlobURL } from '@wordpress/blob';
11
- import { select } from '@wordpress/data';
12
- import { store as blockEditorStore } from '@wordpress/block-editor';
13
11
  import { addFilter } from '@wordpress/hooks';
14
12
 
15
13
  /**
@@ -24,7 +22,7 @@ import {
24
22
  LINK_DESTINATION_ATTACHMENT as DEPRECATED_LINK_DESTINATION_ATTACHMENT,
25
23
  LINK_DESTINATION_MEDIA as DEPRECATED_LINK_DESTINATION_MEDIA,
26
24
  } from './v1/constants';
27
- import { pickRelevantMediaFiles } from './shared';
25
+ import { pickRelevantMediaFiles, isGalleryV2Enabled } from './shared';
28
26
 
29
27
  const parseShortcodeIds = ( ids ) => {
30
28
  if ( ! ids ) {
@@ -49,9 +47,8 @@ const parseShortcodeIds = ( ids ) => {
49
47
  * @return {Block} The transformed block.
50
48
  */
51
49
  function updateThirdPartyTransformToGallery( block ) {
52
- const settings = select( blockEditorStore ).getSettings();
53
50
  if (
54
- settings.__unstableGalleryWithImageBlocks &&
51
+ isGalleryV2Enabled() &&
55
52
  block.name === 'core/gallery' &&
56
53
  block.attributes?.images.length > 0
57
54
  ) {
@@ -145,8 +142,7 @@ const transforms = {
145
142
 
146
143
  const validImages = filter( attributes, ( { url } ) => url );
147
144
 
148
- const settings = select( blockEditorStore ).getSettings();
149
- if ( settings.__unstableGalleryWithImageBlocks ) {
145
+ if ( isGalleryV2Enabled() ) {
150
146
  const innerBlocks = validImages.map( ( image ) => {
151
147
  return createBlock( 'core/image', image );
152
148
  } );
@@ -184,10 +180,7 @@ const transforms = {
184
180
  images: {
185
181
  type: 'array',
186
182
  shortcode: ( { named: { ids } } ) => {
187
- const settings = select(
188
- blockEditorStore
189
- ).getSettings();
190
- if ( ! settings.__unstableGalleryWithImageBlocks ) {
183
+ if ( ! isGalleryV2Enabled() ) {
191
184
  return parseShortcodeIds( ids ).map( ( id ) => ( {
192
185
  id: toString( id ),
193
186
  } ) );
@@ -197,10 +190,7 @@ const transforms = {
197
190
  ids: {
198
191
  type: 'array',
199
192
  shortcode: ( { named: { ids } } ) => {
200
- const settings = select(
201
- blockEditorStore
202
- ).getSettings();
203
- if ( ! settings.__unstableGalleryWithImageBlocks ) {
193
+ if ( ! isGalleryV2Enabled() ) {
204
194
  return parseShortcodeIds( ids );
205
195
  }
206
196
  },
@@ -208,10 +198,7 @@ const transforms = {
208
198
  shortCodeTransforms: {
209
199
  type: 'array',
210
200
  shortcode: ( { named: { ids } } ) => {
211
- const settings = select(
212
- blockEditorStore
213
- ).getSettings();
214
- if ( settings.__unstableGalleryWithImageBlocks ) {
201
+ if ( isGalleryV2Enabled() ) {
215
202
  return parseShortcodeIds( ids ).map( ( id ) => ( {
216
203
  id: parseInt( id ),
217
204
  } ) );
@@ -227,10 +214,7 @@ const transforms = {
227
214
  linkTo: {
228
215
  type: 'string',
229
216
  shortcode: ( { named: { link } } ) => {
230
- const settings = select(
231
- blockEditorStore
232
- ).getSettings();
233
- if ( ! settings.__unstableGalleryWithImageBlocks ) {
217
+ if ( ! isGalleryV2Enabled() ) {
234
218
  switch ( link ) {
235
219
  case 'post':
236
220
  return DEPRECATED_LINK_DESTINATION_ATTACHMENT;
@@ -273,8 +257,7 @@ const transforms = {
273
257
  );
274
258
  },
275
259
  transform( files ) {
276
- const settings = select( blockEditorStore ).getSettings();
277
- if ( settings.__unstableGalleryWithImageBlocks ) {
260
+ if ( isGalleryV2Enabled() ) {
278
261
  const innerBlocks = files.map( ( file ) =>
279
262
  createBlock( 'core/image', {
280
263
  url: createBlobURL( file ),
@@ -299,8 +282,7 @@ const transforms = {
299
282
  type: 'block',
300
283
  blocks: [ 'core/image' ],
301
284
  transform: ( { align, images, ids, sizeSlug }, innerBlocks ) => {
302
- const settings = select( blockEditorStore ).getSettings();
303
- if ( settings.__unstableGalleryWithImageBlocks ) {
285
+ if ( isGalleryV2Enabled() ) {
304
286
  if ( innerBlocks.length > 0 ) {
305
287
  return innerBlocks.map(
306
288
  ( {
@@ -20,7 +20,7 @@ export default function useMobileWarning( newImages ) {
20
20
 
21
21
  createWarningNotice(
22
22
  __(
23
- 'Editing this Gallery in the WordPress mobile app requires version 18.2 or higher.'
23
+ 'If you want to edit the gallery you just added in the mobile app, to avoid losing any data please make sure you use version 18.2 of the app or above.'
24
24
  ),
25
25
  { type: 'snackbar', explicitDismiss: true }
26
26
  );
@@ -24,10 +24,8 @@ import {
24
24
  ToggleControl,
25
25
  withNotices,
26
26
  RangeControl,
27
- ToolbarButton,
28
27
  } from '@wordpress/components';
29
28
  import {
30
- BlockControls,
31
29
  MediaPlaceholder,
32
30
  InspectorControls,
33
31
  useBlockProps,
@@ -53,7 +51,6 @@ import {
53
51
  LINK_DESTINATION_MEDIA,
54
52
  LINK_DESTINATION_NONE,
55
53
  } from './constants';
56
- import UpdateGalleryModal from './update-gallery-modal';
57
54
 
58
55
  const MAX_COLUMNS = 8;
59
56
  const linkOptions = [
@@ -102,13 +99,10 @@ function GalleryEdit( props ) {
102
99
  mediaUpload,
103
100
  getMedia,
104
101
  wasBlockJustInserted,
105
- __unstableGalleryWithImageBlocks,
106
102
  } = useSelect( ( select ) => {
107
103
  const settings = select( blockEditorStore ).getSettings();
108
104
 
109
105
  return {
110
- __unstableGalleryWithImageBlocks:
111
- settings.__unstableGalleryWithImageBlocks,
112
106
  imageSizes: settings.imageSizes,
113
107
  mediaUpload: settings.mediaUpload,
114
108
  getMedia: select( coreStore ).getMedia,
@@ -414,10 +408,6 @@ function GalleryEdit( props ) {
414
408
  />
415
409
  );
416
410
 
417
- const [ isUpdateOpen, setUpdateOpen ] = useState( false );
418
- const openUpdateModal = () => setUpdateOpen( true );
419
- const closeUpdateModal = () => setUpdateOpen( false );
420
-
421
411
  const blockProps = useBlockProps();
422
412
 
423
413
  if ( ! hasImages ) {
@@ -466,24 +456,7 @@ function GalleryEdit( props ) {
466
456
  ) }
467
457
  </PanelBody>
468
458
  </InspectorControls>
469
- { /* TODO: Remove platform condition when native conversion is ready */ }
470
- { Platform.isWeb && __unstableGalleryWithImageBlocks && (
471
- <BlockControls group="other">
472
- <ToolbarButton
473
- onClick={ openUpdateModal }
474
- title={ __( 'Update' ) }
475
- label={ __( 'Update to the new gallery format' ) }
476
- >
477
- { __( 'Update' ) }
478
- </ToolbarButton>
479
- </BlockControls>
480
- ) }
481
- { Platform.isWeb && isUpdateOpen && (
482
- <UpdateGalleryModal
483
- onClose={ closeUpdateModal }
484
- clientId={ clientId }
485
- />
486
- ) }
459
+
487
460
  { noticeUI }
488
461
  <Gallery
489
462
  { ...props }
@@ -437,6 +437,7 @@ function Navigation( {
437
437
  { hasColorSettings && (
438
438
  <PanelColorSettings
439
439
  __experimentalHasMultipleOrigins
440
+ __experimentalIsRenderedInSidebar
440
441
  title={ __( 'Color' ) }
441
442
  initialOpen={ false }
442
443
  colorSettings={ [
@@ -110,11 +110,6 @@ export default function NavigationInnerBlocks( {
110
110
  __experimentalDirectInsert: shouldDirectInsert,
111
111
  orientation,
112
112
  renderAppender: CustomAppender || appender,
113
-
114
- // Ensure block toolbar is not too far removed from item
115
- // being edited when in vertical mode.
116
- // see: https://github.com/WordPress/gutenberg/pull/34615.
117
- __experimentalCaptureToolbars: orientation !== 'vertical',
118
113
  // Template lock set to false here so that the Nav
119
114
  // Block on the experimental menus screen does not
120
115
  // inherit templateLock={ 'all' }.
@@ -5,6 +5,120 @@
5
5
  * @package WordPress
6
6
  */
7
7
 
8
+ // These functions are used for the __unstableLocation feature and only active
9
+ // when the gutenberg plugin is active.
10
+ if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) {
11
+ /**
12
+ * Returns the menu items for a WordPress menu location.
13
+ *
14
+ * @param string $location The menu location.
15
+ * @return array Menu items for the location.
16
+ */
17
+ function block_core_navigation_get_menu_items_at_location( $location ) {
18
+ if ( empty( $location ) ) {
19
+ return;
20
+ }
21
+
22
+ // Build menu data. The following approximates the code in
23
+ // `wp_nav_menu()` and `gutenberg_output_block_nav_menu`.
24
+
25
+ // Find the location in the list of locations, returning early if the
26
+ // location can't be found.
27
+ $locations = get_nav_menu_locations();
28
+ if ( ! isset( $locations[ $location ] ) ) {
29
+ return;
30
+ }
31
+
32
+ // Get the menu from the location, returning early if there is no
33
+ // menu or there was an error.
34
+ $menu = wp_get_nav_menu_object( $locations[ $location ] );
35
+ if ( ! $menu || is_wp_error( $menu ) ) {
36
+ return;
37
+ }
38
+
39
+ $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
40
+ _wp_menu_item_classes_by_context( $menu_items );
41
+
42
+ return $menu_items;
43
+ }
44
+
45
+
46
+ /**
47
+ * Sorts a standard array of menu items into a nested structure keyed by the
48
+ * id of the parent menu.
49
+ *
50
+ * @param array $menu_items Menu items to sort.
51
+ * @return array An array keyed by the id of the parent menu where each element
52
+ * is an array of menu items that belong to that parent.
53
+ */
54
+ function block_core_navigation_sort_menu_items_by_parent_id( $menu_items ) {
55
+ $sorted_menu_items = array();
56
+ foreach ( (array) $menu_items as $menu_item ) {
57
+ $sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
58
+ }
59
+ unset( $menu_items, $menu_item );
60
+
61
+ $menu_items_by_parent_id = array();
62
+ foreach ( $sorted_menu_items as $menu_item ) {
63
+ $menu_items_by_parent_id[ $menu_item->menu_item_parent ][] = $menu_item;
64
+ }
65
+
66
+ return $menu_items_by_parent_id;
67
+ }
68
+
69
+ /**
70
+ * Turns menu item data into a nested array of parsed blocks
71
+ *
72
+ * @param array $menu_items An array of menu items that represent
73
+ * an individual level of a menu.
74
+ * @param array $menu_items_by_parent_id An array keyed by the id of the
75
+ * parent menu where each element is an
76
+ * array of menu items that belong to
77
+ * that parent.
78
+ * @return array An array of parsed block data.
79
+ */
80
+ function block_core_navigation_parse_blocks_from_menu_items( $menu_items, $menu_items_by_parent_id ) {
81
+ if ( empty( $menu_items ) ) {
82
+ return array();
83
+ }
84
+
85
+ $blocks = array();
86
+
87
+ foreach ( $menu_items as $menu_item ) {
88
+ $class_name = ! empty( $menu_item->classes ) ? implode( ' ', (array) $menu_item->classes ) : null;
89
+ $id = ( null !== $menu_item->object_id && 'custom' !== $menu_item->object ) ? $menu_item->object_id : null;
90
+ $opens_in_new_tab = null !== $menu_item->target && '_blank' === $menu_item->target;
91
+ $rel = ( null !== $menu_item->xfn && '' !== $menu_item->xfn ) ? $menu_item->xfn : null;
92
+ $kind = null !== $menu_item->type ? str_replace( '_', '-', $menu_item->type ) : 'custom';
93
+
94
+ $block = array(
95
+ 'blockName' => isset( $menu_items_by_parent_id[ $menu_item->ID ] ) ? 'core/navigation-submenu' : 'core/navigation-link',
96
+ 'attrs' => array(
97
+ 'className' => $class_name,
98
+ 'description' => $menu_item->description,
99
+ 'id' => $id,
100
+ 'kind' => $kind,
101
+ 'label' => $menu_item->title,
102
+ 'opensInNewTab' => $opens_in_new_tab,
103
+ 'rel' => $rel,
104
+ 'title' => $menu_item->attr_title,
105
+ 'type' => $menu_item->object,
106
+ 'url' => $menu_item->url,
107
+ ),
108
+ );
109
+
110
+ $block['innerBlocks'] = isset( $menu_items_by_parent_id[ $menu_item->ID ] )
111
+ ? block_core_navigation_parse_blocks_from_menu_items( $menu_items_by_parent_id[ $menu_item->ID ], $menu_items_by_parent_id )
112
+ : array();
113
+ $block['innerContent'] = array_map( 'serialize_block', $block['innerBlocks'] );
114
+
115
+ $blocks[] = $block;
116
+ }
117
+
118
+ return $blocks;
119
+ }
120
+ }
121
+
8
122
  /**
9
123
  * Build an array with CSS classes and inline styles defining the colors
10
124
  * which will be applied to the navigation markup in the front-end.
@@ -145,17 +259,23 @@ function block_core_navigation_get_first_non_empty_navigation() {
145
259
  // see:
146
260
  // - https://github.com/WordPress/wordpress-develop/blob/ba943e113d3b31b121f77a2d30aebe14b047c69d/src/wp-includes/nav-menu.php#L613-L619.
147
261
  // - 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
- )
262
+ $parsed_args = array(
263
+ 'post_type' => 'wp_navigation',
264
+ 'no_found_rows' => true,
265
+ 'order' => 'ASC',
266
+ 'orderby' => 'name',
267
+ 'post_status' => 'publish',
268
+ 'posts_per_page' => 20, // Try the first 20 posts.
156
269
  );
157
- return count( $navigation_posts ) ? $navigation_posts[0] : null;
158
270
 
271
+ $navigation_posts = new WP_Query( $parsed_args );
272
+ foreach ( $navigation_posts->posts as $navigation_post ) {
273
+ if ( has_blocks( $navigation_post ) ) {
274
+ return $navigation_post;
275
+ }
276
+ }
277
+
278
+ return null;
159
279
  }
160
280
 
161
281
  /**
@@ -272,22 +392,33 @@ function render_block_core_navigation( $attributes, $content, $block ) {
272
392
 
273
393
  $inner_blocks = $block->inner_blocks;
274
394
 
275
- // If `__unstableLocation` is defined, create inner blocks from the classic menu assigned to that location.
276
- if ( empty( $inner_blocks ) && array_key_exists( '__unstableLocation', $attributes ) ) {
277
- $menu_items = gutenberg_get_menu_items_at_location( $attributes['__unstableLocation'] );
395
+ // Ensure that blocks saved with the legacy ref attribute name (navigationMenuId) continue to render.
396
+ if ( array_key_exists( 'navigationMenuId', $attributes ) ) {
397
+ $attributes['ref'] = $attributes['navigationMenuId'];
398
+ }
399
+
400
+ // If:
401
+ // - the gutenberg plugin is active
402
+ // - `__unstableLocation` is defined
403
+ // - we have menu items at the defined location
404
+ // - we don't have a relationship to a `wp_navigation` Post (via `ref`).
405
+ // ...then create inner blocks from the classic menu assigned to that location.
406
+ if (
407
+ defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN &&
408
+ array_key_exists( '__unstableLocation', $attributes ) &&
409
+ ! array_key_exists( 'ref', $attributes ) &&
410
+ ! empty( block_core_navigation_get_menu_items_at_location( $attributes['__unstableLocation'] ) )
411
+ ) {
412
+ $menu_items = block_core_navigation_get_menu_items_at_location( $attributes['__unstableLocation'] );
278
413
  if ( empty( $menu_items ) ) {
279
414
  return '';
280
415
  }
281
416
 
282
- $menu_items_by_parent_id = gutenberg_sort_menu_items_by_parent_id( $menu_items );
283
- $parsed_blocks = gutenberg_parse_blocks_from_menu_items( $menu_items_by_parent_id[0], $menu_items_by_parent_id );
417
+ $menu_items_by_parent_id = block_core_navigation_sort_menu_items_by_parent_id( $menu_items );
418
+ $parsed_blocks = block_core_navigation_parse_blocks_from_menu_items( $menu_items_by_parent_id[0], $menu_items_by_parent_id );
284
419
  $inner_blocks = new WP_Block_List( $parsed_blocks, $attributes );
285
420
  }
286
421
 
287
- // Ensure that blocks saved with the legacy ref attribute name (navigationMenuId) continue to render.
288
- if ( array_key_exists( 'navigationMenuId', $attributes ) ) {
289
- $attributes['ref'] = $attributes['navigationMenuId'];
290
- }
291
422
  // Load inner blocks from the navigation post.
292
423
  if ( array_key_exists( 'ref', $attributes ) ) {
293
424
  $navigation_post = get_post( $attributes['ref'] );
@@ -426,7 +426,10 @@ button.wp-block-navigation-item__content {
426
426
  align-items: var(--layout-justification-setting, inherit);
427
427
 
428
428
  // Always align the contents of the menu to the top.
429
- justify-content: flex-start;
429
+ &,
430
+ .wp-block-navigation__container {
431
+ justify-content: flex-start;
432
+ }
430
433
 
431
434
  // Allow menu to scroll.
432
435
  overflow: auto;
@@ -508,6 +508,12 @@ export default function NavigationSubmenuEdit( {
508
508
  allowedBlocks: ALLOWED_BLOCKS,
509
509
  __experimentalDefaultBlock: DEFAULT_BLOCK,
510
510
  __experimentalDirectInsert: true,
511
+
512
+ // Ensure block toolbar is not too far removed from item
513
+ // being edited.
514
+ // see: https://github.com/WordPress/gutenberg/pull/34615.
515
+ __experimentalCaptureToolbars: true,
516
+
511
517
  renderAppender:
512
518
  isSelected ||
513
519
  ( isImmediateParentOfSelectedBlock &&
@@ -163,7 +163,32 @@ function render_block_core_navigation_submenu( $attributes, $content, $block ) {
163
163
  'style' => $style_attribute,
164
164
  )
165
165
  );
166
- $html = '<li ' . $wrapper_attributes . '>';
166
+
167
+ $label = '';
168
+
169
+ if ( isset( $attributes['label'] ) ) {
170
+ $label .= wp_kses(
171
+ $attributes['label'],
172
+ array(
173
+ 'code' => array(),
174
+ 'em' => array(),
175
+ 'img' => array(
176
+ 'scale' => array(),
177
+ 'class' => array(),
178
+ 'style' => array(),
179
+ 'src' => array(),
180
+ 'alt' => array(),
181
+ ),
182
+ 's' => array(),
183
+ 'span' => array(
184
+ 'style' => array(),
185
+ ),
186
+ 'strong' => array(),
187
+ )
188
+ );
189
+ }
190
+
191
+ $html = '<li ' . $wrapper_attributes . '>';
167
192
 
168
193
  // If Submenus open on hover, we render an anchor tag with attributes.
169
194
  // If submenu icons are set to show, we also render a submenu button, so the submenu can be opened on click.
@@ -193,27 +218,7 @@ function render_block_core_navigation_submenu( $attributes, $content, $block ) {
193
218
  $html .= '>';
194
219
  // End appending HTML attributes to anchor tag.
195
220
 
196
- if ( isset( $attributes['label'] ) ) {
197
- $html .= wp_kses(
198
- $attributes['label'],
199
- array(
200
- 'code' => array(),
201
- 'em' => array(),
202
- 'img' => array(
203
- 'scale' => array(),
204
- 'class' => array(),
205
- 'style' => array(),
206
- 'src' => array(),
207
- 'alt' => array(),
208
- ),
209
- 's' => array(),
210
- 'span' => array(
211
- 'style' => array(),
212
- ),
213
- 'strong' => array(),
214
- )
215
- );
216
- }
221
+ $html .= $label;
217
222
 
218
223
  $html .= '</a>';
219
224
  // End anchor tag content.
@@ -221,36 +226,16 @@ function render_block_core_navigation_submenu( $attributes, $content, $block ) {
221
226
  if ( $show_submenu_indicators ) {
222
227
  // The submenu icon is rendered in a button here
223
228
  // so that there's a clickable elment to open the submenu.
224
- $html .= '<button class="wp-block-navigation__submenu-icon wp-block-navigation-submenu__toggle" aria-expanded="false">' . block_core_navigation_submenu_render_submenu_icon() . '</button>';
229
+ $html .= '<button aria-label="' . $label . ' ' . __( 'submenu', 'gutenberg' ) . '" class="wp-block-navigation__submenu-icon wp-block-navigation-submenu__toggle" aria-expanded="false">' . block_core_navigation_submenu_render_submenu_icon() . '</button>';
225
230
  }
226
231
  } else {
227
232
  // If menus open on click, we render the parent as a button.
228
- $html .= '<button class="wp-block-navigation-item__content wp-block-navigation-submenu__toggle" aria-expanded="false">';
233
+ $html .= '<button aria-label="' . $label . ' ' . __( 'submenu', 'gutenberg' ) . '" class="wp-block-navigation-item__content wp-block-navigation-submenu__toggle" aria-expanded="false">';
229
234
 
230
235
  // Wrap title with span to isolate it from submenu icon.
231
236
  $html .= '<span class="wp-block-navigation-item__label">';
232
237
 
233
- if ( isset( $attributes['label'] ) ) {
234
- $html .= wp_kses(
235
- $attributes['label'],
236
- array(
237
- 'code' => array(),
238
- 'em' => array(),
239
- 'img' => array(
240
- 'scale' => array(),
241
- 'class' => array(),
242
- 'style' => array(),
243
- 'src' => array(),
244
- 'alt' => array(),
245
- ),
246
- 's' => array(),
247
- 'span' => array(
248
- 'style' => array(),
249
- ),
250
- 'strong' => array(),
251
- )
252
- );
253
- }
238
+ $html .= $label;
254
239
 
255
240
  $html .= '</span>';
256
241