-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.php
1456 lines (1268 loc) · 52.2 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Server-side rendering of the `core/navigation` block.
*
* @package WordPress
*/
/**
* Helper functions used to render the navigation block.
*/
class WP_Navigation_Block_Renderer {
/**
* Used to determine which blocks are wrapped in an <li>.
*
* @var array
*/
private static $nav_blocks_wrapped_in_list_item = array(
'core/navigation-link',
'core/home-link',
'core/site-title',
'core/site-logo',
'core/navigation-submenu',
);
/**
* Used to determine which blocks need an <li> wrapper.
*
* @var array
*/
private static $needs_list_item_wrapper = array(
'core/site-title',
'core/site-logo',
);
/**
* Keeps track of all the navigation names that have been seen.
*
* @var array
*/
private static $seen_menu_names = array();
/**
* Returns whether or not this is responsive navigation.
*
* @param array $attributes The block attributes.
* @return bool Returns whether or not this is responsive navigation.
*/
private static function is_responsive( $attributes ) {
/**
* This is for backwards compatibility after the `isResponsive` attribute was been removed.
*/
$has_old_responsive_attribute = ! empty( $attributes['isResponsive'] ) && $attributes['isResponsive'];
return isset( $attributes['overlayMenu'] ) && 'never' !== $attributes['overlayMenu'] || $has_old_responsive_attribute;
}
/**
* Returns whether or not a navigation has a submenu.
*
* @param WP_Block_List $inner_blocks The list of inner blocks.
* @return bool Returns whether or not a navigation has a submenu.
*/
private static function has_submenus( $inner_blocks ) {
foreach ( $inner_blocks as $inner_block ) {
$inner_block_content = $inner_block->render();
$p = new WP_HTML_Tag_Processor( $inner_block_content );
if ( $p->next_tag(
array(
'name' => 'LI',
'class_name' => 'has-child',
)
) ) {
return true;
}
}
return false;
}
/**
* Determine whether the navigation blocks is interactive.
*
* @param array $attributes The block attributes.
* @param WP_Block_List $inner_blocks The list of inner blocks.
* @return bool Returns whether or not to load the view script.
*/
private static function is_interactive( $attributes, $inner_blocks ) {
$has_submenus = static::has_submenus( $inner_blocks );
$is_responsive_menu = static::is_responsive( $attributes );
return ( $has_submenus && ( $attributes['openSubmenusOnClick'] || $attributes['showSubmenuIcon'] ) ) || $is_responsive_menu;
}
/**
* Returns whether or not a block needs a list item wrapper.
*
* @param WP_Block $block The block.
* @return bool Returns whether or not a block needs a list item wrapper.
*/
private static function does_block_need_a_list_item_wrapper( $block ) {
return in_array( $block->name, static::$needs_list_item_wrapper, true );
}
/**
* Returns the markup for a single inner block.
*
* @param WP_Block $inner_block The inner block.
* @return string Returns the markup for a single inner block.
*/
private static function get_markup_for_inner_block( $inner_block ) {
$inner_block_content = $inner_block->render();
if ( ! empty( $inner_block_content ) ) {
if ( static::does_block_need_a_list_item_wrapper( $inner_block ) ) {
return '<li class="wp-block-navigation-item">' . $inner_block_content . '</li>';
}
return $inner_block_content;
}
}
/**
* Returns the html for the inner blocks of the navigation block.
*
* @param array $attributes The block attributes.
* @param WP_Block_List $inner_blocks The list of inner blocks.
* @return string Returns the html for the inner blocks of the navigation block.
*/
private static function get_inner_blocks_html( $attributes, $inner_blocks ) {
$has_submenus = static::has_submenus( $inner_blocks );
$is_interactive = static::is_interactive( $attributes, $inner_blocks );
$style = static::get_styles( $attributes );
$class = static::get_classes( $attributes );
$container_attributes = get_block_wrapper_attributes(
array(
'class' => 'wp-block-navigation__container ' . $class,
'style' => $style,
)
);
$inner_blocks_html = '';
$is_list_open = false;
foreach ( $inner_blocks as $inner_block ) {
$is_list_item = in_array( $inner_block->name, static::$nav_blocks_wrapped_in_list_item, true );
if ( $is_list_item && ! $is_list_open ) {
$is_list_open = true;
$inner_blocks_html .= sprintf(
'<ul %1$s>',
$container_attributes
);
}
if ( ! $is_list_item && $is_list_open ) {
$is_list_open = false;
$inner_blocks_html .= '</ul>';
}
$inner_blocks_html .= static::get_markup_for_inner_block( $inner_block );
}
if ( $is_list_open ) {
$inner_blocks_html .= '</ul>';
}
// Add directives to the submenu if needed.
if ( $has_submenus && $is_interactive ) {
$tags = new WP_HTML_Tag_Processor( $inner_blocks_html );
$inner_blocks_html = block_core_navigation_add_directives_to_submenu( $tags, $attributes );
}
return $inner_blocks_html;
}
/**
* Gets the inner blocks for the navigation block from the navigation post.
*
* @param array $attributes The block attributes.
* @return WP_Block_List Returns the inner blocks for the navigation block.
*/
private static function get_inner_blocks_from_navigation_post( $attributes ) {
$navigation_post = get_post( $attributes['ref'] );
if ( ! isset( $navigation_post ) ) {
return new WP_Block_List( array(), $attributes );
}
// Only published posts are valid. If this is changed then a corresponding change
// must also be implemented in `use-navigation-menu.js`.
if ( 'publish' === $navigation_post->post_status ) {
$parsed_blocks = parse_blocks( $navigation_post->post_content );
// 'parse_blocks' includes a null block with '\n\n' as the content when
// it encounters whitespace. This code strips it.
$blocks = block_core_navigation_filter_out_empty_blocks( $parsed_blocks );
if ( function_exists( 'get_hooked_block_markup' ) ) {
// Run Block Hooks algorithm to inject hooked blocks.
$markup = block_core_navigation_insert_hooked_blocks( $blocks, $navigation_post );
$root_nav_block = parse_blocks( $markup )[0];
$blocks = isset( $root_nav_block['innerBlocks'] ) ? $root_nav_block['innerBlocks'] : $blocks;
}
// TODO - this uses the full navigation block attributes for the
// context which could be refined.
return new WP_Block_List( $blocks, $attributes );
}
}
/**
* Gets the inner blocks for the navigation block from the fallback.
*
* @param array $attributes The block attributes.
* @return WP_Block_List Returns the inner blocks for the navigation block.
*/
private static function get_inner_blocks_from_fallback( $attributes ) {
$fallback_blocks = block_core_navigation_get_fallback_blocks();
// Fallback my have been filtered so do basic test for validity.
if ( empty( $fallback_blocks ) || ! is_array( $fallback_blocks ) ) {
return new WP_Block_List( array(), $attributes );
}
return new WP_Block_List( $fallback_blocks, $attributes );
}
/**
* Gets the inner blocks for the navigation block.
*
* @param array $attributes The block attributes.
* @param WP_Block $block The parsed block.
* @return WP_Block_List Returns the inner blocks for the navigation block.
*/
private static function get_inner_blocks( $attributes, $block ) {
$inner_blocks = $block->inner_blocks;
// Ensure that blocks saved with the legacy ref attribute name (navigationMenuId) continue to render.
if ( array_key_exists( 'navigationMenuId', $attributes ) ) {
$attributes['ref'] = $attributes['navigationMenuId'];
}
// If:
// - the gutenberg plugin is active
// - `__unstableLocation` is defined
// - we have menu items at the defined location
// - we don't have a relationship to a `wp_navigation` Post (via `ref`).
// ...then create inner blocks from the classic menu assigned to that location.
if (
defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN &&
array_key_exists( '__unstableLocation', $attributes ) &&
! array_key_exists( 'ref', $attributes ) &&
! empty( block_core_navigation_get_menu_items_at_location( $attributes['__unstableLocation'] ) )
) {
$inner_blocks = block_core_navigation_get_inner_blocks_from_unstable_location( $attributes );
}
// Load inner blocks from the navigation post.
if ( array_key_exists( 'ref', $attributes ) ) {
$inner_blocks = static::get_inner_blocks_from_navigation_post( $attributes );
}
// If there are no inner blocks then fallback to rendering an appropriate fallback.
if ( empty( $inner_blocks ) ) {
$inner_blocks = static::get_inner_blocks_from_fallback( $attributes );
}
/**
* Filter navigation block $inner_blocks.
* Allows modification of a navigation block menu items.
*
* @since 6.1.0
*
* @param \WP_Block_List $inner_blocks
*/
$inner_blocks = apply_filters( 'block_core_navigation_render_inner_blocks', $inner_blocks );
$post_ids = block_core_navigation_get_post_ids( $inner_blocks );
if ( $post_ids ) {
_prime_post_caches( $post_ids, false, false );
}
return $inner_blocks;
}
/**
* Gets the name of the current navigation, if it has one.
*
* @param array $attributes The block attributes.
* @return string Returns the name of the navigation.
*/
private static function get_navigation_name( $attributes ) {
$navigation_name = $attributes['ariaLabel'] ?? '';
// Load the navigation post.
if ( array_key_exists( 'ref', $attributes ) ) {
$navigation_post = get_post( $attributes['ref'] );
if ( ! isset( $navigation_post ) ) {
return $navigation_name;
}
// Only published posts are valid. If this is changed then a corresponding change
// must also be implemented in `use-navigation-menu.js`.
if ( 'publish' === $navigation_post->post_status ) {
$navigation_name = $navigation_post->post_title;
// This is used to count the number of times a navigation name has been seen,
// so that we can ensure every navigation has a unique id.
if ( isset( static::$seen_menu_names[ $navigation_name ] ) ) {
++static::$seen_menu_names[ $navigation_name ];
} else {
static::$seen_menu_names[ $navigation_name ] = 1;
}
}
}
return $navigation_name;
}
/**
* Returns the layout class for the navigation block.
*
* @param array $attributes The block attributes.
* @return string Returns the layout class for the navigation block.
*/
private static function get_layout_class( $attributes ) {
$layout_justification = array(
'left' => 'items-justified-left',
'right' => 'items-justified-right',
'center' => 'items-justified-center',
'space-between' => 'items-justified-space-between',
);
$layout_class = '';
if (
isset( $attributes['layout']['justifyContent'] ) &&
isset( $layout_justification[ $attributes['layout']['justifyContent'] ] )
) {
$layout_class .= $layout_justification[ $attributes['layout']['justifyContent'] ];
}
if ( isset( $attributes['layout']['orientation'] ) && 'vertical' === $attributes['layout']['orientation'] ) {
$layout_class .= ' is-vertical';
}
if ( isset( $attributes['layout']['flexWrap'] ) && 'nowrap' === $attributes['layout']['flexWrap'] ) {
$layout_class .= ' no-wrap';
}
return $layout_class;
}
/**
* Return classes for the navigation block.
*
* @param array $attributes The block attributes.
* @return string Returns the classes for the navigation block.
*/
private static function get_classes( $attributes ) {
// Restore legacy classnames for submenu positioning.
$layout_class = static::get_layout_class( $attributes );
$colors = block_core_navigation_build_css_colors( $attributes );
$font_sizes = block_core_navigation_build_css_font_sizes( $attributes );
$is_responsive_menu = static::is_responsive( $attributes );
// Manually add block support text decoration as CSS class.
$text_decoration = $attributes['style']['typography']['textDecoration'] ?? null;
$text_decoration_class = sprintf( 'has-text-decoration-%s', $text_decoration );
// Sets the is-collapsed class when the navigation is set to always use the overlay.
// This saves us from needing to do this check in the view.js file (see the collapseNav function).
$is_collapsed_class = static::is_always_overlay( $attributes ) ? array( 'is-collapsed' ) : array();
$classes = array_merge(
$colors['css_classes'],
$font_sizes['css_classes'],
$is_responsive_menu ? array( 'is-responsive' ) : array(),
$layout_class ? array( $layout_class ) : array(),
$text_decoration ? array( $text_decoration_class ) : array(),
$is_collapsed_class
);
return implode( ' ', $classes );
}
private static function is_always_overlay( $attributes ) {
return isset( $attributes['overlayMenu'] ) && 'always' === $attributes['overlayMenu'];
}
/**
* Get styles for the navigation block.
*
* @param array $attributes The block attributes.
* @return string Returns the styles for the navigation block.
*/
private static function get_styles( $attributes ) {
$colors = block_core_navigation_build_css_colors( $attributes );
$font_sizes = block_core_navigation_build_css_font_sizes( $attributes );
$block_styles = isset( $attributes['styles'] ) ? $attributes['styles'] : '';
return $block_styles . $colors['inline_styles'] . $font_sizes['inline_styles'];
}
/**
* Get the responsive container markup
*
* @param array $attributes The block attributes.
* @param WP_Block_List $inner_blocks The list of inner blocks.
* @param string $inner_blocks_html The markup for the inner blocks.
* @return string Returns the container markup.
*/
private static function get_responsive_container_markup( $attributes, $inner_blocks, $inner_blocks_html ) {
$is_interactive = static::is_interactive( $attributes, $inner_blocks );
$colors = block_core_navigation_build_css_colors( $attributes );
$modal_unique_id = wp_unique_id( 'modal-' );
$responsive_container_classes = array(
'wp-block-navigation__responsive-container',
implode( ' ', $colors['overlay_css_classes'] ),
);
$open_button_classes = array(
'wp-block-navigation__responsive-container-open',
);
$should_display_icon_label = isset( $attributes['hasIcon'] ) && true === $attributes['hasIcon'];
$toggle_button_icon = '<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><rect x="4" y="7.5" width="16" height="1.5" /><rect x="4" y="15" width="16" height="1.5" /></svg>';
if ( isset( $attributes['icon'] ) ) {
if ( 'menu' === $attributes['icon'] ) {
$toggle_button_icon = '<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M5 5v1.5h14V5H5zm0 7.8h14v-1.5H5v1.5zM5 19h14v-1.5H5V19z" /></svg>';
}
}
$toggle_button_content = $should_display_icon_label ? $toggle_button_icon : __( 'Menu' );
$toggle_close_button_icon = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false"><path d="M13 11.8l6.1-6.3-1-1-6.1 6.2-6.1-6.2-1 1 6.1 6.3-6.5 6.7 1 1 6.5-6.6 6.5 6.6 1-1z"></path></svg>';
$toggle_close_button_content = $should_display_icon_label ? $toggle_close_button_icon : __( 'Close' );
$toggle_aria_label_open = $should_display_icon_label ? 'aria-label="' . __( 'Open menu' ) . '"' : ''; // Open button label.
$toggle_aria_label_close = $should_display_icon_label ? 'aria-label="' . __( 'Close menu' ) . '"' : ''; // Close button label.
// Add Interactivity API directives to the markup if needed.
$open_button_directives = '';
$responsive_container_directives = '';
$responsive_dialog_directives = '';
$close_button_directives = '';
if ( $is_interactive ) {
$open_button_directives = '
data-wp-on--click="actions.openMenuOnClick"
data-wp-on--keydown="actions.handleMenuKeydown"
';
$responsive_container_directives = '
data-wp-class--has-modal-open="state.isMenuOpen"
data-wp-class--is-menu-open="state.isMenuOpen"
data-wp-watch="callbacks.initMenu"
data-wp-on--keydown="actions.handleMenuKeydown"
data-wp-on--focusout="actions.handleMenuFocusout"
tabindex="-1"
';
$responsive_dialog_directives = '
data-wp-bind--aria-modal="state.ariaModal"
data-wp-bind--aria-label="state.ariaLabel"
data-wp-bind--role="state.roleAttribute"
';
$close_button_directives = '
data-wp-on--click="actions.closeMenuOnClick"
';
$responsive_container_content_directives = '
data-wp-watch="callbacks.focusFirstElement"
';
}
return sprintf(
'<button aria-haspopup="dialog" %3$s class="%6$s" %10$s>%8$s</button>
<div class="%5$s" style="%7$s" id="%1$s" %11$s>
<div class="wp-block-navigation__responsive-close" tabindex="-1">
<div class="wp-block-navigation__responsive-dialog" %12$s>
<button %4$s class="wp-block-navigation__responsive-container-close" %13$s>%9$s</button>
<div class="wp-block-navigation__responsive-container-content" %14$s id="%1$s-content">
%2$s
</div>
</div>
</div>
</div>',
esc_attr( $modal_unique_id ),
$inner_blocks_html,
$toggle_aria_label_open,
$toggle_aria_label_close,
esc_attr( implode( ' ', $responsive_container_classes ) ),
esc_attr( implode( ' ', $open_button_classes ) ),
esc_attr( safecss_filter_attr( $colors['overlay_inline_styles'] ) ),
$toggle_button_content,
$toggle_close_button_content,
$open_button_directives,
$responsive_container_directives,
$responsive_dialog_directives,
$close_button_directives,
$responsive_container_content_directives
);
}
/**
* Get the wrapper attributes
*
* @param array $attributes The block attributes.
* @param WP_Block_List $inner_blocks A list of inner blocks.
* @return string Returns the navigation block markup.
*/
private static function get_nav_wrapper_attributes( $attributes, $inner_blocks ) {
$nav_menu_name = static::get_unique_navigation_name( $attributes );
$is_interactive = static::is_interactive( $attributes, $inner_blocks );
$is_responsive_menu = static::is_responsive( $attributes );
$style = static::get_styles( $attributes );
$class = static::get_classes( $attributes );
$wrapper_attributes = get_block_wrapper_attributes(
array(
'class' => $class,
'style' => $style,
'aria-label' => $nav_menu_name,
)
);
if ( $is_responsive_menu ) {
$nav_element_directives = static::get_nav_element_directives( $is_interactive, $attributes );
$wrapper_attributes .= ' ' . $nav_element_directives;
}
return $wrapper_attributes;
}
/**
* Gets the nav element directives.
*
* @param bool $is_interactive Whether the block is interactive.
* @param array $attributes The block attributes.
* @return string the directives for the navigation element.
*/
private static function get_nav_element_directives( $is_interactive, $attributes ) {
if ( ! $is_interactive ) {
return '';
}
// When adding to this array be mindful of security concerns.
$nav_element_context = wp_json_encode(
array(
'overlayOpenedBy' => array(),
'type' => 'overlay',
'roleAttribute' => '',
'ariaLabel' => __( 'Menu' ),
),
JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP
);
$nav_element_directives = '
data-wp-interactive=\'{"namespace":"core/navigation"}\'
data-wp-context=\'' . $nav_element_context . '\'
';
/*
* When the navigation's 'overlayMenu' attribute is set to 'always', JavaScript
* is not needed for collapsing the menu because the class is set manually.
*/
if ( ! static::is_always_overlay( $attributes ) ) {
$nav_element_directives .= 'data-wp-init="callbacks.initNav"';
$nav_element_directives .= ' '; // space separator
$nav_element_directives .= 'data-wp-class--is-collapsed="context.isCollapsed"';
}
return $nav_element_directives;
}
/**
* Handle view script module loading.
*
* @param array $attributes The block attributes.
* @param WP_Block $block The parsed block.
* @param WP_Block_List $inner_blocks The list of inner blocks.
*/
private static function handle_view_script_module_loading( $attributes, $block, $inner_blocks ) {
if ( static::is_interactive( $attributes, $inner_blocks ) ) {
wp_enqueue_script_module( '@wordpress/block-library/navigation' );
}
}
/**
* Returns the markup for the navigation block.
*
* @param array $attributes The block attributes.
* @param WP_Block_List $inner_blocks The list of inner blocks.
* @return string Returns the navigation wrapper markup.
*/
private static function get_wrapper_markup( $attributes, $inner_blocks ) {
$inner_blocks_html = static::get_inner_blocks_html( $attributes, $inner_blocks );
if ( static::is_responsive( $attributes ) ) {
return static::get_responsive_container_markup( $attributes, $inner_blocks, $inner_blocks_html );
}
return $inner_blocks_html;
}
/**
* Returns a unique name for the navigation.
*
* @param array $attributes The block attributes.
* @return string Returns a unique name for the navigation.
*/
private static function get_unique_navigation_name( $attributes ) {
$nav_menu_name = static::get_navigation_name( $attributes );
// If the menu name has been used previously then append an ID
// to the name to ensure uniqueness across a given post.
if ( isset( static::$seen_menu_names[ $nav_menu_name ] ) && static::$seen_menu_names[ $nav_menu_name ] > 1 ) {
$count = static::$seen_menu_names[ $nav_menu_name ];
$nav_menu_name = $nav_menu_name . ' ' . ( $count );
}
return $nav_menu_name;
}
/**
* Renders the navigation block.
*
* @param array $attributes The block attributes.
* @param string $content The saved content.
* @param WP_Block $block The parsed block.
* @return string Returns the navigation block markup.
*/
public static function render( $attributes, $content, $block ) {
/**
* Deprecated:
* The rgbTextColor and rgbBackgroundColor attributes
* have been deprecated in favor of
* customTextColor and customBackgroundColor ones.
* Move the values from old attrs to the new ones.
*/
if ( isset( $attributes['rgbTextColor'] ) && empty( $attributes['textColor'] ) ) {
$attributes['customTextColor'] = $attributes['rgbTextColor'];
}
if ( isset( $attributes['rgbBackgroundColor'] ) && empty( $attributes['backgroundColor'] ) ) {
$attributes['customBackgroundColor'] = $attributes['rgbBackgroundColor'];
}
unset( $attributes['rgbTextColor'], $attributes['rgbBackgroundColor'] );
$inner_blocks = static::get_inner_blocks( $attributes, $block );
// Prevent navigation blocks referencing themselves from rendering.
if ( block_core_navigation_block_contains_core_navigation( $inner_blocks ) ) {
return '';
}
static::handle_view_script_module_loading( $attributes, $block, $inner_blocks );
return sprintf(
'<nav %1$s>%2$s</nav>',
static::get_nav_wrapper_attributes( $attributes, $inner_blocks ),
static::get_wrapper_markup( $attributes, $inner_blocks )
);
}
}
// These functions are used for the __unstableLocation feature and only active
// when the gutenberg plugin is active.
if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) {
/**
* Returns the menu items for a WordPress menu location.
*
* @param string $location The menu location.
* @return array Menu items for the location.
*/
function block_core_navigation_get_menu_items_at_location( $location ) {
if ( empty( $location ) ) {
return;
}
// Build menu data. The following approximates the code in
// `wp_nav_menu()` and `gutenberg_output_block_nav_menu`.
// Find the location in the list of locations, returning early if the
// location can't be found.
$locations = get_nav_menu_locations();
if ( ! isset( $locations[ $location ] ) ) {
return;
}
// Get the menu from the location, returning early if there is no
// menu or there was an error.
$menu = wp_get_nav_menu_object( $locations[ $location ] );
if ( ! $menu || is_wp_error( $menu ) ) {
return;
}
$menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
_wp_menu_item_classes_by_context( $menu_items );
return $menu_items;
}
/**
* Sorts a standard array of menu items into a nested structure keyed by the
* id of the parent menu.
*
* @param array $menu_items Menu items to sort.
* @return array An array keyed by the id of the parent menu where each element
* is an array of menu items that belong to that parent.
*/
function block_core_navigation_sort_menu_items_by_parent_id( $menu_items ) {
$sorted_menu_items = array();
foreach ( (array) $menu_items as $menu_item ) {
$sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
}
unset( $menu_items, $menu_item );
$menu_items_by_parent_id = array();
foreach ( $sorted_menu_items as $menu_item ) {
$menu_items_by_parent_id[ $menu_item->menu_item_parent ][] = $menu_item;
}
return $menu_items_by_parent_id;
}
/**
* Gets the inner blocks for the navigation block from the unstable location attribute.
*
* @param array $attributes The block attributes.
* @return WP_Block_List Returns the inner blocks for the navigation block.
*/
function block_core_navigation_get_inner_blocks_from_unstable_location( $attributes ) {
$menu_items = block_core_navigation_get_menu_items_at_location( $attributes['__unstableLocation'] );
if ( empty( $menu_items ) ) {
return new WP_Block_List( array(), $attributes );
}
$menu_items_by_parent_id = block_core_navigation_sort_menu_items_by_parent_id( $menu_items );
$parsed_blocks = block_core_navigation_parse_blocks_from_menu_items( $menu_items_by_parent_id[0], $menu_items_by_parent_id );
return new WP_Block_List( $parsed_blocks, $attributes );
}
}
/**
* Add Interactivity API directives to the navigation-submenu and page-list
* blocks markup using the Tag Processor.
*
* @param WP_HTML_Tag_Processor $tags Markup of the navigation block.
* @param array $block_attributes Block attributes.
*
* @return string Submenu markup with the directives injected.
*/
function block_core_navigation_add_directives_to_submenu( $tags, $block_attributes ) {
while ( $tags->next_tag(
array(
'tag_name' => 'LI',
'class_name' => 'has-child',
)
) ) {
// Add directives to the parent `<li>`.
$tags->set_attribute( 'data-wp-interactive', '{ "namespace": "core/navigation" }' );
$tags->set_attribute( 'data-wp-context', '{ "submenuOpenedBy": {}, "type": "submenu" }' );
$tags->set_attribute( 'data-wp-watch', 'callbacks.initMenu' );
$tags->set_attribute( 'data-wp-on--focusout', 'actions.handleMenuFocusout' );
$tags->set_attribute( 'data-wp-on--keydown', 'actions.handleMenuKeydown' );
// This is a fix for Safari. Without it, Safari doesn't change the active
// element when the user clicks on a button. It can be removed once we add
// an overlay to capture the clicks, instead of relying on the focusout
// event.
$tags->set_attribute( 'tabindex', '-1' );
if ( ! isset( $block_attributes['openSubmenusOnClick'] ) || false === $block_attributes['openSubmenusOnClick'] ) {
$tags->set_attribute( 'data-wp-on--mouseenter', 'actions.openMenuOnHover' );
$tags->set_attribute( 'data-wp-on--mouseleave', 'actions.closeMenuOnHover' );
}
// Add directives to the toggle submenu button.
if ( $tags->next_tag(
array(
'tag_name' => 'BUTTON',
'class_name' => 'wp-block-navigation-submenu__toggle',
)
) ) {
$tags->set_attribute( 'data-wp-on--click', 'actions.toggleMenuOnClick' );
$tags->set_attribute( 'data-wp-bind--aria-expanded', 'state.isMenuOpen' );
// The `aria-expanded` attribute for SSR is already added in the submenu block.
}
// Add directives to the submenu.
if ( $tags->next_tag(
array(
'tag_name' => 'UL',
'class_name' => 'wp-block-navigation__submenu-container',
)
) ) {
$tags->set_attribute( 'data-wp-on--focus', 'actions.openMenuOnFocus' );
}
// Iterate through subitems if exist.
block_core_navigation_add_directives_to_submenu( $tags, $block_attributes );
}
return $tags->get_updated_html();
}
/**
* Build an array with CSS classes and inline styles defining the colors
* which will be applied to the navigation markup in the front-end.
*
* @param array $attributes Navigation block attributes.
*
* @return array Colors CSS classes and inline styles.
*/
function block_core_navigation_build_css_colors( $attributes ) {
$colors = array(
'css_classes' => array(),
'inline_styles' => '',
'overlay_css_classes' => array(),
'overlay_inline_styles' => '',
);
// Text color.
$has_named_text_color = array_key_exists( 'textColor', $attributes );
$has_custom_text_color = array_key_exists( 'customTextColor', $attributes );
// If has text color.
if ( $has_custom_text_color || $has_named_text_color ) {
// Add has-text-color class.
$colors['css_classes'][] = 'has-text-color';
}
if ( $has_named_text_color ) {
// Add the color class.
$colors['css_classes'][] = sprintf( 'has-%s-color', $attributes['textColor'] );
} elseif ( $has_custom_text_color ) {
// Add the custom color inline style.
$colors['inline_styles'] .= sprintf( 'color: %s;', $attributes['customTextColor'] );
}
// Background color.
$has_named_background_color = array_key_exists( 'backgroundColor', $attributes );
$has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes );
// If has background color.
if ( $has_custom_background_color || $has_named_background_color ) {
// Add has-background class.
$colors['css_classes'][] = 'has-background';
}
if ( $has_named_background_color ) {
// Add the background-color class.
$colors['css_classes'][] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] );
} elseif ( $has_custom_background_color ) {
// Add the custom background-color inline style.
$colors['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['customBackgroundColor'] );
}
// Overlay text color.
$has_named_overlay_text_color = array_key_exists( 'overlayTextColor', $attributes );
$has_custom_overlay_text_color = array_key_exists( 'customOverlayTextColor', $attributes );
// If has overlay text color.
if ( $has_custom_overlay_text_color || $has_named_overlay_text_color ) {
// Add has-text-color class.
$colors['overlay_css_classes'][] = 'has-text-color';
}
if ( $has_named_overlay_text_color ) {
// Add the overlay color class.
$colors['overlay_css_classes'][] = sprintf( 'has-%s-color', $attributes['overlayTextColor'] );
} elseif ( $has_custom_overlay_text_color ) {
// Add the custom overlay color inline style.
$colors['overlay_inline_styles'] .= sprintf( 'color: %s;', $attributes['customOverlayTextColor'] );
}
// Overlay background color.
$has_named_overlay_background_color = array_key_exists( 'overlayBackgroundColor', $attributes );
$has_custom_overlay_background_color = array_key_exists( 'customOverlayBackgroundColor', $attributes );
// If has overlay background color.
if ( $has_custom_overlay_background_color || $has_named_overlay_background_color ) {
// Add has-background class.
$colors['overlay_css_classes'][] = 'has-background';
}
if ( $has_named_overlay_background_color ) {
// Add the overlay background-color class.
$colors['overlay_css_classes'][] = sprintf( 'has-%s-background-color', $attributes['overlayBackgroundColor'] );
} elseif ( $has_custom_overlay_background_color ) {
// Add the custom overlay background-color inline style.
$colors['overlay_inline_styles'] .= sprintf( 'background-color: %s;', $attributes['customOverlayBackgroundColor'] );
}
return $colors;
}
/**
* Build an array with CSS classes and inline styles defining the font sizes
* which will be applied to the navigation markup in the front-end.
*
* @param array $attributes Navigation block attributes.
*
* @return array Font size CSS classes and inline styles.
*/
function block_core_navigation_build_css_font_sizes( $attributes ) {
// CSS classes.
$font_sizes = array(
'css_classes' => array(),
'inline_styles' => '',
);
$has_named_font_size = array_key_exists( 'fontSize', $attributes );
$has_custom_font_size = array_key_exists( 'customFontSize', $attributes );
if ( $has_named_font_size ) {
// Add the font size class.
$font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $attributes['fontSize'] );
} elseif ( $has_custom_font_size ) {
// Add the custom font size inline style.
$font_sizes['inline_styles'] = sprintf( 'font-size: %spx;', $attributes['customFontSize'] );
}
return $font_sizes;
}
/**
* Returns the top-level submenu SVG chevron icon.
*
* @return string
*/
function block_core_navigation_render_submenu_icon() {
return '<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 12 12" fill="none" aria-hidden="true" focusable="false"><path d="M1.50002 4L6.00002 8L10.5 4" stroke-width="1.5"></path></svg>';
}
/**
* Filter out empty "null" blocks from the block list.
* 'parse_blocks' includes a null block with '\n\n' as the content when
* it encounters whitespace. This is not a bug but rather how the parser
* is designed.
*
* @param array $parsed_blocks the parsed blocks to be normalized.
* @return array the normalized parsed blocks.
*/
function block_core_navigation_filter_out_empty_blocks( $parsed_blocks ) {
$filtered = array_filter(
$parsed_blocks,
static function ( $block ) {
return isset( $block['blockName'] );
}
);
// Reset keys.
return array_values( $filtered );
}
/**
* Returns true if the navigation block contains a nested navigation block.
*
* @param WP_Block_List $inner_blocks Inner block instance to be normalized.
* @return bool true if the navigation block contains a nested navigation block.
*/
function block_core_navigation_block_contains_core_navigation( $inner_blocks ) {
foreach ( $inner_blocks as $block ) {
if ( 'core/navigation' === $block->name ) {
return true;
}
if ( $block->inner_blocks && block_core_navigation_block_contains_core_navigation( $block->inner_blocks ) ) {
return true;
}
}
return false;
}
/**
* Retrieves the appropriate fallback to be used on the front of the
* site when there is no menu assigned to the Nav block.
*
* This aims to mirror how the fallback mechanic for wp_nav_menu works.
* See https://developer.wordpress.org/reference/functions/wp_nav_menu/#more-information.
*
* @return array the array of blocks to be used as a fallback.
*/
function block_core_navigation_get_fallback_blocks() {
$page_list_fallback = array(
array(
'blockName' => 'core/page-list',
),
);
$registry = WP_Block_Type_Registry::get_instance();
// If `core/page-list` is not registered then return empty blocks.
$fallback_blocks = $registry->is_registered( 'core/page-list' ) ? $page_list_fallback : array();
$navigation_post = WP_Navigation_Fallback::get_fallback();
// Use the first non-empty Navigation as fallback if available.
if ( $navigation_post ) {
$parsed_blocks = parse_blocks( $navigation_post->post_content );
$maybe_fallback = block_core_navigation_filter_out_empty_blocks( $parsed_blocks );
// Normalizing blocks may result in an empty array of blocks if they were all `null` blocks.
// In this case default to the (Page List) fallback.
$fallback_blocks = ! empty( $maybe_fallback ) ? $maybe_fallback : $fallback_blocks;
if ( function_exists( 'get_hooked_block_markup' ) ) {
// Run Block Hooks algorithm to inject hooked blocks.
// We have to run it here because we need the post ID of the Navigation block to track ignored hooked blocks.
$markup = block_core_navigation_insert_hooked_blocks( $fallback_blocks, $navigation_post );
$blocks = parse_blocks( $markup );
if ( isset( $blocks[0]['innerBlocks'] ) ) {
$fallback_blocks = $blocks[0]['innerBlocks'];
}
}
}