Resources

My Agape try

Your Agape try


Support Forum

You must be logged in to post Login Register

Search 
Search Forums:


 




Bulk Role Assignments

UserPost

3:07 pm
August 12, 2011


galz

Member

posts 4

1

Hi, 

First off, this plugin is amazing.  It really transforms WP into a true CMS.

My question is, I'm wondering if I can do a bulk roles assignment.  I have a situation where I have 1 post that has 5000 "sub" posts that relate to it.  The only way they are tied together in the db is through a custom field on the "sub" posts.  They are not tied via the post_parent column in the wp_posts table so WP and RS don't know these posts are related.  

What I'm trying to do, is assign a contributor to the 1 main post and have that role assignment propogate to the 5000 "sub" posts.  I'm currently accomplishing this like so:

1. Call ScoperRoleAssignments::get_assigned_roles on the main post

2. Create an "entity" array for use in assign_roles() that matches the assigned roles I get in step 1. 

    For example:   array( "custom_post_type_contributor" => array( $group_id => "entity") );

3. Then calling assign_roles() for each of the 5000 "sub" posts assigning them the "entity" array created in step 2.

This works great, except it's slow for a large number of "sub" posts.  Is there any way to do a bulk role assignment so I would only have to call assign_roles once for all 5000 "sub" posts?

Or is there a way to tie the posts together somehow so that roles and permissions applied to the main post get inherited by the "sub" posts?  


Thanks and please let me know.

3:44 pm
August 12, 2011


Kevin

Admin

posts 2503

2

I would look into hacking function awp_query_descendant_ids().

Maybe something like:

if ( defined( 'MY_BULK_ROLES_HACK' ) ) {
  // set $descendant_ids based on post_meta query you write
} else {
  // set $descendant_ids based on existing post_parent query
}

6:58 pm
August 14, 2011


galz

Member

posts 4

3

Thanks Kevin.  I was able to make that work.  

In addition to modifying awp_query_descendant_ids, I also had to modify _compare_role_settings to force group role propogation to descendants for a particular post type.  I had to force $assign_for = "both". Otherwise, the following code, would never go into the if condition and awp_query_descendant_ids would never get called down the line:

// if assign_for was changed from 'entity' to 'children' or 'both', need to insert roles for children

if ( ($assign_for == ASSIGN_FOR_CHILDREN_RS) || ($assign_for == ASSIGN_FOR_BOTH_RS) ) {

    if ( empty($role_assignment) || ( ASSIGN_FOR_ENTITY_RS == $role_assignment['assign_for'] ) ) {

        $retval['new_propagation'] = ( $assignment_id ) ? $assignment_id : true;

        $retval['role_change'] = true;

    }

}

Does that make sense to do?  Is there a simpler place in the code to indicate that group roles should be propogated to descendants even though the post being modified doesn't have descendants from WP's perspective (i.e. no post_parent values that match the post being modified)?

Thanks,

Gal


7:16 am
August 15, 2011


Kevin

Admin

posts 2503

4

Good point about setting assign_for to 'both'.

I was also thinking that if you're planning to leave your hack enabled long-term, you should merge it with the normal awp_query_descendant_ids() output, instead of replacing it.

1:09 pm
August 15, 2011


galz

Member

posts 4

5

yeah, that makes sense.  I can add an apply_filters with the list of descendant ids your plugin generates to array_merge them with one that my hack generates.  

Thanks!

4:34 pm
August 15, 2011


Kevin

Admin

posts 2503

6

Sounds good. How about

apply_filters( 'awp_query_descendant_ids', $descendant_ids, $table_name, $col_id, $parent_id );

I'll add that to the development code soon if it looks sufficient to you.

4:37 pm
August 15, 2011


Kevin

Admin

posts 2503

7

Actually, I'd rather swap those last two arguments:

apply_filters( 'awp_query_descendant_ids', $descendant_ids, $table_name, $parent_id, $col_id );

7:00 pm
August 15, 2011


galz

Member

posts 4

8

Looks good to me.  Hopefully that will help other developers.  

I still won't be able to upgrade though (without reapplying the hack).  I had to make another change to force propogation of roles on a save of a post that doesn't technically have any descendants (i.e. no other posts have the main post as a parent_post).  Is there any hook or easy way to accomplish this?  Otherwise, awp_query_descendant_ids never gets called.

Thanks Again,

Gal


About the Agapetry forum

Currently Online:

11 Guests

Maximum Online: 150

Forums:

Groups: 2

Forums: 7

Topics: 1245

Posts: 5653

Members:

There are 1255 members

There are 1 guests


Kevin has made 2503 posts

Top Posters:

metal450 - 178

Ragnar - 108

YikYak - 70

whiteorb - 49

Daisy - 35

Administrator: Kevin | Moderators: Kevin