Ticket #2299: forum-subscriptions.diff
File forum-subscriptions.diff, 57.2 KB (added by , 11 years ago) |
---|
-
includes/common/functions.php
982 982 /** Subscriptions *************************************************************/ 983 983 984 984 /** 985 * Sends notification emails for new posts985 * Sends notification emails for new replies to subscribed topics 986 986 * 987 987 * Gets new post's ID and check if there are subscribed users to that topic, and 988 988 * if there are, send notifications … … 992 992 * @param int $reply_id ID of the newly made reply 993 993 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active 994 994 * @uses bbp_get_reply_id() To validate the reply ID 995 * @uses bbp_get_topic_id() To validate the topic ID 996 * @uses bbp_get_forum_id() To validate the forum ID 995 997 * @uses bbp_get_reply() To get the reply 996 * @uses bbp_get_reply_topic_id() To get the topic ID of the reply997 998 * @uses bbp_is_reply_published() To make sure the reply is published 998 999 * @uses bbp_get_topic_id() To validate the topic ID 999 1000 * @uses bbp_get_topic() To get the reply's topic 1000 1001 * @uses bbp_is_topic_published() To make sure the topic is published 1001 * @uses get_the_author_meta() To get theauthor's display name1002 * @uses do_action() Calls 'bbp_pre_notify_subscribers' with the reply id and1003 * topic id 1002 * @uses bbp_get_reply_author_display_name() To get the reply author's display name 1003 * @uses do_action() Calls 'bbp_pre_notify_subscribers' with the reply id, 1004 * topic id and user id 1004 1005 * @uses bbp_get_topic_subscribers() To get the topic subscribers 1005 1006 * @uses apply_filters() Calls 'bbp_subscription_mail_message' with the 1006 * message, reply id, topic id and user id 1007 * message, reply id, topic id and user id 1008 * @uses apply_filters() Calls 'bbp_subscription_mail_title' with the 1009 * topic title, reply id, topic id and user id 1010 * @uses apply_filters() Calls 'bbp_subscription_mail_headers' 1007 1011 * @uses get_userdata() To get the user data 1008 1012 * @uses wp_mail() To send the mail 1009 * @uses do_action() Calls 'bbp_post_notify_subscribers' with the reply id 1010 * and topicid1013 * @uses do_action() Calls 'bbp_post_notify_subscribers' with the reply id, 1014 * topic id and user id 1011 1015 * @return bool True on success, false on failure 1012 1016 */ 1013 1017 function bbp_notify_subscribers( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = false, $reply_author = 0 ) { … … 1036 1040 1037 1041 /** User ******************************************************************/ 1038 1042 1039 // Get subscribers and bail if empty1043 // Get topic subscribers and bail if empty 1040 1044 $user_ids = bbp_get_topic_subscribers( $topic_id, true ); 1041 1045 if ( empty( $user_ids ) ) 1042 1046 return false; … … 1108 1112 return true; 1109 1113 } 1110 1114 1115 /** 1116 * Sends notification emails for new topics to subscribed forums 1117 * 1118 * @since bbPress (r5155) 1119 * 1120 * Gets new post's ID and check if there are subscribed users to that topic, and 1121 * if there are, send notifications 1122 * 1123 * @since bbPress (r5155) 1124 * 1125 * @param int $topic_id ID of the newly made reply 1126 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active 1127 * @uses bbp_get_topic_id() To validate the topic ID 1128 * @uses bbp_get_forum_id() To validate the forum ID 1129 * @uses bbp_is_topic_published() To make sure the topic is published 1130 * @uses bbp_get_forum_subscribers() To get the forum subscribers 1131 * @uses bbp_get_topic_author_display_name() To get the topic author's display name 1132 * @uses do_action() Calls 'bbp_pre_notify_forum_subscribers' with the topic id, 1133 * forum id and user id 1134 * @uses apply_filters() Calls 'bbp_forum_subscription_mail_message' with the 1135 * message, topic id, forum id and user id 1136 * @uses apply_filters() Calls 'bbp_forum_subscription_mail_title' with the 1137 * topic title, topic id, forum id and user id 1138 * @uses apply_filters() Calls 'bbp_forum_subscription_mail_headers' 1139 * @uses get_userdata() To get the user data 1140 * @uses wp_mail() To send the mail 1141 * @uses do_action() Calls 'bbp_post_notify_forum_subscribers' with the topic, 1142 * id, forum id and user id 1143 * @return bool True on success, false on failure 1144 */ 1145 function bbp_notify_forum_subscribers( $topic_id = 0, $forum_id = 0, $anonymous_data = false, $topic_author = 0 ) { 1146 1147 // Bail if subscriptions are turned off 1148 if ( !bbp_is_subscriptions_active() ) 1149 return false; 1150 1151 /** Validation ************************************************************/ 1152 1153 $topic_id = bbp_get_topic_id( $topic_id ); 1154 $forum_id = bbp_get_forum_id( $forum_id ); 1155 1156 /** Topic *****************************************************************/ 1157 1158 // Bail if topic is not published 1159 if ( ! bbp_is_topic_published( $topic_id ) ) 1160 return false; 1161 1162 /** User ******************************************************************/ 1163 1164 // Get forum subscribers and bail if empty 1165 $user_ids = bbp_get_forum_subscribers( $forum_id, true ); 1166 if ( empty( $user_ids ) ) 1167 return false; 1168 1169 // Poster name 1170 $topic_author_name = bbp_get_topic_author_display_name( $topic_id ); 1171 1172 /** Mail ******************************************************************/ 1173 1174 do_action( 'bbp_pre_notify_forum_subscribers', $topic_id, $forum_id, $user_ids ); 1175 1176 // Remove filters from reply content and topic title to prevent content 1177 // from being encoded with HTML entities, wrapped in paragraph tags, etc... 1178 remove_all_filters( 'bbp_get_topic_content' ); 1179 remove_all_filters( 'bbp_get_topic_title' ); 1180 1181 // Strip tags from text 1182 $topic_title = strip_tags( bbp_get_topic_title( $topic_id ) ); 1183 $topic_content = strip_tags( bbp_get_topic_content( $topic_id ) ); 1184 $topic_url = get_permalink( $topic_id ); 1185 $blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); 1186 1187 // Loop through users 1188 foreach ( (array) $user_ids as $user_id ) { 1189 1190 // Don't send notifications to the person who made the post 1191 if ( !empty( $topic_author ) && (int) $user_id === (int) $topic_author ) 1192 continue; 1193 1194 // For plugins to filter messages per reply/topic/user 1195 $message = sprintf( __( '%1$s wrote: 1196 1197 %2$s 1198 1199 Topic Link: %3$s 1200 1201 ----------- 1202 1203 You are receiving this email because you subscribed to a forum. 1204 1205 Login and visit the topic to unsubscribe from these emails.', 'bbpress' ), 1206 1207 $topic_author_name, 1208 $topic_content, 1209 $topic_url 1210 ); 1211 $message = apply_filters( 'bbp_forum_subscription_mail_message', $message, $topic_id, $forum_id, $user_id ); 1212 if ( empty( $message ) ) 1213 continue; 1214 1215 // For plugins to filter titles per reply/topic/user 1216 $subject = apply_filters( 'bbp_forum_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $topic_id, $forum_id, $user_id ); 1217 if ( empty( $subject ) ) 1218 continue; 1219 1220 // Custom headers 1221 $headers = apply_filters( 'bbp_forum_subscription_mail_headers', array() ); 1222 1223 // Get user data of this user 1224 $user = get_userdata( $user_id ); 1225 1226 // Send notification email 1227 wp_mail( $user->user_email, $subject, $message, $headers ); 1228 } 1229 1230 do_action( 'bbp_post_notify_forum_subscribers', $topic_id, $forum_id, $user_ids ); 1231 1232 return true; 1233 } 1234 1111 1235 /** Login *********************************************************************/ 1112 1236 1113 1237 /** -
includes/common/template.php
2344 2344 return apply_filters( 'bbp_get_breadcrumb', $trail, $crumbs, $r ); 2345 2345 } 2346 2346 2347 /** Forum Subscriptions ******************************************************/ 2348 2349 /** 2350 * Output the forum subscription link 2351 * 2352 * @since bbPress (r5155) 2353 * 2354 * @uses bbp_get_forum_subscription_link() 2355 */ 2356 function bbp_forum_subscription_link( $args = array() ) { 2357 echo bbp_get_forum_subscription_link( $args ); 2358 } 2359 2360 /** 2361 * Get the forum subscription link 2362 * 2363 * @since bbPress (r5155) 2364 * 2365 * @uses bbp_parse_args() 2366 * @uses bbp_get_user_subscribe_link() 2367 * @uses apply_filters() Calls 'bbp_get_forum_subscribe_link' 2368 */ 2369 function bbp_get_forum_subscription_link( $args = array() ) { 2370 2371 // Parse args 2372 $r = bbp_parse_args( $args, array( 2373 2374 // Forum 2375 'forum_id' => 0, 2376 2377 // User 2378 'user_id' => 0, 2379 2380 // HTML 2381 'before' => '<div class="bbp-forum-subscription"><p>', 2382 'after' => '</p></div>', 2383 2384 // Text 2385 'subscribe' => __( 'Subscribe to Forum', 'bbpress' ), 2386 'unsubscribe' => __( 'Unsubscribe from Forum', 'bbpress' ), 2387 2388 ), 'get_forum_subscribe_link' ); 2389 2390 $link = $r['before']; 2391 2392 $link .= bbp_get_user_subscribe_link( array( 2393 'before' => '', 2394 'subscribe' => $r['subscribe'], 2395 'unsubscribe' => $r['unsubscribe'] 2396 ) ); 2397 2398 $link .= $r['after']; 2399 2400 return apply_filters( 'bbp_get_forum_subscribe_link', $link, $r ); 2401 } 2402 2347 2403 /** Topic Tags ***************************************************************/ 2348 2404 2349 2405 /** -
includes/core/actions.php
37 37 * 38 38 * v--WordPress Actions v--bbPress Sub-actions 39 39 */ 40 add_action( 'plugins_loaded', 'bbp_loaded', 41 add_action( 'init', 'bbp_init', 42 add_action( 'parse_query', 'bbp_parse_query', 43 add_action( 'widgets_init', 'bbp_widgets_init', 44 add_action( 'generate_rewrite_rules', 'bbp_generate_rewrite_rules', 45 add_action( 'wp_enqueue_scripts', 'bbp_enqueue_scripts', 46 add_action( 'wp_head', 'bbp_head', 47 add_action( 'wp_footer', 'bbp_footer', 48 add_action( 'set_current_user', 'bbp_setup_current_user', 49 add_action( 'setup_theme', 'bbp_setup_theme', 50 add_action( 'after_setup_theme', 'bbp_after_setup_theme', 51 add_action( 'template_redirect', 'bbp_template_redirect', 52 add_action( 'login_form_login', 'bbp_login_form_login', 53 add_action( 'profile_update', 'bbp_profile_update', 54 add_action( 'user_register', 'bbp_user_register', 40 add_action( 'plugins_loaded', 'bbp_loaded', 10 ); 41 add_action( 'init', 'bbp_init', 0 ); // Early for bbp_register 42 add_action( 'parse_query', 'bbp_parse_query', 2 ); // Early for overrides 43 add_action( 'widgets_init', 'bbp_widgets_init', 10 ); 44 add_action( 'generate_rewrite_rules', 'bbp_generate_rewrite_rules', 10 ); 45 add_action( 'wp_enqueue_scripts', 'bbp_enqueue_scripts', 10 ); 46 add_action( 'wp_head', 'bbp_head', 10 ); 47 add_action( 'wp_footer', 'bbp_footer', 10 ); 48 add_action( 'set_current_user', 'bbp_setup_current_user', 10 ); 49 add_action( 'setup_theme', 'bbp_setup_theme', 10 ); 50 add_action( 'after_setup_theme', 'bbp_after_setup_theme', 10 ); 51 add_action( 'template_redirect', 'bbp_template_redirect', 8 ); // Before BuddyPress's 10 [BB2225] 52 add_action( 'login_form_login', 'bbp_login_form_login', 10 ); 53 add_action( 'profile_update', 'bbp_profile_update', 10, 2 ); // user_id and old_user_data 54 add_action( 'user_register', 'bbp_user_register', 10 ); 55 55 56 56 /** 57 57 * bbp_loaded - Attached to 'plugins_loaded' above … … 118 118 add_action( 'bbp_register', 'bbp_register_shortcodes', 10 ); 119 119 120 120 // Autoembeds 121 add_action( 'bbp_init', 'bbp_reply_content_autoembed', 8 122 add_action( 'bbp_init', 'bbp_topic_content_autoembed', 8 121 add_action( 'bbp_init', 'bbp_reply_content_autoembed', 8 ); 122 add_action( 'bbp_init', 'bbp_topic_content_autoembed', 8 ); 123 123 124 124 /** 125 125 * bbp_ready - attached to end 'bbp_init' above … … 224 224 // Subscriptions 225 225 add_action( 'bbp_trash_topic', 'bbp_remove_topic_from_all_subscriptions' ); 226 226 add_action( 'bbp_delete_topic', 'bbp_remove_topic_from_all_subscriptions' ); 227 add_action( 'bbp_trash_forum', 'bbp_remove_forum_from_all_subscriptions' ); 228 add_action( 'bbp_delete_forum', 'bbp_remove_forum_from_all_subscriptions' ); 227 229 add_action( 'bbp_new_reply', 'bbp_notify_subscribers', 11, 5 ); 230 add_action( 'bbp_new_topic', 'bbp_notify_forum_subscribers', 11, 4 ); 228 231 229 232 // Sticky 230 233 add_action( 'bbp_trash_topic', 'bbp_unstick_topic' ); … … 297 300 add_action( 'bbp_post_request', 'bbp_new_topic_handler', 10 ); 298 301 299 302 // Theme-side GET requests 300 add_action( 'bbp_get_request', 'bbp_toggle_topic_handler', 1 ); 301 add_action( 'bbp_get_request', 'bbp_toggle_reply_handler', 1 ); 302 add_action( 'bbp_get_request', 'bbp_favorites_handler', 1 ); 303 add_action( 'bbp_get_request', 'bbp_subscriptions_handler', 1 ); 304 add_action( 'bbp_get_request', 'bbp_search_results_redirect', 10 ); 303 add_action( 'bbp_get_request', 'bbp_toggle_topic_handler', 1 ); 304 add_action( 'bbp_get_request', 'bbp_toggle_reply_handler', 1 ); 305 add_action( 'bbp_get_request', 'bbp_favorites_handler', 1 ); 306 add_action( 'bbp_get_request', 'bbp_subscriptions_handler', 1 ); 307 add_action( 'bbp_get_request', 'bbp_forum_subscriptions_handler', 1 ); 308 add_action( 'bbp_get_request', 'bbp_search_results_redirect', 10 ); 305 309 306 310 // Maybe convert the users password 307 311 add_action( 'bbp_login_form_login', 'bbp_user_maybe_convert_pass' ); -
includes/forums/functions.php
961 961 return true; 962 962 } 963 963 964 /** Subscriptions *************************************************/ 965 966 /** 967 * Remove a deleted forum from all users' subscriptions 968 * 969 * @since bbPress (r5155) 970 * 971 * @param int $forum_id Get the forum ID to remove 972 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active 973 * @uses bbp_get_forum_id To get the forum id 974 * @uses bbp_get_forum_subscribers() To get the forum subscribers 975 * @uses bbp_remove_user_subscription() To remove the user subscription 976 */ 977 function bbp_remove_forum_from_all_subscriptions( $forum_id = 0 ) { 978 979 // Subscriptions are not active 980 if ( ! bbp_is_subscriptions_active() ) 981 return; 982 983 $forum_id = bbp_get_forum_id( $forum_id ); 984 985 // Bail if no forum 986 if ( empty( $forum_id ) ) 987 return; 988 989 // Get users 990 $users = (array) bbp_get_forum_subscribers( $forum_id ); 991 992 // Users exist 993 if ( !empty( $users ) ) { 994 995 // Loop through users 996 foreach ( $users as $user ) { 997 998 // Remove each user 999 bbp_remove_user_subscription( $user, $forum_id ); 1000 } 1001 } 1002 } 1003 964 1004 /** Count Bumpers *************************************************************/ 965 1005 966 1006 /** -
includes/forums/template.php
442 442 } 443 443 444 444 /** 445 * Output checked value of forum subscription 446 * 447 * @since bbPress (r5155) 448 * 449 * @uses bbp_get_form_forum_subscribed() To get the subscribed checkbox value 450 */ 451 function bbp_form_forum_subscribed() { 452 echo bbp_get_form_forum_subscribed(); 453 } 454 /** 455 * Return checked value of forum subscription 456 * 457 * @since bbPress (r5155) 458 * 459 * @uses bbp_is_forum_edit() To check if it's the forum edit page 460 * @uses bbp_get_global_post_field() To get current post author 461 * @uses bbp_get_current_user_id() To get the current user id 462 * @uses bbp_is_user_subscribed() To check if the user is subscribed to 463 * the forum 464 * @uses apply_filters() Calls 'bbp_get_form_forum_subscribed' with the 465 * option 466 * @return string Checked value of forum subscription 467 */ 468 function bbp_get_form_forum_subscribed() { 469 470 // Get _POST data 471 if ( bbp_is_post_request() && isset( $_POST['bbp_forum_subscription'] ) ) { 472 $forum_subscribed = (bool) $_POST['bbp_forum_subscription']; 473 474 // Get edit data 475 } elseif ( bbp_is_forum_edit() || bbp_is_reply_edit() ) { 476 477 // Get current posts author 478 $post_author = bbp_get_global_post_field( 'post_author', 'raw' ); 479 480 // Post author is not the current user 481 if ( bbp_get_current_user_id() !== $post_author ) { 482 $forum_subscribed = bbp_is_user_subscribed( $post_author ); 483 484 // Post author is the current user 485 } else { 486 $forum_subscribed = bbp_is_user_subscribed( bbp_get_current_user_id() ); 487 } 488 489 // Get current status 490 } elseif ( bbp_is_single_forum() ) { 491 $forum_subscribed = bbp_is_user_subscribed( bbp_get_current_user_id() ); 492 493 // No data 494 } else { 495 $forum_subscribed = false; 496 } 497 498 // Get checked output 499 $checked = checked( $forum_subscribed, true, false ); 500 501 return apply_filters( 'bbp_get_form_forum_subscribed', $checked, $forum_subscribed ); 502 } 503 504 /** 445 505 * Output the forums last active ID 446 506 * 447 507 * @since bbPress (r2860) -
includes/topics/functions.php
2200 2200 * 2201 2201 * @since bbPress (r2652) 2202 2202 * 2203 * @param int $topic_id Topic ID to remove 2203 * @param int $topic_id Get the topic id to remove 2204 * @uses bbp_get_topic_id To get the topic id 2204 2205 * @uses bbp_get_topic_favoriters() To get the topic's favoriters 2205 2206 * @uses bbp_remove_user_favorite() To remove the topic from user's favorites 2206 2207 */ … … 2231 2232 * 2232 2233 * @since bbPress (r2652) 2233 2234 * 2234 * @param int $topic_id Topic IDto remove2235 * @param int $topic_id Get the topic id to remove 2235 2236 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active 2237 * @uses bbp_get_topic_id To get the topic id 2236 2238 * @uses bbp_get_topic_subscribers() To get the topic subscribers 2237 2239 * @uses bbp_remove_user_subscription() To remove the user subscription 2238 2240 */ -
includes/users/functions.php
313 313 $favorites = bbp_get_user_favorites_topic_ids( $user_id ); 314 314 315 315 if ( !empty( $favorites ) ) { 316 316 317 317 // Checking a specific topic id 318 318 if ( !empty( $topic_id ) ) { 319 319 $topic = bbp_get_topic( $topic_id ); … … 539 539 } 540 540 541 541 /** 542 * Get the users who have subscribed to the forum 543 * 544 * @since bbPress (r2668) 545 * 546 * @param int $forum_id Optional. forum id 547 * @uses wpdb::get_col() To execute our query and get the column back 548 * @uses apply_filters() Calls 'bbp_get_forum_subscribers' with the subscribers 549 * @return array|bool Results if the forum has any subscribers, otherwise false 550 */ 551 function bbp_get_forum_subscribers( $forum_id = 0 ) { 552 $forum_id = bbp_get_forum_id( $forum_id ); 553 if ( empty( $forum_id ) ) 554 return; 555 556 global $wpdb; 557 558 $key = $wpdb->prefix . '_bbp_forum_subscriptions'; 559 $users = wp_cache_get( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' ); 560 if ( false === $users ) { 561 $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$forum_id}', meta_value) > 0" ); 562 wp_cache_set( 'bbp_get_forum_subscribers_' . $forum_id, $users, 'bbpress_users' ); 563 } 564 565 return apply_filters( 'bbp_get_forum_subscribers', $users ); 566 } 567 568 /** 542 569 * Get a user's subscribed topics 543 570 * 544 571 * @since bbPress (r2668) … … 592 619 } 593 620 594 621 /** 595 * Check if a topic is in user's subscription list or not622 * Get a user's subscribed forum ids 596 623 * 597 624 * @since bbPress (r2668) 598 625 * 599 626 * @param int $user_id Optional. User id 627 * @uses bbp_get_user_id() To get the user id 628 * @uses get_user_option() To get the user's subscriptions 629 * @uses apply_filters() Calls 'bbp_get_user_subscribed_forum_ids' with 630 * the subscriptions and user id 631 * @return array|bool Results if user has subscriptions, otherwise false 632 */ 633 function bbp_get_user_subscribed_forum_ids( $user_id = 0 ) { 634 $user_id = bbp_get_user_id( $user_id ); 635 if ( empty( $user_id ) ) 636 return false; 637 638 $subscriptions = get_user_option( '_bbp_forum_subscriptions', $user_id ); 639 $subscriptions = array_filter( wp_parse_id_list( $subscriptions ) ); 640 641 return (array) apply_filters( 'bbp_get_user_subscribed_forum_ids', $subscriptions, $user_id ); 642 } 643 644 /** 645 * Check if a topic is in user's subscription list or not 646 * 647 * @param int $user_id Optional. User id 600 648 * @param int $topic_id Optional. Topic id 601 649 * @uses bbp_get_user_id() To get the user id 602 650 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions … … 606 654 * topic id and subsriptions 607 655 * @return bool True if the topic is in user's subscriptions, otherwise false 608 656 */ 609 function bbp_is_user_subscribed ( $user_id = 0, $topic_id = 0 ) {657 function bbp_is_user_subscribed_to_topic( $user_id = 0, $topic_id = 0 ) { 610 658 611 659 // Validate user 612 660 $user_id = bbp_get_user_id( $user_id, true, true ); … … 616 664 $retval = false; 617 665 $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id ); 618 666 619 if ( ! empty( $subscriptions ) ) {667 if ( ! empty( $subscriptions ) ) { 620 668 621 669 // Checking a specific topic id 622 if ( ! empty( $topic_id ) ) {623 $topic 624 $topic_id = ! empty( $topic ) ? $topic->ID : 0;670 if ( ! empty( $topic_id ) ) { 671 $topic = bbp_get_topic( $topic_id ); 672 $topic_id = ! empty( $topic ) ? $topic->ID : 0; 625 673 626 674 // Using the global topic id 627 675 } elseif ( bbp_get_topic_id() ) { … … 633 681 } 634 682 635 683 // Is topic_id in the user's favorites 636 if ( ! empty( $topic_id ) ) {684 if ( ! empty( $topic_id ) ) { 637 685 $retval = in_array( $topic_id, $subscriptions ); 638 686 } 639 687 } 640 688 641 return (bool) apply_filters( 'bbp_is_user_subscribed ', (bool) $retval, $user_id, $topic_id, $subscriptions );689 return (bool) apply_filters( 'bbp_is_user_subscribed_to_topic', (bool) $retval, $user_id, $topic_id, $subscriptions ); 642 690 } 643 691 644 692 /** 693 * Check if a forum is in user's subscription list or not 694 * 695 * @since bbPress (r2668) 696 * 697 * @param int $user_id Optional. User id 698 * @param int $forum_id Optional. Topic id 699 * @uses bbp_get_user_id() To get the user id 700 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions 701 * @uses bbp_get_forum() To get the forum 702 * @uses bbp_get_forum_id() To get the forum id 703 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id, 704 * forum id and subsriptions 705 * @return bool True if the forum is in user's subscriptions, otherwise false 706 */ 707 function bbp_is_user_subscribed_to_forum( $user_id = 0, $forum_id = 0 ) { 708 709 // Validate user 710 $user_id = bbp_get_user_id( $user_id, true, true ); 711 if ( empty( $user_id ) ) 712 return false; 713 714 $retval = false; 715 $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id ); 716 717 if ( ! empty( $subscriptions ) ) { 718 719 // Checking a specific forum id 720 if ( ! empty( $forum_id ) ) { 721 $forum = bbp_get_forum( $forum_id ); 722 $forum_id = ! empty( $forum ) ? $forum->ID : 0; 723 724 // Using the global forum id 725 } elseif ( bbp_get_forum_id() ) { 726 $forum_id = bbp_get_forum_id(); 727 728 // Use the current post id 729 } elseif ( ! bbp_get_forum_id() ) { 730 $forum_id = get_the_ID(); 731 } 732 733 // Is forum_id in the user's favorites 734 if ( ! empty( $forum_id ) ) { 735 $retval = in_array( $forum_id, $subscriptions ); 736 } 737 } 738 739 return (bool) apply_filters( 'bbp_is_user_subscribed_to_forum', (bool) $retval, $user_id, $forum_id, $subscriptions ); 740 } 741 742 743 /** 744 * Check if a user's subscription list or not 745 * 746 * @param int $user_id Optional. User id 747 * @param int $forum_id Optional. Topic id 748 * @uses get_post() To get the post object 749 * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions 750 * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions 751 * @uses bbp_get_forum_post_type() To get the forum post type 752 * @uses bbp_get_topic_post_type() To get the topic post type 753 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id, 754 * forum/topic id and subsriptions 755 * @return bool True if the forum or topic is in user's subscriptions, otherwise false 756 */ 757 function bbp_is_user_subscribed( $user_id = 0, $object_id = 0 ) { 758 if ( empty( $user_id ) || empty( $object_id ) ) 759 return false; 760 761 $object = get_post( $object_id ); 762 if( ! $object ) 763 return false; 764 765 $post_type = $object->post_type; 766 767 switch( $post_type ) { 768 769 case bbp_get_forum_post_type() : 770 771 $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id ); 772 $retval = bbp_is_user_subscribed_to_forum( $user_id, $object_id ); 773 break; 774 775 case bbp_get_topic_post_type() : 776 default : 777 778 $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id ); 779 $retval = bbp_is_user_subscribed_to_topic( $user_id, $object_id ); 780 break; 781 } 782 783 return (bool) apply_filters( 'bbp_is_user_subscribed', $retval, $user_id, $object_id, $subscriptions ); 784 } 785 786 /** 645 787 * Add a topic to user's subscriptions 646 788 * 647 789 * @since bbPress (r2668) … … 654 796 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id 655 797 * @return bool Always true 656 798 */ 657 function bbp_add_user_ subscription( $user_id = 0, $topic_id = 0 ) {799 function bbp_add_user_topic_subscription( $user_id = 0, $topic_id = 0 ) { 658 800 if ( empty( $user_id ) || empty( $topic_id ) ) 659 801 return false; 660 802 … … 671 813 wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' ); 672 814 } 673 815 674 do_action( 'bbp_add_user_ subscription', $user_id, $topic_id );816 do_action( 'bbp_add_user_topic_subscription', $user_id, $topic_id ); 675 817 676 818 return true; 677 819 } 678 820 679 821 /** 680 * Remove a topic fromuser's subscriptions822 * Add a forum to user's subscriptions 681 823 * 824 * @param int $user_id Optional. User id 825 * @param int $forum_id Optional. forum id 826 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions 827 * @uses bbp_get_forum() To get the forum 828 * @uses update_user_option() To update the user's subscriptions 829 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & forum id 830 * @return bool Always true 831 */ 832 function bbp_add_user_forum_subscription( $user_id = 0, $forum_id = 0 ) { 833 if ( empty( $user_id ) || empty( $forum_id ) ) 834 return false; 835 836 $forum = bbp_get_forum( $forum_id ); 837 if ( empty( $forum ) ) 838 return false; 839 840 $subscriptions = (array) bbp_get_user_subscribed_forum_ids( $user_id ); 841 if ( !in_array( $forum_id, $subscriptions ) ) { 842 $subscriptions[] = $forum_id; 843 $subscriptions = implode( ',', wp_parse_id_list( array_filter( $subscriptions ) ) ); 844 update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions ); 845 846 wp_cache_delete( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' ); 847 } 848 849 do_action( 'bbp_add_user_forum_subscription', $user_id, $forum_id ); 850 851 return true; 852 } 853 854 /** 855 * Add a topic to user's subscriptions 856 * 682 857 * @since bbPress (r2668) 683 858 * 684 859 * @param int $user_id Optional. User id 685 860 * @param int $topic_id Optional. Topic id 861 * @uses get_post() To get the post object 862 * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions 863 * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions 864 * @uses bbp_get_forum_post_type() To get the forum post type 865 * @uses bbp_get_topic_post_type() To get the topic post type 866 * @uses update_user_option() To update the user's subscriptions 867 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id 868 * @return bool Always true 869 */ 870 function bbp_add_user_subscription( $user_id = 0, $object_id = 0 ) { 871 if ( empty( $user_id ) || empty( $object_id ) ) 872 return false; 873 874 $object = get_post( $object_id ); 875 if( ! $object ) 876 return false; 877 878 $post_type = $object->post_type; 879 880 switch( $post_type ) { 881 882 case bbp_get_forum_post_type() : 883 884 $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id ); 885 886 if ( ! in_array( $object_id, $subscriptions ) ) { 887 $subscriptions[] = $object_id; 888 $subscriptions = implode( ',', wp_parse_id_list( array_filter( $subscriptions ) ) ); 889 update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions ); 890 891 wp_cache_delete( 'bbp_get_forum_subscribers_' . $object_id, 'bbpress_users' ); 892 } 893 894 break; 895 896 case bbp_get_topic_post_type() : 897 default : 898 899 $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id ); 900 901 if ( ! in_array( $object_id, $subscriptions ) ) { 902 $subscriptions[] = $object_id; 903 $subscriptions = implode( ',', wp_parse_id_list( array_filter( $subscriptions ) ) ); 904 update_user_option( $user_id, '_bbp_subscriptions', $subscriptions ); 905 906 wp_cache_delete( 'bbp_get_topic_subscribers_' . $object_id, 'bbpress_users' ); 907 } 908 909 break; 910 } 911 912 do_action( 'bbp_add_user_subscription', $user_id, $object_id, $post_type ); 913 914 return true; 915 } 916 917 /** 918 * Remove a topic from user's subscriptions 919 * 920 * @param int $user_id Optional. User id 921 * @param int $topic_id Optional. Topic id 686 922 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions 687 923 * @uses update_user_option() To update the user's subscriptions 688 924 * @uses delete_user_option() To delete the user's subscriptions meta 689 * @uses do_action() Calls 'bbp_remove_user_ subscription' with the user id and925 * @uses do_action() Calls 'bbp_remove_user_topic_subscription' with the user id and 690 926 * topic id 691 927 * @return bool True if the topic was removed from user's subscriptions, 692 928 * otherwise false 693 929 */ 694 function bbp_remove_user_ subscription( $user_id, $topic_id ) {930 function bbp_remove_user_topic_subscription( $user_id, $topic_id ) { 695 931 if ( empty( $user_id ) || empty( $topic_id ) ) 696 932 return false; 697 933 … … 715 951 wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' ); 716 952 } 717 953 718 do_action( 'bbp_remove_user_ subscription', $user_id, $topic_id );954 do_action( 'bbp_remove_user_topic_subscription', $user_id, $topic_id ); 719 955 720 956 return true; 721 957 } 722 958 723 959 /** 960 * Remove a forum from user's subscriptions 961 * 962 * @param int $user_id Optional. User id 963 * @param int $forum_id Optional. forum id 964 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions 965 * @uses update_user_option() To update the user's subscriptions 966 * @uses delete_user_option() To delete the user's subscriptions meta 967 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and 968 * forum id 969 * @return bool True if the forum was removed from user's subscriptions, 970 * otherwise false 971 */ 972 function bbp_remove_user_forum_subscription( $user_id, $forum_id ) { 973 if ( empty( $user_id ) || empty( $forum_id ) ) 974 return false; 975 976 $subscriptions = (array) bbp_get_user_subscribed_forum_ids( $user_id ); 977 978 if ( empty( $subscriptions ) ) 979 return false; 980 981 $pos = array_search( $forum_id, $subscriptions ); 982 if ( is_numeric( $pos ) ) { 983 array_splice( $subscriptions, $pos, 1 ); 984 $subscriptions = array_filter( $subscriptions ); 985 986 if ( !empty( $subscriptions ) ) { 987 $subscriptions = implode( ',', wp_parse_id_list( $subscriptions ) ); 988 update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions ); 989 } else { 990 delete_user_option( $user_id, '_bbp_forum_subscriptions' ); 991 } 992 993 wp_cache_delete( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' ); 994 } 995 996 do_action( 'bbp_remove_user_forum_subscription', $user_id, $forum_id ); 997 998 return true; 999 } 1000 1001 /** 1002 * Remove a topic from user's subscriptions 1003 * 1004 * @since bbPress (r2668) 1005 * 1006 * @param int $user_id Optional. User id 1007 * @param int $topic_id Optional. Topic id 1008 * @uses get_post() To get the post object 1009 * @uses bbp_get_forum_post_type() To get the forum post type 1010 * @uses bbp_get_topic_post_type() To get the topic post type 1011 * @uses bbp_remove_user_forum_subscription() To remove the user's subscription 1012 * @uses bbp_remove_user_topic_subscription() To remove the user's subscription 1013 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and 1014 * topic id 1015 * @return bool True if the topic was removed from user's subscriptions, 1016 * otherwise false 1017 */ 1018 function bbp_remove_user_subscription( $user_id = 0, $object_id = 0 ) { 1019 if ( empty( $user_id ) || empty( $object_id ) ) 1020 return false; 1021 1022 $post_type = get_post_type( $object_id ); 1023 1024 switch( $post_type ) { 1025 1026 case bbp_get_forum_post_type() : 1027 1028 bbp_remove_user_forum_subscription( $user_id, $object_id ); 1029 1030 break; 1031 1032 case bbp_get_topic_post_type() : 1033 default : 1034 1035 bbp_remove_user_topic_subscription( $user_id, $object_id ); 1036 1037 break; 1038 } 1039 1040 do_action( 'bbp_remove_user_subscription', $user_id, $object_id, $post_type ); 1041 1042 return true; 1043 } 1044 1045 /** 724 1046 * Handles the front end subscribing and unsubscribing topics 725 1047 * 726 1048 * @param string $action The requested action to compare this function to … … 822 1144 } 823 1145 } 824 1146 1147 /** 1148 * Handles the front end subscribing and unsubscribing forums 1149 * 1150 * @param string $action The requested action to compare this function to 1151 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active 1152 * @uses bbp_get_user_id() To get the user id 1153 * @uses bbp_verify_nonce_request() To verify the nonce and check the request 1154 * @uses current_user_can() To check if the current user can edit the user 1155 * @uses bbPress:errors:add() To log the error messages 1156 * @uses bbp_is_user_subscribed() To check if the forum is in user's 1157 * subscriptions 1158 * @uses bbp_remove_user_subscription() To remove the user subscription 1159 * @uses bbp_add_user_subscription() To add the user subscription 1160 * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id, 1161 * forum id and action 1162 * @uses bbp_is_subscription() To check if it's the subscription page 1163 * @uses bbp_get_subscription_link() To get the subscription page link 1164 * @uses bbp_get_forum_permalink() To get the forum permalink 1165 * @uses wp_safe_redirect() To redirect to the url 1166 */ 1167 function bbp_forum_subscriptions_handler( $action = '' ) { 1168 1169 if ( !bbp_is_subscriptions_active() ) 1170 return false; 1171 1172 // Bail if no forum ID is passed 1173 if ( empty( $_GET['forum_id'] ) ) 1174 return; 1175 1176 // Setup possible get actions 1177 $possible_actions = array( 1178 'bbp_subscribe', 1179 'bbp_unsubscribe', 1180 ); 1181 1182 // Bail if actions aren't meant for this function 1183 if ( !in_array( $action, $possible_actions ) ) 1184 return; 1185 1186 // Get required data 1187 $user_id = bbp_get_user_id( 0, true, true ); 1188 $forum_id = intval( $_GET['forum_id'] ); 1189 1190 // Check for empty forum 1191 if ( empty( $forum_id ) ) { 1192 bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: No forum was found! Which forum are you subscribing/unsubscribing to?', 'bbpress' ) ); 1193 1194 // Check nonce 1195 } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $forum_id ) ) { 1196 bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); 1197 1198 // Check current user's ability to edit the user 1199 } elseif ( !current_user_can( 'edit_user', $user_id ) ) { 1200 bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) ); 1201 } 1202 1203 // Bail if we have errors 1204 if ( bbp_has_errors() ) 1205 return; 1206 1207 /** No errors *************************************************************/ 1208 1209 $is_subscription = bbp_is_user_subscribed( $user_id, $forum_id ); 1210 $success = false; 1211 1212 if ( true === $is_subscription && 'bbp_unsubscribe' === $action ) 1213 $success = bbp_remove_user_subscription( $user_id, $forum_id ); 1214 elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) 1215 $success = bbp_add_user_subscription( $user_id, $forum_id ); 1216 1217 // Do additional subscriptions actions 1218 do_action( 'bbp_subscriptions_handler', $success, $user_id, $forum_id, $action ); 1219 1220 // Success! 1221 if ( true === $success ) { 1222 1223 // Redirect back from whence we came 1224 if ( bbp_is_subscriptions() ) { 1225 $redirect = bbp_get_subscriptions_permalink( $user_id ); 1226 } elseif ( bbp_is_single_user() ) { 1227 $redirect = bbp_get_user_profile_url(); 1228 } elseif ( is_singular( bbp_get_forum_post_type() ) ) { 1229 $redirect = bbp_get_forum_permalink( $forum_id ); 1230 } elseif ( is_single() || is_page() ) { 1231 $redirect = get_permalink(); 1232 } else { 1233 $redirect = get_permalink( $forum_id ); 1234 } 1235 1236 wp_safe_redirect( $redirect ); 1237 1238 // For good measure 1239 exit(); 1240 1241 // Fail! Handle errors 1242 } elseif ( true === $is_subscription && 'bbp_unsubscribe' === $action ) { 1243 bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that forum!', 'bbpress' ) ); 1244 } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) { 1245 bbp_add_error( 'bbp_subscribe', __( '<strong>ERROR</strong>: There was a problem subscribing to that forum!', 'bbpress' ) ); 1246 } 1247 } 1248 825 1249 /** Edit **********************************************************************/ 826 1250 827 1251 /** … … 968 1392 * @return array|bool Results if the user has created topics, otherwise false 969 1393 */ 970 1394 function bbp_get_user_topics_started( $user_id = 0 ) { 971 1395 972 1396 // Validate user 973 1397 $user_id = bbp_get_user_id( $user_id ); 974 1398 if ( empty( $user_id ) ) … … 993 1417 * @return array|bool Results if the user has created topics, otherwise false 994 1418 */ 995 1419 function bbp_get_user_replies_created( $user_id = 0 ) { 996 1420 997 1421 // Validate user 998 1422 $user_id = bbp_get_user_id( $user_id ); 999 1423 if ( empty( $user_id ) ) … … 1031 1455 * which a user can edit another user (or themselves.) If these conditions are 1032 1456 * met. We assume a user cannot perform this task, and look for ways they can 1033 1457 * earn the ability to access this template. 1034 * 1458 * 1035 1459 * @since bbPress (r3605) 1036 1460 * 1037 1461 * @uses bbp_is_topic_edit() -
includes/users/template.php
612 612 } 613 613 614 614 /** Anonymous Fields **********************************************************/ 615 615 616 616 /** 617 617 * Output the author disylay-name of a topic or reply. 618 618 * … … 1027 1027 * - unsubscribe: Unsubscribe text 1028 1028 * - user_id: User id 1029 1029 * - topic_id: Topic id 1030 * - forum_id: Forum id 1030 1031 * - before: Before the link 1031 1032 * - after: After the link 1032 1033 * @param int $user_id Optional. User id 1033 1034 * @param bool $wrap Optional. If you want to wrap the link in <span id="subscription-toggle">. 1035 * @uses bbp_is_subscriptions_active() to check if subscriptions are active 1034 1036 * @uses bbp_get_user_id() To get the user id 1037 * @uses bbp_get_user_id() To get the user id 1038 * @uses bbp_get_topic_id() To get the topic id 1039 * @uses bbp_get_forum_id() To get the forum id 1035 1040 * @uses current_user_can() To check if the current user can edit user 1036 * @uses bbp_ get_topic_id() To get the topic id1037 * @uses bbp_is_user_subscribed () To check if the user is subscribed1041 * @uses bbp_is_user_subscribed_to_forum() To check if the user is subscribed to the forum 1042 * @uses bbp_is_user_subscribed_to_topic() To check if the user is subscribed to the topic 1038 1043 * @uses bbp_is_subscriptions() To check if it's the subscriptions page 1039 1044 * @uses bbp_get_subscriptions_permalink() To get subscriptions link 1040 1045 * @uses bbp_get_topic_permalink() To get topic link … … 1043 1048 * @return string Permanent link to topic 1044 1049 */ 1045 1050 function bbp_get_user_subscribe_link( $args = '', $user_id = 0, $wrap = true ) { 1046 if ( ! bbp_is_subscriptions_active() )1051 if ( ! bbp_is_subscriptions_active() ) 1047 1052 return; 1048 1053 1049 1054 // Parse arguments against default values … … 1052 1057 'unsubscribe' => __( 'Unsubscribe', 'bbpress' ), 1053 1058 'user_id' => 0, 1054 1059 'topic_id' => 0, 1060 'forum_id' => 0, 1055 1061 'before' => ' | ', 1056 1062 'after' => '' 1057 1063 ), 'get_user_subscribe_link' ); 1058 1064 1059 // Validate user and topic ID's 1060 $user_id = bbp_get_user_id( $r['user_id'], true, true ); 1061 $topic_id = bbp_get_topic_id( $r['topic_id'] ); 1062 if ( empty( $user_id ) || empty( $topic_id ) ) { 1065 // Validate user and object ID's 1066 $user_id = bbp_get_user_id( $r['user_id'], true, true ); 1067 $topic_id = bbp_get_topic_id( $r['topic_id'] ); 1068 $forum_id = bbp_get_forum_id( $r['forum_id'] ); 1069 if ( empty( $user_id ) || ( empty( $topic_id ) && empty( $forum_id ) ) ) { 1063 1070 return false; 1064 1071 } 1065 1072 1066 1073 // No link if you can't edit yourself 1067 if ( ! current_user_can( 'edit_user', (int) $user_id ) ) {1074 if ( ! current_user_can( 'edit_user', (int) $user_id ) ) { 1068 1075 return false; 1069 1076 } 1070 1077 1071 // Decide which link to show 1072 $is_subscribed = bbp_is_user_subscribed( $user_id, $topic_id ); 1073 if ( !empty( $is_subscribed ) ) { 1074 $text = $r['unsubscribe']; 1075 $query_args = array( 'action' => 'bbp_unsubscribe', 'topic_id' => $topic_id ); 1076 } else { 1077 $text = $r['subscribe']; 1078 $query_args = array( 'action' => 'bbp_subscribe', 'topic_id' => $topic_id ); 1079 } 1078 // Check if viewing a single forum 1079 if( empty( $topic_id ) && ! empty( $forum_id ) ) { 1080 1080 1081 // Create the link based where the user is and if the user is 1082 // subscribed already 1083 if ( bbp_is_subscriptions() ) { 1084 $permalink = bbp_get_subscriptions_permalink( $user_id ); 1085 } elseif ( bbp_is_single_topic() || bbp_is_single_reply() ) { 1086 $permalink = bbp_get_topic_permalink( $topic_id ); 1081 // Decide which link to show 1082 $is_subscribed = bbp_is_user_subscribed_to_forum( $user_id, $forum_id ); 1083 if ( ! empty( $is_subscribed ) ) { 1084 $text = $r['unsubscribe']; 1085 $query_args = array( 'action' => 'bbp_unsubscribe', 'forum_id' => $forum_id ); 1086 } else { 1087 $text = $r['subscribe']; 1088 $query_args = array( 'action' => 'bbp_subscribe', 'forum_id' => $forum_id ); 1089 } 1090 1091 // Create the link based where the user is and if the user is 1092 // subscribed already 1093 if ( bbp_is_subscriptions() ) { 1094 $permalink = bbp_get_subscriptions_permalink( $user_id ); 1095 } elseif ( bbp_is_single_forum() || bbp_is_single_reply() ) { 1096 $permalink = bbp_get_forum_permalink( $forum_id ); 1097 } else { 1098 $permalink = get_permalink(); 1099 } 1100 1101 $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $forum_id ) ); 1102 $sub = $is_subscribed ? ' class="is-subscribed"' : ''; 1103 $html = sprintf( '%s<span id="subscribe-%d" %s><a href="%s" class="subscription-toggle" data-forum="%d">%s</a></span>%s', $r['before'], $forum_id, $sub, $url, $forum_id, $text, $r['after'] ); 1104 1105 // Initial output is wrapped in a span, ajax output is hooked to this 1106 if ( !empty( $wrap ) ) { 1107 $html = '<span id="subscription-toggle">' . $html . '</span>'; 1108 } 1109 1087 1110 } else { 1088 $permalink = get_permalink();1089 }1090 1111 1091 $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) ); 1092 $sub = $is_subscribed ? ' class="is-subscribed"' : ''; 1093 $html = sprintf( '%s<span id="subscribe-%d" %s><a href="%s" class="subscription-toggle" data-topic="%d">%s</a></span>%s', $r['before'], $topic_id, $sub, $url, $topic_id, $text, $r['after'] ); 1112 // Decide which link to show 1113 $is_subscribed = bbp_is_user_subscribed_to_topic( $user_id, $topic_id ); 1114 if ( ! empty( $is_subscribed ) ) { 1115 $text = $r['unsubscribe']; 1116 $query_args = array( 'action' => 'bbp_unsubscribe', 'topic_id' => $topic_id ); 1117 } else { 1118 $text = $r['subscribe']; 1119 $query_args = array( 'action' => 'bbp_subscribe', 'topic_id' => $topic_id ); 1120 } 1094 1121 1095 // Initial output is wrapped in a span, ajax output is hooked to this 1096 if ( !empty( $wrap ) ) { 1097 $html = '<span id="subscription-toggle">' . $html . '</span>'; 1122 // Create the link based where the user is and if the user is 1123 // subscribed already 1124 if ( bbp_is_subscriptions() ) { 1125 $permalink = bbp_get_subscriptions_permalink( $user_id ); 1126 } elseif ( bbp_is_single_topic() || bbp_is_single_reply() ) { 1127 $permalink = bbp_get_topic_permalink( $topic_id ); 1128 } else { 1129 $permalink = get_permalink(); 1130 } 1131 1132 $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) ); 1133 $sub = $is_subscribed ? ' class="is-subscribed"' : ''; 1134 $html = sprintf( '%s<span id="subscribe-%d" %s><a href="%s" class="subscription-toggle" data-topic="%d">%s</a></span>%s', $r['before'], $topic_id, $sub, $url, $topic_id, $text, $r['after'] ); 1135 1136 // Initial output is wrapped in a span, ajax output is hooked to this 1137 if ( !empty( $wrap ) ) { 1138 $html = '<span id="subscription-toggle">' . $html . '</span>'; 1139 } 1140 1098 1141 } 1099 1142 1100 1143 // Return the link -
templates/default/bbpress-functions.php
84 84 85 85 /** Scripts ***********************************************************/ 86 86 87 add_action( 'bbp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); // Enqueue theme CSS 88 add_action( 'bbp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); // Enqueue theme JS 89 add_filter( 'bbp_enqueue_scripts', array( $this, 'localize_topic_script' ) ); // Enqueue theme script localization 90 add_action( 'bbp_head', array( $this, 'head_scripts' ) ); // Output some extra JS in the <head> 91 add_action( 'bbp_ajax_favorite', array( $this, 'ajax_favorite' ) ); // Handles the ajax favorite/unfavorite 92 add_action( 'bbp_ajax_subscription', array( $this, 'ajax_subscription' ) ); // Handles the ajax subscribe/unsubscribe 87 add_action( 'bbp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); // Enqueue theme CSS 88 add_action( 'bbp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); // Enqueue theme JS 89 add_filter( 'bbp_enqueue_scripts', array( $this, 'localize_topic_script' ) ); // Enqueue theme script localization 90 add_action( 'bbp_head', array( $this, 'head_scripts' ) ); // Output some extra JS in the <head> 91 add_action( 'bbp_ajax_favorite', array( $this, 'ajax_favorite' ) ); // Handles the topic ajax favorite/unfavorite 92 add_action( 'bbp_ajax_subscription', array( $this, 'ajax_subscription' ) ); // Handles the topic ajax subscribe/unsubscribe 93 add_action( 'bbp_ajax_forum_subscription', array( $this, 'ajax_forum_subscription' ) ); // Handles the forum ajax subscribe/unsubscribe 93 94 94 95 /** Template Wrappers *************************************************/ 95 96 … … 168 169 * 169 170 * @since bbPress (r3732) 170 171 * 172 * @uses bbp_is_single_forum() To check if it's the forum page 171 173 * @uses bbp_is_single_topic() To check if it's the topic page 174 * @uses bbp_thread_replies() To check if threaded replies are enabled 172 175 * @uses bbp_is_single_user_edit() To check if it's the profile edit page 173 176 * @uses wp_enqueue_script() To enqueue the scripts 174 177 */ … … 179 182 wp_enqueue_script( 'jquery' ); 180 183 } 181 184 185 // Forum-specific scripts 186 if ( bbp_is_single_forum() ) { 187 188 // Forum subscribe/unsubscribe 189 wp_enqueue_script( 'bbpress-forum', $this->url . 'js/forum.js', array( 'jquery' ), $this->version ); 190 } 191 182 192 // Topic-specific scripts 183 193 if ( bbp_is_single_topic() ) { 184 194 … … 285 295 * 286 296 * @since bbPress (r3732) 287 297 * 298 * @uses bbp_is_single_forum() To check if it's the forum page 288 299 * @uses bbp_is_single_topic() To check if it's the topic page 289 300 * @uses is_user_logged_in() To check if user is logged in 290 301 * @uses bbp_get_current_user_id() To get the current user id 302 * @uses bbp_get_forum_id() To get the forum id 291 303 * @uses bbp_get_topic_id() To get the topic id 292 304 * @uses bbp_get_favorites_permalink() To get the favorites permalink 293 305 * @uses bbp_is_user_favorite() To check if the topic is in user's favorites … … 298 310 */ 299 311 public function localize_topic_script() { 300 312 301 // Bail if not viewing a single topic 302 if ( !bbp_is_single_topic() ) 303 return; 313 if( bbp_is_single_forum() ) { 304 314 305 wp_localize_script( 'bbpress-topic', 'bbpTopicJS', array( 306 'bbp_ajaxurl' => bbp_get_ajax_url(), 307 'generic_ajax_error' => __( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ), 308 'is_user_logged_in' => is_user_logged_in(), 309 'fav_nonce' => wp_create_nonce( 'toggle-favorite_' . get_the_ID() ), 310 'subs_nonce' => wp_create_nonce( 'toggle-subscription_' . get_the_ID() ) 311 ) ); 315 wp_localize_script( 'bbpress-forum', 'bbpForumJS', array( 316 'bbp_ajaxurl' => bbp_get_ajax_url(), 317 'generic_ajax_error' => __( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ), 318 'is_user_logged_in' => is_user_logged_in(), 319 'subs_nonce' => wp_create_nonce( 'toggle-subscription_' . get_the_ID() ) 320 ) ); 321 } 322 323 if ( bbp_is_single_topic() ) { 324 325 wp_localize_script( 'bbpress-topic', 'bbpTopicJS', array( 326 'bbp_ajaxurl' => bbp_get_ajax_url(), 327 'generic_ajax_error' => __( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ), 328 'is_user_logged_in' => is_user_logged_in(), 329 'fav_nonce' => wp_create_nonce( 'toggle-favorite_' . get_the_ID() ), 330 'subs_nonce' => wp_create_nonce( 'toggle-subscription_' . get_the_ID() ) 331 ) ); 332 } 312 333 } 313 334 314 335 /** 336 * AJAX handler to Subscribe/Unsubscribe a user from a forum 337 * 338 * @since bbPress (r5155) 339 * 340 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active 341 * @uses bbp_is_user_logged_in() To check if user is logged in 342 * @uses bbp_get_current_user_id() To get the current user id 343 * @uses current_user_can() To check if the current user can edit the user 344 * @uses bbp_get_forum() To get the forum 345 * @uses wp_verify_nonce() To verify the nonce 346 * @uses bbp_is_user_subscribed() To check if the forum is in user's subscriptions 347 * @uses bbp_remove_user_subscriptions() To remove the forum from user's subscriptions 348 * @uses bbp_add_user_subscriptions() To add the forum from user's subscriptions 349 * @uses bbp_ajax_response() To return JSON 350 */ 351 public function ajax_forum_subscription() { 352 353 // Bail if subscriptions are not active 354 if ( !bbp_is_subscriptions_active() ) { 355 bbp_ajax_response( false, __( 'Subscriptions are no longer active.', 'bbpress' ), 300 ); 356 } 357 358 // Bail if user is not logged in 359 if ( !is_user_logged_in() ) { 360 bbp_ajax_response( false, __( 'Please login to subscribe to this forum.', 'bbpress' ), 301 ); 361 } 362 363 // Get user and forum data 364 $user_id = bbp_get_current_user_id(); 365 $id = intval( $_POST['id'] ); 366 367 // Bail if user cannot add favorites for this user 368 if ( !current_user_can( 'edit_user', $user_id ) ) { 369 bbp_ajax_response( false, __( 'You do not have permission to do this.', 'bbpress' ), 302 ); 370 } 371 372 // Get the forum 373 $forum = bbp_get_forum( $id ); 374 375 // Bail if forum cannot be found 376 if ( empty( $forum ) ) { 377 bbp_ajax_response( false, __( 'The forum could not be found.', 'bbpress' ), 303 ); 378 } 379 380 // Bail if user did not take this action 381 if ( !isset( $_POST['nonce'] ) || !wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $forum->ID ) ) { 382 bbp_ajax_response( false, __( 'Are you sure you meant to do that?', 'bbpress' ), 304 ); 383 } 384 385 // Take action 386 $status = bbp_is_user_subscribed( $user_id, $forum->ID ) ? bbp_remove_user_subscription( $user_id, $forum->ID ) : bbp_add_user_subscription( $user_id, $forum->ID ); 387 388 // Bail if action failed 389 if ( empty( $status ) ) { 390 bbp_ajax_response( false, __( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 ); 391 } 392 393 // Put subscription attributes in convenient array 394 $attrs = array( 395 'forum_id' => $forum->ID, 396 'user_id' => $user_id 397 ); 398 399 // Action succeeded 400 bbp_ajax_response( true, bbp_get_forum_subscription_link( $attrs, $user_id, false ), 200 ); 401 } 402 403 /** 315 404 * AJAX handler to add or remove a topic from a user's favorites 316 405 * 317 406 * @since bbPress (r3732) 318 407 * 408 * @uses bbp_is_favorites_active() To check if favorites are active 409 * @uses bbp_is_user_logged_in() To check if user is logged in 319 410 * @uses bbp_get_current_user_id() To get the current user id 320 411 * @uses current_user_can() To check if the current user can edit the user 321 412 * @uses bbp_get_topic() To get the topic … … 383 474 * @since bbPress (r3732) 384 475 * 385 476 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active 477 * @uses bbp_is_user_logged_in() To check if user is logged in 386 478 * @uses bbp_get_current_user_id() To get the current user id 387 479 * @uses current_user_can() To check if the current user can edit the user 388 480 * @uses bbp_get_topic() To get the topic -
templates/default/bbpress/content-archive-forum.php
23 23 24 24 <?php bbp_breadcrumb(); ?> 25 25 26 <?php bbp_forum_subscription_link(); ?> 27 26 28 <?php do_action( 'bbp_template_before_forums_index' ); ?> 27 29 28 30 <?php if ( bbp_has_forums() ) : ?> -
templates/default/bbpress/content-single-forum.php
13 13 14 14 <?php bbp_breadcrumb(); ?> 15 15 16 <?php bbp_forum_subscription_link(); ?> 17 16 18 <?php do_action( 'bbp_template_before_single_forum' ); ?> 17 19 18 20 <?php if ( post_password_required() ) : ?> -
templates/default/css/bbpress-rtl.css
173 173 margin: 8px 0; 174 174 padding: 0; 175 175 text-transform: none; 176 } 176 } 177 177 178 #bbpress-forums div.bbp-forum-author, 178 #bbpress-forums div.bbp-forum-author, 179 179 #bbpress-forums div.bbp-topic-author, 180 180 #bbpress-forums div.bbp-reply-author { 181 181 float: right; … … 183 183 width: 115px; 184 184 } 185 185 186 #bbpress-forums div.bbp-forum-author img.avatar, 186 #bbpress-forums div.bbp-forum-author img.avatar, 187 187 #bbpress-forums div.bbp-topic-author img.avatar, 188 188 #bbpress-forums div.bbp-reply-author img.avatar { 189 189 border: none; … … 250 250 padding: 8px; 251 251 } 252 252 253 div.bbp-forum-header, 253 div.bbp-forum-header, 254 254 div.bbp-topic-header, 255 255 div.bbp-reply-header { 256 256 border-top: 1px solid #ddd; … … 398 398 text-align: left; 399 399 } 400 400 401 /* =Breadcrumb and Tags401 /* =Breadcrumb, Forum Subscription Link and Tags 402 402 -------------------------------------------------------------- */ 403 403 404 404 div.bbp-breadcrumb { … … 415 415 margin-bottom: 10px 416 416 } 417 417 418 #bbpress-forums div.bbp-forum-subscription { 419 float: left; 420 } 421 418 422 #bbpress-forums div.bbp-topic-tags { 419 423 float: left; 420 424 } … … 473 477 width: 50%; 474 478 } 475 479 476 .bbp-forum-header a.bbp-forum-permalink, 480 .bbp-forum-header a.bbp-forum-permalink, 477 481 .bbp-topic-header a.bbp-topic-permalink, 478 482 .bbp-reply-header a.bbp-reply-permalink { 479 483 float: left; … … 1181 1185 display: inline-block; 1182 1186 margin-right: 0; 1183 1187 word-wrap: break-word; 1184 } 1188 } 1185 1189 #bbpress-forums div.bbp-reply-author img.avatar { 1186 1190 position: absolute; 1187 1191 top: 15px; … … 1204 1208 div.bbp-submit-wrapper { 1205 1209 float: right; 1206 1210 } 1207 #bbpress-forums fieldset.bbp-form { 1211 #bbpress-forums fieldset.bbp-form { 1208 1212 padding: 0 10px 10px; 1209 1213 } 1210 1214 #bbpress-forums #bbp-user-body { … … 1299 1303 #bbpress-forums li.bbp-header li.bbp-topic-reply-count, 1300 1304 #bbpress-forums li.bbp-body li.bbp-topic-voice-count, 1301 1305 #bbpress-forums li.bbp-body li.bbp-topic-reply-count, 1302 #bbpress-forums li.bbp-footer div.bbp-reply-author, 1306 #bbpress-forums li.bbp-footer div.bbp-reply-author, 1303 1307 #bbpress-forums li.bbp-footer div.bbp-reply-content { 1304 1308 width: 45%; 1305 1309 } … … 1313 1317 width: 100%; 1314 1318 } 1315 1319 #bbpress-forums li.bbp-header li.bbp-forum-freshness, 1316 #bbpress-forums li.bbp-header li.bbp-topic-freshness { 1320 #bbpress-forums li.bbp-header li.bbp-topic-freshness { 1317 1321 text-align: center; 1318 1322 } 1319 1323 #bbpress-forums li.bbp-body li.bbp-topic-freshness, … … 1338 1342 #bbpress-forums div.bbp-reply-author img.avatar { 1339 1343 width: 40px; 1340 1344 height: auto; 1341 } 1345 } 1342 1346 } 1347 No newline at end of file -
templates/default/css/bbpress.css
398 398 text-align: right; 399 399 } 400 400 401 /* =Breadcrumb and Tags401 /* =Breadcrumb, Forum Subscription Link and Tags 402 402 -------------------------------------------------------------- */ 403 403 404 404 div.bbp-breadcrumb { … … 415 415 margin-bottom: 10px 416 416 } 417 417 418 #bbpress-forums div.bbp-forum-subscription { 419 float: right; 420 } 421 418 422 #bbpress-forums div.bbp-topic-tags { 419 423 float: right; 420 424 }