| 1 | <?php |
|---|
| 2 | //Includes also fix based on https://buddypress.trac.wordpress.org/ticket/5303 |
|---|
| 3 | /** |
|---|
| 4 | * BuddyPress Blogs Classes. |
|---|
| 5 | * |
|---|
| 6 | * @package BuddyPress |
|---|
| 7 | * @subpackage BlogsClasses |
|---|
| 8 | */ |
|---|
| 9 | // Exit if accessed directly |
|---|
| 10 | if (!defined('ABSPATH')) |
|---|
| 11 | 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 BuddyPress (1.0.0) |
|---|
| 20 | |
|---|
| 21 | */ |
|---|
| 22 | class BP_Blogs_Blog { |
|---|
| 23 | |
|---|
| 24 | public $id; |
|---|
| 25 | public $user_id; |
|---|
| 26 | public $blog_id; |
|---|
| 27 | |
|---|
| 28 | /** |
|---|
| 29 | * Constructor method. |
|---|
| 30 | * |
|---|
| 31 | * @param int $id Optional. The ID of the blog. |
|---|
| 32 | */ |
|---|
| 33 | public function __construct($id = null) { |
|---|
| 34 | if (!empty($id)) { |
|---|
| 35 | $this->id = $id; |
|---|
| 36 | $this->populate(); |
|---|
| 37 | } |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | /** |
|---|
| 41 | * Populate the object with data about the specific activity item. |
|---|
| 42 | */ |
|---|
| 43 | public function populate() { |
|---|
| 44 | global $wpdb, $bp; |
|---|
| 45 | |
|---|
| 46 | $blog = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$bp->blogs->table_name} WHERE id = %d", $this->id)); |
|---|
| 47 | |
|---|
| 48 | $this->user_id = $blog->user_id; |
|---|
| 49 | $this->blog_id = $blog->blog_id; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | * Save the BP blog data to the database. |
|---|
| 54 | * |
|---|
| 55 | * @return bool True on success, false on failure. |
|---|
| 56 | */ |
|---|
| 57 | public function save() { |
|---|
| 58 | global $wpdb, $bp; |
|---|
| 59 | |
|---|
| 60 | $this->user_id = apply_filters('bp_blogs_blog_user_id_before_save', $this->user_id, $this->id); |
|---|
| 61 | $this->blog_id = apply_filters('bp_blogs_blog_id_before_save', $this->blog_id, $this->id); |
|---|
| 62 | |
|---|
| 63 | do_action_ref_array('bp_blogs_blog_before_save', array(&$this)); |
|---|
| 64 | |
|---|
| 65 | // Don't try and save if there is no user ID or blog ID set. |
|---|
| 66 | if (!$this->user_id || !$this->blog_id) |
|---|
| 67 | return false; |
|---|
| 68 | |
|---|
| 69 | // Don't save if this blog has already been recorded for the user. |
|---|
| 70 | if (!$this->id && $this->exists()) |
|---|
| 71 | return false; |
|---|
| 72 | |
|---|
| 73 | if ($this->id) { |
|---|
| 74 | // Update |
|---|
| 75 | $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); |
|---|
| 76 | } else { |
|---|
| 77 | // Save |
|---|
| 78 | $sql = $wpdb->prepare("INSERT INTO {$bp->blogs->table_name} ( user_id, blog_id ) VALUES ( %d, %d )", $this->user_id, $this->blog_id); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | if (!$wpdb->query($sql)) |
|---|
| 82 | return false; |
|---|
| 83 | |
|---|
| 84 | do_action_ref_array('bp_blogs_blog_after_save', array(&$this)); |
|---|
| 85 | |
|---|
| 86 | if ($this->id) |
|---|
| 87 | return $this->id; |
|---|
| 88 | else |
|---|
| 89 | return $wpdb->insert_id; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | * Check whether an association between this user and this blog exists. |
|---|
| 94 | * |
|---|
| 95 | * @return int The number of associations between the user and blog |
|---|
| 96 | * saved in the blog component tables. |
|---|
| 97 | */ |
|---|
| 98 | public function exists() { |
|---|
| 99 | global $bp, $wpdb; |
|---|
| 100 | |
|---|
| 101 | 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)); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | /** Static Methods ************************************************** */ |
|---|
| 105 | |
|---|
| 106 | /** |
|---|
| 107 | * Retrieve a set of blog-user associations. |
|---|
| 108 | * @param array $args { |
|---|
| 109 | * An array of arguments. All items are optional. |
|---|
| 110 | * @param string $type The order in which results should be returned. |
|---|
| 111 | * 'active', 'alphabetical', 'newest', or 'random'. |
|---|
| 112 | * @param int|bool $limit Optional. The maximum records to return. |
|---|
| 113 | * Default: false. |
|---|
| 114 | * @param int|bool $page Optional. The page of records to return. |
|---|
| 115 | * Default: false (unlimited results). |
|---|
| 116 | * @param int $user_id Optional. ID of the user whose blogs are being |
|---|
| 117 | * retrieved. Default: 0. |
|---|
| 118 | * @param string|bool $search_terms Optional. Search by text stored in |
|---|
| 119 | * blogmeta (such as the blog name). Default: false. |
|---|
| 120 | * @param bool $show_hidden. Whether to include private blogs of user |
|---|
| 121 | * } |
|---|
| 122 | * @return array Multidimensional results array, structured as follows: |
|---|
| 123 | * 'blogs' - Array of located blog objects |
|---|
| 124 | * 'total' - A count of the total blogs matching the filter params |
|---|
| 125 | * |
|---|
| 126 | * @version 2, stergatu merged all func arguments into an array and added show hidden |
|---|
| 127 | * |
|---|
| 128 | */ |
|---|
| 129 | public static function get($args = array()) { |
|---|
| 130 | global $bp, $wpdb; |
|---|
| 131 | // Backward compatibility with old method of passing arguments |
|---|
| 132 | if (!is_array($args) || func_num_args() > 1) { |
|---|
| 133 | _deprecated_argument(__METHOD__, '2', sprintf(__('Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress'), __METHOD__, __FILE__)); |
|---|
| 134 | |
|---|
| 135 | $old_args_keys = array( |
|---|
| 136 | 0 => 'type', |
|---|
| 137 | 1 => 'per_page', |
|---|
| 138 | 2 => 'page', |
|---|
| 139 | 3 => 'user_id', |
|---|
| 140 | 4 => 'search_terms', |
|---|
| 141 | 5 => 'show_hidden', |
|---|
| 142 | ); |
|---|
| 143 | |
|---|
| 144 | $func_args = func_get_args(); |
|---|
| 145 | $args = bp_core_parse_args_array($old_args_keys, $func_args); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | $defaults = array( |
|---|
| 149 | 'type' => 'active', |
|---|
| 150 | 'per_page' => false, |
|---|
| 151 | 'page' => false, |
|---|
| 152 | 'user_id' => 0, |
|---|
| 153 | 'search_terms' => false, // Terms to search by |
|---|
| 154 | 'show_hidden' => false, // Show blogs marked hidden |
|---|
| 155 | ); |
|---|
| 156 | $r = wp_parse_args($args, $defaults); |
|---|
| 157 | extract($r); |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | $hidden_sql = ''; |
|---|
| 161 | if (!bp_is_my_profile() && !$show_hidden && !bp_current_user_can('bp_moderate')) |
|---|
| 162 | $hidden_sql = " AND wb.public = 1"; |
|---|
| 163 | |
|---|
| 164 | $pag_sql = ( $per_page && $page ) ? $wpdb->prepare(" LIMIT %d, %d", intval(( $page - 1 ) * $per_page), intval($per_page)) : ''; |
|---|
| 165 | |
|---|
| 166 | $user_sql = !empty($user_id) ? $wpdb->prepare(" AND b.user_id = %d", $user_id) : ''; |
|---|
| 167 | |
|---|
| 168 | switch ($type) { |
|---|
| 169 | case 'active': default: |
|---|
| 170 | $order_sql = "ORDER BY bm.meta_value DESC"; |
|---|
| 171 | break; |
|---|
| 172 | case 'alphabetical': |
|---|
| 173 | $order_sql = "ORDER BY bm2.meta_value ASC"; |
|---|
| 174 | break; |
|---|
| 175 | case 'newest': |
|---|
| 176 | $order_sql = "ORDER BY wb.registered DESC"; |
|---|
| 177 | break; |
|---|
| 178 | case 'random': |
|---|
| 179 | $order_sql = "ORDER BY RAND()"; |
|---|
| 180 | break; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | if (!empty($search_terms)) { |
|---|
| 184 | $filter = esc_sql(like_escape($search_terms)); |
|---|
| 185 | $paged_blogs = $wpdb->get_results("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, bm2.meta_value as name FROM {$bp->blogs->table_name} b, {$bp->blogs->table_name_blogmeta} bm, {$bp->blogs->table_name_blogmeta} bm2, {$wpdb->base_prefix}blogs wb, {$wpdb->users} u WHERE b.blog_id = wb.blog_id AND b.user_id = u.ID AND b.blog_id = bm.blog_id AND b.blog_id = bm2.blog_id AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} AND bm.meta_key = 'last_activity' AND bm2.meta_key = 'name' AND bm2.meta_value LIKE '%%$filter%%' {$user_sql} GROUP BY b.blog_id {$order_sql} {$pag_sql}"); |
|---|
| 186 | $total_blogs = $wpdb->get_var("SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b, {$wpdb->base_prefix}blogs wb, {$bp->blogs->table_name_blogmeta} bm, {$bp->blogs->table_name_blogmeta} bm2 WHERE b.blog_id = wb.blog_id AND bm.blog_id = b.blog_id AND bm2.blog_id = b.blog_id AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} AND bm.meta_key = 'name' AND bm2.meta_key = 'description' AND ( bm.meta_value LIKE '%%$filter%%' || bm2.meta_value LIKE '%%$filter%%' ) {$user_sql}"); |
|---|
| 187 | } else { |
|---|
| 188 | $paged_blogs = $wpdb->get_results("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, bm2.meta_value as name FROM {$bp->blogs->table_name} b, {$bp->blogs->table_name_blogmeta} bm, {$bp->blogs->table_name_blogmeta} bm2, {$wpdb->base_prefix}blogs wb, {$wpdb->users} u WHERE b.blog_id = wb.blog_id AND b.user_id = u.ID AND b.blog_id = bm.blog_id AND b.blog_id = bm2.blog_id {$user_sql} AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql} AND bm.meta_key = 'last_activity' AND bm2.meta_key = 'name' GROUP BY b.blog_id {$order_sql} {$pag_sql}"); |
|---|
| 189 | $total_blogs = $wpdb->get_var("SELECT COUNT(DISTINCT b.blog_id) FROM {$bp->blogs->table_name} b, {$wpdb->base_prefix}blogs wb WHERE b.blog_id = wb.blog_id {$user_sql} AND wb.archived = '0' AND wb.spam = 0 AND wb.mature = 0 AND wb.deleted = 0 {$hidden_sql}"); |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | $blog_ids = array(); |
|---|
| 193 | foreach ((array) $paged_blogs as $blog) { |
|---|
| 194 | $blog_ids[] = (int) $blog->blog_id; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | $paged_blogs = BP_Blogs_Blog::get_blog_extras($paged_blogs, $blog_ids, $type); |
|---|
| 198 | |
|---|
| 199 | return array('blogs' => $paged_blogs, 'total' => $total_blogs); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | /** |
|---|
| 203 | * Delete the record of a given blog for all users. |
|---|
| 204 | * |
|---|
| 205 | * @param int $blog_id The blog being removed from all users. |
|---|
| 206 | * @return int|bool Number of rows deleted on success, false on failure. |
|---|
| 207 | */ |
|---|
| 208 | public static function delete_blog_for_all($blog_id) { |
|---|
| 209 | global $wpdb, $bp; |
|---|
| 210 | |
|---|
| 211 | bp_blogs_delete_blogmeta($blog_id); |
|---|
| 212 | return $wpdb->query($wpdb->prepare("DELETE FROM {$bp->blogs->table_name} WHERE blog_id = %d", $blog_id)); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | /** |
|---|
| 216 | * Delete the record of a given blog for a specific user. |
|---|
| 217 | * |
|---|
| 218 | * @param int $blog_id The blog being removed. |
|---|
| 219 | * @param int $user_id Optional. The ID of the user from whom the blog |
|---|
| 220 | * is being removed. If absent, defaults to the logged-in user ID. |
|---|
| 221 | * @return int|bool Number of rows deleted on success, false on failure. |
|---|
| 222 | */ |
|---|
| 223 | public static function delete_blog_for_user($blog_id, $user_id = null) { |
|---|
| 224 | global $wpdb, $bp; |
|---|
| 225 | |
|---|
| 226 | if (!$user_id) |
|---|
| 227 | $user_id = bp_loggedin_user_id(); |
|---|
| 228 | |
|---|
| 229 | return $wpdb->query($wpdb->prepare("DELETE FROM {$bp->blogs->table_name} WHERE user_id = %d AND blog_id = %d", $user_id, $blog_id)); |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | /** |
|---|
| 233 | * Delete all of a user's blog associations in the BP tables. |
|---|
| 234 | * |
|---|
| 235 | * @param int $user_id Optional. The ID of the user whose blog |
|---|
| 236 | * associations are being deleted. If absent, defaults to |
|---|
| 237 | * logged-in user ID. |
|---|
| 238 | * @return int|bool Number of rows deleted on success, false on failure. |
|---|
| 239 | */ |
|---|
| 240 | public static function delete_blogs_for_user($user_id = null) { |
|---|
| 241 | global $wpdb, $bp; |
|---|
| 242 | |
|---|
| 243 | if (!$user_id) |
|---|
| 244 | $user_id = bp_loggedin_user_id(); |
|---|
| 245 | |
|---|
| 246 | return $wpdb->query($wpdb->prepare("DELETE FROM {$bp->blogs->table_name} WHERE user_id = %d", $user_id)); |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | /** |
|---|
| 250 | * Get all of a user's blogs, as tracked by BuddyPress. |
|---|
| 251 | * |
|---|
| 252 | * Note that this is different from the WordPress function |
|---|
| 253 | * {@link get_blogs_of_user()}; the current method returns only those |
|---|
| 254 | * blogs that have been recorded by BuddyPress, while the WP function |
|---|
| 255 | * does a true query of a user's blog capabilities. |
|---|
| 256 | * |
|---|
| 257 | * @param int $user_id Optional. ID of the user whose blogs are being |
|---|
| 258 | * queried. Defaults to logged-in user. |
|---|
| 259 | * @param bool $show_hidden Optional. Whether to include blogs that are |
|---|
| 260 | * not marked public. Defaults to true when viewing one's own |
|---|
| 261 | * profile. |
|---|
| 262 | * @return array Multidimensional results array, structured as follows: |
|---|
| 263 | * 'blogs' - Array of located blog objects |
|---|
| 264 | * 'total' - A count of the total blogs for the user. |
|---|
| 265 | |
|---|
| 266 | @version 2 stergatu |
|---|
| 267 | */ |
|---|
| 268 | public static function get_blogs_for_user($user_id = 0, $show_hidden = false) { |
|---|
| 269 | global $bp, $wpdb; |
|---|
| 270 | |
|---|
| 271 | if (!$user_id) |
|---|
| 272 | $user_id = bp_displayed_user_id(); |
|---|
| 273 | |
|---|
| 274 | // Show logged in users their hidden blogs or user can moderate |
|---|
| 275 | if (!bp_is_my_profile() && !$show_hidden && !bp_current_user_can('bp_moderate')) |
|---|
| 276 | $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)); |
|---|
| 277 | else |
|---|
| 278 | $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)); |
|---|
| 279 | |
|---|
| 280 | $total_blog_count = BP_Blogs_Blog::total_blog_count_for_user($user_id, $show_hidden); |
|---|
| 281 | |
|---|
| 282 | $user_blogs = array(); |
|---|
| 283 | |
|---|
| 284 | foreach ((array) $blogs as $blog) { |
|---|
| 285 | $user_blogs[$blog->blog_id] = new stdClass; |
|---|
| 286 | $user_blogs[$blog->blog_id]->id = $blog->id; |
|---|
| 287 | $user_blogs[$blog->blog_id]->blog_id = $blog->blog_id; |
|---|
| 288 | $user_blogs[$blog->blog_id]->siteurl = ( is_ssl() ) ? 'https://' . $blog->domain . $blog->path : 'http://' . $blog->domain . $blog->path; |
|---|
| 289 | $user_blogs[$blog->blog_id]->name = $blog->name; |
|---|
| 290 | } |
|---|
| 291 | |
|---|
| 292 | return array('blogs' => $user_blogs, 'count' => $total_blog_count); |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | /** |
|---|
| 296 | * Get IDs of all of a user's blogs, as tracked by BuddyPress. |
|---|
| 297 | * |
|---|
| 298 | * This method always includes hidden blogs. |
|---|
| 299 | * |
|---|
| 300 | * @param int $user_id Optional. ID of the user whose blogs are being |
|---|
| 301 | * queried. Defaults to logged-in user. |
|---|
| 302 | * @return int The number of blogs associated with the user. |
|---|
| 303 | */ |
|---|
| 304 | public static function get_blog_ids_for_user($user_id = 0) { |
|---|
| 305 | global $bp, $wpdb; |
|---|
| 306 | |
|---|
| 307 | if (!$user_id) |
|---|
| 308 | $user_id = bp_displayed_user_id(); |
|---|
| 309 | |
|---|
| 310 | return $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM {$bp->blogs->table_name} WHERE user_id = %d", $user_id)); |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | /** |
|---|
| 314 | * Check whether a blog has been recorded by BuddyPress. |
|---|
| 315 | * |
|---|
| 316 | * @param int $blog_id ID of the blog being queried. |
|---|
| 317 | * @return int|null The ID of the first located entry in the BP table |
|---|
| 318 | * on success, otherwise null. |
|---|
| 319 | */ |
|---|
| 320 | public static function is_recorded($blog_id) { |
|---|
| 321 | global $bp, $wpdb; |
|---|
| 322 | |
|---|
| 323 | return $wpdb->get_var($wpdb->prepare("SELECT id FROM {$bp->blogs->table_name} WHERE blog_id = %d", $blog_id)); |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | /** |
|---|
| 327 | * Return a count of associated blogs for a given user. |
|---|
| 328 | * |
|---|
| 329 | * Includes hidden blogs when the logged-in user is the same as the |
|---|
| 330 | * $user_id parameter, or when the logged-in user has the bp_moderate |
|---|
| 331 | * cap. |
|---|
| 332 | * |
|---|
| 333 | * @param int $user_id Optional. ID of the user whose blogs are being |
|---|
| 334 | * queried. Defaults to logged-in user. |
|---|
| 335 | * @param type $show_hidden |
|---|
| 336 | * @return int Blog count for the user. |
|---|
| 337 | * @version 2.0, stergatu on 22/11/2013 |
|---|
| 338 | */ |
|---|
| 339 | public static function total_blog_count_for_user($user_id = null, $show_hidden = false) { |
|---|
| 340 | global $bp, $wpdb; |
|---|
| 341 | |
|---|
| 342 | if (!$user_id) |
|---|
| 343 | $user_id = bp_displayed_user_id(); |
|---|
| 344 | |
|---|
| 345 | |
|---|
| 346 | // Show logged in users their hidden blogs or show also hidden user's blog. |
|---|
| 347 | if (!bp_is_my_profile() && !$show_hidden && !bp_current_user_can('bp_moderate')) |
|---|
| 348 | 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)); |
|---|
| 349 | else |
|---|
| 350 | 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)); |
|---|
| 351 | } |
|---|
| 352 | |
|---|
| 353 | /** |
|---|
| 354 | * Return a list of blogs matching a search term. |
|---|
| 355 | * |
|---|
| 356 | * Matches against blog names and descriptions, as stored in the BP |
|---|
| 357 | * blogmeta table. |
|---|
| 358 | * |
|---|
| 359 | * @param string $filter The search term. |
|---|
| 360 | * @param int $limit Optional. The maximum number of items to return. |
|---|
| 361 | * Default: null (no limit). |
|---|
| 362 | * @param int $page Optional. The page of results to return. Default: |
|---|
| 363 | * null (no limit). |
|---|
| 364 | * @return array Multidimensional results array, structured as follows: |
|---|
| 365 | * 'blogs' - Array of located blog objects |
|---|
| 366 | * 'total' - A count of the total blogs matching the query. |
|---|
| 367 | * @version 2 stergatu |
|---|
| 368 | */ |
|---|
| 369 | public static function search_blogs($filter, $per_page = null, $page = null, $show_hidden = FALSE) { |
|---|
| 370 | global $wpdb, $bp; |
|---|
| 371 | |
|---|
| 372 | $filter = esc_sql(like_escape($filter)); |
|---|
| 373 | |
|---|
| 374 | $hidden_sql = ''; |
|---|
| 375 | |
|---|
| 376 | |
|---|
| 377 | if (!bp_is_my_profile() && !$show_hidden && !bp_current_user_can('bp_moderate')) |
|---|
| 378 | $hidden_sql = "AND wb.public = 1"; |
|---|
| 379 | |
|---|
| 380 | if ($per_page && $page) { |
|---|
| 381 | $pag_sql = $wpdb->prepare(" LIMIT %d, %d", intval(( $page - 1 ) * $per_page), intval($per_page)); |
|---|
| 382 | } |
|---|
| 383 | |
|---|
| 384 | $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 bm.meta_value LIKE '%%$filter%%' ) {$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}"); |
|---|
| 385 | $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 bm.meta_value LIKE '%%$filter%%' ) {$hidden_sql} AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY meta_value ASC"); |
|---|
| 386 | |
|---|
| 387 | return array('blogs' => $paged_blogs, 'total' => $total_blogs); |
|---|
| 388 | } |
|---|
| 389 | |
|---|
| 390 | /** |
|---|
| 391 | * Retrieve a list of all blogs. |
|---|
| 392 | * |
|---|
| 393 | * Query will include hidden blogs if the logged-in user has the |
|---|
| 394 | * 'bp_moderate' cap. |
|---|
| 395 | * |
|---|
| 396 | * @param int $limit Optional. The maximum number of items to return. |
|---|
| 397 | * Default: null (no limit). |
|---|
| 398 | * @param int $page Optional. The page of results to return. Default: |
|---|
| 399 | * null (no limit). |
|---|
| 400 | * @return array Multidimensional results array, structured as follows: |
|---|
| 401 | * 'blogs' - Array of located blog objects |
|---|
| 402 | * 'total' - A count of the total blogs. |
|---|
| 403 | * @version 2 stergatu |
|---|
| 404 | */ |
|---|
| 405 | public static function get_all($per_page = null, $page = null, $show_hidden = false) { |
|---|
| 406 | global $bp, $wpdb; |
|---|
| 407 | $hidden_sql = ''; |
|---|
| 408 | if (!bp_is_my_profile() && !$show_hidden && !bp_current_user_can('bp_moderate')) |
|---|
| 409 | $hidden_sql = "AND wb.public = 1"; |
|---|
| 410 | |
|---|
| 411 | |
|---|
| 412 | $pag_sql = ( $per_page && $page ) ? $wpdb->prepare(" LIMIT %d, %d", intval(( $page - 1 ) * $per_page), intval($per_page)) : ''; |
|---|
| 413 | |
|---|
| 414 | $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}"); |
|---|
| 415 | $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}"); |
|---|
| 416 | |
|---|
| 417 | return array('blogs' => $paged_blogs, 'total' => $total_blogs); |
|---|
| 418 | } |
|---|
| 419 | |
|---|
| 420 | /** |
|---|
| 421 | * Retrieve a list of blogs whose names start with a given letter. |
|---|
| 422 | * |
|---|
| 423 | * Query will include hidden blogs if the logged-in user has the |
|---|
| 424 | * 'bp_moderate' cap. |
|---|
| 425 | * |
|---|
| 426 | * @param string $letter. The letter you're looking for. |
|---|
| 427 | * @param int $per_page Optional. The maximum number of items to return. |
|---|
| 428 | * Default: null (no limit). |
|---|
| 429 | * @param int $page Optional. The page of results to return. Default: |
|---|
| 430 | * null (no limit). |
|---|
| 431 | * @return array Multidimensional results array, structured as follows: |
|---|
| 432 | * 'blogs' - Array of located blog objects. |
|---|
| 433 | * 'total' - A count of the total blogs matching the query. |
|---|
| 434 | * @version 2, stergatu changed show_hidden and $limit to $per_page |
|---|
| 435 | */ |
|---|
| 436 | public static function get_by_letter($letter, $per_page = null, $page = null, $show_hidden = false) { |
|---|
| 437 | global $bp, $wpdb; |
|---|
| 438 | |
|---|
| 439 | $letter = esc_sql(like_escape($letter)); |
|---|
| 440 | |
|---|
| 441 | $hidden_sql = ''; |
|---|
| 442 | |
|---|
| 443 | if (!bp_is_my_profile() && !$show_hidden && !bp_current_user_can('bp_moderate')) |
|---|
| 444 | $hidden_sql = "AND wb.public = 1"; |
|---|
| 445 | |
|---|
| 446 | |
|---|
| 447 | |
|---|
| 448 | |
|---|
| 449 | if ($per_page && $page) |
|---|
| 450 | $pag_sql = $wpdb->prepare(" LIMIT %d, %d", intval(( $page - 1 ) * $per_page), intval($per_page)); |
|---|
| 451 | |
|---|
| 452 | $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 bm.meta_value LIKE '$letter%%' {$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}"); |
|---|
| 453 | $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 bm.meta_value LIKE '$letter%%' {$hidden_sql} AND wb.mature = 0 AND wb.spam = 0 AND wb.archived = '0' AND wb.deleted = 0 ORDER BY bm.meta_value ASC"); |
|---|
| 454 | |
|---|
| 455 | return array('blogs' => $paged_blogs, 'total' => $total_blogs); |
|---|
| 456 | } |
|---|
| 457 | |
|---|
| 458 | /** |
|---|
| 459 | * Fetch blog data not caught in the main query and append it to results array. |
|---|
| 460 | * |
|---|
| 461 | * Gets the following information, which is either unavailable at the |
|---|
| 462 | * time of the original query, or is more efficient to look up in one |
|---|
| 463 | * fell swoop: |
|---|
| 464 | * - The latest post for each blog, include Featured Image data |
|---|
| 465 | * - The blog description |
|---|
| 466 | * |
|---|
| 467 | * @param array $paged_blogs Array of results from the original query. |
|---|
| 468 | * @param array $blog_ids Array of IDs returned from the original query. |
|---|
| 469 | * @param string|bool $type Not currently used. Default: false. |
|---|
| 470 | * @return array $paged_blogs The located blogs array, with the extras added. |
|---|
| 471 | */ |
|---|
| 472 | public static function get_blog_extras(&$paged_blogs, &$blog_ids, $type = false) { |
|---|
| 473 | global $bp, $wpdb; |
|---|
| 474 | |
|---|
| 475 | if (empty($blog_ids)) |
|---|
| 476 | return $paged_blogs; |
|---|
| 477 | |
|---|
| 478 | $blog_ids = implode(',', wp_parse_id_list($blog_ids)); |
|---|
| 479 | |
|---|
| 480 | for ($i = 0, $count = count($paged_blogs); $i < $count; ++$i) { |
|---|
| 481 | $blog_prefix = $wpdb->get_blog_prefix($paged_blogs[$i]->blog_id); |
|---|
| 482 | $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"); |
|---|
| 483 | $images = array(); |
|---|
| 484 | |
|---|
| 485 | // Add URLs to any Featured Image this post might have |
|---|
| 486 | if (!empty($paged_blogs[$i]->latest_post) && has_post_thumbnail($paged_blogs[$i]->latest_post->ID)) { |
|---|
| 487 | |
|---|
| 488 | // Grab 4 sizes of the image. Thumbnail. |
|---|
| 489 | $image = wp_get_attachment_image_src(get_post_thumbnail_id($paged_blogs[$i]->latest_post->ID), 'thumbnail', false); |
|---|
| 490 | if (!empty($image)) |
|---|
| 491 | $images['thumbnail'] = $image[0]; |
|---|
| 492 | |
|---|
| 493 | // Medium |
|---|
| 494 | $image = wp_get_attachment_image_src(get_post_thumbnail_id($paged_blogs[$i]->latest_post->ID), 'medium', false); |
|---|
| 495 | if (!empty($image)) |
|---|
| 496 | $images['medium'] = $image[0]; |
|---|
| 497 | |
|---|
| 498 | // Large |
|---|
| 499 | $image = wp_get_attachment_image_src(get_post_thumbnail_id($paged_blogs[$i]->latest_post->ID), 'large', false); |
|---|
| 500 | if (!empty($image)) |
|---|
| 501 | $images['large'] = $image[0]; |
|---|
| 502 | |
|---|
| 503 | // Post thumbnail |
|---|
| 504 | $image = wp_get_attachment_image_src(get_post_thumbnail_id($paged_blogs[$i]->latest_post->ID), 'post-thumbnail', false); |
|---|
| 505 | if (!empty($image)) |
|---|
| 506 | $images['post-thumbnail'] = $image[0]; |
|---|
| 507 | |
|---|
| 508 | // Add the images to the latest_post object |
|---|
| 509 | $paged_blogs[$i]->latest_post->images = $images; |
|---|
| 510 | } |
|---|
| 511 | } |
|---|
| 512 | |
|---|
| 513 | /* Fetch the blog description for each blog (as it may be empty we can't fetch it in the main query). */ |
|---|
| 514 | $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} )"); |
|---|
| 515 | |
|---|
| 516 | for ($i = 0, $count = count($paged_blogs); $i < $count; ++$i) { |
|---|
| 517 | foreach ((array) $blog_descs as $desc) { |
|---|
| 518 | if ($desc->blog_id == $paged_blogs[$i]->blog_id) |
|---|
| 519 | $paged_blogs[$i]->description = $desc->description; |
|---|
| 520 | } |
|---|
| 521 | } |
|---|
| 522 | |
|---|
| 523 | return $paged_blogs; |
|---|
| 524 | } |
|---|
| 525 | |
|---|
| 526 | /** |
|---|
| 527 | * Check whether a given blog is hidden. |
|---|
| 528 | * |
|---|
| 529 | * Checks the 'public' column in the wp_blogs table. |
|---|
| 530 | * |
|---|
| 531 | * @param int $blog_id The ID of the blog being checked. |
|---|
| 532 | * @return bool True if hidden (public = 0), false otherwise. |
|---|
| 533 | */ |
|---|
| 534 | public static function is_hidden($blog_id) { |
|---|
| 535 | global $wpdb; |
|---|
| 536 | |
|---|
| 537 | if (!(int) $wpdb->get_var($wpdb->prepare("SELECT DISTINCT public FROM {$wpdb->base_prefix}blogs WHERE blog_id = %d", $blog_id))) |
|---|
| 538 | return true; |
|---|
| 539 | |
|---|
| 540 | return false; |
|---|
| 541 | } |
|---|
| 542 | |
|---|
| 543 | } |
|---|