Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Greg Ross

    (@gregross)

    I hacked around a bit with the code anyway, here’s a patch that enables role based access:

    Index: customWalker.php
    ===================================================================
    --- customWalker.php	(revision 1103563)
    +++ customWalker.php	(working copy)
    @@ -209,11 +209,20 @@
                     $display_roles = apply_filters( 'priv_menu_roles', $wp_roles->role_names );
    
                     /* Get the roles saved for the post. */
    -                $roles = get_post_meta( $item->ID, '_priv_menu_role', true );
    -
    +                $users_and_roles = get_post_meta( $item->ID, '_priv_menu_role', true );
    +
    +				if( !is_array( $users_and_roles ) ) {
    +					$roles = $users_and_roles;
    +					$users = $users_and_roles;
    +				}
    +				else {
    +					$roles = $users_and_roles['roles'];
    +					$users = $users_and_roles['users'];
    +				}
    +
                     $checked_roles = is_array( $roles ) ? $roles : false;
    
    -                $logged_in_out = ! is_array( $roles ) ? $roles : false;
    +                $logged_in_out = $users;
    
                     ?>
    
    @@ -226,14 +235,7 @@
                         <input type="hidden" class="nav-menu-id" value="<?php echo $item->ID ;?>" />
    
                         <div class="logged-input-holder" style="float: left; width: 35%;">
    -                        <input type="radio" class="nav-menu-logged-in-out" name="priv-menu-logged-in-out[<?php echo $item->ID ;?>]" id="priv_menu_logged_out-for-<?php echo $item->ID ;?>" <?php checked( 'admin', $logged_in_out ); ?> value="admin" />
    -                        <label for="priv_menu_admin_user-for-<?php echo $item->ID ;?>">
    -                            <?php _e( 'Admin Users', 'privilege-menu'); ?>
    -                        </label>
    -                    </div>
    -
    -                    <div class="logged-input-holder" style="float: left; width: 35%;">
    -                        <input type="radio" class="nav-menu-logged-in-out" name="priv-menu-logged-in-out[<?php echo $item->ID ;?>]" id="priv_menu_logged_out-for-<?php echo $item->ID ;?>" <?php checked( 'out', $logged_in_out ); ?> value="out" />
    +                        <input type="radio" class="nav-menu-logged-in-out" name="priv-menu-logged-in-out[<?php echo $item->ID ;?>]" id="priv_menu_logged_out-for-<?php echo $item->ID ;?>" <?php checked( 'out', $logged_in_out ); ?> value="out" onclick="jQuery('#priv-menu-access-role-div-<?php echo $item->ID ;?>').hide()"/>
                             <label for="priv_menu_logged_out-for-<?php echo $item->ID ;?>">
                                 <?php _e( 'Logged Out Users', 'privilege-menu'); ?>
                             </label>
    @@ -240,7 +242,7 @@
                         </div>
    
                         <div class="logged-input-holder" style="float: left; width: 35%;">
    -                        <input type="radio" class="nav-menu-logged-in-out" name="priv-menu-logged-in-out[<?php echo $item->ID ;?>]" id="priv_menu_logged_in-for-<?php echo $item->ID ;?>" <?php checked( 'in', $logged_in_out ); ?> value="in" />
    +                        <input type="radio" class="nav-menu-logged-in-out" name="priv-menu-logged-in-out[<?php echo $item->ID ;?>]" id="priv_menu_logged_in-for-<?php echo $item->ID ;?>" <?php checked( 'in', $logged_in_out ); ?> value="in" onclick="jQuery('#priv-menu-access-role-div-<?php echo $item->ID ;?>').show();"/>
                             <label for="priv_menu_logged_in-for-<?php echo $item->ID ;?>">
                                 <?php _e( 'Logged In Users', 'privilege-menu'); ?>
                             </label>
    @@ -247,7 +249,7 @@
                         </div>
    
                         <div class="logged-input-holder" style="float: left; width: 30%;">
    -                        <input type="radio" class="nav-menu-logged-in-out" name="priv-menu-logged-in-out[<?php echo $item->ID ;?>]" id="priv_menu_by_role-for-<?php echo $item->ID ;?>" <?php checked( '', $logged_in_out ); ?> value="" />
    +                        <input type="radio" class="nav-menu-logged-in-out" name="priv-menu-logged-in-out[<?php echo $item->ID ;?>]" id="priv_menu_by_role-for-<?php echo $item->ID ;?>" <?php checked( '', $logged_in_out ); ?> value="" onclick="jQuery('#priv-menu-access-role-div-<?php echo $item->ID ;?>').hide();"/>
                             <label for="priv_menu_by_role-for-<?php echo $item->ID ;?>">
                                 <?php _e( 'All Users', 'privilege-menu'); ?>
                             </label>
    @@ -255,14 +257,11 @@
    
                     </div>
    
    -
    -<?php /* ?>
    -                <div class="field-nav_menu_role nav_menu_role_field description-wide" style="margin: 5px 0;">
    -                    <span class="description"><?php _e( "Access Role", 'priv-menu-roles' ); ?></span>
    +                <div class="field-nav_menu_role nav_menu_role_field description-wide" style="margin: 5px 0;<?php if( $logged_in_out != 'in' ) { echo ' display: none;'; }?>" id="priv-menu-access-role-div-<?php echo $item->ID ;?>">
    +                    <span class="description"><?php _e( "Access Role: leave all unchecked to allow all logged in users to see the menu.", 'priv-menu-roles' ); ?></span>
                         <br />
    
                         <?php
    -
                         // Loop through each of the available roles.
                         foreach ( $display_roles as $role => $name ) {
    
    @@ -284,7 +283,7 @@
    
                     <?php
    
    -		*/
    +		
    
                     /*
                     * end added section
    Index: privMenu.php
    ===================================================================
    --- privMenu.php	(revision 1103563)
    +++ privMenu.php	(working copy)
    @@ -69,29 +69,61 @@
          	* @since 0.2
          	*/
     	function remove_menu_items( $items, $menu, $args ) {
    +
    +    		foreach ( $items as $key => $item ) {
    +				$meta_data = get_post_meta( $item->ID, '_priv_menu_role', true);
    +
    +				// Handle the old format of the meta data.
    +				if( !is_array( $meta_data ) ) {
    +					$temp = $meta_data;
    +					$meta_data = array();
    +					$meta_data['users'] = $temp;
    +					$meta_data['roles'] = array();
    +				}
    +
    +				$visible = true;
    +
    +         		switch( $meta_data['users'] ) {
    +					case 'admin':
    +						$meta_data['roles'][] = 'administrator';
    +					case 'in' :
    +						if( is_user_logged_in() ) {
    +							// By default assume a menu with the "logged in" user resitriction is visible when a user is logged in.
    +							$visible = true;
    +
    +							// Setup for matching of roles if they exist.
    +							$role_match = false;
    +							$role_count = 0;
    +
    +							// Check to see if we have an array of roles or not.
    +							if ( is_array( $meta_data['roles'] ) ) {
    +								// Count the number of roles we have to check.
    +								$role_count = count( $meta_data['roles'] );
    +
    +								// Loop through each role and check to see if this user has it.
    +								foreach( $meta_data['roles'] as $role ) {
    +									if ( current_user_can( $role ) ) {
    +										$role_match = true;
    +									}
    +								}
    +							}
    +
    +							// If we haven't match a user role to the current user and we have user roles set, make this menu item invisible.
    +							if( !$role_match && $role_count > 0 ) { $visible = false; }
    +						}
    +						else {
    +							$visible = false;
    +						}
    
    -    		foreach ( $items as $key => $item ) {
    -			$meta_data = get_post_meta( $item->ID, '_priv_menu_role', true);
    -          		switch( $meta_data ) {
    -				case 'admin':
    -					$visible = current_user_can( 'manage_options' ) ? true : false;
    -					break;
    -            			case 'in' :
    -              				$visible = is_user_logged_in() ? true : false;
    -              				break;
    -            			case 'out' :
    -              				$visible = ! is_user_logged_in() ? true : false;
    -              				break;
    -            			default:
    -	      				$visible = true;
    -				/*
    -              				$visible = false;
    -              				if ( is_array( $item->roles ) && ! empty( $item->roles ) ) foreach ( $item->roles as $role ) {
    -                				if ( current_user_can( $role ) ) $visible = true;
    -              				}
    -				*/
    -              				break;
    +						break;
    +					case 'out' :
    +						$visible = ! is_user_logged_in() ? true : false;
    +						break;
    +					default:
    +						$visible = true;
    +						break;
               		}
    +
               		// add filter to work with plugins that don't use traditional roles
               		$visible = apply_filters( 'nav_menu_roles_item_visibility', $visible, $item );
    
    @@ -124,20 +156,25 @@
             if ( ! isset( $_POST['priv-menu-role-nonce'] ) || ! wp_verify_nonce( $_POST['priv-menu-role-nonce'], 'priv-menu-nonce-name' ) )
                 return;
    
    -        $saved_data = false;
    +        $saved_data = array( 'users' => '', 'roles' => '');
    
    -        if ( isset( $_POST['priv-menu-logged-in-out'][$menu_item_db_id]  )  && in_array( $_POST['priv-menu-logged-in-out'][$menu_item_db_id], array( 'in', 'out', 'admin') ) ) {
    -              $saved_data = $_POST['priv-menu-logged-in-out'][$menu_item_db_id];
    -        } elseif ( isset( $_POST['priv-menu-role'][$menu_item_db_id] ) ) {
    +        if ( isset( $_POST['priv-menu-logged-in-out'][$menu_item_db_id]  )  && in_array( $_POST['priv-menu-logged-in-out'][$menu_item_db_id], array( 'in', 'out') ) ) {
    +              $saved_data['users'] = $_POST['priv-menu-logged-in-out'][$menu_item_db_id];
    +        }
    +
    +		if ( isset( $_POST['priv-menu-role'][$menu_item_db_id] ) ) {
                 $custom_roles = array();
                 // only save allowed roles
                 foreach( $_POST['priv-menu-role'][$menu_item_db_id] as $role ) {
                     if ( array_key_exists ( $role, $allowed_roles ) ) $custom_roles[] = $role;
                 }
    -            if ( ! empty ( $custom_roles ) ) $saved_data = $custom_roles;
    +
    +            if ( ! empty ( $custom_roles ) ) {
    +				$saved_data['roles'] = $custom_roles;
    +			}
             }
    
    -        if ( $saved_data ) {
    +        if ( $saved_data['roles'] != '' || $saved_data['users'] != '' ) {
                 update_post_meta( $menu_item_db_id, '_priv_menu_role', $saved_data );
             } else {
                 delete_post_meta( $menu_item_db_id, '_priv_menu_role' );
    Plugin Author FuzzGuard

    (@fuzzguard)

    Hi Greg,

    I will take a look at adding this code. We have been in development of several other plugins recently so haven’t had a chance to check back on this one.

    I will test and make the changes to the plugin to enable role-based applications.

    You were correct. There had been attempts already at adding in the user role based coding, it just hadn’t been completed.

    PS: Completely happy to credit you in the plugin for helping out and taking the time to do this.

    Thanks,
    FuzzGuard

    Thread Starter Greg Ross

    (@gregross)

    No problem, however as it took so long for a response I found another plugin that already did the user roles.

    Plugin Author FuzzGuard

    (@fuzzguard)

    This has been added into the new plugin. Sorry was waiting on Language translations.

    The Serbian/Croatian translation has not been updated yet. This will be updated in the next week.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘User Roles’ is closed to new replies.