Ticket #2959: 2959.1.diff
| File 2959.1.diff, 30.5 KB (added by , 10 years ago) |
|---|
-
src/includes/users/capabilities.php
811 811 * @return @mixed 812 812 */ 813 813 function bbp_add_moderator( $object_id = 0, $user_id = 0 ) { 814 return add_post_meta( $object_id, '_bbp_moderator_id', $user_id);814 return bbp_add_object_user_term( $object_id, $user_id, '_bbp_moderator_id' ); 815 815 } 816 816 817 817 /** … … 825 825 * @return mixed 826 826 */ 827 827 function bbp_remove_moderator( $object_id = 0, $user_id = 0 ) { 828 return delete_post_meta( $object_id, '_bbp_moderator_id', $user_id);828 return bbp_remove_object_user_term( $object_id, $user_id, '_bbp_moderator_id' ); 829 829 } 830 830 831 831 /** … … 838 838 * @return mixed 839 839 */ 840 840 function bbp_get_moderator_ids( $object_id = 0 ) { 841 return get_post_meta( $object_id, '_bbp_moderator_id', false);841 return bbp_get_user_terms_by_object( $object_id, '_bbp_moderator_id' ); 842 842 } 843 843 844 844 /** … … 865 865 'include' => bbp_get_moderator_ids( $object_id ), 866 866 ) ); 867 867 } 868 error_log( print_r( $users, true ) ); 868 869 869 870 return apply_filters( 'bbp_get_moderators', $users, $object_id ); 870 871 } -
src/includes/users/functions.php
169 169 return apply_filters( 'bbp_current_author_ua', $retval ); 170 170 } 171 171 172 /** User Taxonomy Terms *******************************************************/ 173 174 /** 175 * Check if the user term is set on an object 176 * 177 * @since 2.7 bbPress () 178 * 179 * @param int $object_id The object id 180 * @param int $user_id The user id 181 * @param string $taxonomy The object user taxonomy 182 * @uses get_post_meta() To check if the user term is set on the object 183 * @uses apply_filters() Calls 'bbp_object_has_user_term' with the object id, 184 * user id, and taxonomy 185 * @return bool Returns true if the user term is set on the object for the 186 * taxonomy, otherwise false 187 */ 188 function bbp_object_has_user_term( $object_id = 0, $user_id = 0, $taxonomy = '' ) { 189 if ( empty( $object_id ) || empty( $user_id ) || empty( $taxonomy ) ) { 190 return; 191 } 192 193 $user_ids = get_post_meta( $object_id, $taxonomy, false ); 194 $retval = (bool) in_array( $user_id, $user_ids ); 195 return apply_filters( 'bbp_object_has_user_term', $retval, $object_id, $user_id, $taxonomy ); 196 } 197 198 /** 199 * Set a user term on an object 200 * 201 * @since 2.7 bbPress () 202 * 203 * @param int $object_id The object id 204 * @param int $user_id The user id 205 * @param string $taxonomy The user term taxonomy 206 * @uses bbp_object_has_user_term() To check if the term has already been set 207 * @uses add_post_meta() To set the term on the object 208 * @uses apply_filters() Calls 'bbp_add_object_user_term' with the object id, user 209 * id, and taxonomy 210 * @return bool Returns true if the user taxonomy term is added to the object, 211 * otherwise false 212 */ 213 function bbp_add_object_user_term( $object_id = 0, $user_id = 0, $taxonomy = '' ) { 214 if ( empty( $object_id ) || empty( $user_id ) || empty( $taxonomy ) ) { 215 return; 216 } 217 218 if ( bbp_object_has_user_term( $object_id, $user_id, $taxonomy ) ) { 219 $retval = false; 220 } else { 221 $retval = (bool) add_post_meta( $object_id, $taxonomy, $user_id, false ); 222 } 223 return apply_filters( 'bbp_add_object_user_term', $retval, $object_id, $user_id, $taxonomy ); 224 } 225 226 /** 227 * Remove a user term from an object 228 * 229 * @since 2.7 bbPress () 230 * 231 * @param int $object_id The post id 232 * @param int $user_id The user id 233 * @param string $taxonomy The user term taxonomy 234 * @uses bbp_object_has_user_term() To check if the term is set 235 * @uses delete_post_meta() To remove the term from the object 236 * @uses apply_filters() Calls 'bbp_remove_object_user_term' with the object 237 * id, user id, and taxonomy 238 * @return bool Returns true is the user taxonomy term is removed from the object, 239 * otherwise false 240 */ 241 function bbp_remove_object_user_term( $object_id = 0, $user_id = 0, $taxonomy = '' ) { 242 if ( empty( $object_id ) || empty( $user_id ) || empty( $taxonomy ) ) { 243 return; 244 } 245 246 if ( ! bbp_object_has_user_term( $object_id, $user_id, $taxonomy ) ) { 247 $retval = false; 248 } else { 249 $retval = delete_post_meta( $object_id, $taxonomy, $user_id ); 250 } 251 return apply_filters( 'bbp_remove_object_user_term', $retval, $object_id, $user_id, $taxonomy ); 252 } 253 254 /** 255 * Get objects for a user taxonomy term 256 * 257 * @since 2.7 bbPress () 258 * 259 * @param int $user_id The user id 260 * @param string $taxonomy The user term taxonomy 261 * @param string $object_type Optional. The object type 262 * @uses post_type_exists() To validate the object type 263 * @uses get_posts() To get the objects with a meta query 264 * @uses apply_filters() Calls 'bbp_get_objects_by_user_term' with the objects, 265 * user_id, taxonomy, and object type 266 * @return array Returns the objects with the user taxonomy term 267 */ 268 function bbp_get_objects_by_user_term( $user_id = 0, $taxonomy = '', $object_type = null ) { 269 if ( empty( $user_id ) || empty( $taxonomy ) || $object_type && ! post_type_exists( $object_type ) ) { 270 return; 271 } 272 273 $args = array( 274 'meta_key' => $taxonomy, 275 'meta_type' => 'NUMERIC', 276 'meta_value' => $user_id, 277 'numberposts' => -1 278 ); 279 if ( $object_type ) { 280 $args['post_type'] = $object_type; 281 } 282 $objects = get_posts( $args ); 283 return apply_filters( 'bbp_get_objects_by_user_term', $objects, $user_id, $taxonomy, $object_type ); 284 } 285 286 /** 287 * Get user taxonomy terms for an object 288 * 289 * @since 2.7 bbPress () 290 * 291 * @param int $object_id The object id 292 * @param string $taxonomy The user term taxonomy 293 * @uses get_post_meta() To get the user taxonomy terms 294 * @uses apply_filters() Calls 'bbp_get_user_terms_by_object' with the user 295 * taxonomy terms, object id, and taxonomy 296 * @return array Returns the user taxonomy terms of the object 297 */ 298 function bbp_get_user_terms_by_object( $object_id = 0, $taxonomy = '' ) { 299 if ( empty( $object_id ) || empty( $taxonomy ) ) { 300 return; 301 } 302 303 $retval = wp_parse_id_list( get_post_meta( $object_id, $taxonomy, false ) ); 304 return apply_filters( 'bbp_get_user_terms_by_object', $retval, $object_id, $taxonomy ); 305 } 306 172 307 /** Favorites *****************************************************************/ 173 308 174 309 /** … … 188 323 return; 189 324 } 190 325 191 $bbp_db = bbp_db(); 192 $key = $bbp_db->prefix . '_bbp_favorites'; 193 $users = wp_cache_get( 'bbp_get_topic_favoriters_' . $topic_id, 'bbpress_users' ); 194 if ( false === $users ) { 195 $users = $bbp_db->get_col( "SELECT user_id FROM {$bbp_db->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$topic_id}', meta_value) > 0" ); 196 wp_cache_set( 'bbp_get_topic_favoriters_' . $topic_id, $users, 'bbpress_users' ); 197 } 326 $users = bbp_get_user_terms_by_object( $topic_id, '_bbp_favorite' ); 198 327 199 328 return apply_filters( 'bbp_get_topic_favoriters', $users, $topic_id ); 200 329 } … … 235 364 * 236 365 * @param int $user_id Optional. User id 237 366 * @uses bbp_get_user_id() To get the user id 238 * @uses get_user_option() To get the user favorites 367 * @uses bbp_get_objects_by_user_term() To get the user favorites 368 * @uses bbp_get_topic_post_type() To get the topic post type 239 369 * @uses apply_filters() Calls 'bbp_get_user_favorites_topic_ids' with 240 370 * the favorites and user id 241 371 * @return array|bool Results if user has favorites, otherwise false … … 246 376 return false; 247 377 } 248 378 249 $favorites = get_user_option( '_bbp_favorites', $user_id);250 $favorites = array_filter( wp_parse_id_list( $favorites ));379 $favorites = bbp_get_objects_by_user_term( $user_id, '_bbp_favorite', bbp_get_topic_post_type() ); 380 $favorites_ids = array_map( function ( $o ) { return (int) $o->ID; }, $favorites ); 251 381 252 return (array) apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites , $user_id );382 return (array) apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites_ids, $user_id ); 253 383 } 254 384 255 385 /** … … 263 393 * @uses bbp_get_user_favorites_topic_ids() To get the user favorites 264 394 * @uses bbp_get_topic() To get the topic 265 395 * @uses bbp_get_topic_id() To get the topic id 396 * @uses bbp_object_has_user_term() To check if the user has a favorite 266 397 * @uses apply_filters() Calls 'bbp_is_user_favorite' with the bool, user id, 267 398 * topic id and favorites 268 399 * @return bool True if the topic is in user's favorites, otherwise false 269 400 */ 270 401 function bbp_is_user_favorite( $user_id = 0, $topic_id = 0 ) { 271 272 402 $user_id = bbp_get_user_id( $user_id, true, true ); 273 403 if ( empty( $user_id ) ) { 274 404 return false; 275 405 } 276 406 277 $retval = false;407 $retval = false; 278 408 $favorites = bbp_get_user_favorites_topic_ids( $user_id ); 279 409 280 410 if ( ! empty( $favorites ) ) { … … 295 425 296 426 // Is topic_id in the user's favorites 297 427 if ( ! empty( $topic_id ) ) { 298 $retval = in_array( $topic_id, $favorites);428 $retval = bbp_object_has_user_term( $topic_id, $user_id, '_bbp_favorite' ); 299 429 } 300 430 } 301 431 … … 309 439 * 310 440 * @param int $user_id Optional. User id 311 441 * @param int $topic_id Optional. Topic id 312 * @uses bbp_get_user_favorites_topic_ids() To get the user favorites 313 * @uses update_user_option() To update the user favorites 442 * @uses bbp_is_user_favorite() To check if the topic is a user favorite 314 443 * @uses do_action() Calls 'bbp_add_user_favorite' with the user id and topic id 315 444 * @return bool Always true 316 445 */ … … 324 453 return false; 325 454 } 326 455 327 $favorites = bbp_get_user_favorites_topic_ids( $user_id ); 328 if ( ! in_array( $topic_id, $favorites ) ) { 329 $favorites[] = $topic_id; 330 $favorites = implode( ',', wp_parse_id_list( array_filter( $favorites ) ) ); 331 update_user_option( $user_id, '_bbp_favorites', $favorites ); 332 333 // Purge cache 334 wp_cache_delete( 'bbp_get_topic_favoriters_' . $topic_id, 'bbpress_users' ); 456 if ( ! bbp_is_user_favorite( $user_id, $topic_id ) ) { 457 bbp_add_object_user_term( $topic_id, $user_id, '_bbp_favorite' ); 335 458 } 336 459 337 460 do_action( 'bbp_add_user_favorite', $user_id, $topic_id ); … … 346 469 * 347 470 * @param int $user_id Optional. User id 348 471 * @param int $topic_id Optional. Topic id 349 * @uses bbp_get_user_favorites_topic_ids() To get the user favorites 350 * @uses update_user_option() To update the user favorites 351 * @uses delete_user_option() To delete the user favorites meta 472 * @uses bbp_is_user_favorite() To check if the topic is a user favorite 352 473 * @uses do_action() Calls 'bbp_remove_user_favorite' with the user & topic id 353 474 * @return bool True if the topic was removed from user's favorites, otherwise 354 475 * false … … 358 479 return false; 359 480 } 360 481 361 $favorites = (array) bbp_get_user_favorites_topic_ids( $user_id ); 362 if ( empty( $favorites ) ) { 482 if ( ! bbp_is_user_favorite( $user_id, $topic_id ) ) { 363 483 return false; 364 484 } 365 485 366 $pos = array_search( $topic_id, $favorites ); 367 if ( is_numeric( $pos ) ) { 368 array_splice( $favorites, $pos, 1 ); 369 $favorites = array_filter( $favorites ); 370 371 if ( ! empty( $favorites ) ) { 372 $favorites = implode( ',', wp_parse_id_list( $favorites ) ); 373 update_user_option( $user_id, '_bbp_favorites', $favorites ); 374 } else { 375 delete_user_option( $user_id, '_bbp_favorites' ); 376 } 377 378 // Purge cache 379 wp_cache_delete( 'bbp_get_topic_favoriters_' . $topic_id, 'bbpress_users' ); 486 if ( ! bbp_remove_object_user_term( $topic_id, $user_id, '_bbp_favorite' ) ) { 487 return false; 380 488 } 381 489 382 490 do_action( 'bbp_remove_user_favorite', $user_id, $topic_id ); … … 494 602 * @since 2.5.0 bbPress (r5156) 495 603 * 496 604 * @param int $forum_id Optional. forum id 497 * @uses wpdb::get_col() To execute our query and get the column back605 * @uses bbp_get_user_terms_by_object() To get the forum subscribers 498 606 * @uses apply_filters() Calls 'bbp_get_forum_subscribers' with the subscribers 499 607 * @return array|bool Results if the forum has any subscribers, otherwise false 500 608 */ … … 504 612 return; 505 613 } 506 614 507 $bbp_db = bbp_db(); 508 $key = $bbp_db->prefix . '_bbp_forum_subscriptions'; 509 $users = wp_cache_get( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' ); 510 if ( false === $users ) { 511 $users = $bbp_db->get_col( "SELECT user_id FROM {$bbp_db->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$forum_id}', meta_value) > 0" ); 512 wp_cache_set( 'bbp_get_forum_subscribers_' . $forum_id, $users, 'bbpress_users' ); 513 } 615 $users = bbp_get_user_terms_by_object( $forum_id, '_bbp_subscription' ); 514 616 515 617 return apply_filters( 'bbp_get_forum_subscribers', $users, $forum_id ); 516 618 } … … 521 623 * @since 2.0.0 bbPress (r2668) 522 624 * 523 625 * @param int $topic_id Optional. Topic id 524 * @uses wpdb::get_col() To execute our query and get the column back626 * @uses bbp_get_user_terms_by_objet() To get the topic subscribers 525 627 * @uses apply_filters() Calls 'bbp_get_topic_subscribers' with the subscribers 526 628 * @return array|bool Results if the topic has any subscribers, otherwise false 527 629 */ … … 531 633 return; 532 634 } 533 635 534 $bbp_db = bbp_db(); 535 $key = $bbp_db->prefix . '_bbp_subscriptions'; 536 $users = wp_cache_get( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' ); 537 if ( false === $users ) { 538 $users = $bbp_db->get_col( "SELECT user_id FROM {$bbp_db->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$topic_id}', meta_value) > 0" ); 539 wp_cache_set( 'bbp_get_topic_subscribers_' . $topic_id, $users, 'bbpress_users' ); 540 } 636 $users = bbp_get_user_terms_by_object( $topic_id, '_bbp_subscription' ); 541 637 542 638 return apply_filters( 'bbp_get_topic_subscribers', $users, $topic_id ); 543 639 } … … 628 724 * 629 725 * @param int $user_id Optional. User id 630 726 * @uses bbp_get_user_id() To get the user id 631 * @uses get_user_option() To get the user's subscriptions 727 * @uses bbp_get_objects_by_user_term() To get the user's subscriptions 728 * @uses bbp_get_forum_post_type() To get the forum post type 632 729 * @uses apply_filters() Calls 'bbp_get_user_subscribed_forum_ids' with 633 730 * the subscriptions and user id 634 731 * @return array|bool Results if user has subscriptions, otherwise false … … 639 736 return false; 640 737 } 641 738 642 $subscriptions = get_user_option( '_bbp_forum_subscriptions', $user_id);643 $subscriptions = array_ filter( wp_parse_id_list( $subscriptions ));739 $subscriptions = bbp_get_objects_by_user_term( $user_id, '_bbp_subscription', bbp_get_forum_post_type() ); 740 $subscriptions = array_map( function ( $o ) { return $o->ID; }, $subscriptions); 644 741 645 742 return (array) apply_filters( 'bbp_get_user_subscribed_forum_ids', $subscriptions, $user_id ); 646 743 } … … 652 749 * 653 750 * @param int $user_id Optional. User id 654 751 * @uses bbp_get_user_id() To get the user id 655 * @uses get_user_option() To get the user's subscriptions 752 * @uses bbp_get_objects_by_user_term() To get the user's subscriptions 753 * @uses bbp_get_topic_post_type() To get the topic post type 656 754 * @uses apply_filters() Calls 'bbp_get_user_subscribed_topic_ids' with 657 755 * the subscriptions and user id 658 756 * @return array|bool Results if user has subscriptions, otherwise false … … 663 761 return false; 664 762 } 665 763 666 $subscriptions = get_user_option( '_bbp_subscriptions', $user_id);667 $subscriptions = array_ filter( wp_parse_id_list( $subscriptions ));764 $subscriptions = bbp_get_objects_by_user_term( $user_id, '_bbp_subscription', bbp_get_topic_post_type() ); 765 $subscriptions = array_map( function ( $o ) { return $o->ID; }, $subscriptions); 668 766 669 767 return (array) apply_filters( 'bbp_get_user_subscribed_topic_ids', $subscriptions, $user_id ); 670 768 } … … 706 804 707 805 // Forum 708 806 case bbp_get_forum_post_type() : 709 $subscribed_ids = bbp_get_user_subscribed_forum_ids( $user_id);710 $retval = bbp_is_user_subscribed_to_forum( $user_id, $object_id, $subscribed_ids);807 $subscribed_ids = bbp_get_user_subscribed_forum_ids(); 808 $retval = bbp_object_has_user_term( $object_id, $user_id, '_bbp_subscription' ); 711 809 break; 712 810 713 811 // Topic (default) 714 812 case bbp_get_topic_post_type() : 715 813 default : 716 $subscribed_ids = bbp_get_user_subscribed_topic_ids( $user_id);717 $retval = bbp_is_user_subscribed_to_topic( $user_id, $object_id, $subscribed_ids);814 $subscribed_ids = bbp_get_user_subscribed_topic_ids(); 815 $retval = bbp_object_has_user_term( $object_id, $user_id, '_bbp_subscription' ); 718 816 break; 719 817 } 720 818 } … … 732 830 * @param int $forum_id Optional. Topic id 733 831 * @param array $subscribed_ids Optional. Array of forum ID's to check 734 832 * @uses bbp_get_user_id() To get the user id 735 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions736 833 * @uses bbp_get_forum() To get the forum 737 834 * @uses bbp_get_forum_id() To get the forum id 835 * @uses bbp_object_has_user_term() To check if the user has a subscription 738 836 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id, 739 837 * forum id and subsriptions 740 838 * @return bool True if the forum is in user's subscriptions, otherwise false … … 770 868 $forum_id = get_the_ID(); 771 869 } 772 870 773 // Is forum_id in the user's favorites871 // Is forum_id in the user's subscriptions 774 872 if ( ! empty( $forum_id ) ) { 775 $retval = in_array( $forum_id, $subscribed_ids);873 $retval = bbp_object_has_user_term( $forum_id, $user_id, '_bbp_subscription' ); 776 874 } 777 875 } 778 876 } … … 789 887 * @param int $topic_id Optional. Topic id 790 888 * @param array $subscribed_ids Optional. Array of topic ID's to check 791 889 * @uses bbp_get_user_id() To get the user id 792 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions793 890 * @uses bbp_get_topic() To get the topic 794 891 * @uses bbp_get_topic_id() To get the topic id 892 * @uses bbp_object_has_user_term() To check if the user is subscribed 795 893 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id, 796 894 * topic id and subsriptions 797 895 * @return bool True if the topic is in user's subscriptions, otherwise false … … 827 925 $topic_id = get_the_ID(); 828 926 } 829 927 830 // Is topic_id in the user's favorites928 // Is topic_id in the user's subscriptions 831 929 if ( ! empty( $topic_id ) ) { 832 $retval = in_array( $topic_id, $subscribed_ids);930 $retval = bbp_object_has_user_term( $topic_id, $user_id, '_bbp_subscription' ); 833 931 } 834 932 } 835 933 } … … 838 936 } 839 937 840 938 /** 841 * Add a topic to user's subscriptions939 * Add a user subscription 842 940 * 843 941 * @since 2.5.0 bbPress (r5156) 844 942 * 845 943 * @param int $user_id Optional. User id 846 944 * @param int $object_id Optional. Topic id 847 945 * @uses get_post() To get the post object 848 * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions 849 * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions 850 * @uses bbp_get_forum_post_type() To get the forum post type 851 * @uses bbp_get_topic_post_type() To get the topic post type 852 * @uses update_user_option() To update the user's subscriptions 853 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id 946 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & object id 854 947 * @return bool Always true 855 948 */ 856 949 function bbp_add_user_subscription( $user_id = 0, $object_id = 0 ) { … … 864 957 return false; 865 958 } 866 959 867 switch( $post_type ) { 868 869 // Forum 870 case bbp_get_forum_post_type() : 871 bbp_add_user_forum_subscription( $user_id, $object_id ); 872 break; 873 874 // Topic 875 case bbp_get_topic_post_type() : 876 default : 877 bbp_add_user_topic_subscription( $user_id, $object_id ); 878 break; 960 if ( ! bbp_is_user_subscribed( $user_id, $object_id ) ) { 961 bbp_add_object_user_term( $object_id, $user_id, '_bbp_subscription' ); 879 962 } 880 963 881 964 do_action( 'bbp_add_user_subscription', $user_id, $object_id, $post_type ); … … 890 973 * 891 974 * @param int $user_id Optional. User id 892 975 * @param int $forum_id Optional. forum id 893 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions894 976 * @uses bbp_get_forum() To get the forum 895 * @uses update_user_option() To update the user's subscriptions977 * @uses bbp_add_user_subscription() To add the user subscription 896 978 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & forum id 897 979 * @return bool Always true 898 980 */ … … 906 988 return false; 907 989 } 908 990 909 $subscriptions = (array) bbp_get_user_subscribed_forum_ids( $user_id ); 910 if ( ! in_array( $forum_id, $subscriptions ) ) { 911 $subscriptions[] = $forum_id; 912 $subscriptions = implode( ',', wp_parse_id_list( array_filter( $subscriptions ) ) ); 913 update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions ); 991 bbp_add_user_subscription( $user_id, $forum_id ); 914 992 915 wp_cache_delete( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' );916 }917 918 993 do_action( 'bbp_add_user_forum_subscription', $user_id, $forum_id ); 919 994 920 995 return true; … … 927 1002 * 928 1003 * @param int $user_id Optional. User id 929 1004 * @param int $topic_id Optional. Topic id 930 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions931 1005 * @uses bbp_get_topic() To get the topic 932 * @uses update_user_option() To update the user's subscriptions1006 * @uses bbp_add_user_subscription() To add the subscription 933 1007 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id 934 1008 * @return bool Always true 935 1009 */ … … 943 1017 return false; 944 1018 } 945 1019 946 $subscriptions = (array) bbp_get_user_subscribed_topic_ids( $user_id ); 947 if ( ! in_array( $topic_id, $subscriptions ) ) { 948 $subscriptions[] = $topic_id; 949 $subscriptions = implode( ',', wp_parse_id_list( array_filter( $subscriptions ) ) ); 950 update_user_option( $user_id, '_bbp_subscriptions', $subscriptions ); 1020 bbp_add_user_subscription( $user_id, $topic_id ); 951 1021 952 wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' );953 }954 955 1022 do_action( 'bbp_add_user_topic_subscription', $user_id, $topic_id ); 956 1023 957 1024 return true; 958 1025 } 959 1026 960 1027 /** 961 * Remove a topic from user's subscriptions1028 * Remove a user subscription 962 1029 * 963 1030 * @since 2.0.0 bbPress (r2668) 964 1031 * 965 1032 * @param int $user_id Optional. User id 966 1033 * @param int $object_id Optional. Topic id 967 1034 * @uses get_post() To get the post object 968 * @uses bbp_get_forum_post_type() To get the forum post type 969 * @uses bbp_get_topic_post_type() To get the topic post type 970 * @uses bbp_remove_user_forum_subscription() To remove the user's subscription 971 * @uses bbp_remove_user_topic_subscription() To remove the user's subscription 1035 * @uses bbp_is_user_subscribed() To check if the user is already subscribed 972 1036 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and 973 1037 * topic id 974 1038 * @return bool True if the topic was removed from user's subscriptions, … … 984 1048 return false; 985 1049 } 986 1050 987 switch( $post_type ) { 988 989 // Forum 990 case bbp_get_forum_post_type() : 991 bbp_remove_user_forum_subscription( $user_id, $object_id ); 992 break; 993 994 // Topic 995 case bbp_get_topic_post_type() : 996 default : 997 bbp_remove_user_topic_subscription( $user_id, $object_id ); 998 break; 1051 if ( bbp_is_user_subscribed( $user_id, $object_id ) ) { 1052 bbp_remove_object_user_term( $object_id, $user_id, '_bbp_subscription' ); 999 1053 } 1000 1054 1001 1055 do_action( 'bbp_remove_user_subscription', $user_id, $object_id, $post_type ); … … 1010 1064 * 1011 1065 * @param int $user_id Optional. User id 1012 1066 * @param int $forum_id Optional. forum id 1013 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions 1014 * @uses update_user_option() To update the user's subscriptions 1015 * @uses delete_user_option() To delete the user's subscriptions meta 1067 * @uses bbp_remove_user_subscription() To remove the subscription 1016 1068 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and 1017 1069 * forum id 1018 1070 * @return bool True if the forum was removed from user's subscriptions, … … 1023 1075 return false; 1024 1076 } 1025 1077 1026 $subscriptions = (array) bbp_get_user_subscribed_forum_ids( $user_id ); 1027 if ( empty( $subscriptions ) ) { 1028 return false; 1029 } 1078 bbp_remove_user_subscription( $user_id, $forum_id ); 1030 1079 1031 $pos = array_search( $forum_id, $subscriptions );1032 if ( false === $pos ) {1033 return false;1034 }1035 1036 array_splice( $subscriptions, $pos, 1 );1037 $subscriptions = array_filter( $subscriptions );1038 1039 if ( ! empty( $subscriptions ) ) {1040 $subscriptions = implode( ',', wp_parse_id_list( $subscriptions ) );1041 update_user_option( $user_id, '_bbp_forum_subscriptions', $subscriptions );1042 } else {1043 delete_user_option( $user_id, '_bbp_forum_subscriptions' );1044 }1045 1046 wp_cache_delete( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' );1047 1048 1080 do_action( 'bbp_remove_user_forum_subscription', $user_id, $forum_id ); 1049 1081 1050 1082 return true; … … 1057 1089 * 1058 1090 * @param int $user_id Optional. User id 1059 1091 * @param int $topic_id Optional. Topic id 1060 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions 1061 * @uses update_user_option() To update the user's subscriptions 1062 * @uses delete_user_option() To delete the user's subscriptions meta 1092 * @uses bbp_remove_user_subscription() To remove the subscription 1063 1093 * @uses do_action() Calls 'bbp_remove_user_topic_subscription' with the user id and 1064 1094 * topic id 1065 1095 * @return bool True if the topic was removed from user's subscriptions, … … 1070 1100 return false; 1071 1101 } 1072 1102 1073 $subscriptions = (array) bbp_get_user_subscribed_topic_ids( $user_id ); 1074 if ( empty( $subscriptions ) ) { 1075 return false; 1076 } 1103 bbp_remove_user_subscription( $user_id, $topic_id ); 1077 1104 1078 $pos = array_search( $topic_id, $subscriptions );1079 if ( false === $pos ) {1080 return false;1081 }1082 1083 array_splice( $subscriptions, $pos, 1 );1084 $subscriptions = array_filter( $subscriptions );1085 1086 if ( ! empty( $subscriptions ) ) {1087 $subscriptions = implode( ',', wp_parse_id_list( $subscriptions ) );1088 update_user_option( $user_id, '_bbp_subscriptions', $subscriptions );1089 } else {1090 delete_user_option( $user_id, '_bbp_subscriptions' );1091 }1092 1093 wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' );1094 1095 1105 do_action( 'bbp_remove_user_topic_subscription', $user_id, $topic_id ); 1096 1106 1097 1107 return true; … … 1154 1164 1155 1165 // Check current user's ability to edit the user 1156 1166 } elseif ( ! current_user_can( 'edit_user', $user_id ) ) { 1157 bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) );1167 bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit subscriptions of that user!', 'bbpress' ) ); 1158 1168 } 1159 1169 1160 1170 // Bail if we have errors … … 1259 1269 1260 1270 // Check current user's ability to edit the user 1261 1271 } elseif ( ! current_user_can( 'edit_user', $user_id ) ) { 1262 bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) );1272 bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit subscriptions of that user!', 'bbpress' ) ); 1263 1273 } 1264 1274 1265 1275 // Bail if we have errors -
tests/phpunit/testcases/users/functions/favorites.php
119 119 $t = $this->factory->topic->create_many( 3 ); 120 120 121 121 // Add topic favorites. 122 update_user_option( $u, '_bbp_favorites', $t[0]);122 add_post_meta( $t[0], '_bbp_favorite', $u, false ); 123 123 124 124 // Add user favorite. 125 125 bbp_add_user_favorite( $u, $t[1] ); … … 144 144 $t = $this->factory->topic->create_many( 3 ); 145 145 146 146 // Add topic favorites. 147 update_user_option( $u, '_bbp_favorites', implode( ',', $t ) ); 147 foreach ( $t as $topic ) { 148 add_post_meta( $topic, '_bbp_favorite', $u ); 149 } 148 150 149 151 // Remove user favorite. 150 152 bbp_remove_user_favorite( $u, $t[2] ); -
tests/phpunit/testcases/users/functions/functions.php
86 86 } 87 87 88 88 /** 89 * @covers ::bbp_object_has_user_term 90 */ 91 public function test_bbp_object_has_user_term() { 92 $u = $this->factory->user->create(); 93 $t = $this->factory->topic->create(); 94 95 $r = bbp_object_has_user_term( $t, $u, $this->user_taxonomy ); 96 97 $this->assertFalse( $r ); 98 99 // Add user term. 100 add_post_meta( $t, $this->user_taxonomy, $u, false ); 101 102 $r = bbp_object_has_user_term( $t, $u, $this->user_taxonomy ); 103 104 $this->assertTrue( $r ); 105 } 106 107 /** 108 * @covers ::bbp_add_object_user_term 109 */ 110 public function test_bbp_add_object_user_term() { 111 $u = $this->factory->user->create_many( 3 ); 112 $t = $this->factory->topic->create(); 113 114 // Add object terms. 115 foreach ( $u as $k => $v ) { 116 bbp_add_object_user_term( $t, $v, $this->user_taxonomy ); 117 } 118 119 $r = get_post_meta( $t, $this->user_taxonomy, false ); 120 121 $this->assertCount( 3, $r ); 122 } 123 124 /** 125 * @covers ::bbp_remove_object_user_term 126 */ 127 public function test_bbp_remove_object_user_term() { 128 $u = $this->factory->user->create(); 129 $t = $this->factory->topic->create(); 130 131 // Add object terms. 132 add_post_meta( $t, $this->user_taxonomy, $u, false ); 133 134 $r = get_post_meta( $t, $this->user_taxonomy, false ); 135 136 $this->assertCount( 1, $r ); 137 138 $r = bbp_remove_object_user_term( $t, $u, $this->user_taxonomy ); 139 140 $this->assertTrue( $r ); 141 142 $r = get_post_meta( $t, $this->user_taxonomy, false ); 143 144 $this->assertCount( 0, $r ); 145 } 146 147 /** 148 * @covers ::bbp_get_objects_by_user_term 149 */ 150 public function test_bbp_get_objects_by_user_term() { 151 $u = $this->factory->user->create(); 152 $t = $this->factory->topic->create_many( 3 ); 153 154 // Add object terms. 155 foreach ( $t as $k => $v ) { 156 add_post_meta( $v, $this->user_taxonomy, $u, false ); 157 } 158 159 $r = bbp_get_objects_by_user_term( $u, $this->user_taxonomy, 'topic' ); 160 161 $this->assertCount( 3, $r ); 162 } 163 164 /** 165 * @covers ::bbp_get_user_terms_by_object 166 */ 167 public function test_bbp_get_user_terms_by_object() { 168 $u = $this->factory->user->create_many( 3 ); 169 $t = $this->factory->topic->create(); 170 171 // Add object terms. 172 foreach ( $u as $k => $v ) { 173 add_post_meta( $t, $this->user_taxonomy, $u, false ); 174 } 175 176 $r = bbp_get_user_terms_by_object( $t, $this->user_taxonomy ); 177 178 $this->assertCount( 3, $r ); 179 } 180 181 /** 89 182 * @covers ::bbp_edit_user_handler 90 183 * @todo Implement test_bbp_edit_user_handler(). 91 184 */ … … 172 265 'This test has not been implemented yet.' 173 266 ); 174 267 } 268 269 public function setUp() { 270 parent::setUp(); 271 272 $this->user_taxonomy = '_bbp_test_user_taxonomy'; 273 } 175 274 }