|
1 | 1 | <?php
|
2 | 2 |
|
3 | 3 | namespace WordPressdotorg\MU_Plugins\Global_Header_Footer;
|
| 4 | +use Rosetta_Sites; |
4 | 5 |
|
5 | 6 | defined( 'WPINC' ) || die();
|
6 | 7 |
|
@@ -141,7 +142,11 @@ function restore_inner_group_container() {
|
141 | 142 | function render_global_header() {
|
142 | 143 | remove_inner_group_container();
|
143 | 144 |
|
144 |
| - $menu_items = get_global_menu_items(); |
| 145 | + if ( is_rosetta_site() ) { |
| 146 | + $menu_items = get_rosetta_menu_items(); |
| 147 | + } else { |
| 148 | + $menu_items = get_global_menu_items(); |
| 149 | + } |
145 | 150 |
|
146 | 151 | // The mobile Get WordPress button needs to be in both menus.
|
147 | 152 | $menu_items[] = array(
|
@@ -171,6 +176,19 @@ function render_global_header() {
|
171 | 176 | return $markup;
|
172 | 177 | }
|
173 | 178 |
|
| 179 | +/** |
| 180 | + * Determine if the current site is a Rosetta site (e.g., `es-mx.wordpress.org`). |
| 181 | + * |
| 182 | + * This returns `false` for `translate.wordpress.org`; it's part of the Rosetta network, but isn't a Rosetta site. |
| 183 | + * |
| 184 | + * @return bool |
| 185 | + */ |
| 186 | +function is_rosetta_site() { |
| 187 | + global $rosetta; |
| 188 | + |
| 189 | + return $rosetta instanceof Rosetta_Sites; |
| 190 | +} |
| 191 | + |
174 | 192 | /**
|
175 | 193 | * Get the standard items for the global header menu.
|
176 | 194 | *
|
@@ -278,13 +296,102 @@ function get_global_menu_items() {
|
278 | 296 | return $global_items;
|
279 | 297 | }
|
280 | 298 |
|
| 299 | +/** |
| 300 | + * Rosetta sites each have their own menus, rather than using the global menu items. |
| 301 | + * |
| 302 | + * It's a combination of the items that site admins add to the "Rosetta" menu, and some items that are added |
| 303 | + * programmatically to all sites. |
| 304 | + * |
| 305 | + * @return array[] |
| 306 | + */ |
| 307 | +function get_rosetta_menu_items() : array { |
| 308 | + /** @var Rosetta_Sites $rosetta */ |
| 309 | + global $rosetta; |
| 310 | + |
| 311 | + switch_to_blog( $rosetta->get_root_site_id() ); |
| 312 | + |
| 313 | + $database_items = wp_get_nav_menu_items( get_nav_menu_locations()['rosetta_main'] ); |
| 314 | + $mock_args = (object) array( 'theme_location' => 'rosetta_main' ); |
| 315 | + $database_and_hardcoded_items = $rosetta->wp_nav_menu_objects( $database_items, $mock_args ); |
| 316 | + $normalized_items = normalize_rosetta_items( $database_and_hardcoded_items ); |
| 317 | + |
| 318 | + restore_current_blog(); |
| 319 | + |
| 320 | + return $normalized_items; |
| 321 | +} |
| 322 | + |
| 323 | +/** |
| 324 | + * Normalize the data to be consistent with the format of `get_global_menu_items()`. |
| 325 | + * |
| 326 | + * @param object[] $rosetta_items Some are `WP_Post`, and some are `stdClass` that are mocking a `WP_Post`. |
| 327 | + * |
| 328 | + * @return array |
| 329 | + */ |
| 330 | +function normalize_rosetta_items( $rosetta_items ) { |
| 331 | + $normalized_items = array(); |
| 332 | + $parent_indices = array(); |
| 333 | + |
| 334 | + foreach ( $rosetta_items as $index => $item ) { |
| 335 | + $top_level_item = empty( $item->menu_item_parent ); |
| 336 | + $item->classes = implode( ' ', $item->classes ); |
| 337 | + |
| 338 | + if ( $top_level_item ) { |
| 339 | + // Track the indexes of parent items, so the submenu can be built later on. |
| 340 | + $parent_indices[ $item->ID ] = $index; |
| 341 | + $normalized_items[ $index ] = (array) $item; |
| 342 | + |
| 343 | + } else { |
| 344 | + $parent_index = $parent_indices[ $item->menu_item_parent ]; |
| 345 | + |
| 346 | + $normalized_items[ $parent_index ]['submenu'][] = array( |
| 347 | + 'title' => $item->title, |
| 348 | + 'url' => $item->url, |
| 349 | + 'type' => $item->type, |
| 350 | + ); |
| 351 | + } |
| 352 | + } |
| 353 | + |
| 354 | + return $normalized_items; |
| 355 | +} |
| 356 | + |
281 | 357 | /**
|
282 | 358 | * Retrieve the URL to download WordPress.
|
283 | 359 | *
|
284 | 360 | * Rosetta sites sometimes have a localized page, rather than the main English one.
|
| 361 | + * |
| 362 | + * @todo Make DRY with `Rosetta_Sites::wp_nav_menu_objects()` and `WordPressdotorg\MainTheme\get_downloads_url()`. |
| 363 | + * There are some differences between these three that need to be reconciled, though. |
285 | 364 | */
|
286 | 365 | function get_download_url() {
|
287 |
| - $url = 'https://wordpress.org/downloads/'; |
| 366 | + /** @var Rosetta_Sites $rosetta */ |
| 367 | + global $rosetta; |
| 368 | + |
| 369 | + $url = false; |
| 370 | + |
| 371 | + if ( is_rosetta_site() ) { |
| 372 | + $root_site = $rosetta->get_root_site_id(); |
| 373 | + |
| 374 | + switch_to_blog( $root_site ); |
| 375 | + |
| 376 | + $download = get_page_by_path( 'download' ); |
| 377 | + |
| 378 | + if ( ! $download ) { |
| 379 | + $download = get_page_by_path( 'txt-download' ); |
| 380 | + } |
| 381 | + if ( ! $download ) { |
| 382 | + $download = get_page_by_path( 'releases' ); |
| 383 | + } |
| 384 | + |
| 385 | + if ( $download ) { |
| 386 | + $url = get_permalink( $download ); |
| 387 | + } |
| 388 | + |
| 389 | + restore_current_blog(); |
| 390 | + } |
| 391 | + |
| 392 | + if ( ! $url ) { |
| 393 | + $url = 'https://wordpress.org/downloads/'; |
| 394 | + } |
288 | 395 |
|
289 | 396 | return $url;
|
290 | 397 | }
|
|
0 commit comments