Skip to:
Content

BuddyPress.org

Ticket #297: bp-blogs.php

File bp-blogs.php, 26.7 KB (added by gwrey, 18 years ago)
Line 
1<?php
2require_once( 'bp-core.php' );
3
4define ( 'BP_BLOGS_VERSION', '1.0b1' );
5
6/* These will be moved into admin configurable settings */
7define ( 'TOTAL_RECORDED_POSTS', 10 );
8define ( 'TOTAL_RECORDED_COMMENTS', 25 );
9
10include_once( 'bp-blogs/bp-blogs-classes.php' );
11include_once( 'bp-blogs/bp-blogs-cssjs.php' );
12include_once( 'bp-blogs/bp-blogs-templatetags.php' );
13include_once( 'bp-blogs/bp-blogs-widgets.php' );
14include_once( 'bp-blogs/bp-blogs-ajax.php' );
15include_once( 'bp-blogs/directories/bp-blogs-directory-blogs.php' );
16
17
18/**************************************************************************
19 bp_blogs_install()
20
21 Sets up the database tables ready for use on a site installation.
22 **************************************************************************/
23
24function bp_blogs_install( $version ) {
25 global $wpdb, $bp;
26
27 if ( !empty($wpdb->charset) )
28 $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
29
30 $sql[] = "CREATE TABLE ". $bp['blogs']['table_name'] ." (
31 id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
32 user_id int(11) NOT NULL,
33 blog_id int(11) NOT NULL,
34 KEY user_id (user_id),
35 KEY blog_id (blog_id)
36 ) {$charset_collate};";
37
38 $sql[] = "CREATE TABLE ". $bp['blogs']['table_name_blog_posts'] ." (
39 id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
40 user_id int(11) NOT NULL,
41 blog_id int(11) NOT NULL,
42 post_id int(11) NOT NULL,
43 date_created datetime NOT NULL,
44 KEY user_id (user_id),
45 KEY blog_id (blog_id),
46 KEY post_id (post_id)
47 ) {$charset_collate};";
48
49 $sql[] = "CREATE TABLE ". $bp['blogs']['table_name_blog_comments'] ." (
50 id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
51 user_id int(11) NOT NULL,
52 blog_id int(11) NOT NULL,
53 comment_id int(11) NOT NULL,
54 comment_post_id int(11) NOT NULL,
55 date_created datetime NOT NULL,
56 KEY user_id (user_id),
57 KEY blog_id (blog_id),
58 KEY comment_id (comment_id),
59 KEY comment_post_id (comment_post_id)
60 ) {$charset_collate};";
61
62 $sql[] = "CREATE TABLE ". $bp['blogs']['table_name_blogmeta'] ." (
63 id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
64 blog_id int(11) NOT NULL,
65 meta_key varchar(255) DEFAULT NULL,
66 meta_value longtext DEFAULT NULL,
67 KEY blog_id (blog_id),
68 KEY meta_key (meta_key)
69 ) {$charset_collate};";
70
71
72 require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
73
74 dbDelta($sql);
75
76 // dbDelta won't change character sets, so we need to do this seperately.
77 // This will only be in here pre v1.0
78 $wpdb->query( $wpdb->prepare( "ALTER TABLE " . $bp['blogs']['table_name'] . " DEFAULT CHARACTER SET %s", $wpdb->charset ) );
79 $wpdb->query( $wpdb->prepare( "ALTER TABLE " . $bp['blogs']['table_name_blog_posts'] . " DEFAULT CHARACTER SET %s", $wpdb->charset ) );
80 $wpdb->query( $wpdb->prepare( "ALTER TABLE " . $bp['blogs']['table_name_blog_comments'] . " DEFAULT CHARACTER SET %s", $wpdb->charset ) );
81
82 // On first installation - record all existing blogs in the system.
83 if ( !(int)get_site_option( 'bp-blogs-first-install') ) {
84
85 bp_blogs_record_existing_blogs();
86 add_site_option( 'bp-blogs-first-install', 1 );
87
88 } else {
89
90 // Import blog titles and descriptions into the blogmeta table
91 if ( get_site_option( 'bp-blogs-version' ) <= '0.1.5' ) {
92 $blog_ids = $wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM " . $bp['blogs']['table_name'] ) );
93
94 for ( $i = 0; $i < count($blog_ids); $i++ ) {
95 $name = get_blog_option( $blog_ids[$i], 'blogname' );
96 $desc = get_blog_option( $blog_ids[$i], 'blogdescription' );
97
98 bp_blogs_update_blogmeta( $blog_ids[$i], 'name', $name );
99 bp_blogs_update_blogmeta( $blog_ids[$i], 'description', $desc );
100 bp_blogs_update_blogmeta( $blog_ids[$i], 'last_activity', time() );
101 }
102 }
103
104 }
105
106 add_site_option( 'bp-blogs-version', $version );
107}
108
109
110function bp_blogs_check_installed() {
111 global $wpdb, $bp, $userdata;
112
113 if ( is_site_admin() ) {
114 /* Need to check db tables exist, activate hook no-worky in mu-plugins folder. */
115 if ( ( $wpdb->get_var("show tables like '%" . $bp['blogs']['table_name'] . "%'") == false ) || ( get_site_option('bp-blogs-version') < BP_BLOGS_VERSION ) )
116 bp_blogs_install(BP_BLOGS_VERSION);
117 }
118}
119add_action( 'admin_menu', 'bp_blogs_check_installed' );
120
121
122/**************************************************************************
123 bp_blogs_setup_globals()
124
125 Set up and add all global variables for this component, and add them to
126 the $bp global variable array.
127 **************************************************************************/
128
129function bp_blogs_setup_globals() {
130 global $bp, $wpdb;
131
132 $bp['blogs'] = array(
133 'table_name' => $wpdb->base_prefix . 'bp_user_blogs',
134 'table_name_blog_posts' => $wpdb->base_prefix . 'bp_user_blogs_posts',
135 'table_name_blog_comments' => $wpdb->base_prefix . 'bp_user_blogs_comments',
136 'table_name_blogmeta' => $wpdb->base_prefix . 'bp_user_blogs_blogmeta',
137 'format_activity_function' => 'bp_blogs_format_activity',
138 'image_base' => site_url() . '/wp-content/mu-plugins/bp-groups/images',
139 'slug' => 'blogs'
140 );
141}
142add_action( 'wp', 'bp_blogs_setup_globals', 1 );
143add_action( '_admin_menu', 'bp_blogs_setup_globals', 1 );
144
145
146/**
147 * bp_blogs_setup_nav()
148 *
149 * Adds "Blog" to the navigation arrays for the current and logged in user.
150 * $bp['bp_nav'] represents the main component navigation
151 * $bp['bp_users_nav'] represents the sub navigation when viewing a users
152 * profile other than that of the current logged in user.
153 *
154 * @package BuddyPress Blogs
155 * @global $bp The global BuddyPress settings variable created in bp_core_setup_globals()
156 * @uses bp_is_home() Checks to see if the current user being viewed is the logged in user
157 */
158function bp_blogs_setup_nav() {
159 global $bp;
160
161 /* Add 'Blogs' to the main navigation */
162 bp_core_add_nav_item( __('Blogs', 'buddypress'), $bp['blogs']['slug'] );
163
164 if ( $bp['current_userid'] )
165 bp_core_add_nav_default( $bp['blogs']['slug'], 'bp_blogs_screen_my_blogs', 'my-blogs' );
166
167 $blogs_link = $bp['loggedin_domain'] . $bp['blogs']['slug'] . '/';
168
169 /* Add the subnav items to the blogs nav item */
170 bp_core_add_subnav_item( $bp['blogs']['slug'], 'my-blogs', __('My Blogs', 'buddypress'), $blogs_link, 'bp_blogs_screen_my_blogs', 'my-blogs-list' );
171 bp_core_add_subnav_item( $bp['blogs']['slug'], 'recent-posts', __('Recent Posts', 'buddypress'), $blogs_link, 'bp_blogs_screen_recent_posts' );
172 bp_core_add_subnav_item( $bp['blogs']['slug'], 'recent-comments', __('Recent Comments', 'buddypress'), $blogs_link, 'bp_blogs_screen_recent_comments' );
173 bp_core_add_subnav_item( $bp['blogs']['slug'], 'create-a-blog', __('Create a Blog', 'buddypress'), $blogs_link, 'bp_blogs_screen_create_a_blog' );
174
175 /* Set up the component options navigation for Blog */
176 if ( $bp['current_component'] == 'blogs' ) {
177 if ( bp_is_home() ) {
178 if ( function_exists('xprofile_setup_nav') ) {
179 $bp['bp_options_title'] = __('My Blogs', 'buddypress');
180 }
181 } else {
182 /* If we are not viewing the logged in user, set up the current users avatar and name */
183 $bp['bp_options_avatar'] = bp_core_get_avatar( $bp['current_userid'], 1 );
184 $bp['bp_options_title'] = $bp['current_fullname'];
185 }
186 }
187}
188add_action( 'wp', 'bp_blogs_setup_nav', 2 );
189
190function bp_blogs_screen_my_blogs() {
191 bp_catch_uri( 'blogs/my-blogs' );
192}
193
194function bp_blogs_screen_recent_posts() {
195 bp_catch_uri( 'blogs/recent-posts' );
196}
197
198function bp_blogs_screen_recent_comments() {
199 bp_catch_uri( 'blogs/recent-comments' );
200}
201
202function bp_blogs_screen_create_a_blog() {
203 bp_catch_uri( 'blogs/create' );
204}
205
206
207/**************************************************************************
208 bp_blogs_record_activity()
209
210 Records activity for the logged in user within the friends component so that
211 it will show in the users activity stream (if installed)
212 **************************************************************************/
213
214function bp_blogs_record_activity( $args = true ) {
215 global $bp;
216
217 /* Because blog, comment, and blog post code execution happens before anything else
218 we need to manually instantiate the activity component globals */
219 if ( !$bp['activity'] && function_exists('bp_activity_setup_globals') )
220 bp_activity_setup_globals();
221
222 if ( function_exists('bp_activity_record') ) {
223 extract($args);
224
225 bp_activity_record( $item_id, $component_name, $component_action, $is_private, $secondary_item_id, $user_id, $secondary_user_id );
226 }
227}
228
229function bp_blogs_delete_activity( $args = true ) {
230 if ( function_exists('bp_activity_delete') ) {
231 extract($args);
232 bp_activity_delete( $item_id, $component_name, $component_action, $user_id, $secondary_item_id );
233 }
234}
235
236/**************************************************************************
237 bp_blogs_format_activity()
238
239 Selects and formats recorded blogs component activity.
240 **************************************************************************/
241
242function bp_blogs_format_activity( $item_id, $user_id, $action, $secondary_item_id = false, $for_secondary_user = false ) {
243 global $bp;
244
245 switch( $action ) {
246 case 'new_blog':
247 $blog = new BP_Blogs_Blog($item_id);
248
249 if ( !$blog )
250 return false;
251
252 if ( !$user_id )
253 return false;
254
255 return array(
256 'primary_link' => get_blog_option( $blog->blog_id, 'siteurl' ),
257 'content' => sprintf( __( '%s created a new blog: %s', 'buddypress' ), bp_core_get_userlink($user_id), '<a href="' . get_blog_option( $blog->blog_id, 'siteurl' ) . '">' . get_blog_option( $blog->blog_id, 'blogname' ) . '</a>' ) . ' <span class="time-since">%s</span>'
258 );
259 break;
260 case 'new_blog_post':
261 $post = new BP_Blogs_Post($item_id);
262
263 if ( !$post )
264 return false;
265
266 $post = BP_Blogs_Post::fetch_post_content($post);
267
268 /* gwrey
269 we need to stay here as long as we DO have a post
270 so we can update the table to remove posts that were previously
271 added but have been changed to unpublished or have had a password added
272 so I removed "$post->post_status != 'publish' || $post->post_password != ''"
273 from the test below.
274 */
275 if ( !$post || $post->post_type != 'post')
276 return false;
277
278 $post_link = bp_post_get_permalink( $post, $post->blog_id );
279 $content = sprintf( __( '%s wrote a new blog post: %s', 'buddypress' ), bp_core_get_userlink($user_id), '<a href="' . $post_link . '">' . $post->post_title . '</a>' ) . ' <span class="time-since">%s</span>';
280 $content .= '<blockquote>' . bp_create_excerpt($post->post_content) . '</blockquote>';
281
282 return array(
283 'primary_link' => $post_link,
284 'content' => $content
285 );
286 break;
287 case 'new_blog_comment':
288
289 if ( !is_user_logged_in() )
290 return false;
291
292 $comment = new BP_Blogs_Comment($item_id);
293
294 if ( !$comment )
295 return false;
296
297 $comment = BP_Blogs_Comment::fetch_comment_content($comment);
298
299 if ( !$comment )
300 return false;
301
302 $post_link = bp_post_get_permalink( $comment->post, $comment->blog_id );
303 $content = sprintf( __( '%s commented on the blog post %s', 'buddypress' ), bp_core_get_userlink($user_id), '<a href="' . $post_link . '#comment-' . $comment->comment_ID . '">' . $comment->post->post_title . '</a>' ) . ' <span class="time-since">%s</span>';
304 $content .= '<blockquote>' . bp_create_excerpt($comment->comment_content) . '</blockquote>';
305
306 return array(
307 'primary_link' => $post_link . '#comment-' . $comment->comment_ID,
308 'content' => $content
309 );
310 break;
311 }
312
313 return false;
314}
315
316function bp_blogs_record_existing_blogs() {
317 global $wpdb;
318
319 $blog_ids = $wpdb->get_col( $wpdb->prepare( "SELECT blog_id FROM {$wpdb->base_prefix}blogs WHERE public = 1 AND mature = 0 AND spam = 0 AND deleted = 0" ) );
320
321 if ( $blog_ids ) {
322 foreach( $blog_ids as $blog_id ) {
323 $users = get_users_of_blog( $blog_id );
324
325 if ( $users ) {
326 foreach ( $users as $user ) {
327 $role = unserialize( $user->meta_value );
328
329 if ( !isset( $role['subscriber'] ) )
330 bp_blogs_record_blog( $blog_id, $user->user_id );
331 }
332 }
333 }
334 }
335}
336
337
338function bp_blogs_record_blog( $blog_id, $user_id ) {
339 global $bp;
340
341 if ( !$bp ) {
342 bp_core_setup_globals();
343 bp_blogs_setup_globals();
344 }
345
346 if ( !$user_id )
347 $user_id = $bp['loggedin_userid'];
348
349 $name = get_blog_option( $blog_id, 'blogname' );
350 $description = get_blog_option( $blog_id, 'blogdescription' );
351
352 $recorded_blog = new BP_Blogs_Blog;
353 $recorded_blog->user_id = $user_id;
354 $recorded_blog->blog_id = $blog_id;
355
356 $recorded_blog_id = $recorded_blog->save();
357
358 bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'name', $name );
359 bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'description', $description );
360 bp_blogs_update_blogmeta( $recorded_blog->blog_id, 'last_activity', time() );
361
362 $is_private = bp_blogs_is_blog_hidden( $recorded_blog_id );
363
364 // Record in activity streams
365 bp_blogs_record_activity( array( 'item_id' => $recorded_blog_id, 'component_name' => 'blogs', 'component_action' => 'new_blog', 'is_private' => $is_private ) );
366
367 do_action( 'bp_blogs_new_blog', $recorded_blog, $is_private );
368}
369add_action( 'wpmu_new_blog', 'bp_blogs_record_blog', 10, 2 );
370
371
372function bp_blogs_record_post($post_id) {
373 global $bp, $current_blog;
374
375 if ( !$bp ) {
376 bp_core_setup_globals();
377 bp_blogs_setup_globals();
378 }
379
380 $post_id = (int)$post_id;
381 $user_id = (int)$bp['loggedin_userid'];
382 $blog_id = (int)$current_blog->blog_id;
383
384 /* This is to stop infinate loops with Donncha's sitewide tags plugin */
385 if ( get_site_option('tags_blog_id') == $blog_id )
386 return false;
387
388 $post = get_post($post_id);
389
390 /* Don't record this if it's not a post, not published, or password protected */
391 /* gwrey
392 Actually, we need to stay here as long as we DO have a post
393 so we can update the table to remove posts that were previously
394 added but have been changed to unpublished or have had a password added
395 so I removed "$post->post_status != 'publish' || $post->post_password != ''"
396 from the test below.
397 */
398 if ( $post->post_type != 'post' )
399 return false;
400
401 if ( !BP_Blogs_Post::is_recorded( $post_id, $blog_id ) ) {
402 if ( $post->post_status == 'publish' ) {
403 /* gwrey
404 Moved the test for max total records inside this
405 block so we will only delete a record IF we are going to add a new one
406 */
407 /**
408 * Check how many recorded posts there are for the user. If we are
409 * at the max, then delete the oldest recorded post first.
410 */
411 if ( BP_Blogs_Post::get_total_recorded_for_user() >= TOTAL_RECORDED_POSTS )
412 BP_Blogs_Post::delete_oldest();
413
414 $recorded_post = new BP_Blogs_Post;
415 $recorded_post->user_id = $user_id;
416 $recorded_post->blog_id = $blog_id;
417 $recorded_post->post_id = $post_id;
418 $recorded_post->date_created = strtotime( $post->post_date );
419
420 $recorded_post_id = $recorded_post->save();
421
422 bp_blogs_update_blogmeta( $recorded_post->blog_id, 'last_activity', time() );
423
424 $is_private = bp_blogs_is_blog_hidden( $recorded_post->blog_id );
425
426 // Record in activity streams
427 bp_blogs_record_activity( array( 'item_id' => $recorded_post_id, 'component_name' => 'blogs', 'component_action' => 'new_blog_post', 'is_private' => $is_private, 'user_id' => $recorded_post->user_id ) );
428
429 do_action( 'bp_blogs_new_blog_post', $recorded_post, $is_private );
430 }
431 } else {
432 /**
433 * Check to see if the post have previously been recorded.
434 * If the post status has changed from public to private then we need
435 * to remove the record of the post.
436 */
437 /* gwrey
438 also need to delete if we now have a password
439 */
440 if ( $post->post_status != 'publish' || $post->post_password != '')
441 BP_Blogs_Post::delete( $post_id, $blog_id );
442 }
443}
444/* gwrey
445 we need this action on save_post rather than publish_post
446 so changes to a previously saved post will get updated.
447
448 add_action( 'publish_post', 'bp_blogs_record_post' );
449*/
450add_action( 'save_post', 'bp_blogs_record_post' );
451
452function bp_blogs_record_comment( $comment_id, $from_ajax = false ) {
453 global $bp, $current_blog, $current_user;
454
455 if ( !$bp ) {
456 bp_core_setup_globals();
457 bp_blogs_setup_globals();
458 }
459
460 $comment = get_comment($comment_id);
461
462 /* Get the user_id from the author email. */
463 $user = get_user_by_email( $comment->comment_author_email );
464 $user_id = (int)$user->ID;
465
466 /* Only record a comment if it is by a registered user. */
467 if ( $user_id ) {
468 $comment_id = (int)$comment_id;
469 $blog_id = (int)$current_blog->blog_id;
470 $post_id = (int)$comment->comment_post_ID;
471
472 /**
473 * Check how many recorded posts there are for the user. If we are
474 * at the max, then delete the oldest recorded post first.
475 */
476 if ( BP_Blogs_Comment::get_total_recorded_for_user() >= TOTAL_RECORDED_COMMENTS )
477 BP_Blogs_Comment::delete_oldest();
478
479 if ( !BP_Blogs_Comment::is_recorded( $comment_id, $post_id, $blog_id ) ) {
480 if ( $comment->comment_approved || $from_ajax ) {
481 $recorded_comment = new BP_Blogs_Comment;
482 $recorded_comment->user_id = $user_id;
483 $recorded_comment->blog_id = $blog_id;
484 $recorded_comment->comment_id = $comment_id;
485 $recorded_comment->comment_post_id = $post_id;
486 $recorded_comment->date_created = strtotime( $comment->comment_date );
487
488 $recorded_commment_id = $recorded_comment->save();
489
490 bp_blogs_update_blogmeta( $recorded_comment->blog_id, 'last_activity', time() );
491
492 $is_private = bp_blogs_is_blog_hidden( $recorded_comment->blog_id );
493
494 // Record in activity streams
495 bp_blogs_record_activity( array( 'item_id' => $recorded_commment_id, 'component_name' => 'blogs', 'component_action' => 'new_blog_comment', 'is_private' => $is_private, 'user_id' => $user_id ) );
496
497 do_action( 'bp_blogs_new_blog_comment', $recorded_comment, $is_private );
498 }
499 } else {
500 /**
501 * Check to see if the post have previously been recorded.
502 * If the post status has changed from public to private then we need
503 * to remove the record of the post.
504 */
505 if ( !$comment->comment_approved || $comment->comment_approved == 'spam' )
506 BP_Blogs_Comment::delete( $comment_id, $blog_id );
507 }
508 }
509}
510add_action( 'comment_post', 'bp_blogs_record_comment', 10, 2 );
511add_action( 'edit_comment', 'bp_blogs_record_comment', 10, 2 );
512
513
514function bp_blogs_modify_comment( $comment_id, $comment_status ) {
515 global $bp;
516
517 if ( !$bp ) {
518 bp_core_setup_globals();
519 bp_blogs_setup_globals();
520 }
521
522 $comment = get_comment($comment_id);
523
524 // This is backwards, but it's just the way things work with WP AJAX.
525 if ( $comment->comment_approved ) {
526 bp_blogs_remove_comment( $comment_id );
527 } else {
528 bp_blogs_record_comment( $comment_id, true );
529 }
530}
531add_action( 'wp_set_comment_status', 'bp_blogs_modify_comment', 10, 2 );
532
533function bp_blogs_remove_blog( $blog_id ) {
534 global $bp;
535
536 if ( !$bp ) {
537 bp_core_setup_globals();
538 bp_blogs_setup_globals();
539 }
540
541 $blog_id = (int)$blog_id;
542
543 BP_Blogs_Blog::delete_blog_for_all( $blog_id );
544
545 // Delete activity stream item
546 bp_blogs_delete_activity( array( 'item_id' => $blog_id, 'component_name' => 'blogs', 'component_action' => 'new_blog', 'user_id' => $bp['loggedin_userid'] ) );
547
548 do_action( 'bp_blogs_remove_blog', $blog_id );
549}
550add_action( 'delete_blog', 'bp_blogs_remove_blog' );
551
552function bp_blogs_remove_blog_for_user( $user_id, $blog_id ) {
553 global $current_user;
554
555 $blog_id = (int)$blog_id;
556 $user_id = (int)$user_id;
557
558 BP_Blogs_Blog::delete_blog_for_user( $blog_id, $user_id );
559
560 // Delete activity stream item
561 bp_blogs_delete_activity( array( 'item_id' => $blog_id, 'component_name' => 'blogs', 'component_action' => 'new_blog', 'user_id' => $current_user->ID ) );
562
563 do_action( 'bp_blogs_remove_blog_for_user', $blog_id, $user_id );
564}
565add_action( 'remove_user_from_blog', 'bp_blogs_remove_blog_for_user', 10, 2 );
566
567function bp_blogs_remove_post( $post_id ) {
568 global $current_blog, $bp;
569
570 if ( !$bp ) {
571 bp_core_setup_globals();
572 bp_blogs_setup_globals();
573 }
574
575 $post_id = (int)$post_id;
576 $blog_id = (int)$current_blog->blog_id;
577
578 BP_Blogs_Post::delete( $post_id, $blog_id, $bp['loggedin_userid'] );
579
580 // Delete activity stream item
581 bp_blogs_delete_activity( array( 'item_id' => $post_id, 'component_name' => 'blogs', 'component_action' => 'new_blog_post', 'user_id' => $bp['loggedin_userid'] ) );
582
583 do_action( 'bp_blogs_remove_post', $blog_id, $post_id );
584}
585add_action( 'delete_post', 'bp_blogs_remove_post' );
586
587function bp_blogs_remove_comment( $comment_id ) {
588 global $current_blog, $bp;
589
590 if ( !$bp ) {
591 bp_core_setup_globals();
592 bp_blogs_setup_globals();
593 }
594
595 $comment_id = (int)$comment_id;
596 $blog_id = (int)$current_blog->blog_id;
597
598 BP_Blogs_Comment::delete( $comment_id, $blog_id, $bp['loggedin_userid'] );
599
600 // Delete activity stream item
601 bp_blogs_delete_activity( array( 'item_id' => $comment_id, 'component_name' => 'blogs', 'component_action' => 'new_blog_comment', 'user_id' => $bp['loggedin_userid'] ) );
602
603 do_action( 'bp_blogs_remove_comment', $blog_id, $comment_id );
604}
605add_action( 'delete_comment', 'bp_blogs_remove_comment' );
606
607function bp_blogs_remove_data_for_blog( $blog_id ) {
608 /* If this is regular blog, delete all data for that blog. */
609 BP_Blogs_Blog::delete_blog_for_all( $blog_id );
610 BP_Blogs_Post::delete_posts_for_blog( $blog_id );
611 BP_Blogs_Comment::delete_comments_for_blog( $blog_id );
612
613 do_action( 'bp_blogs_remove_data_for_blog', $blog_id );
614}
615add_action( 'delete_blog', 'bp_blogs_remove_data_for_blog', 1 );
616
617function bp_blogs_register_existing_content( $blog_id ) {
618 global $bp, $current_blog;
619
620 if ( !$bp ) {
621 bp_core_setup_globals();
622 bp_blogs_setup_globals();
623 }
624
625 $user_id = $bp['loggedin_userid'];
626 $blogs = get_blogs_of_user($user_id);
627
628 if ( is_array($blogs) ) {
629 foreach ( $blogs as $blog ) {
630 bp_blogs_record_blog( (int)$blog->userblog_id, (int)$user_id );
631
632 switch_to_blog( $blog->userblog_id );
633 $posts_for_blog = bp_core_get_all_posts_for_user( $user_id );
634
635 for ( $i = 0; $i < count($posts); $i++ ) {
636 bp_blogs_record_post( $posts[$i] );
637 }
638
639 do_action( 'bp_blogs_register_existing_content', $blog );
640 }
641 }
642
643 switch_to_blog( $current_blog->blog_id );
644}
645add_action( 'bp_homebase_signup_completed', 'bp_blogs_register_existing_content', 10 );
646
647function bp_blogs_get_blogs_for_user( $user_id ) {
648 return BP_Blogs_Blog::get_blogs_for_user( $user_id );
649}
650
651function bp_blogs_get_posts_for_user( $user_id ) {
652 return BP_Blogs_Post::get_posts_for_user( $user_id );
653}
654
655function bp_blogs_get_comments_for_user( $user_id ) {
656 return BP_Blogs_Comment::get_comments_for_user( $user_id );
657}
658
659function bp_blogs_get_latest_posts( $blog_id = null, $limit = 5 ) {
660 global $bp;
661
662 if ( !is_numeric( $limit ) )
663 $limit = 5;
664
665 return BP_Blogs_Post::get_latest_posts( $blog_id, $limit );
666}
667
668function bp_blogs_get_all_blogs( $limit = null, $page = null ) {
669 return BP_Blogs_Blog::get_all( $limit, $page );
670}
671
672function bp_blogs_get_random_blog( $limit = null, $page = null ) {
673 return BP_Blogs_Blog::get_random( $limit, $page );
674}
675
676function bp_blogs_get_all_posts( $limit = null, $page = null ) {
677 return BP_Blogs_Post::get_all( $limit, $page );
678}
679
680function bp_blogs_total_post_count( $blog_id ) {
681 return BP_Blogs_Post::total_post_count( $blog_id );
682}
683
684function bp_blogs_total_comment_count( $blog_id, $post_id = false ) {
685 return BP_Blogs_Post::total_comment_count( $blog_id, $post_id );
686}
687
688function bp_blogs_is_blog_hidden( $blog_id ) {
689 return BP_Blogs_Blog::is_hidden( $blog_id );
690}
691
692function bp_blogs_redirect_to_random_blog() {
693 global $bp, $wpdb;
694
695 if ( $bp['current_component'] == $bp['blogs']['slug'] && isset( $_GET['random-blog'] ) ) {
696 $blog_id = bp_blogs_get_random_blog();
697
698 bp_core_redirect( get_blog_option( $blog_id, 'siteurl') );
699 }
700}
701add_action( 'wp', 'bp_blogs_redirect_to_random_blog', 6 );
702
703
704
705
706//
707// Blog meta functions
708//
709
710function bp_blogs_delete_blogmeta( $blog_id, $meta_key = false, $meta_value = false ) {
711 global $wpdb, $bp;
712
713 if ( !is_numeric( $blog_id ) )
714 return false;
715
716 $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
717
718 if ( is_array($meta_value) || is_object($meta_value) )
719 $meta_value = serialize($meta_value);
720
721 $meta_value = trim( $meta_value );
722
723 if ( !$meta_key ) {
724 $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp['blogs']['table_name_blogpmeta'] . " WHERE blog_id = %d", $blog_id ) );
725 } else if ( !$meta_value ) {
726 $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 ) );
727 } else {
728 $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp['blogs']['table_name_blogmeta'] . " WHERE blog_id = %d AND meta_key = %s", $blog_id, $meta_key ) );
729 }
730
731 // TODO need to look into using this.
732 // wp_cache_delete($group_id, 'groups');
733
734 return true;
735}
736
737function bp_blogs_get_blogmeta( $blog_id, $meta_key = '') {
738 global $wpdb, $bp;
739
740 $blog_id = (int) $blog_id;
741
742 if ( !$blog_id )
743 return false;
744
745 if ( !empty($meta_key) ) {
746 $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
747
748 // TODO need to look into using this.
749 //$user = wp_cache_get($user_id, 'users');
750
751 // Check the cached user object
752 //if ( false !== $user && isset($user->$meta_key) )
753 // $metas = array($user->$meta_key);
754 //else
755 $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) );
756 } else {
757 $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM " . $bp['blogs']['table_name_blogmeta'] . " WHERE blog_id = %d", $blog_id) );
758 }
759
760 if ( empty($metas) ) {
761 if ( empty($meta_key) )
762 return array();
763 else
764 return '';
765 }
766
767 $metas = array_map('maybe_unserialize', $metas);
768
769 if ( count($metas) == 1 )
770 return $metas[0];
771 else
772 return $metas;
773}
774
775function bp_blogs_update_blogmeta( $blog_id, $meta_key, $meta_value ) {
776 global $wpdb, $bp;
777
778 if ( !is_numeric( $blog_id ) )
779 return false;
780
781 $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
782
783 if ( is_string($meta_value) )
784 $meta_value = stripslashes($wpdb->escape($meta_value));
785
786 $meta_value = maybe_serialize($meta_value);
787
788 if (empty($meta_value)) {
789 return bp_blogs_delete_blogmeta( $blog_id, $meta_key );
790 }
791
792 $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 ) );
793
794 if ( !$cur ) {
795 $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 ) );
796 } else if ( $cur->meta_value != $meta_value ) {
797 $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 ) );
798 } else {
799 return false;
800 }
801
802 // TODO need to look into using this.
803 // wp_cache_delete($user_id, 'users');
804
805 return true;
806}
807
808function bp_blogs_remove_data( $user_id ) {
809 /* If this is regular blog, delete all data for that blog. */
810 BP_Blogs_Blog::delete_blogs_for_user( $user_id );
811 BP_Blogs_Post::delete_posts_for_user( $user_id );
812 BP_Blogs_Comment::delete_comments_for_user( $user_id );
813
814 do_action( 'bp_blogs_remove_data', $user_id );
815}
816add_action( 'wpmu_delete_user', 'bp_blogs_remove_data', 1 );
817add_action( 'delete_user', 'bp_blogs_remove_data', 1 );
818
819
820
821
822?>