Changeset 6421
- Timestamp:
- 05/22/2017 06:02:20 PM (8 years ago)
- Location:
- trunk/src
- Files:
-
- 1 added
- 2 edited
-
bbpress.php (modified) (1 diff)
-
includes/users/engagements.php (added)
-
includes/users/functions.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bbpress.php
r6341 r6421 360 360 // Users 361 361 require $this->includes_dir . 'users/capabilities.php'; 362 require $this->includes_dir . 'users/engagements.php'; 362 363 require $this->includes_dir . 'users/functions.php'; 363 364 require $this->includes_dir . 'users/template.php'; -
trunk/src/includes/users/functions.php
r6415 r6421 273 273 274 274 return (bool) apply_filters( 'bbp_is_object_of_user', (bool) $retval, $object_id, $user_id, $meta_key, $meta_type ); 275 }276 277 /** Engagements ***************************************************************/278 279 /**280 * Get the users who have engaged in a topic281 *282 * @since 2.6.0 bbPress (r6320)283 *284 * @param int $topic_id Optional. Topic id285 * @uses bbp_get_users_for_object() To get user ids who engaged286 * @uses apply_filters() Calls 'bbp_get_topic_engagements' with the users and287 * topic id288 * @return array|bool Results if the topic has any engagements, otherwise false289 */290 function bbp_get_topic_engagements( $topic_id = 0 ) {291 $topic_id = bbp_get_topic_id( $topic_id );292 $users = bbp_get_users_for_object( $topic_id, '_bbp_engagement' );293 294 return (array) apply_filters( 'bbp_get_topic_engagements', $users, $topic_id );295 }296 297 /**298 * Get a user's topic engagements299 *300 * @since 2.6.0 bbPress (r6320)301 *302 * @param int $user_id Optional. User id303 * @uses bbp_has_topics() To get the topics304 * @uses apply_filters() Calls 'bbp_get_user_engagements' with the topic query and305 * user id306 * @return array|bool Results if user has engaged, otherwise false307 */308 function bbp_get_user_engagements( $user_id = 0 ) {309 $user_id = bbp_get_user_id( $user_id );310 $engagements = bbp_has_topics( array(311 'meta_query' => array(312 array(313 'key' => '_bbp_engagement',314 'value' => $user_id,315 'compare' => 'NUMERIC'316 )317 )318 ) );319 320 return apply_filters( 'bbp_get_user_engagements', $engagements, $user_id );321 }322 323 /**324 * Get a user's engaged topic ids325 *326 * @since 2.6.0 bbPress (r6320)327 *328 * @param int $user_id Optional. User id329 * @uses bbp_get_user_id() To get the user id330 * @uses bbp_get_topic_post_type() To get the topic post type331 * @uses apply_filters() Calls 'bbp_get_user_engaged_topic_ids' with332 * the engaged topics and user id333 * @return array|bool Results if user has engaged, otherwise null334 */335 function bbp_get_user_engaged_topic_ids( $user_id = 0 ) {336 $user_id = bbp_get_user_id( $user_id );337 $engagements = new WP_Query( array(338 'fields' => 'ids',339 'post_type' => bbp_get_topic_post_type(),340 'nopaging' => true,341 'no_found_rows' => true,342 'meta_query' => array( array(343 'key' => '_bbp_engagement',344 'value' => $user_id,345 'compare' => 'NUMERIC'346 ) )347 ) );348 349 return (array) apply_filters( 'bbp_get_user_engaged_topic_ids', $engagements->posts, $user_id );350 }351 352 /**353 * Check if a user is engaged in a topic or not354 *355 * @since 2.6.0 bbPress (r6320)356 *357 * @param int $user_id Optional. User id358 * @param int $topic_id Optional. Topic id359 * @uses bbp_get_user_id() To get the user id360 * @uses bbp_get_topic_id() To get the topic id361 * @uses bbp_is_object_of_user() To check if the user has engaged362 * @uses apply_filters() Calls 'bbp_is_user_engaged' with the bool, user id,363 * topic id and engagements364 * @return bool True if the topic is in user's engagements, otherwise false365 */366 function bbp_is_user_engaged( $user_id = 0, $topic_id = 0 ) {367 $user_id = bbp_get_user_id( $user_id, true, true );368 $topic_id = bbp_get_topic_id( $topic_id );369 $retval = bbp_is_object_of_user( $topic_id, $user_id, '_bbp_engagement' );370 371 return (bool) apply_filters( 'bbp_is_user_engaged', (bool) $retval, $user_id, $topic_id );372 }373 374 /**375 * Add a topic to user's engagements376 *377 * @since 2.6.0 bbPress (r6320)378 *379 * @param int $user_id Optional. User id380 * @param int $topic_id Optional. Topic id381 * @uses bbp_is_user_engaged() To check if the user is engaged in a topic382 * @uses do_action() Calls 'bbp_add_user_engagement' with the user id and topic id383 * @return bool Always true384 */385 function bbp_add_user_engagement( $user_id = 0, $topic_id = 0 ) {386 387 // Bail if not enough info388 if ( empty( $user_id ) || empty( $topic_id ) ) {389 return false;390 }391 392 // Bail if no topic393 $topic = bbp_get_topic( $topic_id );394 if ( empty( $topic ) ) {395 return false;396 }397 398 // Bail if already a engaged399 if ( bbp_is_user_engaged( $user_id, $topic_id ) ) {400 return false;401 }402 403 // Bail if add fails404 if ( ! bbp_add_user_to_object( $topic_id, $user_id, '_bbp_engagement' ) ) {405 return false;406 }407 408 do_action( 'bbp_add_user_engagement', $user_id, $topic_id );409 410 return true;411 }412 413 /**414 * Remove a topic from user's engagements415 *416 * @since 2.6.0 bbPress (r6320)417 *418 * @param int $user_id Optional. User id419 * @param int $topic_id Optional. Topic id420 * @uses bbp_is_user_engaged() To check if the user is engaged in a topic421 * @uses do_action() Calls 'bbp_remove_user_engagement' with the user & topic id422 * @return bool True if the topic was removed from user's engagements, otherwise423 * false424 */425 function bbp_remove_user_engagement( $user_id, $topic_id ) {426 427 // Bail if not enough info428 if ( empty( $user_id ) || empty( $topic_id ) ) {429 return false;430 }431 432 // Bail if not already engaged433 if ( ! bbp_is_user_engaged( $user_id, $topic_id ) ) {434 return false;435 }436 437 // Bail if remove fails438 if ( ! bbp_remove_user_from_object( $topic_id, $user_id, '_bbp_engagement' ) ) {439 return false;440 }441 442 do_action( 'bbp_remove_user_engagement', $user_id, $topic_id );443 444 return true;445 }446 447 /** Favorites *****************************************************************/448 449 /**450 * Get the users who have made the topic favorite451 *452 * @since 2.0.0 bbPress (r2658)453 *454 * @param int $topic_id Optional. Topic id455 * @uses bbp_get_users_for_object() To get user IDs who favorited456 * @uses apply_filters() Calls 'bbp_get_topic_favoriters' with the users and457 * topic id458 * @return array|bool Results if the topic has any favoriters, otherwise false459 */460 function bbp_get_topic_favoriters( $topic_id = 0 ) {461 $topic_id = bbp_get_topic_id( $topic_id );462 $users = bbp_get_users_for_object( $topic_id, '_bbp_favorite' );463 464 return (array) apply_filters( 'bbp_get_topic_favoriters', $users, $topic_id );465 }466 467 /**468 * Get a user's favorite topics469 *470 * @since 2.0.0 bbPress (r2652)471 *472 * @param int $user_id Optional. User id473 * @uses bbp_has_topics() To get the topics474 * @uses apply_filters() Calls 'bbp_get_user_favorites' with the topic query and475 * user id476 * @return array|bool Results if user has favorites, otherwise false477 */478 function bbp_get_user_favorites( $user_id = 0 ) {479 $user_id = bbp_get_user_id( $user_id );480 $query = bbp_has_topics( array(481 'meta_query' => array(482 array(483 'key' => '_bbp_favorite',484 'value' => $user_id,485 'compare' => 'NUMERIC'486 )487 )488 ) );489 490 return apply_filters( 'bbp_get_user_favorites', $query, $user_id );491 }492 493 /**494 * Get a user's favorite topic ids495 *496 * @since 2.0.0 bbPress (r2652)497 *498 * @param int $user_id Optional. User id499 * @uses bbp_get_user_id() To get the user id500 * @uses bbp_get_topic_post_type() To get the topic post type501 * @uses apply_filters() Calls 'bbp_get_user_favorites_topic_ids' with502 * the favorites and user id503 * @return array|bool Results if user has favorites, otherwise null504 */505 function bbp_get_user_favorites_topic_ids( $user_id = 0 ) {506 $user_id = bbp_get_user_id( $user_id );507 $favorites = new WP_Query( array(508 'fields' => 'ids',509 'post_type' => bbp_get_topic_post_type(),510 'nopaging' => true,511 'no_found_rows' => true,512 'meta_query' => array( array(513 'key' => '_bbp_favorite',514 'value' => $user_id,515 'compare' => 'NUMERIC'516 ) )517 ) );518 519 return (array) apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites->posts, $user_id );520 }521 522 /**523 * Check if a topic is in user's favorites or not524 *525 * @since 2.0.0 bbPress (r2652)526 *527 * @param int $user_id Optional. User id528 * @param int $topic_id Optional. Topic id529 * @uses bbp_get_user_id() To get the user id530 * @uses bbp_get_user_favorites_topic_ids() To get the user favorites531 * @uses bbp_get_topic() To get the topic532 * @uses bbp_get_topic_id() To get the topic id533 * @uses bbp_is_object_of_user() To check if the user has a favorite534 * @uses apply_filters() Calls 'bbp_is_user_favorite' with the bool, user id,535 * topic id and favorites536 * @return bool True if the topic is in user's favorites, otherwise false537 */538 function bbp_is_user_favorite( $user_id = 0, $topic_id = 0 ) {539 $retval = false;540 $user_id = bbp_get_user_id( $user_id, true, true );541 $favorites = bbp_get_user_favorites_topic_ids( $user_id );542 543 if ( ! empty( $favorites ) ) {544 545 // Checking a specific topic id546 if ( ! empty( $topic_id ) ) {547 $topic = bbp_get_topic( $topic_id );548 $topic_id = ! empty( $topic ) ? $topic->ID : 0;549 550 // Using the global topic id551 } elseif ( bbp_get_topic_id() ) {552 $topic_id = bbp_get_topic_id();553 554 // Use the current post id555 } elseif ( ! bbp_get_topic_id() ) {556 $topic_id = get_the_ID();557 }558 559 // Is topic_id in the user's favorites560 if ( ! empty( $topic_id ) ) {561 $retval = bbp_is_object_of_user( $topic_id, $user_id, '_bbp_favorite' );562 }563 }564 565 return (bool) apply_filters( 'bbp_is_user_favorite', (bool) $retval, $user_id, $topic_id, $favorites );566 }567 568 /**569 * Add a topic to user's favorites570 *571 * @since 2.0.0 bbPress (r2652)572 *573 * @param int $user_id Optional. User id574 * @param int $topic_id Optional. Topic id575 * @uses bbp_is_user_favorite() To check if the topic is a user favorite576 * @uses do_action() Calls 'bbp_add_user_favorite' with the user id and topic id577 * @return bool Always true578 */579 function bbp_add_user_favorite( $user_id = 0, $topic_id = 0 ) {580 581 // Bail if not enough info582 if ( empty( $user_id ) || empty( $topic_id ) ) {583 return false;584 }585 586 // Bail if no topic587 $topic = bbp_get_topic( $topic_id );588 if ( empty( $topic ) ) {589 return false;590 }591 592 // Bail if already a favorite593 if ( bbp_is_user_favorite( $user_id, $topic_id ) ) {594 return false;595 }596 597 // Bail if add fails598 if ( ! bbp_add_user_to_object( $topic_id, $user_id, '_bbp_favorite' ) ) {599 return false;600 }601 602 do_action( 'bbp_add_user_favorite', $user_id, $topic_id );603 604 return true;605 }606 607 /**608 * Remove a topic from user's favorites609 *610 * @since 2.0.0 bbPress (r2652)611 *612 * @param int $user_id Optional. User id613 * @param int $topic_id Optional. Topic id614 * @uses bbp_is_user_favorite() To check if the topic is a user favorite615 * @uses do_action() Calls 'bbp_remove_user_favorite' with the user & topic id616 * @return bool True if the topic was removed from user's favorites, otherwise617 * false618 */619 function bbp_remove_user_favorite( $user_id, $topic_id ) {620 621 // Bail if not enough info622 if ( empty( $user_id ) || empty( $topic_id ) ) {623 return false;624 }625 626 // Bail if not already a favorite627 if ( ! bbp_is_user_favorite( $user_id, $topic_id ) ) {628 return false;629 }630 631 // Bail if remove fails632 if ( ! bbp_remove_user_from_object( $topic_id, $user_id, '_bbp_favorite' ) ) {633 return false;634 }635 636 do_action( 'bbp_remove_user_favorite', $user_id, $topic_id );637 638 return true;639 }640 641 /**642 * Handles the front end adding and removing of favorite topics643 *644 * @param string $action The requested action to compare this function to645 * @uses bbp_get_user_id() To get the user id646 * @uses bbp_verify_nonce_request() To verify the nonce and check the request647 * @uses current_user_can() To check if the current user can edit the user648 * @uses bbPress:errors:add() To log the error messages649 * @uses bbp_is_user_favorite() To check if the topic is in user's favorites650 * @uses bbp_remove_user_favorite() To remove the user favorite651 * @uses bbp_add_user_favorite() To add the user favorite652 * @uses do_action() Calls 'bbp_favorites_handler' with success, user id, topic653 * id and action654 * @uses bbp_is_favorites() To check if it's the favorites page655 * @uses bbp_get_favorites_link() To get the favorites page link656 * @uses bbp_get_topic_permalink() To get the topic permalink657 * @uses bbp_redirect() To redirect to the url658 */659 function bbp_favorites_handler( $action = '' ) {660 661 if ( ! bbp_is_favorites_active() ) {662 return false;663 }664 665 // Bail if no topic ID is passed666 if ( empty( $_GET['topic_id'] ) ) {667 return;668 }669 670 // Setup possible get actions671 $possible_actions = array(672 'bbp_favorite_add',673 'bbp_favorite_remove',674 );675 676 // Bail if actions aren't meant for this function677 if ( ! in_array( $action, $possible_actions, true ) ) {678 return;679 }680 681 // What action is taking place?682 $topic_id = intval( $_GET['topic_id'] );683 $user_id = bbp_get_user_id( 0, true, true );684 685 // Check for empty topic686 if ( empty( $topic_id ) ) {687 bbp_add_error( 'bbp_favorite_topic_id', __( '<strong>ERROR</strong>: No topic was found. Which topic are you marking/unmarking as favorite?', 'bbpress' ) );688 689 // Check nonce690 } elseif ( ! bbp_verify_nonce_request( 'toggle-favorite_' . $topic_id ) ) {691 bbp_add_error( 'bbp_favorite_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );692 693 // Check current user's ability to edit the user694 } elseif ( ! current_user_can( 'edit_user', $user_id ) ) {695 bbp_add_error( 'bbp_favorite_permission', __( '<strong>ERROR</strong>: You do not have permission to edit favorites for that user!.', 'bbpress' ) );696 }697 698 // Bail if errors699 if ( bbp_has_errors() ) {700 return;701 }702 703 /** No errors *************************************************************/704 705 $is_favorite = bbp_is_user_favorite( $user_id, $topic_id );706 $success = false;707 708 if ( true === $is_favorite && 'bbp_favorite_remove' === $action ) {709 $success = bbp_remove_user_favorite( $user_id, $topic_id );710 } elseif ( false === $is_favorite && 'bbp_favorite_add' === $action ) {711 $success = bbp_add_user_favorite( $user_id, $topic_id );712 }713 714 // Do additional favorites actions715 do_action( 'bbp_favorites_handler', $success, $user_id, $topic_id, $action );716 717 // Success!718 if ( true === $success ) {719 720 // Redirect back from whence we came721 if ( ! empty( $_REQUEST['redirect_to'] ) ) {722 $redirect = $_REQUEST['redirect_to']; // Validated later723 } elseif ( bbp_is_favorites() ) {724 $redirect = bbp_get_favorites_permalink( $user_id, true );725 } elseif ( bbp_is_single_user() ) {726 $redirect = bbp_get_user_profile_url();727 } elseif ( is_singular( bbp_get_topic_post_type() ) ) {728 $redirect = bbp_get_topic_permalink( $topic_id );729 } elseif ( is_single() || is_page() ) {730 $redirect = get_permalink();731 } else {732 $redirect = get_permalink( $topic_id );733 }734 735 bbp_redirect( $redirect );736 737 // Fail! Handle errors738 } elseif ( true === $is_favorite && 'bbp_favorite_remove' === $action ) {739 bbp_add_error( 'bbp_favorite_remove', __( '<strong>ERROR</strong>: There was a problem removing that topic from favorites.', 'bbpress' ) );740 } elseif ( false === $is_favorite && 'bbp_favorite_add' === $action ) {741 bbp_add_error( 'bbp_favorite_add', __( '<strong>ERROR</strong>: There was a problem favoriting that topic.', 'bbpress' ) );742 }743 }744 745 /** Subscriptions *************************************************************/746 747 /**748 * Get the users who have subscribed to the forum749 *750 * @since 2.5.0 bbPress (r5156)751 *752 * @param int $forum_id Optional. forum id753 * @uses bbp_get_users_for_object() To get the forum subscribers754 * @uses apply_filters() Calls 'bbp_get_forum_subscribers' with the subscribers755 * @return array|bool Results if the forum has any subscribers, otherwise false756 */757 function bbp_get_forum_subscribers( $forum_id = 0 ) {758 $forum_id = bbp_get_forum_id( $forum_id );759 $users = bbp_get_users_for_object( $forum_id, '_bbp_subscription' );760 761 return (array) apply_filters( 'bbp_get_forum_subscribers', $users, $forum_id );762 }763 764 /**765 * Get the users who have subscribed to the topic766 *767 * @since 2.0.0 bbPress (r2668)768 *769 * @param int $topic_id Optional. Topic id770 * @uses bbp_get_users_for_object() To get the topic subscribers771 * @uses apply_filters() Calls 'bbp_get_topic_subscribers' with the subscribers772 * @return array|bool Results if the topic has any subscribers, otherwise false773 */774 function bbp_get_topic_subscribers( $topic_id = 0 ) {775 $topic_id = bbp_get_topic_id( $topic_id );776 $users = bbp_get_users_for_object( $topic_id, '_bbp_subscription' );777 778 return (array) apply_filters( 'bbp_get_topic_subscribers', $users, $topic_id );779 }780 781 /**782 * Get a user's subscribed topics783 *784 * @since 2.0.0 bbPress (r2668)785 *786 * @deprecated 2.5.0 bbPress (r5156)787 *788 * @param int $user_id Optional. User id789 * @uses bbp_get_user_topic_subscriptions() To get the user's subscriptions790 * @return array|bool Results if user has subscriptions, otherwise false791 */792 function bbp_get_user_subscriptions( $user_id = 0 ) {793 _deprecated_function( __FUNCTION__, 2.5, 'bbp_get_user_topic_subscriptions()' );794 $query = bbp_get_user_topic_subscriptions( $user_id );795 return apply_filters( 'bbp_get_user_subscriptions', $query, $user_id );796 }797 798 /**799 * Get a user's subscribed topics800 *801 * @since 2.0.0 bbPress (r2668)802 *803 * @param int $user_id Optional. User id804 * @uses bbp_has_topics() To get the topics805 * @uses apply_filters() Calls 'bbp_get_user_subscriptions' with the topic query806 * and user id807 * @return array|bool Results if user has subscriptions, otherwise false808 */809 function bbp_get_user_topic_subscriptions( $user_id = 0 ) {810 $user_id = bbp_get_user_id( $user_id );811 $query = bbp_has_topics( array(812 'meta_query' => array(813 array(814 'key' => '_bbp_subscription',815 'value' => $user_id,816 'compare' => 'NUMERIC'817 )818 )819 ) );820 821 return apply_filters( 'bbp_get_user_topic_subscriptions', $query, $user_id );822 }823 824 /**825 * Get a user's subscribed forums826 *827 * @since 2.5.0 bbPress (r5156)828 *829 * @param int $user_id Optional. User id830 * @uses bbp_has_forums() To get the forums831 * @uses apply_filters() Calls 'bbp_get_user_forum_subscriptions' with the forum832 * query and user id833 * @return array|bool Results if user has subscriptions, otherwise false834 */835 function bbp_get_user_forum_subscriptions( $user_id = 0 ) {836 $user_id = bbp_get_user_id( $user_id );837 $query = bbp_has_forums( array(838 'meta_query' => array(839 array(840 'key' => '_bbp_subscription',841 'value' => $user_id,842 'compare' => 'NUMERIC'843 )844 )845 ) );846 847 return apply_filters( 'bbp_get_user_forum_subscriptions', $query, $user_id );848 }849 850 /**851 * Get a user's subscribed forum ids852 *853 * @since 2.5.0 bbPress (r5156)854 *855 * @param int $user_id Optional. User id856 * @uses bbp_get_user_id() To get the user id857 * @uses bbp_get_forum_post_type() To get the forum post type858 * @uses apply_filters() Calls 'bbp_get_user_subscribed_forum_ids' with859 * the subscriptions and user id860 * @return array|bool Results if user has subscriptions, otherwise null861 */862 function bbp_get_user_subscribed_forum_ids( $user_id = 0 ) {863 $user_id = bbp_get_user_id( $user_id );864 $subscriptions = new WP_Query( array(865 'fields' => 'ids',866 'post_type' => bbp_get_forum_post_type(),867 'nopaging' => true,868 'no_found_rows' => true,869 'meta_query' => array( array(870 'key' => '_bbp_subscription',871 'value' => $user_id,872 'compare' => 'NUMERIC'873 ) )874 ) );875 876 return (array) apply_filters( 'bbp_get_user_subscribed_forum_ids', $subscriptions->posts, $user_id );877 }878 879 /**880 * Get a user's subscribed topics' ids881 *882 * @since 2.0.0 bbPress (r2668)883 *884 * @param int $user_id Optional. User id885 * @uses bbp_get_user_id() To get the user id886 * @uses bbp_get_topic_post_type() To get the topic post type887 * @uses apply_filters() Calls 'bbp_get_user_subscribed_topic_ids' with888 * the subscriptions and user id889 * @return array|bool Results if user has subscriptions, otherwise null890 */891 function bbp_get_user_subscribed_topic_ids( $user_id = 0 ) {892 $user_id = bbp_get_user_id( $user_id );893 $subscriptions = new WP_Query( array(894 'fields' => 'ids',895 'post_type' => bbp_get_topic_post_type(),896 'nopaging' => true,897 'no_found_rows' => true,898 'meta_query' => array( array(899 'key' => '_bbp_subscription',900 'value' => $user_id,901 'compare' => 'NUMERIC'902 ) )903 ) );904 905 return (array) apply_filters( 'bbp_get_user_subscribed_topic_ids', $subscriptions->posts, $user_id );906 }907 908 /**909 * Check if a topic or forum is in user's subscription list or not910 *911 * @since 2.5.0 bbPress (r5156)912 *913 * @param int $user_id Optional. User id914 * @param int $object_id Optional. Topic id915 * @uses get_post() To get the post object916 * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions917 * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions918 * @uses bbp_get_forum_post_type() To get the forum post type919 * @uses bbp_get_topic_post_type() To get the topic post type920 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,921 * forum/topic id and subsriptions922 * @return bool True if the forum or topic is in user's subscriptions, otherwise false923 */924 function bbp_is_user_subscribed( $user_id = 0, $object_id = 0 ) {925 926 // Assume user is not subscribed927 $retval = false;928 929 // Setup ID's array930 $subscribed_ids = array();931 932 // User and object ID's are passed933 if ( ! empty( $user_id ) && ! empty( $object_id ) ) {934 935 // Get the post type936 $post_type = get_post_type( $object_id );937 938 // Post exists, so check the types939 if ( ! empty( $post_type ) ) {940 941 switch( $post_type ) {942 943 // Forum944 case bbp_get_forum_post_type() :945 $subscribed_ids = bbp_get_user_subscribed_forum_ids( $user_id );946 $retval = bbp_is_user_subscribed_to_forum( $user_id, $object_id, $subscribed_ids );947 break;948 949 // Topic (default)950 case bbp_get_topic_post_type() :951 default :952 $subscribed_ids = bbp_get_user_subscribed_topic_ids( $user_id );953 $retval = bbp_is_user_subscribed_to_topic( $user_id, $object_id, $subscribed_ids );954 break;955 }956 }957 }958 959 return (bool) apply_filters( 'bbp_is_user_subscribed', $retval, $user_id, $object_id, $subscribed_ids );960 }961 962 /**963 * Check if a forum is in user's subscription list or not964 *965 * @since 2.5.0 bbPress (r5156)966 *967 * @param int $user_id Optional. User id968 * @param int $forum_id Optional. Topic id969 * @param array $subscribed_ids Optional. Array of forum ID's to check970 * @uses bbp_get_user_id() To get the user id971 * @uses bbp_get_forum() To get the forum972 * @uses bbp_get_forum_id() To get the forum id973 * @uses bbp_is_object_of_user() To check if the user has a subscription974 * @uses apply_filters() Calls 'bbp_is_user_subscribed_to_forum' with the bool, user id,975 * forum id and subsriptions976 * @return bool True if the forum is in user's subscriptions, otherwise false977 */978 function bbp_is_user_subscribed_to_forum( $user_id = 0, $forum_id = 0, $subscribed_ids = array() ) {979 980 // Assume user is not subscribed981 $retval = false;982 983 // Validate user984 $user_id = bbp_get_user_id( $user_id, true, true );985 if ( ! empty( $user_id ) ) {986 987 // Get subscription ID's if none passed988 if ( empty( $subscribed_ids ) ) {989 $subscribed_ids = bbp_get_user_subscribed_forum_ids( $user_id );990 }991 992 // User has forum subscriptions993 if ( ! empty( $subscribed_ids ) ) {994 995 // Checking a specific forum id996 if ( ! empty( $forum_id ) ) {997 $forum = bbp_get_forum( $forum_id );998 $forum_id = ! empty( $forum ) ? $forum->ID : 0;999 1000 // Using the global forum id1001 } elseif ( bbp_get_forum_id() ) {1002 $forum_id = bbp_get_forum_id();1003 1004 // Use the current post id1005 } elseif ( ! bbp_get_forum_id() ) {1006 $forum_id = get_the_ID();1007 }1008 1009 // Is forum_id in the user's subscriptions1010 if ( ! empty( $forum_id ) ) {1011 $retval = bbp_is_object_of_user( $forum_id, $user_id, '_bbp_subscription' );1012 }1013 }1014 }1015 1016 return (bool) apply_filters( 'bbp_is_user_subscribed_to_forum', (bool) $retval, $user_id, $forum_id, $subscribed_ids );1017 }1018 1019 /**1020 * Check if a topic is in user's subscription list or not1021 *1022 * @since 2.5.0 bbPress (r5156)1023 *1024 * @param int $user_id Optional. User id1025 * @param int $topic_id Optional. Topic id1026 * @param array $subscribed_ids Optional. Array of topic ID's to check1027 * @uses bbp_get_user_id() To get the user id1028 * @uses bbp_get_topic() To get the topic1029 * @uses bbp_get_topic_id() To get the topic id1030 * @uses bbp_is_object_of_user() To check if the user is subscribed1031 * @uses apply_filters() Calls 'bbp_is_user_subscribed_to_topic' with the bool, user id,1032 * topic id and subsriptions1033 * @return bool True if the topic is in user's subscriptions, otherwise false1034 */1035 function bbp_is_user_subscribed_to_topic( $user_id = 0, $topic_id = 0, $subscribed_ids = array() ) {1036 1037 // Assume user is not subscribed1038 $retval = false;1039 1040 // Validate user1041 $user_id = bbp_get_user_id( $user_id, true, true );1042 if ( ! empty( $user_id ) ) {1043 1044 // Get subscription ID's if none passed1045 if ( empty( $subscribed_ids ) ) {1046 $subscribed_ids = bbp_get_user_subscribed_topic_ids( $user_id );1047 }1048 1049 // User has topic subscriptions1050 if ( ! empty( $subscribed_ids ) ) {1051 1052 // Checking a specific topic id1053 if ( ! empty( $topic_id ) ) {1054 $topic = bbp_get_topic( $topic_id );1055 $topic_id = ! empty( $topic ) ? $topic->ID : 0;1056 1057 // Using the global topic id1058 } elseif ( bbp_get_topic_id() ) {1059 $topic_id = bbp_get_topic_id();1060 1061 // Use the current post id1062 } elseif ( ! bbp_get_topic_id() ) {1063 $topic_id = get_the_ID();1064 }1065 1066 // Is topic_id in the user's subscriptions1067 if ( ! empty( $topic_id ) ) {1068 $retval = bbp_is_object_of_user( $topic_id, $user_id, '_bbp_subscription' );1069 }1070 }1071 }1072 1073 return (bool) apply_filters( 'bbp_is_user_subscribed_to_topic', (bool) $retval, $user_id, $topic_id, $subscribed_ids );1074 }1075 1076 /**1077 * Add a user subscription1078 *1079 * @since 2.5.0 bbPress (r5156)1080 *1081 * @param int $user_id Optional. User id1082 * @param int $object_id Optional. Topic id1083 * @uses get_post() To get the post object1084 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & object id1085 * @return bool Always true1086 */1087 function bbp_add_user_subscription( $user_id = 0, $object_id = 0 ) {1088 1089 // Bail if not enough info1090 if ( empty( $user_id ) || empty( $object_id ) ) {1091 return false;1092 }1093 1094 // Get the post type1095 $post_type = get_post_type( $object_id );1096 if ( empty( $post_type ) ) {1097 return false;1098 }1099 1100 // Bail if already subscribed1101 if ( bbp_is_user_subscribed( $user_id, $object_id ) ) {1102 return false;1103 }1104 1105 // Bail if add fails1106 if ( ! bbp_add_user_to_object( $object_id, $user_id, '_bbp_subscription' ) ) {1107 return false;1108 }1109 1110 do_action( 'bbp_add_user_subscription', $user_id, $object_id, $post_type );1111 1112 return true;1113 }1114 1115 /**1116 * Add a forum to user's subscriptions1117 *1118 * @since 2.5.0 bbPress (r5156)1119 *1120 * @param int $user_id Optional. User id1121 * @param int $forum_id Optional. forum id1122 * @uses bbp_get_forum() To get the forum1123 * @uses bbp_add_user_subscription() To add the user subscription1124 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & forum id1125 * @return bool Always true1126 */1127 function bbp_add_user_forum_subscription( $user_id = 0, $forum_id = 0 ) {1128 1129 // Bail if not enough info1130 if ( empty( $user_id ) || empty( $forum_id ) ) {1131 return false;1132 }1133 1134 // Bail if no forum1135 $forum = bbp_get_forum( $forum_id );1136 if ( empty( $forum ) ) {1137 return false;1138 }1139 1140 // Bail if already subscribed1141 if ( bbp_is_user_subscribed( $user_id, $forum_id ) ) {1142 return false;1143 }1144 1145 // Bail if add fails1146 if ( ! bbp_add_user_subscription( $user_id, $forum_id ) ) {1147 return false;1148 }1149 1150 do_action( 'bbp_add_user_forum_subscription', $user_id, $forum_id );1151 1152 return true;1153 }1154 1155 /**1156 * Add a topic to user's subscriptions1157 *1158 * @since 2.0.0 bbPress (r2668)1159 *1160 * @param int $user_id Optional. User id1161 * @param int $topic_id Optional. Topic id1162 * @uses bbp_get_topic() To get the topic1163 * @uses bbp_add_user_subscription() To add the subscription1164 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id1165 * @return bool Always true1166 */1167 function bbp_add_user_topic_subscription( $user_id = 0, $topic_id = 0 ) {1168 1169 // Bail if not enough info1170 if ( empty( $user_id ) || empty( $topic_id ) ) {1171 return false;1172 }1173 1174 // Bail if no topic1175 $topic = bbp_get_topic( $topic_id );1176 if ( empty( $topic ) ) {1177 return false;1178 }1179 1180 // Bail if already subscribed1181 if ( bbp_is_user_subscribed_to_topic( $user_id, $topic_id ) ) {1182 return false;1183 }1184 1185 // Bail if add fails1186 if ( ! bbp_add_user_subscription( $user_id, $topic_id ) ) {1187 return false;1188 }1189 1190 do_action( 'bbp_add_user_topic_subscription', $user_id, $topic_id );1191 1192 return true;1193 }1194 1195 /**1196 * Remove a user subscription1197 *1198 * @since 2.0.0 bbPress (r2668)1199 *1200 * @param int $user_id Optional. User id1201 * @param int $object_id Optional. Topic id1202 * @uses get_post() To get the post object1203 * @uses bbp_is_user_subscribed() To check if the user is already subscribed1204 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and1205 * topic id1206 * @return bool True if the topic was removed from user's subscriptions,1207 * otherwise false1208 */1209 function bbp_remove_user_subscription( $user_id = 0, $object_id = 0 ) {1210 1211 // Bail if not enough info1212 if ( empty( $user_id ) || empty( $object_id ) ) {1213 return false;1214 }1215 1216 // Get post type1217 $post_type = get_post_type( $object_id );1218 if ( empty( $post_type ) ) {1219 return false;1220 }1221 1222 // Bail if not subscribed1223 if ( ! bbp_is_user_subscribed( $user_id, $object_id ) ) {1224 return false;1225 }1226 1227 // Bail if remove fails1228 if ( ! bbp_remove_user_from_object( $object_id, $user_id, '_bbp_subscription' ) ) {1229 return false;1230 }1231 1232 do_action( 'bbp_remove_user_subscription', $user_id, $object_id, $post_type );1233 1234 return true;1235 }1236 1237 /**1238 * Remove a forum from user's subscriptions1239 *1240 * @since 2.5.0 bbPress (r5156)1241 *1242 * @param int $user_id Optional. User id1243 * @param int $forum_id Optional. forum id1244 * @uses bbp_remove_user_subscription() To remove the subscription1245 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and1246 * forum id1247 * @return bool True if the forum was removed from user's subscriptions,1248 * otherwise false1249 */1250 function bbp_remove_user_forum_subscription( $user_id = 0, $forum_id = 0 ) {1251 1252 // Bail if not enough info1253 if ( empty( $user_id ) || empty( $forum_id ) ) {1254 return false;1255 }1256 1257 // Bail if remove fails1258 if ( ! bbp_remove_user_subscription( $user_id, $forum_id ) ) {1259 return false;1260 }1261 1262 do_action( 'bbp_remove_user_forum_subscription', $user_id, $forum_id );1263 1264 return true;1265 }1266 1267 /**1268 * Remove a topic from user's subscriptions1269 *1270 * @since 2.5.0 bbPress (r5156)1271 *1272 * @param int $user_id Optional. User id1273 * @param int $topic_id Optional. Topic id1274 * @uses bbp_remove_user_subscription() To remove the subscription1275 * @uses do_action() Calls 'bbp_remove_user_topic_subscription' with the user id and1276 * topic id1277 * @return bool True if the topic was removed from user's subscriptions,1278 * otherwise false1279 */1280 function bbp_remove_user_topic_subscription( $user_id = 0, $topic_id = 0 ) {1281 1282 // Bail if not enough info1283 if ( empty( $user_id ) || empty( $topic_id ) ) {1284 return false;1285 }1286 1287 // Bail if remove fails1288 if ( ! bbp_remove_user_subscription( $user_id, $topic_id ) ) {1289 return false;1290 }1291 1292 do_action( 'bbp_remove_user_topic_subscription', $user_id, $topic_id );1293 1294 return true;1295 }1296 1297 /**1298 * Handles the front end subscribing and unsubscribing forums1299 *1300 * @since 2.5.0 bbPress (r5156)1301 *1302 * @param string $action The requested action to compare this function to1303 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active1304 * @uses bbp_get_user_id() To get the user id1305 * @uses bbp_verify_nonce_request() To verify the nonce and check the request1306 * @uses current_user_can() To check if the current user can edit the user1307 * @uses bbPress:errors:add() To log the error messages1308 * @uses bbp_is_user_subscribed() To check if the forum is in user's1309 * subscriptions1310 * @uses bbp_remove_user_subscription() To remove the user subscription1311 * @uses bbp_add_user_subscription() To add the user subscription1312 * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,1313 * forum id and action1314 * @uses bbp_is_subscription() To check if it's the subscription page1315 * @uses bbp_get_forum_permalink() To get the forum permalink1316 * @uses bbp_redirect() To redirect to the url1317 */1318 function bbp_forum_subscriptions_handler( $action = '' ) {1319 1320 if ( ! bbp_is_subscriptions_active() ) {1321 return false;1322 }1323 1324 // Bail if no forum ID is passed1325 if ( empty( $_GET['forum_id'] ) ) {1326 return;1327 }1328 1329 // Setup possible get actions1330 $possible_actions = array(1331 'bbp_subscribe',1332 'bbp_unsubscribe',1333 );1334 1335 // Bail if actions aren't meant for this function1336 if ( ! in_array( $action, $possible_actions, true ) ) {1337 return;1338 }1339 1340 // Get required data1341 $user_id = bbp_get_user_id( 0, true, true );1342 $forum_id = intval( $_GET['forum_id'] );1343 1344 // Check for empty forum1345 if ( empty( $forum_id ) ) {1346 bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: No forum was found. Which forum are you subscribing/unsubscribing to?', 'bbpress' ) );1347 1348 // Check nonce1349 } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $forum_id ) ) {1350 bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );1351 1352 // Check current user's ability to edit the user1353 } elseif ( ! current_user_can( 'edit_user', $user_id ) ) {1354 bbp_add_error( 'bbp_subscription_permission', __( '<strong>ERROR</strong>: You do not have permission to edit favorites of that user.', 'bbpress' ) );1355 }1356 1357 // Bail if we have errors1358 if ( bbp_has_errors() ) {1359 return;1360 }1361 1362 /** No errors *************************************************************/1363 1364 $is_subscription = bbp_is_user_subscribed( $user_id, $forum_id );1365 $success = false;1366 1367 if ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {1368 $success = bbp_remove_user_subscription( $user_id, $forum_id );1369 } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {1370 $success = bbp_add_user_subscription( $user_id, $forum_id );1371 }1372 1373 // Do additional subscriptions actions1374 do_action( 'bbp_subscriptions_handler', $success, $user_id, $forum_id, $action );1375 1376 // Success!1377 if ( true === $success ) {1378 1379 // Redirect back from whence we came1380 if ( ! empty( $_REQUEST['redirect_to'] ) ) {1381 $redirect = $_REQUEST['redirect_to']; // Validated later1382 } elseif ( bbp_is_subscriptions() ) {1383 $redirect = bbp_get_subscriptions_permalink( $user_id );1384 } elseif ( bbp_is_single_user() ) {1385 $redirect = bbp_get_user_profile_url();1386 } elseif ( is_singular( bbp_get_forum_post_type() ) ) {1387 $redirect = bbp_get_forum_permalink( $forum_id );1388 } elseif ( is_single() || is_page() ) {1389 $redirect = get_permalink();1390 } else {1391 $redirect = get_permalink( $forum_id );1392 }1393 1394 bbp_redirect( $redirect );1395 1396 // Fail! Handle errors1397 } elseif ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {1398 bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that forum.', 'bbpress' ) );1399 } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {1400 bbp_add_error( 'bbp_subscribe', __( '<strong>ERROR</strong>: There was a problem subscribing to that forum.', 'bbpress' ) );1401 }1402 }1403 1404 /**1405 * Handles the front end subscribing and unsubscribing topics1406 *1407 * @since 2.0.0 bbPress (r2790)1408 *1409 * @param string $action The requested action to compare this function to1410 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active1411 * @uses bbp_get_user_id() To get the user id1412 * @uses bbp_verify_nonce_request() To verify the nonce and check the request1413 * @uses current_user_can() To check if the current user can edit the user1414 * @uses bbPress:errors:add() To log the error messages1415 * @uses bbp_is_user_subscribed() To check if the topic is in user's1416 * subscriptions1417 * @uses bbp_remove_user_subscription() To remove the user subscription1418 * @uses bbp_add_user_subscription() To add the user subscription1419 * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,1420 * topic id and action1421 * @uses bbp_is_subscription() To check if it's the subscription page1422 * @uses bbp_get_topic_permalink() To get the topic permalink1423 * @uses bbp_redirect() To redirect to the url1424 */1425 function bbp_subscriptions_handler( $action = '' ) {1426 1427 if ( ! bbp_is_subscriptions_active() ) {1428 return false;1429 }1430 1431 // Bail if no topic ID is passed1432 if ( empty( $_GET['topic_id'] ) ) {1433 return;1434 }1435 1436 // Setup possible get actions1437 $possible_actions = array(1438 'bbp_subscribe',1439 'bbp_unsubscribe',1440 );1441 1442 // Bail if actions aren't meant for this function1443 if ( ! in_array( $action, $possible_actions, true ) ) {1444 return;1445 }1446 1447 // Get required data1448 $user_id = bbp_get_user_id( 0, true, true );1449 $topic_id = intval( $_GET['topic_id'] );1450 1451 // Check for empty topic1452 if ( empty( $topic_id ) ) {1453 bbp_add_error( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: No topic was found. Which topic are you subscribing/unsubscribing to?', 'bbpress' ) );1454 1455 // Check nonce1456 } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $topic_id ) ) {1457 bbp_add_error( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );1458 1459 // Check current user's ability to edit the user1460 } elseif ( ! current_user_can( 'edit_user', $user_id ) ) {1461 bbp_add_error( 'bbp_subscription_permission', __( '<strong>ERROR</strong>: You do not have permission to edit favorites of that user.', 'bbpress' ) );1462 }1463 1464 // Bail if we have errors1465 if ( bbp_has_errors() ) {1466 return;1467 }1468 1469 /** No errors *************************************************************/1470 1471 $is_subscription = bbp_is_user_subscribed( $user_id, $topic_id );1472 $success = false;1473 1474 if ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {1475 $success = bbp_remove_user_subscription( $user_id, $topic_id );1476 } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {1477 $success = bbp_add_user_subscription( $user_id, $topic_id );1478 }1479 1480 // Do additional subscriptions actions1481 do_action( 'bbp_subscriptions_handler', $success, $user_id, $topic_id, $action );1482 1483 // Success!1484 if ( true === $success ) {1485 1486 // Redirect back from whence we came1487 if ( ! empty( $_REQUEST['redirect_to'] ) ) {1488 $redirect = $_REQUEST['redirect_to']; // Validated later1489 } elseif ( bbp_is_subscriptions() ) {1490 $redirect = bbp_get_subscriptions_permalink( $user_id );1491 } elseif ( bbp_is_single_user() ) {1492 $redirect = bbp_get_user_profile_url();1493 } elseif ( is_singular( bbp_get_topic_post_type() ) ) {1494 $redirect = bbp_get_topic_permalink( $topic_id );1495 } elseif ( is_single() || is_page() ) {1496 $redirect = get_permalink();1497 } else {1498 $redirect = get_permalink( $topic_id );1499 }1500 1501 bbp_redirect( $redirect );1502 1503 // Fail! Handle errors1504 } elseif ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {1505 bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that topic.', 'bbpress' ) );1506 } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {1507 bbp_add_error( 'bbp_subscribe', __( '<strong>ERROR</strong>: There was a problem subscribing to that topic.', 'bbpress' ) );1508 }1509 275 } 1510 276
Note: See TracChangeset
for help on using the changeset viewer.