WordPress.org

Making WordPress.org

Changeset 4788


Ignore:
Timestamp:
01/25/2017 10:56:00 PM (5 years ago)
Author:
coffee2code
Message:

Handbook plugin: Highlights a menu link to the handbook home page when on any constituent handbook page.

Assuming the handbook page isn't already directly linked in the menu, preference is given to highlight a link to the front page of the current handbook. Barring the presence of such a link, it will check to see if there is a link to a 'handbook' or 'handbooks' page, which could be the case for multi-handbook sites.

Fixes #2283.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/handbook.php

    r4753 r4788  
    136136        add_filter( 'o2_post_fragment',                   array( $this, 'o2_post_fragment' ) );
    137137        add_filter( 'comments_open',                      array( $this, 'comments_open' ), 10, 2 );
     138        add_filter( 'wp_nav_menu_objects',                array( $this, 'highlight_menu_handbook_link' ) );
    138139    }
    139140
     
    457458        return $open;
    458459    }
     460
     461    /**
     462     * Highlights a menu link to the handbook home page when on any constituent
     463     * handbook page.
     464     *
     465     * Assuming the handbook page isn't already directly linked in the menu,
     466     * preference is given to highlight a link to the front page of the current
     467     * handbook. Barring the presence of such a link, it will check to see if
     468     * there is a link to a 'handbook' or 'handbooks' page, which could be the
     469     * case for multi-handbook sites.
     470     *
     471     * @param array $menu_items Array of sorted menu items.
     472     * @return array
     473     */
     474    function highlight_menu_handbook_link( $menu_items ) {
     475        // Must be on a handbook page that isn't the handbook landing page (which will already be handled).
     476        if ( ! is_page( array( 'handbook', 'handbooks' ) ) && ( ! wporg_is_handbook() || wporg_is_handbook_landing_page() ) ) {
     477            return $menu_items;
     478        }
     479
     480        // Menu must not have an item that is already noted as being current.
     481        $current_menu_item = wp_filter_object_list( $menu_items, array( 'current' => true ) );
     482        if ( $current_menu_item ) {
     483            return $menu_items;
     484        }
     485
     486        // Menu must have an item that links to handbook home page.
     487        $root_handbook_menu_item = wp_filter_object_list( $menu_items, array( 'url' => wporg_get_current_handbook_home_url() ) );
     488        if ( ! $root_handbook_menu_item ) {
     489            // Or it must have an item that links to a 'handbook' or 'handbooks' page.
     490            $page_slug = is_page( 'handbooks' ) ? 'handbooks' : 'handbook';
     491            $page = get_page_by_path( $page_slug );
     492            if ( $page ) {
     493                $url = get_page_link( $page );
     494                $root_handbook_menu_item = wp_filter_object_list( $menu_items, array( 'url' => $url ) );
     495            }
     496        }
     497        if ( ! $root_handbook_menu_item ) {
     498            return $menu_items;
     499        }
     500
     501        // Add current-menu-item class to the handbook menu item.
     502        reset( $root_handbook_menu_item );
     503        $handbook_item_index = key( $root_handbook_menu_item );
     504        $menu_items[ $handbook_item_index ]->classes[] = 'current-menu-item';
     505
     506        return $menu_items;
     507    }
     508
    459509}
Note: See TracChangeset for help on using the changeset viewer.