Change WordPress user roles and capabilities Forums How to or FAQ Set Posts to manage at User level but by an ACF setting

Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #6671
    edering
    Participant

    We have an Advanced Custom Field group that allows us to set Pages/Posts by a Team number (Team 1, Team 2, Team 3 etc). We will be creating a limited role for editing pages/posts, and want to know if we can use this when creating a User that is set to that Role, like you offer for by Page/Post ID Restrictions. This way, we would not have to find all of the Page/Post IDs for each team, especially because the pages that Teams are set for change sometimes. Is this possible?

    #6672
    Vladimir
    Keymaster

    If you can extract needed post/page IDs for a team via PHP, you can add them automatically to the edit restrictions set for selected user or role using ure_edit_posts_access_id_list custom filter.

    #6702
    edering
    Participant

    Hi Vladimir,

    I took your recommendation above and to use the ure_edit_posts_access_id_list function and created this:

    add_filter('ure_edit_posts_access_id_list', 'ure_edit_posts_access_set_id_list_cu');
    
    function ure_edit_posts_access_set_id_list_cu($list) {
    
    	$current_user = wp_get_current_user();
    
    	$ag = get_field('analytics_group_name','user_' . $current_user->ID);
    
    	if(!isset($ag)){
    
    		return; 
    
    	}else{
    
    		$posts = get_posts(array(
    			'numberposts'	=> -1,
    			'post_type'		=> 'page',
    			'meta_query'	=> array(
    				'relation'		=> 'OR',
    				array(
    					'key'	 	=> 'analytics_group_name',
    					'value'	  	=> $ag,
    					'compare' 	=> 'IN',
    				),
    			),
    		));
    
    		foreach($posts as $post){
    			$list .= $post->ID.',';
    		}
    
    		$list = rtrim($list, ',');
    
    		wp_reset_postdata();
    
        	return $list;
    	}
    }

    I’m able to return a list via echo and can see my comma separated list in the admin at the top of the page if I use init() temporarily. However, when I attempt to create a page (or any post type) as one of the role types that have a restriction by custom field, the page eventually runs out of memory (currently set to 1G) and we receive several fatal errors:
    https://www.dropbox.com/s/wa0x9v892xrf0ua/server-errors.jpg?dl=0

    This role has what I believe to be everything needed for this role to create a post type:
    create_pages
    create_posts
    create_tablepress_tables
    create_tribe_events

    Other roles that we have set up do not have this issue, only this one. Any thoughts or recommendations on what we can try to resolve this issue?

    #6703
    Vladimir
    Keymaster

    1st of all, filter function should always return value.
    Replace

    
    if (!isset($ag)) {
       return; 
    } else {
    

    with

    
    if (!isset($ag)) {
       return $list; 
    } else {
    

    Is it too much posts (approximate quantity) has analytics group name, for which you get out of memory error?

    #6704
    edering
    Participant

    Vladimir,

    Thanks for the response. I had it return early in the even tthat there the were no values assigned to the custom field. Should I not do that? Also, there are ~500 pages on the site. There are likely occasions that nearly all could be returned. Would that cause this to time out?

    #6705
    Vladimir
    Keymaster

    Yes, your function as filter should always return value. If there is nothing to change, then return unchanged value it got as input parameter.

    500 is not a problem. There are installations with thousands posts and/or pages, which work fine.

    Try to turn ON wordpress debug mode with writing to file. Add this lines to wp-config.php for that:

    
    define('WP_DEBUG', true);
    define('WP_DEBUG_LOG', true);
    

    WordPress will write all error messages to wp-content/debug.log file in this case. Reproduce the error.
    Look if ‘user-role-editor-pro’ is mentioned at the error messages.

    Another variant for investigation:
    Strange, that out of memory error is raised at different times from different sources. Can you try to deactivate all plugins except URE and test again. In case error will go away, activate plugins back one by one until error will return. Just in case some other plugin eats to much memory.

    #6706
    edering
    Participant

    Thanks. I have debugging turned on and the only errors logged are the ones are showing are the ones I’ve pasted above:
    https://www.dropbox.com/s/wa0x9v892xrf0ua/server-errors.jpg?dl=0

    I’ll try turning off plugins and making the return adjustment above.

    #6707
    Vladimir
    Keymaster

    Another suggestion –
    You use get_posts() inside your filter function. URE’s uses ‘pre_get_posts’ hook to define the list of the allowed posts, thus calling get_posts() inside filter function may lead to the endless recursion and finally to the problem with available memory. So try to switch off this custom filter before to call get_posts(), then restore it back, like this:

    
    remove_filter('ure_edit_posts_access_id_list', 'ure_edit_posts_access_set_id_list_cu'); 
    $posts = get_posts(array(
    			'numberposts'	=> -1,
    			'post_type'		=> 'page',
    			'meta_query'	=> array(
    				'relation'		=> 'OR',
    				array(
    					'key'	 	=> 'analytics_group_name',
    					'value'	  	=> $ag,
    					'compare' 	=> 'IN',
    				),
    			),
    		));
    add_filter('ure_edit_posts_access_id_list', 'ure_edit_posts_access_set_id_list_cu');
    

    If this will not help, let me know, we will try to switch off/restore directly URE’s global post edit access filter.

    #6709
    edering
    Participant

    Vladimir,

    I’ve determined that having the get_posts loop inside the function is causing issues, however, if I take it outside of the function, I can no longer access the values of the loop. Am I missing something?

    #6710
    edering
    Participant

    Vladimir, I create an Admin user for you. Please look for an email to set your password. I am hoping you having access to the functions file and seeing where people assign the Analytics Group Name to a User will help you.

    #6713
    Vladimir
    Keymaster

    Erik,

    I moved get_posts() out of filter function and saved its result inside global variable. So filter function looks now as:

    
    function ure_edit_posts_access_set_id_list_cu($list) {
    	global $ag_posts_list;
    	
    	if ( empty( $ag_posts_list ) ) {
    		return $list;
    	}
    	
    	if ( empty( $list ) ) {
    		return $ag_posts_list;
    	}
    	
    	$list .= ', '. $ag_posts_list;
    	
    	return $list;
    

    I created successfully new page ‘Test 124’ under user with ‘Program Editor’.
    Make your own test and let me know the result.

    #6716
    edering
    Participant

    Vlad,

    Thanks for your work on this. Unfortunately, the user group in question isn’t working. If you’re able, can you create a user with the role Program Editor and assign a
    Analytics Group Name checkbox in the user profile? You should not be able to see all posts — only a limited number. The good news is that creating a post no longer fails, however, all posts should not show for non-admin roles. Are you able to take a look? Thanks for your help so far.

    #6717
    Vladimir
    Keymaster

    Erik,

    I updated custom code at functions.php to this version:

    
    function fetch_analytics_posts(){
    	global $post; 
    
    	$current_user = wp_get_current_user();
    	$ag = get_field('analytics_group_name','user_' . $current_user->ID);
    	if ( !isset($ag) ) {
    		return '';
    	}
    	
    	$posts = get_posts(array(
            'suppress_filters'  => true,    
    	'numberposts'	=> -1,
    	'post_type'		=> 'page',
    	'meta_query'	=> array(
    		'relation'		=> 'OR',
    		array(
    			'key'	 	=> 'analytics_group_name',
    			'value'	  	=> $ag,
    			'compare' 	=> 'IN',
    			),		
    		),
    	));
    
    	foreach($posts as $post){
    		$list .= $post->ID.',';
    	}
    
    	$list = rtrim($list, ',');
    
    	wp_reset_postdata();
    
    	// echo 'allowed posts:'. $list;
    	return $list;
    }
    
    add_filter('ure_edit_posts_access_id_list', 'ure_edit_posts_access_set_id_list_cu', 10, 1);
    
    function ure_edit_posts_access_set_id_list_cu($list) {
    	
            remove_filter('ure_edit_posts_access_id_list', 'ure_edit_posts_access_set_id_list_cu', 10);
            $ag_posts_list = fetch_analytics_posts();
            add_filter('ure_edit_posts_access_id_list', 'ure_edit_posts_access_set_id_list_cu', 10, 1);
            
    	if ( empty( $ag_posts_list ) ) {
    		return $list;
    	}
    	
    	if ( empty( $list ) ) {
    		return $ag_posts_list;
    	}
    	
    	$list .= ', '. $ag_posts_list;
    	
    	return $list;
    }
    

    I tested with existing user with ‘Program Editor’ role. When I turn ON SBU002 analytics group for her, she sees 30 pages only. (Pay attention that some pages with empty analytics group field were returned by your get_posts() query).

    #6722
    edering
    Participant

    Thank you! This is working great now!

Viewing 14 posts - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.