Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /***
- * Menu WALKER - for restricting the menu items visibility
- * Code modified by - Trupti Bhatt (http://3sided.co.in)
- * 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
- ***/
- class description_walker extends Walker_Nav_Menu
- {
- /*
- * Custom var to store current role
- */
- private $current_user_role = "";
- /*
- * Get te current user role
- */
- private function getCurrentUserRole()
- {
- global $current_user;
- if ( is_user_logged_in() )
- {
- if ( $this->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 . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
- $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
- $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
- $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
- $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
- if($depth != 0)
- {
- $description = $append = $prepend = "";
- }
- // --> If the flag is true, when display the output
- if ( $item_to_display )
- {
- $item_output = $args->before;
- $item_output .= '<a'. $attributes .'>';
- $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
- $item_output .= '</a>';
- $item_output .= $args->after;
- }
- $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
- }
- }
- /*
- * Restrictions configuration
- * 2 is page id of Homepage
- **/
- define("_USE_RESTRICTED_ACCESS", true);
- $menu_restricted_access_array['subscriber'] = array('2');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement