1. <?php
  2.  
  3. /***
  4. * Menu WALKER - for restricting the menu items visibility
  5. * Code modified by - Trupti Bhatt (http://3sided.co.in)
  6. * using original code posted here - http://www.tisseur-de-toile.fr/wordpress-tricks/pimp-my-wordpress-menu-part-2-access-granted-to-authorized-personnel-only.html
  7. ***/
  8. class description_walker extends Walker_Nav_Menu
  9. {
  10. /*
  11. * Custom var to store current role
  12. */
  13. private $current_user_role = "";
  14.  
  15. /*
  16. * Get te current user role
  17. */
  18. private function getCurrentUserRole()
  19. {
  20. global $current_user;
  21. if ( is_user_logged_in() )
  22. {
  23. if ( $this->current_user_role == "" )
  24. {
  25. $this->current_user_role = $current_user->roles[0];
  26. }
  27.  
  28. return $this->current_user_role;
  29. }
  30. else
  31. {
  32. $this->current_user_role='visitor';
  33. return $this->current_user_role;
  34. }
  35. }
  36.  
  37. /*
  38. * Check if the user is an administrator
  39. */
  40. private function isAdmin()
  41. {
  42. $current_role = $this->getCurrentUserRole();
  43.  
  44. if ( $current_role == "administrator" )
  45. {
  46. return true;
  47. }
  48. else
  49. {
  50. return false;
  51. }
  52. }
  53.  
  54. /*
  55. * Get all restrictions
  56. */
  57. private function getAllRestrictions()
  58. {
  59. global $menu_restricted_access_array;
  60.  
  61.  
  62. $all_restrictions_array = array();
  63.  
  64. foreach ( $menu_restricted_access_array as $one_restriction )
  65. {
  66. $all_restrictions_array = array_merge($all_restrictions_array, $one_restriction);
  67. }
  68. $all_restrictions_array = array_unique($all_restrictions_array);
  69.  
  70. return $all_restrictions_array;
  71. }
  72.  
  73. /*
  74. * Check the access
  75. */
  76. private function isAccessGranted( $id_menu_item )
  77. {
  78. global $menu_restricted_access_array;
  79.  
  80. if ( $this->isAdmin() )
  81. {
  82. return true;
  83. }
  84. else if ( isset($menu_restricted_access_array[$this->current_user_role]) )
  85. {
  86. $restricted_access = $menu_restricted_access_array[$this->current_user_role];
  87.  
  88. if ( in_array($id_menu_item, $restricted_access) )
  89. {
  90. return true;
  91. }
  92. else
  93. {
  94. return false;
  95. }
  96. }
  97. else {
  98. return true;
  99. }
  100.  
  101. }
  102.  
  103. /*
  104. * Element render
  105. */
  106. function start_el(&$output, $item, $depth, $args)
  107. {
  108.  
  109. global $wp_query, $menu_restricted_access_array;
  110. global $g_role,$g_pageid;
  111. $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
  112. $g_role=strtolower((trim($item->description)));
  113.  
  114. $str = explode(',',$g_role);
  115. for( $i=0; $i< count($str); $i++)
  116. {
  117.  
  118. if (strtolower(trim($str[$i]))==$this->current_user_role)
  119. {
  120. $restriction =$item->object_id;
  121. $menu_restricted_access_array[$this->current_user_role] =array( $restriction);
  122. }
  123.  
  124.  
  125. }
  126.  
  127.  
  128. $class_names = $value = '';
  129.  
  130. $classes = empty( $item->classes ) ? array() : (array) $item->classes;
  131. $classes[] = 'menu-item-' . $item->ID;
  132.  
  133.  
  134. /*
  135. * First test, add custom class to each menu item
  136. */
  137. $classes[] = 'my-custom-menu-class';
  138.  
  139. /*
  140. * Detect the menu item matching the unpublished page
  141. * Detect the menu item matching the unpublished page
  142. */
  143. // -> FLag to display the output
  144. $item_to_display = true;
  145. $is_item_published = true;
  146.  
  147. // -> Gather data of linked object
  148. $item_data = get_post($item->object_id);
  149.  
  150. // --> If it's a page, act on the flag
  151.  
  152. if ( !empty($item_data) && ($item->object == "page") )
  153. {
  154. $is_item_published = ( $item_data->post_status == "publish" ) ? true : false;
  155. $item_output = "";
  156. }
  157.  
  158. /*
  159. * Detect and display by user Role
  160. **/
  161. if ( _USE_RESTRICTED_ACCESS )
  162. {
  163. $restrictions_array = $this->getAllRestrictions();
  164. $this->isAccessGranted($item->object_id);
  165.  
  166. }
  167. else
  168. {
  169. $item_to_display = $is_item_published;
  170. }
  171.  
  172. $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
  173. $class_names = ' class="' . esc_attr( $class_names ) . '"';
  174.  
  175.  
  176.  
  177. $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
  178. $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : '';
  179.  
  180. $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
  181.  
  182. $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
  183. $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
  184. $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
  185. $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
  186.  
  187.  
  188.  
  189. if($depth != 0)
  190. {
  191. $description = $append = $prepend = "";
  192. }
  193.  
  194. // --> If the flag is true, when display the output
  195.  
  196. if ( $item_to_display )
  197. {
  198. $item_output = $args->before;
  199. $item_output .= '<a'. $attributes .'>';
  200. $item_output .= $args->link_before .apply_filters( 'the_title', $item->title, $item->ID ).$append; // this is where the strong tags are prepend and append to the description
  201.  
  202. $item_output .= '</a>';
  203. $item_output .= $args->after;
  204. }
  205.  
  206. $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
  207. }
  208. }
  209. /*
  210. * Restrictions configuration
  211. * 2 is page id of Homepage
  212. **/
  213. define("_USE_RESTRICTED_ACCESS", true);
  214. $menu_restricted_access_array['subscriber'] = array('2');
  215.  
  216. ?>