Skip to:
Content

BuddyPress.org

Changeset 12616


Ignore:
Timestamp:
04/13/2020 05:50:50 PM (6 years ago)
Author:
dcavins
Message:

Add invitations repair tool.

Add tool to the WP Admin Dashboard > Tools > Buddypress screen
to create the invitations table when table creation has failed
during the plugin update process.
Additionally, this tool will migrate any outstanding
group invitations once the table is created.

Props shanebp.

Fixes #8141.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/admin/bp-core-admin-tools.php

    r12588 r12616  
    148148        );
    149149
     150        // Invitations:
     151        // - maybe create the database table and migrate any existing group invitations.
     152        $repair_list[110] = array(
     153                'bp-invitations-table',
     154                __( 'Create the database table for Invitations and migrate existing group invitations if needed.', 'buddypress' ),
     155                'bp_admin_invitations_table',
     156        );
     157
    150158        ksort( $repair_list );
    151159
     
    324332        bp_last_activity_migrate();
    325333        return array( 0, sprintf( $statement, __( 'Complete!', 'buddypress' ) ) );
     334}
     335
     336/**
     337 * Create the invitations database table if it does not exist.
     338 * Migrate outstanding group invitations if needed.
     339 *
     340 * @since 6.0.0
     341 *
     342 * @return array
     343 */
     344function bp_admin_invitations_table() {
     345        global $wpdb;
     346
     347        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
     348        require_once( buddypress()->plugin_dir . '/bp-core/admin/bp-core-admin-schema.php' );
     349
     350        $statement = __( 'Creating the Invitations database table if it does not exist… %s', 'buddypress' );
     351        $result    = __( 'Failed to create table!', 'buddypress' );
     352
     353        bp_core_install_invitations();
     354
     355        // Check for existence of invitations table.
     356        $bp_prefix  = bp_core_get_table_prefix();
     357        $table_name = "{$bp_prefix}bp_invitations";
     358        $query = $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table_name ) );
     359        if ( ! $wpdb->get_var( $query ) == $table_name ) {
     360                // Early return if table creation failed.
     361                return array( 2, sprintf( $statement, $result ) );
     362        } else {
     363                $result = __( 'Created invitations table!', 'buddypress' );
     364        }
     365
     366        // Migrate group invitations if needed.
     367        if ( bp_is_active( 'groups' ) ) {
     368                $bp                = buddypress();
     369                $migrate_statement = __( 'Migrating group invitations… %s', 'buddypress' );
     370                $migrate_result    = __( 'Failed to migrate invitations!', 'buddypress' );
     371
     372                bp_groups_migrate_invitations();
     373
     374                // Check that there are no outstanding group invites in the group_members table.
     375                $records = $wpdb->get_results( "SELECT id FROM {$bp->groups->table_name_members} WHERE is_confirmed = 0 AND is_banned = 0" );
     376                if ( empty( $records ) ) {
     377                        $migrate_result = __( 'Migrated invitations!', 'buddypress' );
     378                        return array( 0, sprintf( $statement . ' ' . $migrate_statement , $result, $migrate_result ) );
     379                } else {
     380                        return array( 2, sprintf( $statement . ' ' . $migrate_statement , $result, $migrate_result ) );
     381                }
     382        }
     383
     384        // Return a "create-only" success message.
     385        return array( 0, sprintf( $statement, $result ) );
    326386}
    327387
Note: See TracChangeset for help on using the changeset viewer.