Ticket #6370: 6370.02.patch
File 6370.02.patch, 7.8 KB (added by , 10 years ago) |
---|
-
src/bp-blogs/bp-blogs-functions.php
71 71 /** 72 72 * Populate the BP blogs table with existing blogs. 73 73 * 74 * Warning: By default, this will remove all existing records from the BP 75 * blogs and blogmeta tables before re-populating the tables. 76 * 74 77 * @since 1.0.0 78 * @since 2.4.0 Accepts $args as a parameter. 75 79 * 76 80 * @global object $wpdb WordPress database object. 77 * @uses get_users() 78 * @uses bp_blogs_record_blog() 79 * 81 82 * @param array $args { 83 * Array of arguments. 84 * @type int $offset The offset to use. 85 * @type int $limit The number of blogs to record at one time. 86 * @type array $blog_ids Blog IDs to record. If empty, all blogs will be recorded. 87 * @type array $site_id The network site ID to use. 88 * } 80 89 * @return bool 81 90 */ 82 function bp_blogs_record_existing_blogs( ) {91 function bp_blogs_record_existing_blogs( $args = array() ) { 83 92 global $wpdb; 84 93 85 94 // 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 86 120 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'] ); 87 123 88 // Get blog ID's if not a large network 89 if ( ! wp_is_large_network() ) { 90 $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 } 91 128 92 // If error running this query, set blog ID's to false 93 if ( is_wp_error( $blog_ids ) ) { 94 $blog_ids = false; 95 } 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 } 96 134 97 // Large networks are not currently supported 98 } else { 99 $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'] ); 100 141 } 101 142 143 $blogs = $wpdb->get_results( implode( ' ', $sql ) ); 144 102 145 // Record a single site 103 146 } else { 104 $blog_ids = $wpdb->blogid; 105 } 106 107 // Bail if there are no blogs in the network 108 if ( empty( $blog_ids ) ) { 109 return false; 147 $blogs = array(); 148 $blogs[] = (object) array( 149 'blog_id' => $wpdb->blogid, 150 ); 110 151 } 111 152 112 // Get BuddyPress 113 $bp = buddypress(); 114 115 // Truncate user blogs table 116 $truncate = $wpdb->query( "TRUNCATE {$bp->blogs->table_name}" ); 117 if ( is_wp_error( $truncate ) ) { 118 return false; 119 } 153 // Bail if there are no blogs 154 if ( empty( $blogs ) ) { 155 // Make sure we remove our offset marker 156 if ( is_multisite() ) { 157 bp_delete_option( '_bp_record_blogs_offset' ); 158 } 120 159 121 // Truncate user blogsmeta table122 $truncate = $wpdb->query( "TRUNCATE {$bp->blogs->table_name_blogmeta}" );123 if ( is_wp_error( $truncate ) ) {124 160 return false; 125 161 } 126 162 127 163 // Loop through users of blogs and record the relationship 128 foreach ( (array) $blog_ids as $blog_id ) { 129 164 foreach ( (array) $blogs as $blog ) { 130 165 // Ensure that the cache is clear after the table TRUNCATE above 131 wp_cache_delete( $blog _id, 'blog_meta' );166 wp_cache_delete( $blog->blog_id, 'blog_meta' ); 132 167 133 168 // Get all users 134 169 $users = get_users( array( 135 'blog_id' => $blog _id170 'blog_id' => $blog->blog_id 136 171 ) ); 137 172 138 173 // Continue on if no users exist for this site (how did this happen?) … … 142 177 143 178 // Loop through users and record their relationship to this blog 144 179 foreach ( (array) $users as $user ) { 145 bp_blogs_add_user_to_blog( $user->ID, false, $blog_id ); 180 bp_blogs_add_user_to_blog( $user->ID, false, $blog->blog_id ); 181 182 // Clear cache 183 bp_blogs_clear_blog_object_cache( $blog->blog_id, $user->ID ); 184 } 185 186 // Update blog last activity timestamp 187 if ( ! empty( $blog->last_updated ) ) { 188 bp_blogs_update_blogmeta( $blog->blog_id, 'last_activity', $blog->last_updated ); 189 } 190 } 191 192 // See if we need to do this again 193 if ( is_multisite() && empty( $r['blog_ids'] ) ) { 194 $sql['offset'] = $wpdb->prepare( " OFFSET %d", $r['limit'] + $r['offset'] ); 195 196 // Check if there are more blogs to record 197 $blog_ids = $wpdb->get_results( implode( ' ', $sql ) ); 198 199 // We have more blogs; record offset and re-run function 200 if ( ! empty( $blog_ids ) ) { 201 bp_update_option( '_bp_record_blogs_offset', $r['limit'] + $r['offset'] ); 202 bp_blogs_record_existing_blogs( array( 203 'offset' => $r['limit'] + $r['offset'], 204 'limit' => $r['limit'], 205 'blog_ids' => $r['blog_ids'], 206 'site_id' => $r['site_id'] 207 ) ); 208 209 // Bail since we have more blogs to record. 210 return; 211 212 // No more blogs; delete offset marker 213 } else { 214 bp_delete_option( '_bp_record_blogs_offset' ); 146 215 } 147 216 } 148 217 -
src/bp-core/admin/bp-core-admin-tools.php
413 413 </div> 414 414 <?php 415 415 } 416 417 /** 418 * Add a notice on the "Tools > BuddyPress" page if there are more blogs to record. 419 * 420 * This notice only shows up in the network admin dashboard. 421 * 422 * @since 2.4.0 423 */ 424 function bp_core_admin_notice_repopulate_blogs_resume() { 425 if ( 'tools_page_bp-tools' !== $GLOBALS['hook_suffix'] ) { 426 return; 427 } 428 429 $blogs_offset = bp_get_option( '_bp_record_blogs_offset' ); 430 if ( '' === $blogs_offset ) { 431 return; 432 } 433 434 echo '<div class="error"><p>' . __( 'It looks like you have more blogs to record. Resume recording by checking the "Repopulate blogs records" option.', 'buddypress' ) . '</p></div>'; 435 } 436 add_action( 'network_admin_notices', 'bp_core_admin_notice_repopulate_blogs_resume' ); -
tests/phpunit/testcases/blogs/functions.php
711 711 remove_filter( 'bp_is_blog_public', '__return_zero' ); 712 712 } 713 713 714 /** 715 * @group bp_blogs_record_existing_blogs 716 */ 717 public function test_bp_blogs_record_existing_blogs_limit() { 718 if ( ! is_multisite() ) { 719 return; 720 } 721 722 $old_user = get_current_user_id(); 723 724 $u = $this->factory->user->create(); 725 $this->set_current_user( $u ); 726 727 // Create three sites. 728 $this->factory->blog->create_many( 3, array( 729 'user_id' => $u 730 ) ); 731 732 // Record each site one at a time 733 bp_blogs_record_existing_blogs( array( 734 'limit' => 1 735 ) ); 736 737 // Assert! 738 $blogs = bp_blogs_get_blogs( array( 739 'user_id' => $u 740 ) ); 741 $this->assertSame( 3, (int) $blogs['total'] ); 742 743 $this->set_current_user( $old_user ); 744 } 745 714 746 protected function activity_exists_for_post( $post_id ) { 715 747 $a = bp_activity_get( array( 716 748 'component' => buddypress()->blogs->id,