Skip to:
Content

BuddyPress.org

Ticket #8141: 8141.2.patch

File 8141.2.patch, 2.9 KB (added by dcavins, 6 years ago)

Expand Shane's patch to also migrate group invitations.

  • src/bp-core/admin/bp-core-admin-tools.php

    diff --git src/bp-core/admin/bp-core-admin-tools.php src/bp-core/admin/bp-core-admin-tools.php
    index 0ad810e0d..3af5b1616 100644
    function bp_admin_repair_list() { 
    147147                'bp_admin_reinstall_emails',
    148148        );
    149149
     150        // Invitations
     151        // - maybe create the database table
     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
    152160        /**
    function bp_admin_repair_last_activity() { 
    325333        return array( 0, sprintf( $statement, __( 'Complete!', 'buddypress' ) ) );
    326334}
    327335
     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 a "create-only" failure message.
     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, group_id, user_id, inviter_id, date_modified, comments, invite_sent 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 ) );
     386}
     387
    328388/**
    329389 * Assemble admin notices relating success/failure of repair processes.
    330390 *