Ticket #6026: 6026.smaller-steps.01.patch
| File 6026.smaller-steps.01.patch, 78.8 KB (added by , 9 years ago) |
|---|
-
phpunit.xml.dist
diff --git phpunit.xml.dist phpunit.xml.dist index 376a6c8..ef890b8 100644
6 6 convertNoticesToExceptions="true" 7 7 convertWarningsToExceptions="true" 8 8 > 9 <groups> 10 <exclude> 11 <group>multisite</group> 12 </exclude> 13 </groups> 9 14 <testsuites> 10 15 <testsuite> 11 16 <directory suffix=".php">tests/phpunit/testcases/</directory> -
src/bp-blogs/bp-blogs-classes.php
diff --git src/bp-blogs/bp-blogs-classes.php src/bp-blogs/bp-blogs-classes.php index 49c55dc..30ffee4 100644
11 11 defined( 'ABSPATH' ) || exit; 12 12 13 13 require dirname( __FILE__ ) . '/classes/class-bp-blogs-blog.php'; 14 15 if ( function_exists( 'get_sites' ) ) { 16 require dirname( __FILE__ ) . '/classes/class-bp-site-query.php'; 17 } -
src/bp-blogs/bp-blogs-functions.php
diff --git src/bp-blogs/bp-blogs-functions.php src/bp-blogs/bp-blogs-functions.php index 6fe24a6..1aee053 100644
function bp_blogs_is_blog_trackable( $blog_id, $user_id = 0 ) { 303 303 */ 304 304 function bp_blogs_record_blog( $blog_id, $user_id, $no_activity = false ) { 305 305 306 if ( empty( $user_id ) ) 306 if ( empty( $user_id ) ) { 307 307 $user_id = bp_loggedin_user_id(); 308 } 308 309 309 310 // If blog is not recordable, do not record the activity. 310 if ( ! bp_blogs_is_blog_recordable( $blog_id, $user_id ) )311 if ( ! bp_blogs_is_blog_recordable( $blog_id, $user_id ) ) { 311 312 return false; 313 } 312 314 313 315 $name = get_blog_option( $blog_id, 'blogname' ); 314 316 $url = get_home_url( $blog_id ); … … function bp_blogs_record_blog( $blog_id, $user_id, $no_activity = false ) { 329 331 $thread_depth = 1; 330 332 } 331 333 332 $recorded_blog = new BP_Blogs_Blog ;334 $recorded_blog = new BP_Blogs_Blog( null, $user_id, $blog_id ); 333 335 $recorded_blog->user_id = $user_id; 334 336 $recorded_blog->blog_id = $blog_id; 335 $recorded_blog_id = $recorded_blog->save(); 336 $is_recorded = !empty( $recorded_blog_id ) ? true : false; 337 338 // The user is a contributor. 339 if ( bp_user_can( $user_id, 'edit_posts', array( 'site_id' => $blog_id ) ) ) { 340 $recorded_blog->is_contributor = 1; 341 342 // The user is a subscriber. 343 } else { 344 $recorded_blog->is_contributor = 0; 345 } 346 347 $recorded_blog_id = $recorded_blog->save(); 348 $is_recorded = false; 349 350 if ( ! empty( $recorded_blog_id ) ) { 351 $is_recorded = true; 352 } 337 353 338 354 bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'url', $url ); 339 355 bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'name', $name ); … … function bp_blogs_record_blog( $blog_id, $user_id, $no_activity = false ) { 343 359 bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'close_comments_days_old', $close_days_old ); 344 360 bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'thread_comments_depth', $thread_depth ); 345 361 346 $is_private = !empty( $_POST['blog_public'] ) && (int) $_POST['blog_public'] ? false : true; 347 $is_private = !apply_filters( 'bp_is_new_blog_public', !$is_private ); 362 // Get site admins 363 $site_admins = bp_blogs_get_blogmeta( $recorded_blog->blog_id, 'site_admins' ); 364 if ( empty( $site_admins ) && ! is_array( $site_admins ) ) { 365 $site_admins = array(); 366 } 367 368 // Eventually Add the user to site admins. 369 if ( bp_user_can( $user_id, 'manage_options', array( 'site_id' => $recorded_blog->blog_id ) ) ) { 370 if ( empty( $site_admins ) ) { 371 bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'site_admins', array( (int) $user_id ) ); 372 } else { 373 $site_admins[] = (int) $user_id; 374 bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'site_admins', wp_parse_id_list( $site_admins ) ); 375 } 376 377 // If the user was an admin and no more is, remove it from site admins. 378 } else { 379 $was_admin = array_search( $user_id, $site_admins ); 380 381 if ( false !== $was_admin ) { 382 unset( $site_admins[ $was_admin ] ); 383 bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'site_admins', $site_admins ); 384 } 385 } 386 387 $is_private = true; 388 if ( ! empty( $_POST['blog_public'] ) && (int) $_POST['blog_public'] ) { 389 $is_private = false; 390 } 391 392 $is_private = ! apply_filters( 'bp_is_new_blog_public', ! $is_private ); 348 393 349 394 /** 350 395 * Fires after BuddyPress has been made aware of a new site for activity tracking. … … function bp_blogs_add_user_to_blog( $user_id, $role = false, $blog_id = 0 ) { 800 845 } 801 846 } 802 847 803 // Bail if no role was found or role is not in the allowed roles array. 804 if ( empty( $role ) || ! in_array( $role, bp_blogs_get_allowed_roles() ) ) { 848 // Bail if no role was found. 849 if ( empty( $role ) ) { 850 return false; 851 } 852 853 // Bail if role is not in the allowed roles array. 854 if ( ! in_array( $role, bp_blogs_get_allowed_roles() ) ) { 855 $user_blog_association = new BP_Blogs_Blog( null, $user_id, $blog_id ); 856 857 // If the user is associated to the blog remove him. 858 if ( ! empty( $user_blog_association->id ) ) { 859 $user_blog_association::delete_blog_for_user( $blog_id, $user_id ); 860 } 861 805 862 return false; 806 863 } 807 864 808 865 // Record the blog activity for this user being added to this blog. 809 866 bp_blogs_record_blog( $blog_id, $user_id, true ); 810 867 } 811 add_action( 'add_user_to_blog', 'bp_blogs_add_user_to_blog', 10, 3 ); 812 add_action( 'profile_update', 'bp_blogs_add_user_to_blog' ); 813 add_action( 'user_register', 'bp_blogs_add_user_to_blog' ); 868 add_action( 'set_user_role', 'bp_blogs_add_user_to_blog', 10, 2 ); 814 869 815 870 /** 816 871 * The allowed blog roles a member must have to be recorded into the … … add_action( 'user_register', 'bp_blogs_add_user_to_blog' ); 820 875 * to extend the functionality of the Blogs component. 821 876 * 822 877 * @since 2.1.0 878 * @since 2.7.0 Add the subscriber role to the list of allowed roles if front-end subscriptions are enabled. 823 879 * 824 * @return string880 * @return array The list of allowed blog roles. 825 881 */ 826 882 function bp_blogs_get_allowed_roles() { 827 return apply_filters( 'bp_blogs_get_allowed_roles', array( 'contributor', 'author', 'editor', 'administrator' ) ); 883 /** 884 * NB: All roles except the subcriber one will be saved into the User to blog association as contributors. 885 * (the is_contributor field of the buddypress()->blogs->table_name_blogmeta will be set to 1) 886 */ 887 $allowed_roles = array( 'contributor', 'author', 'editor', 'administrator' ); 888 889 // If front-end subscriptions are not disabled, add the subsciber role. 890 if ( ! bp_disable_site_subscriptions() ) { 891 array_unshift( $allowed_roles, 'subscriber' ); 892 } 893 894 return apply_filters( 'bp_blogs_get_allowed_roles', $allowed_roles ); 828 895 } 829 896 830 897 /** -
src/bp-blogs/bp-blogs-site.php
diff --git src/bp-blogs/bp-blogs-site.php src/bp-blogs/bp-blogs-site.php index e69de29..1a8e445 100644
1 <?php 2 /** 3 * BuddyPress Blogs Site. 4 * 5 * @package BuddyPress 6 * @subpackage BlogsSite 7 * @since 2.7.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 require dirname( __FILE__ ) . '/site/functions.php'; 14 require dirname( __FILE__ ) . '/site/screens.php'; 15 require dirname( __FILE__ ) . '/site/template.php'; 16 require dirname( __FILE__ ) . '/site/adminbar.php'; 17 18 if ( bp_is_active( 'activity' ) ) { 19 require dirname( __FILE__ ) . '/site/activity.php'; 20 } 21 22 if ( bp_is_active( 'notifications' ) ) { 23 require dirname( __FILE__ ) . '/site/notifications.php'; 24 } 25 26 if ( ! bp_disable_site_subscriptions() ) { 27 require dirname( __FILE__ ) . '/site/subscriptions.php'; 28 } -
src/bp-blogs/classes/class-bp-blogs-blog.php
diff --git src/bp-blogs/classes/class-bp-blogs-blog.php src/bp-blogs/classes/class-bp-blogs-blog.php index 6e36295..c84adb9 100644
class BP_Blogs_Blog { 44 44 /** 45 45 * Constructor method. 46 46 * 47 * @param int|null $id Optional. The ID of the blog. 47 * @since 2.7.0 Add the $blog_id and $user_id parameter. 48 * 49 * @param int|null $id Optional. The ID of the user blog association. 50 * @param int $blog_id Optional. The ID of the blog. 51 * @param int $user_id Optional. The ID of the user. 48 52 */ 49 public function __construct( $id = null ) { 50 if ( !empty( $id ) ) { 51 $this->id = $id; 53 public function __construct( $id = null, $user_id = 0, $blog_id = 0 ) { 54 $this->id = $id; 55 $this->user_id = $user_id; 56 $this->blog_id = $blog_id; 57 58 if ( $this->id || ( $this->user_id && $this->blog_id ) ) { 52 59 $this->populate(); 53 60 } 54 61 } 55 62 56 63 /** 57 64 * Populate the object with data about the specific activity item. 65 * 66 * @since 2.7.0 Add the is_contributor field. 58 67 */ 59 68 public function populate() { 60 69 global $wpdb; 61 70 62 71 $bp = buddypress(); 63 72 64 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->blogs->table_name} WHERE id = %d", $this->id ) ); 73 if ( ! empty( $this->id ) ) { 74 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->blogs->table_name} WHERE id = %d", $this->id ) ); 75 } else { 76 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->blogs->table_name} WHERE user_id = %d AND blog_id = %d", $this->user_id, $this->blog_id ) ); 77 } 78 79 if ( empty( $blog ) ) { 80 $this->id = 0; 81 return; 82 } 65 83 66 $this->user_id = $blog->user_id; 67 $this->blog_id = $blog->blog_id; 84 $this->id = $blog->id; 85 $this->user_id = $blog->user_id; 86 $this->blog_id = $blog->blog_id; 87 $this->is_contributor = $blog->is_contributor; 68 88 } 69 89 70 90 /** 71 91 * Save the BP blog data to the database. 72 92 * 93 * @since 2.7.0 Add the is_contributor field. 94 * 73 95 * @return bool True on success, false on failure. 74 96 */ 75 97 public function save() { 76 98 global $wpdb; 77 99 78 $this->user_id = apply_filters( 'bp_blogs_blog_user_id_before_save', $this->user_id, $this->id ); 79 $this->blog_id = apply_filters( 'bp_blogs_blog_id_before_save', $this->blog_id, $this->id ); 100 $this->user_id = apply_filters( 'bp_blogs_blog_user_id_before_save', $this->user_id, $this->id ); 101 $this->blog_id = apply_filters( 'bp_blogs_blog_id_before_save', $this->blog_id, $this->id ); 102 $this->is_contributor = apply_filters( 'bp_blogs_is_contributor_before_save', $this->is_contributor, $this->id ); 80 103 81 104 /** 82 105 * Fires before the current blog item gets saved. … … class BP_Blogs_Blog { 90 113 do_action_ref_array( 'bp_blogs_blog_before_save', array( &$this ) ); 91 114 92 115 // Don't try and save if there is no user ID or blog ID set. 93 if ( ! $this->user_id || !$this->blog_id )116 if ( ! $this->user_id || ! $this->blog_id ) { 94 117 return false; 118 } 95 119 96 120 // Don't save if this blog has already been recorded for the user. 97 if ( ! $this->id && $this->exists() )121 if ( ! $this->id && $this->exists() ) { 98 122 return false; 123 } 99 124 100 125 $bp = buddypress(); 101 126 102 127 if ( $this->id ) { 103 128 // Update. 104 $sql = $wpdb->prepare( "UPDATE {$bp->blogs->table_name} SET user_id = %d, blog_id = %d WHERE id = %d", $this->user_id, $this->blog_id, $this->id );129 $sql = $wpdb->prepare( "UPDATE {$bp->blogs->table_name} SET user_id = %d, blog_id = %d, is_contributor = %d WHERE id = %d", $this->user_id, $this->blog_id, $this->is_contributor, $this->id ); 105 130 } else { 106 131 // Save. 107 $sql = $wpdb->prepare( "INSERT INTO {$bp->blogs->table_name} ( user_id, blog_id ) VALUES ( %d, %d )", $this->user_id, $this->blog_id);132 $sql = $wpdb->prepare( "INSERT INTO {$bp->blogs->table_name} ( user_id, blog_id, is_contributor ) VALUES ( %d, %d, %d )", $this->user_id, $this->blog_id, $this->is_contributor ); 108 133 } 109 134 110 if ( ! $wpdb->query($sql) )135 if ( ! $wpdb->query( $sql ) ) { 111 136 return false; 137 } 112 138 113 139 /** 114 140 * Fires after the current blog item gets saved. … … class BP_Blogs_Blog { 121 147 */ 122 148 do_action_ref_array( 'bp_blogs_blog_after_save', array( &$this ) ); 123 149 124 if ( $this->id ) 150 if ( $this->id ) { 125 151 return $this->id; 126 else152 } else { 127 153 return $wpdb->insert_id; 154 } 128 155 } 129 156 130 157 /** … … class BP_Blogs_Blog { 146 173 /** 147 174 * Retrieve a set of blog-user associations. 148 175 * 176 * @since 2.7.0 Use the BP_Site_Query class if WordPress is >= 4.6 177 * 149 178 * @param string $type The order in which results should be returned. 150 179 * 'active', 'alphabetical', 'newest', or 'random'. 151 180 * @param int|bool $limit Optional. The maximum records to return. … … class BP_Blogs_Blog { 164 193 * 'total' - A count of the total blogs matching the filter params 165 194 */ 166 195 public static function get( $type, $limit = false, $page = false, $user_id = 0, $search_terms = false, $update_meta_cache = true, $include_blog_ids = false ) { 167 global $wpdb; 168 169 $bp = buddypress(); 170 171 if ( !is_user_logged_in() || ( !bp_current_user_can( 'bp_moderate' ) && ( $user_id != bp_loggedin_user_id() ) ) ) 172 $hidden_sql = "AND wb.public = 1"; 173 else 174 $hidden_sql = ''; 196 // Default query vars 197 $query_vars = array( 198 'public' => 1, 199 'offset' => 0, 200 'number' => 20, 201 'update_blogmeta_cache' => $update_meta_cache, 202 ); 203 204 if ( bp_current_user_can( 'bp_moderate' ) || ( ! empty( $user_id ) && (int) $user_id === (int) bp_loggedin_user_id() ) ) { 205 unset( $query_vars['public'] ); 206 } 175 207 176 $pag_sql = ( $limit && $page ) ? $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ) : ''; 208 if ( $limit && $page ) { 209 $query_vars = array_merge( $query_vars, array( 210 'offset' => (int) ( ( $page - 1 ) * $limit ), 211 'number' => (int) $limit, 212 ) ); 213 } 177 214 178 $user_sql = !empty( $user_id ) ? $wpdb->prepare( " AND b.user_id = %d", $user_id ) : ''; 215 if ( ! empty( $user_id ) ) { 216 $query_vars['user_id'] = (int) $user_id; 217 } 179 218 180 219 switch ( $type ) { 181 220 case 'active': default: 182 $order_sql = "ORDER BY bm.meta_value DESC"; 221 $query_vars = array_merge( $query_vars, array( 222 'orderby' => 'last_activity', 223 'order' => 'DESC', 224 ) ); 183 225 break; 184 226 case 'alphabetical': 185 $order_sql = "ORDER BY bm_name.meta_value ASC"; 227 $query_vars = array_merge( $query_vars, array( 228 'orderby' => 'name', 229 'order' => 'ASC', 230 ) ); 186 231 break; 187 232 case 'newest': 188 $order_sql = "ORDER BY wb.registered DESC"; 233 $query_vars = array_merge( $query_vars, array( 234 'orderby' => 'registered', 235 'order' => 'DESC', 236 ) ); 189 237 break; 190 238 case 'random': 191 $order_sql = "ORDER BY RAND()"; 239 $query_vars = array_merge( $query_vars, array( 240 'orderby' => 'RAND()', 241 'order' => '', 242 ) ); 192 243 break; 193 244 } 194 245 195 $include_sql = '';196 246 $include_blog_ids = array_filter( wp_parse_id_list( $include_blog_ids ) ); 197 247 if ( ! empty( $include_blog_ids ) ) { 198 $blog_ids_sql = implode( ',', $include_blog_ids ); 199 $include_sql = " AND b.blog_id IN ({$blog_ids_sql})"; 248 $query_vars['site__in'] = $include_blog_ids; 200 249 } 201 250 202 251 if ( ! empty( $search_terms ) ) { 203 $search_terms_like = '%' . bp_esc_like( $search_terms ) . '%'; 204 $search_terms_sql = $wpdb->prepare( 'AND (bm_name.meta_value LIKE %s OR bm_description.meta_value LIKE %s)', $search_terms_like, $search_terms_like ); 252 $query_vars['search_terms'] = bp_esc_like( $search_terms ); 253 } 254 255 // Init empty results 256 $results = array( 'blogs' => array(), 'total' => 0 ); 257 258 /** 259 * If the WordPress version is at least 4.6, we will use BP_Site_Query 260 * unless you filter 'bp_blogs_use_bp_site_query' by returning false. 261 * 262 * @since 2.7.0 263 * 264 * @param bool $value True to use BP_Site_Query. False otherwise. 265 */ 266 if ( true === (bool) apply_filters( 'bp_blogs_use_bp_site_query', true ) && function_exists( 'get_sites' ) ) { 267 $sites = new BP_Site_Query( $query_vars ); 268 $results = $sites->get_results(); 269 } else { 270 $results = self::legacy_get( $query_vars, $type ); 271 } 272 273 return $results; 274 } 275 276 /** 277 * Get blogs using the legacy way. 278 * 279 * @since 2.7.0 280 * 281 * @param array $query_vars An array containing the query vars. 282 * @param string $type The sort order type. 283 * @return array Multidimensional results array, structured as follows: 284 * 'blogs' - Array of located blog objects 285 * 'total' - A count of the total blogs matching the filter params 286 */ 287 public static function legacy_get( $query_vars = array(), $type = 'active' ) { 288 global $wpdb; 289 290 $bp = buddypress(); 291 292 $sql = array( 293 'select' => 'SELECT b.blog_id, b.user_id as admin_user_id, u.user_email as admin_user_email, wb.domain, wb.path, bm.meta_value as last_activity, bm_name.meta_value as name', 294 'from' => "FROM {$bp->blogs->table_name} b", 295 'join' => array( 296 'bm' => "{$bp->blogs->table_name_blogmeta} bm ON (b.blog_id = bm.blog_id)", 297 'bm_name' => "{$bp->blogs->table_name_blogmeta} bm_name ON (b.blog_id = bm_name.blog_id)", 298 'bm_description' => "{$bp->blogs->table_name_blogmeta} bm_description ON (b.blog_id = bm_description.blog_id)", 299 'wb' => "{$wpdb->blogs} wb ON (b.blog_id = wb.blog_id)", 300 'u' => "{$wpdb->users} u ON (b.user_id = u.ID)", 301 ), 302 'where' => array( 303 'common' => "wb.archived = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 AND bm_name.meta_key = 'name' AND bm_description.meta_key = 'description'", 304 'active' => "bm.meta_key = 'last_activity'", 305 ), 306 'groupby' => 'GROUP BY b.blog_id', 307 'orderby' => array( 308 'last_activity' => 'ORDER BY bm.meta_value DESC', 309 'name' => 'ORDER BY bm_name.meta_value ASC', 310 'registered' => 'ORDER BY wb.registered DESC', 311 'RAND()' => 'ORDER BY RAND()', 312 ), 313 'limit' => $wpdb->prepare( "LIMIT %d, %d", $query_vars['offset'], $query_vars['number'] ), 314 ); 315 316 // Hide private blogs if needed 317 if ( ! empty( $query_vars['public'] ) ) { 318 $sql['where']['hidden'] = 'wb.public = 1'; 319 } 320 321 // Limit results to blogs the user_id is a member of. 322 if ( ! empty( $query_vars['user_id'] ) ) { 323 $sql['where']['user_id'] = $wpdb->prepare( 'b.user_id = %d', $query_vars['user_id'] ); 324 325 // Defaults to blogs user is a least a contributor of 326 if ( ! isset( $query_vars['only_contributors'] ) ) { 327 $sql['where']['is_contributor'] = 'b.is_contributor = 1'; 328 } else { 329 $sql['where']['is_contributor'] = $wpdb->prepare( 'b.is_contributor = %d', $query_vars['only_contributors'] ); 330 } 331 } 332 333 // Limit results to included sites. 334 if ( ! empty( $query_vars['site__in'] ) ) { 335 $sql['where']['include'] = $wpdb->prepare( 'b.blog_id IN (%s)', implode( ',', $query_vars['site__in'] ) ); 336 } 337 338 // Limit resultes to sites matching search terms. 339 if ( ! empty( $query_vars['search_terms'] ) ) { 340 $search_terms_like = '%' . $query_vars['search_terms'] . '%'; 341 $sql['where']['search'] = $wpdb->prepare( '( bm_name.meta_value LIKE %s OR bm_description.meta_value LIKE %s )', $search_terms_like, $search_terms_like ); 342 } 343 344 // Eventually handle single site query. 345 $single_site = ! empty( $query_vars['single_site'] ); 346 347 if ( $single_site ) { 348 if ( ! empty( $query_vars['ID'] ) ) { 349 $sql['where']['single_site'] = $wpdb->prepare( 'wb.blog_id = %d', $query_vars['ID'] ); 350 } elseif ( ! empty( $query_vars['domain__in'] ) ) { 351 $sql['where']['single_site'] = $wpdb->prepare( 'wb.domain = %s', reset( $query_vars['domain__in'] ) ); 352 } else { 353 $sql['where']['single_site'] = $wpdb->prepare( 'wb.path = %s', reset( $query_vars['path__in'] ) ); 354 } 355 } 356 357 // Set a copy for the total query 358 $total_sql = $sql; 359 360 // Set joins 361 $sql['join'] = 'LEFT JOIN ' . join( ' LEFT JOIN ', $sql['join'] ); 362 363 // Set where clauses 364 $sql['where'] = 'WHERE ' . join( ' AND ', $sql['where'] ); 365 366 // Set order 367 if ( ! empty( $query_vars['orderby'] ) ) { 368 $sql_orderby = array_intersect_key( $sql['orderby'], array( $query_vars['orderby'] => true ) ); 369 $sql['orderby'] = reset( $sql_orderby ); 205 370 } else { 206 $search_terms_sql = ''; 207 } 208 209 $paged_blogs = $wpdb->get_results( " 210 SELECT b.blog_id, b.user_id as admin_user_id, u.user_email as admin_user_email, wb.domain, wb.path, bm.meta_value as last_activity, bm_name.meta_value as name 211 FROM 212 {$bp->blogs->table_name} b 213 LEFT JOIN {$bp->blogs->table_name_blogmeta} bm ON (b.blog_id = bm.blog_id) 214 LEFT JOIN {$bp->blogs->table_name_blogmeta} bm_name ON (b.blog_id = bm_name.blog_id) 215 LEFT JOIN {$bp->blogs->table_name_blogmeta} bm_description ON (b.blog_id = bm_description.blog_id) 216 LEFT JOIN {$wpdb->base_prefix}blogs wb ON (b.blog_id = wb.blog_id) 217 LEFT JOIN {$wpdb->users} u ON (b.user_id = u.ID) 218 WHERE 219 wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} 220 AND bm.meta_key = 'last_activity' AND bm_name.meta_key = 'name' AND bm_description.meta_key = 'description' 221 {$search_terms_sql} {$user_sql} {$include_sql} 222 GROUP BY b.blog_id {$order_sql} {$pag_sql} 223 " ); 224 225 $total_blogs = $wpdb->get_var( " 226 SELECT COUNT(DISTINCT b.blog_id) 227 FROM 228 {$bp->blogs->table_name} b 229 LEFT JOIN {$wpdb->base_prefix}blogs wb ON (b.blog_id = wb.blog_id) 230 LEFT JOIN {$bp->blogs->table_name_blogmeta} bm_name ON (b.blog_id = bm_name.blog_id) 231 LEFT JOIN {$bp->blogs->table_name_blogmeta} bm_description ON (b.blog_id = bm_description.blog_id) 232 WHERE 233 wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} 234 AND 235 bm_name.meta_key = 'name' AND bm_description.meta_key = 'description' 236 {$search_terms_sql} {$user_sql} {$include_sql} 237 " ); 238 239 $blog_ids = array(); 240 foreach ( (array) $paged_blogs as $blog ) { 241 $blog_ids[] = (int) $blog->blog_id; 242 } 243 244 $paged_blogs = BP_Blogs_Blog::get_blog_extras( $paged_blogs, $blog_ids, $type ); 245 246 if ( $update_meta_cache ) { 371 unset( $sql['orderby'] ); 372 } 373 374 $return = array( 375 'blogs' => $wpdb->get_results( join( ' ', $sql ) ), 376 ); 377 378 // Set the total sql parts 379 if ( ! $single_site ) { 380 $total_sql['select'] = 'SELECT COUNT(DISTINCT b.blog_id)'; 381 unset( $total_sql['join']['bm'], $total_sql['join']['u'], $total_sql['where']['active'], $total_sql['groupby'], $total_sql['orderby'], $total_sql['limit'] ); 382 $total_sql['join'] = 'LEFT JOIN ' . join( ' LEFT JOIN ', $total_sql['join'] ); 383 $total_sql['where'] = 'WHERE ' . join( ' AND ', $total_sql['where'] ); 384 385 $return['total'] = $wpdb->get_var( join( ' ', $total_sql ) ); 386 } 387 388 $blog_ids = array_map( 'intval', wp_list_pluck( $return[ 'blogs' ], 'blog_id' ) ); 389 $return[ 'blogs' ] = BP_Blogs_Blog::get_blog_extras( $return[ 'blogs' ], $blog_ids, $type, ! $single_site ); 390 391 if ( ! empty( $query_vars['update_blogmeta_cache'] ) ) { 247 392 bp_blogs_update_meta_cache( $blog_ids ); 248 393 } 249 394 250 return array( 'blogs' => $paged_blogs, 'total' => $total_blogs );395 return $return; 251 396 } 252 397 253 398 /** … … class BP_Blogs_Blog { 277 422 public static function delete_blog_for_user( $blog_id, $user_id = null ) { 278 423 global $wpdb; 279 424 280 if ( ! $user_id )425 if ( ! $user_id ) { 281 426 $user_id = bp_loggedin_user_id(); 427 } 282 428 283 429 $bp = buddypress(); 284 430 … … class BP_Blogs_Blog { 311 457 * blogs that have been recorded by BuddyPress, while the WP function 312 458 * does a true query of a user's blog capabilities. 313 459 * 314 * @param int $user_id Optional. ID of the user whose blogs are being 315 * queried. Defaults to logged-in user. 316 * @param bool $show_hidden Optional. Whether to include blogs that are not marked 317 * public. Defaults to true when viewing one's own profile. 460 * @since 2.7.0 Add the is_contributor parameter. 461 * 462 * @param int $user_id Optional. ID of the user whose blogs are being 463 * queried. Defaults to logged-in user. 464 * @param bool $show_hidden Optional. Whether to include blogs that are not marked 465 * public. Defaults to true when viewing one's own profile. 466 * @param int|null $is_contributor Optional. Type of blogs to get. Defaults to 1, 467 * blogs the user is a contributor of. 318 468 * @return array Multidimensional results array, structured as follows: 319 469 * 'blogs' - Array of located blog objects. 320 470 * 'total' - A count of the total blogs for the user. 321 471 */ 322 public static function get_blogs_for_user( $user_id = 0, $show_hidden = false ) {472 public static function get_blogs_for_user( $user_id = 0, $show_hidden = false, $is_contributor = 1 ) { 323 473 global $wpdb; 324 474 325 475 $bp = buddypress(); 326 476 327 if ( ! $user_id )477 if ( ! $user_id ) { 328 478 $user_id = bp_displayed_user_id(); 479 } 480 481 $is_contributor_sql = ''; 482 if ( ! is_null( $is_contributor ) ) { 483 $is_contributor_sql = sprintf( 'AND b.is_contributor = %d', $is_contributor ); 484 } 329 485 330 486 // Show logged in users their hidden blogs. 331 if ( !bp_is_my_profile() && !$show_hidden ) 332 $blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.blog_id, b.id, bm1.meta_value as name, wb.domain, wb.path FROM {$bp->blogs->table_name} b, {$wpdb->base_prefix}blogs wb, {$bp->blogs->table_name_blogmeta} bm1 WHERE b.blog_id = wb.blog_id AND b.blog_id = bm1.blog_id AND bm1.meta_key = 'name' AND wb.public = 1 AND wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND b.user_id = %d ORDER BY b.blog_id", $user_id ) ); 333 else 334 $blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.blog_id, b.id, bm1.meta_value as name, wb.domain, wb.path FROM {$bp->blogs->table_name} b, {$wpdb->base_prefix}blogs wb, {$bp->blogs->table_name_blogmeta} bm1 WHERE b.blog_id = wb.blog_id AND b.blog_id = bm1.blog_id AND bm1.meta_key = 'name' AND wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND b.user_id = %d ORDER BY b.blog_id", $user_id ) ); 487 if ( ! bp_is_my_profile() && ! $show_hidden ) { 488 $blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.blog_id, b.id, bm1.meta_value as name, wb.domain, wb.path FROM {$bp->blogs->table_name} b, {$wpdb->base_prefix}blogs wb, {$bp->blogs->table_name_blogmeta} bm1 WHERE b.blog_id = wb.blog_id AND b.blog_id = bm1.blog_id AND bm1.meta_key = 'name' AND wb.public = 1 AND wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND b.user_id = %d {$is_contributor_sql} ORDER BY b.blog_id", $user_id ) ); 489 } else { 490 $blogs = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT b.blog_id, b.id, bm1.meta_value as name, wb.domain, wb.path FROM {$bp->blogs->table_name} b, {$wpdb->base_prefix}blogs wb, {$bp->blogs->table_name_blogmeta} bm1 WHERE b.blog_id = wb.blog_id AND b.blog_id = bm1.blog_id AND bm1.meta_key = 'name' AND wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND b.user_id = %d {$is_contributor_sql} ORDER BY b.blog_id", $user_id ) ); 491 } 335 492 336 493 $total_blog_count = BP_Blogs_Blog::total_blog_count_for_user( $user_id ); 337 494 … … class BP_Blogs_Blog { 352 509 * 353 510 * This method always includes hidden blogs. 354 511 * 355 * @param int $user_id Optional. ID of the user whose blogs are being 356 * queried. Defaults to logged-in user. 512 * @since 2.7.0 Add the is_contributor parameter. 513 * 514 * @param int $user_id Optional. ID of the user whose blogs are being 515 * queried. Defaults to logged-in user. 516 * @param int|null $is_contributor Optional. Type of blogs to get. Defaults to 1, 517 * blogs the user is a contributor of. 357 518 * @return int The number of blogs associated with the user. 358 519 */ 359 public static function get_blog_ids_for_user( $user_id = 0 ) {520 public static function get_blog_ids_for_user( $user_id = 0, $is_contributor = 1 ) { 360 521 global $wpdb; 361 522 362 523 $bp = buddypress(); 363 524 364 if ( ! $user_id )525 if ( ! $user_id ) { 365 526 $user_id = bp_displayed_user_id(); 527 } 528 529 $where = array( 530 'user_id' => $wpdb->prepare( 'user_id = %d', $user_id ), 531 ); 532 533 if ( ! is_null( $is_contributor ) ) { 534 $where['is_contributor'] = $wpdb->prepare( 'is_contributor = %d', $is_contributor ); 535 } 366 536 367 return $wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM {$bp->blogs->table_name} WHERE user_id = %d", $user_id ) ); 537 $where = join( ' AND ', $where ); 538 539 return $wpdb->get_col( "SELECT blog_id FROM {$bp->blogs->table_name} WHERE {$where}" ); 368 540 } 369 541 370 542 /** … … class BP_Blogs_Blog { 389 561 * $user_id parameter, or when the logged-in user has the bp_moderate 390 562 * cap. 391 563 * 392 * @param int|null $user_id Optional. ID of the user whose blogs are being 393 * queried. Defaults to logged-in user. 564 * @since 2.7.0 Add the is_contributor parameter. 565 * 566 * @param int|null $user_id Optional. ID of the user whose blogs are being 567 * queried. Defaults to logged-in user. 568 * @param int|null $is_contributor Optional. Type of blogs to get. Defaults to 1, 569 * blogs the user is a contributor of. 394 570 * @return int Blog count for the user. 395 571 */ 396 public static function total_blog_count_for_user( $user_id = null ) {572 public static function total_blog_count_for_user( $user_id = null, $is_contributor = 1 ) { 397 573 global $wpdb; 398 574 399 575 $bp = buddypress(); 400 576 401 if ( ! $user_id )577 if ( ! $user_id ) { 402 578 $user_id = bp_displayed_user_id(); 579 } 580 581 $is_contributor_sql = ''; 582 if ( ! is_null( $is_contributor ) ) { 583 $is_contributor_sql = sprintf( 'AND b.is_contributor = %d', $is_contributor ); 584 } 403 585 404 586 // If the user is logged in return the blog count including their hidden blogs. 405 587 if ( ( is_user_logged_in() && $user_id == bp_loggedin_user_id() ) || bp_current_user_can( 'bp_moderate' ) ) { 406 return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND user_id = %d ", $user_id ) );588 return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND user_id = %d {$is_contributor_sql}", $user_id ) ); 407 589 } else { 408 return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.public = 1 AND wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND user_id = %d ", $user_id ) );590 return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.public = 1 AND wb.deleted = 0 AND wb.spam = 0 AND wb.mature = 0 AND wb.archived = '0' AND user_id = %d {$is_contributor_sql}", $user_id ) ); 409 591 } 410 592 } 411 593 … … class BP_Blogs_Blog { 453 635 * Query will include hidden blogs if the logged-in user has the 454 636 * 'bp_moderate' cap. 455 637 * 456 * @param int|null $limit Optional. The maximum number of items to return. 457 * Default: null (no limit). 458 * @param int|null $page Optional. The page of results to return. Default: 459 * null (no limit). 638 * @param int|null $limit Optional. The maximum number of items to return. 639 * Default: null (no limit). 640 * @param int|null $page Optional. The page of results to return. Default: 641 * null (no limit). 642 * @param int|null $is_contributor Optional. The type of blogs to return. Default: 643 * 1 (blogs the user is a contributor of). 460 644 * @return array Multidimensional results array, structured as follows: 461 645 * 'blogs' - Array of located blog objects. 462 646 * 'total' - A count of the total blogs. 463 647 */ 464 public static function get_all( $limit = null, $page = null ) {648 public static function get_all( $limit = null, $page = null, $is_contributor = 1 ) { 465 649 global $wpdb; 466 650 467 651 $bp = buddypress(); 468 652 469 $hidden_sql = !bp_current_user_can( 'bp_moderate' ) ? "AND wb.public = 1" : ''; 653 $is_contributor_sql = ''; 654 if ( ! is_null( $is_contributor ) ) { 655 $is_contributor_sql = sprintf( 'AND b.is_contributor = %d', $is_contributor ); 656 } 657 658 $hidden_sql = "AND wb.public = 1"; 659 if ( bp_current_user_can( 'bp_moderate' ) ) { 660 $hidden_sql = ''; 661 } 662 470 663 $pag_sql = ( $limit && $page ) ? $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ) : ''; 471 664 472 $paged_blogs = $wpdb->get_results( "SELECT DISTINCT b.blog_id FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 {$hidden_sql} {$ pag_sql}" );473 $total_blogs = $wpdb->get_var( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 {$hidden_sql} " );665 $paged_blogs = $wpdb->get_results( "SELECT DISTINCT b.blog_id FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 {$hidden_sql} {$is_contributor_sql} {$pag_sql}" ); 666 $total_blogs = $wpdb->get_var( "SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b LEFT JOIN {$wpdb->base_prefix}blogs wb ON b.blog_id = wb.blog_id WHERE wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 {$hidden_sql} {$is_contributor_sql}" ); 474 667 475 668 return array( 'blogs' => $paged_blogs, 'total' => $total_blogs ); 476 669 } … … class BP_Blogs_Blog { 495 688 496 689 $bp = buddypress(); 497 690 498 $letter_like = '%' .bp_esc_like( $letter ) . '%';691 $letter_like = bp_esc_like( $letter ) . '%'; 499 692 $letter_sql = $wpdb->prepare( 'bm.meta_value LIKE %s', $letter_like ); 500 693 501 694 $hidden_sql = ''; … … class BP_Blogs_Blog { 526 719 * @param string|bool $type Not currently used. Default: false. 527 720 * @return array $paged_blogs The located blogs array, with the extras added. 528 721 */ 529 public static function get_blog_extras( &$paged_blogs, &$blog_ids, $type = false ) {722 public static function get_blog_extras( &$paged_blogs, &$blog_ids, $type = false, $get_latest_post = true ) { 530 723 global $wpdb; 531 724 532 725 $bp = buddypress(); 533 726 534 if ( empty( $blog_ids ) ) 727 if ( empty( $blog_ids ) ) { 535 728 return $paged_blogs; 729 } 536 730 537 731 $blog_ids = implode( ',', wp_parse_id_list( $blog_ids ) ); 538 732 539 for ( $i = 0, $count = count( $paged_blogs ); $i < $count; ++$i ) { 540 $blog_prefix = $wpdb->get_blog_prefix( $paged_blogs[$i]->blog_id ); 541 $paged_blogs[$i]->latest_post = $wpdb->get_row( "SELECT ID, post_content, post_title, post_excerpt, guid FROM {$blog_prefix}posts WHERE post_status = 'publish' AND post_type = 'post' AND id != 1 ORDER BY id DESC LIMIT 1" ); 542 $images = array(); 543 544 // Add URLs to any Featured Image this post might have. 545 if ( ! empty( $paged_blogs[$i]->latest_post ) && has_post_thumbnail( $paged_blogs[$i]->latest_post->ID ) ) { 546 547 // Grab 4 sizes of the image. Thumbnail. 548 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'thumbnail', false ); 549 if ( ! empty( $image ) ) 550 $images['thumbnail'] = $image[0]; 551 552 // Medium. 553 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'medium', false ); 554 if ( ! empty( $image ) ) 555 $images['medium'] = $image[0]; 556 557 // Large. 558 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'large', false ); 559 if ( ! empty( $image ) ) 560 $images['large'] = $image[0]; 561 562 // Post thumbnail. 563 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'post-thumbnail', false ); 564 if ( ! empty( $image ) ) 565 $images['post-thumbnail'] = $image[0]; 566 567 // Add the images to the latest_post object. 568 $paged_blogs[$i]->latest_post->images = $images; 733 // Only get latest post in loops. 734 if ( $get_latest_post ) { 735 736 for ( $i = 0, $count = count( $paged_blogs ); $i < $count; ++$i ) { 737 $blog_prefix = $wpdb->get_blog_prefix( $paged_blogs[$i]->blog_id ); 738 $paged_blogs[$i]->latest_post = $wpdb->get_row( "SELECT ID, post_content, post_title, post_excerpt, guid FROM {$blog_prefix}posts WHERE post_status = 'publish' AND post_type = 'post' AND id != 1 ORDER BY id DESC LIMIT 1" ); 739 $images = array(); 740 741 // Add URLs to any Featured Image this post might have. 742 if ( ! empty( $paged_blogs[$i]->latest_post ) && has_post_thumbnail( $paged_blogs[$i]->latest_post->ID ) ) { 743 744 // Grab 4 sizes of the image. Thumbnail. 745 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'thumbnail', false ); 746 if ( ! empty( $image ) ) 747 $images['thumbnail'] = $image[0]; 748 749 // Medium. 750 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'medium', false ); 751 if ( ! empty( $image ) ) 752 $images['medium'] = $image[0]; 753 754 // Large. 755 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'large', false ); 756 if ( ! empty( $image ) ) 757 $images['large'] = $image[0]; 758 759 // Post thumbnail. 760 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'post-thumbnail', false ); 761 if ( ! empty( $image ) ) 762 $images['post-thumbnail'] = $image[0]; 763 764 // Add the images to the latest_post object. 765 $paged_blogs[$i]->latest_post->images = $images; 766 } 569 767 } 570 768 } 571 769 -
src/bp-blogs/classes/class-bp-blogs-component.php
diff --git src/bp-blogs/classes/class-bp-blogs-component.php src/bp-blogs/classes/class-bp-blogs-component.php index 69dfe37..ee9b204 100644
class BP_Blogs_Component extends BP_Component { 31 31 buddypress()->plugin_dir, 32 32 array( 33 33 'adminbar_myaccount_order' => 30, 34 'search_query_arg' => 'sites_search', 34 'search_query_arg' => 'sites_search', 35 'features' => array( 'site' ), 35 36 ) 36 37 ); 37 38 } … … class BP_Blogs_Component extends BP_Component { 137 138 138 139 if ( is_multisite() ) { 139 140 $includes[] = 'widgets'; 141 142 // Conditional includes 143 if ( bp_is_active( $this->id, 'site' ) ) { 144 $includes[] = 'site'; 145 } 140 146 } 141 147 142 148 // Include the files. -
src/bp-blogs/classes/class-bp-site-query.php
diff --git src/bp-blogs/classes/class-bp-site-query.php src/bp-blogs/classes/class-bp-site-query.php index e69de29..0b92618 100644
1 <?php 2 /** 3 * BuddyPress Blogs Classes. 4 * 5 * @package BuddyPress 6 * @subpackage BlogsClasses 7 * @since 2.7.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 /** 14 * BuddyPress class used for querying sites. 15 * 16 * @since 2.7.0 17 * 18 * @see BP_Site_Query::__construct() for accepted arguments. 19 */ 20 class BP_Site_Query extends WP_Site_Query { 21 22 /** 23 * Get sites of a network according to a given request. 24 * 25 * @since 2.7.0 26 * 27 * @access public 28 * 29 * @param string|array $query { 30 * Optional. Array or query string of site query parameters. Default empty. 31 * @see WP_Site_Query::__construct() for WordPress arguments. 32 * Below are arguments specific to BuddyPress. 33 * 34 * @type int $user_id Optional. ID of the user whose blogs are being 35 * retrieved. Default: 0. 36 * @type string|bool $search_terms Optional. Search by text stored in 37 * blogmeta (such as the blog name). Default: false. 38 * } 39 */ 40 public function __construct( $query = '' ) { 41 // Set filters. 42 $this->set_filters(); 43 44 // Set defaults query vars. 45 parent::__construct(); 46 47 // Set BuddyPress specific query vars. 48 $this->set_bp_query( $query ); 49 50 // Get the matching sites 51 $this->get_sites(); 52 53 // Get extra data for each found sites. 54 $this->get_site_extras(); 55 } 56 57 /** 58 * Set custom filters to edit WordPress query 59 * 60 * @since 2.7.0 61 * 62 * @access private 63 */ 64 private function set_filters() { 65 add_filter( 'sites_clauses', array( $this, 'edit_query' ), 10, 1 ); 66 } 67 68 /** 69 * Get sites of a network according to a given request. 70 * 71 * @since 2.7.0 72 * 73 * @access private 74 * 75 * @param string|array $query Array or string of BP_Site_Query arguments. See BP_Site_Query::__construct(). 76 */ 77 private function set_bp_query( $query = '' ) { 78 $this->query_vars = wp_parse_args( $query, array( 79 'offset' => 0, 80 'number' => 20, 81 ) ); 82 83 // Get total for loop queries. 84 if ( empty( $this->query_vars['single_site'] ) ) { 85 $this->query_vars['no_found_rows'] = false; 86 } 87 88 /** 89 * Use the WP_Site_Query 'site__in' argument to limit fetched sites to the ones the user 90 * is a contributor or a subscriber of. 91 */ 92 if ( ! empty( $this->query_vars['user_id'] ) ) { 93 $user_sites = BP_BLOGS_BLOG::get_blog_ids_for_user( $this->query_vars['user_id'] ); 94 95 if ( ! empty( $this->query_vars['site__in'] ) ) { 96 $intersect = array_intersect( $user_sites, $this->query_vars['site__in'] ); 97 98 if ( ! empty( $intersect ) ) { 99 $user_sites = $intersect; 100 } 101 } 102 103 $this->query_vars['site__in'] = wp_parse_id_list( $user_sites ); 104 } 105 106 /** 107 * In case of a search, build a Meta Query to look inside 'name' and 108 * 'description' metadatas. 109 */ 110 if ( ! empty( $this->query_vars['search_terms'] ) ) { 111 $this->query_vars['meta_query'] = array( 112 'relation' => 'OR', 113 array( 114 'key' => 'name', 115 'value' => $this->query_vars['search_terms'], 116 'compare' => 'LIKE', 117 ), 118 array( 119 'key' => 'description', 120 'value' => $this->query_vars['search_terms'], 121 'compare' => 'LIKE', 122 ), 123 ); 124 } 125 } 126 127 /** 128 * Only get blogmeta without caching results. 129 * 130 * @since 2.7.0 131 * 132 * @access private 133 * 134 * @param int $site_id The site ID 135 * @return array The list of blogmeta for the requested site. 136 */ 137 private function get_blogmeta_no_cache( $site_id = 0 ) { 138 global $wpdb; 139 $return = array(); 140 141 if ( empty( $site_id ) ) { 142 return $return; 143 } 144 145 $blogmeta_table = buddypress()->blogs->table_name_blogmeta; 146 147 $metas = $wpdb->get_results( $wpdb->prepare( "SELECT meta_key, meta_value FROM {$blogmeta_table} WHERE blog_id = %d", $site_id ), ARRAY_A ); 148 149 foreach( $metas as $row ) { 150 if ( ! isset( $return[ $row['meta_key'] ] ) ) { 151 $return[ $row['meta_key'] ] = array( $row['meta_value'] ); 152 } else { 153 $return[ $row['meta_key'] ][] = $row['meta_value']; 154 } 155 } 156 157 return $return; 158 } 159 160 /** 161 * Loop in found sites to attach metadatas. 162 * 163 * @since 2.7.0 164 * 165 * @access private 166 */ 167 private function get_site_extras() { 168 if ( is_null( $this->sites ) ) { 169 return; 170 } 171 172 $site_ids = $this->get_ids(); 173 174 // It can happen when the main requests is only fetching site ids. 175 if ( empty( $site_ids ) ) { 176 return; 177 } 178 179 $update_meta_cache = false; 180 if ( ! empty( $this->query_vars['update_blogmeta_cache'] ) ) { 181 $update_meta_cache = true; 182 } 183 184 $sites = array(); 185 186 foreach ( $this->sites as $ak => $site ) { 187 188 if ( $update_meta_cache ) { 189 $site_metas = bp_blogs_get_blogmeta( $site->blog_id ); 190 } else { 191 $site_metas = $this->get_blogmeta_no_cache( $site->blog_id ); 192 } 193 194 foreach ( $site_metas as $key => $meta ) { 195 // Don't fetch private blogmeta 196 if ( is_protected_meta( $key ) ) { 197 continue; 198 } 199 200 if ( 1 === count( $meta ) ) { 201 $site->{$key} = maybe_unserialize( $meta[0] ); 202 } else { 203 $site->{$key} = array_map( 'maybe_unserialize', $meta ); 204 } 205 206 // Get the first admin to make sure the user avatar will be used. 207 if ( 'site_admins' === $key ) { 208 $site->admin_user_id = reset( $site->{$key} ); 209 } 210 211 // Attach latest post data, if not querying for a single site. 212 if ( empty( $this->query_vars['single_site'] ) ) { 213 $site = $this->get_latest_post( $site ); 214 } 215 } 216 217 // Reindex for easier matching. 218 $sites[ $site->blog_id ] = $site; 219 } 220 221 $this->sites = $sites; 222 } 223 224 /** 225 * Attach the latest publish post to each site. 226 * 227 * @since 2.7.0 228 * 229 * @param WP_Site $site The site object. 230 * @return WP_Site The site object with its latest published post. 231 */ 232 private function get_latest_post( WP_Site $site ) { 233 if ( empty( $site->blog_id ) ) { 234 return $site; 235 } 236 237 switch_to_blog( $site->blog_id ); 238 239 $latest_post = get_posts( array( 240 'numberposts' => 1, 241 'offset' => 0, 242 'orderby' => 'post_date', 243 'order' => 'DESC', 244 'post_type' => 'post', 245 'post_status' => 'publish', 246 'suppress_filters' => true, 247 'exclude' => 1, 248 ) ); 249 250 $site->latest_post = reset( $latest_post ); 251 252 if ( ! $site->latest_post ) { 253 restore_current_blog(); 254 255 return $site; 256 } 257 258 $post_thumbnail_id = get_post_thumbnail_id( $site->latest_post ); 259 if ( ! empty( $post_thumbnail_id ) ) { 260 $site->latest_post->images = array(); 261 262 foreach( (array) get_intermediate_image_sizes() as $size ) { 263 $site->latest_post->images[ $size ] = wp_get_attachment_image_url( $post_thumbnail_id, $size ); 264 } 265 } 266 267 // Only keep what BuddyPress needs 268 $fields = array_fill_keys( array( 269 'ID', 'post_content', 'post_title', 'post_excerpt', 'guid', 'images' 270 ), true ); 271 272 foreach ( $site->latest_post as $property => $value ) { 273 if ( ! empty( $fields[ $property ] ) ) { 274 continue; 275 } 276 277 unset( $site->latest_post->{$property} ); 278 } 279 280 restore_current_blog(); 281 282 return $site; 283 } 284 285 /** 286 * Edit the query to add BuddyPress custom orders or search features. 287 * 288 * @since 2.7.0 289 * 290 * @param array $pieces A compacted array of site query clauses. 291 * @return array The same/edited compacted array of site query clauses. 292 */ 293 public function edit_query( $pieces = array() ) { 294 global $wpdb; 295 296 $buddypress_orders = array( 297 'last_activity' => true, 298 'name' => true, 299 'RAND()' => true, 300 ); 301 302 // Bail if we don't need to edit the query 303 if ( empty( $this->query_vars['meta_query'] ) && empty( $this->query_vars['orderby'] ) && isset( $buddypress_orders[$this->query_vars['orderby']] ) ) { 304 return $pieces; 305 } 306 307 // There's no WordPress blogmeta table for now but it can change! 308 if ( isset( $wpdb->blogmeta ) ) { 309 $reset_blogmeta = $wpdb->blogmeta; 310 } 311 312 // Use the BuddyPress metadata table 313 $wpdb->blogmeta = buddypress()->blogs->table_name_blogmeta; 314 315 // Add the main table name to each existing $where clauses to avoid SQL warnings. 316 if ( ! empty( $pieces['where' ] ) && ! empty( $this->sql_clauses['where'] ) && $pieces['where'] === join( ' AND ', $this->sql_clauses['where'] ) ) { 317 $possible_fields = array_diff_key( $this->query_var_defaults, array( 318 'fields' => false, 319 'number' => false, 320 'offset' => false, 321 'no_found_rows' => false, 322 'orderby' => false, 323 'order' => false, 324 'search_columns' => false, 325 'count' => false, 326 'date_query' => false, 327 'update_site_cache' => false, 328 ) ); 329 330 $sql_clauses = array(); 331 foreach ( array_keys( $possible_fields ) as $key ) { 332 if ( empty( $this->sql_clauses['where'][$key] ) ) { 333 continue; 334 } 335 336 $kc = $key; 337 338 if ( 0 === strpos( $key, 'site' ) ) { 339 $kc = 'blog_id'; 340 } elseif ( 0 === strpos( $key, 'network' ) ) { 341 $kc = 'site_id'; 342 } 343 344 $sql_clauses[$key] = str_replace( $kc, $wpdb->blogs . '.' . $kc, $this->sql_clauses['where'][$key] ); 345 } 346 347 if ( ! empty( $sql_clauses ) ) { 348 $pieces['where' ] = join( ' AND ', $sql_clauses ); 349 } 350 } 351 352 /** Meta Queries *****************************************************/ 353 354 if ( ! empty( $this->query_vars['meta_query'] ) ) { 355 $meta_query = new WP_Meta_Query( $this->query_vars['meta_query'] ); 356 357 $parts = $meta_query->get_sql( 'blog', $wpdb->blogs, 'blog_id' ); 358 359 if ( 'blog_id' === $pieces['fields'] ) { 360 $pieces['fields'] = "{$wpdb->blogs}.blog_id"; 361 } 362 363 if ( ! empty( $parts['join'] ) ) { 364 if ( ! isset( $pieces['join'] ) ) { 365 $pieces['join'] = $parts['join']; 366 } else { 367 $pieces['join'] .= $parts['join']; 368 } 369 } 370 371 if ( ! empty( $parts['where'] ) ) { 372 if ( empty( $pieces['where'] ) ) { 373 $pieces['where'] = '1=1' . $parts['where']; 374 } else { 375 $pieces['where'] .= $parts['where']; 376 } 377 } 378 } 379 380 /** Custom order *****************************************************/ 381 382 // Alphabetical or active order 383 if ( 'name' === $this->query_vars['orderby'] || 'last_activity' === $this->query_vars['orderby'] ) { 384 if ( 'blog_id' === $pieces['fields'] ) { 385 $pieces['fields'] = "{$wpdb->blogs}.blog_id"; 386 } 387 388 $pieces['fields'] .= sprintf( ', bm.meta_value as %s', $this->query_vars['orderby'] ); 389 $join = " INNER JOIN {$wpdb->blogmeta} bm ON ({$wpdb->blogs}.blog_id = bm.blog_id )"; 390 391 if ( empty( $pieces['join'] ) ) { 392 $pieces['join'] = $join; 393 } else { 394 $pieces['join'] .= $join; 395 } 396 397 $where = sprintf( " AND bm.meta_key = '%s'", $this->query_vars['orderby'] ); 398 399 if ( empty( $pieces['where'] ) ) { 400 $pieces['where'] = '1=1' . $where; 401 } else { 402 $pieces['where'] .= $where; 403 } 404 405 $pieces['orderby'] = ' ' . $this->query_vars['orderby'] . ' ' . $this->query_vars['order'] ; 406 407 // Random order 408 } elseif ( 'RAND()' === $this->query_vars['orderby'] ) { 409 $pieces['orderby'] = $this->query_vars['orderby']; 410 } 411 412 // Reset the global blogmeta table if needed. 413 if ( ! empty( $reset_blogmeta ) ) { 414 $wpdb->blogmeta = $reset_blogmeta; 415 } 416 417 return $pieces; 418 } 419 420 /** 421 * Return results as BP_Blogs_Blog::get() expects it. 422 * 423 * @since 2.7.0 424 * 425 * @return array Multidimensional results array, structured as follows: 426 * 'blogs' - Array of located blog objects. 427 * 'total' - A count of the total blogs matching the filter params. 428 */ 429 public function get_results() { 430 // Single site query. 431 if ( $this->query_vars['no_found_rows'] ) { 432 return array( 'blogs' => array_values( $this->sites ) ); 433 } 434 435 return array( 'blogs' => array_values( $this->sites ), 'total' => $this->found_sites ); 436 } 437 438 /** 439 * Return the site ids found. 440 * 441 * @since 2.7.0 442 * 443 * @return array The list of site ids. 444 */ 445 public function get_ids() { 446 return array_map( 'intval', wp_list_pluck( $this->sites, 'blog_id' ) ); 447 } 448 } -
src/bp-blogs/site/activity.php
diff --git src/bp-blogs/site/activity.php src/bp-blogs/site/activity.php index e69de29..a522230 100644
1 <?php 2 /** 3 * BuddyPress Blogs Site activity. 4 * 5 * @package BuddyPress 6 * @subpackage BlogsSiteActivity 7 * @since 2.7.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 // Put the code for the site activity here! -
src/bp-blogs/site/adminbar.php
diff --git src/bp-blogs/site/adminbar.php src/bp-blogs/site/adminbar.php index e69de29..f054387 100644
1 <?php 2 /** 3 * BuddyPress Blogs Site adminbar. 4 * 5 * @package BuddyPress 6 * @subpackage BlogsSiteAdminbar 7 * @since 2.7.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 // Put the code for the site adminbar here! -
src/bp-blogs/site/functions.php
diff --git src/bp-blogs/site/functions.php src/bp-blogs/site/functions.php index e69de29..ebf6684 100644
1 <?php 2 /** 3 * BuddyPress Blogs Site functions. 4 * 5 * @package BuddyPress 6 * @subpackage BlogsSiteFunctions 7 * @since 2.7.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 /** 14 * Get a single site for the requested slug or ID. 15 * 16 * @since 2.7.0 17 * 18 * @param string|int $slug The slug or ID of the blog 19 * @return WP_Site|object The site object. 20 */ 21 function bp_blogs_get_site( $slug = '' ) { 22 if ( empty( $slug ) ) { 23 return false; 24 } 25 26 $slug = trim( $slug, '/' ); 27 $query_vars = array( 28 'single_site' => true, 29 'offset' => 0, 30 'number' => 1, 31 ); 32 33 if ( is_numeric( $slug ) ) { 34 $query_vars['ID'] = (int) $slug; 35 } elseif ( is_subdomain_install() ) { 36 $query_vars['domain__in'] = array( $slug . '.' . get_current_site()->domain ); 37 } else { 38 $query_vars['path__in'] = array( '/' . $slug . '/' ); 39 } 40 41 if ( true === (bool) apply_filters( 'bp_blogs_use_bp_site_query', true ) && function_exists( 'get_sites' ) ) { 42 $queried_site = new BP_Site_Query( $query_vars ); 43 $result = $queried_site->get_results(); 44 } else { 45 $result = BP_Blogs_Blog::legacy_get( $query_vars ); 46 } 47 48 if ( empty( $result['blogs'] ) ) { 49 return false; 50 } 51 52 return reset( $result['blogs'] ); 53 } -
src/bp-blogs/site/notifications.php
diff --git src/bp-blogs/site/notifications.php src/bp-blogs/site/notifications.php index e69de29..dc7cdb5 100644
1 <?php 2 /** 3 * BuddyPress Blogs Site notifications. 4 * 5 * @package BuddyPress 6 * @subpackage BlogsSiteNotifications 7 * @since 2.7.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 // Put the code for the site notifications here! -
src/bp-blogs/site/screens.php
diff --git src/bp-blogs/site/screens.php src/bp-blogs/site/screens.php index e69de29..50d5c08 100644
1 <?php 2 /** 3 * BuddyPress Blogs Site screens. 4 * 5 * @package BuddyPress 6 * @subpackage BlogsSiteScreens 7 * @since 2.7.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 // Put the code for the site screens here! -
src/bp-blogs/site/subscriptions.php
diff --git src/bp-blogs/site/subscriptions.php src/bp-blogs/site/subscriptions.php index e69de29..c6debc5 100644
1 <?php 2 /** 3 * BuddyPress Blogs Site subscriptions. 4 * 5 * @package BuddyPress 6 * @subpackage BlogsSiteSubscriptions 7 * @since 2.7.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 // Put the code for the front-end site subscriptions feature here! -
src/bp-blogs/site/template.php
diff --git src/bp-blogs/site/template.php src/bp-blogs/site/template.php index e69de29..6c095a7 100644
1 <?php 2 /** 3 * BuddyPress Blogs Site template. 4 * 5 * @package BuddyPress 6 * @subpackage BlogsSiteTemplate 7 * @since 2.7.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 // Put the code for the site template here! -
src/bp-core/admin/bp-core-admin-schema.php
diff --git src/bp-core/admin/bp-core-admin-schema.php src/bp-core/admin/bp-core-admin-schema.php index 6f9043a..9c8f5b1 100644
function bp_core_install_blog_tracking() { 400 400 id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY, 401 401 user_id bigint(20) NOT NULL, 402 402 blog_id bigint(20) NOT NULL, 403 is_contributor tinyint(1) NOT NULL DEFAULT '1', 403 404 KEY user_id (user_id), 404 KEY blog_id (blog_id) 405 KEY blog_id (blog_id), 406 KEY is_contributor (is_contributor) 405 407 ) {$charset_collate};"; 406 408 407 409 $sql[] = "CREATE TABLE {$bp_prefix}bp_user_blogs_blogmeta ( -
src/bp-core/admin/bp-core-admin-settings.php
diff --git src/bp-core/admin/bp-core-admin-settings.php src/bp-core/admin/bp-core-admin-settings.php index c13029f..d17ff83 100644
function bp_admin_setting_callback_bbpress_configuration() { 249 249 <?php 250 250 } 251 251 252 /** Blogs Section *************************************************************/ 253 254 /** 255 * Blogs settings section description for the settings page. 256 * 257 * @since 2.7.0 258 */ 259 function bp_admin_setting_callback_blogs_section() { } 260 261 /** 262 * Disable front-end site subscriptions. 263 * 264 * @since 2.7.0 265 * 266 */ 267 function bp_admin_setting_callback_blog_subscriptions() { 268 ?> 269 270 <input id="bp-disable-site-subscriptions" name="bp-disable-site-subscriptions" type="checkbox" value="1" <?php checked( ! bp_disable_site_subscriptions() ); ?> /> 271 <label for="bp-disable-site-subscriptions"><?php _e( 'Allow members to subscribe to sites from their front-end presentation page.', 'buddypress' ); ?></label> 272 273 <?php 274 } 275 252 276 /** Settings Page *************************************************************/ 253 277 254 278 /** … … function bp_core_admin_settings_save() { 316 340 'bp-disable-group-avatar-uploads', 317 341 'bp-disable-group-cover-image-uploads', 318 342 'bp_disable_blogforum_comments', 343 'bp-disable-site-subscriptions', 319 344 'bp-disable-profile-sync', 320 345 'bp_restrict_group_creation', 321 346 'hide-loggedout-adminbar', -
src/bp-core/bp-core-options.php
diff --git src/bp-core/bp-core-options.php src/bp-core/bp-core-options.php index dac2409..cb73d93 100644
function bp_get_default_options() { 70 70 // Allow comments on blog and forum activity items. 71 71 'bp-disable-blogforum-comments' => true, 72 72 73 // Disallow members to subscribe to sites. 74 'bp-disable-site-subscriptions' => false, 75 73 76 // The ID for the current theme package. 74 77 '_bp_theme_package_id' => 'legacy', 75 78 … … function bp_disable_blogforum_comments( $default = false ) { 696 699 } 697 700 698 701 /** 702 * Is subscribing to sites allowed to members of the network? 703 * 704 * @since 2.7.0 705 * 706 * @param bool $default Optional. Fallback value if not found in the database. 707 * Default: false. 708 * @return bool True if site subscriptions are disabled for members of the network. 709 * False otherwise. 710 */ 711 function bp_disable_site_subscriptions( $default = false ) { 712 713 /** 714 * Filters whether or not members can subscibe to sites from the front-end. 715 * 716 * @since 1.6.0 717 * 718 * @param bool $value Whether or not front-end site subscriptions are disabled. 719 */ 720 return (bool) apply_filters( 'bp_disable_site_subscriptions', (bool) bp_get_option( 'bp-disable-site-subscriptions', $default ) ); 721 } 722 723 /** 699 724 * Is group creation turned off? 700 725 * 701 726 * @since 1.6.0 -
src/bp-core/bp-core-update.php
diff --git src/bp-core/bp-core-update.php src/bp-core/bp-core-update.php index 16608a4..8b270f1 100644
function bp_version_updater() { 265 265 } 266 266 267 267 // Version 2.7.0. 268 if ( $raw_db_version < 109 40) {268 if ( $raw_db_version < 10952 ) { 269 269 bp_update_to_2_7(); 270 270 } 271 271 } … … function bp_update_to_2_5() { 513 513 */ 514 514 function bp_update_to_2_7() { 515 515 bp_add_option( 'bp-emails-unsubscribe-salt', base64_encode( wp_generate_password( 64, true, true ) ) ); 516 517 if ( bp_is_active( 'blogs' ) ) { 518 bp_core_install_blog_tracking(); 519 } 516 520 } 517 521 518 522 /** -
src/bp-core/classes/class-bp-admin.php
diff --git src/bp-core/classes/class-bp-admin.php src/bp-core/classes/class-bp-admin.php index 5946130..be89886 100644
class BP_Admin { 473 473 register_setting( 'buddypress', '_bp_enable_akismet', 'intval' ); 474 474 } 475 475 } 476 477 if ( bp_is_active( 'blogs', 'site' ) ) { 478 // Add the main section. 479 add_settings_section( 'bp_blogs', __( 'Sites Settings', 'buddypress' ), 'bp_admin_setting_callback_blogs_section', 'buddypress' ); 480 481 // Activity commenting on blog and forum posts. 482 add_settings_field( 'bp-disable-site-subscriptions', __( 'Site subscriptions.', 'buddypress' ), 'bp_admin_setting_callback_blog_subscriptions', 'buddypress', 'bp_blogs' ); 483 register_setting( 'buddypress', 'bp-disable-site-subscriptions', 'intval' ); 484 } 476 485 } 477 486 478 487 /** -
src/bp-loader.php
diff --git src/bp-loader.php src/bp-loader.php index 1d05b96..27272ab 100644
class BuddyPress { 331 331 /** Versions **********************************************************/ 332 332 333 333 $this->version = '2.7-alpha'; 334 $this->db_version = 10 469;334 $this->db_version = 10952; 335 335 336 336 /** Loading ***********************************************************/ 337 337 … … class BuddyPress { 593 593 'BP_Signup' => 'members', 594 594 ); 595 595 596 if ( function_exists( 'get_sites' ) ) { 597 $irregular_map['BP_Site_Query'] = 'blogs'; 598 } 599 596 600 $component = null; 597 601 598 602 // First check to see if the class is one without a properly namespaced name. -
tests/phpunit/subdomains-multisite.xml
diff --git tests/phpunit/subdomains-multisite.xml tests/phpunit/subdomains-multisite.xml index e69de29..367cf9e 100644
1 <phpunit 2 bootstrap="bootstrap.php" 3 backupGlobals="false" 4 colors="true" 5 convertErrorsToExceptions="true" 6 convertNoticesToExceptions="true" 7 convertWarningsToExceptions="true" 8 > 9 <php> 10 <const name="WP_TESTS_MULTISITE" value="1" /> 11 <const name="SUBDOMAIN_INSTALL" value="true" /> 12 </php> 13 <testsuites> 14 <testsuite> 15 <directory suffix=".php">./testcases/</directory> 16 </testsuite> 17 </testsuites> 18 </phpunit> 19 No newline at end of file -
tests/phpunit/testcases/blogs/activity.php
diff --git tests/phpunit/testcases/blogs/activity.php tests/phpunit/testcases/blogs/activity.php index 8ab30ff..b7d7584 100644
class BP_Tests_Blogs_Activity extends BP_UnitTestCase { 200 200 201 201 /** 202 202 * @group bp_blogs_format_activity_action_new_blog 203 * @group multisite 203 204 */ 204 205 public function test_bp_activity_format_activity_action_new_blog_backpat() { 205 if ( ! is_multisite() ) {206 return;207 }208 209 206 add_filter( 'bp_blogs_activity_created_blog_action', array( $this, 'created_blog_passthrough' ), 10, 2 ); 210 207 211 208 $b = $this->factory->blog->create(); 212 209 $u = $this->factory->user->create(); 213 210 214 $recorded_blog = new BP_Blogs_Blog; 215 $recorded_blog->user_id = $u; 216 $recorded_blog->blog_id = $b; 217 $recorded_blog_id = $recorded_blog->save(); 211 $recorded_blog = new BP_Blogs_Blog; 212 $recorded_blog->user_id = $u; 213 $recorded_blog->blog_id = $b; 214 $recorded_blog->is_contributor = 1; 215 $recorded_blog_id = $recorded_blog->save(); 218 216 219 217 $a = $this->factory->activity->create( array( 220 218 'component' => buddypress()->blogs->id, -
tests/phpunit/testcases/blogs/cache.php
diff --git tests/phpunit/testcases/blogs/cache.php tests/phpunit/testcases/blogs/cache.php index 1163278..4880e80 100644
3 3 /** 4 4 * @group blogs 5 5 * @group cache 6 * @group multisite 6 7 */ 7 8 class BP_Tests_Blogs_Cache extends BP_UnitTestCase { 8 9 /** 9 10 * @group bp_blogs_update_meta_cache 10 11 */ 11 12 public function test_bp_blogs_update_meta_cache() { 12 if ( ! is_multisite() ) {13 return;14 }15 16 13 $b1 = $this->factory->blog->create(); 17 14 $b2 = $this->factory->blog->create(); 18 15 … … class BP_Tests_Blogs_Cache extends BP_UnitTestCase { 90 87 * @group bp_has_blogs 91 88 */ 92 89 public function test_bp_blogs_update_meta_cache_bp_has_blogs() { 93 if ( ! is_multisite() ) {94 return;95 }96 97 90 $u = $this->factory->user->create(); 98 91 99 92 // Switch user so we have access to non-public blogs … … class BP_Tests_Blogs_Cache extends BP_UnitTestCase { 184 177 * @group bp_has_blogs 185 178 */ 186 179 public function test_bp_blogs_update_meta_cache_bp_has_blogs_false() { 187 if ( ! is_multisite() ) {188 return;189 }190 191 180 $u = $this->factory->user->create(); 192 181 193 182 // Switch user so we have access to non-public blogs … … class BP_Tests_Blogs_Cache extends BP_UnitTestCase { 237 226 * @group counts 238 227 */ 239 228 public function test_bp_blogs_total_count_should_respect_cached_value_of_0() { 240 if ( ! is_multisite() ) {241 return;242 }243 244 229 global $wpdb; 245 230 246 231 // prime cache … … class BP_Tests_Blogs_Cache extends BP_UnitTestCase { 259 244 * @group bp_blogs_total_blogs 260 245 */ 261 246 public function test_bp_blogs_total_blogs_count_after_delete_blog() { 262 if ( ! is_multisite() ) {263 return;264 }265 266 247 $u = $this->factory->user->create(); 267 248 268 249 // need to make sure we set the 'public' flag due to how BP_Blogs_Blogs:get_all() works 269 250 $b1 = $this->factory->blog->create( array( 270 'meta' => array( 251 'user_id' => $u, 252 'meta' => array( 271 253 'public' => 1 272 254 ) 273 255 ) ); 274 256 $b2 = $this->factory->blog->create( array( 275 'meta' => array( 257 'user_id' => $u, 258 'meta' => array( 276 259 'public' => 1 277 260 ) 278 261 ) ); … … class BP_Tests_Blogs_Cache extends BP_UnitTestCase { 293 276 * @group update_blog_details 294 277 */ 295 278 public function test_update_blog_details_should_purge_blogmeta_cache() { 296 if ( ! is_multisite() ) {297 return;298 }299 300 279 $u = $this->factory->user->create(); 301 280 302 281 $b1 = $this->factory->blog->create(); -
tests/phpunit/testcases/blogs/class-bp-blogs-blog.php
diff --git tests/phpunit/testcases/blogs/class-bp-blogs-blog.php tests/phpunit/testcases/blogs/class-bp-blogs-blog.php index 6ecc308..719c9d7 100644
3 3 /** 4 4 * @group blogs 5 5 * @group BP_Blogs_Blog 6 * @group multisite 6 7 */ 7 8 class BP_Tests_BP_Blogs_Blog_TestCases extends BP_UnitTestCase { 8 public function test_get_with_search_terms() { 9 if ( ! is_multisite() ) { 10 return; 11 } 9 public function setUp() { 10 parent::setUp(); 11 12 $this->test_legacy = function_exists( 'get_sites' ); 13 14 $this->old_user = get_current_user_id(); 15 $this->current_user = $this->factory->user->create(); 16 17 $this->set_current_user( $this->current_user ); 18 19 $this->blogs = array( 20 'foobar' => $this->factory->blog->create( array( 21 'title' => 'Foo Bar Blog', 22 'user_id' => $this->current_user, 23 'path' => '/path' . rand() . time() . '/', 24 ) ), 25 'barfoo' => $this->factory->blog->create( array( 26 'title' => 'Bar foo Blog', 27 'user_id' => $this->current_user, 28 'path' => '/path' . rand() . time() . '/', 29 ) ), 30 ); 31 } 12 32 13 $old_user = get_current_user_id(); 33 public function tearDown() { 34 parent::tearDown(); 14 35 15 $u = $this->factory->user->create(); 16 $this->set_current_user( $u ); 17 $b = $this->factory->blog->create( array( 18 'title' => 'The Foo Bar Blog', 19 'user_id' => $u, 20 ) ); 36 $this->set_current_user( $this->old_user ); 37 } 38 39 public function test_get_with_search_terms() { 21 40 bp_blogs_record_existing_blogs(); 22 41 23 42 // make the blog public or it won't turn up in generic results 24 update_blog_option( $ b, 'blog_public', '1' );43 update_blog_option( $this->blogs['foobar'], 'blog_public', '1' ); 25 44 26 $blogs = BP_Blogs_Blog::get( 'active', false, false, 0, 'Foo ' );45 $blogs = BP_Blogs_Blog::get( 'active', false, false, 0, 'Foo Bar' ); 27 46 $blog_ids = wp_list_pluck( $blogs['blogs'], 'blog_id' ); 28 47 29 $this->assertEquals( array( $b ), $blog_ids ); 48 $this->assertEquals( array( $this->blogs['foobar'] ), $blog_ids ); 49 50 if ( $this->test_legacy ) { 51 add_filter( 'bp_blogs_use_bp_site_query', '__return_false' ); 52 53 $blogs = BP_Blogs_Blog::get( 'active', false, false, 0, 'Foo Bar' ); 54 $blog_ids = wp_list_pluck( $blogs['blogs'], 'blog_id' ); 55 56 $this->assertEquals( array( $this->blogs['foobar'] ), $blog_ids ); 57 58 remove_filter( 'bp_blogs_use_bp_site_query', '__return_false' ); 59 } 30 60 } 31 61 32 62 /** 33 63 * @ticket BP5858 34 64 */ 35 65 public function test_get_with_search_terms_should_match_description() { 36 if ( ! is_multisite() ) { 37 return; 38 } 39 40 $old_user = get_current_user_id(); 41 42 $u = $this->factory->user->create(); 43 $this->set_current_user( $u ); 44 $b = $this->factory->blog->create( array( 45 'title' => 'The Foo Bar Blog', 46 'domain' => __METHOD__, 47 'user_id' => $u, 48 ) ); 49 update_blog_option( $b, 'blogdescription', 'Full of foorificness' ); 66 update_blog_option( $this->blogs['foobar'], 'blogdescription', 'Full of foorificness' ); 50 67 bp_blogs_record_existing_blogs(); 51 68 52 69 // make the blog public or it won't turn up in generic results 53 update_blog_option( $ b, 'blog_public', '1' );70 update_blog_option( $this->blogs['foobar'], 'blog_public', '1' ); 54 71 55 72 $blogs = BP_Blogs_Blog::get( 'active', false, false, 0, 'Full' ); 56 73 $blog_ids = wp_list_pluck( $blogs['blogs'], 'blog_id' ); 57 74 58 $this->assertEquals( array( $ b), $blog_ids );75 $this->assertEquals( array( $this->blogs['foobar'] ), $blog_ids ); 59 76 $this->assertEquals( 1, $blogs['total'] ); 60 }61 77 62 public function test_search_blogs() { 63 if ( ! is_multisite() ) { 64 return; 65 } 78 if ( $this->test_legacy ) { 79 add_filter( 'bp_blogs_use_bp_site_query', '__return_false' ); 66 80 67 $old_user = get_current_user_id(); 81 $blogs = BP_Blogs_Blog::get( 'active', false, false, 0, 'Full' ); 82 $blog_ids = wp_list_pluck( $blogs['blogs'], 'blog_id' ); 68 83 69 $u = $this->factory->user->create(); 70 $this->set_current_user( $u ); 71 $b = $this->factory->blog->create( array( 72 'title' => 'The Foo Bar Blog', 73 'user_id' => $u, 74 'path' => '/path' . rand() . time() . '/', 75 ) ); 84 $this->assertEquals( array( $this->blogs['foobar'] ), $blog_ids ); 85 $this->assertEquals( 1, $blogs['total'] ); 86 87 remove_filter( 'bp_blogs_use_bp_site_query', '__return_false' ); 88 } 89 } 90 91 public function test_search_blogs() { 76 92 bp_blogs_record_existing_blogs(); 77 93 78 94 // make the blog public or it won't turn up in generic results 79 update_blog_option( $ b, 'blog_public', '1' );95 update_blog_option( $this->blogs['foobar'], 'blog_public', '1' ); 80 96 81 $blogs = BP_Blogs_Blog::search_blogs( 'Foo ' );97 $blogs = BP_Blogs_Blog::search_blogs( 'Foo Bar' ); 82 98 $blog_ids = wp_list_pluck( $blogs['blogs'], 'blog_id' ); 83 99 84 $this->assertEquals( array( $ b), $blog_ids );100 $this->assertEquals( array( $this->blogs['foobar'] ), $blog_ids ); 85 101 } 86 102 87 103 /** 88 104 * @group get_by_letter 89 105 */ 90 106 public function test_get_by_letter() { 91 if ( ! is_multisite() ) {92 return;93 }94 95 $old_user = get_current_user_id();96 97 $u = $this->factory->user->create();98 $this->set_current_user( $u );99 $b = $this->factory->blog->create( array(100 'title' => 'Foo Bar Blog',101 'user_id' => $u,102 'path' => '/path' . rand() . time() . '/',103 ) );104 107 bp_blogs_record_existing_blogs(); 105 108 106 109 // make the blog public or it won't turn up in generic results 107 update_blog_option( $ b, 'blog_public', '1' );110 update_blog_option( $this->blogs['foobar'], 'blog_public', '1' ); 108 111 109 112 $blogs = BP_Blogs_Blog::get_by_letter( 'F' ); 110 113 $blog_ids = wp_list_pluck( $blogs['blogs'], 'blog_id' ); 111 114 112 $this->assertEquals( array( $ b), $blog_ids );115 $this->assertEquals( array( $this->blogs['foobar'] ), $blog_ids ); 113 116 } 114 117 115 118 /** 116 119 * @group get_order_by 117 120 */ 118 public function test_get_order_by() { 119 if ( ! is_multisite() ) { 120 return; 121 public function test_get_order_by_alphabetical() { 122 bp_blogs_record_existing_blogs(); 123 124 // make the blog public or it won't turn up in generic results 125 foreach ( $this->blogs as $b ) { 126 update_blog_option( $b, 'blog_public', '1' ); 121 127 } 122 128 123 $old_user = get_current_user_id(); 129 // Used to make sure barfoo is older than foobar 130 $b_time = date_i18n( 'Y-m-d H:i:s', strtotime( '-5 minutes' ) ); 124 131 125 $u = $this->factory->user->create(); 126 $this->set_current_user( $u ); 127 $bs = array( 128 'foobar' => $this->factory->blog->create( array( 129 'title' => 'Foo Bar Blog', 130 'user_id' => $u, 131 'path' => '/path' . rand() . time() . '/', 132 ) ), 133 'barfoo' => $this->factory->blog->create( array( 134 'title' => 'Bar foo Blog', 135 'user_id' => $u, 136 'path' => '/path' . rand() . time() . '/', 137 ) ), 138 ); 132 /* Alphabetical */ 133 $blogs = BP_Blogs_Blog::get( 'alphabetical', false, false, $this->current_user ); 134 $blog_ids = wp_list_pluck( $blogs['blogs'], 'blog_id' ); 135 $this->assertEquals( array( $this->blogs['barfoo'], $this->blogs['foobar'] ), $blog_ids ); 139 136 137 if ( $this->test_legacy ) { 138 add_filter( 'bp_blogs_use_bp_site_query', '__return_false' ); 139 140 $blogs = BP_Blogs_Blog::get( 'alphabetical', false, false, $this->current_user ); 141 $blog_ids = wp_list_pluck( $blogs['blogs'], 'blog_id' ); 142 $this->assertEquals( array( $this->blogs['barfoo'], $this->blogs['foobar'] ), $blog_ids ); 143 144 remove_filter( 'bp_blogs_use_bp_site_query', '__return_false' ); 145 } 146 } 147 148 /** 149 * @group get_order_by 150 */ 151 public function test_get_order_by_newest() { 140 152 bp_blogs_record_existing_blogs(); 141 153 142 154 // make the blog public or it won't turn up in generic results 143 foreach ( $ bs as $b ) {155 foreach ( $this->blogs as $b ) { 144 156 update_blog_option( $b, 'blog_public', '1' ); 145 157 } 146 158 147 159 // Used to make sure barfoo is older than foobar 148 160 $b_time = date_i18n( 'Y-m-d H:i:s', strtotime( '-5 minutes' ) ); 149 161 150 /* Alphabetical */151 $blogs = BP_Blogs_Blog::get( 'alphabetical', false, false, $u );152 $blog_ids = wp_list_pluck( $blogs['blogs'], 'blog_id' );153 $this->assertEquals( array( $bs['barfoo'], $bs['foobar'] ), $blog_ids );154 155 162 /* Newest */ 156 update_blog_details( $bs['barfoo'], array( 'registered' => $b_time ) ); 157 $blogs = BP_Blogs_Blog::get( 'newest', false, false, $u ); 163 update_blog_details( $this->blogs['barfoo'], array( 'registered' => $b_time ) ); 164 165 $blogs = BP_Blogs_Blog::get( 'newest', false, false, $this->current_user ); 158 166 $blog_ids = wp_list_pluck( $blogs['blogs'], 'blog_id' ); 159 $this->assertEquals( array( $bs['foobar'], $bs['barfoo'] ), $blog_ids ); 167 $this->assertEquals( array( $this->blogs['foobar'], $this->blogs['barfoo'] ), $blog_ids ); 168 169 if ( $this->test_legacy ) { 170 add_filter( 'bp_blogs_use_bp_site_query', '__return_false' ); 171 172 $blogs = BP_Blogs_Blog::get( 'newest', false, false, $this->current_user ); 173 $blog_ids = wp_list_pluck( $blogs['blogs'], 'blog_id' ); 174 $this->assertEquals( array( $this->blogs['foobar'], $this->blogs['barfoo'] ), $blog_ids ); 175 176 remove_filter( 'bp_blogs_use_bp_site_query', '__return_false' ); 177 } 178 } 179 180 /** 181 * @group get_order_by 182 */ 183 public function test_get_order_by_active() { 184 bp_blogs_record_existing_blogs(); 185 186 // make the blog public or it won't turn up in generic results 187 foreach ( $this->blogs as $b ) { 188 update_blog_option( $b, 'blog_public', '1' ); 189 } 190 191 // Used to make sure barfoo is older than foobar 192 $b_time = date_i18n( 'Y-m-d H:i:s', strtotime( '-5 minutes' ) ); 160 193 161 194 /* Active */ 162 bp_blogs_update_blogmeta( $bs['barfoo'], 'last_activity', $b_time ); 163 $blogs = BP_Blogs_Blog::get( 'active', false, false, $u ); 195 bp_blogs_update_blogmeta( $this->blogs['barfoo'], 'last_activity', $b_time ); 196 197 $blogs = BP_Blogs_Blog::get( 'active', false, false, $this->current_user ); 164 198 $blog_ids = wp_list_pluck( $blogs['blogs'], 'blog_id' ); 165 $this->assertEquals( array( $bs['foobar'],$bs['barfoo'] ), $blog_ids ); 199 $this->assertEquals( array( $this->blogs['foobar'], $this->blogs['barfoo'] ), $blog_ids ); 200 201 if ( $this->test_legacy ) { 202 add_filter( 'bp_blogs_use_bp_site_query', '__return_false' ); 203 204 $blogs = BP_Blogs_Blog::get( 'active', false, false, $this->current_user ); 205 $blog_ids = wp_list_pluck( $blogs['blogs'], 'blog_id' ); 206 $this->assertEquals( array( $this->blogs['foobar'], $this->blogs['barfoo'] ), $blog_ids ); 207 208 remove_filter( 'bp_blogs_use_bp_site_query', '__return_false' ); 209 } 210 } 211 212 /** 213 * @group get_order_by 214 */ 215 public function test_get_order_by_random() { 216 bp_blogs_record_existing_blogs(); 217 218 // make the blog public or it won't turn up in generic results 219 foreach ( $this->blogs as $b ) { 220 update_blog_option( $b, 'blog_public', '1' ); 221 } 166 222 167 223 /* Random */ 168 $blogs = BP_Blogs_Blog::get( 'random', false, false, $ u);224 $blogs = BP_Blogs_Blog::get( 'random', false, false, $this->current_user ); 169 225 $this->assertTrue( 2 == count( $blogs['blogs'] ) ); 170 226 171 $this->set_current_user( $old_user ); 227 if ( $this->test_legacy ) { 228 add_filter( 'bp_blogs_use_bp_site_query', '__return_false' ); 229 230 $blogs = BP_Blogs_Blog::get( 'random', false, false, $this->current_user ); 231 $this->assertTrue( 2 == count( $blogs['blogs'] ) ); 232 233 remove_filter( 'bp_blogs_use_bp_site_query', '__return_false' ); 234 } 172 235 } 173 236 } -
tests/phpunit/testcases/blogs/functions.php
diff --git tests/phpunit/testcases/blogs/functions.php tests/phpunit/testcases/blogs/functions.php index dcbed4c..5425675 100644
class BP_Tests_Blogs_Functions extends BP_UnitTestCase { 281 281 282 282 /** 283 283 * @group bp_blogs_restore_data 284 * @group multisite 284 285 */ 285 286 public function test_bp_blogs_restore_data() { 286 if ( ! is_multisite() ) {287 return;288 }289 290 287 // Create a regular member 291 288 $u = $this->factory->user->create(); 292 289 -
tests/phpunit/testcases/blogs/site.php
diff --git tests/phpunit/testcases/blogs/site.php tests/phpunit/testcases/blogs/site.php index e69de29..e42cc22 100644
1 <?php 2 3 /** 4 * @group site 5 * @group multisite 6 */ 7 class BP_Tests_Blogs_Site_TestCases extends BP_UnitTestCase { 8 public function setUp() { 9 parent::setUp(); 10 11 $this->test_legacy = function_exists( 'get_sites' ); 12 13 $this->old_user = get_current_user_id(); 14 $this->current_user = $this->factory->user->create(); 15 $this->other_user = $this->factory->user->create(); 16 17 $this->sites = array( 18 'foo' => array( 'domain' => WP_TESTS_DOMAIN, 'path' => '/foobar/', 'user_id' => $this->current_user, 'title' => 'Foo Bar' ), 19 'bar' => array( 'domain' => WP_TESTS_DOMAIN, 'path' => '/barfoo/', 'user_id' => $this->other_user, 'title' => 'Bar Foo' ), 20 'taz' => array( 'domain' => WP_TESTS_DOMAIN, 'path' => '/tazfoo/', 'user_id' => $this->current_user, 'title' => 'Taz Foo', 'meta' => array( 'public' => 0 ) ), 21 'private' => array( 'domain' => WP_TESTS_DOMAIN, 'path' => '/private/', 'user_id' => $this->other_user, 'meta' => array( 'public' => 0 ) ), 22 ); 23 24 if ( is_subdomain_install() ) { 25 $this->sites = array( 26 'foo' => array( 'domain' => 'foobar.' . WP_TESTS_DOMAIN, 'path' => '/', 'user_id' => $this->current_user, 'title' => 'Foo Bar' ), 27 'bar' => array( 'domain' => 'barfoo.' . WP_TESTS_DOMAIN, 'path' => '/', 'user_id' => $this->other_user, 'title' => 'Bar Foo' ), 28 'taz' => array( 'domain' => 'tazfoo.' . WP_TESTS_DOMAIN, 'path' => '/', 'user_id' => $this->current_user, 'title' => 'Taz Foo', 'meta' => array( 'public' => 0 ) ), 29 'private' => array( 'domain' => 'private.' . WP_TESTS_DOMAIN, 'path' => '/', 'user_id' => $this->other_user, 'meta' => array( 'public' => 0 ) ), 30 ); 31 } 32 33 foreach ( $this->sites as $key => $site ) { 34 $this->sites[ $key ]['id'] = $this->factory->blog->create( $site ); 35 } 36 37 $this->set_current_user( $this->current_user ); 38 } 39 40 public function tearDown() { 41 parent::tearDown(); 42 43 $this->set_current_user( $this->old_user ); 44 } 45 46 /** 47 * @group bp_blogs_get_site 48 */ 49 public function test_bp_blogs_get_site_by_slug() { 50 $single_site = bp_blogs_get_site( '/tazfoo/' ); 51 $this->assertSame( $single_site->domain, $this->sites['taz']['domain'] ); 52 53 if ( $this->test_legacy ) { 54 add_filter( 'bp_blogs_use_bp_site_query', '__return_false' ); 55 56 $legacy_site = bp_blogs_get_site( 'barfoo/' ); 57 $this->assertSame( $legacy_site->domain, $this->sites['bar']['domain'] ); 58 59 remove_filter( 'bp_blogs_use_bp_site_query', '__return_false' ); 60 } 61 } 62 63 /** 64 * @group bp_blogs_get_site 65 */ 66 public function test_bp_blogs_get_site_by_id() { 67 $single_site = bp_blogs_get_site( $this->sites['foo']['id'] ); 68 $this->assertSame( $single_site->domain, $this->sites['foo']['domain'] ); 69 70 if ( $this->test_legacy ) { 71 add_filter( 'bp_blogs_use_bp_site_query', '__return_false' ); 72 73 $legacy_site = bp_blogs_get_site( $this->sites['private']['id'] ); 74 $this->assertSame( $legacy_site->domain, $this->sites['private']['domain'] ); 75 76 remove_filter( 'bp_blogs_use_bp_site_query', '__return_false' ); 77 } 78 } 79 }