Ticket #2299: forum-subscriptions.2.diff
File forum-subscriptions.2.diff, 57.3 KB (added by , 10 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 ); … … 512 512 /** Subscriptions *************************************************************/ 513 513 514 514 /** 515 * Get the users who have subscribed to the forum 516 * 517 * @since bbPress (r2668) 518 * 519 * @param int $forum_id Optional. forum id 520 * @uses wpdb::get_col() To execute our query and get the column back 521 * @uses apply_filters() Calls 'bbp_get_forum_subscribers' with the subscribers 522 * @return array|bool Results if the forum has any subscribers, otherwise false 523 */ 524 function bbp_get_forum_subscribers( $forum_id = 0 ) { 525 $forum_id = bbp_get_forum_id( $forum_id ); 526 if ( empty( $forum_id ) ) 527 return; 528 529 global $wpdb; 530 531 $key = $wpdb->prefix . '_bbp_forum_subscriptions'; 532 $users = wp_cache_get( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' ); 533 if ( false === $users ) { 534 $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$forum_id}', meta_value) > 0" ); 535 wp_cache_set( 'bbp_get_forum_subscribers_' . $forum_id, $users, 'bbpress_users' ); 536 } 537 538 return apply_filters( 'bbp_get_forum_subscribers', $users ); 539 } 540 541 /** 515 542 * Get the users who have subscribed to the topic 516 543 * 517 544 * @since bbPress (r2668) … … 569 596 } 570 597 571 598 /** 599 * Get a user's subscribed forum ids 600 * 601 * @since bbPress (r2668) 602 * 603 * @param int $user_id Optional. User id 604 * @uses bbp_get_user_id() To get the user id 605 * @uses get_user_option() To get the user's subscriptions 606 * @uses apply_filters() Calls 'bbp_get_user_subscribed_forum_ids' with 607 * the subscriptions and user id 608 * @return array|bool Results if user has subscriptions, otherwise false 609 */ 610 function bbp_get_user_subscribed_forum_ids( $user_id = 0 ) { 611 $user_id = bbp_get_user_id( $user_id ); 612 if ( empty( $user_id ) ) 613 return false; 614 615 $subscriptions = get_user_option( '_bbp_forum_subscriptions', $user_id ); 616 $subscriptions = array_filter( wp_parse_id_list( $subscriptions ) ); 617 618 return (array) apply_filters( 'bbp_get_user_subscribed_forum_ids', $subscriptions, $user_id ); 619 } 620 621 /** 572 622 * Get a user's subscribed topics' ids 573 623 * 574 624 * @since bbPress (r2668) … … 592 642 } 593 643 594 644 /** 595 * Check if a topic is inuser's subscription list or not645 * Check if a user's subscription list or not 596 646 * 647 * @param int $user_id Optional. User id 648 * @param int $forum_id Optional. Topic id 649 * @uses get_post() To get the post object 650 * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions 651 * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions 652 * @uses bbp_get_forum_post_type() To get the forum post type 653 * @uses bbp_get_topic_post_type() To get the topic post type 654 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id, 655 * forum/topic id and subsriptions 656 * @return bool True if the forum or topic is in user's subscriptions, otherwise false 657 */ 658 function bbp_is_user_subscribed( $user_id = 0, $object_id = 0 ) { 659 if ( empty( $user_id ) || empty( $object_id ) ) 660 return false; 661 662 $object = get_post( $object_id ); 663 if( ! $object ) 664 return false; 665 666 $post_type = $object->post_type; 667 668 switch( $post_type ) { 669 670 case bbp_get_forum_post_type() : 671 672 $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id ); 673 $retval = bbp_is_user_subscribed_to_forum( $user_id, $object_id ); 674 break; 675 676 case bbp_get_topic_post_type() : 677 default : 678 679 $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id ); 680 $retval = bbp_is_user_subscribed_to_topic( $user_id, $object_id ); 681 break; 682 } 683 684 return (bool) apply_filters( 'bbp_is_user_subscribed', $retval, $user_id, $object_id, $subscriptions ); 685 } 686 687 /** 688 * Check if a forum is in user's subscription list or not 689 * 597 690 * @since bbPress (r2668) 598 691 * 599 692 * @param int $user_id Optional. User id 693 * @param int $forum_id Optional. Topic id 694 * @uses bbp_get_user_id() To get the user id 695 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions 696 * @uses bbp_get_forum() To get the forum 697 * @uses bbp_get_forum_id() To get the forum id 698 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id, 699 * forum id and subsriptions 700 * @return bool True if the forum is in user's subscriptions, otherwise false 701 */ 702 function bbp_is_user_subscribed_to_forum( $user_id = 0, $forum_id = 0 ) { 703 704 // Validate user 705 $user_id = bbp_get_user_id( $user_id, true, true ); 706 if ( empty( $user_id ) ) 707 return false; 708 709 $retval = false; 710 $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id ); 711 712 if ( ! empty( $subscriptions ) ) { 713 714 // Checking a specific forum id 715 if ( ! empty( $forum_id ) ) { 716 $forum = bbp_get_forum( $forum_id ); 717 $forum_id = ! empty( $forum ) ? $forum->ID : 0; 718 719 // Using the global forum id 720 } elseif ( bbp_get_forum_id() ) { 721 $forum_id = bbp_get_forum_id(); 722 723 // Use the current post id 724 } elseif ( ! bbp_get_forum_id() ) { 725 $forum_id = get_the_ID(); 726 } 727 728 // Is forum_id in the user's favorites 729 if ( ! empty( $forum_id ) ) { 730 $retval = in_array( $forum_id, $subscriptions ); 731 } 732 } 733 734 return (bool) apply_filters( 'bbp_is_user_subscribed_to_forum', (bool) $retval, $user_id, $forum_id, $subscriptions ); 735 } 736 737 /** 738 * Check if a topic is in user's subscription list or not 739 * 740 * @param int $user_id Optional. User id 600 741 * @param int $topic_id Optional. Topic id 601 742 * @uses bbp_get_user_id() To get the user id 602 743 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions … … 606 747 * topic id and subsriptions 607 748 * @return bool True if the topic is in user's subscriptions, otherwise false 608 749 */ 609 function bbp_is_user_subscribed ( $user_id = 0, $topic_id = 0 ) {750 function bbp_is_user_subscribed_to_topic( $user_id = 0, $topic_id = 0 ) { 610 751 611 752 // Validate user 612 753 $user_id = bbp_get_user_id( $user_id, true, true ); … … 616 757 $retval = false; 617 758 $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id ); 618 759 619 if ( ! empty( $subscriptions ) ) {760 if ( ! empty( $subscriptions ) ) { 620 761 621 762 // Checking a specific topic id 622 if ( ! empty( $topic_id ) ) {623 $topic 624 $topic_id = ! empty( $topic ) ? $topic->ID : 0;763 if ( ! empty( $topic_id ) ) { 764 $topic = bbp_get_topic( $topic_id ); 765 $topic_id = ! empty( $topic ) ? $topic->ID : 0; 625 766 626 767 // Using the global topic id 627 768 } elseif ( bbp_get_topic_id() ) { … … 633 774 } 634 775 635 776 // Is topic_id in the user's favorites 636 if ( ! empty( $topic_id ) ) {777 if ( ! empty( $topic_id ) ) { 637 778 $retval = in_array( $topic_id, $subscriptions ); 638 779 } 639 780 } 640 781 641 return (bool) apply_filters( 'bbp_is_user_subscribed ', (bool) $retval, $user_id, $topic_id, $subscriptions );782 return (bool) apply_filters( 'bbp_is_user_subscribed_to_topic', (bool) $retval, $user_id, $topic_id, $subscriptions ); 642 783 } 643 784 644 785 /** … … 648 789 * 649 790 * @param int $user_id Optional. User id 650 791 * @param int $topic_id Optional. Topic id 792 * @uses get_post() To get the post object 793 * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions 794 * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions 795 * @uses bbp_get_forum_post_type() To get the forum post type 796 * @uses bbp_get_topic_post_type() To get the topic post type 797 * @uses update_user_option() To update the user's subscriptions 798 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id 799 * @return bool Always true 800 */ 801 function bbp_add_user_subscription( $user_id = 0, $object_id = 0 ) { 802 if ( empty( $user_id ) || empty( $object_id ) ) 803 return false; 804 805 $object = get_post( $object_id ); 806 if( ! $object ) 807 return false; 808 809 $post_type = $object->post_type; 810 811 switch( $post_type ) { 812 813 case bbp_get_forum_post_type() : 814 815 $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id ); 816 817 if ( ! in_array( $object_id, $subscriptions ) ) { 818 $subscriptions[] = $object_id; 819 $subscriptions = implode( ',', wp_parse_id_list( array_filter( $subscriptions ) ) ); 820 update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions ); 821 822 wp_cache_delete( 'bbp_get_forum_subscribers_' . $object_id, 'bbpress_users' ); 823 } 824 825 break; 826 827 case bbp_get_topic_post_type() : 828 default : 829 830 $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id ); 831 832 if ( ! in_array( $object_id, $subscriptions ) ) { 833 $subscriptions[] = $object_id; 834 $subscriptions = implode( ',', wp_parse_id_list( array_filter( $subscriptions ) ) ); 835 update_user_option( $user_id, '_bbp_subscriptions', $subscriptions ); 836 837 wp_cache_delete( 'bbp_get_topic_subscribers_' . $object_id, 'bbpress_users' ); 838 } 839 840 break; 841 } 842 843 do_action( 'bbp_add_user_subscription', $user_id, $object_id, $post_type ); 844 845 return true; 846 } 847 848 /** 849 * Add a forum to user's subscriptions 850 * 851 * @param int $user_id Optional. User id 852 * @param int $forum_id Optional. forum id 853 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions 854 * @uses bbp_get_forum() To get the forum 855 * @uses update_user_option() To update the user's subscriptions 856 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & forum id 857 * @return bool Always true 858 */ 859 function bbp_add_user_forum_subscription( $user_id = 0, $forum_id = 0 ) { 860 if ( empty( $user_id ) || empty( $forum_id ) ) 861 return false; 862 863 $forum = bbp_get_forum( $forum_id ); 864 if ( empty( $forum ) ) 865 return false; 866 867 $subscriptions = (array) bbp_get_user_subscribed_forum_ids( $user_id ); 868 if ( !in_array( $forum_id, $subscriptions ) ) { 869 $subscriptions[] = $forum_id; 870 $subscriptions = implode( ',', wp_parse_id_list( array_filter( $subscriptions ) ) ); 871 update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions ); 872 873 wp_cache_delete( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' ); 874 } 875 876 do_action( 'bbp_add_user_forum_subscription', $user_id, $forum_id ); 877 878 return true; 879 } 880 881 /** 882 * Add a topic to user's subscriptions 883 * 884 * @since bbPress (r2668) 885 * 886 * @param int $user_id Optional. User id 887 * @param int $topic_id Optional. Topic id 651 888 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions 652 889 * @uses bbp_get_topic() To get the topic 653 890 * @uses update_user_option() To update the user's subscriptions 654 891 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id 655 892 * @return bool Always true 656 893 */ 657 function bbp_add_user_ subscription( $user_id = 0, $topic_id = 0 ) {894 function bbp_add_user_topic_subscription( $user_id = 0, $topic_id = 0 ) { 658 895 if ( empty( $user_id ) || empty( $topic_id ) ) 659 896 return false; 660 897 … … 671 908 wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' ); 672 909 } 673 910 674 do_action( 'bbp_add_user_ subscription', $user_id, $topic_id );911 do_action( 'bbp_add_user_topic_subscription', $user_id, $topic_id ); 675 912 676 913 return true; 677 914 } … … 683 920 * 684 921 * @param int $user_id Optional. User id 685 922 * @param int $topic_id Optional. Topic id 923 * @uses get_post() To get the post object 924 * @uses bbp_get_forum_post_type() To get the forum post type 925 * @uses bbp_get_topic_post_type() To get the topic post type 926 * @uses bbp_remove_user_forum_subscription() To remove the user's subscription 927 * @uses bbp_remove_user_topic_subscription() To remove the user's subscription 928 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and 929 * topic id 930 * @return bool True if the topic was removed from user's subscriptions, 931 * otherwise false 932 */ 933 function bbp_remove_user_subscription( $user_id = 0, $object_id = 0 ) { 934 if ( empty( $user_id ) || empty( $object_id ) ) 935 return false; 936 937 $post_type = get_post_type( $object_id ); 938 939 switch( $post_type ) { 940 941 case bbp_get_forum_post_type() : 942 943 bbp_remove_user_forum_subscription( $user_id, $object_id ); 944 945 break; 946 947 case bbp_get_topic_post_type() : 948 default : 949 950 bbp_remove_user_topic_subscription( $user_id, $object_id ); 951 952 break; 953 } 954 955 do_action( 'bbp_remove_user_subscription', $user_id, $object_id, $post_type ); 956 957 return true; 958 } 959 960 /** 961 * Remove a forum from user's subscriptions 962 * 963 * @param int $user_id Optional. User id 964 * @param int $forum_id Optional. forum id 965 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions 966 * @uses update_user_option() To update the user's subscriptions 967 * @uses delete_user_option() To delete the user's subscriptions meta 968 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and 969 * forum id 970 * @return bool True if the forum was removed from user's subscriptions, 971 * otherwise false 972 */ 973 function bbp_remove_user_forum_subscription( $user_id, $forum_id ) { 974 if ( empty( $user_id ) || empty( $forum_id ) ) 975 return false; 976 977 $subscriptions = (array) bbp_get_user_subscribed_forum_ids( $user_id ); 978 979 if ( empty( $subscriptions ) ) 980 return false; 981 982 $pos = array_search( $forum_id, $subscriptions ); 983 if ( is_numeric( $pos ) ) { 984 array_splice( $subscriptions, $pos, 1 ); 985 $subscriptions = array_filter( $subscriptions ); 986 987 if ( !empty( $subscriptions ) ) { 988 $subscriptions = implode( ',', wp_parse_id_list( $subscriptions ) ); 989 update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions ); 990 } else { 991 delete_user_option( $user_id, '_bbp_forum_subscriptions' ); 992 } 993 994 wp_cache_delete( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' ); 995 } 996 997 do_action( 'bbp_remove_user_forum_subscription', $user_id, $forum_id ); 998 999 return true; 1000 } 1001 1002 /** 1003 * Remove a topic from user's subscriptions 1004 * 1005 * @param int $user_id Optional. User id 1006 * @param int $topic_id Optional. Topic id 686 1007 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions 687 1008 * @uses update_user_option() To update the user's subscriptions 688 1009 * @uses delete_user_option() To delete the user's subscriptions meta 689 * @uses do_action() Calls 'bbp_remove_user_ subscription' with the user id and1010 * @uses do_action() Calls 'bbp_remove_user_topic_subscription' with the user id and 690 1011 * topic id 691 1012 * @return bool True if the topic was removed from user's subscriptions, 692 1013 * otherwise false 693 1014 */ 694 function bbp_remove_user_ subscription( $user_id, $topic_id ) {1015 function bbp_remove_user_topic_subscription( $user_id, $topic_id ) { 695 1016 if ( empty( $user_id ) || empty( $topic_id ) ) 696 1017 return false; 697 1018 … … 715 1036 wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' ); 716 1037 } 717 1038 718 do_action( 'bbp_remove_user_ subscription', $user_id, $topic_id );1039 do_action( 'bbp_remove_user_topic_subscription', $user_id, $topic_id ); 719 1040 720 1041 return true; 721 1042 } 722 1043 723 1044 /** 1045 * Handles the front end subscribing and unsubscribing forums 1046 * 1047 * @param string $action The requested action to compare this function to 1048 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active 1049 * @uses bbp_get_user_id() To get the user id 1050 * @uses bbp_verify_nonce_request() To verify the nonce and check the request 1051 * @uses current_user_can() To check if the current user can edit the user 1052 * @uses bbPress:errors:add() To log the error messages 1053 * @uses bbp_is_user_subscribed() To check if the forum is in user's 1054 * subscriptions 1055 * @uses bbp_remove_user_subscription() To remove the user subscription 1056 * @uses bbp_add_user_subscription() To add the user subscription 1057 * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id, 1058 * forum id and action 1059 * @uses bbp_is_subscription() To check if it's the subscription page 1060 * @uses bbp_get_subscription_link() To get the subscription page link 1061 * @uses bbp_get_forum_permalink() To get the forum permalink 1062 * @uses wp_safe_redirect() To redirect to the url 1063 */ 1064 function bbp_forum_subscriptions_handler( $action = '' ) { 1065 1066 if ( !bbp_is_subscriptions_active() ) 1067 return false; 1068 1069 // Bail if no forum ID is passed 1070 if ( empty( $_GET['forum_id'] ) ) 1071 return; 1072 1073 // Setup possible get actions 1074 $possible_actions = array( 1075 'bbp_subscribe', 1076 'bbp_unsubscribe', 1077 ); 1078 1079 // Bail if actions aren't meant for this function 1080 if ( !in_array( $action, $possible_actions ) ) 1081 return; 1082 1083 // Get required data 1084 $user_id = bbp_get_user_id( 0, true, true ); 1085 $forum_id = intval( $_GET['forum_id'] ); 1086 1087 // Check for empty forum 1088 if ( empty( $forum_id ) ) { 1089 bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: No forum was found! Which forum are you subscribing/unsubscribing to?', 'bbpress' ) ); 1090 1091 // Check nonce 1092 } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $forum_id ) ) { 1093 bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); 1094 1095 // Check current user's ability to edit the user 1096 } elseif ( !current_user_can( 'edit_user', $user_id ) ) { 1097 bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) ); 1098 } 1099 1100 // Bail if we have errors 1101 if ( bbp_has_errors() ) 1102 return; 1103 1104 /** No errors *************************************************************/ 1105 1106 $is_subscription = bbp_is_user_subscribed( $user_id, $forum_id ); 1107 $success = false; 1108 1109 if ( true === $is_subscription && 'bbp_unsubscribe' === $action ) 1110 $success = bbp_remove_user_subscription( $user_id, $forum_id ); 1111 elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) 1112 $success = bbp_add_user_subscription( $user_id, $forum_id ); 1113 1114 // Do additional subscriptions actions 1115 do_action( 'bbp_subscriptions_handler', $success, $user_id, $forum_id, $action ); 1116 1117 // Success! 1118 if ( true === $success ) { 1119 1120 // Redirect back from whence we came 1121 if ( bbp_is_subscriptions() ) { 1122 $redirect = bbp_get_subscriptions_permalink( $user_id ); 1123 } elseif ( bbp_is_single_user() ) { 1124 $redirect = bbp_get_user_profile_url(); 1125 } elseif ( is_singular( bbp_get_forum_post_type() ) ) { 1126 $redirect = bbp_get_forum_permalink( $forum_id ); 1127 } elseif ( is_single() || is_page() ) { 1128 $redirect = get_permalink(); 1129 } else { 1130 $redirect = get_permalink( $forum_id ); 1131 } 1132 1133 wp_safe_redirect( $redirect ); 1134 1135 // For good measure 1136 exit(); 1137 1138 // Fail! Handle errors 1139 } elseif ( true === $is_subscription && 'bbp_unsubscribe' === $action ) { 1140 bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that forum!', 'bbpress' ) ); 1141 } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) { 1142 bbp_add_error( 'bbp_subscribe', __( '<strong>ERROR</strong>: There was a problem subscribing to that forum!', 'bbpress' ) ); 1143 } 1144 } 1145 1146 /** 724 1147 * Handles the front end subscribing and unsubscribing topics 725 1148 * 726 1149 * @param string $action The requested action to compare this function to … … 822 1245 } 823 1246 } 824 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/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/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/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 }