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