[ 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( $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( 'wpmu_new_blog', '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      if ( 0 === $new_value ) {
 553          bp_blogs_update_blogmeta( get_current_blog_id(), 'site_icon_url_thumb', 0 );
 554          bp_blogs_update_blogmeta( get_current_blog_id(), 'site_icon_url_full',  0 );
 555      } else {
 556          // Save site icon URL as blogmeta.
 557          bp_blogs_update_blogmeta( get_current_blog_id(), 'site_icon_url_thumb', get_site_icon_url( bp_core_avatar_thumb_width() ) );
 558          bp_blogs_update_blogmeta( get_current_blog_id(), 'site_icon_url_full',  get_site_icon_url( bp_core_avatar_full_width()  ) );
 559      }
 560  }
 561  add_action( 'update_option_site_icon', 'bp_blogs_update_option_site_icon', 10, 2 );
 562  
 563  /**
 564   * Deletes the 'url' blogmeta for a site.
 565   *
 566   * Fires when a site's details are updated, which generally happens when
 567   * editing a site under "Network Admin > Sites". Prior to WP 4.9, the
 568   * correct hook was 'refresh_blog_details'; afterward, 'clean_site_cache'.
 569   *
 570   * @since 2.3.0
 571   *
 572   * @param int $site_id The site ID.
 573   */
 574  function bp_blogs_delete_url_blogmeta( $site_id = 0 ) {
 575      bp_blogs_delete_blogmeta( (int) $site_id, 'url' );
 576  }
 577  
 578  if ( function_exists( 'wp_switch_roles_and_user' ) ) {
 579      add_action( 'clean_site_cache', 'bp_blogs_delete_url_blogmeta' );
 580  } else {
 581      add_action( 'refresh_blog_details', 'bp_blogs_delete_url_blogmeta' );
 582  }
 583  
 584  /**
 585   * Record activity metadata about a published blog post.
 586   *
 587   * @since 2.2.0
 588   *
 589   * @param int     $activity_id ID of the activity item.
 590   * @param WP_Post $post        Post object.
 591   * @param array   $args        Array of arguments.
 592   */
 593  function bp_blogs_publish_post_activity_meta( $activity_id, $post, $args ) {
 594      if ( empty( $activity_id ) || 'post' != $post->post_type ) {
 595          return;
 596      }
 597  
 598      bp_activity_update_meta( $activity_id, 'post_title', $post->post_title );
 599  
 600      if ( ! empty( $args['post_url'] ) ) {
 601          $post_permalink = $args['post_url'];
 602      } else {
 603          $post_permalink = $post->guid;
 604      }
 605  
 606      bp_activity_update_meta( $activity_id, 'post_url',   $post_permalink );
 607  
 608      // Update the blog's last activity.
 609      bp_blogs_update_blogmeta( $args['item_id'], 'last_activity', bp_core_current_time() );
 610  
 611      /**
 612       * Fires after BuddyPress has recorded metadata about a published blog post.
 613       *
 614       * @since 1.0.0
 615       *
 616       * @param int     $ID    ID of the blog post being recorded.
 617       * @param WP_Post $post  WP_Post object for the current blog post.
 618       * @param string  $value ID of the user associated with the current blog post.
 619       */
 620      do_action( 'bp_blogs_new_blog_post', $post->ID, $post, $args['user_id'] );
 621  }
 622  add_action( 'bp_activity_post_type_published', 'bp_blogs_publish_post_activity_meta', 10, 3 );
 623  
 624  /**
 625   * Updates a blog post's activity meta entry during a post edit.
 626   *
 627   * @since 2.2.0
 628   * @since 2.5.0 Add the post type tracking args object parameter
 629   *
 630   * @param WP_Post              $post                 Post object.
 631   * @param BP_Activity_Activity $activity             Activity object.
 632   * @param object               $activity_post_object The post type tracking args object.
 633   */
 634  function bp_blogs_update_post_activity_meta( $post, $activity, $activity_post_object ) {
 635      if ( empty( $activity->id ) || empty( $activity_post_object->action_id ) ) {
 636          return;
 637      }
 638  
 639      // Update post title in activity meta.
 640      $existing_title = bp_activity_get_meta( $activity->id, 'post_title' );
 641      if ( $post->post_title !== $existing_title ) {
 642          bp_activity_update_meta( $activity->id, 'post_title', $post->post_title );
 643  
 644          if ( ! empty( $activity_post_object->comments_tracking->action_id ) ) {
 645              // Now update activity meta for post comments... sigh.
 646              add_filter( 'comments_clauses', 'bp_blogs_comments_clauses_select_by_id' );
 647              $comments = get_comments( array( 'post_id' => $post->ID ) );
 648              remove_filter( 'comments_clauses', 'bp_blogs_comments_clauses_select_by_id' );
 649  
 650              if ( ! empty( $comments ) ) {
 651                  $activity_ids = array();
 652                  $comment_ids  = wp_list_pluck( $comments, 'comment_ID' );
 653  
 654                  // Set up activity args.
 655                  $args = array(
 656                      'update_meta_cache' => false,
 657                      'show_hidden'       => true,
 658                      'per_page'          => 99999,
 659                  );
 660  
 661                  // Query for old-style "new_blog_comment" activity items.
 662                  $args['filter'] = array(
 663                      'object'       => $activity_post_object->comments_tracking->component_id,
 664                      'action'       => $activity_post_object->comments_tracking->action_id,
 665                      'primary_id'   => get_current_blog_id(),
 666                      'secondary_id' => implode( ',', $comment_ids ),
 667                  );
 668  
 669                  $activities = bp_activity_get( $args );
 670                  if ( ! empty( $activities['activities'] ) ) {
 671                      $activity_ids = (array) wp_list_pluck( $activities['activities'], 'id' );
 672                  }
 673  
 674                  // Query for activity comments connected to a blog post.
 675                  unset( $args['filter'] );
 676                  $args['meta_query'] = array( array(
 677                      'key'     => 'bp_blogs_' . $post->post_type . '_comment_id',
 678                      'value'   => $comment_ids,
 679                      'compare' => 'IN',
 680                  ) );
 681                  $args['type'] = 'activity_comment';
 682                  $args['display_comments'] = 'stream';
 683  
 684                  $activities = bp_activity_get( $args );
 685                  if ( ! empty( $activities['activities'] ) ) {
 686                      $activity_ids = array_merge( $activity_ids, (array) wp_list_pluck( $activities['activities'], 'id' ) );
 687                  }
 688  
 689                  // Update activity meta for all found activity items.
 690                  if ( ! empty( $activity_ids ) ) {
 691                      foreach ( $activity_ids as $aid ) {
 692                          bp_activity_update_meta( $aid, 'post_title', $post->post_title );
 693                      }
 694                  }
 695  
 696                  unset( $activities, $activity_ids, $comment_ids, $comments );
 697              }
 698          }
 699      }
 700  
 701      // Add post comment status to activity meta if closed.
 702      if( 'closed' == $post->comment_status ) {
 703          bp_activity_update_meta( $activity->id, 'post_comment_status', $post->comment_status );
 704      } else {
 705          bp_activity_delete_meta( $activity->id, 'post_comment_status' );
 706      }
 707  }
 708  add_action( 'bp_activity_post_type_updated', 'bp_blogs_update_post_activity_meta', 10, 3 );
 709  
 710  /**
 711   * Update Activity and blogs meta and eventually sync comment with activity comment
 712   *
 713   * @since  2.5.0
 714   *
 715   * @param  int|bool        $activity_id          ID of recorded activity, or false if sync is active.
 716   * @param  WP_Comment|null $comment              The comment object.
 717   * @param  array           $activity_args        Array of activity arguments.
 718   * @param  object|null     $activity_post_object The post type tracking args object.
 719   * @return WP_Error|bool|int Returns false if no activity, the activity id otherwise.
 720   */
 721  function bp_blogs_comment_sync_activity_comment( &$activity_id, $comment = null, $activity_args = array(), $activity_post_object = null ) {
 722      if ( empty( $activity_args ) || empty( $comment->post->ID ) || empty( $activity_post_object->comment_action_id ) ) {
 723          return false;
 724      }
 725  
 726      // Set the current blog id.
 727      $blog_id = get_current_blog_id();
 728  
 729      // These activity metadatas are used to build the new_blog_comment action string
 730      if ( ! empty( $activity_id ) && ! empty( $activity_args['item_id'] ) && 'new_blog_comment' === $activity_post_object->comment_action_id ) {
 731          // add some post info in activity meta
 732          bp_activity_update_meta( $activity_id, 'post_title', $comment->post->post_title );
 733          bp_activity_update_meta( $activity_id, 'post_url',   esc_url_raw( add_query_arg( 'p', $comment->post->ID, home_url( '/' ) ) ) );
 734      }
 735  
 736      // Sync comment - activity comment
 737      if ( ! bp_disable_blogforum_comments() ) {
 738  
 739          if ( ! empty( $_REQUEST['action'] ) ) {
 740              $existing_activity_id = get_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', true );
 741  
 742              if ( ! empty( $existing_activity_id ) ) {
 743                  $activity_args['id'] = $existing_activity_id;
 744              }
 745          }
 746  
 747          if ( empty( $activity_post_object ) ) {
 748              $activity_post_object = bp_activity_get_post_type_tracking_args( $comment->post->post_type );
 749          }
 750  
 751          if ( isset( $activity_post_object->action_id ) && isset( $activity_post_object->component_id ) ) {
 752              // find the parent 'new_post_type' activity entry
 753              $parent_activity_id = bp_activity_get_activity_id( array(
 754                  'component'         => $activity_post_object->component_id,
 755                  'type'              => $activity_post_object->action_id,
 756                  'item_id'           => $blog_id,
 757                  'secondary_item_id' => $comment->comment_post_ID
 758              ) );
 759  
 760              // Try to create a new activity item for the parent blog post.
 761              if ( empty( $parent_activity_id ) ) {
 762                  $parent_activity_id = bp_activity_post_type_publish( $comment->post->ID, $comment->post );
 763              }
 764          }
 765  
 766          // we found the parent activity entry
 767          // so let's go ahead and reconfigure some activity args
 768          if ( ! empty( $parent_activity_id ) ) {
 769              // set the parent activity entry ID
 770              $activity_args['activity_id'] = $parent_activity_id;
 771  
 772              // now see if the WP parent comment has a BP activity ID
 773              $comment_parent = 0;
 774              if ( ! empty( $comment->comment_parent ) ) {
 775                  $comment_parent = get_comment_meta( $comment->comment_parent, 'bp_activity_comment_id', true );
 776              }
 777  
 778              // WP parent comment does not have a BP activity ID
 779              // so set to 'new_' . post_type activity ID
 780              if ( empty( $comment_parent ) ) {
 781                  $comment_parent = $parent_activity_id;
 782              }
 783  
 784              $activity_args['parent_id']         = $comment_parent;
 785              $activity_args['skip_notification'] = true;
 786  
 787          // could not find corresponding parent activity entry
 788          // so wipe out $args array
 789          } else {
 790              $activity_args = array();
 791          }
 792  
 793          // Record in activity streams
 794          if ( ! empty( $activity_args ) ) {
 795              $activity_id = bp_activity_new_comment( $activity_args );
 796  
 797              if ( empty( $activity_args['id'] ) ) {
 798                  // The activity metadata to inform about the corresponding comment ID
 799                  bp_activity_update_meta( $activity_id, "bp_blogs_{$comment->post->post_type}_comment_id", $comment->comment_ID );
 800  
 801                  // The comment metadata to inform about the corresponding activity ID
 802                  add_comment_meta( $comment->comment_ID, 'bp_activity_comment_id', $activity_id );
 803  
 804                  // These activity metadatas are used to build the new_blog_comment action string
 805                  if ( 'new_blog_comment' === $activity_post_object->comment_action_id ) {
 806                      bp_activity_update_meta( $activity_id, 'post_title', $comment->post->post_title );
 807                      bp_activity_update_meta( $activity_id, 'post_url', esc_url_raw( add_query_arg( 'p', $comment->post->ID, home_url( '/' ) ) ) );
 808                  }
 809              }
 810  
 811              /**
 812               * Fires after an activity comment is added from a WP post comment.
 813               *
 814               * @since 2.6.0
 815               *
 816               * @param int        $activity_id          The activity comment ID.
 817               * @param WP_Comment $post_type_comment    WP Comment object.
 818               * @param array      $activity_args        Activity comment arguments.
 819               * @param object     $activity_post_object The post type tracking args object.
 820               */
 821              do_action( 'bp_blogs_comment_sync_activity_comment', $activity_id, $comment, $activity_args, $activity_post_object );
 822          }
 823      }
 824  
 825      // Update the blogs last active date
 826      bp_blogs_update_blogmeta( $blog_id, 'last_activity', bp_core_current_time() );
 827  
 828      if ( 'new_blog_comment' === $activity_post_object->comment_action_id ) {
 829          /**
 830           * Fires after BuddyPress has recorded metadata about a published blog post comment.
 831           *
 832           * @since 2.5.0
 833           *
 834           * @param int     $value    Comment ID of the blog post comment being recorded.
 835           * @param WP_Post $post  WP_Comment object for the current blog post.
 836           * @param string  $value ID of the user associated with the current blog post comment.
 837           */
 838          do_action( 'bp_blogs_new_blog_comment', $comment->comment_ID, $comment, bp_loggedin_user_id() );
 839      }
 840  
 841      return $activity_id;
 842  }
 843  add_action( 'bp_activity_post_type_comment', 'bp_blogs_comment_sync_activity_comment', 10, 4 );
 844  
 845  /**
 846   * Record a user's association with a blog.
 847   *
 848   * This function is hooked to several WordPress actions where blog roles are
 849   * set/changed ('add_user_to_blog', 'profile_update', 'user_register'). It
 850   * parses the changes, and records them as necessary in the BP blog tracker.
 851   *
 852   * BuddyPress does not track blogs for users with the 'subscriber' role by
 853   * default, though as of 2.1.0 you can filter 'bp_blogs_get_allowed_roles' to
 854   * modify this behavior.
 855   *
 856   * @param int         $user_id The ID of the user.
 857   * @param string|bool $role    User's WordPress role for this blog ID.
 858   * @param int         $blog_id Blog ID user is being added to.
 859   * @return false|null False on failure.
 860   */
 861  function bp_blogs_add_user_to_blog( $user_id, $role = false, $blog_id = 0 ) {
 862      global $wpdb;
 863  
 864      // If no blog ID was passed, use the root blog ID.
 865      if ( empty( $blog_id ) ) {
 866          $blog_id = isset( $wpdb->blogid ) ? $wpdb->blogid : bp_get_root_blog_id();
 867      }
 868  
 869      // If no role was passed, try to find the blog role.
 870      if ( empty( $role ) ) {
 871  
 872          // Get user capabilities.
 873          $key        = $wpdb->get_blog_prefix( $blog_id ). 'capabilities';
 874          $user_roles = array_keys( (array) bp_get_user_meta( $user_id, $key, true ) );
 875  
 876          // User has roles so lets.
 877          if ( ! empty( $user_roles ) ) {
 878  
 879              // Get blog roles.
 880              $blog_roles      = array_keys( bp_get_current_blog_roles() );
 881  
 882              // Look for blog only roles of the user.
 883              $intersect_roles = array_intersect( $user_roles, $blog_roles );
 884  
 885              // If there's a role in the array, use the first one. This isn't
 886              // very smart, but since roles aren't exactly hierarchical, and
 887              // WordPress does not yet have a UI for multiple user roles, it's
 888              // fine for now.
 889              if ( ! empty( $intersect_roles ) ) {
 890                  $role = array_shift( $intersect_roles );
 891              }
 892          }
 893      }
 894  
 895      // Bail if no role was found or role is not in the allowed roles array.
 896      if ( empty( $role ) || ! in_array( $role, bp_blogs_get_allowed_roles() ) ) {
 897          return false;
 898      }
 899  
 900      // Record the blog activity for this user being added to this blog.
 901      bp_blogs_record_blog( $blog_id, $user_id, true );
 902  }
 903  add_action( 'add_user_to_blog', 'bp_blogs_add_user_to_blog', 10, 3 );
 904  add_action( 'profile_update',   'bp_blogs_add_user_to_blog'        );
 905  add_action( 'user_register',    'bp_blogs_add_user_to_blog'        );
 906  
 907  /**
 908   * The allowed blog roles a member must have to be recorded into the
 909   * `bp_user_blogs` pointer table.
 910   *
 911   * This added and was made filterable in BuddyPress 2.1.0 to make it easier
 912   * to extend the functionality of the Blogs component.
 913   *
 914   * @since 2.1.0
 915   *
 916   * @return string
 917   */
 918  function bp_blogs_get_allowed_roles() {
 919  
 920      /**
 921       * Filters the allowed roles a member must have to be recorded into bp_user_blogs pointer table.
 922       *
 923       * @since 2.1.0
 924       *
 925       * @param array $value Array of potential roles user needs.
 926       */
 927      return apply_filters( 'bp_blogs_get_allowed_roles', array( 'contributor', 'author', 'editor', 'administrator' ) );
 928  }
 929  
 930  /**
 931   * Remove a blog-user pair from BP's blog tracker.
 932   *
 933   * @param int $user_id ID of the user whose blog is being removed.
 934   * @param int $blog_id Optional. ID of the blog being removed. Default: current blog ID.
 935   */
 936  function bp_blogs_remove_user_from_blog( $user_id, $blog_id = 0 ) {
 937      global $wpdb;
 938  
 939      if ( empty( $blog_id ) ) {
 940          $blog_id = $wpdb->blogid;
 941      }
 942  
 943      bp_blogs_remove_blog_for_user( $user_id, $blog_id );
 944  }
 945  add_action( 'remove_user_from_blog', 'bp_blogs_remove_user_from_blog', 10, 2 );
 946  
 947  /**
 948   * Rehook WP's maybe_add_existing_user_to_blog with a later priority.
 949   *
 950   * WordPress catches add-user-to-blog requests at init:10. In some cases, this
 951   * can precede BP's Blogs component. This function bumps the priority of the
 952   * core function, so that we can be sure that the Blogs component is loaded
 953   * first. See https://buddypress.trac.wordpress.org/ticket/3916.
 954   *
 955   * @since 1.6.0
 956   */
 957  function bp_blogs_maybe_add_user_to_blog() {
 958      if ( ! is_multisite() )
 959          return;
 960  
 961      remove_action( 'init', 'maybe_add_existing_user_to_blog' );
 962      add_action( 'init', 'maybe_add_existing_user_to_blog', 20 );
 963  }
 964  add_action( 'init', 'bp_blogs_maybe_add_user_to_blog', 1 );
 965  
 966  /**
 967   * Remove the "blog created" item from the BP blogs tracker and activity stream.
 968   *
 969   * @param int $blog_id ID of the blog being removed.
 970   */
 971  function bp_blogs_remove_blog( $blog_id ) {
 972  
 973      $blog_id = (int) $blog_id;
 974  
 975      /**
 976       * Fires before a "blog created" item is removed from blogs
 977       * tracker and activity stream.
 978       *
 979       * @since 1.5.0
 980       *
 981       * @param int $blog_id ID of the blog having its item removed.
 982       */
 983      do_action( 'bp_blogs_before_remove_blog', $blog_id );
 984  
 985      BP_Blogs_Blog::delete_blog_for_all( $blog_id );
 986  
 987      /**
 988       * Fires after a "blog created" item has been removed from blogs
 989       * tracker and activity stream.
 990       *
 991       * @since 1.0.0
 992       *
 993       * @param int $blog_id ID of the blog who had its item removed.
 994       */
 995      do_action( 'bp_blogs_remove_blog', $blog_id );
 996  }
 997  add_action( 'delete_blog', 'bp_blogs_remove_blog' );
 998  
 999  /**
1000   * Remove a blog from the tracker for a specific user.
1001   *
1002   * @param int $user_id ID of the user for whom the blog is being removed.
1003   * @param int $blog_id ID of the blog being removed.
1004   */
1005  function bp_blogs_remove_blog_for_user( $user_id, $blog_id ) {
1006  
1007      $blog_id = (int) $blog_id;
1008      $user_id = (int) $user_id;
1009  
1010      /**
1011       * Fires before a blog is removed from the tracker for a specific user.
1012       *
1013       * @since 1.5.0
1014       *
1015       * @param int $blog_id ID of the blog being removed.
1016       * @param int $user_id ID of the user having the blog removed for.
1017       */
1018      do_action( 'bp_blogs_before_remove_blog_for_user', $blog_id, $user_id );
1019  
1020      BP_Blogs_Blog::delete_blog_for_user( $blog_id, $user_id );
1021  
1022      /**
1023       * Fires after a blog has been removed from the tracker for a specific user.
1024       *
1025       * @since 1.0.0
1026       *
1027       * @param int $blog_id ID of the blog that was removed.
1028       * @param int $user_id ID of the user having the blog removed for.
1029       */
1030      do_action( 'bp_blogs_remove_blog_for_user', $blog_id, $user_id );
1031  }
1032  add_action( 'remove_user_from_blog', 'bp_blogs_remove_blog_for_user', 10, 2 );
1033  
1034  /**
1035   * Remove a synced activity comment from the activity stream.
1036   *
1037   * @since 2.5.0
1038   *
1039   * @param bool   $deleted              True when a comment post type activity was successfully removed.
1040   * @param int    $comment_id           ID of the comment to be removed.
1041   * @param object $activity_post_object The post type tracking args object.
1042   * @param string $activity_type        The post type comment activity type.
1043   *
1044   * @return bool True on success. False on error.
1045   */
1046  function bp_blogs_post_type_remove_comment( $deleted, $comment_id, $activity_post_object, $activity_type = '' ) {
1047      // Remove synced activity comments, if needed.
1048      if ( ! bp_disable_blogforum_comments() ) {
1049          // Get associated activity ID from comment meta
1050          $activity_id = get_comment_meta( $comment_id, 'bp_activity_comment_id', true );
1051  
1052          /**
1053           * Delete the associated activity comment & also remove
1054           * child post comments and associated activity comments.
1055           */
1056          if ( ! empty( $activity_id ) ) {
1057              // fetch the activity comments for the activity item
1058              $activity = bp_activity_get( array(
1059                  'in'               => $activity_id,
1060                  'display_comments' => 'stream',
1061                  'spam'             => 'all',
1062              ) );
1063  
1064              // get all activity comment IDs for the pending deleted item
1065              if ( ! empty( $activity['activities'] ) ) {
1066                  $activity_ids   = bp_activity_recurse_comments_activity_ids( $activity );
1067                  $activity_ids[] = $activity_id;
1068  
1069                  // delete activity items
1070                  foreach ( $activity_ids as $activity_id ) {
1071                      bp_activity_delete( array(
1072                          'id' => $activity_id
1073                      ) );
1074                  }
1075  
1076                  // remove associated blog comments
1077                  bp_blogs_remove_associated_blog_comments( $activity_ids );
1078  
1079                  // rebuild activity comment tree
1080                  BP_Activity_Activity::rebuild_activity_comment_tree( $activity['activities'][0]->item_id );
1081  
1082                  // Set the result
1083                  $deleted = true;
1084              }
1085          }
1086      }
1087  
1088      // Backcompat for comments about the 'post' post type.
1089      if ( 'new_blog_comment' === $activity_type ) {
1090          /**
1091           * Fires after a blog comment activity item was removed from activity stream.
1092           *
1093           * @since 1.0.0
1094           *
1095           * @param int $value      ID for the blog associated with the removed comment.
1096           * @param int $comment_id ID of the comment being removed.
1097           * @param int $value      ID of the current logged in user.
1098           */
1099          do_action( 'bp_blogs_remove_comment', get_current_blog_id(), $comment_id, bp_loggedin_user_id() );
1100      }
1101  
1102      return $deleted;
1103  }
1104  add_action( 'bp_activity_post_type_remove_comment', 'bp_blogs_post_type_remove_comment', 10, 4 );
1105  
1106  /**
1107   * Removes blog comments that are associated with activity comments.
1108   *
1109   * @since 2.0.0
1110   *
1111   * @see bp_blogs_remove_synced_comment()
1112   * @see bp_blogs_sync_delete_from_activity_comment()
1113   *
1114   * @param array $activity_ids The activity IDs to check association with blog
1115   *                            comments.
1116   * @param bool  $force_delete  Whether to force delete the comments. If false,
1117   *                            comments are trashed instead.
1118   */
1119  function bp_blogs_remove_associated_blog_comments( $activity_ids = array(), $force_delete = true ) {
1120      // Query args.
1121      $query_args = array(
1122          'meta_query' => array(
1123              array(
1124                  'key'     => 'bp_activity_comment_id',
1125                  'value'   => implode( ',', (array) $activity_ids ),
1126                  'compare' => 'IN',
1127              )
1128          )
1129      );
1130  
1131      // Get comment.
1132      $comment_query = new WP_Comment_Query;
1133      $comments = $comment_query->query( $query_args );
1134  
1135      // Found the corresponding comments
1136      // let's delete them!
1137      foreach ( $comments as $comment ) {
1138          wp_delete_comment( $comment->comment_ID, $force_delete );
1139  
1140          // If we're trashing the comment, remove the meta key as well.
1141          if ( empty( $force_delete ) ) {
1142              delete_comment_meta( $comment->comment_ID, 'bp_activity_comment_id' );
1143          }
1144      }
1145  }
1146  
1147  /**
1148   * Get the total number of blogs being tracked by BuddyPress.
1149   *
1150   * @return int $count Total blog count.
1151   */
1152  function bp_blogs_total_blogs() {
1153      $count = wp_cache_get( 'bp_total_blogs', 'bp' );
1154  
1155      if ( false === $count ) {
1156          $blogs = BP_Blogs_Blog::get_all();
1157          $count = $blogs['total'];
1158          wp_cache_set( 'bp_total_blogs', $count, 'bp' );
1159      }
1160      return $count;
1161  }
1162  
1163  /**
1164   * Get the total number of blogs being tracked by BP for a specific user.
1165   *
1166   * @since 1.2.0
1167   *
1168   * @param int $user_id ID of the user being queried. Default: on a user page,
1169   *                     the displayed user. Otherwise, the logged-in user.
1170   * @return int $count Total blog count for the user.
1171   */
1172  function bp_blogs_total_blogs_for_user( $user_id = 0 ) {
1173      if ( empty( $user_id ) ) {
1174          $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id();
1175      }
1176  
1177      // No user ID? do not attempt to look at cache.
1178      if ( empty( $user_id ) ) {
1179          return 0;
1180      }
1181  
1182      $count = wp_cache_get( 'bp_total_blogs_for_user_' . $user_id, 'bp' );
1183      if ( false === $count ) {
1184          $count = BP_Blogs_Blog::total_blog_count_for_user( $user_id );
1185          wp_cache_set( 'bp_total_blogs_for_user_' . $user_id, $count, 'bp' );
1186      }
1187  
1188      return $count;
1189  }
1190  
1191  /**
1192   * Remove the all data related to a given blog from the BP blogs tracker and activity stream.
1193   *
1194   * @param int $blog_id The ID of the blog to expunge.
1195   */
1196  function bp_blogs_remove_data_for_blog( $blog_id ) {
1197  
1198      /**
1199       * Fires before all data related to a given blog is removed from blogs tracker
1200       * and activity stream.
1201       *
1202       * @since 1.5.0
1203       *
1204       * @param int $blog_id ID of the blog whose data is being removed.
1205       */
1206      do_action( 'bp_blogs_before_remove_data_for_blog', $blog_id );
1207  
1208      // If this is regular blog, delete all data for that blog.
1209      BP_Blogs_Blog::delete_blog_for_all( $blog_id );
1210  
1211      /**
1212       * Fires after all data related to a given blog has been removed from blogs tracker
1213       * and activity stream.
1214       *
1215       * @since 1.0.0
1216       *
1217       * @param int $blog_id ID of the blog whose data is being removed.
1218       */
1219      do_action( 'bp_blogs_remove_data_for_blog', $blog_id );
1220  }
1221  add_action( 'delete_blog', 'bp_blogs_remove_data_for_blog', 1 );
1222  
1223  /**
1224   * Get all of a user's blogs, as tracked by BuddyPress.
1225   *
1226   * @see BP_Blogs_Blog::get_blogs_for_user() for a description of parameters
1227   *      and return values.
1228   *
1229   * @param int  $user_id     See {@BP_Blogs_Blog::get_blogs_for_user()}.
1230   * @param bool $show_hidden See {@BP_Blogs_Blog::get_blogs_for_user()}.
1231   * @return array See {@BP_Blogs_Blog::get_blogs_for_user()}.
1232   */
1233  function bp_blogs_get_blogs_for_user( $user_id, $show_hidden = false ) {
1234      return BP_Blogs_Blog::get_blogs_for_user( $user_id, $show_hidden );
1235  }
1236  
1237  /**
1238   * Retrieve a list of all blogs.
1239   *
1240   * @see BP_Blogs_Blog::get_all() for a description of parameters and return values.
1241   *
1242   * @param int|null $limit See {@BP_Blogs_Blog::get_all()}.
1243   * @param int|null $page  See {@BP_Blogs_Blog::get_all()}.
1244   * @return array See {@BP_Blogs_Blog::get_all()}.
1245   */
1246  function bp_blogs_get_all_blogs( $limit = null, $page = null ) {
1247      return BP_Blogs_Blog::get_all( $limit, $page );
1248  }
1249  
1250  /**
1251   * Retrieve a random list of blogs.
1252   *
1253   * @see BP_Blogs_Blog::get() for a description of parameters and return values.
1254   *
1255   * @param int|null $limit See {@BP_Blogs_Blog::get()}.
1256   * @param int|null $page  See {@BP_Blogs_Blog::get()}.
1257   * @return array See {@BP_Blogs_Blog::get()}.
1258   */
1259  function bp_blogs_get_random_blogs( $limit = null, $page = null ) {
1260      return BP_Blogs_Blog::get( 'random', $limit, $page );
1261  }
1262  
1263  /**
1264   * Check whether a given blog is hidden.
1265   *
1266   * @see BP_Blogs_Blog::is_hidden() for a description of parameters and return values.
1267   *
1268   * @param int $blog_id See {@BP_Blogs_Blog::is_hidden()}.
1269   * @return bool See {@BP_Blogs_Blog::is_hidden()}.
1270   */
1271  function bp_blogs_is_blog_hidden( $blog_id ) {
1272      return BP_Blogs_Blog::is_hidden( $blog_id );
1273  }
1274  
1275  /*
1276   * Blog meta functions
1277   *
1278   * These functions are used to store specific blogmeta in one global table,
1279   * rather than in each blog's options table. Significantly speeds up global blog
1280   * queries. By default each blog's name, description and last updated time are
1281   * stored and synced here.
1282   */
1283  
1284  /**
1285   * Delete a metadata from the DB for a blog.
1286   *
1287   * @global object $wpdb WordPress database access object.
1288   *
1289   * @param int         $blog_id    ID of the blog whose metadata is being deleted.
1290   * @param string|bool $meta_key   Optional. The key of the metadata being deleted. If
1291   *                                omitted, all BP metadata associated with the blog will
1292   *                                be deleted.
1293   * @param string|bool $meta_value Optional. If present, the metadata will only be
1294   *                                deleted if the meta_value matches this parameter.
1295   * @param bool        $delete_all Optional. If true, delete matching metadata entries for
1296   *                                all objects, ignoring the specified blog_id. Otherwise, only
1297   *                                delete matching metadata entries for the specified blog.
1298   *                                Default: false.
1299   * @return bool True on success, false on failure.
1300   */
1301  function bp_blogs_delete_blogmeta( $blog_id, $meta_key = false, $meta_value = false, $delete_all = false ) {
1302      global $wpdb;
1303  
1304      // Legacy - if no meta_key is passed, delete all for the blog_id.
1305      if ( empty( $meta_key ) ) {
1306          $table_name = buddypress()->blogs->table_name_blogmeta;
1307          $sql        = "SELECT meta_key FROM {$table_name} WHERE blog_id = %d";
1308          $query      = $wpdb->prepare( $sql, $blog_id );
1309          $keys       = $wpdb->get_col( $query );
1310  
1311          // With no meta_key, ignore $delete_all.
1312          $delete_all = false;
1313      } else {
1314          $keys = array( $meta_key );
1315      }
1316  
1317      add_filter( 'query', 'bp_filter_metaid_column_name' );
1318      add_filter( 'sanitize_key', 'bp_blogs_filter_meta_column_name' );
1319  
1320      $retval = false;
1321      foreach ( $keys as $key ) {
1322          $retval = delete_metadata( 'bp_blog', $blog_id, $key, $meta_value, $delete_all );
1323      }
1324  
1325      remove_filter( 'query', 'bp_filter_metaid_column_name' );
1326      remove_filter( 'sanitize_key', 'bp_blogs_filter_meta_column_name' );
1327  
1328      return $retval;
1329  }
1330  
1331  /**
1332   * Get metadata for a given blog.
1333   *
1334   * @since 1.2.0
1335   *
1336   * @global object $wpdb WordPress database access object.
1337   *
1338   * @param int    $blog_id  ID of the blog whose metadata is being requested.
1339   * @param string $meta_key Optional. If present, only the metadata matching
1340   *                         that meta key will be returned. Otherwise, all
1341   *                         metadata for the blog will be fetched.
1342   * @param bool   $single   Optional. If true, return only the first value of the
1343   *                         specified meta_key. This parameter has no effect if
1344   *                         meta_key is not specified. Default: true.
1345   * @return mixed The meta value(s) being requested.
1346   */
1347  function bp_blogs_get_blogmeta( $blog_id, $meta_key = '', $single = true ) {
1348      add_filter( 'query', 'bp_filter_metaid_column_name' );
1349      add_filter( 'sanitize_key', 'bp_blogs_filter_meta_column_name' );
1350      $retval = get_metadata( 'bp_blog', $blog_id, $meta_key, $single );
1351      remove_filter( 'sanitize_key', 'bp_blogs_filter_meta_column_name' );
1352      remove_filter( 'query', 'bp_filter_metaid_column_name' );
1353  
1354      return $retval;
1355  }
1356  
1357  /**
1358   * Update a piece of blog meta.
1359   *
1360   * @global object $wpdb WordPress database access object.
1361   *
1362   * @param int    $blog_id    ID of the blog whose metadata is being updated.
1363   * @param string $meta_key   Key of the metadata being updated.
1364   * @param mixed  $meta_value Value to be set.
1365   * @param mixed  $prev_value Optional. If specified, only update existing
1366   *                           metadata entries with the specified value.
1367   *                           Otherwise, update all entries.
1368   * @return bool|int Returns false on failure. On successful update of existing
1369   *                  metadata, returns true. On successful creation of new metadata,
1370   *                  returns the integer ID of the new metadata row.
1371   */
1372  function bp_blogs_update_blogmeta( $blog_id, $meta_key, $meta_value, $prev_value = '' ) {
1373      add_filter( 'query', 'bp_filter_metaid_column_name' );
1374      add_filter( 'sanitize_key', 'bp_blogs_filter_meta_column_name' );
1375      $retval = update_metadata( 'bp_blog', $blog_id, $meta_key, $meta_value, $prev_value );
1376      remove_filter( 'sanitize_key', 'bp_blogs_filter_meta_column_name' );
1377      remove_filter( 'query', 'bp_filter_metaid_column_name' );
1378  
1379      return $retval;
1380  }
1381  
1382  /**
1383   * Add a piece of blog metadata.
1384   *
1385   * @since 2.0.0
1386   *
1387   * @param int    $blog_id    ID of the blog.
1388   * @param string $meta_key   Metadata key.
1389   * @param mixed  $meta_value Metadata value.
1390   * @param bool   $unique     Optional. Whether to enforce a single metadata value
1391   *                           for the given key. If true, and the object already has a value for
1392   *                           the key, no change will be made. Default: false.
1393   * @return int|bool The meta ID on successful update, false on failure.
1394   */
1395  function bp_blogs_add_blogmeta( $blog_id, $meta_key, $meta_value, $unique = false ) {
1396      add_filter( 'query', 'bp_filter_metaid_column_name' );
1397      add_filter( 'sanitize_key', 'bp_blogs_filter_meta_column_name' );
1398      $retval = add_metadata( 'bp_blog', $blog_id, $meta_key, $meta_value, $unique );
1399      remove_filter( 'sanitize_key', 'bp_blogs_filter_meta_column_name' );
1400      remove_filter( 'query', 'bp_filter_metaid_column_name' );
1401  
1402      return $retval;
1403  }
1404  /**
1405   * Remove all blog associations for a given user.
1406   *
1407   * @param int $user_id ID whose blog data should be removed.
1408   * @return bool Returns false on failure.
1409   */
1410  function bp_blogs_remove_data( $user_id ) {
1411      if ( !is_multisite() )
1412          return false;
1413  
1414      /**
1415       * Fires before all blog associations are removed for a given user.
1416       *
1417       * @since 1.5.0
1418       *
1419       * @param int $user_id ID of the user whose blog associations are being removed.
1420       */
1421      do_action( 'bp_blogs_before_remove_data', $user_id );
1422  
1423      // If this is regular blog, delete all data for that blog.
1424      BP_Blogs_Blog::delete_blogs_for_user( $user_id );
1425  
1426      /**
1427       * Fires after all blog associations are removed for a given user.
1428       *
1429       * @since 1.0.0
1430       *
1431       * @param int $user_id ID of the user whose blog associations were removed.
1432       */
1433      do_action( 'bp_blogs_remove_data', $user_id );
1434  }
1435  add_action( 'wpmu_delete_user',  'bp_blogs_remove_data' );
1436  add_action( 'delete_user',       'bp_blogs_remove_data' );
1437  add_action( 'bp_make_spam_user', 'bp_blogs_remove_data' );
1438  
1439  /**
1440   * Restore all blog associations for a given user.
1441   *
1442   * @since 2.2.0
1443   *
1444   * @param int $user_id ID whose blog data should be restored.
1445   */
1446  function bp_blogs_restore_data( $user_id = 0 ) {
1447      if ( ! is_multisite() ) {
1448          return;
1449      }
1450  
1451      // Get the user's blogs.
1452      $user_blogs = get_blogs_of_user( $user_id );
1453      if ( empty( $user_blogs ) ) {
1454          return;
1455      }
1456  
1457      $blogs = array_keys( $user_blogs );
1458  
1459      foreach ( $blogs as $blog_id ) {
1460          bp_blogs_add_user_to_blog( $user_id, false, $blog_id );
1461      }
1462  }
1463  add_action( 'bp_make_ham_user', 'bp_blogs_restore_data', 10, 1 );


Generated: Sat Sep 21 01:01:46 2019 Cross-referenced by PHPXref 0.7.1