Created
March 15, 2017 06:36
-
-
Save robertstaddon/d0a5db2f138261cd56d475931c5a8689 to your computer and use it in GitHub Desktop.
Hide category and forum menu items that are restricted by Itthinx Groups
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Menu items of categories and forums should be hidden if the user doesn't belong to the Group | |
*/ | |
if ( !is_admin() ) { | |
add_filter( 'wp_get_nav_menu_items', 'lenspiration_hide_groups_restricted_menu_items', 999, 3 ); | |
} | |
function lenspiration_hide_groups_restricted_menu_items( $items = null, $menu = null, $args = null ) { | |
$user_id = get_current_user_id(); | |
foreach ( $items as $key => $item ) { | |
error_log('Item: ' . var_export($item, true)); | |
if ( $item->type == "taxonomy" && class_exists( 'Groups_Restrict_Categories') && method_exists( 'Groups_Restrict_Categories', 'user_can_read_term' ) ) { | |
if ( !Groups_Restrict_Categories::user_can_read_term( $user_id, $item->object_id ) ) { | |
unset( $items[$key] ); | |
} | |
} elseif ( $item->type == "forum" && class_exists( 'Groups_Post_Access' ) && method_exists( 'Groups_Post_Access', 'user_can_read_post' ) ) { | |
if ( Groups_Post_Acceses::user_can_read_post( $item->object_id, $user_id ) ) { | |
unset( $items[$key] ); | |
} | |
} | |
} | |
// Remove any orphaned menu items whose parents have been removed | |
foreach ( $items as $key => $item ) { | |
$menu_item_parent = $item->menu_item_parent; | |
if ( $menu_item_parent != 0 ) { | |
$match = false; | |
foreach ( $items as $search_key => $search_item) { | |
if( $search_item->ID == $menu_item_parent ) $match = true; | |
} | |
if( !$match ) { | |
unset( $items[$key] ); | |
} | |
} | |
} | |
return $items; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment