Ticket #2299: forum-subscriptions2.diff
File forum-subscriptions2.diff, 45.1 KB (added by , 10 years ago) |
---|
-
includes/common/functions.php
diff --git includes/common/functions.php includes/common/functions.php index 67f9dd7..d12586f 100644
Login and visit the topic to unsubscribe from these emails.', 'bbpress' ), 1108 1108 return true; 1109 1109 } 1110 1110 1111 /** 1112 * Sends notification emails for new posts 1113 * 1114 * Gets new post's ID and check if there are subscribed users to that topic, and 1115 * if there are, send notifications 1116 * 1117 * @since bbPress (r2668) 1118 * 1119 * @param int $reply_id ID of the newly made reply 1120 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active 1121 * @uses bbp_get_reply_id() To validate the reply ID 1122 * @uses bbp_get_reply() To get the reply 1123 * @uses bbp_get_reply_topic_id() To get the topic ID of the reply 1124 * @uses bbp_is_reply_published() To make sure the reply is published 1125 * @uses bbp_get_topic_id() To validate the topic ID 1126 * @uses bbp_get_topic() To get the reply's topic 1127 * @uses bbp_is_topic_published() To make sure the topic is published 1128 * @uses get_the_author_meta() To get the author's display name 1129 * @uses do_action() Calls 'bbp_pre_notify_subscribers' with the reply id and 1130 * topic id 1131 * @uses bbp_get_topic_subscribers() To get the topic subscribers 1132 * @uses apply_filters() Calls 'bbp_subscription_mail_message' with the 1133 * message, reply id, topic id and user id 1134 * @uses get_userdata() To get the user data 1135 * @uses wp_mail() To send the mail 1136 * @uses do_action() Calls 'bbp_post_notify_subscribers' with the reply id 1137 * and topic id 1138 * @return bool True on success, false on failure 1139 */ 1140 function bbp_notify_forum_subscribers( $topic_id = 0, $forum_id = 0, $anonymous_data = false, $topic_author = 0 ) { 1141 1142 // Bail if subscriptions are turned off 1143 if ( !bbp_is_subscriptions_active() ) 1144 return false; 1145 1146 /** Validation ************************************************************/ 1147 1148 $topic_id = bbp_get_topic_id( $topic_id ); 1149 $forum_id = bbp_get_forum_id( $forum_id ); 1150 1151 /** Topic *****************************************************************/ 1152 1153 // Bail if topic is not published 1154 if ( ! bbp_is_topic_published( $topic_id ) ) 1155 return false; 1156 1157 /** User ******************************************************************/ 1158 1159 // Get subscribers and bail if empty 1160 $user_ids = bbp_get_forum_subscribers( $forum_id, true ); 1161 if ( empty( $user_ids ) ) 1162 return false; 1163 1164 // Poster name 1165 $topic_author_name = bbp_get_topic_author_display_name( $topic_id ); 1166 1167 /** Mail ******************************************************************/ 1168 1169 do_action( 'bbp_pre_notify_forum_subscribers', $topic_id, $forum_id, $user_ids ); 1170 1171 // Remove filters from reply content and topic title to prevent content 1172 // from being encoded with HTML entities, wrapped in paragraph tags, etc... 1173 remove_all_filters( 'bbp_get_topic_content' ); 1174 remove_all_filters( 'bbp_get_topic_title' ); 1175 1176 // Strip tags from text 1177 $topic_title = strip_tags( bbp_get_topic_title( $topic_id ) ); 1178 $topic_content = strip_tags( bbp_get_topic_content( $topic_id ) ); 1179 $topic_url = get_permalink( $topic_id ); 1180 $blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); 1181 1182 // Loop through users 1183 foreach ( (array) $user_ids as $user_id ) { 1184 1185 // Don't send notifications to the person who made the post 1186 if ( !empty( $topic_author ) && (int) $user_id === (int) $topic_author ) 1187 continue; 1188 1189 // For plugins to filter messages per reply/topic/user 1190 $message = sprintf( __( '%1$s wrote: 1191 1192 %2$s 1193 1194 Topic Link: %3$s 1195 1196 ----------- 1197 1198 You are receiving this email because you subscribed to a forum. 1199 1200 Login and visit the topic to unsubscribe from these emails.', 'bbpress' ), 1201 1202 $topic_author_name, 1203 $topic_content, 1204 $topic_url 1205 ); 1206 $message = apply_filters( 'bbp_forum_subscription_mail_message', $message, $topic_id, $forum_id, $user_id ); 1207 if ( empty( $message ) ) 1208 continue; 1209 1210 // For plugins to filter titles per reply/topic/user 1211 $subject = apply_filters( 'bbp_forum_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $topic_id, $forum_id, $user_id ); 1212 if ( empty( $subject ) ) 1213 continue; 1214 1215 // Custom headers 1216 $headers = apply_filters( 'bbp_forum_subscription_mail_headers', array() ); 1217 1218 // Get user data of this user 1219 $user = get_userdata( $user_id ); 1220 1221 // Send notification email 1222 wp_mail( $user->user_email, $subject, $message, $headers ); 1223 } 1224 1225 do_action( 'bbp_post_notify_forum_subscribers', $topic_id, $forum_id, $user_ids ); 1226 1227 return true; 1228 } 1229 1111 1230 /** Login *********************************************************************/ 1112 1231 1113 1232 /** … … function bbp_request_feed_trap( $query_vars = array() ) { 1531 1650 1532 1651 // Forum/Topic/Reply Feed 1533 1652 if ( isset( $query_vars['post_type'] ) ) { 1534 1653 1535 1654 // Supported select query vars 1536 1655 $select_query_vars = array( 1537 1656 'p' => false, -
includes/common/template.php
diff --git includes/common/template.php includes/common/template.php index c943158..65ae111 100644
function bbp_breadcrumb( $args = array() ) { 2332 2332 return apply_filters( 'bbp_get_breadcrumb', $trail, $crumbs, $r ); 2333 2333 } 2334 2334 2335 2336 /** Forum Subscriptions ******************************************************/ 2337 2338 /** 2339 * Output the forum subscription link 2340 * 2341 * @uses bbp_get_forum_subscription_link() 2342 */ 2343 function bbp_forum_subscription_link( $args = array() ) { 2344 echo bbp_get_forum_subscription_link( $args ); 2345 } 2346 2347 /** 2348 * Get the forum subscription link 2349 * 2350 * @uses bbp_parse_args() 2351 * @uses bbp_get_user_subscribe_link() 2352 */ 2353 function bbp_get_forum_subscription_link( $args = array() ) { 2354 2355 // Parse args 2356 $r = bbp_parse_args( $args, array( 2357 2358 // Forum 2359 'forum_id' => 0, 2360 2361 // User 2362 'user_id' => 0, 2363 2364 // HTML 2365 'before' => '<div class="bbp-forum-subscription"><p>', 2366 'after' => '</p></div>', 2367 2368 // Text 2369 'subscribe' => __( 'Subscribe to Forum', 'bbpress' ), 2370 'unsubscribe' => __( 'Unsubscribe from Forum', 'bbpress' ), 2371 2372 ), 'get_forum_subscribe_link' ); 2373 2374 $link = $r['before']; 2375 2376 $link .= bbp_get_user_subscribe_link( array( 2377 'before' => '', 2378 'subscribe' => $r['subscribe'], 2379 'unsubscribe' => $r['unsubscribe'] 2380 ) ); 2381 2382 $link .= $r['after']; 2383 2384 return apply_filters( 'bbp_get_forum_subscribe_link', $link, $r ); 2385 } 2386 2335 2387 /** Topic Tags ***************************************************************/ 2336 2388 2337 2389 /** -
includes/core/actions.php
diff --git includes/core/actions.php includes/core/actions.php index a698269..b870ae3 100644
add_action( 'bbp_delete_topic', 'bbp_remove_topic_from_all_favorites' ); 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_new_reply', 'bbp_notify_subscribers', 11, 5 ); 227 228 // TODO add remove forum subscriptions for forums 229 230 add_action( 'bbp_new_reply', 'bbp_notify_subscribers', 11, 5 ); 231 add_action( 'bbp_new_topic', 'bbp_notify_forum_subscribers', 11, 4 ); 228 232 229 233 // Sticky 230 234 add_action( 'bbp_trash_topic', 'bbp_unstick_topic' ); … … add_action( 'bbp_get_request', 'bbp_toggle_topic_handler', 1 ); 301 305 add_action( 'bbp_get_request', 'bbp_toggle_reply_handler', 1 ); 302 306 add_action( 'bbp_get_request', 'bbp_favorites_handler', 1 ); 303 307 add_action( 'bbp_get_request', 'bbp_subscriptions_handler', 1 ); 308 add_action( 'bbp_get_request', 'bbp_forum_subscriptions_handler', 1 ); 304 309 add_action( 'bbp_get_request', 'bbp_search_results_redirect', 10 ); 305 310 306 311 // Maybe convert the users password -
includes/forums/functions.php
diff --git includes/forums/functions.php includes/forums/functions.php index 5952ad5..137a3eb 100644
function bbp_repair_forum_visibility() { 957 957 return true; 958 958 } 959 959 960 /** Subscriptions *************************************************/ 961 962 /** 963 * Remove a deleted forum from all users' subscriptions 964 * 965 * @since bbPress (r2652) 966 * 967 * @param int $forum_id forum ID to remove 968 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active 969 * @uses bbp_get_forum_subscribers() To get the forum subscribers 970 * @uses bbp_remove_user_subscription() To remove the user subscription 971 */ 972 function bbp_remove_forum_from_all_subscriptions( $forum_id = 0 ) { 973 974 // Subscriptions are not active 975 if ( ! bbp_is_subscriptions_active() ) 976 return; 977 978 $forum_id = bbp_get_forum_id( $forum_id ); 979 980 // Bail if no forum 981 if ( empty( $forum_id ) ) 982 return; 983 984 // Get users 985 $users = (array) bbp_get_forum_subscribers( $forum_id ); 986 987 // Users exist 988 if ( !empty( $users ) ) { 989 990 // Loop through users 991 foreach ( $users as $user ) { 992 993 // Remove each user 994 bbp_remove_user_subscription( $user, $forum_id ); 995 } 996 } 997 } 998 960 999 /** Count Bumpers *************************************************************/ 961 1000 962 1001 /** -
includes/forums/template.php
diff --git includes/forums/template.php includes/forums/template.php index 6f5a3aa..7d3524e 100644
function bbp_forum_row_actions() { 385 385 } 386 386 387 387 /** 388 * Output checked value of topic subscription 389 * 390 * @since bbPress (r2976) 391 * 392 * @uses bbp_get_form_topic_subscribed() To get the subscribed checkbox value 393 */ 394 function bbp_form_forum_subscribed() { 395 echo bbp_get_form_forum_subscribed(); 396 } 397 /** 398 * Return checked value of forum subscription 399 * 400 * @since bbPress (r2976) 401 * 402 * @uses bbp_is_forum_edit() To check if it's the forum edit page 403 * @uses bbp_is_user_subscribed() To check if the user is subscribed to 404 * the forum 405 * @uses apply_filters() Calls 'bbp_get_form_forum_subscribed' with the 406 * option 407 * @return string Checked value of forum subscription 408 */ 409 function bbp_get_form_forum_subscribed() { 410 411 // Get _POST data 412 if ( bbp_is_post_request() && isset( $_POST['bbp_forum_subscription'] ) ) { 413 $forum_subscribed = (bool) $_POST['bbp_forum_subscription']; 414 415 // Get edit data 416 } elseif ( bbp_is_forum_edit() || bbp_is_reply_edit() ) { 417 418 // Get current posts author 419 $post_author = bbp_get_global_post_field( 'post_author', 'raw' ); 420 421 // Post author is not the current user 422 if ( bbp_get_current_user_id() !== $post_author ) { 423 $forum_subscribed = bbp_is_user_subscribed( $post_author ); 424 425 // Post author is the current user 426 } else { 427 $forum_subscribed = bbp_is_user_subscribed( bbp_get_current_user_id() ); 428 } 429 430 // Get current status 431 } elseif ( bbp_is_single_forum() ) { 432 $forum_subscribed = bbp_is_user_subscribed( bbp_get_current_user_id() ); 433 434 // No data 435 } else { 436 $forum_subscribed = false; 437 } 438 439 // Get checked output 440 $checked = checked( $forum_subscribed, true, false ); 441 442 return apply_filters( 'bbp_get_form_forum_subscribed', $checked, $forum_subscribed ); 443 } 444 445 /** 388 446 * Output the forums last active ID 389 447 * 390 448 * @since bbPress (r2860) -
includes/users/functions.php
diff --git includes/users/functions.php includes/users/functions.php index caa11de..7d1eb47 100644
function bbp_is_user_favorite( $user_id = 0, $topic_id = 0 ) { 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 ); … … function bbp_get_topic_subscribers( $topic_id = 0 ) { 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) … … function bbp_get_user_subscribed_topic_ids( $user_id = 0 ) { 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 … … function bbp_get_user_subscribed_topic_ids( $user_id = 0 ) { 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 ); … … function bbp_is_user_subscribed( $user_id = 0, $topic_id = 0 ) { 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() ) { … … function bbp_is_user_subscribed( $user_id = 0, $topic_id = 0 ) { 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 ); 690 } 691 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 ); 642 784 } 643 785 644 786 /** … … function bbp_is_user_subscribed( $user_id = 0, $topic_id = 0 ) { 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 … … function bbp_add_user_subscription( $user_id = 0, $topic_id = 0 ) { 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 from user's subscriptions 822 * Add a forum to user's subscriptions 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 681 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 … … function bbp_remove_user_subscription( $user_id, $topic_id ) { 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 ); 955 956 return true; 957 } 958 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 ); 719 1041 720 1042 return true; 721 1043 } … … function bbp_subscriptions_handler( $action = '' ) { 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 /** … … function bbp_user_edit_after() { 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 ) ) … … function bbp_get_user_topics_started( $user_id = 0 ) { 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 ) ) … … function bbp_get_total_users() { 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
diff --git includes/users/template.php includes/users/template.php index 6ccf1ef..41ea205 100644
function bbp_user_subscribe_link( $args = '', $user_id = 0, $wrap = true ) { 874 874 * @return string Permanent link to topic 875 875 */ 876 876 function bbp_get_user_subscribe_link( $args = '', $user_id = 0, $wrap = true ) { 877 if ( ! bbp_is_subscriptions_active() )877 if ( ! bbp_is_subscriptions_active() ) 878 878 return; 879 879 880 880 // Parse arguments against default values … … function bbp_user_subscribe_link( $args = '', $user_id = 0, $wrap = true ) { 883 883 'unsubscribe' => __( 'Unsubscribe', 'bbpress' ), 884 884 'user_id' => 0, 885 885 'topic_id' => 0, 886 'forum_id' => 0, 886 887 'before' => ' | ', 887 888 'after' => '' 888 889 ), 'get_user_subscribe_link' ); 889 890 890 // Validate user and topic ID's 891 $user_id = bbp_get_user_id( $r['user_id'], true, true ); 892 $topic_id = bbp_get_topic_id( $r['topic_id'] ); 893 if ( empty( $user_id ) || empty( $topic_id ) ) { 891 // Validate user and object ID's 892 $user_id = bbp_get_user_id( $r['user_id'], true, true ); 893 $topic_id = bbp_get_topic_id( $r['topic_id'] ); 894 $forum_id = bbp_get_forum_id( $r['forum_id'] ); 895 if ( empty( $user_id ) || ( empty( $topic_id ) && empty( $forum_id ) ) ) { 894 896 return false; 895 897 } 896 898 897 899 // No link if you can't edit yourself 898 if ( ! current_user_can( 'edit_user', (int) $user_id ) ) {900 if ( ! current_user_can( 'edit_user', (int) $user_id ) ) { 899 901 return false; 900 902 } 901 903 902 // Decide which link to show 903 $is_subscribed = bbp_is_user_subscribed( $user_id, $topic_id ); 904 if ( !empty( $is_subscribed ) ) { 905 $text = $r['unsubscribe']; 906 $query_args = array( 'action' => 'bbp_unsubscribe', 'topic_id' => $topic_id ); 907 } else { 908 $text = $r['subscribe']; 909 $query_args = array( 'action' => 'bbp_subscribe', 'topic_id' => $topic_id ); 910 } 904 // Check if viewing a single forum 905 if( empty( $topic_id ) && ! empty( $forum_id ) ) { 906 907 // Decide which link to show 908 $is_subscribed = bbp_is_user_subscribed_to_forum( $user_id, $forum_id ); 909 if ( ! empty( $is_subscribed ) ) { 910 $text = $r['unsubscribe']; 911 $query_args = array( 'action' => 'bbp_unsubscribe', 'forum_id' => $forum_id ); 912 } else { 913 $text = $r['subscribe']; 914 $query_args = array( 'action' => 'bbp_subscribe', 'forum_id' => $forum_id ); 915 } 916 917 // Create the link based where the user is and if the user is 918 // subscribed already 919 if ( bbp_is_subscriptions() ) { 920 $permalink = bbp_get_subscriptions_permalink( $user_id ); 921 } elseif ( bbp_is_single_forum() || bbp_is_single_reply() ) { 922 $permalink = bbp_get_forum_permalink( $forum_id ); 923 } else { 924 $permalink = get_permalink(); 925 } 926 927 $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $forum_id ) ); 928 $sub = $is_subscribed ? ' class="is-subscribed"' : ''; 929 $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'] ); 930 931 // Initial output is wrapped in a span, ajax output is hooked to this 932 if ( !empty( $wrap ) ) { 933 $html = '<span id="subscription-toggle">' . $html . '</span>'; 934 } 935 911 936 912 // Create the link based where the user is and if the user is913 // subscribed already914 if ( bbp_is_subscriptions() ) {915 $permalink = bbp_get_subscriptions_permalink( $user_id );916 } elseif ( bbp_is_single_topic() || bbp_is_single_reply() ) {917 $permalink = bbp_get_topic_permalink( $topic_id );918 937 } else { 919 $permalink = get_permalink();920 }921 938 922 $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) ); 923 $sub = $is_subscribed ? ' class="is-subscribed"' : ''; 924 $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'] ); 939 // Decide which link to show 940 $is_subscribed = bbp_is_user_subscribed_to_topic( $user_id, $topic_id ); 941 if ( ! empty( $is_subscribed ) ) { 942 $text = $r['unsubscribe']; 943 $query_args = array( 'action' => 'bbp_unsubscribe', 'topic_id' => $topic_id ); 944 } else { 945 $text = $r['subscribe']; 946 $query_args = array( 'action' => 'bbp_subscribe', 'topic_id' => $topic_id ); 947 } 948 949 // Create the link based where the user is and if the user is 950 // subscribed already 951 if ( bbp_is_subscriptions() ) { 952 $permalink = bbp_get_subscriptions_permalink( $user_id ); 953 } elseif ( bbp_is_single_topic() || bbp_is_single_reply() ) { 954 $permalink = bbp_get_topic_permalink( $topic_id ); 955 } else { 956 $permalink = get_permalink(); 957 } 958 959 $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) ); 960 $sub = $is_subscribed ? ' class="is-subscribed"' : ''; 961 $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'] ); 962 963 // Initial output is wrapped in a span, ajax output is hooked to this 964 if ( !empty( $wrap ) ) { 965 $html = '<span id="subscription-toggle">' . $html . '</span>'; 966 } 925 967 926 // Initial output is wrapped in a span, ajax output is hooked to this927 if ( !empty( $wrap ) ) {928 $html = '<span id="subscription-toggle">' . $html . '</span>';929 968 } 930 969 931 970 // Return the link -
templates/default/bbpress-functions.php
diff --git templates/default/bbpress-functions.php templates/default/bbpress-functions.php index d216947..a31e7c5 100644
class BBP_Default extends BBP_Theme_Compat { 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 ajax favorite/unfavorite 92 add_action( 'bbp_ajax_subscription', array( $this, 'ajax_subscription' ) ); // Handles the ajax subscribe/unsubscribe 93 add_action( 'bbp_ajax_forum_subscription', array( $this, 'ajax_forum_subscription' ) ); // Handles the ajax subscribe/unsubscribe 93 94 94 95 /** Template Wrappers *************************************************/ 95 96 … … class BBP_Default extends BBP_Theme_Compat { 191 192 } 192 193 } 193 194 195 // Forum-specific scripts 196 if ( bbp_is_single_forum() ) { 197 198 // Forum subscribe/unsubscribe 199 wp_enqueue_script( 'bbpress-forum', $this->url . 'js/forum.js', array( 'jquery' ), $this->version ); 200 201 } 202 194 203 // User Profile edit 195 204 if ( bbp_is_single_user_edit() ) { 196 205 wp_enqueue_script( 'user-profile' ); … … class BBP_Default extends BBP_Theme_Compat { 299 308 public function localize_topic_script() { 300 309 301 310 // Bail if not viewing a single topic 302 if ( !bbp_is_single_topic() ) 303 return; 304 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 ) ); 311 if ( bbp_is_single_topic() ) { 312 313 wp_localize_script( 'bbpress-topic', 'bbpTopicJS', array( 314 'bbp_ajaxurl' => bbp_get_ajax_url(), 315 'generic_ajax_error' => __( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ), 316 'is_user_logged_in' => is_user_logged_in(), 317 'fav_nonce' => wp_create_nonce( 'toggle-favorite_' . get_the_ID() ), 318 'subs_nonce' => wp_create_nonce( 'toggle-subscription_' . get_the_ID() ) 319 ) ); 320 321 } 322 323 if( bbp_is_single_forum() ) { 324 325 wp_localize_script( 'bbpress-forum', 'bbpForumJS', 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 'subs_nonce' => wp_create_nonce( 'toggle-subscription_' . get_the_ID() ) 330 ) ); 331 332 } 312 333 } 313 334 314 335 /** … … class BBP_Default extends BBP_Theme_Compat { 443 464 // Action succeeded 444 465 bbp_ajax_response( true, bbp_get_user_subscribe_link( $attrs, $user_id, false ), 200 ); 445 466 } 467 468 /** 469 * AJAX handler to Subscribe/Unsubscribe a user from a forum 470 * 471 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active 472 * @uses bbp_get_current_user_id() To get the current user id 473 * @uses current_user_can() To check if the current user can edit the user 474 * @uses bbp_get_forum() To get the forum 475 * @uses wp_verify_nonce() To verify the nonce 476 * @uses bbp_is_user_subscribed() To check if the forum is in user's subscriptions 477 * @uses bbp_remove_user_subscriptions() To remove the forum from user's subscriptions 478 * @uses bbp_add_user_subscriptions() To add the forum from user's subscriptions 479 * @uses bbp_ajax_response() To return JSON 480 */ 481 public function ajax_forum_subscription() { 482 483 // Bail if subscriptions are not active 484 if ( !bbp_is_subscriptions_active() ) { 485 bbp_ajax_response( false, __( 'Subscriptions are no longer active.', 'bbpress' ), 300 ); 486 } 487 488 // Bail if user is not logged in 489 if ( !is_user_logged_in() ) { 490 bbp_ajax_response( false, __( 'Please login to subscribe to this forum.', 'bbpress' ), 301 ); 491 } 492 493 // Get user and forum data 494 $user_id = bbp_get_current_user_id(); 495 $id = intval( $_POST['id'] ); 496 497 // Bail if user cannot add favorites for this user 498 if ( !current_user_can( 'edit_user', $user_id ) ) { 499 bbp_ajax_response( false, __( 'You do not have permission to do this.', 'bbpress' ), 302 ); 500 } 501 502 // Get the forum 503 $forum = bbp_get_forum( $id ); 504 505 // Bail if forum cannot be found 506 if ( empty( $forum ) ) { 507 bbp_ajax_response( false, __( 'The forum could not be found.', 'bbpress' ), 303 ); 508 } 509 510 // Bail if user did not take this action 511 if ( !isset( $_POST['nonce'] ) || !wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $forum->ID ) ) { 512 bbp_ajax_response( false, __( 'Are you sure you meant to do that?', 'bbpress' ), 304 ); 513 } 514 515 // Take action 516 $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 ); 517 518 // Bail if action failed 519 if ( empty( $status ) ) { 520 bbp_ajax_response( false, __( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 ); 521 } 522 523 // Put subscription attributes in convenient array 524 $attrs = array( 525 'forum_id' => $forum->ID, 526 'user_id' => $user_id 527 ); 528 529 // Action succeeded 530 bbp_ajax_response( true, bbp_get_forum_subscription_link( $attrs, $user_id, false ), 200 ); 531 } 446 532 } 447 533 new BBP_Default(); 448 534 endif; -
templates/default/bbpress/content-archive-forum.php
diff --git templates/default/bbpress/content-archive-forum.php templates/default/bbpress/content-archive-forum.php index bb17ac8..e9f628c 100644
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
diff --git templates/default/bbpress/content-single-forum.php templates/default/bbpress/content-single-forum.php index c2ecbb0..3f7057d 100644
12 12 <div id="bbpress-forums"> 13 13 14 14 <?php bbp_breadcrumb(); ?> 15 <?php bbp_forum_subscription_link(); ?> 15 16 16 17 <?php do_action( 'bbp_template_before_single_forum' ); ?> 17 18 -
templates/default/css/bbpress.css
diff --git templates/default/css/bbpress.css templates/default/css/bbpress.css index 34caeb8..ad82380 100644
span.bbp-author-ip { 397 397 text-align: right; 398 398 } 399 399 400 /* =Breadcrumb and Tags400 /* =Breadcrumb, Forum Subscription Link and Tags 401 401 -------------------------------------------------------------- */ 402 402 403 403 div.bbp-breadcrumb { … … div.bbp-topic-tags { 414 414 margin-bottom: 10px 415 415 } 416 416 417 #bbpress-forums div.bbp-forum-subscription { 418 float: right; 419 } 420 417 421 #bbpress-forums div.bbp-topic-tags { 418 422 float: right; 419 423 } -
templates/default/js/topic.js
diff --git templates/default/js/topic.js templates/default/js/topic.js index 725e87c..a3f08c4 100644
jQuery( document ).ready( function ( $ ) { 28 28 e.preventDefault(); 29 29 bbp_ajax_call( 'subscription', $( this ).attr( 'data-topic' ), bbpTopicJS.subs_nonce, '#subscription-toggle' ); 30 30 } ); 31 31 32 } );