Changeset 6109
- Timestamp:
- 11/01/2016 07:24:50 AM (4 years ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/includes/users/capabilities.php
r6056 r6109 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_user_to_object( $object_id, $user_id, '_bbp_moderator_id' ); 815 815 } 816 816 … … 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_user_from_object( $object_id, $user_id, '_bbp_moderator_id' ); 829 829 } 830 830 … … 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_users_for_object( $object_id, '_bbp_moderator_id' ); 842 842 } 843 843 -
trunk/src/includes/users/functions.php
r6056 r6109 170 170 } 171 171 172 /** User Relationships ********************************************************/ 173 174 /** 175 * Set a user id 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 $meta_key The relationship key 182 * @param string $meta_type The relationship type 183 * 184 * @uses bbp_is_object_of_user() To check if the term has already been set 185 * @uses add_post_meta() To set the term on the object 186 * @uses apply_filters() Calls 'bbp_add_user_to_object' with the object id, user 187 * id, and taxonomy 188 * @return bool Returns true if the user taxonomy term is added to the object, 189 * otherwise false 190 */ 191 function bbp_add_user_to_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) { 192 $retval = add_metadata( $meta_type, $object_id, $meta_key, $user_id, false ); 193 194 return (bool) apply_filters( 'bbp_add_user_to_object', (bool) $retval, $object_id, $user_id, $meta_key, $meta_type ); 195 } 196 197 /** 198 * Remove a user id from an object 199 * 200 * @since 2.7 bbPress() 201 * 202 * @param int $object_id The post id 203 * @param int $user_id The user id 204 * @param string $meta_key The relationship key 205 * @param string $meta_type The relationship type 206 * 207 * @uses bbp_is_object_of_user() To check if the term is set 208 * @uses delete_post_meta() To remove the term from the object 209 * @uses apply_filters() Calls 'bbp_remove_user_from_object' with the object 210 * id, user id, and taxonomy 211 * @return bool Returns true is the user taxonomy term is removed from the object, 212 * otherwise false 213 */ 214 function bbp_remove_user_from_object( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) { 215 $retval = delete_metadata( $meta_type, $object_id, $meta_key, $user_id, false ); 216 217 return (bool) apply_filters( 'bbp_remove_user_from_object', (bool) $retval, $object_id, $user_id, $meta_key, $meta_type ); 218 } 219 220 /** 221 * Get user taxonomy terms for an object 222 * 223 * @since 2.7 bbPress() 224 * 225 * @param int $object_id The object id 226 * @param string $meta_key The key used to index this relationship 227 * @param string $meta_type The type of meta to look in 228 * 229 * @uses get_post_meta() To get the user taxonomy terms 230 * @uses apply_filters() Calls 'bbp_get_users_for_object' with the user 231 * taxonomy terms, object id, and taxonomy 232 * @return array Returns the user taxonomy terms of the object 233 */ 234 function bbp_get_users_for_object( $object_id = 0, $meta_key = '', $meta_type = 'post' ) { 235 $meta = get_metadata( $meta_type, $object_id, $meta_key, false ); 236 $retval = wp_parse_id_list( $meta ); 237 238 return (array) apply_filters( 'bbp_get_users_for_object', (array) $retval, $object_id, $meta_key, $meta_type ); 239 } 240 241 /** 242 * Check if the user id is set on an object 243 * 244 * @since 2.7 bbPress() 245 * 246 * @param int $object_id The object id 247 * @param int $user_id The user id 248 * @param string $meta_key The relationship key 249 * @param string $meta_type The relationship type 250 * 251 * @uses get_post_meta() To check if the user id is set on the object 252 * @uses apply_filters() Calls 'bbp_is_object_of_user' with the object id, 253 * user id, and taxonomy 254 * @return bool Returns true if the user id is set on the object for the 255 * taxonomy, otherwise false 256 */ 257 function bbp_is_object_of_user( $object_id = 0, $user_id = 0, $meta_key = '', $meta_type = 'post' ) { 258 $user_ids = bbp_get_users_for_object( $object_id, $meta_key, $meta_type ); 259 $retval = is_numeric( array_search( $user_id, $user_ids, true ) ); 260 261 return (bool) apply_filters( 'bbp_is_object_of_user', (bool) $retval, $object_id, $user_id, $meta_key, $meta_type ); 262 } 263 172 264 /** Favorites *****************************************************************/ 173 265 … … 185 277 function bbp_get_topic_favoriters( $topic_id = 0 ) { 186 278 $topic_id = bbp_get_topic_id( $topic_id ); 187 if ( empty( $topic_id ) ) { 188 return; 189 } 190 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 } 198 199 return apply_filters( 'bbp_get_topic_favoriters', $users, $topic_id ); 279 $users = bbp_get_users_for_object( $topic_id, '_bbp_favorite' ); 280 281 return (array) apply_filters( 'bbp_get_topic_favoriters', $users, $topic_id ); 200 282 } 201 283 … … 213 295 */ 214 296 function bbp_get_user_favorites( $user_id = 0 ) { 215 $user_id = bbp_get_user_id( $user_id ); 216 if ( empty( $user_id ) ) { 217 return false; 218 } 219 220 // If user has favorites, load them 297 $user_id = bbp_get_user_id( $user_id ); 221 298 $favorites = bbp_get_user_favorites_topic_ids( $user_id ); 222 if ( ! empty( $favorites ) ) { 223 $query = bbp_has_topics( array( 'post__in' => $favorites ) ); 224 } else { 225 $query = false; 226 } 299 $query = ! empty( $favorites ) 300 ? bbp_has_topics( array( 'post__in' => $favorites ) ) 301 : false; 227 302 228 303 return apply_filters( 'bbp_get_user_favorites', $query, $user_id, $favorites ); … … 230 305 231 306 /** 232 * Get a user's favorite topic s'ids307 * Get a user's favorite topic ids 233 308 * 234 309 * @since 2.0.0 bbPress (r2652) … … 236 311 * @param int $user_id Optional. User id 237 312 * @uses bbp_get_user_id() To get the user id 238 * @uses get_user_option() To get the user favorites313 * @uses bbp_get_topic_post_type() To get the topic post type 239 314 * @uses apply_filters() Calls 'bbp_get_user_favorites_topic_ids' with 240 315 * the favorites and user id … … 242 317 */ 243 318 function bbp_get_user_favorites_topic_ids( $user_id = 0 ) { 244 $user_id = bbp_get_user_id( $user_id ); 245 if ( empty( $user_id ) ) { 246 return false; 247 } 248 249 $favorites = get_user_option( '_bbp_favorites', $user_id ); 250 $favorites = array_filter( wp_parse_id_list( $favorites ) ); 251 252 return (array) apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites, $user_id ); 319 $user_id = bbp_get_user_id( $user_id ); 320 $favorites = new WP_Query( array( 321 'fields' => 'ids', 322 'post_type' => bbp_get_topic_post_type(), 323 'meta_query' => array( array( 324 'key' => '_bbp_favorite', 325 'value' => $user_id, 326 'compare' => 'NUMERIC' 327 ) ) 328 ) ); 329 330 return (array) apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites->posts, $user_id ); 253 331 } 254 332 … … 264 342 * @uses bbp_get_topic() To get the topic 265 343 * @uses bbp_get_topic_id() To get the topic id 344 * @uses bbp_is_object_of_user() To check if the user has a favorite 266 345 * @uses apply_filters() Calls 'bbp_is_user_favorite' with the bool, user id, 267 346 * topic id and favorites … … 269 348 */ 270 349 function bbp_is_user_favorite( $user_id = 0, $topic_id = 0 ) { 271 272 $user_id = bbp_get_user_id( $user_id, true, true );273 if ( empty( $user_id ) ) {274 return false;275 }276 277 350 $retval = false; 351 $user_id = bbp_get_user_id( $user_id, true, true ); 278 352 $favorites = bbp_get_user_favorites_topic_ids( $user_id ); 279 353 … … 296 370 // Is topic_id in the user's favorites 297 371 if ( ! empty( $topic_id ) ) { 298 $retval = in_array( $topic_id, $favorites);372 $retval = bbp_is_object_of_user( $topic_id, $user_id, '_bbp_favorite' ); 299 373 } 300 374 } … … 310 384 * @param int $user_id Optional. User id 311 385 * @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 386 * @uses bbp_is_user_favorite() To check if the topic is a user favorite 314 387 * @uses do_action() Calls 'bbp_add_user_favorite' with the user id and topic id 315 388 * @return bool Always true 316 389 */ 317 390 function bbp_add_user_favorite( $user_id = 0, $topic_id = 0 ) { 391 392 // Bail if not enough info 318 393 if ( empty( $user_id ) || empty( $topic_id ) ) { 319 394 return false; 320 395 } 321 396 397 // Bail if to topic 322 398 $topic = bbp_get_topic( $topic_id ); 323 399 if ( empty( $topic ) ) { … … 325 401 } 326 402 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 cache334 wp_cache_delete( 'bbp_get_topic_favoriters_' . $topic_id, 'bbpress_users' );403 // Bail if already a favorite 404 if ( bbp_is_user_favorite( $user_id, $topic_id ) ) { 405 return false; 406 } 407 408 // Bail if add fails 409 if ( ! bbp_add_user_to_object( $topic_id, $user_id, '_bbp_favorite' ) ) { 410 return false; 335 411 } 336 412 … … 347 423 * @param int $user_id Optional. User id 348 424 * @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 425 * @uses bbp_is_user_favorite() To check if the topic is a user favorite 352 426 * @uses do_action() Calls 'bbp_remove_user_favorite' with the user & topic id 353 427 * @return bool True if the topic was removed from user's favorites, otherwise … … 355 429 */ 356 430 function bbp_remove_user_favorite( $user_id, $topic_id ) { 431 432 // Bail if not enough info 357 433 if ( empty( $user_id ) || empty( $topic_id ) ) { 358 434 return false; 359 435 } 360 436 361 $favorites = (array) bbp_get_user_favorites_topic_ids( $user_id ); 362 if ( empty( $favorites ) ) { 363 return false; 364 } 365 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' ); 437 // Bail if not already a favorite 438 if ( ! bbp_is_user_favorite( $user_id, $topic_id ) ) { 439 return false; 440 } 441 442 // Bail if remove fails 443 if ( ! bbp_remove_user_from_object( $topic_id, $user_id, '_bbp_favorite' ) ) { 444 return false; 380 445 } 381 446 … … 426 491 427 492 // What action is taking place? 428 $topic_id 429 $user_id 493 $topic_id = intval( $_GET['topic_id'] ); 494 $user_id = bbp_get_user_id( 0, true, true ); 430 495 431 496 // Check for empty topic … … 495 560 * 496 561 * @param int $forum_id Optional. forum id 497 * @uses wpdb::get_col() To execute our query and get the column back562 * @uses bbp_get_users_for_object() To get the forum subscribers 498 563 * @uses apply_filters() Calls 'bbp_get_forum_subscribers' with the subscribers 499 564 * @return array|bool Results if the forum has any subscribers, otherwise false … … 501 566 function bbp_get_forum_subscribers( $forum_id = 0 ) { 502 567 $forum_id = bbp_get_forum_id( $forum_id ); 503 if ( empty( $forum_id ) ) { 504 return; 505 } 506 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 } 514 515 return apply_filters( 'bbp_get_forum_subscribers', $users, $forum_id ); 568 $users = bbp_get_users_for_object( $forum_id, '_bbp_subscription' ); 569 570 return (array) apply_filters( 'bbp_get_forum_subscribers', $users, $forum_id ); 516 571 } 517 572 … … 522 577 * 523 578 * @param int $topic_id Optional. Topic id 524 * @uses wpdb::get_col() To execute our query and get the column back579 * @uses bbp_get_user_terms_by_object() To get the topic subscribers 525 580 * @uses apply_filters() Calls 'bbp_get_topic_subscribers' with the subscribers 526 581 * @return array|bool Results if the topic has any subscribers, otherwise false … … 528 583 function bbp_get_topic_subscribers( $topic_id = 0 ) { 529 584 $topic_id = bbp_get_topic_id( $topic_id ); 530 if ( empty( $topic_id ) ) { 531 return; 532 } 533 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 } 541 542 return apply_filters( 'bbp_get_topic_subscribers', $users, $topic_id ); 585 $users = bbp_get_users_for_object( $topic_id, '_bbp_subscription' ); 586 587 return (array) apply_filters( 'bbp_get_topic_subscribers', $users, $topic_id ); 543 588 } 544 589 … … 575 620 576 621 // Default to the displayed user 577 $user_id = bbp_get_user_id( $user_id ); 578 if ( empty( $user_id ) ) { 579 return false; 580 } 622 $user_id = bbp_get_user_id( $user_id ); 623 $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id ); 581 624 582 625 // If user has subscriptions, load them 583 $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );584 626 if ( ! empty( $subscriptions ) ) { 585 627 $query = bbp_has_topics( array( 'post__in' => $subscriptions ) ); … … 606 648 607 649 // Default to the displayed user 608 $user_id = bbp_get_user_id( $user_id ); 609 if ( empty( $user_id ) ) { 610 return false; 611 } 650 $user_id = bbp_get_user_id( $user_id ); 651 $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id ); 612 652 613 653 // If user has subscriptions, load them 614 $subscriptions = bbp_get_user_subscribed_forum_ids( $user_id );615 654 if ( ! empty( $subscriptions ) ) { 616 655 $query = bbp_has_forums( array( 'post__in' => $subscriptions ) ); … … 629 668 * @param int $user_id Optional. User id 630 669 * @uses bbp_get_user_id() To get the user id 631 * @uses get_user_option() To get the user's subscriptions670 * @uses bbp_get_forum_post_type() To get the forum post type 632 671 * @uses apply_filters() Calls 'bbp_get_user_subscribed_forum_ids' with 633 672 * the subscriptions and user id … … 635 674 */ 636 675 function bbp_get_user_subscribed_forum_ids( $user_id = 0 ) { 637 $user_id = bbp_get_user_id( $user_id ); 638 if ( empty( $user_id ) ) { 639 return false; 640 } 641 642 $subscriptions = get_user_option( '_bbp_forum_subscriptions', $user_id ); 643 $subscriptions = array_filter( wp_parse_id_list( $subscriptions ) ); 644 645 return (array) apply_filters( 'bbp_get_user_subscribed_forum_ids', $subscriptions, $user_id ); 676 $user_id = bbp_get_user_id( $user_id ); 677 $subscriptions = new WP_Query( array( 678 'fields' => 'ids', 679 'post_type' => bbp_get_forum_post_type(), 680 'meta_query' => array( array( 681 'key' => '_bbp_subscription', 682 'value' => $user_id, 683 'compare' => 'NUMERIC' 684 ) ) 685 ) ); 686 687 return (array) apply_filters( 'bbp_get_user_subscribed_forum_ids', $subscriptions->posts, $user_id ); 646 688 } 647 689 … … 653 695 * @param int $user_id Optional. User id 654 696 * @uses bbp_get_user_id() To get the user id 655 * @uses get_user_option() To get the user's subscriptions697 * @uses bbp_get_topic_post_type() To get the topic post type 656 698 * @uses apply_filters() Calls 'bbp_get_user_subscribed_topic_ids' with 657 699 * the subscriptions and user id … … 659 701 */ 660 702 function bbp_get_user_subscribed_topic_ids( $user_id = 0 ) { 661 $user_id = bbp_get_user_id( $user_id ); 662 if ( empty( $user_id ) ) { 663 return false; 664 } 665 666 $subscriptions = get_user_option( '_bbp_subscriptions', $user_id ); 667 $subscriptions = array_filter( wp_parse_id_list( $subscriptions ) ); 668 669 return (array) apply_filters( 'bbp_get_user_subscribed_topic_ids', $subscriptions, $user_id ); 703 $user_id = bbp_get_user_id( $user_id ); 704 $subscriptions = new WP_Query( array( 705 'fields' => 'ids', 706 'post_type' => bbp_get_topic_post_type(), 707 'meta_query' => array( array( 708 'key' => '_bbp_subscription', 709 'value' => $user_id, 710 'compare' => 'NUMERIC' 711 ) ) 712 ) ); 713 714 return (array) apply_filters( 'bbp_get_user_subscribed_topic_ids', $subscriptions->posts, $user_id ); 670 715 } 671 716 … … 733 778 * @param array $subscribed_ids Optional. Array of forum ID's to check 734 779 * @uses bbp_get_user_id() To get the user id 735 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions736 780 * @uses bbp_get_forum() To get the forum 737 781 * @uses bbp_get_forum_id() To get the forum id 782 * @uses bbp_is_object_of_user() To check if the user has a subscription 738 783 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id, 739 784 * forum id and subsriptions … … 771 816 } 772 817 773 // Is forum_id in the user's favorites818 // Is forum_id in the user's subscriptions 774 819 if ( ! empty( $forum_id ) ) { 775 $retval = in_array( $forum_id, $subscribed_ids);820 $retval = bbp_is_object_of_user( $forum_id, $user_id, '_bbp_subscription' ); 776 821 } 777 822 } … … 790 835 * @param array $subscribed_ids Optional. Array of topic ID's to check 791 836 * @uses bbp_get_user_id() To get the user id 792 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions793 837 * @uses bbp_get_topic() To get the topic 794 838 * @uses bbp_get_topic_id() To get the topic id 839 * @uses bbp_is_object_of_user() To check if the user is subscribed 795 840 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id, 796 841 * topic id and subsriptions … … 828 873 } 829 874 830 // Is topic_id in the user's favorites875 // Is topic_id in the user's subscriptions 831 876 if ( ! empty( $topic_id ) ) { 832 $retval = in_array( $topic_id, $subscribed_ids);877 $retval = bbp_is_object_of_user( $topic_id, $user_id, '_bbp_subscription' ); 833 878 } 834 879 } … … 839 884 840 885 /** 841 * Add a topic to user's subscriptions886 * Add a user subscription 842 887 * 843 888 * @since 2.5.0 bbPress (r5156) … … 846 891 * @param int $object_id Optional. Topic id 847 892 * @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 893 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & object id 854 894 * @return bool Always true 855 895 */ 856 896 function bbp_add_user_subscription( $user_id = 0, $object_id = 0 ) { 897 898 // Bail if not enough info 857 899 if ( empty( $user_id ) || empty( $object_id ) ) { 858 900 return false; … … 865 907 } 866 908 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; 909 // Bail if already subscribed 910 if ( bbp_is_user_subscribed( $user_id, $object_id ) ) { 911 return false; 912 } 913 914 // Bail if add fails 915 if ( ! bbp_add_user_to_object( $object_id, $user_id, '_bbp_subscription' ) ) { 916 return false; 879 917 } 880 918 … … 891 929 * @param int $user_id Optional. User id 892 930 * @param int $forum_id Optional. forum id 893 * @uses bbp_get_user_subscribed_forum_ids() To get the user's subscriptions894 931 * @uses bbp_get_forum() To get the forum 895 * @uses update_user_option() To update the user's subscriptions932 * @uses bbp_add_user_subscription() To add the user subscription 896 933 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & forum id 897 934 * @return bool Always true 898 935 */ 899 936 function bbp_add_user_forum_subscription( $user_id = 0, $forum_id = 0 ) { 937 938 // Bail if not enough info 900 939 if ( empty( $user_id ) || empty( $forum_id ) ) { 901 940 return false; 902 941 } 903 942 943 // Bail if no forum 904 944 $forum = bbp_get_forum( $forum_id ); 905 945 if ( empty( $forum ) ) { … … 907 947 } 908 948 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 ); 914 915 wp_cache_delete( 'bbp_get_forum_subscribers_' . $forum_id, 'bbpress_users' ); 949 // Bail if already subscribed 950 if ( bbp_is_user_subscribed( $user_id, $forum_id ) ) { 951 return false; 952 } 953 954 // Bail if add fails 955 if ( ! bbp_add_user_subscription( $user_id, $forum_id ) ) { 956 return false; 916 957 } 917 958 … … 928 969 * @param int $user_id Optional. User id 929 970 * @param int $topic_id Optional. Topic id 930 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions931 971 * @uses bbp_get_topic() To get the topic 932 * @uses update_user_option() To update the user's subscriptions972 * @uses bbp_add_user_subscription() To add the subscription 933 973 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id 934 974 * @return bool Always true 935 975 */ 936 976 function bbp_add_user_topic_subscription( $user_id = 0, $topic_id = 0 ) { 977 978 // Bail if not enough info 937 979 if ( empty( $user_id ) || empty( $topic_id ) ) { 938 980 return false; 939 981 } 940 982 983 // Bail if no topic 941 984 $topic = bbp_get_topic( $topic_id ); 942 985 if ( empty( $topic ) ) { … … 944 987 } 945 988 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 ); 951 952 wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress_users' ); 989 // Bail if already subscribed 990 if ( bbp_is_user_subscribed_to_topic( $user_id, $topic_id ) ) { 991 return false; 992 } 993 994 // Bail if add fails 995 if ( ! bbp_add_user_subscription( $user_id, $topic_id ) ) { 996 return false; 953 997 } 954 998 … … 959 1003 960 1004 /** 961 * Remove a topic from user's subscriptions1005 * Remove a user subscription 962 1006 * 963 1007 * @since 2.0.0 bbPress (r2668) … … 966 1010 * @param int $object_id Optional. Topic id 967 1011 * @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 1012 * @uses bbp_is_user_subscribed() To check if the user is already subscribed 972 1013 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and 973 1014 * topic id … … 976 1017 */ 977 1018 function bbp_remove_user_subscription( $user_id = 0, $object_id = 0 ) { 1019 1020 // Bail if not enough info 978 1021 if ( empty( $user_id ) || empty( $object_id ) ) { 979 1022 return false; 980 1023 } 981 1024 1025 // Get post type 982 1026 $post_type = get_post_type( $object_id ); 983 1027 if ( empty( $post_type ) ) { … … 985 1029 } 986 1030 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; 1031 // Bail if not subscribed 1032 if ( ! bbp_is_user_subscribed( $user_id, $object_id ) ) { 1033 return false; 1034 } 1035 1036 // Bail if remove fails 1037 if ( ! bbp_remove_user_from_object( $object_id, $user_id, '_bbp_subscription' ) ) { 1038 return false; 999 1039 } 1000 1040 … … 1011 1051 * @param int $user_id Optional. User id 1012 1052 * @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 1053 * @uses bbp_remove_user_subscription() To remove the subscription 1016 1054 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and 1017 1055 * forum id … … 1019 1057 * otherwise false 1020 1058 */ 1021 function bbp_remove_user_forum_subscription( $user_id, $forum_id ) { 1059 function bbp_remove_user_forum_subscription( $user_id = 0, $forum_id = 0 ) { 1060 1061 // Bail if not enough info 1022 1062 if ( empty( $user_id ) || empty( $forum_id ) ) { 1023 1063 return false; 1024 1064 } 1025 1065 1026 $subscriptions = (array) bbp_get_user_subscribed_forum_ids( $user_id ); 1027 if ( empty( $subscriptions ) ) { 1028 return false; 1029 } 1030 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' ); 1066 // Bail if remove fails 1067 if ( ! bbp_remove_user_subscription( $user_id, $forum_id ) ) { 1068 return false; 1069 } 1047 1070 1048 1071 do_action( 'bbp_remove_user_forum_subscription', $user_id, $forum_id ); … … 1058 1081 * @param int $user_id Optional. User id 1059 1082 * @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 1083 * @uses bbp_remove_user_subscription() To remove the subscription 1063 1084 * @uses do_action() Calls 'bbp_remove_user_topic_subscription' with the user id and 1064 1085 * topic id … … 1066 1087 * otherwise false 1067 1088 */ 1068 function bbp_remove_user_topic_subscription( $user_id, $topic_id ) { 1089 function bbp_remove_user_topic_subscription( $user_id = 0, $topic_id = 0 ) { 1090 1091 // Bail if not enough info 1069 1092 if ( empty( $user_id ) || empty( $topic_id ) ) { 1070 1093 return false; 1071 1094 } 1072 1095 1073 $subscriptions = (array) bbp_get_user_subscribed_topic_ids( $user_id ); 1074 if ( empty( $subscriptions ) ) { 1075 return false; 1076 } 1077 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' ); 1096 // Bail if remove fails 1097 if ( ! bbp_remove_user_subscription( $user_id, $topic_id ) ) { 1098 return false; 1099 } 1094 1100 1095 1101 do_action( 'bbp_remove_user_topic_subscription', $user_id, $topic_id ); -
trunk/tests/phpunit/testcases/users/functions/favorites.php
r5928 r6109 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. … … 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. -
trunk/tests/phpunit/testcases/users/functions/functions.php
r5755 r6109 87 87 88 88 /** 89 * @covers ::bbp_add_user_to_object 90 */ 91 public function test_bbp_add_user_to_object() { 92 $u = $this->factory->user->create_many( 3 ); 93 $t = $this->factory->topic->create(); 94 95 // Add object terms. 96 foreach ( $u as $k => $v ) { 97 bbp_add_user_to_object( $t, $v, '_bbp_moderator' ); 98 } 99 100 $r = get_metadata( 'post', $t, '_bbp_moderator', false ); 101 102 $this->assertCount( 3, $r ); 103 } 104 105 /** 106 * @covers ::bbp_remove_user_from_object 107 */ 108 public function test_bbp_remove_user_from_object() { 109 $u = $this->factory->user->create(); 110 $t = $this->factory->topic->create(); 111 112 // Add object terms. 113 add_metadata( 'post', $t, '_bbp_moderator', $u, false ); 114 115 $r = get_metadata( 'post', $t, '_bbp_moderator', false ); 116 117 $this->assertCount( 1, $r ); 118 119 $r = bbp_remove_user_from_object( $t, $u, '_bbp_moderator' ); 120 121 $this->assertTrue( $r ); 122 123 $r = get_metadata( 'post', $t, '_bbp_moderator', false ); 124 125 $this->assertCount( 0, $r ); 126 } 127 128 /** 129 * @covers ::bbp_is_object_of_user 130 */ 131 public function test_bbp_is_object_of_user() { 132 $u = $this->factory->user->create(); 133 $t = $this->factory->topic->create(); 134 135 $r = bbp_is_object_of_user( $t, $u, '_bbp_moderator' ); 136 137 $this->assertFalse( $r ); 138 139 // Add user id. 140 add_metadata( 'post', $t, '_bbp_moderator', $u, false ); 141 142 $r = bbp_is_object_of_user( $t, $u, '_bbp_moderator' ); 143 144 $this->assertTrue( $r ); 145 } 146 147 /** 89 148 * @covers ::bbp_edit_user_handler 90 149 * @todo Implement test_bbp_edit_user_handler().
Note: See TracChangeset
for help on using the changeset viewer.