Making WordPress.org

Changeset 14077


Ignore:
Timestamp:
09/25/2024 04:43:24 AM (6 months ago)
Author:
adamwood
Message:

Make 2024: Add login link to blog local nav

See https://github.com/WordPress/wporg-mu-plugins/issues/647

File:
1 edited

Legend:

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

    r14040 r14077  
    192192
    193193/**
     194 * Add a login link to the local nav if there is no logged in user.
     195 */
     196function _maybe_add_login_item_to_menu( $menus ) {
     197    if ( is_user_logged_in() ) {
     198        return $menus;
     199    }
     200
     201    $login_item = array(
     202        'label' => __( 'Log in', 'wporg-learn' ),
     203        'url' => wp_login_url( $redirect_url ),
     204    );
     205
     206    if ( $menus['breathe'] ) {
     207        $login_item['className'] = 'has-separator';
     208        $menus['breathe'][] = $login_item;
     209    } else {
     210        $menus['breathe'] = array( $login_item );
     211    }
     212
     213    return $menus;
     214}
     215
     216/**
    194217 * Provide a list of local navigation menus.
    195218 */
     
    198221        return;
    199222    }
     223
    200224    $local_nav_menu_object = wporg_breathe_get_local_nav_menu_object();
    201225
    202226    if ( ! $local_nav_menu_object ) {
    203         return array();
     227        return _maybe_add_login_item_to_menu( $menus );
    204228    }
    205229
     
    207231
    208232    if ( ! $menu_items || empty( $menu_items ) ) {
    209         return array();
    210     }
    211 
    212     return array(
    213         'breathe' => array_map(
    214             function( $menu_item ) {
    215                 return array(
    216                     'label' => esc_html( $menu_item->title ),
    217                     'url' => esc_url( $menu_item->url )
    218                 );
    219             },
    220             // Limit local nav items to 6
    221             array_slice( $menu_items, 0, 6 )
    222         )
     233        return _maybe_add_login_item_to_menu( $menus );
     234    }
     235
     236    $menu = array_map(
     237        function( $menu_item ) {
     238            return array(
     239                'label' => esc_html( $menu_item->title ),
     240                'url' => esc_url( $menu_item->url )
     241            );
     242        },
     243        // Limit local nav items to 6
     244        array_slice( $menu_items, 0, 6 )
    223245    );
     246
     247    $menus['breathe'] = $menu;
     248
     249    return _maybe_add_login_item_to_menu( $menus );
    224250}
    225251add_filter( 'wporg_block_navigation_menus', __NAMESPACE__ . '\wporg_breathe_add_site_navigation_menus' );
Note: See TracChangeset for help on using the changeset viewer.