1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Blogs component functions. |
---|
5 | * |
---|
6 | * @package BuddyPress |
---|
7 | * @subpackage BlogsFunctions |
---|
8 | */ |
---|
9 | // Exit if accessed directly |
---|
10 | if (!defined('ABSPATH')) |
---|
11 | exit; |
---|
12 | |
---|
13 | /** |
---|
14 | * Check whether the $bp global lists an activity directory page. |
---|
15 | * |
---|
16 | * @since BuddyPress (1.5.0) |
---|
17 | * |
---|
18 | * @global BuddyPress $bp The one true BuddyPress instance. |
---|
19 | * |
---|
20 | * @return bool True if set, false if empty. |
---|
21 | */ |
---|
22 | function bp_blogs_has_directory() { |
---|
23 | global $bp; |
---|
24 | |
---|
25 | return (bool) !empty($bp->pages->blogs->id); |
---|
26 | } |
---|
27 | |
---|
28 | /** |
---|
29 | * Retrieve a set of blogs |
---|
30 | * |
---|
31 | * @see BP_Blogs_Blog::get() for a description of arguments and return value. |
---|
32 | * |
---|
33 | * @param array $args { |
---|
34 | * Arguments are listed here with their default values. For more |
---|
35 | * information about the arguments, see {@link BP_Blogs_Blog::get()}. |
---|
36 | * @type string $type Default: 'active'. |
---|
37 | * @type int|bool $user_id Default: false. |
---|
38 | * @type string|bool $search_terms Default: false. |
---|
39 | * @type int $per_page Default: 20. |
---|
40 | * @type int $page Default: 1. |
---|
41 | * @type boolean $show_hidden Default: false. |
---|
42 | * } |
---|
43 | * @return array See {@link BP_Blogs_Blog::get()}. |
---|
44 | * @return type |
---|
45 | * @version 2, stergatu, added show_hidden |
---|
46 | */ |
---|
47 | function bp_blogs_get_blogs($args = '') { |
---|
48 | |
---|
49 | $defaults = array( |
---|
50 | 'type' => 'active', // active, alphabetical, newest, or random |
---|
51 | 'user_id' => false, // Pass a user_id to limit to only blogs that this user has privilages higher than subscriber on |
---|
52 | 'search_terms' => false, // Limit to blogs that match these search terms |
---|
53 | 'per_page' => 20, // The number of results to return per page |
---|
54 | 'page' => 1, // The page to return if limiting per page |
---|
55 | 'show_hidden' => false |
---|
56 | ); |
---|
57 | |
---|
58 | $params = wp_parse_args($args, $defaults); |
---|
59 | extract($params, EXTR_SKIP); |
---|
60 | |
---|
61 | $args_n = array('type' => $type, 'per_page' => $per_page, 'page' => $page, 'user_id' => $user_id, 'search_terms' => $search_terms, 'show_hidden' => $show_hidden); |
---|
62 | |
---|
63 | return apply_filters('bp_blogs_get_blogs', BP_Blogs_Blog::get($args_n), $params); |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | * Populate the BP blogs table with existing blogs. |
---|
68 | * |
---|
69 | * @global object $bp BuddyPress global settings |
---|
70 | * @global object $wpdb WordPress database object |
---|
71 | * @uses get_users() |
---|
72 | * @uses bp_blogs_record_blog() |
---|
73 | */ |
---|
74 | function bp_blogs_record_existing_blogs() { |
---|
75 | global $bp, $wpdb; |
---|
76 | |
---|
77 | // Truncate user blogs table and re-record. |
---|
78 | $wpdb->query("TRUNCATE TABLE {$bp->blogs->table_name}"); |
---|
79 | |
---|
80 | if (is_multisite()) { |
---|
81 | $blog_ids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM {$wpdb->base_prefix}blogs WHERE mature = 0 AND spam = 0 AND deleted = 0 AND site_id = %d", $wpdb->siteid)); |
---|
82 | } else { |
---|
83 | $blog_ids = 1; |
---|
84 | } |
---|
85 | |
---|
86 | if (!empty($blog_ids)) { |
---|
87 | foreach ((array) $blog_ids as $blog_id) { |
---|
88 | $users = get_users(array('blog_id' => $blog_id, 'fields' => 'ID')); |
---|
89 | $subscribers = get_users(array('blog_id' => $blog_id, 'fields' => 'ID', 'role' => 'subscriber')); |
---|
90 | |
---|
91 | if (!empty($users)) { |
---|
92 | foreach ((array) $users as $user) { |
---|
93 | // Don't record blogs for subscribers |
---|
94 | if (!in_array($user, $subscribers)) { |
---|
95 | bp_blogs_record_blog($blog_id, $user, true); |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * Check whether a given blog should be recorded in activity streams. |
---|
105 | * |
---|
106 | * If $user_id is provided, you can restrict site from being recordable |
---|
107 | * only to particular users. |
---|
108 | * |
---|
109 | * @since BuddyPress (1.7.0) |
---|
110 | * |
---|
111 | * @uses apply_filters() |
---|
112 | * |
---|
113 | * @param int $blog_id ID of the blog being checked. |
---|
114 | * @param int $user_id Optional. ID of the user for whom access is being checked. |
---|
115 | * @return bool True if blog is recordable, otherwise false. |
---|
116 | */ |
---|
117 | function bp_blogs_is_blog_recordable($blog_id, $user_id = 0) { |
---|
118 | |
---|
119 | $recordable_globally = apply_filters('bp_blogs_is_blog_recordable', true, $blog_id); |
---|
120 | |
---|
121 | if (!empty($user_id)) { |
---|
122 | $recordable_for_user = apply_filters('bp_blogs_is_blog_recordable_for_user', $recordable_globally, $blog_id, $user_id); |
---|
123 | } else { |
---|
124 | $recordable_for_user = $recordable_globally; |
---|
125 | } |
---|
126 | |
---|
127 | if (!empty($recordable_for_user)) { |
---|
128 | return true; |
---|
129 | } |
---|
130 | |
---|
131 | return $recordable_globally; |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * Check whether a given blog should be tracked by the Blogs component. |
---|
136 | * |
---|
137 | * If $user_id is provided, the developer can restrict site from |
---|
138 | * being trackable only to particular users. |
---|
139 | * |
---|
140 | * @since BuddyPress (1.7.0) |
---|
141 | * |
---|
142 | * @uses bp_blogs_is_blog_recordable |
---|
143 | * @uses apply_filters() |
---|
144 | * |
---|
145 | * @param int $blog_id ID of the blog being checked. |
---|
146 | * @param int $user_id Optional. ID of the user for whom access is being checked. |
---|
147 | * @return bool True if blog is trackable, otherwise false. |
---|
148 | */ |
---|
149 | function bp_blogs_is_blog_trackable($blog_id, $user_id = 0) { |
---|
150 | |
---|
151 | $trackable_globally = apply_filters('bp_blogs_is_blog_trackable', bp_blogs_is_blog_recordable($blog_id, $user_id), $blog_id); |
---|
152 | |
---|
153 | if (!empty($user_id)) { |
---|
154 | $trackable_for_user = apply_filters('bp_blogs_is_blog_trackable_for_user', $trackable_globally, $blog_id, $user_id); |
---|
155 | } else { |
---|
156 | $trackable_for_user = $trackable_globally; |
---|
157 | } |
---|
158 | |
---|
159 | if (!empty($trackable_for_user)) { |
---|
160 | return $trackable_for_user; |
---|
161 | } |
---|
162 | |
---|
163 | return $trackable_globally; |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * Make BuddyPress aware of a new site so that it can track its activity. |
---|
168 | * |
---|
169 | * @since BuddyPress (1.0.0) |
---|
170 | * |
---|
171 | * @uses BP_Blogs_Blog |
---|
172 | * |
---|
173 | * @param int $blog_id ID of the blog being recorded. |
---|
174 | * @param int $user_id ID of the user for whom the blog is being recorded. |
---|
175 | * @param bool $no_activity Optional. Whether to skip recording an activity |
---|
176 | * item about this blog creation. Default: false. |
---|
177 | * @return bool|null Returns false on failure. |
---|
178 | */ |
---|
179 | function bp_blogs_record_blog($blog_id, $user_id, $no_activity = false) { |
---|
180 | |
---|
181 | if (empty($user_id)) |
---|
182 | $user_id = bp_loggedin_user_id(); |
---|
183 | |
---|
184 | // If blog is not recordable, do not record the activity. |
---|
185 | if (!bp_blogs_is_blog_recordable($blog_id, $user_id)) |
---|
186 | return false; |
---|
187 | |
---|
188 | $name = get_blog_option($blog_id, 'blogname'); |
---|
189 | $description = get_blog_option($blog_id, 'blogdescription'); |
---|
190 | |
---|
191 | if (empty($name)) |
---|
192 | return false; |
---|
193 | |
---|
194 | $recorded_blog = new BP_Blogs_Blog; |
---|
195 | $recorded_blog->user_id = $user_id; |
---|
196 | $recorded_blog->blog_id = $blog_id; |
---|
197 | $recorded_blog_id = $recorded_blog->save(); |
---|
198 | $is_recorded = !empty($recorded_blog_id) ? true : false; |
---|
199 | |
---|
200 | bp_blogs_update_blogmeta($recorded_blog->blog_id, 'name', $name); |
---|
201 | bp_blogs_update_blogmeta($recorded_blog->blog_id, 'description', $description); |
---|
202 | bp_blogs_update_blogmeta($recorded_blog->blog_id, 'last_activity', bp_core_current_time()); |
---|
203 | |
---|
204 | $is_private = !empty($_POST['blog_public']) && (int) $_POST['blog_public'] ? false : true; |
---|
205 | $is_private = !apply_filters('bp_is_new_blog_public', !$is_private); |
---|
206 | |
---|
207 | // Only record this activity if the blog is public |
---|
208 | if (!$is_private && !$no_activity && bp_blogs_is_blog_trackable($blog_id, $user_id)) { |
---|
209 | |
---|
210 | // Record this in activity streams |
---|
211 | bp_blogs_record_activity(array( |
---|
212 | 'user_id' => $recorded_blog->user_id, |
---|
213 | 'action' => apply_filters('bp_blogs_activity_created_blog_action', sprintf(__('%s created the site %s', 'buddypress'), bp_core_get_userlink($recorded_blog->user_id), '<a href="' . get_home_url($recorded_blog->blog_id) . '">' . esc_attr($name) . '</a>'), $recorded_blog, $name, $description), |
---|
214 | 'primary_link' => apply_filters('bp_blogs_activity_created_blog_primary_link', get_home_url($recorded_blog->blog_id), $recorded_blog->blog_id), |
---|
215 | 'type' => 'new_blog', |
---|
216 | 'item_id' => $recorded_blog->blog_id |
---|
217 | )); |
---|
218 | } |
---|
219 | |
---|
220 | do_action_ref_array('bp_blogs_new_blog', array(&$recorded_blog, $is_private, $is_recorded)); |
---|
221 | } |
---|
222 | |
---|
223 | add_action('wpmu_new_blog', 'bp_blogs_record_blog', 10, 2); |
---|
224 | |
---|
225 | /** |
---|
226 | * Update blog name in BuddyPress blogmeta table. |
---|
227 | * |
---|
228 | * @global object $wpdb DB Layer. |
---|
229 | * |
---|
230 | * @param string $oldvalue Value before save. Passed by do_action() but |
---|
231 | * unused here. |
---|
232 | * @param string $newvalue Value to change meta to. |
---|
233 | */ |
---|
234 | function bp_blogs_update_option_blogname($oldvalue, $newvalue) { |
---|
235 | global $wpdb; |
---|
236 | |
---|
237 | bp_blogs_update_blogmeta($wpdb->blogid, 'name', $newvalue); |
---|
238 | } |
---|
239 | |
---|
240 | add_action('update_option_blogname', 'bp_blogs_update_option_blogname', 10, 2); |
---|
241 | |
---|
242 | /** |
---|
243 | * Update blog description in BuddyPress blogmeta table |
---|
244 | * |
---|
245 | * @global object $wpdb DB Layer. |
---|
246 | * |
---|
247 | * @param string $oldvalue Value before save. Passed by do_action() but |
---|
248 | * unused here. |
---|
249 | * @param string $newvalue Value to change meta to. |
---|
250 | */ |
---|
251 | function bp_blogs_update_option_blogdescription($oldvalue, $newvalue) { |
---|
252 | global $wpdb; |
---|
253 | |
---|
254 | bp_blogs_update_blogmeta($wpdb->blogid, 'description', $newvalue); |
---|
255 | } |
---|
256 | |
---|
257 | add_action('update_option_blogdescription', 'bp_blogs_update_option_blogdescription', 10, 2); |
---|
258 | |
---|
259 | /** |
---|
260 | * Detect a change in post status, and initiate an activity update if necessary. |
---|
261 | * |
---|
262 | * Posts get new activity updates when (a) they are being published, and (b) |
---|
263 | * they have not already been published. This enables proper posting for |
---|
264 | * regular posts as well as scheduled posts, while preventing post bumping. |
---|
265 | * |
---|
266 | * See #4090, #3746, #2546 for background. |
---|
267 | * |
---|
268 | * @since BuddyPress (1.9.0) |
---|
269 | * |
---|
270 | * @param string $new_status New status for the post. |
---|
271 | * @param string $old_status Old status for the post. |
---|
272 | * @param object $post Post data. |
---|
273 | */ |
---|
274 | function bp_blogs_catch_published_post($new_status, $old_status, $post) { |
---|
275 | |
---|
276 | // Only record published posts |
---|
277 | if ('publish' !== $new_status) { |
---|
278 | return; |
---|
279 | } |
---|
280 | |
---|
281 | // Don't record edits (publish -> publish) |
---|
282 | if ('publish' === $old_status) { |
---|
283 | return; |
---|
284 | } |
---|
285 | |
---|
286 | return bp_blogs_record_post($post->ID, $post); |
---|
287 | } |
---|
288 | |
---|
289 | add_action('transition_post_status', 'bp_blogs_catch_published_post', 10, 3); |
---|
290 | |
---|
291 | /** |
---|
292 | * Record a new blog post in the BuddyPress activity stream. |
---|
293 | * |
---|
294 | * @param int $post_id ID of the post being recorded. |
---|
295 | * @param object $post The WP post object passed to the 'save_post' action. |
---|
296 | * @param int $user_id Optional. The user to whom the activity item will be |
---|
297 | * associated. Defaults to the post_author. |
---|
298 | * @return bool|null Returns false on failure. |
---|
299 | */ |
---|
300 | function bp_blogs_record_post($post_id, $post, $user_id = 0) { |
---|
301 | global $bp, $wpdb; |
---|
302 | |
---|
303 | $post_id = (int) $post_id; |
---|
304 | $blog_id = (int) $wpdb->blogid; |
---|
305 | |
---|
306 | // If blog is not trackable, do not record the activity. |
---|
307 | if (!bp_blogs_is_blog_trackable($blog_id, $user_id)) |
---|
308 | return false; |
---|
309 | |
---|
310 | if (!$user_id) |
---|
311 | $user_id = (int) $post->post_author; |
---|
312 | |
---|
313 | // Stop infinite loops with WordPress MU Sitewide Tags. |
---|
314 | // That plugin changed the way its settings were stored at some point. Thus the dual check. |
---|
315 | if (!empty($bp->site_options['sitewide_tags_blog'])) { |
---|
316 | $st_options = maybe_unserialize($bp->site_options['sitewide_tags_blog']); |
---|
317 | $tags_blog_id = isset($st_options['tags_blog_id']) ? $st_options['tags_blog_id'] : 0; |
---|
318 | } else { |
---|
319 | $tags_blog_id = isset($bp->site_options['tags_blog_id']) ? $bp->site_options['tags_blog_id'] : 0; |
---|
320 | } |
---|
321 | |
---|
322 | if ((int) $blog_id == $tags_blog_id && apply_filters('bp_blogs_block_sitewide_tags_activity', true)) |
---|
323 | return false; |
---|
324 | |
---|
325 | // Don't record this if it's not a post |
---|
326 | if (!in_array($post->post_type, apply_filters('bp_blogs_record_post_post_types', array('post')))) |
---|
327 | return false; |
---|
328 | |
---|
329 | $is_blog_public = apply_filters('bp_is_blog_public', (int) get_blog_option($blog_id, 'blog_public')); |
---|
330 | |
---|
331 | if ('publish' == $post->post_status && empty($post->post_password)) { |
---|
332 | if ($is_blog_public || !is_multisite()) { |
---|
333 | |
---|
334 | // Record this in activity streams |
---|
335 | $post_permalink = add_query_arg( |
---|
336 | 'p', $post_id, trailingslashit(get_home_url($blog_id)) |
---|
337 | ); |
---|
338 | |
---|
339 | if (is_multisite()) |
---|
340 | $activity_action = sprintf(__('%1$s wrote a new post, %2$s, on the site %3$s', 'buddypress'), bp_core_get_userlink((int) $post->post_author), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>', '<a href="' . get_blog_option($blog_id, 'home') . '">' . get_blog_option($blog_id, 'blogname') . '</a>'); |
---|
341 | else |
---|
342 | $activity_action = sprintf(__('%1$s wrote a new post, %2$s', 'buddypress'), bp_core_get_userlink((int) $post->post_author), '<a href="' . $post_permalink . '">' . $post->post_title . '</a>'); |
---|
343 | |
---|
344 | // Make sure there's not an existing entry for this post (prevent bumping) |
---|
345 | if (bp_is_active('activity')) { |
---|
346 | $existing = bp_activity_get(array( |
---|
347 | 'filter' => array( |
---|
348 | 'action' => 'new_blog_post', |
---|
349 | 'primary_id' => $blog_id, |
---|
350 | 'secondary_id' => $post_id, |
---|
351 | ) |
---|
352 | )); |
---|
353 | |
---|
354 | if (!empty($existing['activities'])) { |
---|
355 | return; |
---|
356 | } |
---|
357 | } |
---|
358 | |
---|
359 | $activity_content = $post->post_content; |
---|
360 | |
---|
361 | bp_blogs_record_activity(array( |
---|
362 | 'user_id' => (int) $post->post_author, |
---|
363 | 'action' => apply_filters('bp_blogs_activity_new_post_action', $activity_action, $post, $post_permalink), |
---|
364 | 'content' => apply_filters('bp_blogs_activity_new_post_content', $activity_content, $post, $post_permalink), |
---|
365 | 'primary_link' => apply_filters('bp_blogs_activity_new_post_primary_link', $post_permalink, $post_id), |
---|
366 | 'type' => 'new_blog_post', |
---|
367 | 'item_id' => $blog_id, |
---|
368 | 'secondary_item_id' => $post_id, |
---|
369 | 'recorded_time' => $post->post_date_gmt, |
---|
370 | )); |
---|
371 | } |
---|
372 | |
---|
373 | // Update the blogs last activity |
---|
374 | bp_blogs_update_blogmeta($blog_id, 'last_activity', bp_core_current_time()); |
---|
375 | } else { |
---|
376 | bp_blogs_remove_post($post_id, $blog_id, $user_id); |
---|
377 | } |
---|
378 | |
---|
379 | do_action('bp_blogs_new_blog_post', $post_id, $post, $user_id); |
---|
380 | } |
---|
381 | |
---|
382 | /** |
---|
383 | * Record a new blog comment in the BuddyPress activity stream. |
---|
384 | * |
---|
385 | * Only posts the item if blog is public and post is not password-protected. |
---|
386 | * |
---|
387 | * @param int $comment_id ID of the comment being recorded. |
---|
388 | * @param bool|string $is_approved Optional. The $is_approved value passed to |
---|
389 | * the 'comment_post' action. Default: true. |
---|
390 | * @return bool|object Returns false on failure, the comment object on success. |
---|
391 | */ |
---|
392 | function bp_blogs_record_comment($comment_id, $is_approved = true) { |
---|
393 | // Get the users comment |
---|
394 | $recorded_comment = get_comment($comment_id); |
---|
395 | |
---|
396 | // Don't record activity if the comment hasn't been approved |
---|
397 | if (empty($is_approved)) |
---|
398 | return false; |
---|
399 | |
---|
400 | // Don't record activity if no email address has been included |
---|
401 | if (empty($recorded_comment->comment_author_email)) |
---|
402 | return false; |
---|
403 | |
---|
404 | // Don't record activity if the comment has already been marked as spam |
---|
405 | if ('spam' === $is_approved) |
---|
406 | return false; |
---|
407 | |
---|
408 | // Get the user by the comment author email. |
---|
409 | $user = get_user_by('email', $recorded_comment->comment_author_email); |
---|
410 | |
---|
411 | // If user isn't registered, don't record activity |
---|
412 | if (empty($user)) |
---|
413 | return false; |
---|
414 | |
---|
415 | // Get the user_id |
---|
416 | $user_id = (int) $user->ID; |
---|
417 | |
---|
418 | // Get blog and post data |
---|
419 | $blog_id = get_current_blog_id(); |
---|
420 | |
---|
421 | // If blog is not trackable, do not record the activity. |
---|
422 | if (!bp_blogs_is_blog_trackable($blog_id, $user_id)) |
---|
423 | return false; |
---|
424 | |
---|
425 | $recorded_comment->post = get_post($recorded_comment->comment_post_ID); |
---|
426 | |
---|
427 | if (empty($recorded_comment->post) || is_wp_error($recorded_comment->post)) |
---|
428 | return false; |
---|
429 | |
---|
430 | // If this is a password protected post, don't record the comment |
---|
431 | if (!empty($recorded_comment->post->post_password)) |
---|
432 | return false; |
---|
433 | |
---|
434 | // Don't record activity if the comment's associated post isn't a WordPress Post |
---|
435 | if (!in_array($recorded_comment->post->post_type, apply_filters('bp_blogs_record_comment_post_types', array('post')))) |
---|
436 | return false; |
---|
437 | |
---|
438 | $is_blog_public = apply_filters('bp_is_blog_public', (int) get_blog_option($blog_id, 'blog_public')); |
---|
439 | |
---|
440 | // If blog is public allow activity to be posted |
---|
441 | if ($is_blog_public) { |
---|
442 | |
---|
443 | // Get activity related links |
---|
444 | $post_permalink = get_permalink($recorded_comment->comment_post_ID); |
---|
445 | $comment_link = get_comment_link($recorded_comment->comment_ID); |
---|
446 | |
---|
447 | // Prepare to record in activity streams |
---|
448 | if (is_multisite()) |
---|
449 | $activity_action = sprintf(__('%1$s commented on the post, %2$s, on the site %3$s', 'buddypress'), bp_core_get_userlink($user_id), '<a href="' . $post_permalink . '">' . apply_filters('the_title', $recorded_comment->post->post_title) . '</a>', '<a href="' . get_blog_option($blog_id, 'home') . '">' . get_blog_option($blog_id, 'blogname') . '</a>'); |
---|
450 | else |
---|
451 | $activity_action = sprintf(__('%1$s commented on the post, %2$s', 'buddypress'), bp_core_get_userlink($user_id), '<a href="' . $post_permalink . '">' . apply_filters('the_title', $recorded_comment->post->post_title) . '</a>'); |
---|
452 | |
---|
453 | $activity_content = $recorded_comment->comment_content; |
---|
454 | |
---|
455 | // Record in activity streams |
---|
456 | bp_blogs_record_activity(array( |
---|
457 | 'user_id' => $user_id, |
---|
458 | 'action' => apply_filters_ref_array('bp_blogs_activity_new_comment_action', array($activity_action, &$recorded_comment, $comment_link)), |
---|
459 | 'content' => apply_filters_ref_array('bp_blogs_activity_new_comment_content', array($activity_content, &$recorded_comment, $comment_link)), |
---|
460 | 'primary_link' => apply_filters_ref_array('bp_blogs_activity_new_comment_primary_link', array($comment_link, &$recorded_comment)), |
---|
461 | 'type' => 'new_blog_comment', |
---|
462 | 'item_id' => $blog_id, |
---|
463 | 'secondary_item_id' => $comment_id, |
---|
464 | 'recorded_time' => $recorded_comment->comment_date_gmt |
---|
465 | )); |
---|
466 | |
---|
467 | // Update the blogs last active date |
---|
468 | bp_blogs_update_blogmeta($blog_id, 'last_activity', bp_core_current_time()); |
---|
469 | } |
---|
470 | |
---|
471 | return $recorded_comment; |
---|
472 | } |
---|
473 | |
---|
474 | add_action('comment_post', 'bp_blogs_record_comment', 10, 2); |
---|
475 | add_action('edit_comment', 'bp_blogs_record_comment', 10); |
---|
476 | |
---|
477 | /** |
---|
478 | * Record a user's association with a blog. |
---|
479 | * |
---|
480 | * This function is hooked to several WordPress actions where blog roles are |
---|
481 | * set/changed ('add_user_to_blog', 'profile_update', 'user_register'). It |
---|
482 | * parses the changes, and records them as necessary in the BP blog tracker. |
---|
483 | * |
---|
484 | * BuddyPress does not track blogs for Subscribers. |
---|
485 | * |
---|
486 | * @param int $user_id The ID of the user. |
---|
487 | * @param string|bool $role The WP role being assigned to the user |
---|
488 | * ('subscriber', 'contributor', 'author', 'editor', 'administrator', or |
---|
489 | * a custom role). Defaults to false. |
---|
490 | * @param int $blog_id Default: the current blog ID. |
---|
491 | * @return bool|null False on failure. |
---|
492 | */ |
---|
493 | function bp_blogs_add_user_to_blog($user_id, $role = false, $blog_id = 0) { |
---|
494 | global $wpdb; |
---|
495 | |
---|
496 | if (empty($blog_id)) { |
---|
497 | $blog_id = isset($wpdb->blogid) ? $wpdb->blogid : bp_get_root_blog_id(); |
---|
498 | } |
---|
499 | |
---|
500 | if (empty($role)) { |
---|
501 | $key = $wpdb->get_blog_prefix($blog_id) . 'capabilities'; |
---|
502 | |
---|
503 | $roles = bp_get_user_meta($user_id, $key, true); |
---|
504 | |
---|
505 | if (is_array($roles)) |
---|
506 | $role = array_search(1, $roles); |
---|
507 | else |
---|
508 | return false; |
---|
509 | } |
---|
510 | |
---|
511 | if ($role != 'subscriber') |
---|
512 | bp_blogs_record_blog($blog_id, $user_id, true); |
---|
513 | } |
---|
514 | |
---|
515 | add_action('add_user_to_blog', 'bp_blogs_add_user_to_blog', 10, 3); |
---|
516 | add_action('profile_update', 'bp_blogs_add_user_to_blog'); |
---|
517 | add_action('user_register', 'bp_blogs_add_user_to_blog'); |
---|
518 | |
---|
519 | /** |
---|
520 | * Remove a blog-user pair from BP's blog tracker. |
---|
521 | * |
---|
522 | * @param int $user_id ID of the user whose blog is being removed. |
---|
523 | * @param int $blog_id Optional. ID of the blog being removed. Default: current blog ID. |
---|
524 | */ |
---|
525 | function bp_blogs_remove_user_from_blog($user_id, $blog_id = 0) { |
---|
526 | global $wpdb; |
---|
527 | |
---|
528 | if (empty($blog_id)) |
---|
529 | $blog_id = $wpdb->blogid; |
---|
530 | |
---|
531 | bp_blogs_remove_blog_for_user($user_id, $blog_id); |
---|
532 | } |
---|
533 | |
---|
534 | add_action('remove_user_from_blog', 'bp_blogs_remove_user_from_blog', 10, 2); |
---|
535 | |
---|
536 | /** |
---|
537 | * Rehook WP's maybe_add_existing_user_to_blog with a later priority |
---|
538 | * |
---|
539 | * WordPress catches add-user-to-blog requests at init:10. In some cases, this |
---|
540 | * can precede BP's Blogs component. This function bumps the priority of the |
---|
541 | * core function, so that we can be sure that the Blogs component is loaded |
---|
542 | * first. See http://buddypress.trac.wordpress.org/ticket/3916. |
---|
543 | * |
---|
544 | * @since BuddyPress (1.6) |
---|
545 | * @access private |
---|
546 | */ |
---|
547 | function bp_blogs_maybe_add_user_to_blog() { |
---|
548 | if (!is_multisite()) |
---|
549 | return; |
---|
550 | |
---|
551 | remove_action('init', 'maybe_add_existing_user_to_blog'); |
---|
552 | add_action('init', 'maybe_add_existing_user_to_blog', 20); |
---|
553 | } |
---|
554 | |
---|
555 | add_action('init', 'bp_blogs_maybe_add_user_to_blog', 1); |
---|
556 | |
---|
557 | /** |
---|
558 | * Remove the "blog created" item from the BP blogs tracker and activity stream. |
---|
559 | * |
---|
560 | * @param int $blog_id ID of the blog being removed. |
---|
561 | */ |
---|
562 | function bp_blogs_remove_blog($blog_id) { |
---|
563 | global $bp; |
---|
564 | |
---|
565 | $blog_id = (int) $blog_id; |
---|
566 | do_action('bp_blogs_before_remove_blog', $blog_id); |
---|
567 | |
---|
568 | BP_Blogs_Blog::delete_blog_for_all($blog_id); |
---|
569 | |
---|
570 | // Delete activity stream item |
---|
571 | bp_blogs_delete_activity(array('item_id' => $blog_id, 'component' => $bp->blogs->id, 'type' => 'new_blog')); |
---|
572 | |
---|
573 | do_action('bp_blogs_remove_blog', $blog_id); |
---|
574 | } |
---|
575 | |
---|
576 | add_action('delete_blog', 'bp_blogs_remove_blog'); |
---|
577 | |
---|
578 | /** |
---|
579 | * Remove a blog from the tracker for a specific user. |
---|
580 | * |
---|
581 | * @param int $user_id ID of the user for whom the blog is being removed. |
---|
582 | * @param int $blog_id ID of the blog being removed. |
---|
583 | */ |
---|
584 | function bp_blogs_remove_blog_for_user($user_id, $blog_id) { |
---|
585 | global $bp; |
---|
586 | |
---|
587 | $blog_id = (int) $blog_id; |
---|
588 | $user_id = (int) $user_id; |
---|
589 | |
---|
590 | do_action('bp_blogs_before_remove_blog_for_user', $blog_id, $user_id); |
---|
591 | |
---|
592 | BP_Blogs_Blog::delete_blog_for_user($blog_id, $user_id); |
---|
593 | |
---|
594 | // Delete activity stream item |
---|
595 | bp_blogs_delete_activity(array( |
---|
596 | 'item_id' => $blog_id, |
---|
597 | 'component' => $bp->blogs->id, |
---|
598 | 'type' => 'new_blog' |
---|
599 | )); |
---|
600 | |
---|
601 | do_action('bp_blogs_remove_blog_for_user', $blog_id, $user_id); |
---|
602 | } |
---|
603 | |
---|
604 | add_action('remove_user_from_blog', 'bp_blogs_remove_blog_for_user', 10, 2); |
---|
605 | |
---|
606 | /** |
---|
607 | * Remove a blog post activity item from the activity stream. |
---|
608 | * |
---|
609 | * @param int $post_id ID of the post to be removed. |
---|
610 | * @param int $blog_id Optional. Defaults to current blog ID. |
---|
611 | * @param int $user_id Optional. Defaults to the logged-in user ID. This param |
---|
612 | * is currently unused in the function (but is passed to hooks). |
---|
613 | */ |
---|
614 | function bp_blogs_remove_post($post_id, $blog_id = 0, $user_id = 0) { |
---|
615 | global $wpdb, $bp; |
---|
616 | |
---|
617 | if (empty($wpdb->blogid)) |
---|
618 | return false; |
---|
619 | |
---|
620 | $post_id = (int) $post_id; |
---|
621 | |
---|
622 | if (!$blog_id) |
---|
623 | $blog_id = (int) $wpdb->blogid; |
---|
624 | |
---|
625 | if (!$user_id) |
---|
626 | $user_id = bp_loggedin_user_id(); |
---|
627 | |
---|
628 | do_action('bp_blogs_before_remove_post', $blog_id, $post_id, $user_id); |
---|
629 | |
---|
630 | // Delete activity stream item |
---|
631 | bp_blogs_delete_activity(array('item_id' => $blog_id, 'secondary_item_id' => $post_id, 'component' => $bp->blogs->id, 'type' => 'new_blog_post')); |
---|
632 | |
---|
633 | do_action('bp_blogs_remove_post', $blog_id, $post_id, $user_id); |
---|
634 | } |
---|
635 | |
---|
636 | add_action('delete_post', 'bp_blogs_remove_post'); |
---|
637 | |
---|
638 | /** |
---|
639 | * Remove a blog comment activity item from the activity stream. |
---|
640 | * |
---|
641 | * @param int $comment_id ID of the comment to be removed. |
---|
642 | */ |
---|
643 | function bp_blogs_remove_comment($comment_id) { |
---|
644 | global $wpdb; |
---|
645 | |
---|
646 | // Delete activity stream item |
---|
647 | bp_blogs_delete_activity(array('item_id' => $wpdb->blogid, 'secondary_item_id' => $comment_id, 'type' => 'new_blog_comment')); |
---|
648 | |
---|
649 | do_action('bp_blogs_remove_comment', $wpdb->blogid, $comment_id, bp_loggedin_user_id()); |
---|
650 | } |
---|
651 | |
---|
652 | add_action('delete_comment', 'bp_blogs_remove_comment'); |
---|
653 | |
---|
654 | /** |
---|
655 | * When a blog comment status transition occurs, update the relevant activity's status. |
---|
656 | * |
---|
657 | * @since BuddyPress (1.6.0) |
---|
658 | * |
---|
659 | * @global object $bp BuddyPress global settings. |
---|
660 | * |
---|
661 | * @param string $new_status New comment status. |
---|
662 | * @param string $old_status Previous comment status. |
---|
663 | * @param object $comment Comment data. |
---|
664 | */ |
---|
665 | function bp_blogs_transition_activity_status($new_status, $old_status, $comment) { |
---|
666 | global $bp; |
---|
667 | |
---|
668 | // Check the Activity component is active |
---|
669 | if (!bp_is_active('activity')) |
---|
670 | return; |
---|
671 | |
---|
672 | /** |
---|
673 | * Activity currently doesn't have any concept of a trash, or an unapproved/approved state. |
---|
674 | * |
---|
675 | * If a blog comment transitions to a "delete" or "hold" status, delete the activity item. |
---|
676 | * If a blog comment transitions to trashed, or spammed, mark the activity as spam. |
---|
677 | * If a blog comment transitions to approved (and the activity exists), mark the activity as ham. |
---|
678 | * Otherwise, record the comment into the activity stream. |
---|
679 | */ |
---|
680 | // This clause was moved in from bp_blogs_remove_comment() in BuddyPress 1.6. It handles delete/hold. |
---|
681 | if (in_array($new_status, array('delete', 'hold'))) |
---|
682 | return bp_blogs_remove_comment($comment->comment_ID); |
---|
683 | |
---|
684 | // These clauses handle trash, spam, and un-spams. |
---|
685 | elseif (in_array($new_status, array('trash', 'spam'))) |
---|
686 | $action = 'spam_activity'; |
---|
687 | elseif ('approved' == $new_status) |
---|
688 | $action = 'ham_activity'; |
---|
689 | |
---|
690 | // Get the activity |
---|
691 | $activity_id = bp_activity_get_activity_id(array('component' => $bp->blogs->id, 'item_id' => get_current_blog_id(), 'secondary_item_id' => $comment->comment_ID, 'type' => 'new_blog_comment',)); |
---|
692 | |
---|
693 | // Check activity item exists |
---|
694 | if (!$activity_id) { |
---|
695 | |
---|
696 | // If no activity exists, but the comment has been approved, record it into the activity table. |
---|
697 | if ('approved' == $new_status) |
---|
698 | return bp_blogs_record_comment($comment->comment_ID, true); |
---|
699 | |
---|
700 | return; |
---|
701 | } |
---|
702 | |
---|
703 | // Create an activity object |
---|
704 | $activity = new BP_Activity_Activity($activity_id); |
---|
705 | if (empty($activity->component)) |
---|
706 | return; |
---|
707 | |
---|
708 | // Spam/ham the activity if it's not already in that state |
---|
709 | if ('spam_activity' == $action && !$activity->is_spam) { |
---|
710 | bp_activity_mark_as_spam($activity); |
---|
711 | } elseif ('ham_activity' == $action) { |
---|
712 | bp_activity_mark_as_ham($activity); |
---|
713 | } |
---|
714 | |
---|
715 | // Add "new_blog_comment" to the whitelisted activity types, so that the activity's Akismet history is generated |
---|
716 | $comment_akismet_history = create_function('$t', '$t[] = "new_blog_comment"; return $t;'); |
---|
717 | add_filter('bp_akismet_get_activity_types', $comment_akismet_history); |
---|
718 | |
---|
719 | // Save the updated activity |
---|
720 | $activity->save(); |
---|
721 | |
---|
722 | // Remove the "new_blog_comment" activity type whitelist so we don't break anything |
---|
723 | remove_filter('bp_akismet_get_activity_types', $comment_akismet_history); |
---|
724 | } |
---|
725 | |
---|
726 | add_action('transition_comment_status', 'bp_blogs_transition_activity_status', 10, 3); |
---|
727 | |
---|
728 | /** |
---|
729 | * Get the total number of blogs being tracked by BuddyPress. |
---|
730 | * |
---|
731 | * @return int $count Total blog count. |
---|
732 | */ |
---|
733 | function bp_blogs_total_blogs() { |
---|
734 | if (!$count = wp_cache_get('bp_total_blogs', 'bp')) { |
---|
735 | $blogs = BP_Blogs_Blog::get_all(); |
---|
736 | $count = $blogs['total']; |
---|
737 | wp_cache_set('bp_total_blogs', $count, 'bp'); |
---|
738 | } |
---|
739 | return $count; |
---|
740 | } |
---|
741 | |
---|
742 | /** |
---|
743 | * Get the total number of blogs being tracked by BP for a specific user. |
---|
744 | * |
---|
745 | * @param int $user_id ID of the user being queried. Default: on a user page, |
---|
746 | * the displayed user. Otherwise, the logged-in user. |
---|
747 | * @param bool $show_hidden |
---|
748 | * @return int $count Total blog count for the user. |
---|
749 | * @version 2, stergatu added show_hidden |
---|
750 | */ |
---|
751 | function bp_blogs_total_blogs_for_user($user_id = 0, $show_hidden = false) { |
---|
752 | |
---|
753 | if (empty($user_id)) |
---|
754 | $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id(); |
---|
755 | |
---|
756 | if (!$count = wp_cache_get('bp_total_blogs_for_user_' . $user_id, 'bp')) { |
---|
757 | $count = BP_Blogs_Blog::total_blog_count_for_user($user_id, $show_hidden); |
---|
758 | wp_cache_set('bp_total_blogs_for_user_' . $user_id, $count, 'bp'); |
---|
759 | } |
---|
760 | |
---|
761 | return $count; |
---|
762 | } |
---|
763 | |
---|
764 | /** |
---|
765 | * Remove the all data related to a given blog from the BP blogs tracker and activity stream. |
---|
766 | * |
---|
767 | * @param int $blog_id The ID of the blog to expunge. |
---|
768 | */ |
---|
769 | function bp_blogs_remove_data_for_blog($blog_id) { |
---|
770 | global $bp; |
---|
771 | |
---|
772 | do_action('bp_blogs_before_remove_data_for_blog', $blog_id); |
---|
773 | |
---|
774 | // If this is regular blog, delete all data for that blog. |
---|
775 | BP_Blogs_Blog::delete_blog_for_all($blog_id); |
---|
776 | |
---|
777 | // Delete activity stream item |
---|
778 | bp_blogs_delete_activity(array('item_id' => $blog_id, 'component' => $bp->blogs->id, 'type' => false)); |
---|
779 | |
---|
780 | do_action('bp_blogs_remove_data_for_blog', $blog_id); |
---|
781 | } |
---|
782 | |
---|
783 | add_action('delete_blog', 'bp_blogs_remove_data_for_blog', 1); |
---|
784 | |
---|
785 | /** |
---|
786 | * Get all of a user's blogs, as tracked by BuddyPress. |
---|
787 | * |
---|
788 | * @see BP_Blogs_Blog::get_blogs_for_user() for a description of parameters |
---|
789 | * and return values. |
---|
790 | * |
---|
791 | * @param int $user_id See {@BP_Blogs_Blog::get_blogs_for_user()}. |
---|
792 | * @param bool $show_hidden See {@BP_Blogs_Blog::get_blogs_for_user()}. |
---|
793 | * @return array See {@BP_Blogs_Blog::get_blogs_for_user()}. |
---|
794 | */ |
---|
795 | function bp_blogs_get_blogs_for_user($user_id, $show_hidden = false) { |
---|
796 | return BP_Blogs_Blog::get_blogs_for_user($user_id, $show_hidden); |
---|
797 | } |
---|
798 | |
---|
799 | /** |
---|
800 | * Retrieve a list of all blogs. |
---|
801 | * |
---|
802 | * @see BP_Blogs_Blog::get_all() for a description of parameters and return values. |
---|
803 | * |
---|
804 | * @param int $per_page See {@BP_Blogs_Blog::get_all()}. |
---|
805 | * @param int $page See {@BP_Blogs_Blog::get_all()}. |
---|
806 | * @return array See {@BP_Blogs_Blog::get_all()}. |
---|
807 | * @version 2 stergatu, add $show_hidden, change $limit to $per_page |
---|
808 | */ |
---|
809 | function bp_blogs_get_all_blogs($per_page = null, $page = null, $show_hidden = false) { |
---|
810 | return BP_Blogs_Blog::get_all($per_page, $page, $show_hidden); |
---|
811 | } |
---|
812 | |
---|
813 | /** |
---|
814 | * Retrieve a random list of blogs. |
---|
815 | * |
---|
816 | * @see BP_Blogs_Blog::get() for a description of parameters and return values. |
---|
817 | * |
---|
818 | * @param int $per_page See {@BP_Blogs_Blog::get()}. |
---|
819 | * @param int $page See {@BP_Blogs_Blog::get()}. |
---|
820 | * @return array See {@BP_Blogs_Blog::get()}. |
---|
821 | * @version 2 stergatu change $limit to $per_page add the way get is called |
---|
822 | */ |
---|
823 | function bp_blogs_get_random_blogs($per_page = null, $page = null) { |
---|
824 | $args = array('type' => 'random', 'per_page' => $per_page, 'page' => $page); |
---|
825 | return BP_Blogs_Blog::get($args); |
---|
826 | } |
---|
827 | |
---|
828 | /** |
---|
829 | * Check whether a given blog is hidden. |
---|
830 | * |
---|
831 | * @see BP_Blogs_Blog::is_hidden() for a description of parameters and return values. |
---|
832 | * |
---|
833 | * @param int $blog_id See {@BP_Blogs_Blog::is_hidden()}. |
---|
834 | * @return bool See {@BP_Blogs_Blog::is_hidden()}. |
---|
835 | */ |
---|
836 | function bp_blogs_is_blog_hidden($blog_id) { |
---|
837 | return BP_Blogs_Blog::is_hidden($blog_id); |
---|
838 | } |
---|
839 | |
---|
840 | /* * ***************************************************************************** |
---|
841 | * Blog meta functions |
---|
842 | * |
---|
843 | * These functions are used to store specific blogmeta in one global table, |
---|
844 | * rather than in each blog's options table. Significantly speeds up global blog |
---|
845 | * queries. By default each blog's name, description and last updated time are |
---|
846 | * stored and synced here. |
---|
847 | */ |
---|
848 | |
---|
849 | /** |
---|
850 | * Delete a metadta from the DB for a blog. |
---|
851 | * |
---|
852 | * @global object $wpdb WordPress database access object. |
---|
853 | * @global object $bp BuddyPress global settings. |
---|
854 | * |
---|
855 | * @param int $blog_id ID of the blog whose metadata is being deleted. |
---|
856 | * @param string $meta_key Optional. The key of the metadata being deleted. If |
---|
857 | * omitted, all BP metadata associated with the blog will be deleted. |
---|
858 | * @param string $meta_value Optional. If present, the metadata will only be |
---|
859 | * deleted if the meta_value matches this parameter. |
---|
860 | * @return bool True on success, false on failure. |
---|
861 | */ |
---|
862 | function bp_blogs_delete_blogmeta($blog_id, $meta_key = false, $meta_value = false) { |
---|
863 | global $wpdb, $bp; |
---|
864 | |
---|
865 | if (!is_numeric($blog_id)) |
---|
866 | return false; |
---|
867 | |
---|
868 | $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key); |
---|
869 | |
---|
870 | if (is_array($meta_value) || is_object($meta_value)) |
---|
871 | $meta_value = serialize($meta_value); |
---|
872 | |
---|
873 | $meta_value = trim($meta_value); |
---|
874 | |
---|
875 | if (!$meta_key) |
---|
876 | $wpdb->query($wpdb->prepare("DELETE FROM {$bp->blogs->table_name_blogmeta} WHERE blog_id = %d", $blog_id)); |
---|
877 | else if ($meta_value) |
---|
878 | $wpdb->query($wpdb->prepare("DELETE FROM {$bp->blogs->table_name_blogmeta} WHERE blog_id = %d AND meta_key = %s AND meta_value = %s", $blog_id, $meta_key, $meta_value)); |
---|
879 | else |
---|
880 | $wpdb->query($wpdb->prepare("DELETE FROM {$bp->blogs->table_name_blogmeta} WHERE blog_id = %d AND meta_key = %s", $blog_id, $meta_key)); |
---|
881 | |
---|
882 | wp_cache_delete('bp_blogs_blogmeta_' . $blog_id . '_' . $meta_key, 'bp'); |
---|
883 | |
---|
884 | return true; |
---|
885 | } |
---|
886 | |
---|
887 | /** |
---|
888 | * Get metadata for a given blog. |
---|
889 | * |
---|
890 | * @since BuddyPress (1.2.0) |
---|
891 | * |
---|
892 | * @global object $wpdb WordPress database access object. |
---|
893 | * @global object $bp BuddyPress global settings. |
---|
894 | * |
---|
895 | * @param int $blog_id ID of the blog whose metadata is being requested. |
---|
896 | * @param string $meta_key Optional. If present, only the metadata matching |
---|
897 | * that meta key will be returned. Otherwise, all metadata for the |
---|
898 | * blog will be fetched. |
---|
899 | * @return mixed The meta value(s) being requested. |
---|
900 | */ |
---|
901 | function bp_blogs_get_blogmeta($blog_id, $meta_key = '') { |
---|
902 | global $wpdb, $bp; |
---|
903 | |
---|
904 | $blog_id = (int) $blog_id; |
---|
905 | |
---|
906 | if (!$blog_id) |
---|
907 | return false; |
---|
908 | |
---|
909 | if (!empty($meta_key)) { |
---|
910 | $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key); |
---|
911 | |
---|
912 | if (!$metas = wp_cache_get('bp_blogs_blogmeta_' . $blog_id . '_' . $meta_key, 'bp')) { |
---|
913 | $metas = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM {$bp->blogs->table_name_blogmeta} WHERE blog_id = %d AND meta_key = %s", $blog_id, $meta_key)); |
---|
914 | wp_cache_set('bp_blogs_blogmeta_' . $blog_id . '_' . $meta_key, $metas, 'bp'); |
---|
915 | } |
---|
916 | } else { |
---|
917 | $metas = $wpdb->get_col($wpdb->prepare("SELECT meta_value FROM {$bp->blogs->table_name_blogmeta} WHERE blog_id = %d", $blog_id)); |
---|
918 | } |
---|
919 | |
---|
920 | if (empty($metas)) { |
---|
921 | if (empty($meta_key)) |
---|
922 | return array(); |
---|
923 | else |
---|
924 | return ''; |
---|
925 | } |
---|
926 | |
---|
927 | $metas = array_map('maybe_unserialize', (array) $metas); |
---|
928 | |
---|
929 | if (1 == count($metas)) |
---|
930 | return $metas[0]; |
---|
931 | else |
---|
932 | return $metas; |
---|
933 | } |
---|
934 | |
---|
935 | /** |
---|
936 | * Update a piece of blog meta. |
---|
937 | * |
---|
938 | * @global object $wpdb WordPress database access object. |
---|
939 | * @global object $bp BuddyPress global settings. |
---|
940 | * |
---|
941 | * @param int $blog_id ID of the blog whose metadata is being updated. |
---|
942 | * @param string $meta_key Key of the metadata being updated. |
---|
943 | * @param mixed $meta_value Value to be set. |
---|
944 | * @return bool True on success, false on failure. |
---|
945 | */ |
---|
946 | function bp_blogs_update_blogmeta($blog_id, $meta_key, $meta_value) { |
---|
947 | global $wpdb, $bp; |
---|
948 | |
---|
949 | if (!is_numeric($blog_id)) |
---|
950 | return false; |
---|
951 | |
---|
952 | $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key); |
---|
953 | |
---|
954 | if (is_string($meta_value)) |
---|
955 | $meta_value = stripslashes(esc_sql($meta_value)); |
---|
956 | |
---|
957 | $meta_value = maybe_serialize($meta_value); |
---|
958 | |
---|
959 | if (empty($meta_value)) |
---|
960 | return bp_blogs_delete_blogmeta($blog_id, $meta_key); |
---|
961 | |
---|
962 | $cur = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$bp->blogs->table_name_blogmeta} WHERE blog_id = %d AND meta_key = %s", $blog_id, $meta_key)); |
---|
963 | |
---|
964 | if (!$cur) |
---|
965 | $wpdb->query($wpdb->prepare("INSERT INTO {$bp->blogs->table_name_blogmeta} ( blog_id, meta_key, meta_value ) VALUES ( %d, %s, %s )", $blog_id, $meta_key, $meta_value)); |
---|
966 | else if ($cur->meta_value != $meta_value) |
---|
967 | $wpdb->query($wpdb->prepare("UPDATE {$bp->blogs->table_name_blogmeta} SET meta_value = %s WHERE blog_id = %d AND meta_key = %s", $meta_value, $blog_id, $meta_key)); |
---|
968 | else |
---|
969 | return false; |
---|
970 | |
---|
971 | wp_cache_set('bp_blogs_blogmeta_' . $blog_id . '_' . $meta_key, $meta_value, 'bp'); |
---|
972 | |
---|
973 | return true; |
---|
974 | } |
---|
975 | |
---|
976 | /** |
---|
977 | * Remove all blog associations for a given user. |
---|
978 | * |
---|
979 | * @param int $user_id ID whose blog data should be removed. |
---|
980 | * @return bool|null Returns false on failure. |
---|
981 | */ |
---|
982 | function bp_blogs_remove_data($user_id) { |
---|
983 | if (!is_multisite()) |
---|
984 | return false; |
---|
985 | |
---|
986 | do_action('bp_blogs_before_remove_data', $user_id); |
---|
987 | |
---|
988 | // If this is regular blog, delete all data for that blog. |
---|
989 | BP_Blogs_Blog::delete_blogs_for_user($user_id); |
---|
990 | |
---|
991 | do_action('bp_blogs_remove_data', $user_id); |
---|
992 | } |
---|
993 | |
---|
994 | add_action('wpmu_delete_user', 'bp_blogs_remove_data'); |
---|
995 | add_action('delete_user', 'bp_blogs_remove_data'); |
---|
996 | add_action('bp_make_spam_user', 'bp_blogs_remove_data'); |
---|