[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-blogs/ -> bp-blogs-functions.php (source)

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


Generated: Tue Oct 26 01:00:55 2021 Cross-referenced by PHPXref 0.7.1