Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breadcrumbs also for news on a static page #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 64 additions & 14 deletions includes/class-menu-breadcrumb.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,20 @@ class Menu_Breadcrumb {
*
* @since 1.0.0
* @access private
* @deprecated Use $sorted_menu_items instead
* @var array $menu_items The current version of the plugin.
*/
private $menu_items = array();

/**
* The Menu items
*
* @since 1.0.0
* @access protected
* @var array $sorted_menu_items The menu items in array.
*/
protected $sorted_menu_items = array();

/**
* Define the core functionality of the plugin.
*
Expand All @@ -106,14 +116,32 @@ public function __construct( $menu_location = '' ) {
// make sure the location exists
if ( isset( $menu_locations[ $this->menu_location ] ) ) {
$this->menu = wp_get_nav_menu_object( $menu_locations[ $this->menu_location ] );
$this->menu_items = wp_get_nav_menu_items( $this->menu->term_id );

add_filter( 'wp_nav_menu_objects', array( $this, 'set_sorted_menu_items' ) );
wp_nav_menu( array(
'theme_location' => $this->menu_location,
'echo' => false,
'walker' => new Walker_Nav_Menu(), /* make sure the default walker is used! */
) );
remove_filter( 'wp_nav_menu_objects', array( $this, 'set_sorted_menu_items' ) );
}

$this->load_dependencies();
$this->set_locale();
$this->define_public_hooks();

}

/**
* This function will be called by the wp_nav_menu_objects filter,
* after wordpress has added the current page information
* and after execution of the default walker.
*
* @param array $items
*/
public function set_sorted_menu_items($items) {
$this->sorted_menu_items = $items;
}

/**
* Load the required dependencies for this plugin.
Expand Down Expand Up @@ -265,33 +293,41 @@ public function is_at_url( $url = '' ) {
* Retrieve the current Menu item object for the current Menu.
*
* @since 1.0.0
* @return bool|WP_Post The current Menu item
* @return bool|WP_Post The current Menu item (false if not found)
*/
public function get_current_menu_item_object() {

$current_menu_item = false;

if ( empty( $this->menu_items ) ) {
if ( empty( $this->sorted_menu_items ) ) {
return $current_menu_item;
}

// loop through the entire nav menu and determine whether any have a class="current" or are the current URL (e.g. a Custom Link was used)
foreach ( $this->menu_items as $menu_item ) {
$parent = array();

// loop through the entire nav menu and determine whether any have a class="current" or are the current URL (e.g. a Custom Link was used)
foreach ( $this->sorted_menu_items as $menu_item) {
// if WordPress was able to detect the current page
if ( is_array( $menu_item->classes ) && in_array( 'current', $menu_item->classes ) ) {
if ( $menu_item->current ) {
$current_menu_item = $menu_item;
break;
}

// Maybe we don't find the page in the menu, but a parent
if ( is_array($menu_item->classes) && in_array('current_page_parent', $menu_item->classes) ) {
$parent = $menu_item;
}

// if the current URL matches a Custom Link
if ( ! $current_menu_item && isset( $menu_item->url ) && $this->is_at_url( $menu_item->url ) ) {
if ( isset( $menu_item->url ) && $this->is_at_url( $menu_item->url ) ) {
$current_menu_item = $menu_item;
}

if ( $current_menu_item ) {
break;
}
}

if (!$current_menu_item && $parent) {
return $parent;
}

return $current_menu_item;
}
Expand All @@ -301,17 +337,17 @@ public function get_current_menu_item_object() {
*
* @since 1.0.0
* @param WP_Post $current_menu_item The current Menu item object
* @return bool|WP_post The parent Menu object
* @return bool|WP_Post The parent Menu object
*/
public function get_parent_menu_item_object( $current_menu_item ) {

$parent_menu_item = false;

if ( empty( $this->menu_items ) ) {
if ( empty( $this->sorted_menu_items ) ) {
return $current_menu_item;
}

foreach ( $this->menu_items as $menu_item ) {
foreach ( $this->sorted_menu_items as $menu_item ) {
if ( absint( $current_menu_item->menu_item_parent ) == absint( $menu_item->ID ) ) {
$parent_menu_item = $menu_item;
break;
Expand Down Expand Up @@ -430,10 +466,24 @@ public function get_menu() {
* Getter for Menu items
*
* @since 1.0.3
* @deprecated Use get_sorted_menu_items() instead
* @return array|mixed Menu item objects
*/
public function get_menu_items() {
if ($this->menu_items === array())
$this->menu_items = wp_get_nav_menu_items( $this->menu->term_id );
return $this->menu_items;
}

/**
* Getter for Menu items
*
* @since 1.0.3
* @return array|mixed Menu item objects
*/
public function get_sorted_menu_items() {
return $this->sorted_menu_items;
}

}

}