Skip to:
Content

BuddyPress.org

Ticket #6370: 6370.03.patch

File 6370.03.patch, 8.1 KB (added by r-a-y, 9 years ago)
  • src/bp-blogs/bp-blogs-functions.php

     
    7272/**
    7373 * Populate the BP blogs table with existing blogs.
    7474 *
    75  * @since 1.0.0
     75 * Warning: By default, this will remove all existing records from the BP
     76 * blogs and blogmeta tables before re-populating the tables.
    7677 *
    77  * @global object $wpdb WordPress database object.
    78  * @uses get_users()
    79  * @uses bp_blogs_record_blog()
     78 * @since 1.0.0
     79 * @since 2.6.0 Accepts $args as a parameter.
     80 *
     81 * @param array $args {
     82 *     Array of arguments.
     83 *     @type int   $offset   The offset to use.
     84 *     @type int   $limit    The number of blogs to record at one time.
     85 *     @type array $blog_ids Blog IDs to record. If empty, all blogs will be recorded.
     86 *     @type array $site_id  The network site ID to use.
     87 * }
    8088 *
    8189 * @return bool
    8290 */
    83 function bp_blogs_record_existing_blogs() {
     91function bp_blogs_record_existing_blogs( $args = array() ) {
    8492        global $wpdb;
    8593
    8694        // Query for all sites in network.
     95        $r = bp_parse_args( $args, array(
     96                'offset'   => false === bp_get_option( '_bp_record_blogs_offset' ) ? 0 : bp_get_option( '_bp_record_blogs_offset' ),
     97                'limit'    => 50,
     98                'blog_ids' => array(),
     99                'site_id'  => $wpdb->siteid
     100        ), 'record_existing_blogs' );
     101
     102        // Truncate all BP blogs tables if starting fresh
     103        if ( empty( $r['offset'] ) && empty( $r['blog_ids'] ) ) {
     104                $bp = buddypress();
     105
     106                // Truncate user blogs table
     107                $truncate = $wpdb->query( "TRUNCATE {$bp->blogs->table_name}" );
     108                if ( is_wp_error( $truncate ) ) {
     109                        return false;
     110                }
     111
     112                // Truncate user blogmeta table
     113                $truncate = $wpdb->query( "TRUNCATE {$bp->blogs->table_name_blogmeta}" );
     114                if ( is_wp_error( $truncate ) ) {
     115                        return false;
     116                }
     117        }
     118
     119        // Multisite
    87120        if ( is_multisite() ) {
     121                $sql = array();
     122                $sql['select'] = $wpdb->prepare( "SELECT blog_id, last_updated FROM {$wpdb->base_prefix}blogs WHERE mature = 0 AND spam = 0 AND deleted = 0 AND site_id = %d", $r['site_id'] );
    88123
    89                 // Get blog ID's if not a large network.
    90                 if ( ! wp_is_large_network() ) {
    91                         $blog_ids = $wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM {$wpdb->base_prefix}blogs WHERE mature = 0 AND spam = 0 AND deleted = 0 AND site_id = %d", $wpdb->siteid ) );
     124                // Omit root blog if large network
     125                if ( wp_is_large_network( 'users' ) ) {
     126                        $sql['omit_root_blog'] = $wpdb->prepare( "AND blog_id != %d", bp_get_root_blog_id() );
     127                }
    92128
    93                         // If error running this query, set blog ID's to false.
    94                         if ( is_wp_error( $blog_ids ) ) {
    95                                 $blog_ids = false;
    96                         }
     129                // Filter by selected blog IDs
     130                if ( ! empty( $r['blog_ids'] ) ) {
     131                        $in        = implode( ',', wp_parse_id_list( $r['blog_ids'] ) );
     132                        $sql['in'] = "AND blog_id IN ({$in})";
     133                }
    97134
    98                 // Large networks are not currently supported.
    99                 } else {
    100                         $blog_ids = false;
     135                $sql['orderby'] = 'ORDER BY blog_id ASC';
     136
     137                $sql['limit'] = $wpdb->prepare( "LIMIT %d", $r['limit'] );
     138
     139                if ( ! empty( $r['offset'] ) ) {
     140                        $sql['offset'] = $wpdb->prepare( "OFFSET %d", $r['offset'] );
    101141                }
    102142
     143                $blogs = $wpdb->get_results( implode( ' ', $sql ) );
     144
    103145        // Record a single site.
    104146        } else {
    105                 $blog_ids = $wpdb->blogid;
    106         }
     147                // Just record blog for the current user only.
     148                $record = bp_blogs_record_blog( $wpdb->blogid, get_current_user_id(), true );
    107149
    108         // Bail if there are no blogs in the network.
    109         if ( empty( $blog_ids ) ) {
    110                 return false;
     150                if ( false === $record ) {
     151                        return false;
     152                } else {
     153                        return true;
     154                }
    111155        }
    112156
    113         // Get BuddyPress.
    114         $bp = buddypress();
    115 
    116         // Truncate user blogs table.
    117         $truncate = $wpdb->query( "TRUNCATE {$bp->blogs->table_name}" );
    118         if ( is_wp_error( $truncate ) ) {
    119                 return false;
    120         }
     157         // Bail if there are no blogs
     158         if ( empty( $blogs ) ) {
     159                // Make sure we remove our offset marker
     160                if ( is_multisite() ) {
     161                        bp_delete_option( '_bp_record_blogs_offset' );
     162                }
    121163
    122         // Truncate user blogsmeta table.
    123         $truncate = $wpdb->query( "TRUNCATE {$bp->blogs->table_name_blogmeta}" );
    124         if ( is_wp_error( $truncate ) ) {
    125164                return false;
    126165        }
    127166
    128167        // Loop through users of blogs and record the relationship.
    129         foreach ( (array) $blog_ids as $blog_id ) {
     168        foreach ( (array) $blogs as $blog ) {
    130169
    131170                // Ensure that the cache is clear after the table TRUNCATE above.
    132                 wp_cache_delete( $blog_id, 'blog_meta' );
     171                wp_cache_delete( $blog->blog_id, 'blog_meta' );
    133172
    134173                // Get all users.
    135174                $users = get_users( array(
    136                         'blog_id' => $blog_id
     175                        'blog_id' => $blog->blog_id,
     176                        'fields'  => 'ID'
    137177                ) );
    138178
    139179                // Continue on if no users exist for this site (how did this happen?).
     
    142182                }
    143183
    144184                // Loop through users and record their relationship to this blog.
    145                 foreach ( (array) $users as $user ) {
    146                         bp_blogs_add_user_to_blog( $user->ID, false, $blog_id );
     185                foreach ( (array) $users as $user_id ) {
     186                        bp_blogs_add_user_to_blog( $user_id, false, $blog->blog_id );
     187
     188                        // Clear cache
     189                        bp_blogs_clear_blog_object_cache( $blog->blog_id, $user_id );
     190                }
     191
     192                // Update blog last activity timestamp
     193                if ( ! empty( $blog->last_updated ) && false !== strtotime( $blog->last_updated ) ) {
     194                        bp_blogs_update_blogmeta( $blog->blog_id, 'last_activity', $blog->last_updated );
     195                }
     196        }
     197
     198        // See if we need to do this again
     199        if ( is_multisite() && empty( $r['blog_ids'] ) ) {
     200                $sql['offset'] = $wpdb->prepare( " OFFSET %d", $r['limit'] + $r['offset'] );
     201
     202                // Check if there are more blogs to record
     203                $blog_ids = $wpdb->get_results( implode( ' ', $sql ) );
     204
     205                // We have more blogs; record offset and re-run function
     206                if ( ! empty( $blog_ids  ) ) {
     207                        bp_update_option( '_bp_record_blogs_offset', $r['limit'] + $r['offset'] );
     208                        bp_blogs_record_existing_blogs( array(
     209                                'offset'   => $r['limit'] + $r['offset'],
     210                                'limit'    => $r['limit'],
     211                                'blog_ids' => $r['blog_ids'],
     212                                'site_id'  => $r['site_id']
     213                        ) );
     214
     215                        // Bail since we have more blogs to record.
     216                        return;
     217
     218                // No more blogs; delete offset marker
     219                } else {
     220                        bp_delete_option( '_bp_record_blogs_offset' );
    147221                }
    148222        }
    149223
  • src/bp-core/admin/bp-core-admin-tools.php

     
    473473
    474474        return array( 0, __( 'Emails have been successfully reinstalled.', 'buddypress' ) );
    475475}
     476
     477/**
     478 * Add notice on the "Tools > BuddyPress" page if more sites need recording.
     479 *
     480 * This notice only shows up in the network admin dashboard.
     481 *
     482 * @since 2.6.0
     483 */
     484function bp_core_admin_notice_repopulate_blogs_resume() {
     485        $screen = get_current_screen();
     486        if ( 'tools_page_bp-tools-network' !== $screen->id ) {
     487                return;
     488        }
     489
     490        if ( '' === bp_get_option( '_bp_record_blogs_offset' ) ) {
     491                return;
     492        }
     493
     494        echo '<div class="error"><p>' . __( 'It looks like you have more sites to record. Resume recording by checking the "Repopulate site tracking records" option.', 'buddypress' ) . '</p></div>';
     495}
     496add_action( 'network_admin_notices', 'bp_core_admin_notice_repopulate_blogs_resume' );
  • tests/phpunit/testcases/blogs/functions.php

     
    984984                $this->comment_saved_count += 1;
    985985        }
    986986
     987        /**
     988         * @group bp_blogs_record_existing_blogs
     989         */
     990        public function test_bp_blogs_record_existing_blogs_limit() {
     991                if ( ! is_multisite() ) {
     992                        return;
     993                }
     994
     995                $old_user = get_current_user_id();
     996
     997                $u = $this->factory->user->create();
     998                $this->set_current_user( $u );
     999
     1000                // Create three sites.
     1001                $this->factory->blog->create_many( 3, array(
     1002                        'user_id' => $u
     1003                ) );
     1004
     1005                // Record each site one at a time
     1006                bp_blogs_record_existing_blogs( array(
     1007                        'limit' => 1
     1008                ) );
     1009
     1010                // Assert!
     1011                $blogs = bp_blogs_get_blogs( array(
     1012                        'user_id' => $u
     1013                ) );
     1014                $this->assertSame( 3, (int) $blogs['total'] );
     1015
     1016                $this->set_current_user( $old_user );
     1017        }
     1018
    9871019        protected function activity_exists_for_post( $post_id ) {
    9881020                $a = bp_activity_get( array(
    9891021                        'component' => buddypress()->blogs->id,