[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-blogs/classes/ -> class-bp-blogs-blog.php (source)

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


Generated: Sun Nov 17 01:01:36 2019 Cross-referenced by PHPXref 0.7.1