-
Notifications
You must be signed in to change notification settings - Fork 30
/
blocks.php
1124 lines (948 loc) · 29.9 KB
/
blocks.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
namespace WordPressdotorg\MU_Plugins\Global_Header_Footer;
use Rosetta_Sites, WP_Post, WP_REST_Server, WP_Theme_JSON_Resolver;
defined( 'WPINC' ) || die();
add_action( 'init', __NAMESPACE__ . '\register_block_types' );
add_action( 'admin_bar_init', __NAMESPACE__ . '\remove_admin_bar_callback', 15 );
add_action( 'rest_api_init', __NAMESPACE__ . '\register_routes' );
add_action( 'enqueue_block_assets', __NAMESPACE__ . '\register_block_types_js' );
add_filter( 'wp_enqueue_scripts', __NAMESPACE__ . '\register_block_assets', 200 ); // Always last.
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\enqueue_compat_wp4_styles', 5 ); // Before any theme CSS.
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\unregister_classic_global_styles', 20 );
add_action( 'wp_head', __NAMESPACE__ . '\preload_google_fonts' );
add_filter( 'style_loader_src', __NAMESPACE__ . '\update_google_fonts_url', 10, 2 );
add_filter( 'render_block_core/navigation-link', __NAMESPACE__ . '\swap_submenu_arrow_svg' );
add_filter( 'render_block_core/search', __NAMESPACE__ . '\swap_header_search_action', 10, 2 );
/**
* Register block types
*
* These are intentionally missing arguments like `title`, `category`, `icon`, etc, because we don't want them
* showing up in the Block Inserter, regardless of which theme is running.
*/
function register_block_types() {
register_block_type(
'wporg/global-header',
array(
'title' => 'Global Header',
'render_callback' => __NAMESPACE__ . '\render_global_header',
'style' => 'wporg-global-header-footer',
'editor_style' => 'wporg-global-header-footer',
'script' => 'wporg-global-header-script',
)
);
register_block_type(
'wporg/global-footer',
array(
'title' => 'Global Footer',
'render_callback' => __NAMESPACE__ . '\render_global_footer',
'style' => 'wporg-global-header-footer',
'editor_style' => 'wporg-global-header-footer',
)
);
}
/**
* Register the script & stylesheet for use in the blocks.
*/
function register_block_assets() {
// Our custom login screen is technically a front-end page, so the script/style are enqueued by default.
// That's unnecessary because the header/footer isn't rendered in there.
if ( 'login.wordpress.org' === $_SERVER['SERVER_NAME'] ) {
return;
}
$suffix = is_rtl() ? '-rtl' : '';
// Load `block-library` styles first, so that our styles override them.
$style_dependencies = array( 'wp-block-library' );
if ( wp_style_is( 'wporg-global-fonts', 'registered' ) ) {
$style_dependencies[] = 'wporg-global-fonts';
}
wp_register_style(
'wporg-global-header-footer',
plugins_url( "/build/style$suffix.css", __FILE__ ),
$style_dependencies,
filemtime( __DIR__ . "/build/style$suffix.css" )
);
wp_register_script(
'wporg-global-header-script',
plugins_url( '/js/wporg-global-header-script.js', __FILE__ ),
array(),
filemtime( __DIR__ . '/js/wporg-global-header-script.js' ),
true
);
wp_localize_script(
'wporg-global-header-script',
'wporgGlobalHeaderI18n',
array(
'openSearchLabel' => __( 'Open Search', 'wporg' ),
'closeSearchLabel' => __( 'Close Search', 'wporg' ),
)
);
}
/**
* Remove the default margin-top added when the admin bar is used.
*
* The core handling uses `!important`, which overrides the sticky header offset in `common.pcss`.
*/
function remove_admin_bar_callback() {
remove_action( 'gp_head', '_admin_bar_bump_cb' );
remove_action( 'wp_head', '_admin_bar_bump_cb' );
}
/**
* Register REST API routes, so non-WP applications can integrate it.
*/
function register_routes() {
register_rest_route(
'global-header-footer/v1',
'header',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => __NAMESPACE__ . '\rest_render_global_header',
'permission_callback' => '__return_true',
),
)
);
register_rest_route(
'global-header-footer/v1',
'header/codex',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => __NAMESPACE__ . '\rest_render_codex_global_header',
'permission_callback' => '__return_true',
),
)
);
register_rest_route(
'global-header-footer/v1',
'header/planet',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => __NAMESPACE__ . '\rest_render_planet_global_header',
'permission_callback' => '__return_true',
),
)
);
register_rest_route(
'global-header-footer/v1',
'footer',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => __NAMESPACE__ . '\rest_render_global_footer',
'permission_callback' => '__return_true',
),
)
);
// Requesting this on another network would create an infinite loop.
if ( is_wporg_network() ) {
register_rest_route(
'global-header-footer/v1',
'styles',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => __NAMESPACE__ . '\rest_render_global_styles',
'permission_callback' => '__return_true',
),
)
);
}
}
/**
* Register block types in JS, for the editor.
*
* Blocks need to be registered in JS to show up in the editor. We can dynamically register the blocks using
* ServerSideRender, which will render the PHP callback. This runs through the existing blocks to find any
* matching `wporg/global-*` blocks, so it will match the header & footer, and any other pattern-blocks we
* might add in the future.
*
* Watch https://github.com/WordPress/gutenberg/issues/28734 for a possible core solution.
*/
function register_block_types_js() {
$blocks = \WP_Block_Type_Registry::get_instance()->get_all_registered();
$wporg_global_blocks = array_filter(
$blocks,
function ( $block ) {
return 'wporg/global-' === substr( $block->name, 0, 13 );
}
);
ob_start();
?>
( function( wp ) {
<?php foreach ( $wporg_global_blocks as $block ) : ?>
wp.blocks.registerBlockType(
'<?php echo esc_html( $block->name ); ?>',
{
title: '<?php echo esc_html( $block->title ); ?>',
edit: function( props ) {
return wp.element.createElement( wp.serverSideRender, {
block: '<?php echo esc_html( $block->name ); ?>',
attributes: props.attributes
} );
},
}
);
<?php endforeach; ?>
}( window.wp ));
<?php
wp_add_inline_script( 'wp-editor', ob_get_clean(), 'after' );
}
/**
* Filter the google fonts URL to use the "CSS2" version of the API.
*
* @param string $src The source URL of the enqueued style.
* @param string $handle The style's registered handle.
* @return string Updated URL for `open-sans`.
*/
function update_google_fonts_url( $src, $handle ) {
if ( 'open-sans' === $handle ) {
return 'https://fonts.googleapis.com/css2?family=Open+Sans:ital,wght@0,300;0,400;0,600;1,300;1,400;1,600&display=swap';
}
return $src;
}
/**
* Add preconnect resource hints for the Google Fonts API.
*/
function preload_google_fonts() {
echo '<link rel="preconnect" href="https://fonts.googleapis.com">';
echo '<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> ';
}
/**
* Output styles for themes that don't use `wp4-styles`. This provides compat with the classic header.php.
*/
function enqueue_compat_wp4_styles() {
// See https://wordpress.slack.com/archives/C02QB8GMM/p1642056619063500
if (
( defined( 'FEATURE_2021_GLOBAL_HEADER_FOOTER' ) && ! FEATURE_2021_GLOBAL_HEADER_FOOTER ) &&
( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST )
) {
return;
}
if (
( ! wp_is_block_theme() && ! current_theme_supports( 'wp4-styles' ) ) ||
( defined( 'REST_REQUEST' ) && REST_REQUEST )
) {
$suffix = is_rtl() ? '-rtl' : '';
wp_register_style(
'wp4-styles',
'https://s.w.org/style/wp4' . $suffix . '.css',
array( 'open-sans' ),
filemtime( WPORGPATH . '/style/wp4' . $suffix . '.css' )
);
wp_enqueue_style( 'wp4-styles' );
}
}
/**
* Unregister the `global-styles` from classic themes, to avoid overwriting our custom properties.
*/
function unregister_classic_global_styles() {
if ( wp_is_block_theme() ) {
return;
}
wp_dequeue_style( 'global-styles' );
wp_deregister_style( 'global-styles' );
}
/**
* Remove the wrapping element to preserve markup.
*
* Core and Gutenberg add a wrapper `div` for backwards-compatibility, but that is unnecessary here, and breaks
* CSS selectors.
*
* @see restore_inner_group_container()
*/
function remove_inner_group_container() {
if ( wp_is_block_theme() ) {
return;
}
remove_filter( 'render_block', 'wp_restore_group_inner_container' );
remove_filter( 'render_block', 'gutenberg_restore_group_inner_container' );
}
/**
* Restore the wrapping element to prevent side-effects on the content area.
*
* @see remove_inner_group_container()
*/
function restore_inner_group_container() {
if ( wp_is_block_theme() ) {
return;
}
if ( function_exists( 'gutenberg_restore_group_inner_container' ) ) {
add_filter( 'render_block', 'gutenberg_restore_group_inner_container', 10, 2 );
} else {
add_filter( 'render_block', 'wp_restore_group_inner_container', 10, 2 );
}
}
/**
* Render the global header via a REST request.
*
* @return string
*/
function rest_render_global_header( $request ) {
// Remove the theme stylesheet from rest requests.
add_filter( 'wp_enqueue_scripts', function() {
remove_theme_support( 'wp4-styles' );
wp_dequeue_style( 'wporg-style' );
wp_enqueue_style( 'dashicons' );
wp_enqueue_style( 'open-sans' );
}, 20 );
// Serve the request as HTML.
add_filter( 'rest_pre_serve_request', function( $served, $result ) {
header( 'Content-Type: text/html' );
header( 'X-Robots-Tag: noindex, follow' );
echo $result->get_data();
return true;
}, 10, 2 );
return render_global_header();
}
/**
* Render the global header via a REST request for the Codex with appropriate tags.
*
* @return string
*/
function rest_render_codex_global_header( $request ) {
add_action( 'wp_head', function() {
echo '<!-- [codex head meta] -->', "\n";
}, 1 );
add_action( 'wp_head', function() {
echo '<!-- [codex head scripts] -->', "\n";
}, 100 );
add_filter( 'body_class', function( $class ) {
return [
'wporg-responsive',
'wporg-codex'
];
} );
wp_enqueue_style( 'codex-wp4', 'https://s.w.org/style/codex-wp4.css', array( 'wp4-styles' ), 4 );
// Remove <title> tags.
remove_theme_support( 'title-tag' );
$markup = rest_render_global_header( $request );
$markup = preg_replace( '!<html[^>]+>!i', '<!-- [codex head html] -->', $markup );
return $markup;
}
/**
* Render the global header via a REST request for use with Planet.
*
* @return string
*/
function rest_render_planet_global_header( $request ) {
add_filter( 'pre_get_document_title', function() {
return 'Planet — WordPress.org';
} );
add_filter( 'wporg_canonical_url', function() {
return 'https://planet.wordpress.org/';
} );
add_filter( 'body_class', function( $class ) {
return [
'wporg-responsive',
'wporg-planet'
];
} );
return rest_render_global_header( $request );
}
/**
* Render the global header in a block context.
*
* @return string
*/
function render_global_header() {
remove_inner_group_container();
if ( is_rosetta_site() ) {
$menu_items = get_rosetta_menu_items();
$locale_title = get_rosetta_name();
$show_search = false;
} else {
$menu_items = get_global_menu_items();
$locale_title = '';
$show_search = true;
}
// The mobile Get WordPress button needs to be in both menus.
$menu_items[] = array(
'title' => esc_html_x( 'Get WordPress', 'Menu item title', 'wporg' ),
'url' => get_download_url(),
'type' => 'custom',
'classes' => 'global-header__mobile-get-wordpress global-header__get-wordpress',
);
$menu_items = set_current_item_class( $menu_items );
/*
* Render the block mockup first, in case anything in that process adds hooks to `wp_head`.
* Allow multiple includes to allow for the double `site-header-offset` workaround.
*/
ob_start();
require __DIR__ . '/header.php';
$markup = do_blocks( ob_get_clean() );
restore_inner_group_container();
$is_rest_request = defined( 'REST_REQUEST' ) && REST_REQUEST;
/*
* Render the classic markup second, so the `wp_head()` call will execute callbacks that blocks added above.
*
* API requests also need `<head>` etc so they can get the styles.
*/
if ( ! wp_is_block_theme() || $is_rest_request ) {
ob_start();
require __DIR__ . '/classic-header.php';
$markup = ob_get_clean() . $markup;
}
return $markup;
}
/**
* Determine if the current site is a Rosetta site (e.g., `es-mx.wordpress.org`).
*
* This returns `false` for `translate.wordpress.org`; it's part of the Rosetta network, but isn't a Rosetta site.
*
* @return bool
*/
function is_rosetta_site() {
global $rosetta;
return $rosetta instanceof Rosetta_Sites;
}
/**
* Get the standard items for the global header menu.
*
* These are used on all sites, except Rosetta.
*
* @return array[]
*/
function get_global_menu_items() {
$global_items = array(
array(
'title' => esc_html_x( 'Plugins', 'Menu item title', 'wporg' ),
'url' => 'https://wordpress.org/plugins/',
'type' => 'custom',
),
array(
'title' => esc_html_x( 'Themes', 'Menu item title', 'wporg' ),
'url' => 'https://wordpress.org/themes/',
'type' => 'custom',
),
array(
'title' => esc_html_x( 'Patterns', 'Menu item title', 'wporg' ),
'url' => 'https://wordpress.org/patterns/',
'type' => 'custom',
),
array(
'title' => esc_html_x( 'Learn', 'Menu item title', 'wporg' ),
'url' => 'https://learn.wordpress.org/',
'type' => 'custom',
),
array(
'title' => esc_html_x( 'Support', 'Menu item title', 'wporg' ),
'url' => 'https://wordpress.org/support/',
'type' => 'custom',
'submenu' => array(
array(
'title' => esc_html_x( 'Documentation', 'Menu item title', 'wporg' ),
'url' => 'https://wordpress.org/support/',
'type' => 'custom',
),
array(
'title' => esc_html_x( 'Forums', 'Menu item title', 'wporg' ),
'url' => 'https://wordpress.org/support/forums/',
'type' => 'custom',
),
),
),
array(
'title' => esc_html_x( 'News', 'Menu item title', 'wporg' ),
'url' => 'https://wordpress.org/news/',
'type' => 'custom',
),
array(
'title' => esc_html_x( 'About', 'Menu item title', 'wporg' ),
'url' => 'https://wordpress.org/about/',
'type' => 'custom',
),
array(
'title' => esc_html_x( 'Get Involved', 'Menu item title', 'wporg' ),
'url' => 'https://make.wordpress.org/',
'type' => 'custom',
'submenu' => array(
array(
'title' => esc_html_x( 'Five for the Future', 'Menu item title', 'wporg' ),
'url' => 'https://wordpress.org/five-for-the-future/',
'type' => 'custom',
),
),
),
array(
'title' => esc_html_x( 'Showcase', 'Menu item title', 'wporg' ),
'url' => 'https://wordpress.org/showcase/',
'type' => 'custom',
),
array(
'title' => esc_html_x( 'Mobile', 'Menu item title', 'wporg' ),
'url' => 'https://wordpress.org/mobile/',
'type' => 'custom',
),
array(
'title' => esc_html_x( 'Hosting', 'Menu item title', 'wporg' ),
'url' => 'https://wordpress.org/hosting/',
'type' => 'custom',
),
array(
'title' => esc_html_x( 'Openverse', 'Menu item title', 'wporg' ),
'url' => 'https://wordpress.org/openverse/',
'type' => 'custom',
),
);
return $global_items;
}
/**
* Rosetta sites each have their own menus, rather than using the global menu items.
*
* It's a combination of the items that site admins add to the "Rosetta" menu, and some items that are added
* programmatically to all sites.
*
* @return array[]
*/
function get_rosetta_menu_items() : array {
/** @var Rosetta_Sites $rosetta */
global $rosetta;
switch_to_blog( $rosetta->get_root_site_id() );
// `Rosetta_Sites::wp_nav_menu_objects` sometimes removes redundant items, but sometimes returns early.
$database_items = wp_get_nav_menu_items( get_nav_menu_locations()['rosetta_main'] );
$database_items = array_filter( $database_items, __NAMESPACE__ . '\is_valid_rosetta_menu_item' );
$mock_args = (object) array( 'theme_location' => 'rosetta_main' );
$database_and_hardcoded_items = $rosetta->wp_nav_menu_objects( $database_items, $mock_args );
$normalized_items = normalize_rosetta_items( $database_and_hardcoded_items );
restore_current_blog();
return $normalized_items;
}
/**
* Fetch the Rosetta site name.
*
* @return string
*/
function get_rosetta_name() : string {
/** @var Rosetta_Sites $rosetta */
global $rosetta;
return get_blog_option( $rosetta->get_root_site_id(), 'blogname' );
}
/**
* Determines if a Rosetta menu item is valid.
*
* Some items saved in Rosetta nav menus are redundant, because the global header already includes Download and
* Home links (via the logo).
*
* @param WP_Post $menu_item
*
* @return bool
*/
function is_valid_rosetta_menu_item( $item ) {
/*
* Cover full URLs like `https://ar.wordpress.org/` and `https://ar.wordpress.org/download/`; and relative
* ones like `/` and `/download/`.
*/
$redundant_slugs = array( '/download/', '/txt-download/', '/', "/{$_SERVER['HTTP_HOST']}/" );
// Not using `basename()` because that would match `/foo/download`
$irrelevant_url_parts = array( 'http://', 'https://', $_SERVER['HTTP_HOST'] );
$item_slug = str_replace( $irrelevant_url_parts, '', $item->url );
$item_slug = trailingslashit( $item_slug );
return ! in_array( $item_slug, $redundant_slugs, true );
}
/**
* Normalize the data to be consistent with the format of `get_global_menu_items()`.
*
* @param object[] $rosetta_items Some are `WP_Post`, and some are `stdClass` that are mocking a `WP_Post`.
*
* @return array
*/
function normalize_rosetta_items( $rosetta_items ) {
$normalized_items = array();
$parent_indices = array();
// Standardise the menu classes.
foreach ( $rosetta_items as $index => $item ) {
$rosetta_items[ $index ]->classes = implode( ' ', (array) $item->classes );
}
// Assign the top-level menu items.
foreach ( $rosetta_items as $index => $item ) {
$top_level_item = empty( $item->menu_item_parent );
if ( ! $top_level_item ) {
continue;
}
// Track the indexes of parent items, so the submenu can be built later on.
$parent_indices[ $item->ID ] = $index;
$normalized_items[ $index ] = (array) $item;
}
// Add all submenu items.
foreach ( $rosetta_items as $index => $item ) {
$top_level_item = empty( $item->menu_item_parent );
if ( $top_level_item ) {
continue;
}
// Page has a parent that is not in the menu?
if ( ! isset( $parent_indices[ $item->menu_item_parent ] ) ) {
continue;
}
$parent_index = $parent_indices[ $item->menu_item_parent ];
$normalized_items[ $parent_index ]['submenu'][] = array(
'title' => $item->title,
'url' => $item->url,
'type' => $item->type,
);
}
return $normalized_items;
}
/**
* Retrieve the URL of the home page.
*
* Most of the time it will just be `w.org/`, but Rosetta sites use the URL of the "root site" homepage.
*/
function get_home_url() {
/** @var Rosetta_Sites $rosetta */
global $rosetta;
$url = false;
if ( is_rosetta_site() ) {
$root_site = $rosetta->get_root_site_id();
$url = \get_home_url( $root_site, '/' );
}
if ( ! $url ) {
$url = 'https://wordpress.org/';
}
return $url;
}
/**
* Retrieve the URL to download WordPress.
*
* Rosetta sites sometimes have a localized page, rather than the main English one.
*
* @todo Make DRY with `Rosetta_Sites::wp_nav_menu_objects()` and `WordPressdotorg\MainTheme\get_downloads_url()`.
* There are some differences between these three that need to be reconciled, though.
*/
function get_download_url() {
/** @var Rosetta_Sites $rosetta */
global $rosetta;
$url = false;
if ( is_rosetta_site() ) {
$root_site = $rosetta->get_root_site_id();
switch_to_blog( $root_site );
$download = get_page_by_path( 'download' );
if ( ! $download ) {
$download = get_page_by_path( 'txt-download' );
}
if ( ! $download ) {
$download = get_page_by_path( 'releases' );
}
if ( $download ) {
$url = get_permalink( $download );
}
restore_current_blog();
}
if ( ! $url ) {
$url = 'https://wordpress.org/download/';
}
return $url;
}
/**
* Render the global footer via a REST request.
*
* @return string
*/
function rest_render_global_footer( $request ) {
/*
* Render the header but discard the markup, so that any header styles/scripts
* required are then available for output in the footer.
*/
render_global_header();
// Serve the request as HTML
add_filter( 'rest_pre_serve_request', function( $served, $result ) {
header( 'Content-Type: text/html' );
header( 'X-Robots-Tag: noindex, follow' );
echo $result->get_data();
return true;
}, 10, 2 );
return render_global_footer();
}
/**
* Render the global footer in a block context.
*
* @return string
*/
function render_global_footer() {
remove_inner_group_container();
if ( is_rosetta_site() ) {
$locale_title = get_rosetta_name();
add_filter( 'render_block_data', __NAMESPACE__ . '\localize_nav_links' );
} else {
$locale_title = '';
}
// Render the block mockup first, because `wp_render_layout_support_flag()` adds callbacks to `wp_footer`.
ob_start();
require_once __DIR__ . '/footer.php';
$markup = do_blocks( ob_get_clean() );
restore_inner_group_container();
$is_rest_request = defined( 'REST_REQUEST' ) && REST_REQUEST;
// Render the classic markup second, so the `wp_footer()` call will execute callbacks that blocks added.
if ( ! wp_is_block_theme() || $is_rest_request ) {
ob_start();
require_once __DIR__ . '/classic-footer.php';
$markup .= ob_get_clean();
}
remove_filter( 'render_block_data', __NAMESPACE__ . '\localize_nav_links' );
return $markup;
}
/**
* Localise a `core/navigation-link` block link to point to the Rosetta site resource.
*
* Unfortunately WordPress doesn't have a block-specific pre- filter, only a block-specific post-filter.
* That's why we specifically check for the blockName here.
*
* @param array $block The parsed block data.
*
* @return array
*/
function localize_nav_links( $block ) {
if (
! empty( $block['blockName'] ) &&
'core/navigation-link' === $block['blockName'] &&
! empty( $block['attrs']['url'] )
) {
$block['attrs']['url'] = get_localized_footer_link( $block['attrs']['url'] );
}
return $block;
}
/**
* Get a localized variant of a link included in the global footer.
*
* @param string $url The URL as it is in the menu.
*
* @return string Replacement URL, which may be localised.
*/
function get_localized_footer_link( $url ) {
global $rosetta;
if ( empty( $rosetta->current_site_domain ) ) {
return $url;
}
switch ( $url ) {
case 'https://wordpress.org/showcase/':
case 'https://wordpress.org/hosting/':
return $url;
case 'https://wordpress.org/support/':
// Check if support forum exists.
if ( ! $rosetta->has_support_forum() ) {
return $url;
}
break;
case 'https://learn.wordpress.org/':
return add_query_arg( 'locale', get_locale(), $url );
}
return str_replace( 'https://wordpress.org/', 'https://' . $rosetta->current_site_domain . '/', $url );
}
/**
* Check if the current site is part of the w.org network.
*
* These blocks are used on some sites (like profiles.w.org) that are running WP, but in a different network.
* In those sites, some things need to behave differently (e.g., because `switch_to_blog()` wouldn't work).
*/
function is_wporg_network() {
if ( 'local' === wp_get_environment_type() ) {
return false;
}
return defined( 'WPORGPATH' ) && 0 === strpos( $_SERVER['SCRIPT_FILENAME'], WPORGPATH );
}
/**
* Render the global styles via a REST request.
*
* @return string
*/
function rest_render_global_styles( $request ) {
// Serve the request as CSS.
add_filter( 'rest_pre_serve_request', function( $served, $result ) {
header( 'Content-Type: text/css' );
header( 'X-Robots-Tag: noindex, follow' );
echo $result->get_data();
return true;
}, 10, 2 );
return get_global_styles();
}
/**
* Output just the variables generated by `theme.json` from the News site.
*
* This will let other themes on the network use the variables, without also
* loading in the block & element styling.
*
* @see `wp_get_global_stylesheet()`
*/
function get_global_styles() {
/*
* The block is used on some sites (like profiles.w.org) that are running WP, but in a different
* network. On those sites, things like `switch_to_blog()` won't work, so they need to use the API.
*/
if ( ! is_wporg_network() ) {
return fetch_global_styles();
}
// Switch to `w.org/news` to generate correct theme properties.
switch_to_blog( WPORG_NEWS_BLOGID );
// Clear the static `$theme` property, which is set by the current (classic theme) site.
WP_Theme_JSON_Resolver::clean_cached_data();
$styles = wp_get_global_stylesheet( [ 'variables', 'presets' ] );
// Also set the block-gap style, which isn't technically a theme variable.
$styles .= 'body { --wp--style--block-gap: 24px; }';
// Restore to current site.
restore_current_blog();
WP_Theme_JSON_Resolver::clean_cached_data();
return $styles;
}
/**
* Fetch the global styles via the API endpoint
*
* @see `get_global_styles()` for background.
*/
function fetch_global_styles() {
$cache_key = 'global_header_styles';
$styles = get_transient( $cache_key );
if ( ! $styles ) {
$styles = '';
$request_args = array();
// Route request to sandbox when testing.
if ( 'staging' === wp_get_environment_type() ) {
$hostname = '127.0.0.1';
$request_args['headers']['host'] = 'wordpress.org';
/*
* It's expected that the sandbox hostname won't be valid. This is safe because we're only connecting
* to `127.0.0.1`.
*/
$request_args['sslverify'] = false;
} else {
$hostname = 'wordpress.org';
}
$response = wp_remote_get( "https://$hostname/wp-json/global-header-footer/v1/styles", $request_args );
$response_code = wp_remote_retrieve_response_code( $response );
if ( is_wp_error( $response ) || 200 !== $response_code ) {
trigger_error( "Fetching global styles failed.", E_USER_WARNING );
} else {
$styles = wp_remote_retrieve_body( $response );
set_transient( $cache_key, $styles, HOUR_IN_SECONDS );
}
}
return $styles;
}
/**
* Set the menu active state for the currently selected menu item.
*
* @param array $menu_items The menu menu items.
*
* @return array The altered menu items.
*/
function set_current_item_class( $menu_items ) {
$current_url = get_menu_url_for_current_page( $menu_items );
foreach ( $menu_items as & $item ) {
$sub = false;
if ( ! empty( $item['submenu'] ) ) {
foreach ( $item['submenu'] as & $subitem ) {
if ( $current_url === $subitem['url'] ) {
$subitem['classes'] = trim( ( $subitem['classes'] ?? '' ) . ' current-menu-item' );
$sub = true;
break;
}
}
}
if ( $sub || $current_url === $item['url'] ) {
$item['classes'] = trim( ( $item['classes'] ?? '' ) . ' current-menu-item' );
}