[buddypress-trac] [BuddyPress Trac] #6210: Create New Invitations API

buddypress-trac noreply at wordpress.org
Thu Aug 1 17:39:25 UTC 2019


#6210: Create New Invitations API
------------------------------+-----------------------
 Reporter:  dcavins           |       Owner:  dcavins
     Type:  enhancement       |      Status:  reopened
 Priority:  low               |   Milestone:  5.0.0
Component:  Core              |     Version:
 Severity:  normal            |  Resolution:
 Keywords:  has-patch commit  |
------------------------------+-----------------------

Comment (by dcavins):

 Hi @imath-

 Thanks for your notifications patch! I also revisited this question this
 morning:

 > About bp_groups_migrate_invitations() I'm a little worried if there are
 a lot of group invites, pending requests to migrate : is there a risk of
 time out ?

 I tested on a site with 12,000 invitations to migrate and it was able to
 finish the migration successfully. It took 25s and used 140MB of memory.

 It is possible to manually build a bulk insert:
 {{{#!php
 <?php
 /**
  * Migrate invitations and requests from pre-5.0 group_members table to
 invitations table.
  *
  * @since 5.0.0
  */
 function bp_groups_migrate_invitations() {
         global $wpdb;
         $bp = buddypress();

         $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" );

         $processed = array();
         $values = array();
         foreach ( $records as $record ) {
                 $values[] = $wpdb->prepare(
                         "(%d, %d, %s, %s, %d, %d, %s, %s, %s, %d, %d)",
                         (int) $record->user_id,
                         (int) $record->inviter_id,
                         '',
                         'bp_groups_invitation_manager',
                         (int) $record->group_id,
                         0,
                         ( 0 === (int) $record->inviter_id ) ? 'request' :
 'invite',
                         $record->comments,
                         $record->date_modified,
                         (int) $record->invite_sent,
                         0
                 );
                 $processed[] = (int) $record->id;
         }

         $table_name = BP_Invitation_Manager::get_table_name();
         $query = "INSERT INTO {$table_name} (user_id, inviter_id,
 invitee_email, class, item_id, secondary_item_id, type, content,
 date_modified, invite_sent, accepted) VALUES ";
         $query .= implode(', ', $values );
         $query .= ';';
         $wpdb->query( $query );

         $ids_to_delete = implode( ',', $processed );
         if ( $ids_to_delete ) {
                 $wpdb->query( "DELETE FROM
 {$bp->groups->table_name_members} WHERE ID IN ($ids_to_delete)" );
         }
 }

 }}}

 The bulk insert is much faster and more efficient (9s and 35MB memory for
 the same 12,000 invitations). I'm not sure that I'm building it in the
 safest/most reliable way, though, so any feedback would be welcome.

-- 
Ticket URL: <https://buddypress.trac.wordpress.org/ticket/6210#comment:50>
BuddyPress Trac <http://buddypress.org/>
BuddyPress Trac


More information about the buddypress-trac mailing list