[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Blogs component functions. 4 * 5 * @package BuddyPress 6 * @subpackage BlogsFunctions 7 * @since 1.5.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 /** 14 * Check whether the $bp global lists an activity directory page. 15 * 16 * @since 1.5.0 17 * 18 * @return bool True if set, false if empty. 19 */ 20 function bp_blogs_has_directory() { 21 $bp = buddypress(); 22 23 return (bool) !empty( $bp->pages->blogs->id ); 24 } 25 26 /** 27 * Retrieve a set of blogs. 28 * 29 * @see BP_Blogs_Blog::get() for a description of arguments and return value. 30 * 31 * @param array|string $args { 32 * Arguments are listed here with their default values. For more 33 * information about the arguments, see {@link BP_Blogs_Blog::get()}. 34 * @type string $type Default: 'active'. 35 * @type int|bool $user_id Default: false. 36 * @type array $include_blog_ids Default: false. 37 * @type string|bool $search_terms Default: false. 38 * @type int $per_page Default: 20. 39 * @type int $page Default: 1. 40 * @type bool $update_meta_cache Whether to pre-fetch blogmeta. Default: true. 41 * } 42 * @return array See {@link BP_Blogs_Blog::get()}. 43 */ 44 function bp_blogs_get_blogs( $args = '' ) { 45 46 // Parse query arguments. 47 $r = bp_parse_args( $args, array( 48 'type' => 'active', // 'active', 'alphabetical', 'newest', or 'random'. 49 'include_blog_ids' => false, // Array of blog IDs to include. 50 'user_id' => false, // Limit to blogs this user can post to. 51 'search_terms' => false, // Limit to blogs matching these search terms. 52 'per_page' => 20, // The number of results to return per page. 53 'page' => 1, // The page to return if limiting per page. 54 'update_meta_cache' => true // Whether to pre-fetch blogmeta. 55 ), 'blogs_get_blogs' ); 56 57 // Get the blogs. 58 $blogs = BP_Blogs_Blog::get( 59 $r['type'], 60 $r['per_page'], 61 $r['page'], 62 $r['user_id'], 63 $r['search_terms'], 64 $r['update_meta_cache'], 65 $r['include_blog_ids'] 66 ); 67 68 /** 69 * Filters a set of blogs. 70 * 71 * @since 1.2.0 72 * 73 * @param array $blogs Array of blog data. 74 * @param array $r Parsed query arguments. 75 */ 76 return apply_filters( 'bp_blogs_get_blogs', $blogs, $r ); 77 } 78 79 /** 80 * Populate the BP blogs table with existing blogs. 81 * 82 * Warning: By default, this will remove all existing records from the BP 83 * blogs and blogmeta tables before re-populating the tables. 84 * 85 * @since 1.0.0 86 * @since 2.6.0 Accepts $args as a parameter. 87 * 88 * @param array $args { 89 * Array of arguments. 90 * @type int $offset The offset to use. 91 * @type int $limit The number of blogs to record at one time. 92 * @type array $blog_ids Blog IDs to record. If empty, all blogs will be recorded. 93 * @type array $site_id The network site ID to use. 94 * } 95 * 96 * @return bool 97 */ 98 function bp_blogs_record_existing_blogs( $args = array() ) { 99 global $wpdb; 100 101 // Query for all sites in network. 102 $r = bp_parse_args( $args, array( 103 'offset' => (int) bp_get_option( '_bp_record_blogs_offset' ), 104 'limit' => 50, 105 'blog_ids' => array(), 106 'site_id' => $wpdb->siteid 107 ), 'record_existing_blogs' ); 108 109 // Truncate all BP blogs tables if starting fresh. 110 if ( empty( $r['offset'] ) && empty( $r['blog_ids'] ) ) { 111 $bp = buddypress(); 112 113 // Truncate user blogs table. 114 $truncate = $wpdb->query( "TRUNCATE {$bp->blogs->table_name}" ); 115 if ( is_wp_error( $truncate ) ) { 116 return false; 117 } 118 119 // Truncate user blogmeta table. 120 $truncate = $wpdb->query( "TRUNCATE {$bp->blogs->table_name_blogmeta}" ); 121 if ( is_wp_error( $truncate ) ) { 122 return false; 123 } 124 } 125 126 // Multisite. 127 if ( is_multisite() ) { 128 $sql = array(); 129 $sql['select'] = $wpdb->prepare( "SELECT blog_id, last_updated FROM {$wpdb->base_prefix}blogs WHERE mature = 0 AND spam = 0 AND deleted = 0 AND site_id = %d", $r['site_id'] ); 130 131 // Omit root blog if large network. 132 if ( bp_is_large_install() ) { 133 $sql['omit_root_blog'] = $wpdb->prepare( "AND blog_id != %d", bp_get_root_blog_id() ); 134 } 135 136 // Filter by selected blog IDs. 137 if ( ! empty( $r['blog_ids'] ) ) { 138 $in = implode( ',', wp_parse_id_list( $r['blog_ids'] ) ); 139 $sql['in'] = "AND blog_id IN ({$in})"; 140 } 141 142 $sql['orderby'] = 'ORDER BY blog_id ASC'; 143 144 $sql['limit'] = $wpdb->prepare( "LIMIT %d", $r['limit'] ); 145 146 if ( ! empty( $r['offset'] ) ) { 147 $sql['offset'] = $wpdb->prepare( "OFFSET %d", $r['offset'] ); 148 } 149 150 $blogs = $wpdb->get_results( implode( ' ', $sql ) ); 151 152 // Record a single site. 153 } else { 154 // Just record blog for the current user only. 155 $record = bp_blogs_record_blog( $wpdb->blogid, get_current_user_id(), true ); 156 157 if ( false === $record ) { 158 return false; 159 } else { 160 return true; 161 } 162 } 163 164 // Bail if there are no blogs. 165 if ( empty( $blogs ) ) { 166 // Make sure we remove our offset marker. 167 if ( is_multisite() ) { 168 bp_delete_option( '_bp_record_blogs_offset' ); 169 } 170 171 return false; 172 } 173 174 // Loop through users of blogs and record the relationship. 175 foreach ( (array) $blogs as $blog ) { 176 177 // Ensure that the cache is clear after the table TRUNCATE above. 178 wp_cache_delete( $blog->blog_id, 'bp_blog_meta' ); 179 180 // Get all users. 181 $users = get_users( array( 182 'blog_id' => $blog->blog_id, 183 'fields' => 'ID' 184 ) ); 185 186 // Continue on if no users exist for this site (how did this happen?). 187 if ( empty( $users ) ) { 188 continue; 189 } 190 191 // Loop through users and record their relationship to this blog. 192 foreach ( (array) $users as $user_id ) { 193 bp_blogs_add_user_to_blog( $user_id, false, $blog->blog_id ); 194 195 // Clear cache. 196 bp_blogs_clear_blog_object_cache( $blog->blog_id, $user_id ); 197 } 198 199 // Update blog last activity timestamp. 200 if ( ! empty( $blog->last_updated ) && false !== strtotime( $blog->last_updated ) ) { 201 bp_blogs_update_blogmeta( $blog->blog_id, 'last_activity', $blog->last_updated ); 202 } 203 } 204 205 // See if we need to do this again. 206 if ( is_multisite() && empty( $r['blog_ids'] ) ) { 207 $sql['offset'] = $wpdb->prepare( " OFFSET %d", $r['limit'] + $r['offset'] ); 208 209 // Check if there are more blogs to record. 210 $blog_ids = $wpdb->get_results( implode( ' ', $sql ) ); 211 212 // We have more blogs; record offset and re-run function. 213 if ( ! empty( $blog_ids ) ) { 214 bp_update_option( '_bp_record_blogs_offset', $r['limit'] + $r['offset'] ); 215 bp_blogs_record_existing_blogs( array( 216 'offset' => $r['limit'] + $r['offset'], 217 'limit' => $r['limit'], 218 'blog_ids' => $r['blog_ids'], 219 'site_id' => $r['site_id'] 220 ) ); 221 222 // Bail since we have more blogs to record. 223 return; 224 225 // No more blogs; delete offset marker. 226 } else { 227 bp_delete_option( '_bp_record_blogs_offset' ); 228 } 229 } 230 231 /** 232 * Fires after the BP blogs tables have been populated with existing blogs. 233 * 234 * @since 2.4.0 235 */ 236 do_action( 'bp_blogs_recorded_existing_blogs' ); 237 238 // No errors. 239 return true; 240 } 241 242 /** 243 * Check whether a given blog should be recorded in activity streams. 244 * 245 * If $user_id is provided, you can restrict site from being recordable 246 * only to particular users. 247 * 248 * @since 1.7.0 249 * 250 * @param int $blog_id ID of the blog being checked. 251 * @param int $user_id Optional. ID of the user for whom access is being checked. 252 * @return bool True if blog is recordable, otherwise false. 253 */ 254 function bp_blogs_is_blog_recordable( $blog_id, $user_id = 0 ) { 255 256 /** 257 * Filters whether or not a blog is globally activity stream recordable. 258 * 259 * @since 1.7.0 260 * 261 * @param bool $value Whether or not recordable. Default true. 262 * @param int $blog_id Current blog ID. 263 */ 264 $recordable_globally = apply_filters( 'bp_blogs_is_blog_recordable', true, $blog_id ); 265 266 if ( !empty( $user_id ) ) { 267 /** 268 * Filters whether or not a blog is globally activity stream recordable for user. 269 * 270 * @since 1.7.0 271 * 272 * @param bool $recordable_globally Whether or not recordable. 273 * @param int $blog_id Current blog ID. 274 * @param int $user_id Current user ID. 275 */ 276 $recordable_for_user = apply_filters( 'bp_blogs_is_blog_recordable_for_user', $recordable_globally, $blog_id, $user_id ); 277 } else { 278 $recordable_for_user = $recordable_globally; 279 } 280 281 if ( !empty( $recordable_for_user ) ) { 282 return true; 283 } 284 285 return $recordable_globally; 286 } 287 288 /** 289 * Check whether a given blog should be tracked by the Blogs component. 290 * 291 * If $user_id is provided, the developer can restrict site from 292 * being trackable only to particular users. 293 * 294 * @since 1.7.0 295 * 296 * @param int $blog_id ID of the blog being checked. 297 * @param int $user_id Optional. ID of the user for whom access is being checked. 298 * @return bool True if blog is trackable, otherwise false. 299 */ 300 function bp_blogs_is_blog_trackable( $blog_id, $user_id = 0 ) { 301 302 /** 303 * Filters whether or not a blog is globally trackable. 304 * 305 * @since 1.7.0 306 * 307 * @param bool $value Whether or not trackable. 308 * @param int $blog_id Current blog ID. 309 */ 310 $trackable_globally = apply_filters( 'bp_blogs_is_blog_trackable', bp_blogs_is_blog_recordable( $blog_id, $user_id ), $blog_id ); 311 312 if ( !empty( $user_id ) ) { 313 314 /** 315 * Filters whether or not a blog is globally trackable for user. 316 * 317 * @since 1.7.0 318 * 319 * @param bool $value Whether or not trackable. 320 * @param int $blog_id Current blog ID. 321 * @param int $user_id Current user ID. 322 */ 323 $trackable_for_user = apply_filters( 'bp_blogs_is_blog_trackable_for_user', $trackable_globally, $blog_id, $user_id ); 324 } else { 325 $trackable_for_user = $trackable_globally; 326 } 327 328 if ( !empty( $trackable_for_user ) ) { 329 return $trackable_for_user; 330 } 331 332 return $trackable_globally; 333 } 334 335 /** 336 * Make BuddyPress aware of a new site so that it can track its activity. 337 * 338 * @since 1.0.0 339 * 340 * @param int $blog_id ID of the blog being recorded. 341 * @param int $user_id ID of the user for whom the blog is being recorded. 342 * @param bool $no_activity Optional. Whether to skip recording an activity 343 * item about this blog creation. Default: false. 344 * @return false|null Returns false on failure. 345 */ 346 function bp_blogs_record_blog( $blog_id, $user_id, $no_activity = false ) { 347 348 if ( empty( $user_id ) ) 349 $user_id = bp_loggedin_user_id(); 350 351 // If blog is not recordable, do not record the activity. 352 if ( !bp_blogs_is_blog_recordable( $blog_id, $user_id ) ) 353 return false; 354 355 $name = get_blog_option( $blog_id, 'blogname' ); 356 $url = get_home_url( $blog_id ); 357 358 if ( empty( $name ) ) { 359 $name = $url; 360 } 361 362 $description = get_blog_option( $blog_id, 'blogdescription' ); 363 $close_old_posts = get_blog_option( $blog_id, 'close_comments_for_old_posts' ); 364 $close_days_old = get_blog_option( $blog_id, 'close_comments_days_old' ); 365 $moderation = get_blog_option( $blog_id, 'comment_moderation' ); 366 367 $thread_depth = get_blog_option( $blog_id, 'thread_comments' ); 368 if ( ! empty( $thread_depth ) ) { 369 $thread_depth = get_blog_option( $blog_id, 'thread_comments_depth' ); 370 } else { 371 // Perhaps filter this? 372 $thread_depth = 1; 373 } 374 375 $recorded_blog = new BP_Blogs_Blog; 376 $recorded_blog->user_id = $user_id; 377 $recorded_blog->blog_id = $blog_id; 378 $recorded_blog_id = $recorded_blog->save(); 379 $is_recorded = !empty( $recorded_blog_id ) ? true : false; 380 381 bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'url', $url ); 382 bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'name', $name ); 383 bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'description', $description ); 384 bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'last_activity', bp_core_current_time() ); 385 bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'close_comments_for_old_posts', $close_old_posts ); 386 bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'close_comments_days_old', $close_days_old ); 387 bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'thread_comments_depth', $thread_depth ); 388 bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'comment_moderation', $moderation ); 389 390 $is_private = !empty( $_POST['blog_public'] ) && (int) $_POST['blog_public'] ? false : true; 391 392 /** 393 * Filters whether or not a new blog is public. 394 * 395 * @since 1.5.0 396 * 397 * @param bool $is_private Whether or not blog is public. 398 */ 399 $is_private = !apply_filters( 'bp_is_new_blog_public', !$is_private ); 400 401 /** 402 * Fires after BuddyPress has been made aware of a new site for activity tracking. 403 * 404 * @since 1.0.0 405 * @since 2.6.0 Added $no_activity as a parameter. 406 * 407 * @param BP_Blogs_Blog $recorded_blog Current blog being recorded. Passed by reference. 408 * @param bool $is_private Whether or not the current blog being recorded is private. 409 * @param bool $is_recorded Whether or not the current blog was recorded. 410 * @param bool $no_activity Whether to skip recording an activity item for this blog creation. 411 */ 412 do_action_ref_array( 'bp_blogs_new_blog', array( &$recorded_blog, $is_private, $is_recorded, $no_activity ) ); 413 } 414 add_action( 'bp_insert_site', 'bp_blogs_record_blog', 10, 2 ); 415 416 /** 417 * Update blog name in BuddyPress blogmeta table. 418 * 419 * @global object $wpdb DB Layer. 420 * 421 * @param string $oldvalue Value before save. Passed by do_action() but 422 * unused here. 423 * @param string $newvalue Value to change meta to. 424 */ 425 function bp_blogs_update_option_blogname( $oldvalue, $newvalue ) { 426 global $wpdb; 427 428 bp_blogs_update_blogmeta( $wpdb->blogid, 'name', $newvalue ); 429 } 430 add_action( 'update_option_blogname', 'bp_blogs_update_option_blogname', 10, 2 ); 431 432 /** 433 * Update blog description in BuddyPress blogmeta table. 434 * 435 * @global object $wpdb DB Layer. 436 * 437 * @param string $oldvalue Value before save. Passed by do_action() but 438 * unused here. 439 * @param string $newvalue Value to change meta to. 440 */ 441 function bp_blogs_update_option_blogdescription( $oldvalue, $newvalue ) { 442 global $wpdb; 443 444 bp_blogs_update_blogmeta( $wpdb->blogid, 'description', $newvalue ); 445 } 446 add_action( 'update_option_blogdescription', 'bp_blogs_update_option_blogdescription', 10, 2 ); 447 448 /** 449 * Update "Close comments for old posts" option in BuddyPress blogmeta table. 450 * 451 * @since 2.0.0 452 * 453 * @global object $wpdb DB Layer. 454 * 455 * @param string $oldvalue Value before save. Passed by do_action() but 456 * unused here. 457 * @param string $newvalue Value to change meta to. 458 */ 459 function bp_blogs_update_option_close_comments_for_old_posts( $oldvalue, $newvalue ) { 460 global $wpdb; 461 462 bp_blogs_update_blogmeta( $wpdb->blogid, 'close_comments_for_old_posts', $newvalue ); 463 } 464 add_action( 'update_option_close_comments_for_old_posts', 'bp_blogs_update_option_close_comments_for_old_posts', 10, 2 ); 465 466 /** 467 * Update "Close comments after days old" option in BuddyPress blogmeta table. 468 * 469 * @since 2.0.0 470 * 471 * @global object $wpdb DB Layer. 472 * 473 * @param string $oldvalue Value before save. Passed by do_action() but 474 * unused here. 475 * @param string $newvalue Value to change meta to. 476 */ 477 function bp_blogs_update_option_close_comments_days_old( $oldvalue, $newvalue ) { 478 global $wpdb; 479 480 bp_blogs_update_blogmeta( $wpdb->blogid, 'close_comments_days_old', $newvalue ); 481 } 482 add_action( 'update_option_close_comments_days_old', 'bp_blogs_update_option_close_comments_days_old', 10, 2 ); 483 484 /** 485 * When toggling threaded comments, update thread depth in blogmeta table. 486 * 487 * @since 2.0.0 488 * 489 * @global object $wpdb DB Layer. 490 * 491 * @param string $oldvalue Value before save. Passed by do_action() but 492 * unused here. 493 * @param string $newvalue Value to change meta to. 494 */ 495 function bp_blogs_update_option_thread_comments( $oldvalue, $newvalue ) { 496 global $wpdb; 497 498 if ( empty( $newvalue ) ) { 499 $thread_depth = 1; 500 } else { 501 $thread_depth = get_option( 'thread_comments_depth' ); 502 } 503 504 bp_blogs_update_blogmeta( $wpdb->blogid, 'thread_comments_depth', $thread_depth ); 505 } 506 add_action( 'update_option_thread_comments', 'bp_blogs_update_option_thread_comments', 10, 2 ); 507 508 /** 509 * When updating comment depth, update thread depth in blogmeta table. 510 * 511 * @since 2.0.0 512 * 513 * @global object $wpdb DB Layer. 514 * 515 * @param string $oldvalue Value before save. Passed by do_action() but 516 * unused here. 517 * @param string $newvalue Value to change meta to. 518 */ 519 function bp_blogs_update_option_thread_comments_depth( $oldvalue, $newvalue ) { 520 global $wpdb; 521 522 $comments_enabled = get_option( 'thread_comments' ); 523 524 if ( $comments_enabled ) { 525 bp_blogs_update_blogmeta( $wpdb->blogid, 'thread_comments_depth', $newvalue ); 526 } 527 } 528 add_action( 'update_option_thread_comments_depth', 'bp_blogs_update_option_thread_comments_depth', 10, 2 ); 529 530 /** 531 * When updating comment moderation, mirror value in blogmeta table. 532 * 533 * @since 3.0.0 534 * 535 * @param string $oldvalue Value before save. Passed by do_action() but unused here. 536 * @param string $newvalue Value to change meta to. 537 */ 538 function bp_blogs_update_option_comment_moderation( $oldvalue, $newvalue ) { 539 bp_blogs_update_blogmeta( $GLOBALS['wpdb']->blogid, 'comment_moderation', $newvalue ); 540 } 541 add_action( 'update_option_comment_moderation', 'bp_blogs_update_option_comment_moderation', 10, 2 ); 542 543 /** 544 * Syncs site icon URLs to blogmeta. 545 * 546 * @since 2.7.0 547 * 548 * @param int|string $old_value Old value 549 * @param int|string $new_value New value 550 */ 551 function bp_blogs_update_option_site_icon( $old_value, $new_value ) { 552 $blog_id = get_current_blog_id(); 553 554 if ( 0 === $new_value ) { 555 bp_blogs_update_blogmeta( $blog_id, 'site_icon_url_thumb', 0 ); 556 bp_blogs_update_blogmeta( $blog_id, 'site_icon_url_full', 0 ); 557 } else { 558 // Save site icon URL as blogmeta. 559 bp_blogs_update_blogmeta( $blog_id, 'site_icon_url_thumb', bp_blogs_get_site_icon_url( $blog_id, bp_core_avatar_thumb_width() ) ); 560 bp_blogs_update_blogmeta( $blog_id, 'site_icon_url_full', bp_blogs_get_site_icon_url( $blog_id, bp_core_avatar_full_width() ) ); 561 } 562 } 563 add_action( 'update_option_site_icon', 'bp_blogs_update_option_site_icon', 10, 2 ); 564 565 /** 566 * Deletes the 'url' blogmeta for a site. 567 * 568 * Fires when a site's details are updated, which generally happens when 569 * editing a site under "Network Admin > Sites". Prior to WP 4.9, the 570 * correct hook was 'refresh_blog_details'; afterward, 'clean_site_cache'. 571 * 572 * @since 2.3.0 573 * 574 * @param int $site_id The site ID. 575 */ 576 function bp_blogs_delete_url_blogmeta( $site_id = 0 ) { 577 bp_blogs_delete_blogmeta( (int) $site_id, 'url' ); 578 } 579 580 if ( bp_is_running_wp( '4.9.0' ) ) { 581 add_action( 'clean_site_cache', 'bp_blogs_delete_url_blogmeta' ); 582 } else { 583 add_action( 'refresh_blog_details', 'bp_blogs_delete_url_blogmeta' ); 584 } 585 586 /** 587 * Record activity metadata about a published blog post. 588 * 589 * @since 2.2.0 590 * 591 * @param int $activity_id ID of the activity item. 592 * @param WP_Post $post Post object. 593 * @param array $args Array of arguments. 594 */ 595 function bp_blogs_publish_post_activity_meta( $activity_id, $post, $args ) { 596 if ( empty( $activity_id ) || 'post' != $post->post_type ) { 597 return; 598 } 599 600 bp_activity_update_meta( $activity_id, 'post_title', $post->post_title ); 601 602 if ( ! empty( $args['post_url'] ) ) { 603 $post_permalink = $args['post_url']; 604 } else { 605 $post_permalink = $post->guid; 606 } 607 608 bp_activity_update_meta( $activity_id, 'post_url', $post_permalink ); 609 610 // Update the blog's last activity. 611 bp_blogs_update_blogmeta( $args['item_id'], 'last_activity', bp_core_current_time() ); 612 613 /** 614 * Fires after BuddyPress has recorded metadata about a published blog post. 615 * 616 * @since 1.0.0 617 * 618 * @param int $ID ID of the blog post being recorded. 619 * @param WP_Post $post WP_Post object for the current blog post. 620 * @param string $value ID of the user associated with the current blog post. 621 */ 622 do_action( 'bp_blogs_new_blog_post', $post->ID, $post, $args['user_id'] ); 623 } 624 add_action( 'bp_activity_post_type_published', 'bp_blogs_publish_post_activity_meta', 10, 3 ); 625 626 /** 627 * Updates a blog post's activity meta entry during a post edit. 628 * 629 * @since 2.2.0 630 * @since 2.5.0 Add the post type tracking args object parameter 631 * 632 * @param WP_Post $post Post object. 633 * @param BP_Activity_Activity $activity Activity object. 634 * @param object $activity_post_object The post type tracking args object. 635 */ 636 function bp_blogs_update_post_activity_meta( $post, $activity, $activity_post_object ) { 637 if ( empty( $activity->id ) || empty( $activity_post_object->action_id ) ) { 638 return; 639 } 640 641 // Update post title in activity meta. 642 $existing_title = bp_activity_get_meta( $activity->id, 'post_title' ); 643 if ( $post->post_title !== $existing_title ) { 644 bp_activity_update_meta( $activity->id, 'post_title', $post->post_title ); 645 646 if ( ! empty( $activity_post_object->comments_tracking->action_id ) ) { 647 // Now update activity meta for post comments... sigh. 648 add_filter( 'comments_clauses', 'bp_blogs_comments_clauses_select_by_id' ); 649 $comments = get_comments( array( 'post_id' => $post->ID ) ); 650 remove_filter( 'comments_clauses', 'bp_blogs_comments_clauses_select_by_id' ); 651 652 if ( ! empty( $comments ) ) { 653 $activity_ids = array(); 654 $comment_ids = wp_list_pluck( $comments, 'comment_ID' ); 655 656 // Set up activity args. 657 $args = array( 658 'update_meta_cache' => false, 659 'show_hidden' => true, 660 'per_page' => 99999, 661 ); 662 663 // Query for old-style "new_blog_comment" activity items. 664 $args['filter'] = array( 665 'object' => $activity_post_object->comments_tracking->component_id, 666 'action' => $activity_post_object->comments_tracking->action_id, 667 'primary_id' => get_current_blog_id(), 668 'secondary_id' => implode( ',', $comment_ids ), 669 ); 670 671 $activities = bp_activity_get( $args ); 672 if ( ! empty( $activities['activities'] ) ) { 673 $activity_ids = (array) wp_list_pluck( $activities['activities'], 'id' ); 674 } 675 676 // Query for activity comments connected to a blog post. 677 unset( $args['filter'] ); 678 $args['meta_query'] = array( array( 679 'key' => 'bp_blogs_' . $post->post_type . '_comment_id', 680 'value' => $comment_ids, 681 'compare' => 'IN', 682 ) ); 683 $args['type'] = 'activity_comment'; 684 $args['display_comments'] = 'stream'; 685 686 $activities = bp_activity_get( $args ); 687 if ( ! empty( $activities['activities'] ) ) { 688 $activity_ids = array_merge( $activity_ids, (array) wp_list_pluck( $activities['activities'], 'id' ) ); 689 } 690 691 // Update activity meta for all found activity items. 692 if ( ! empty( $activity_ids ) ) { 693 foreach ( $activity_ids as $aid ) { 694 bp_activity_update_meta( $aid, 'post_title', $post->post_title ); 695 } 696 } 697 698 unset( $activities, $activity_ids, $comment_ids, $comments ); 699 } 700 } 701 } 702 703 // Add post comment status to activity meta if closed. 704 if( 'closed' == $post->comment_status ) { 705 bp_activity_update_meta( $activity->id, 'post_comment_status', $post->comment_status ); 706 } else { 707 bp_activity_delete_meta( $activity->id, 'post_comment_status' ); 708 } 709 } 710 add_action( 'bp_activity_post_type_updated', 'bp_blogs_update_post_activity_meta', 10, 3 ); 711 712 /** 713 * Update Activity and blogs meta and eventually sync comment with activity comment 714 * 715 * @since 2.5.0 716 * 717 * @param int|bool $activity_id ID of recorded activity, or false if sync is active. 718 * @param WP_Comment|null $comment The comment object. 719 * @param array $activity_args Array of activity arguments. 720 * @param object|null $activity_post_object The post type tracking args object. 721 * @return WP_Error|bool|int Returns false if no activity, the activity id otherwise. 722 */ 723 function bp_blogs_comment_sync_activity_comment( &$activity_id, $comment = null, $activity_args = array(), $activity_post_object = null ) { 724 if ( empty( $activity_args ) || empty( $comment->post->ID ) || empty( $activity_post_object->comment_action_id ) ) { 725 return false; 726 } 727 728 // Set the current blog id. 729 $blog_id = get_current_blog_id(); 730 731 // These activity metadatas are used to build the new_blog_comment action string. 732 if ( ! empty( $activity_id ) && ! empty( $activity_args['item_id'] ) && 'new_blog_comment' === $activity_post_object->comment_action_id ) { 733 // Add some post info in activity meta. 734 bp_activity_update_meta( $activity_id, 'post_title', $comment->post->post_title ); 735 bp_activity_update_meta( $activity_id, 'post_url', esc_url_raw( add_query_arg( 'p', $comment->post->ID, home_url( '/' ) ) ) ); 736 } 737 738 // Sync comment - activity comment. 739 if ( ! bp_disable_blogforum_comments() ) { 740 741 if ( ! empty( $_REQUEST['action'] ) ) { 742 $existing_activity_id = get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true ); 743 744 if ( ! empty( $existing_activity_id ) ) { 745 $activity_args['id'] = $existing_activity_id; 746 } 747 } 748 749 if ( empty( $activity_post_object ) ) { 750 $activity_post_object = bp_activity_get_post_type_tracking_args( $comment->post->post_type ); 751 } 752 753 if ( isset( $activity_post_object->action_id ) && isset( $activity_post_object->component_id ) ) { 754 // Find the parent 'new_post_type' activity entry. 755 $parent_activity_id = bp_activity_get_activity_id( array( 756 'component' => $activity_post_object->component_id, 757 'type' => $activity_post_object->action_id, 758 'item_id' => $blog_id, 759 'secondary_item_id' => $comment->comment_post_ID 760 ) ); 761 762 // Try to create a new activity item for the parent blog post. 763 if ( empty( $parent_activity_id ) ) { 764 $parent_activity_id = bp_activity_post_type_publish( $comment->post->ID, $comment->post ); 765 } 766 } 767 768 // We found the parent activity entry 769 // so let's go ahead and reconfigure some activity args. 770 if ( ! empty( $parent_activity_id ) ) { 771 // Set the parent activity entry ID. 772 $activity_args['activity_id'] = $parent_activity_id; 773 774 // Now see if the WP parent comment has a BP activity ID. 775 $comment_parent = 0; 776 if ( ! empty( $comment->comment_parent ) ) { 777 $comment_parent = get_comment_meta( $comment->comment_parent, 'bp_activity_comment_id', true ); 778 } 779 780 // WP parent comment does not have a BP activity ID 781 // so set to 'new_' . post_type activity ID. 782 if ( empty( $comment_parent ) ) { 783 $comment_parent = $parent_activity_id; 784 } 785 786 $activity_args['parent_id'] = $comment_parent; 787 $activity_args['skip_notification'] = true; 788 789 // Could not find corresponding parent activity entry 790 // so wipe out $args array. 791 } else { 792 $activity_args = array(); 793 } 794 795 // Record in activity streams. 796 if ( ! empty( $activity_args ) ) { 797 $activity_id = bp_activity_new_comment( $activity_args ); 798 799 if ( empty( $activity_args['id'] ) ) { 800 // The activity metadata to inform about the corresponding comment ID. 801 bp_activity_update_meta( $activity_id, "bp_blogs_{$comment->post->post_type}_comment_id", $comment->comment_ID ); 802 803 // The comment metadata to inform about the corresponding activity ID. 804 add_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', $activity_id ); 805 806 // These activity metadatas are used to build the new_blog_comment action string. 807 if ( 'new_blog_comment' === $activity_post_object->comment_action_id ) { 808 bp_activity_update_meta( $activity_id, 'post_title', $comment->post->post_title ); 809 bp_activity_update_meta( $activity_id, 'post_url', esc_url_raw( add_query_arg( 'p', $comment->post->ID, home_url( '/' ) ) ) ); 810 } 811 } 812 813 /** 814 * Fires after an activity comment is added from a WP post comment. 815 * 816 * @since 2.6.0 817 * 818 * @param int $activity_id The activity comment ID. 819 * @param WP_Comment $post_type_comment WP Comment object. 820 * @param array $activity_args Activity comment arguments. 821 * @param object $activity_post_object The post type tracking args object. 822 */ 823 do_action( 'bp_blogs_comment_sync_activity_comment', $activity_id, $comment, $activity_args, $activity_post_object ); 824 } 825 } 826 827 // Update the blogs last active date 828 bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() ); 829 830 if ( 'new_blog_comment' === $activity_post_object->comment_action_id ) { 831 /** 832 * Fires after BuddyPress has recorded metadata about a published blog post comment. 833 * 834 * @since 2.5.0 835 * 836 * @param int $value Comment ID of the blog post comment being recorded. 837 * @param WP_Post $post WP_Comment object for the current blog post. 838 * @param string $value ID of the user associated with the current blog post comment. 839 */ 840 do_action( 'bp_blogs_new_blog_comment', $comment->comment_ID, $comment, bp_loggedin_user_id() ); 841 } 842 843 return $activity_id; 844 } 845 add_action( 'bp_activity_post_type_comment', 'bp_blogs_comment_sync_activity_comment', 10, 4 ); 846 847 /** 848 * Record a user's association with a blog. 849 * 850 * This function is hooked to several WordPress actions where blog roles are 851 * set/changed ('add_user_to_blog', 'profile_update', 'user_register'). It 852 * parses the changes, and records them as necessary in the BP blog tracker. 853 * 854 * BuddyPress does not track blogs for users with the 'subscriber' role by 855 * default, though as of 2.1.0 you can filter 'bp_blogs_get_allowed_roles' to 856 * modify this behavior. 857 * 858 * @param int $user_id The ID of the user. 859 * @param string|bool $role User's WordPress role for this blog ID. 860 * @param int $blog_id Blog ID user is being added to. 861 * @return false|null False on failure. 862 */ 863 function bp_blogs_add_user_to_blog( $user_id, $role = false, $blog_id = 0 ) { 864 global $wpdb; 865 866 // If no blog ID was passed, use the root blog ID. 867 if ( empty( $blog_id ) ) { 868 $blog_id = isset( $wpdb->blogid ) ? $wpdb->blogid : bp_get_root_blog_id(); 869 } 870 871 // If no role was passed, try to find the blog role. 872 if ( empty( $role ) ) { 873 874 // Get user capabilities. 875 $key = $wpdb->get_blog_prefix( $blog_id ). 'capabilities'; 876 $user_roles = array_keys( (array) bp_get_user_meta( $user_id, $key, true ) ); 877 878 // User has roles so lets. 879 if ( ! empty( $user_roles ) ) { 880 881 // Get blog roles. 882 $blog_roles = array_keys( bp_get_current_blog_roles() ); 883 884 // Look for blog only roles of the user. 885 $intersect_roles = array_intersect( $user_roles, $blog_roles ); 886 887 // If there's a role in the array, use the first one. This isn't 888 // very smart, but since roles aren't exactly hierarchical, and 889 // WordPress does not yet have a UI for multiple user roles, it's 890 // fine for now. 891 if ( ! empty( $intersect_roles ) ) { 892 $role = array_shift( $intersect_roles ); 893 } 894 } 895 } 896 897 // Bail if no role was found or role is not in the allowed roles array. 898 if ( empty( $role ) || ! in_array( $role, bp_blogs_get_allowed_roles() ) ) { 899 return false; 900 } 901 902 // Record the blog activity for this user being added to this blog. 903 bp_blogs_record_blog( $blog_id, $user_id, true ); 904 } 905 add_action( 'add_user_to_blog', 'bp_blogs_add_user_to_blog', 10, 3 ); 906 add_action( 'profile_update', 'bp_blogs_add_user_to_blog' ); 907 add_action( 'user_register', 'bp_blogs_add_user_to_blog' ); 908 909 /** 910 * The allowed blog roles a member must have to be recorded into the 911 * `bp_user_blogs` pointer table. 912 * 913 * This added and was made filterable in BuddyPress 2.1.0 to make it easier 914 * to extend the functionality of the Blogs component. 915 * 916 * @since 2.1.0 917 * 918 * @return string 919 */ 920 function bp_blogs_get_allowed_roles() { 921 922 /** 923 * Filters the allowed roles a member must have to be recorded into bp_user_blogs pointer table. 924 * 925 * @since 2.1.0 926 * 927 * @param array $value Array of potential roles user needs. 928 */ 929 return apply_filters( 'bp_blogs_get_allowed_roles', array( 'contributor', 'author', 'editor', 'administrator' ) ); 930 } 931 932 /** 933 * Remove a blog-user pair from BP's blog tracker. 934 * 935 * @param int $user_id ID of the user whose blog is being removed. 936 * @param int $blog_id Optional. ID of the blog being removed. Default: current blog ID. 937 */ 938 function bp_blogs_remove_user_from_blog( $user_id, $blog_id = 0 ) { 939 global $wpdb; 940 941 if ( empty( $blog_id ) ) { 942 $blog_id = $wpdb->blogid; 943 } 944 945 bp_blogs_remove_blog_for_user( $user_id, $blog_id ); 946 } 947 add_action( 'remove_user_from_blog', 'bp_blogs_remove_user_from_blog', 10, 2 ); 948 949 /** 950 * Rehook WP's maybe_add_existing_user_to_blog with a later priority. 951 * 952 * WordPress catches add-user-to-blog requests at init:10. In some cases, this 953 * can precede BP's Blogs component. This function bumps the priority of the 954 * core function, so that we can be sure that the Blogs component is loaded 955 * first. See https://buddypress.trac.wordpress.org/ticket/3916. 956 * 957 * @since 1.6.0 958 */ 959 function bp_blogs_maybe_add_user_to_blog() { 960 if ( ! is_multisite() ) 961 return; 962 963 remove_action( 'init', 'maybe_add_existing_user_to_blog' ); 964 add_action( 'init', 'maybe_add_existing_user_to_blog', 20 ); 965 } 966 add_action( 'init', 'bp_blogs_maybe_add_user_to_blog', 1 ); 967 968 /** 969 * Remove the "blog created" item from the BP blogs tracker and activity stream. 970 * 971 * @param int $blog_id ID of the blog being removed. 972 */ 973 function bp_blogs_remove_blog( $blog_id ) { 974 975 $blog_id = (int) $blog_id; 976 977 /** 978 * Fires before a "blog created" item is removed from blogs 979 * tracker and activity stream. 980 * 981 * @since 1.5.0 982 * 983 * @param int $blog_id ID of the blog having its item removed. 984 */ 985 do_action( 'bp_blogs_before_remove_blog', $blog_id ); 986 987 BP_Blogs_Blog::delete_blog_for_all( $blog_id ); 988 989 /** 990 * Fires after a "blog created" item has been removed from blogs 991 * tracker and activity stream. 992 * 993 * @since 1.0.0 994 * 995 * @param int $blog_id ID of the blog who had its item removed. 996 */ 997 do_action( 'bp_blogs_remove_blog', $blog_id ); 998 } 999 add_action( 'bp_delete_site', 'bp_blogs_remove_blog' ); 1000 1001 /** 1002 * Remove a blog from the tracker for a specific user. 1003 * 1004 * @param int $user_id ID of the user for whom the blog is being removed. 1005 * @param int $blog_id ID of the blog being removed. 1006 */ 1007 function bp_blogs_remove_blog_for_user( $user_id, $blog_id ) { 1008 1009 $blog_id = (int) $blog_id; 1010 $user_id = (int) $user_id; 1011 1012 /** 1013 * Fires before a blog is removed from the tracker for a specific user. 1014 * 1015 * @since 1.5.0 1016 * 1017 * @param int $blog_id ID of the blog being removed. 1018 * @param int $user_id ID of the user having the blog removed for. 1019 */ 1020 do_action( 'bp_blogs_before_remove_blog_for_user', $blog_id, $user_id ); 1021 1022 BP_Blogs_Blog::delete_blog_for_user( $blog_id, $user_id ); 1023 1024 /** 1025 * Fires after a blog has been removed from the tracker for a specific user. 1026 * 1027 * @since 1.0.0 1028 * 1029 * @param int $blog_id ID of the blog that was removed. 1030 * @param int $user_id ID of the user having the blog removed for. 1031 */ 1032 do_action( 'bp_blogs_remove_blog_for_user', $blog_id, $user_id ); 1033 } 1034 add_action( 'remove_user_from_blog', 'bp_blogs_remove_blog_for_user', 10, 2 ); 1035 1036 /** 1037 * Remove a synced activity comment from the activity stream. 1038 * 1039 * @since 2.5.0 1040 * 1041 * @param bool $deleted True when a comment post type activity was successfully removed. 1042 * @param int $comment_id ID of the comment to be removed. 1043 * @param object $activity_post_object The post type tracking args object. 1044 * @param string $activity_type The post type comment activity type. 1045 * 1046 * @return bool True on success. False on error. 1047 */ 1048 function bp_blogs_post_type_remove_comment( $deleted, $comment_id, $activity_post_object, $activity_type = '' ) { 1049 // Remove synced activity comments, if needed. 1050 if ( ! bp_disable_blogforum_comments() ) { 1051 // Get associated activity ID from comment meta 1052 $activity_id = get_comment_meta( $comment_id, 'bp_activity_comment_id', true ); 1053 1054 /** 1055 * Delete the associated activity comment & also remove 1056 * child post comments and associated activity comments. 1057 */ 1058 if ( ! empty( $activity_id ) ) { 1059 // Fetch the activity comments for the activity item. 1060 $activity = bp_activity_get( array( 1061 'in' => $activity_id, 1062 'display_comments' => 'stream', 1063 'spam' => 'all', 1064 ) ); 1065 1066 // Get all activity comment IDs for the pending deleted item. 1067 if ( ! empty( $activity['activities'] ) ) { 1068 $activity_ids = bp_activity_recurse_comments_activity_ids( $activity ); 1069 $activity_ids[] = $activity_id; 1070 1071 // Delete activity items. 1072 foreach ( $activity_ids as $activity_id ) { 1073 bp_activity_delete( array( 1074 'id' => $activity_id 1075 ) ); 1076 } 1077 1078 // Remove associated blog comments. 1079 bp_blogs_remove_associated_blog_comments( $activity_ids ); 1080 1081 // Rebuild activity comment tree. 1082 BP_Activity_Activity::rebuild_activity_comment_tree( $activity['activities'][0]->item_id ); 1083 1084 // Set the result. 1085 $deleted = true; 1086 } 1087 } 1088 } 1089 1090 // Backcompat for comments about the 'post' post type. 1091 if ( 'new_blog_comment' === $activity_type ) { 1092 /** 1093 * Fires after a blog comment activity item was removed from activity stream. 1094 * 1095 * @since 1.0.0 1096 * 1097 * @param int $value ID for the blog associated with the removed comment. 1098 * @param int $comment_id ID of the comment being removed. 1099 * @param int $value ID of the current logged in user. 1100 */ 1101 do_action( 'bp_blogs_remove_comment', get_current_blog_id(), $comment_id, bp_loggedin_user_id() ); 1102 } 1103 1104 return $deleted; 1105 } 1106 add_action( 'bp_activity_post_type_remove_comment', 'bp_blogs_post_type_remove_comment', 10, 4 ); 1107 1108 /** 1109 * Removes blog comments that are associated with activity comments. 1110 * 1111 * @since 2.0.0 1112 * 1113 * @see bp_blogs_remove_synced_comment() 1114 * @see bp_blogs_sync_delete_from_activity_comment() 1115 * 1116 * @param array $activity_ids The activity IDs to check association with blog 1117 * comments. 1118 * @param bool $force_delete Whether to force delete the comments. If false, 1119 * comments are trashed instead. 1120 */ 1121 function bp_blogs_remove_associated_blog_comments( $activity_ids = array(), $force_delete = true ) { 1122 // Query args. 1123 $query_args = array( 1124 'meta_query' => array( 1125 array( 1126 'key' => 'bp_activity_comment_id', 1127 'value' => implode( ',', (array) $activity_ids ), 1128 'compare' => 'IN', 1129 ) 1130 ) 1131 ); 1132 1133 // Get comment. 1134 $comment_query = new WP_Comment_Query; 1135 $comments = $comment_query->query( $query_args ); 1136 1137 // Found the corresponding comments 1138 // let's delete them! 1139 foreach ( $comments as $comment ) { 1140 wp_delete_comment( $comment->comment_ID, $force_delete ); 1141 1142 // If we're trashing the comment, remove the meta key as well. 1143 if ( empty( $force_delete ) ) { 1144 delete_comment_meta( $comment->comment_ID, 'bp_activity_comment_id' ); 1145 } 1146 } 1147 } 1148 1149 /** 1150 * Get the total number of blogs being tracked by BuddyPress. 1151 * 1152 * @return int $count Total blog count. 1153 */ 1154 function bp_blogs_total_blogs() { 1155 $count = wp_cache_get( 'bp_total_blogs', 'bp' ); 1156 1157 if ( false === $count ) { 1158 $blogs = BP_Blogs_Blog::get_all(); 1159 $count = $blogs['total']; 1160 wp_cache_set( 'bp_total_blogs', $count, 'bp' ); 1161 } 1162 return $count; 1163 } 1164 1165 /** 1166 * Get the total number of blogs being tracked by BP for a specific user. 1167 * 1168 * @since 1.2.0 1169 * 1170 * @param int $user_id ID of the user being queried. Default: on a user page, 1171 * the displayed user. Otherwise, the logged-in user. 1172 * @return int $count Total blog count for the user. 1173 */ 1174 function bp_blogs_total_blogs_for_user( $user_id = 0 ) { 1175 if ( empty( $user_id ) ) { 1176 $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id(); 1177 } 1178 1179 // No user ID? do not attempt to look at cache. 1180 if ( empty( $user_id ) ) { 1181 return 0; 1182 } 1183 1184 $count = wp_cache_get( 'bp_total_blogs_for_user_' . $user_id, 'bp' ); 1185 if ( false === $count ) { 1186 $count = BP_Blogs_Blog::total_blog_count_for_user( $user_id ); 1187 wp_cache_set( 'bp_total_blogs_for_user_' . $user_id, $count, 'bp' ); 1188 } 1189 1190 return $count; 1191 } 1192 1193 /** 1194 * Remove the all data related to a given blog from the BP blogs tracker and activity stream. 1195 * 1196 * @param int $blog_id The ID of the blog to expunge. 1197 */ 1198 function bp_blogs_remove_data_for_blog( $blog_id ) { 1199 1200 /** 1201 * Fires before all data related to a given blog is removed from blogs tracker 1202 * and activity stream. 1203 * 1204 * @since 1.5.0 1205 * 1206 * @param int $blog_id ID of the blog whose data is being removed. 1207 */ 1208 do_action( 'bp_blogs_before_remove_data_for_blog', $blog_id ); 1209 1210 // If this is regular blog, delete all data for that blog. 1211 BP_Blogs_Blog::delete_blog_for_all( $blog_id ); 1212 1213 /** 1214 * Fires after all data related to a given blog has been removed from blogs tracker 1215 * and activity stream. 1216 * 1217 * @since 1.0.0 1218 * 1219 * @param int $blog_id ID of the blog whose data is being removed. 1220 */ 1221 do_action( 'bp_blogs_remove_data_for_blog', $blog_id ); 1222 } 1223 add_action( 'bp_delete_site', 'bp_blogs_remove_data_for_blog', 1 ); 1224 1225 /** 1226 * Get all of a user's blogs, as tracked by BuddyPress. 1227 * 1228 * @see BP_Blogs_Blog::get_blogs_for_user() for a description of parameters 1229 * and return values. 1230 * 1231 * @param int $user_id See {@BP_Blogs_Blog::get_blogs_for_user()}. 1232 * @param bool $show_hidden See {@BP_Blogs_Blog::get_blogs_for_user()}. 1233 * @return array See {@BP_Blogs_Blog::get_blogs_for_user()}. 1234 */ 1235 function bp_blogs_get_blogs_for_user( $user_id, $show_hidden = false ) { 1236 return BP_Blogs_Blog::get_blogs_for_user( $user_id, $show_hidden ); 1237 } 1238 1239 /** 1240 * Retrieve a list of all blogs. 1241 * 1242 * @see BP_Blogs_Blog::get_all() for a description of parameters and return values. 1243 * 1244 * @param int|null $limit See {@BP_Blogs_Blog::get_all()}. 1245 * @param int|null $page See {@BP_Blogs_Blog::get_all()}. 1246 * @return array See {@BP_Blogs_Blog::get_all()}. 1247 */ 1248 function bp_blogs_get_all_blogs( $limit = null, $page = null ) { 1249 return BP_Blogs_Blog::get_all( $limit, $page ); 1250 } 1251 1252 /** 1253 * Retrieve a random list of blogs. 1254 * 1255 * @see BP_Blogs_Blog::get() for a description of parameters and return values. 1256 * 1257 * @param int|null $limit See {@BP_Blogs_Blog::get()}. 1258 * @param int|null $page See {@BP_Blogs_Blog::get()}. 1259 * @return array See {@BP_Blogs_Blog::get()}. 1260 */ 1261 function bp_blogs_get_random_blogs( $limit = null, $page = null ) { 1262 return BP_Blogs_Blog::get( 'random', $limit, $page ); 1263 } 1264 1265 /** 1266 * Check whether a given blog is hidden. 1267 * 1268 * @see BP_Blogs_Blog::is_hidden() for a description of parameters and return values. 1269 * 1270 * @param int $blog_id See {@BP_Blogs_Blog::is_hidden()}. 1271 * @return bool See {@BP_Blogs_Blog::is_hidden()}. 1272 */ 1273 function bp_blogs_is_blog_hidden( $blog_id ) { 1274 return BP_Blogs_Blog::is_hidden( $blog_id ); 1275 } 1276 1277 /* 1278 * Blog meta functions 1279 * 1280 * These functions are used to store specific blogmeta in one global table, 1281 * rather than in each blog's options table. Significantly speeds up global blog 1282 * queries. By default each blog's name, description and last updated time are 1283 * stored and synced here. 1284 */ 1285 1286 /** 1287 * Delete a metadata from the DB for a blog. 1288 * 1289 * @global object $wpdb WordPress database access object. 1290 * 1291 * @param int $blog_id ID of the blog whose metadata is being deleted. 1292 * @param string|bool $meta_key Optional. The key of the metadata being deleted. If 1293 * omitted, all BP metadata associated with the blog will 1294 * be deleted. 1295 * @param string|bool $meta_value Optional. If present, the metadata will only be 1296 * deleted if the meta_value matches this parameter. 1297 * @param bool $delete_all Optional. If true, delete matching metadata entries for 1298 * all objects, ignoring the specified blog_id. Otherwise, only 1299 * delete matching metadata entries for the specified blog. 1300 * Default: false. 1301 * @return bool True on success, false on failure. 1302 */ 1303 function bp_blogs_delete_blogmeta( $blog_id, $meta_key = false, $meta_value = false, $delete_all = false ) { 1304 global $wpdb; 1305 1306 // Legacy - if no meta_key is passed, delete all for the blog_id. 1307 if ( empty( $meta_key ) ) { 1308 $table_name = buddypress()->blogs->table_name_blogmeta; 1309 $sql = "SELECT meta_key FROM {$table_name} WHERE blog_id = %d"; 1310 $query = $wpdb->prepare( $sql, $blog_id ); 1311 $keys = $wpdb->get_col( $query ); 1312 1313 // With no meta_key, ignore $delete_all. 1314 $delete_all = false; 1315 } else { 1316 $keys = array( $meta_key ); 1317 } 1318 1319 add_filter( 'query', 'bp_filter_metaid_column_name' ); 1320 add_filter( 'sanitize_key', 'bp_blogs_filter_meta_column_name' ); 1321 1322 $retval = false; 1323 foreach ( $keys as $key ) { 1324 $retval = delete_metadata( 'bp_blog', $blog_id, $key, $meta_value, $delete_all ); 1325 } 1326 1327 remove_filter( 'query', 'bp_filter_metaid_column_name' ); 1328 remove_filter( 'sanitize_key', 'bp_blogs_filter_meta_column_name' ); 1329 1330 return $retval; 1331 } 1332 1333 /** 1334 * Get metadata for a given blog. 1335 * 1336 * @since 1.2.0 1337 * 1338 * @global object $wpdb WordPress database access object. 1339 * 1340 * @param int $blog_id ID of the blog whose metadata is being requested. 1341 * @param string $meta_key Optional. If present, only the metadata matching 1342 * that meta key will be returned. Otherwise, all 1343 * metadata for the blog will be fetched. 1344 * @param bool $single Optional. If true, return only the first value of the 1345 * specified meta_key. This parameter has no effect if 1346 * meta_key is not specified. Default: true. 1347 * @return mixed The meta value(s) being requested. 1348 */ 1349 function bp_blogs_get_blogmeta( $blog_id, $meta_key = '', $single = true ) { 1350 add_filter( 'query', 'bp_filter_metaid_column_name' ); 1351 add_filter( 'sanitize_key', 'bp_blogs_filter_meta_column_name' ); 1352 $retval = get_metadata( 'bp_blog', $blog_id, $meta_key, $single ); 1353 remove_filter( 'sanitize_key', 'bp_blogs_filter_meta_column_name' ); 1354 remove_filter( 'query', 'bp_filter_metaid_column_name' ); 1355 1356 return $retval; 1357 } 1358 1359 /** 1360 * Update a piece of blog meta. 1361 * 1362 * @global object $wpdb WordPress database access object. 1363 * 1364 * @param int $blog_id ID of the blog whose metadata is being updated. 1365 * @param string $meta_key Key of the metadata being updated. 1366 * @param mixed $meta_value Value to be set. 1367 * @param mixed $prev_value Optional. If specified, only update existing 1368 * metadata entries with the specified value. 1369 * Otherwise, update all entries. 1370 * @return bool|int Returns false on failure. On successful update of existing 1371 * metadata, returns true. On successful creation of new metadata, 1372 * returns the integer ID of the new metadata row. 1373 */ 1374 function bp_blogs_update_blogmeta( $blog_id, $meta_key, $meta_value, $prev_value = '' ) { 1375 add_filter( 'query', 'bp_filter_metaid_column_name' ); 1376 add_filter( 'sanitize_key', 'bp_blogs_filter_meta_column_name' ); 1377 $retval = update_metadata( 'bp_blog', $blog_id, $meta_key, $meta_value, $prev_value ); 1378 remove_filter( 'sanitize_key', 'bp_blogs_filter_meta_column_name' ); 1379 remove_filter( 'query', 'bp_filter_metaid_column_name' ); 1380 1381 return $retval; 1382 } 1383 1384 /** 1385 * Add a piece of blog metadata. 1386 * 1387 * @since 2.0.0 1388 * 1389 * @param int $blog_id ID of the blog. 1390 * @param string $meta_key Metadata key. 1391 * @param mixed $meta_value Metadata value. 1392 * @param bool $unique Optional. Whether to enforce a single metadata value 1393 * for the given key. If true, and the object already has a value for 1394 * the key, no change will be made. Default: false. 1395 * @return int|bool The meta ID on successful update, false on failure. 1396 */ 1397 function bp_blogs_add_blogmeta( $blog_id, $meta_key, $meta_value, $unique = false ) { 1398 add_filter( 'query', 'bp_filter_metaid_column_name' ); 1399 add_filter( 'sanitize_key', 'bp_blogs_filter_meta_column_name' ); 1400 $retval = add_metadata( 'bp_blog', $blog_id, $meta_key, $meta_value, $unique ); 1401 remove_filter( 'sanitize_key', 'bp_blogs_filter_meta_column_name' ); 1402 remove_filter( 'query', 'bp_filter_metaid_column_name' ); 1403 1404 return $retval; 1405 } 1406 /** 1407 * Remove all blog associations for a given user. 1408 * 1409 * @param int $user_id ID whose blog data should be removed. 1410 * @return bool Returns false on failure. 1411 */ 1412 function bp_blogs_remove_data( $user_id ) { 1413 if ( !is_multisite() ) 1414 return false; 1415 1416 /** 1417 * Fires before all blog associations are removed for a given user. 1418 * 1419 * @since 1.5.0 1420 * 1421 * @param int $user_id ID of the user whose blog associations are being removed. 1422 */ 1423 do_action( 'bp_blogs_before_remove_data', $user_id ); 1424 1425 // If this is regular blog, delete all data for that blog. 1426 BP_Blogs_Blog::delete_blogs_for_user( $user_id ); 1427 1428 /** 1429 * Fires after all blog associations are removed for a given user. 1430 * 1431 * @since 1.0.0 1432 * 1433 * @param int $user_id ID of the user whose blog associations were removed. 1434 */ 1435 do_action( 'bp_blogs_remove_data', $user_id ); 1436 } 1437 add_action( 'wpmu_delete_user', 'bp_blogs_remove_data' ); 1438 add_action( 'bp_make_spam_user', 'bp_blogs_remove_data' ); 1439 1440 /** 1441 * Deletes user XProfile data on the 'delete_user' hook. 1442 * 1443 * @since 6.0.0 1444 * 1445 * @param int $user_id The ID of the deleted user. 1446 */ 1447 function bp_blogs_remove_data_on_delete_user( $user_id ) { 1448 if ( ! bp_remove_user_data_on_delete_user_hook( 'blogs', $user_id ) ) { 1449 return; 1450 } 1451 1452 bp_blogs_remove_data( $user_id ); 1453 } 1454 add_action( 'delete_user', 'bp_blogs_remove_data_on_delete_user' ); 1455 1456 /** 1457 * Restore all blog associations for a given user. 1458 * 1459 * @since 2.2.0 1460 * 1461 * @param int $user_id ID whose blog data should be restored. 1462 */ 1463 function bp_blogs_restore_data( $user_id = 0 ) { 1464 if ( ! is_multisite() ) { 1465 return; 1466 } 1467 1468 // Get the user's blogs. 1469 $user_blogs = get_blogs_of_user( $user_id ); 1470 if ( empty( $user_blogs ) ) { 1471 return; 1472 } 1473 1474 $blogs = array_keys( $user_blogs ); 1475 1476 foreach ( $blogs as $blog_id ) { 1477 bp_blogs_add_user_to_blog( $user_id, false, $blog_id ); 1478 } 1479 } 1480 add_action( 'bp_make_ham_user', 'bp_blogs_restore_data', 10, 1 ); 1481 1482 /** 1483 * Checks whether blog creation is enabled. 1484 * 1485 * Returns true when blog creation is enabled for logged-in users only, or 1486 * when it's enabled for new registrations. 1487 * 1488 * @since 1.0.0 1489 * @since 7.0.0 The function has been moved into `bp-blogs/bp-blogs-functions.php`. 1490 * 1491 * @return bool True if blog registration is enabled. 1492 */ 1493 function bp_blog_signup_enabled() { 1494 $bp = buddypress(); 1495 $retval = true; 1496 $active_signup = 'all'; 1497 1498 if ( isset( $bp->site_options['registration'] ) ) { 1499 $active_signup = $bp->site_options['registration']; 1500 } 1501 1502 /** 1503 * Filters whether or not blog creation is enabled. 1504 * 1505 * Return "all", "none", "blog" or "user". 1506 * 1507 * @since 1.0.0 1508 * 1509 * @param string $active_signup Value of the registration site option creation status. 1510 */ 1511 $active_signup = apply_filters( 'wpmu_active_signup', $active_signup ); 1512 1513 if ( 'none' === $active_signup || 'user' === $active_signup ) { 1514 $retval = false; 1515 } 1516 1517 return $retval; 1518 } 1519 1520 /** 1521 * Returns the Blog signup's submitted vars. 1522 * 1523 * @since 7.0.0 1524 * 1525 * @return array An associative array containing the Blog signup's submitted vars. 1526 */ 1527 function bp_blogs_get_signup_form_submitted_vars() { 1528 $exprected_vars = array( 1529 'blogname' => '', 1530 'blog_title' => '', 1531 'blog_public' => 0, 1532 ); 1533 1534 $submitted_vars = wp_parse_args( $_POST, $exprected_vars ); 1535 1536 return array_map( 'wp_unslash', array_intersect_key( $submitted_vars, $exprected_vars ) ); 1537 } 1538 1539 /** 1540 * Validate a blog creation submission. 1541 * 1542 * Essentially, a wrapper for {@link wpmu_validate_blog_signup()}. 1543 * 1544 * @since 1.0.0 1545 * @since 7.0.0 Add the blog_name and blog_title parameters. 1546 * The function has been moved into `bp-blogs/bp-blogs-functions.php`. 1547 * 1548 * @return array Contains the new site data and error messages. 1549 */ 1550 function bp_blogs_validate_blog_form( $blog_name = '', $blog_title = '' ) { 1551 $user = ''; 1552 1553 if ( is_user_logged_in() ) { 1554 $user = wp_get_current_user(); 1555 } 1556 1557 if ( ! $blog_name && ! $blog_title ) { 1558 $submitted_vars = bp_blogs_get_signup_form_submitted_vars(); 1559 1560 if ( array_filter( $submitted_vars ) ) { 1561 $blog_name = $submitted_vars['blogname']; 1562 $blog_title = $submitted_vars['blog_title']; 1563 } 1564 } 1565 1566 return wpmu_validate_blog_signup( $blog_name, $blog_title, $user ); 1567 } 1568 1569 /** 1570 * Gets the site icon URL even when BuddyPress is not network activated. 1571 * 1572 * @since 7.0.0 1573 * 1574 * @param integer $blog_id The ID of the blog to get the site icon URL for. 1575 * @param integer $size The size of the site icon. 1576 * @return string The site icon URL 1577 */ 1578 function bp_blogs_get_site_icon_url( $blog_id = 0, $size = 512 ) { 1579 if ( is_multisite() && ! bp_is_network_activated() && ! bp_is_root_blog( $blog_id ) ) { 1580 $switched_blog = false; 1581 $url = ''; 1582 1583 if ( $blog_id && get_current_blog_id() !== (int) $blog_id ) { 1584 switch_to_blog( $blog_id ); 1585 $switched_blog = true; 1586 } 1587 1588 $site_icon_id = get_option( 'site_icon' ); 1589 1590 if ( $site_icon_id ) { 1591 $site_icon_data = wp_get_attachment_metadata( $site_icon_id ); 1592 $sizes = wp_list_pluck( $site_icon_data['sizes'], 'width' ); 1593 1594 sort( $sizes ); 1595 $closest = 'full'; 1596 1597 foreach ( $sizes as $width ) { 1598 $closest = array( $width, $width ); 1599 1600 if ( (int) $size < (int) $width ) { 1601 break; 1602 } 1603 } 1604 1605 $url = wp_get_attachment_image_url( $site_icon_id, $closest ); 1606 } 1607 1608 if ( $switched_blog ) { 1609 restore_current_blog(); 1610 } 1611 1612 return $url; 1613 } 1614 1615 return get_site_icon_url( $size, '', $blog_id ); 1616 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Jan 17 01:01:36 2021 | Cross-referenced by PHPXref 0.7.1 |