current_user_role == "" ) { $this->current_user_role = $current_user->roles[0]; } return $this->current_user_role; } else { $this->current_user_role='visitor'; return $this->current_user_role; } } /* * Check if the user is an administrator */ private function isAdmin() { $current_role = $this->getCurrentUserRole(); if ( $current_role == "administrator" ) { return true; } else { return false; } } /* * Get all restrictions */ private function getAllRestrictions() { global $menu_restricted_access_array; $all_restrictions_array = array(); foreach ( $menu_restricted_access_array as $one_restriction ) { $all_restrictions_array = array_merge($all_restrictions_array, $one_restriction); } $all_restrictions_array = array_unique($all_restrictions_array); return $all_restrictions_array; } /* * Check the access */ private function isAccessGranted( $id_menu_item ) { global $menu_restricted_access_array; if ( $this->isAdmin() ) { return true; } else if ( isset($menu_restricted_access_array[$this->current_user_role]) ) { $restricted_access = $menu_restricted_access_array[$this->current_user_role]; if ( in_array($id_menu_item, $restricted_access) ) { return true; } else { return false; } } else { return true; } } /* * Element render */ function start_el(&$output, $item, $depth, $args) { global $wp_query, $menu_restricted_access_array; global $g_role,$g_pageid; $indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; $g_role=strtolower((trim($item->description))); $str = explode(',',$g_role); for( $i=0; $i< count($str); $i++) { if (strtolower(trim($str[$i]))==$this->current_user_role) { $restriction =$item->object_id; $menu_restricted_access_array[$this->current_user_role] =array( $restriction); } } $class_names = $value = ''; $classes = empty( $item->classes ) ? array() : (array) $item->classes; $classes[] = 'menu-item-' . $item->ID; /* * First test, add custom class to each menu item */ $classes[] = 'my-custom-menu-class'; /* * Detect the menu item matching the unpublished page * Detect the menu item matching the unpublished page */ // -> FLag to display the output $item_to_display = true; $is_item_published = true; // -> Gather data of linked object $item_data = get_post($item->object_id); // --> If it's a page, act on the flag if ( !empty($item_data) && ($item->object == "page") ) { $is_item_published = ( $item_data->post_status == "publish" ) ? true : false; $item_output = ""; } /* * Detect and display by user Role **/ if ( _USE_RESTRICTED_ACCESS ) { $restrictions_array = $this->getAllRestrictions(); $this->isAccessGranted($item->object_id); } else { $item_to_display = $is_item_published; } $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ); $class_names = ' class="' . esc_attr( $class_names ) . '"'; $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args ); $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : ''; $output .= $indent . '