Skip to:
Content

bbPress.org

Changeset 6668


Ignore:
Timestamp:
08/25/2017 06:21:26 AM (8 years ago)
Author:
johnjamesjacoby
Message:

Tools: include users with "unexpected" roles in bbp_admin_repair_user_roles()

This change makes sure that if a user has an unexpectedly missing or predictably broken role entry, they are still mapped to the $default_role for the current site, and fixes a problem with the role repair tool where users with the most broken roles were still never actually repaired.

Trunk, for 2.6.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/admin/tools/repair.php

    r6644 r6668  
    723723    }
    724724
     725    // Get non-forum roles
     726    $blog_roles = array_keys( bbp_get_blog_roles() );
     727
    725728    // Iterate through each role...
    726     foreach ( array_keys( bbp_get_blog_roles() ) as $role ) {
     729    foreach ( $blog_roles as $role ) {
    727730
    728731        // Reset the offset
     
    730733
    731734        // If no role map exists, give the default forum role (bbp-participant)
    732         $new_role = isset( $role_map[ $role ] ) ? $role_map[ $role ] : $default_role;
     735        $new_role = isset( $role_map[ $role ] )
     736            ? $role_map[ $role ]
     737            : $default_role;
    733738
    734739        // Get users of this site, limited to 1000
     
    743748            foreach ( (array) $users as $user_id ) {
    744749                if ( bbp_set_user_role( $user_id, $new_role ) ) {
     750                    ++$changed; // Keep a count to display at the end
     751                }
     752            }
     753
     754            // Bump the offset for the next query iteration
     755            $offset = $offset + 1000;
     756        }
     757    }
     758
     759    // Reset the offset
     760    $offset  = 0;
     761    $bbp_db  = bbp_db();
     762    $cap_key = $bbp_db->get_blog_prefix() . 'capabilities';
     763
     764    // Users without roles should be granted the default role, but not on multi-
     765    // site installations where not all users get a role by default.
     766    if ( ! is_multisite() ) {
     767
     768        // Get users with missing capabilities on this site, limited to 1000
     769        while ( $users = get_users( array(
     770            'meta_key'     => $cap_key,
     771            'meta_compare' => 'NOT EXISTS',
     772            'fields'       => 'ID',
     773            'number'       => 1000,
     774            'offset'       => $offset
     775        ) ) ) {
     776
     777            // Iterate through each user of $role and try to set it
     778            foreach ( (array) $users as $user_id ) {
     779                if ( bbp_set_user_role( $user_id, $default_role ) ) {
     780                    ++$changed; // Keep a count to display at the end
     781                }
     782            }
     783
     784            // Bump the offset for the next query iteration
     785            $offset = $offset + 1000;
     786        }
     787
     788    // On multisite, we'll look for users with an empty capabilities array.
     789    // These are users who basically have malformed caps, and we can fix that.
     790    } else {
     791
     792        // Get users with empty capabilities on this site, limited to 1000
     793        while ( $users = get_users( array(
     794            'meta_key'   => $cap_key,
     795            'meta_value' => 'a:0:{}',
     796            'fields'     => 'ID',
     797            'number'     => 1000,
     798            'offset'     => $offset
     799        ) ) ) {
     800
     801            // Iterate through each user of $role and try to set it
     802            foreach ( (array) $users as $user_id ) {
     803                if ( bbp_set_user_role( $user_id, $default_role ) ) {
    745804                    ++$changed; // Keep a count to display at the end
    746805                }
Note: See TracChangeset for help on using the changeset viewer.