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


Generated: Sat Apr 27 01:00:55 2024 Cross-referenced by PHPXref 0.7.1