itthinx / groups

Groups provides group-based user membership management, group-based capabilities and content access control. It integrates standard WordPress capabilities and application-specific capabilities along with an extensive API.
GNU General Public License v3.0
49 stars 35 forks source link

Allow requested table prefix to be filtered #111

Closed christianwach closed 4 years ago

christianwach commented 4 years ago

@proaktion Another fresh PR as promised in #103, taking into account your thoughts on the scope of the filter. For details and discussion, please refer to that PR.

The reworked filter in this PR gives the Groups plugin the same flexibility as the filter in #103 but without the potential to alter the tables that Groups refers to. Indeed, this is what BuddyPress does (and what I should have suggested in the first place) so thanks for highlighting the problem.

With this filter, I can still restrict Groups to a single point of reference for the user capabilities I'm interested in, i.e. retaining just the tables on the main site:

// Prevent Groups from creating and destroying per-site tables.
remove_action( 'wpmu_new_blog', 'Groups_Controller::wpmu_new_blog', 9, 2 );
remove_action( 'delete_blog', 'Groups_Controller::delete_blog', 10, 2 );

// Prevent Groups from removing users from groups when removed from a blog.
remove_action( 'remove_user_from_blog', 'Groups_User_Group::remove_user_from_blog', 10, 2 );

// Filter the prefix of the referenced tables.
add_filter( 'groups_get_table_prefix', 'my_groups_table_prefix', 10, 1 );

/**
 * Filter the table prefix.
 *
 * @param str $prefix The WordPress table prefix.
 * @return str $my_db_prefix The modified table prefix.
 */
function my_groups_table_prefix( $prefix ) {

    global $wpdb;
    static $my_db_prefix = '';

    // Overwrite with the main site prefix.
    if ( empty( $my_db_prefix ) ) {
        $my_db_prefix = $wpdb->get_blog_prefix( get_main_site_id() );
    }

    return $my_db_prefix;

}

The result is the same as #103 from a functional perspective but with a lighter touch.

Cheers, Christian

christianwach commented 4 years ago

Rebased branch to keep pace with master.

@proaktion Any thoughts? Does this resolve the reservations you had about the previous PR?

christianwach commented 4 years ago

@proaktion @itthinx Any progress on considering this?

christianwach commented 4 years ago

@proaktion @itthinx It would be great to get your thoughts on this.

proaktion commented 4 years ago

Hi there Christian!

Many thanks again, this looks good, going to add this to the current version development due to be released shortly.

Cheers!

christianwach commented 4 years ago

@proaktion Thanks, much appreciated! This will bring really useful flexibility to some multisite contexts.

proaktion commented 4 years ago

FYI 2.11.0 is out with this added

christianwach commented 4 years ago

I look forward to deleting my branch! Thanks again.