Making WordPress.org

Changeset 13552


Ignore:
Timestamp:
04/17/2024 10:00:08 PM (12 months ago)
Author:
adamwood
Message:

wporg-support-2024: Allow rosetta local nav config

Adds a local-navigation menu location which can be configured to override the english local nav items

Props @ryelle

Fixes https://github.com/WordPress/wordpress.org/issues/236

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-support-2024/functions.php

    r13544 r13552  
    1818
    1919/**
     20 * Get the local navigation menu object if it exists.
     21 */
     22function get_local_nav_menu_object() {
     23    $local_nav_menu_locations = get_nav_menu_locations();
     24    $local_nav_menu_object = isset( $local_nav_menu_locations['local-navigation'] )
     25        ? wp_get_nav_menu_object( $local_nav_menu_locations['local-navigation'] )
     26        : false;
     27
     28    return $local_nav_menu_object;
     29}
     30
     31/**
     32 * Register a local nav menu for non-English forums, if it doesn't already exist.
     33 */
     34function register_local_nav_menu() {
     35    if ( substr( get_locale(), 0, 2 ) === 'en' || get_local_nav_menu_object() ) {
     36        return;
     37    }
     38
     39    register_nav_menu( 'local-navigation', __( 'Local Navigation', 'wporg-forums' ) );
     40}
     41add_action( 'after_setup_theme', 'register_local_nav_menu' );
     42
     43/**
    2044 * Provide a list of local navigation menus.
    2145 */
    2246function add_site_navigation_menus( $menus ) {
    23     return array(
    24         'forums' => array(
    25             array(
    26                 'label' => __( 'Welcome to Support', 'wporg-forums' ),
    27                 'url' => '/welcome/',
     47    if ( is_admin() ) {
     48        return;
     49    }
     50
     51    if ( substr( get_locale(), 0, 2 ) === 'en' ) {
     52        return array(
     53            'forums' => array(
     54                array(
     55                    'label' => __( 'Welcome to Support', 'wporg-forums' ),
     56                    'url' => '/welcome/',
     57                ),
     58                array(
     59                    'label' => __( 'Guidelines', 'wporg-forums' ),
     60                    'url' => '/guidelines/',
     61                ),
     62                array(
     63                    'label' => __( 'Get Involved', 'wporg-forums' ),
     64                    'url' => 'https://make.wordpress.org/support/handbook/contributing-to-the-wordpress-forums/',
     65                )
    2866            ),
    29             array(
    30                 'label' => __( 'Guidelines', 'wporg-forums' ),
    31                 'url' => '/guidelines/',
     67        );
     68    } else {
     69        $local_nav_menu_object = get_local_nav_menu_object();
     70        $menu_items_fallback = array(
     71            'forums' => array(
     72                 array(
     73                    'label' => __( 'Get Involved', 'wporg-forums' ),
     74                    'url' => 'https://make.wordpress.org/support/handbook/contributing-to-the-wordpress-forums/',
     75                )
    3276            ),
    33             array(
    34                 'label' => __( 'Get Involved', 'wporg-forums' ),
    35                 'url' => 'https://make.wordpress.org/support/handbook/contributing-to-the-wordpress-forums/',
     77        );
     78
     79        if ( ! $local_nav_menu_object ) {
     80            return $menu_items_fallback;
     81        }
     82
     83        $menu_items = wp_get_nav_menu_items( $local_nav_menu_object->term_id );
     84
     85        if ( ! $menu_items || empty( $menu_items ) ) {
     86            return $menu_items_fallback;
     87        }
     88
     89        return array(
     90            'forums' => array_map(
     91                function( $menu_item ) {
     92                    return array(
     93                        'label' => esc_html( $menu_item->title ),
     94                        'url' => esc_url( $menu_item->url )
     95                    );
     96                },
     97                // Limit local nav items to 3
     98                array_slice( $menu_items, 0, 3 )
    3699            )
    37         ),
    38     );
     100        );
     101    }
    39102}
    40103add_filter( 'wporg_block_navigation_menus', '\add_site_navigation_menus' );
Note: See TracChangeset for help on using the changeset viewer.