Changeset 9485 for trunk/src/bp-blogs/bp-blogs-classes.php
- Timestamp:
- 02/15/2015 12:48:56 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-blogs/bp-blogs-classes.php
r9471 r9485 11 11 defined( 'ABSPATH' ) || exit; 12 12 13 /** 14 * The main BuddyPress blog class. 15 * 16 * A BP_Blogs_Object represents a link between a specific WordPress blog on a 17 * network and a specific user on that blog. 18 * 19 * @since BuddyPress (1.0.0) 20 */ 21 class BP_Blogs_Blog { 22 public $id; 23 public $user_id; 24 public $blog_id; 25 26 /** 27 * Constructor method. 28 * 29 * @param int $id Optional. The ID of the blog. 30 */ 31 public function __construct( $id = null ) { 32 if ( !empty( $id ) ) { 33 $this->id = $id; 34 $this->populate(); 35 } 36 } 37 38 /** 39 * Populate the object with data about the specific activity item. 40 */ 41 public function populate() { 42 global $wpdb; 43 44 $bp = buddypress(); 45 46 $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->blogs->table_name} WHERE id = %d", $this->id ) ); 47 48 $this->user_id = $blog->user_id; 49 $this->blog_id = $blog->blog_id; 50 } 51 52 /** 53 * Save the BP blog data to the database. 54 * 55 * @return bool True on success, false on failure. 56 */ 57 public function save() { 58 global $wpdb; 59 60 $this->user_id = apply_filters( 'bp_blogs_blog_user_id_before_save', $this->user_id, $this->id ); 61 $this->blog_id = apply_filters( 'bp_blogs_blog_id_before_save', $this->blog_id, $this->id ); 62 63 /** 64 * Fires before the current blog item gets saved. 65 * 66 * Please use this hook to filter the properties above. Each part will be passed in. 67 * 68 * @since BuddyPress (1.0.0) 69 * 70 * @param BP_Blogs_Blog Current instance of the blog item being saved. Passed by reference. 71 */ 72 do_action_ref_array( 'bp_blogs_blog_before_save', array( &$this ) ); 73 74 // Don't try and save if there is no user ID or blog ID set. 75 if ( !$this->user_id || !$this->blog_id ) 76 return false; 77 78 // Don't save if this blog has already been recorded for the user. 79 if ( !$this->id && $this->exists() ) 80 return false; 81 82 $bp = buddypress(); 83 84 if ( $this->id ) { 85 // Update 86 $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 ); 87 } else { 88 // Save 89 $sql = $wpdb->prepare( "INSERT INTO {$bp->blogs->table_name} ( user_id, blog_id ) VALUES ( %d, %d )", $this->user_id, $this->blog_id ); 90 } 91 92 if ( !$wpdb->query($sql) ) 93 return false; 94 95 /** 96 * Fires after the current blog item gets saved. 97 * 98 * Please use this hook to filter the properties above. Each part will be passed in. 99 * 100 * @since BuddyPress (1.0.0) 101 * 102 * @param BP_Blogs_Blog Current instance of the blog item being saved. Passed by reference. 103 */ 104 do_action_ref_array( 'bp_blogs_blog_after_save', array( &$this ) ); 105 106 if ( $this->id ) 107 return $this->id; 108 else 109 return $wpdb->insert_id; 110 } 111 112 /** 113 * Check whether an association between this user and this blog exists. 114 * 115 * @return int The number of associations between the user and blog 116 * saved in the blog component tables. 117 */ 118 public function exists() { 119 global $wpdb; 120 121 $bp = buddypress(); 122 123 return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->blogs->table_name} WHERE user_id = %d AND blog_id = %d", $this->user_id, $this->blog_id ) ); 124 } 125 126 /** Static Methods ***************************************************/ 127 128 /** 129 * Retrieve a set of blog-user associations. 130 * 131 * @param string $type The order in which results should be returned. 132 * 'active', 'alphabetical', 'newest', or 'random'. 133 * @param int|bool $limit Optional. The maximum records to return. 134 * Default: false. 135 * @param int|bool $page Optional. The page of records to return. 136 * Default: false (unlimited results). 137 * @param int $user_id Optional. ID of the user whose blogs are being 138 * retrieved. Default: 0. 139 * @param string|bool $search_terms Optional. Search by text stored in 140 * blogmeta (such as the blog name). Default: false. 141 * @param bool $update_meta_cache Whether to pre-fetch metadata for 142 * blogs. Default: true. 143 * @param array $include_blog_ids Array of blog IDs to include. 144 * @return array Multidimensional results array, structured as follows: 145 * 'blogs' - Array of located blog objects 146 * 'total' - A count of the total blogs matching the filter params 147 */ 148 public static function get( $type, $limit = false, $page = false, $user_id = 0, $search_terms = false, $update_meta_cache = true, $include_blog_ids = false ) { 149 global $wpdb; 150 151 $bp = buddypress(); 152 153 if ( !is_user_logged_in() || ( !bp_current_user_can( 'bp_moderate' ) && ( $user_id != bp_loggedin_user_id() ) ) ) 154 $hidden_sql = "AND wb.public = 1"; 155 else 156 $hidden_sql = ''; 157 158 $pag_sql = ( $limit && $page ) ? $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ) : ''; 159 160 $user_sql = !empty( $user_id ) ? $wpdb->prepare( " AND b.user_id = %d", $user_id ) : ''; 161 162 switch ( $type ) { 163 case 'active': default: 164 $order_sql = "ORDER BY bm.meta_value DESC"; 165 break; 166 case 'alphabetical': 167 $order_sql = "ORDER BY bm_name.meta_value ASC"; 168 break; 169 case 'newest': 170 $order_sql = "ORDER BY wb.registered DESC"; 171 break; 172 case 'random': 173 $order_sql = "ORDER BY RAND()"; 174 break; 175 } 176 177 $include_sql = ''; 178 $include_blog_ids = array_filter( wp_parse_id_list( $include_blog_ids ) ); 179 if ( ! empty( $include_blog_ids ) ) { 180 $blog_ids_sql = implode( ',', $include_blog_ids ); 181 $include_sql = " AND b.blog_id IN ({$blog_ids_sql})"; 182 } 183 184 if ( ! empty( $search_terms ) ) { 185 $search_terms_like = '%' . bp_esc_like( $search_terms ) . '%'; 186 $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 ); 187 } else { 188 $search_terms_sql = ''; 189 } 190 191 $paged_blogs = $wpdb->get_results( " 192 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 193 FROM 194 {$bp->blogs->table_name} b 195 LEFT JOIN {$bp->blogs->table_name_blogmeta} bm ON (b.blog_id = bm.blog_id) 196 LEFT JOIN {$bp->blogs->table_name_blogmeta} bm_name ON (b.blog_id = bm_name.blog_id) 197 LEFT JOIN {$bp->blogs->table_name_blogmeta} bm_description ON (b.blog_id = bm_description.blog_id) 198 LEFT JOIN {$wpdb->base_prefix}blogs wb ON (b.blog_id = wb.blog_id) 199 LEFT JOIN {$wpdb->users} u ON (b.user_id = u.ID) 200 WHERE 201 wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} 202 AND bm.meta_key = 'last_activity' AND bm_name.meta_key = 'name' AND bm_description.meta_key = 'description' 203 {$search_terms_sql} {$user_sql} {$include_sql} 204 GROUP BY b.blog_id {$order_sql} {$pag_sql} 205 " ); 206 207 $total_blogs = $wpdb->get_var( " 208 SELECT COUNT(DISTINCT b.blog_id) 209 FROM 210 {$bp->blogs->table_name} b 211 LEFT JOIN {$wpdb->base_prefix}blogs wb ON (b.blog_id = wb.blog_id) 212 LEFT JOIN {$bp->blogs->table_name_blogmeta} bm_name ON (b.blog_id = bm_name.blog_id) 213 LEFT JOIN {$bp->blogs->table_name_blogmeta} bm_description ON (b.blog_id = bm_description.blog_id) 214 WHERE 215 wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} 216 AND 217 bm_name.meta_key = 'name' AND bm_description.meta_key = 'description' 218 {$search_terms_sql} {$user_sql} {$include_sql} 219 " ); 220 221 $blog_ids = array(); 222 foreach ( (array) $paged_blogs as $blog ) { 223 $blog_ids[] = (int) $blog->blog_id; 224 } 225 226 $paged_blogs = BP_Blogs_Blog::get_blog_extras( $paged_blogs, $blog_ids, $type ); 227 228 if ( $update_meta_cache ) { 229 bp_blogs_update_meta_cache( $blog_ids ); 230 } 231 232 return array( 'blogs' => $paged_blogs, 'total' => $total_blogs ); 233 } 234 235 /** 236 * Delete the record of a given blog for all users. 237 * 238 * @param int $blog_id The blog being removed from all users. 239 * @return int|bool Number of rows deleted on success, false on failure. 240 */ 241 public static function delete_blog_for_all( $blog_id ) { 242 global $wpdb; 243 244 bp_blogs_delete_blogmeta( $blog_id ); 245 246 $bp = buddypress(); 247 248 return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name} WHERE blog_id = %d", $blog_id ) ); 249 } 250 251 /** 252 * Delete the record of a given blog for a specific user. 253 * 254 * @param int $blog_id The blog being removed. 255 * @param int $user_id Optional. The ID of the user from whom the blog 256 * is being removed. If absent, defaults to the logged-in user ID. 257 * @return int|bool Number of rows deleted on success, false on failure. 258 */ 259 public static function delete_blog_for_user( $blog_id, $user_id = null ) { 260 global $wpdb; 261 262 if ( !$user_id ) 263 $user_id = bp_loggedin_user_id(); 264 265 $bp = buddypress(); 266 267 return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name} WHERE user_id = %d AND blog_id = %d", $user_id, $blog_id ) ); 268 } 269 270 /** 271 * Delete all of a user's blog associations in the BP tables. 272 * 273 * @param int $user_id Optional. The ID of the user whose blog 274 * associations are being deleted. If absent, defaults to 275 * logged-in user ID. 276 * @return int|bool Number of rows deleted on success, false on failure. 277 */ 278 public static function delete_blogs_for_user( $user_id = null ) { 279 global $wpdb; 280 281 if ( !$user_id ) 282 $user_id = bp_loggedin_user_id(); 283 284 $bp = buddypress(); 285 286 return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->blogs->table_name} WHERE user_id = %d", $user_id ) ); 287 } 288 289 /** 290 * Get all of a user's blogs, as tracked by BuddyPress. 291 * 292 * Note that this is different from the WordPress function 293 * {@link get_blogs_of_user()}; the current method returns only those 294 * blogs that have been recorded by BuddyPress, while the WP function 295 * does a true query of a user's blog capabilities. 296 * 297 * @param int $user_id Optional. ID of the user whose blogs are being 298 * queried. Defaults to logged-in user. 299 * @param bool $show_hidden Optional. Whether to include blogs that are 300 * not marked public. Defaults to true when viewing one's own 301 * profile. 302 * @return array Multidimensional results array, structured as follows: 303 * 'blogs' - Array of located blog objects 304 * 'total' - A count of the total blogs for the user. 305 */ 306 public static function get_blogs_for_user( $user_id = 0, $show_hidden = false ) { 307 global $wpdb; 308 309 $bp = buddypress(); 310 311 if ( !$user_id ) 312 $user_id = bp_displayed_user_id(); 313 314 // Show logged in users their hidden blogs. 315 if ( !bp_is_my_profile() && !$show_hidden ) 316 $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 ) ); 317 else 318 $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 ) ); 319 320 $total_blog_count = BP_Blogs_Blog::total_blog_count_for_user( $user_id ); 321 322 $user_blogs = array(); 323 foreach ( (array) $blogs as $blog ) { 324 $user_blogs[$blog->blog_id] = new stdClass; 325 $user_blogs[$blog->blog_id]->id = $blog->id; 326 $user_blogs[$blog->blog_id]->blog_id = $blog->blog_id; 327 $user_blogs[$blog->blog_id]->siteurl = ( is_ssl() ) ? 'https://' . $blog->domain . $blog->path : 'http://' . $blog->domain . $blog->path; 328 $user_blogs[$blog->blog_id]->name = $blog->name; 329 } 330 331 return array( 'blogs' => $user_blogs, 'count' => $total_blog_count ); 332 } 333 334 /** 335 * Get IDs of all of a user's blogs, as tracked by BuddyPress. 336 * 337 * This method always includes hidden blogs. 338 * 339 * @param int $user_id Optional. ID of the user whose blogs are being 340 * queried. Defaults to logged-in user. 341 * @return int The number of blogs associated with the user. 342 */ 343 public static function get_blog_ids_for_user( $user_id = 0 ) { 344 global $wpdb; 345 346 $bp = buddypress(); 347 348 if ( !$user_id ) 349 $user_id = bp_displayed_user_id(); 350 351 return $wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM {$bp->blogs->table_name} WHERE user_id = %d", $user_id ) ); 352 } 353 354 /** 355 * Check whether a blog has been recorded by BuddyPress. 356 * 357 * @param int $blog_id ID of the blog being queried. 358 * @return int|null The ID of the first located entry in the BP table 359 * on success, otherwise null. 360 */ 361 public static function is_recorded( $blog_id ) { 362 global $wpdb; 363 364 $bp = buddypress(); 365 366 return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->blogs->table_name} WHERE blog_id = %d", $blog_id ) ); 367 } 368 369 /** 370 * Return a count of associated blogs for a given user. 371 * 372 * Includes hidden blogs when the logged-in user is the same as the 373 * $user_id parameter, or when the logged-in user has the bp_moderate 374 * cap. 375 * 376 * @param int $user_id Optional. ID of the user whose blogs are being 377 * queried. Defaults to logged-in user. 378 * @return int Blog count for the user. 379 */ 380 public static function total_blog_count_for_user( $user_id = null ) { 381 global $wpdb; 382 383 $bp = buddypress(); 384 385 if ( !$user_id ) 386 $user_id = bp_displayed_user_id(); 387 388 // If the user is logged in return the blog count including their hidden blogs. 389 if ( ( is_user_logged_in() && $user_id == bp_loggedin_user_id() ) || bp_current_user_can( 'bp_moderate' ) ) { 390 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 ) ); 391 } else { 392 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 ) ); 393 } 394 } 395 396 /** 397 * Return a list of blogs matching a search term. 398 * 399 * Matches against blog names and descriptions, as stored in the BP 400 * blogmeta table. 401 * 402 * @param string $filter The search term. 403 * @param int $limit Optional. The maximum number of items to return. 404 * Default: null (no limit). 405 * @param int $page Optional. The page of results to return. Default: 406 * null (no limit). 407 * @return array Multidimensional results array, structured as follows: 408 * 'blogs' - Array of located blog objects 409 * 'total' - A count of the total blogs matching the query. 410 */ 411 public static function search_blogs( $filter, $limit = null, $page = null ) { 412 global $wpdb; 413 414 $search_terms_like = '%' . bp_esc_like( $filter ) . '%'; 415 $search_terms_sql = $wpdb->prepare( 'bm.meta_value LIKE %s', $search_terms_like ); 416 417 $hidden_sql = ''; 418 if ( !bp_current_user_can( 'bp_moderate' ) ) 419 $hidden_sql = "AND wb.public = 1"; 420 421 $pag_sql = ''; 422 if ( $limit && $page ) { 423 $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 424 } 425 426 $bp = buddypress(); 427 428 $paged_blogs = $wpdb->get_results( "SELECT DISTINCT bm.blog_id FROM {$bp->blogs->table_name_blogmeta} bm LEFT JOIN {$wpdb->base_prefix}blogs wb ON bm.blog_id = wb.blog_id WHERE ( ( bm.meta_key = 'name' OR bm.meta_key = 'description' ) AND {$search_terms_sql} ) {$hidden_sql} AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY meta_value ASC{$pag_sql}" ); 429 $total_blogs = $wpdb->get_var( "SELECT COUNT(DISTINCT bm.blog_id) FROM {$bp->blogs->table_name_blogmeta} bm LEFT JOIN {$wpdb->base_prefix}blogs wb ON bm.blog_id = wb.blog_id WHERE ( ( bm.meta_key = 'name' OR bm.meta_key = 'description' ) AND {$search_terms_sql} ) {$hidden_sql} AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY meta_value ASC" ); 430 431 return array( 'blogs' => $paged_blogs, 'total' => $total_blogs ); 432 } 433 434 /** 435 * Retrieve a list of all blogs. 436 * 437 * Query will include hidden blogs if the logged-in user has the 438 * 'bp_moderate' cap. 439 * 440 * @param int $limit Optional. The maximum number of items to return. 441 * Default: null (no limit). 442 * @param int $page Optional. The page of results to return. Default: 443 * null (no limit). 444 * @return array Multidimensional results array, structured as follows: 445 * 'blogs' - Array of located blog objects 446 * 'total' - A count of the total blogs. 447 */ 448 public static function get_all( $limit = null, $page = null ) { 449 global $wpdb; 450 451 $bp = buddypress(); 452 453 $hidden_sql = !bp_current_user_can( 'bp_moderate' ) ? "AND wb.public = 1" : ''; 454 $pag_sql = ( $limit && $page ) ? $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ) : ''; 455 456 $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}" ); 457 $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}" ); 458 459 return array( 'blogs' => $paged_blogs, 'total' => $total_blogs ); 460 } 461 462 /** 463 * Retrieve a list of blogs whose names start with a given letter. 464 * 465 * Query will include hidden blogs if the logged-in user has the 466 * 'bp_moderate' cap. 467 * 468 * @param string $letter. The letter you're looking for. 469 * @param int $limit Optional. The maximum number of items to return. 470 * Default: null (no limit). 471 * @param int $page Optional. The page of results to return. Default: 472 * null (no limit). 473 * @return array Multidimensional results array, structured as follows: 474 * 'blogs' - Array of located blog objects. 475 * 'total' - A count of the total blogs matching the query. 476 */ 477 public static function get_by_letter( $letter, $limit = null, $page = null ) { 478 global $wpdb; 479 480 $bp = buddypress(); 481 482 $letter_like = '%' . bp_esc_like( $letter ) . '%'; 483 $letter_sql = $wpdb->prepare( 'bm.meta_value LIKE %s', $letter_like ); 484 485 $hidden_sql = ''; 486 if ( !bp_current_user_can( 'bp_moderate' ) ) 487 $hidden_sql = "AND wb.public = 1"; 488 489 $pag_sql = ''; 490 if ( $limit && $page ) 491 $pag_sql = $wpdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) ); 492 493 $paged_blogs = $wpdb->get_results( "SELECT DISTINCT bm.blog_id FROM {$bp->blogs->table_name_blogmeta} bm LEFT JOIN {$wpdb->base_prefix}blogs wb ON bm.blog_id = wb.blog_id WHERE bm.meta_key = 'name' AND {$letter_sql} {$hidden_sql} AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY bm.meta_value ASC{$pag_sql}" ); 494 $total_blogs = $wpdb->get_var( "SELECT COUNT(DISTINCT bm.blog_id) FROM {$bp->blogs->table_name_blogmeta} bm LEFT JOIN {$wpdb->base_prefix}blogs wb ON bm.blog_id = wb.blog_id WHERE bm.meta_key = 'name' AND {$letter_sql} {$hidden_sql} AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY bm.meta_value ASC" ); 495 496 return array( 'blogs' => $paged_blogs, 'total' => $total_blogs ); 497 } 498 499 /** 500 * Fetch blog data not caught in the main query and append it to results array. 501 * 502 * Gets the following information, which is either unavailable at the 503 * time of the original query, or is more efficient to look up in one 504 * fell swoop: 505 * - The latest post for each blog, include Featured Image data 506 * - The blog description 507 * 508 * @param array $paged_blogs Array of results from the original query. 509 * @param array $blog_ids Array of IDs returned from the original query. 510 * @param string|bool $type Not currently used. Default: false. 511 * @return array $paged_blogs The located blogs array, with the extras added. 512 */ 513 public static function get_blog_extras( &$paged_blogs, &$blog_ids, $type = false ) { 514 global $wpdb; 515 516 $bp = buddypress(); 517 518 if ( empty( $blog_ids ) ) 519 return $paged_blogs; 520 521 $blog_ids = implode( ',', wp_parse_id_list( $blog_ids ) ); 522 523 for ( $i = 0, $count = count( $paged_blogs ); $i < $count; ++$i ) { 524 $blog_prefix = $wpdb->get_blog_prefix( $paged_blogs[$i]->blog_id ); 525 $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" ); 526 $images = array(); 527 528 // Add URLs to any Featured Image this post might have 529 if ( ! empty( $paged_blogs[$i]->latest_post ) && has_post_thumbnail( $paged_blogs[$i]->latest_post->ID ) ) { 530 531 // Grab 4 sizes of the image. Thumbnail. 532 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'thumbnail', false ); 533 if ( ! empty( $image ) ) 534 $images['thumbnail'] = $image[0]; 535 536 // Medium 537 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'medium', false ); 538 if ( ! empty( $image ) ) 539 $images['medium'] = $image[0]; 540 541 // Large 542 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'large', false ); 543 if ( ! empty( $image ) ) 544 $images['large'] = $image[0]; 545 546 // Post thumbnail 547 $image = wp_get_attachment_image_src( get_post_thumbnail_id( $paged_blogs[$i]->latest_post->ID ), 'post-thumbnail', false ); 548 if ( ! empty( $image ) ) 549 $images['post-thumbnail'] = $image[0]; 550 551 // Add the images to the latest_post object 552 $paged_blogs[$i]->latest_post->images = $images; 553 } 554 } 555 556 /* Fetch the blog description for each blog (as it may be empty we can't fetch it in the main query). */ 557 $blog_descs = $wpdb->get_results( "SELECT blog_id, meta_value as description FROM {$bp->blogs->table_name_blogmeta} WHERE meta_key = 'description' AND blog_id IN ( {$blog_ids} )" ); 558 559 for ( $i = 0, $count = count( $paged_blogs ); $i < $count; ++$i ) { 560 foreach ( (array) $blog_descs as $desc ) { 561 if ( $desc->blog_id == $paged_blogs[$i]->blog_id ) 562 $paged_blogs[$i]->description = $desc->description; 563 } 564 } 565 566 return $paged_blogs; 567 } 568 569 /** 570 * Check whether a given blog is hidden. 571 * 572 * Checks the 'public' column in the wp_blogs table. 573 * 574 * @param int $blog_id The ID of the blog being checked. 575 * @return bool True if hidden (public = 0), false otherwise. 576 */ 577 public static function is_hidden( $blog_id ) { 578 global $wpdb; 579 580 if ( !(int) $wpdb->get_var( $wpdb->prepare( "SELECT public FROM {$wpdb->base_prefix}blogs WHERE blog_id = %d", $blog_id ) ) ) { 581 return true; 582 } 583 584 return false; 585 } 586 587 /** 588 * Get ID of user-blog link. 589 * 590 * @param int $user_id ID of user. 591 * @param int $blog_id ID of blog. 592 * @return int|bool ID of user-blog link, or false if not found. 593 */ 594 public static function get_user_blog( $user_id, $blog_id ) { 595 global $wpdb; 596 597 $bp = buddypress(); 598 599 $user_blog = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->blogs->table_name} WHERE user_id = %d AND blog_id = %d", $user_id, $blog_id ) ); 600 601 if ( empty( $user_blog ) ) { 602 $user_blog = false; 603 } else { 604 $user_blog = intval( $user_blog ); 605 } 606 607 return $user_blog; 608 } 609 } 13 require __DIR__ . '/classes/class-bp-blogs-blog.php';
Note: See TracChangeset
for help on using the changeset viewer.