How to see old announcements after the role change


Home Forums Front End PM PRO How to see old announcements after the role change

This topic is: Not Resolved
Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #17224
    Aaku Asujamaa
    Participant

    Hi!

    We are looking for a solution to get the user to see old announcements of the role after the user role have been set to the user.

    For example:
    1. We send an Announcement1 to Role1
    2. We add Role1 user role for User2
    3. User2 would see the Announcement1 and all the other previous announcements of Role1

    If I remember right, this is how it worked in the previous version of the plugin? Is there any way to get it work again?

    #17233
    Shamim Hasan
    Keymaster

    Add following code in your theme’s (child theme’s if any) functions.php

    add_action( 'set_user_role', function( $user_id, $role, $old_roles ) {
    	global $wpdb;
    	
    	if ( ! defined( 'FEP_PARTICIPANT_TABLE' ) ) {
    		return false;
    	}
    	$mgs_ids = $wpdb->get_col( $wpdb->prepare( "SELECT fep_message_id FROM $wpdb->fep_messagemeta where meta_key = %s AND meta_value = %s", '_fep_participant_roles', $role ) );
    	if ( $mgs_ids ) {
    		$query = 'INSERT INTO ' . FEP_PARTICIPANT_TABLE . ' (mgs_id, mgs_participant) VALUES ';
    		
    		foreach ( $mgs_ids as $mgs_id ) {
    			$values[] = $mgs_id;
    			$values[] = $user_id;
    			
    			$place_holders[] = '(%d, %d)';
    		}
    		$query .= implode( ', ', $place_holders );
    		$wpdb->query( $wpdb->prepare( $query, $values ) );
    		
    		delete_user_meta( $user_id, '_fep_user_announcement_count' );
    	}
    }, 10, 3 );
    
    #17239
    Aaku Asujamaa
    Participant

    Hi Shamim! And thanks for the fast reply. We have now added that code to our child theme but can’t get the functionality to work. All the other codes in the child theme work correctly. I have tried to activate and deactivate plugins but that have not have any effect.

    I’m not an expert on php scripts so can’t really say if there is some kind of error in the code or if I just misstated our need. I found one conversation regarding the group messages where the asker wanted to have exact the opposite kind of function for group messages:

    https://www.shamimsplugins.com/support/topic/group-message-2/

    At the moment it seems that group messages either do not work the way you described in the above conversation? If I add a member to a group he won’t see the old messages of the group.

    Is this issue correlated with issue in announcements? The announcement behaviour is a priority for us but wouldn’t mind if the group messages would start to work same way too as you described in the above conversation.

    Cheers!

    #17249
    Shamim Hasan
    Keymaster

    From version 10.1.1 both group message and announcement work this way. Users can access group messages/ announcements only which is sent when they are in that role/group.

    I have tested above code again and find working.
    I have created an announcement to test role RA. Then i have checked if user UA (which user role is not RA) have got that announcement. find that user UA have no access to that announcement. Now i changed that user role to RA and now that user have access to that announcement.

    Please check and let me know.

    #17267
    Aaku Asujamaa
    Participant

    Hi Shamim! Thanks again!

    We just made a new conflict test and found out that our membership role plugin caused the problem. We used Members plugin (https://wordpress.org/plugins/members/) and when we deactivated that, your code started working.

    We would need to be able to assign multiple roles to one user. We already tested out another plugin called User Role Editor (https://wordpress.org/plugins/user-role-editor/), but with that activated your code didn’t work either.

    What role management plugin you are using? Or should the code be edited someway to work also when user is assigned to multiple roles?

    Cheers!

    #17272
    Shamim Hasan
    Keymaster

    Use this code

    add_action( 'add_user_role', 'fep_cus_update_ann_when_role_change', 10, 2 );
    add_action( 'set_user_role', 'fep_cus_update_ann_when_role_change', 10, 2 );
    function fep_cus_update_ann_when_role_change( $user_id, $role ) {
    	global $wpdb;
    	
    	if ( ! defined( 'FEP_PARTICIPANT_TABLE' ) ) {
    		return false;
    	}
    	$mgs_ids = $wpdb->get_col( $wpdb->prepare( "SELECT fep_message_id FROM $wpdb->fep_messagemeta where meta_key = %s AND meta_value = %s", '_fep_participant_roles', $role ) );
    	if ( $mgs_ids ) {
    		$query = 'INSERT INTO ' . FEP_PARTICIPANT_TABLE . ' (mgs_id, mgs_participant) VALUES ';
    		
    		foreach ( $mgs_ids as $mgs_id ) {
    			$values[] = $mgs_id;
    			$values[] = $user_id;
    			
    			$place_holders[] = '(%d, %d)';
    		}
    		$query .= implode( ', ', $place_holders );
    		$wpdb->query( $wpdb->prepare( $query, $values ) );
    		
    		delete_user_meta( $user_id, '_fep_user_announcement_count' );
    	}
    }
    
    #17305
    Aaku Asujamaa
    Participant

    Thanks again Shamim! That made the trick! There is still one problem which we haven’t been able to solve yet, but I will get back to you later regarding that.

    Now more urgent problem is that we upgraded our license 2-5 sites license but I can’t get the license to work on a new site. I visited my account and added the new site (tutkimukset.buenno.fi) to the Site URL list, but still FEPM gives me an error:

    “Your Front End PM PRO is not active for this URL. Please visit your account page to manage your license key URLs.”

    I have a correct license key copied and site URL active. Do you know where the problem might be?

    #17309
    Shamim Hasan
    Keymaster
    #17312
    Aaku Asujamaa
    Participant

    Thank you that made the license error to dissapear.

    But the role to role block setting still doesn’t work. I’m a bit confused why. I have erased the whole plugin now three times again and still the “Add more” button does not work. It works on our development site but on the production site it does nothing. (The production site is the same place where we had the license error)

    We have now erased the plugin totally three times and set it up again. The add more button stays unresponsive.

    Any idea where this problem might come from?

    #17316
    Shamim Hasan
    Keymaster

    There are add more buttons for Admins and Groups in Front End PM PRO > Settings > Recipient. Does those work?
    Are you having any js error in role to role block setting page? Please see browser console and let me know.

    #17321
    Aaku Asujamaa
    Participant

    Those add more buttons in Recipient settings do work. I found that there is one JS error in the role block setting page:

    Uncaught SyntaxError: missing ) after argument list
    admin.php?page=fep_settings&tab=security:485

    #17324
    Aaku Asujamaa
    Participant
    This reply has been marked as private.
    #17327
    Shamim Hasan
    Keymaster

    See your role name 'Viitasaari started with '. Remove that ' from role name. Then try.

    Let me know.

    #17329
    Aaku Asujamaa
    Participant

    Thank you Shamim! I should have spotted that by myself but I’m so unfamiliar with the JS that didn’t really know where to look for.

    Now everything else is working as planned, much because of your awesome support!

    The only thing with my original request still is that if I reset same role back to the user the old announcements do not anymore appear.

    I mean that if I continue your example onwards from there where the user UA has received the old announcement:

    1. I take the test role RA away from the user UA
    2. I make a new Announcement2 for role RA
    3. I reset the role for user UA as RA
    4. The user UA does not anymore receive the Announcement2

    It seems that the wordpress somehow remember that the user UA has some time had to role UA and doesn’t anymore run the code.

    #17335
    Shamim Hasan
    Keymaster

    By default wordpress allow only one role for user. It makes difficult to debug. As you are using another plugin to set multiple roles to a user.

    You can debug line by line to find your issue.

Viewing 15 posts - 1 through 15 (of 15 total)

You need to purchase ‘Front End PM PRO’ to create topic in this support forum.

If you already purchased ‘Front End PM PRO’ please LOGIN.