[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/src/includes/users/ -> engagements.php (source)

   1  <?php
   2  
   3  /**
   4   * bbPress User Engagement Functions
   5   *
   6   * @package bbPress
   7   * @subpackage Engagements
   8   */
   9  
  10  // Exit if accessed directly
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /** User Relationships ********************************************************/
  14  
  15  /**
  16   * Add a user id to an object
  17   *
  18   * @since 2.6.0 bbPress (r6109)
  19   *
  20   * @param int    $object_id The object id
  21   * @param int    $user_id   The user id
  22   * @param string $rel_key   The relationship key
  23   * @param string $rel_type  The relationship type (usually 'post')
  24   * @param bool   $unique    Whether meta key should be unique to the object
  25   *
  26   * @return bool Returns true on success, false on failure
  27   */
  28  function bbp_add_user_to_object( $object_id = 0, $user_id = 0, $rel_key = '', $rel_type = 'post', $unique = false ) {
  29      $object_id = absint( $object_id );
  30      $user_id   = absint( $user_id );
  31      $retval    = bbp_user_engagements_interface( $rel_key, $rel_type )->add_user_to_object( $object_id, $user_id, $rel_key, $rel_type, $unique );
  32  
  33      // Filter & return
  34      return (bool) apply_filters( 'bbp_add_user_to_object', $retval, $object_id, $user_id, $rel_key, $rel_type, $unique );
  35  }
  36  
  37  /**
  38   * Remove a user id from an object
  39   *
  40   * @since 2.6.0 bbPress (r6109)
  41   *
  42   * @param int    $object_id The object id
  43   * @param int    $user_id   The user id
  44   * @param string $rel_key   The relationship key
  45   * @param string $rel_type  The relationship type (usually 'post')
  46   *
  47   * @return bool Returns true on success, false on failure
  48   */
  49  function bbp_remove_user_from_object( $object_id = 0, $user_id = 0, $rel_key = '', $rel_type = 'post' ) {
  50      $retval = bbp_user_engagements_interface( $rel_key, $rel_type )->remove_user_from_object( $object_id, $user_id, $rel_key, $rel_type );
  51  
  52      // Filter & return
  53      return (bool) apply_filters( 'bbp_remove_user_from_object', $retval, $object_id, $user_id, $rel_key, $rel_type );
  54  }
  55  
  56  /**
  57   * Remove a user id from all objects
  58   *
  59   * @since 2.6.0 bbPress (r6109)
  60   *
  61   * @param int    $user_id  The user id
  62   * @param string $rel_key  The relationship key
  63   * @param string $rel_type The relationship type (usually 'post')
  64   *
  65   * @return bool Returns true on success, false on failure
  66   */
  67  function bbp_remove_user_from_all_objects( $user_id = 0, $rel_key = '', $rel_type = 'post' ) {
  68      $user_id = absint( $user_id );
  69      $retval  = bbp_user_engagements_interface( $rel_key, $rel_type )->remove_user_from_all_objects( $user_id, $rel_key, $rel_type );
  70  
  71      // Filter & return
  72      return (bool) apply_filters( 'bbp_remove_user_from_all_objects', $retval, $user_id, $rel_key, $rel_type );
  73  }
  74  
  75  /**
  76   * Remove an object from all users
  77   *
  78   * @since 2.6.0 bbPress (r6109)
  79   *
  80   * @param int    $object_id The object id
  81   * @param int    $user_id   The user id
  82   * @param string $rel_key   The relationship key
  83   * @param string $rel_type  The relationship type (usually 'post')
  84   *
  85   * @return bool Returns true on success, false on failure
  86   */
  87  function bbp_remove_object_from_all_users( $object_id = 0, $rel_key = '', $rel_type = 'post' ) {
  88      $object_id = absint( $object_id );
  89      $retval    = bbp_user_engagements_interface( $rel_key, $rel_type )->remove_object_from_all_users( $object_id, $rel_key, $rel_type );
  90  
  91      // Filter & return
  92      return (bool) apply_filters( 'bbp_remove_object_from_all_users', $retval, $object_id, $rel_key, $rel_type );
  93  }
  94  
  95  /**
  96   * Remove all users from all objects
  97   *
  98   * @since 2.6.0 bbPress (r6109)
  99   *
 100   * @param string $rel_key  The relationship key
 101   * @param string $rel_type The relationship type (usually 'post')
 102   *
 103   * @return bool Returns true on success, false on failure
 104   */
 105  function bbp_remove_all_users_from_all_objects( $rel_key = '', $rel_type = 'post' ) {
 106      $retval = bbp_user_engagements_interface( $rel_key, $rel_type )->remove_all_users_from_all_objects( $rel_key, $rel_type );
 107  
 108      // Filter & return
 109      return (bool) apply_filters( 'bbp_remove_all_users_from_all_objects', $retval, $rel_key, $rel_type );
 110  }
 111  
 112  /**
 113   * Get users of an object
 114   *
 115   * @since 2.6.0 bbPress (r6109)
 116   *
 117   * @param int    $object_id The object id
 118   * @param string $rel_key   The key used to index this relationship
 119   * @param string $rel_type  The type of meta to look in
 120   *
 121   * @return array Returns ids of users
 122   */
 123  function bbp_get_users_for_object( $object_id = 0, $rel_key = '', $rel_type = 'post' ) {
 124      $object_id = absint( $object_id );
 125      $retval    = bbp_user_engagements_interface( $rel_key, $rel_type )->get_users_for_object( $object_id, $rel_key, $rel_type );
 126  
 127      // Filter & return
 128      return (array) apply_filters( 'bbp_get_users_for_object', $retval, $object_id, $rel_key, $rel_type );
 129  }
 130  
 131  /**
 132   * Check if an object has a specific user
 133   *
 134   * @since 2.6.0 bbPress (r6109)
 135   *
 136   * @param int    $object_id The object id
 137   * @param int    $user_id   The user id
 138   * @param string $rel_key   The relationship key
 139   * @param string $rel_type  The relationship type (usually 'post')
 140   *
 141   * @return bool Returns true if object has a user, false if not
 142   */
 143  function bbp_is_object_of_user( $object_id = 0, $user_id = 0, $rel_key = '', $rel_type = 'post' ) {
 144      $object_id = absint( $object_id );
 145      $user_id   = absint( $user_id );
 146      $user_ids  = bbp_get_users_for_object( $object_id, $rel_key, $rel_type );
 147      $retval    = is_numeric( array_search( $user_id, $user_ids, true ) );
 148  
 149      // Filter & return
 150      return (bool) apply_filters( 'bbp_is_object_of_user', $retval, $object_id, $user_id, $rel_key, $rel_type );
 151  }
 152  
 153  /**
 154   * Get the query part responsible for JOINing objects to user IDs
 155   *
 156   * @since 2.6.0 bbPress (r6747)
 157   *
 158   * @param array  $args      Default query arguments
 159   * @param string $context   Additional context
 160   * @param string $rel_key   The relationship key
 161   * @param string $rel_type  The relationship type (usually 'post')
 162   *
 163   * @return array
 164   */
 165  function bbp_get_user_object_query( $args = array(), $context = '', $rel_key = '', $rel_type = 'post' ) {
 166      $retval = bbp_user_engagements_interface( $rel_key, $rel_type )->get_query( $args, "get_user_{$context}", $rel_key, $rel_type );
 167  
 168      // Filter & return
 169      return (array) apply_filters( 'bbp_get_user_object_query', $retval, $args, $context, $rel_key, $rel_type );
 170  }
 171  
 172  /** Engagements ***************************************************************/
 173  
 174  /**
 175   * Get the users who have engaged in a topic
 176   *
 177   * @since 2.6.0 bbPress (r6320)
 178   *
 179   * @param int $topic_id Optional. Topic id
 180   *
 181   * @return array|bool Results if the topic has any engagements, otherwise false
 182   */
 183  function bbp_get_topic_engagements( $topic_id = 0 ) {
 184      $topic_id = bbp_get_topic_id( $topic_id );
 185      $users    = bbp_get_users_for_object( $topic_id, '_bbp_engagement' );
 186  
 187      // Filter & return
 188      return (array) apply_filters( 'bbp_get_topic_engagements', $users, $topic_id );
 189  }
 190  
 191  /**
 192   * Return the users who have engaged in a topic, directly with a database query
 193   *
 194   * See: https://bbpress.trac.wordpress.org/ticket/3083
 195   *
 196   * @since 2.6.0 bbPress (r6522)
 197   *
 198   * @param int $topic_id
 199   *
 200   * @return array
 201   */
 202  function bbp_get_topic_engagements_raw( $topic_id = 0 ) {
 203  
 204      // Default variables
 205      $topic_id = bbp_get_topic_id( $topic_id );
 206      $bbp_db   = bbp_db();
 207      $statii   = "'" . implode( "', '", bbp_get_public_topic_statuses() ) . "'";
 208  
 209      // A cool UNION query!
 210      $sql = "
 211  SELECT DISTINCT( post_author ) FROM (
 212      SELECT post_author FROM {$bbp_db->posts}
 213          WHERE ( ID = %d AND post_status IN ({$statii}) AND post_type = %s )
 214  UNION
 215      SELECT post_author FROM {$bbp_db->posts}
 216          WHERE ( post_parent = %d AND post_status = %s AND post_type = %s )
 217  ) as u1";
 218  
 219      // Prepare & get results
 220      $query   = $bbp_db->prepare( $sql, $topic_id, bbp_get_topic_post_type(), $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type() );
 221      $results = $bbp_db->get_col( $query );
 222  
 223      // Parse results into voices
 224      $engagements = ! is_wp_error( $results )
 225          ? wp_parse_id_list( array_filter( $results ) )
 226          : array();
 227  
 228      // Filter & return
 229      return (array) apply_filters( 'bbp_get_topic_engagements_raw', $engagements, $topic_id );
 230  }
 231  
 232  /**
 233   * Get a user's topic engagements
 234   *
 235   * @since 2.6.0 bbPress (r6320)
 236   * @since 2.6.0 bbPress (r6618) Signature changed to accept an array of arguments
 237   *
 238   * @param array $args Optional. Arguments to pass into bbp_has_replies()
 239   *
 240   * @return bool True if user has engaged, otherwise false
 241   */
 242  function bbp_get_user_engagements( $args = array() ) {
 243      $r     = bbp_get_user_object_query( $args, 'engagements', '_bbp_engagement' );
 244      $query = bbp_has_topics( $r );
 245  
 246      // Filter & return
 247      return apply_filters( 'bbp_get_user_engagements', $query, 0, $r, $args );
 248  }
 249  
 250  /**
 251   * Check if a user is engaged in a topic or not
 252   *
 253   * @since 2.6.0 bbPress (r6320)
 254   *
 255   * @param int $user_id Optional. User id
 256   * @param int $topic_id Optional. Topic id
 257   *
 258   * @return bool True if the topic is in user's engagements, otherwise false
 259   */
 260  function bbp_is_user_engaged( $user_id = 0, $topic_id = 0 ) {
 261      $user_id  = bbp_get_user_id( $user_id, true, true );
 262      $topic_id = bbp_get_topic_id( $topic_id );
 263      $retval   = bbp_is_object_of_user( $topic_id, $user_id, '_bbp_engagement' );
 264  
 265      // Filter & return
 266      return (bool) apply_filters( 'bbp_is_user_engaged', $retval, $user_id, $topic_id );
 267  }
 268  
 269  /**
 270   * Add a topic to user's engagements
 271   *
 272   * Note that both the User and Topic should be verified to exist before using
 273   * this function. Originally both were validated, but because this function is
 274   * frequently used within a loop, those verifications were moved upstream to
 275   * improve performance on topics with many engaged users.
 276   *
 277   * @since 2.6.0 bbPress (r6320)
 278   *
 279   * @param int $user_id Optional. User id
 280   * @param int $topic_id Optional. Topic id
 281   *
 282   * @return bool Always true
 283   */
 284  function bbp_add_user_engagement( $user_id = 0, $topic_id = 0 ) {
 285  
 286      // Bail if not enough info
 287      if ( empty( $user_id ) || empty( $topic_id ) ) {
 288          return false;
 289      }
 290  
 291      // Bail if already a engaged
 292      if ( bbp_is_user_engaged( $user_id, $topic_id ) ) {
 293          return false;
 294      }
 295  
 296      // Bail if add fails
 297      if ( ! bbp_add_user_to_object( $topic_id, $user_id, '_bbp_engagement' ) ) {
 298          return false;
 299      }
 300  
 301      do_action( 'bbp_add_user_engagement', $user_id, $topic_id );
 302  
 303      return true;
 304  }
 305  
 306  /**
 307   * Remove a topic from user's engagements
 308   *
 309   * @since 2.6.0 bbPress (r6320)
 310   *
 311   * @param int $user_id Optional. User id
 312   * @param int $topic_id Optional. Topic id
 313   *
 314   * @return bool True if the topic was removed from user's engagements, otherwise
 315   *               false
 316   */
 317  function bbp_remove_user_engagement( $user_id = 0, $topic_id = 0 ) {
 318  
 319      // Bail if not enough info
 320      if ( empty( $user_id ) || empty( $topic_id ) ) {
 321          return false;
 322      }
 323  
 324      // Bail if not already engaged
 325      if ( ! bbp_is_user_engaged( $user_id, $topic_id ) ) {
 326          return false;
 327      }
 328  
 329      // Bail if remove fails
 330      if ( ! bbp_remove_user_from_object( $topic_id, $user_id, '_bbp_engagement' ) ) {
 331          return false;
 332      }
 333  
 334      do_action( 'bbp_remove_user_engagement', $user_id, $topic_id );
 335  
 336      return true;
 337  }
 338  
 339  /**
 340   * Recalculate all of the users who have engaged in a topic.
 341   *
 342   * This happens when permanently deleting a reply, because that reply author may
 343   * have authored other replies to that same topic, or the topic itself.
 344   *
 345   * You may need to do this manually on heavily active forums where engagement
 346   * count accuracy is important.
 347   *
 348   * @since 2.6.0 bbPress (r6522)
 349   *
 350   * @param int  $topic_id
 351   * @param bool $force
 352   *
 353   * @return boolean True if any engagements are added, false otherwise
 354   */
 355  function bbp_recalculate_topic_engagements( $topic_id = 0, $force = false ) {
 356  
 357      // Default return value
 358      $retval = false;
 359  
 360      // Check post type
 361      $topic_id = bbp_is_reply( $topic_id )
 362          ? bbp_get_reply_topic_id( $topic_id )
 363          : bbp_get_topic_id( $topic_id );
 364  
 365      // Bail if no topic ID
 366      if ( empty( $topic_id ) ) {
 367          return $retval;
 368      }
 369  
 370      // Query for engagements
 371      $old_engagements = bbp_get_topic_engagements( $topic_id );
 372      $new_engagements = bbp_get_topic_engagements_raw( $topic_id );
 373  
 374      // Sort arrays
 375      sort( $old_engagements, SORT_NUMERIC );
 376      sort( $new_engagements, SORT_NUMERIC );
 377  
 378      // Only recalculate on change
 379      if ( ( true === $force ) || ( $old_engagements !== $new_engagements ) ) {
 380  
 381          // Delete all engagements
 382          bbp_remove_object_from_all_users( $topic_id, '_bbp_engagement' );
 383  
 384          // Update the voice count for this topic id
 385          foreach ( $new_engagements as $user_id ) {
 386              $retval = bbp_add_user_engagement( $user_id, $topic_id );
 387          }
 388      }
 389  
 390      // Filter & return
 391      return (bool) apply_filters( 'bbp_recalculate_user_engagements', $retval, $topic_id );
 392  }
 393  
 394  /**
 395   * Update the engagements of a topic.
 396   *
 397   * Hooked to 'bbp_new_topic' and 'bbp_new_reply', this gets the post author and
 398   * if not anonymous, passes it into bbp_add_user_engagement().
 399   *
 400   * @since 2.6.0 bbPress (r6526)
 401   *
 402   * @param int $topic_id
 403   */
 404  function bbp_update_topic_engagements( $topic_id = 0 ) {
 405  
 406      // Is a reply
 407      if ( bbp_is_reply( $topic_id ) ) {
 408  
 409          // Bail if reply isn't published
 410          if ( ! bbp_is_reply_published( $topic_id ) ) {
 411              return;
 412          }
 413  
 414          $author_id = bbp_get_reply_author_id( $topic_id );
 415          $topic_id  = bbp_get_reply_topic_id( $topic_id );
 416  
 417      // Is a topic
 418      } elseif ( bbp_is_topic( $topic_id ) ) {
 419          $author_id = bbp_get_topic_author_id( $topic_id );
 420          $topic_id  = bbp_get_topic_id( $topic_id );
 421  
 422      // Is unknown
 423      } else {
 424          return;
 425      }
 426  
 427      // Bail if topic is not public
 428      if ( ! bbp_is_topic_public( $topic_id ) ) {
 429          return;
 430      }
 431  
 432      // Return whether engagement was added
 433      return bbp_add_user_engagement( $author_id, $topic_id );
 434  }
 435  
 436  /** Favorites *****************************************************************/
 437  
 438  /**
 439   * Get the users who have made the topic favorite
 440   *
 441   * @since 2.0.0 bbPress (r2658)
 442   *
 443   * @param int $topic_id Optional. Topic id
 444   *
 445   * @return array|bool Results if the topic has any favoriters, otherwise false
 446   */
 447  function bbp_get_topic_favoriters( $topic_id = 0 ) {
 448      $topic_id = bbp_get_topic_id( $topic_id );
 449      $users    = bbp_get_users_for_object( $topic_id, '_bbp_favorite' );
 450  
 451      // Filter & return
 452      return (array) apply_filters( 'bbp_get_topic_favoriters', $users, $topic_id );
 453  }
 454  
 455  /**
 456   * Get a user's favorite topics
 457   *
 458   * @since 2.0.0 bbPress (r2652)
 459   * @since 2.6.0 bbPress (r6618) Signature changed to accept an array of arguments
 460   *
 461   * @param array $args Optional. Arguments to pass into bbp_has_topics()
 462   *
 463   * @return array Array of topics if user has favorites, otherwise empty array
 464   */
 465  function bbp_get_user_favorites( $args = array() ) {
 466      $r     = bbp_get_user_object_query( $args, 'favorites', '_bbp_favorite' );
 467      $query = ! empty( $r )
 468          ? bbp_has_topics( $r )
 469          : array();
 470  
 471      // Filter & return
 472      return apply_filters( 'bbp_get_user_favorites', $query, 0, $r, $args );
 473  }
 474  
 475  /**
 476   * Check if a topic is in user's favorites or not
 477   *
 478   * @since 2.0.0 bbPress (r2652)
 479   *
 480   * @param int $user_id Optional. User id
 481   * @param int $topic_id Optional. Topic id
 482   *
 483   * @return bool True if the topic is in user's favorites, otherwise false
 484   */
 485  function bbp_is_user_favorite( $user_id = 0, $topic_id = 0 ) {
 486      $retval = bbp_is_object_of_user( $topic_id, $user_id, '_bbp_favorite' );
 487  
 488      // Filter & return
 489      return (bool) apply_filters( 'bbp_is_user_favorite', $retval, $user_id, $topic_id );
 490  }
 491  
 492  /**
 493   * Add a topic to user's favorites
 494   *
 495   * Note that both the User and Topic should be verified to exist before using
 496   * this function. Originally both were validated, but because this function is
 497   * frequently used within a loop, those verifications were moved upstream to
 498   * improve performance on topics with many engaged users.
 499   *
 500   * @since 2.0.0 bbPress (r2652)
 501   *
 502   * @param int $user_id Optional. User id
 503   * @param int $topic_id Optional. Topic id
 504   *
 505   * @return bool True if the topic was added to user's favorites, otherwise false
 506   */
 507  function bbp_add_user_favorite( $user_id = 0, $topic_id = 0 ) {
 508      $user_id  = bbp_get_user_id( $user_id, false, false );
 509      $topic_id = bbp_get_topic_id( $topic_id );
 510  
 511      // Bail if not enough info
 512      if ( empty( $user_id ) || empty( $topic_id ) ) {
 513          return false;
 514      }
 515  
 516      // Bail if already a favorite
 517      if ( bbp_is_user_favorite( $user_id, $topic_id ) ) {
 518          return false;
 519      }
 520  
 521      // Bail if add fails
 522      if ( ! bbp_add_user_to_object( $topic_id, $user_id, '_bbp_favorite' ) ) {
 523          return false;
 524      }
 525  
 526      do_action( 'bbp_add_user_favorite', $user_id, $topic_id );
 527  
 528      return true;
 529  }
 530  
 531  /**
 532   * Remove a topic from user's favorites
 533   *
 534   * @since 2.0.0 bbPress (r2652)
 535   *
 536   * @param int $user_id Optional. User id
 537   * @param int $topic_id Optional. Topic id
 538   *
 539   * @return bool True if the topic was removed from user's favorites, otherwise false
 540   */
 541  function bbp_remove_user_favorite( $user_id, $topic_id ) {
 542      $user_id  = bbp_get_user_id( $user_id, false, false );
 543      $topic_id = bbp_get_topic_id( $topic_id );
 544  
 545      // Bail if not enough info
 546      if ( empty( $user_id ) || empty( $topic_id ) ) {
 547          return false;
 548      }
 549  
 550      // Bail if not already a favorite
 551      if ( ! bbp_is_user_favorite( $user_id, $topic_id ) ) {
 552          return false;
 553      }
 554  
 555      // Bail if remove fails
 556      if ( ! bbp_remove_user_from_object( $topic_id, $user_id, '_bbp_favorite' ) ) {
 557          return false;
 558      }
 559  
 560      do_action( 'bbp_remove_user_favorite', $user_id, $topic_id );
 561  
 562      return true;
 563  }
 564  
 565  /**
 566   * Handles the front end adding and removing of favorite topics
 567   *
 568   * @param string $action The requested action to compare this function to
 569   */
 570  function bbp_favorites_handler( $action = '' ) {
 571  
 572      // Default
 573      $success = false;
 574  
 575      // Bail if favorites not active
 576      if ( ! bbp_is_favorites_active() ) {
 577          return $success;
 578      }
 579  
 580      // Bail if no topic ID is passed
 581      if ( empty( $_GET['object_id'] ) ) {
 582          return $success;
 583      }
 584  
 585      // Setup possible get actions
 586      $possible_actions = array(
 587          'bbp_favorite_add',
 588          'bbp_favorite_remove',
 589      );
 590  
 591      // Bail if actions aren't meant for this function
 592      if ( ! in_array( $action, $possible_actions, true ) ) {
 593          return $success;
 594      }
 595  
 596      // What action is taking place?
 597      $topic_id = bbp_get_topic_id( $_GET['object_id'] );
 598      $user_id  = bbp_get_user_id( 0, true, true );
 599  
 600      // Check for empty topic
 601      if ( empty( $topic_id ) ) {
 602          bbp_add_error( 'bbp_favorite_topic_id', __( '<strong>ERROR</strong>: No topic was found. Which topic are you marking/unmarking as favorite?', 'bbpress' ) );
 603  
 604      // Check nonce
 605      } elseif ( ! bbp_verify_nonce_request( 'toggle-favorite_' . $topic_id ) ) {
 606          bbp_add_error( 'bbp_favorite_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
 607  
 608      // Check current user's ability to edit the user
 609      } elseif ( ! current_user_can( 'edit_user', $user_id ) ) {
 610          bbp_add_error( 'bbp_favorite_permission', __( '<strong>ERROR</strong>: You do not have permission to edit favorites for that user!.', 'bbpress' ) );
 611      }
 612  
 613      // Bail if errors
 614      if ( bbp_has_errors() ) {
 615          return $success;
 616      }
 617  
 618      /** No errors *************************************************************/
 619  
 620      if ( 'bbp_favorite_remove' === $action ) {
 621          $success = bbp_remove_user_favorite( $user_id, $topic_id );
 622      } elseif ( 'bbp_favorite_add' === $action ) {
 623          $success = bbp_add_user_favorite( $user_id, $topic_id );
 624      }
 625  
 626      // Do additional favorites actions
 627      do_action( 'bbp_favorites_handler', $success, $user_id, $topic_id, $action );
 628  
 629      // Success!
 630      if ( true === $success ) {
 631  
 632          // Redirect back from whence we came
 633          if ( ! empty( $_REQUEST['redirect_to'] ) ) {
 634              $redirect = $_REQUEST['redirect_to']; // Validated later
 635          } elseif ( bbp_is_favorites() ) {
 636              $redirect = bbp_get_favorites_permalink( $user_id, true );
 637          } elseif ( bbp_is_single_user() ) {
 638              $redirect = bbp_get_user_profile_url();
 639          } elseif ( is_singular( bbp_get_topic_post_type() ) ) {
 640              $redirect = bbp_get_topic_permalink( $topic_id );
 641          } elseif ( is_single() || is_page() ) {
 642              $redirect = get_permalink();
 643          } else {
 644              $redirect = get_permalink( $topic_id );
 645          }
 646  
 647          bbp_redirect( $redirect );
 648  
 649      // Fail! Handle errors
 650      } elseif ( 'bbp_favorite_remove' === $action ) {
 651          bbp_add_error( 'bbp_favorite_remove', __( '<strong>ERROR</strong>: There was a problem removing that topic from favorites.', 'bbpress' ) );
 652      } elseif ( 'bbp_favorite_add' === $action ) {
 653          bbp_add_error( 'bbp_favorite_add',    __( '<strong>ERROR</strong>: There was a problem favoriting that topic.', 'bbpress' ) );
 654      }
 655  
 656      return (bool) $success;
 657  }
 658  
 659  /** Subscriptions *************************************************************/
 660  
 661  /**
 662   * Get the users who have subscribed
 663   *
 664   * @since 2.6.0 bbPress (r5156)
 665   *
 666   * @param int $object_id Optional. ID of object (forum, topic, or something else)
 667   */
 668  function bbp_get_subscribers( $object_id = 0, $type = 'post' ) {
 669      $users = bbp_get_users_for_object( $object_id, '_bbp_subscription', $type );
 670  
 671      // Filter & return
 672      return (array) apply_filters( 'bbp_get_subscribers', $users, $object_id, $type );
 673  }
 674  
 675  /**
 676   * Get a user's subscribed topics
 677   *
 678   * @since 2.0.0 bbPress (r2668)
 679   * @since 2.6.0 bbPress (r6618) Signature changed to accept an array of arguments
 680   *
 681   * @param array $args Optional. Arguments to pass into bbp_has_topics()
 682   *
 683   * @return array Array of topics if user has topic subscriptions, otherwise empty array
 684   */
 685  function bbp_get_user_topic_subscriptions( $args = array() ) {
 686      $r     = bbp_get_user_object_query( $args, 'topic_subscriptions', '_bbp_subscription' );
 687      $query = ! empty( $r )
 688          ? bbp_has_topics( $r )
 689          : array();
 690  
 691      // Filter & return
 692      return apply_filters( 'bbp_get_user_topic_subscriptions', $query, 0, $r, $args );
 693  }
 694  
 695  /**
 696   * Get a user's subscribed forums
 697   *
 698   * @since 2.5.0 bbPress (r5156)
 699   * @since 2.6.0 bbPress (r6618) Signature changed to accept an array of arguments
 700   *
 701   * @param array $args Optional. Arguments to pass into bbp_has_forums()
 702   *
 703   * @return array Array of forums if user has forum subscriptions, otherwise empty array
 704   */
 705  function bbp_get_user_forum_subscriptions( $args = array() ) {
 706      $r     = bbp_get_user_object_query( $args, 'forum_subscriptions', '_bbp_subscription' );
 707      $query = ! empty( $r )
 708          ? bbp_has_forums( $r )
 709          : array();
 710  
 711      // Filter & return
 712      return apply_filters( 'bbp_get_user_forum_subscriptions', $query, 0, $r, $args );
 713  }
 714  
 715  /**
 716   * Check if an object (forum or topic) is in user's subscription list or not
 717   *
 718   * @since 2.5.0 bbPress (r5156)
 719   *
 720   * @param int $user_id Optional. User id
 721   * @param int $object_id Optional. Object id
 722   *
 723   * @return bool True if the object (forum or topic) is in user's subscriptions, otherwise false
 724   */
 725  function bbp_is_user_subscribed( $user_id = 0, $object_id = 0, $type = 'post' ) {
 726      $retval = bbp_is_object_of_user( $object_id, $user_id, '_bbp_subscription', $type );
 727  
 728      // Filter & return
 729      return (bool) apply_filters( 'bbp_is_user_subscribed', $retval, $user_id, $object_id, $type );
 730  }
 731  
 732  /**
 733   * Add a user subscription
 734   *
 735   * @since 2.5.0 bbPress (r5156)
 736   * @since 2.6.0 bbPress (r6544) Added $type parameter
 737   *
 738   * @param int    $user_id   Optional. User id
 739   * @param int    $object_id Optional. Object id
 740   * @param string $type      Optional. Type of object being subscribed to
 741   *
 742   * @return bool True if the object was added to user subscriptions, otherwise false
 743   */
 744  function bbp_add_user_subscription( $user_id = 0, $object_id = 0, $type = 'post' ) {
 745  
 746      // Bail if not enough info
 747      if ( empty( $user_id ) || empty( $object_id ) ) {
 748          return false;
 749      }
 750  
 751      // Bail if already subscribed
 752      if ( bbp_is_user_subscribed( $user_id, $object_id, $type ) ) {
 753          return false;
 754      }
 755  
 756      // Bail if add fails
 757      if ( ! bbp_add_user_to_object( $object_id, $user_id, '_bbp_subscription', $type ) ) {
 758          return false;
 759      }
 760  
 761      do_action( 'bbp_add_user_subscription', $user_id, $object_id, $type );
 762  
 763      return true;
 764  }
 765  
 766  /**
 767   * Remove a user subscription
 768   *
 769   * @since 2.5.0 bbPress (r5156)
 770   * @since 2.6.0 bbPress (r6544) Added $type parameter
 771   *
 772   * @param int    $user_id   Optional. User id
 773   * @param int    $object_id Optional. Object id
 774   * @param string $type      Optional. Type of object being subscribed to
 775   *
 776   * @return bool True if the object was removed from user subscriptions, otherwise false
 777   */
 778  function bbp_remove_user_subscription( $user_id = 0, $object_id = 0, $type = 'post' ) {
 779  
 780      // Bail if not enough info
 781      if ( empty( $user_id ) || empty( $object_id ) ) {
 782          return false;
 783      }
 784  
 785      // Bail if not subscribed
 786      if ( ! bbp_is_user_subscribed( $user_id, $object_id, $type ) ) {
 787          return false;
 788      }
 789  
 790      // Bail if remove fails
 791      if ( ! bbp_remove_user_from_object( $object_id, $user_id, '_bbp_subscription', $type ) ) {
 792          return false;
 793      }
 794  
 795      do_action( 'bbp_remove_user_subscription', $user_id, $object_id, $type );
 796  
 797      return true;
 798  }
 799  
 800  /**
 801   * Handles the front end toggling of user subscriptions
 802   *
 803   * @since 2.0.0 bbPress (r2790)
 804   * @since 2.6.l bbPress (r6543)
 805   *
 806   * @param string $action The requested action to compare this function to
 807   */
 808  function bbp_subscriptions_handler( $action = '' ) {
 809  
 810      // Default
 811      $success = false;
 812  
 813      // Bail if subscriptions not active
 814      if ( ! bbp_is_subscriptions_active() ) {
 815          return $success;
 816      }
 817  
 818      // Bail if no object ID is passed
 819      if ( empty( $_GET['object_id'] ) ) {
 820          return $success;
 821      }
 822  
 823      // Setup possible get actions
 824      $possible_actions = array(
 825          'bbp_subscribe',
 826          'bbp_unsubscribe'
 827      );
 828  
 829      // Bail if actions aren't meant for this function
 830      if ( ! in_array( $action, $possible_actions, true ) ) {
 831          return $success;
 832      }
 833  
 834      // Get required data
 835      $user_id     = bbp_get_current_user_id();
 836      $object_id   = absint( $_GET['object_id'] );
 837      $object_type = ! empty( $_GET['object_type'] )
 838          ? sanitize_key( $_GET['object_type'] )
 839          : 'post';
 840  
 841      // Check for empty topic
 842      if ( empty( $object_id ) ) {
 843          bbp_add_error( 'bbp_subscription_object_id', __( '<strong>ERROR</strong>: Not found. What are you subscribing/unsubscribing to?', 'bbpress' ) );
 844  
 845      // Check nonce
 846      } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $object_id ) ) {
 847          bbp_add_error( 'bbp_subscription_object_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
 848  
 849      // Check current user's ability to edit the user
 850      } elseif ( ! current_user_can( 'edit_user', $user_id ) ) {
 851          bbp_add_error( 'bbp_subscription_permission', __( '<strong>ERROR</strong>: You do not have permission to edit subscriptions of that user.', 'bbpress' ) );
 852      }
 853  
 854      // Bail if we have errors
 855      if ( bbp_has_errors() ) {
 856          return $success;
 857      }
 858  
 859      /** No errors *************************************************************/
 860  
 861      if ( 'bbp_unsubscribe' === $action ) {
 862          $success = bbp_remove_user_subscription( $user_id, $object_id, $object_type );
 863      } elseif ( 'bbp_subscribe' === $action ) {
 864          $success = bbp_add_user_subscription( $user_id, $object_id, $object_type );
 865      }
 866  
 867      // Do additional subscriptions actions
 868      do_action( 'bbp_subscriptions_handler', $success, $user_id, $object_id, $action, $object_type );
 869  
 870      // Success!
 871      if ( true === $success ) {
 872  
 873          // Redirect back from whence we came
 874          if ( ! empty( $_REQUEST['redirect_to'] ) ) {
 875              $redirect = $_REQUEST['redirect_to']; // Validated later
 876          } elseif ( bbp_is_subscriptions() ) {
 877              $redirect = bbp_get_subscriptions_permalink( $user_id );
 878          } elseif ( bbp_is_single_user() ) {
 879              $redirect = bbp_get_user_profile_url();
 880          } elseif ( is_singular( bbp_get_topic_post_type() ) ) {
 881              $redirect = bbp_get_topic_permalink( $object_id );
 882          } elseif ( is_singular( bbp_get_forum_post_type() ) ) {
 883              $redirect = bbp_get_forum_permalink( $object_id );
 884          } elseif ( is_single() || is_page() ) {
 885              $redirect = get_permalink();
 886          } else {
 887              $redirect = get_permalink( $object_id );
 888          }
 889  
 890          bbp_redirect( $redirect );
 891  
 892      // Fail! Handle errors
 893      } elseif ( 'bbp_unsubscribe' === $action ) {
 894          bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing.', 'bbpress' ) );
 895      } elseif ( 'bbp_subscribe' === $action ) {
 896          bbp_add_error( 'bbp_subscribe',   __( '<strong>ERROR</strong>: There was a problem subscribing.', 'bbpress' ) );
 897      }
 898  
 899      return (bool) $success;
 900  }
 901  
 902  /** Query Helpers *************************************************************/
 903  
 904  /**
 905   * These functions are no longer used in bbPress due to general performance
 906   * concerns on large installations. They are provided here for convenience and
 907   * backwards compatibility only.
 908   */
 909  
 910  /**
 911   * Get a user's object IDs
 912   *
 913   * For the most part, you should not need to use this function, and may even
 914   * want to come up with a more efficient way to get IDs on your own. Nevertheless,
 915   * it is available here for your convenience, using the most efficient query
 916   * parameters available inside of the various query APIs.
 917   *
 918   * @since 2.6.0 bbPress (r6606)
 919   *
 920   * @param int    $user_id   The user id
 921   * @param string $rel_key   The relationship key
 922   * @param string $rel_type  The relationship type (usually 'post')
 923   * @param array  $args      The arguments to override defaults
 924   *
 925   * @return array|bool Results if user has objects, otherwise null
 926   */
 927  function bbp_get_user_object_ids( $args = array() ) {
 928      $object_ids = $defaults = array();
 929  
 930      // Parse arguments
 931      $r = bbp_parse_args( $args, array(
 932          'user_id'     => 0,
 933          'object_type' => bbp_get_topic_post_type(),
 934          'rel_key'     => '',
 935          'rel_type'    => 'post',
 936          'filter'      => 'user_object_ids',
 937          'args'        => array()
 938      ), 'get_user_object_ids' );
 939  
 940      // Sanitize arguments
 941      $r['user_id']     = bbp_get_user_id( $r['user_id'] );
 942      $r['rel_key']     = sanitize_key( $r['rel_key'] );
 943      $r['rel_type']    = sanitize_key( $r['rel_type'] );
 944      $r['object_type'] = sanitize_key( $r['object_type'] );
 945      $r['filter']      = sanitize_key( $r['filter'] );
 946  
 947      // Defaults
 948      if ( 'post' === $r['rel_type'] ) {
 949          $defaults = array(
 950              'fields'         => 'ids',
 951              'post_type'      => $r['object_type'],
 952              'posts_per_page' => -1,
 953              'meta_query'     => array( array(
 954                  'key'     => $r['rel_key'],
 955                  'value'   => $r['user_id'],
 956                  'compare' => 'NUMERIC'
 957              ),
 958  
 959              // Performance
 960              'nopaging'               => true,
 961              'suppress_filters'       => true,
 962              'update_post_term_cache' => false,
 963              'update_post_meta_cache' => false,
 964              'ignore_sticky_posts'    => true,
 965              'no_found_rows'          => true
 966          ) );
 967      }
 968  
 969      // Parse arguments
 970      $query_args = bbp_parse_args( $r['args'], $defaults, "get_{$r['filter']}_args" );
 971  
 972      // Queries
 973      if ( 'post' === $r['rel_type'] ) {
 974          $query      = new WP_Query( $query_args );
 975          $object_ids = $query->posts;
 976      }
 977  
 978      // Filter & return
 979      return (array) apply_filters( "bbp_get_{$r['filter']}", $object_ids, $r, $args );
 980  }
 981  
 982  /**
 983   * Get array of forum IDs that a user can moderate
 984   *
 985   * @since 2.6.0 bbPress (r5834)
 986   *
 987   * @param int $user_id User id.
 988   *
 989   * @return array Return array of forum ids, or empty array
 990   */
 991  function bbp_get_moderator_forum_ids( $user_id = 0 ) {
 992      return bbp_get_user_object_ids( array(
 993          'user_id'     => $user_id,
 994          'rel_key'     => '_bbp_moderator_id',
 995          'object_type' => bbp_get_forum_post_type(),
 996          'filter'      => 'moderator_forum_ids'
 997      ) );
 998  }
 999  
1000  /**
1001   * Get a user's engaged topic ids
1002   *
1003   * @since 2.6.0 bbPress (r6320)
1004   *
1005   * @param int $user_id Optional. User id
1006   *
1007   * @return array Return array of topic ids, or empty array
1008   */
1009  function bbp_get_user_engaged_topic_ids( $user_id = 0 ) {
1010      return bbp_get_user_object_ids( array(
1011          'user_id' => $user_id,
1012          'rel_key' => '_bbp_engagement',
1013          'filter'  => 'user_engaged_topic_ids'
1014      ) );
1015  }
1016  
1017  /**
1018   * Get a user's favorite topic ids
1019   *
1020   * @since 2.0.0 bbPress (r2652)
1021   *
1022   * @param int $user_id Optional. User id
1023   *
1024   * @return array Return array of favorite topic ids, or empty array
1025   */
1026  function bbp_get_user_favorites_topic_ids( $user_id = 0 ) {
1027      return bbp_get_user_object_ids( array(
1028          'user_id' => $user_id,
1029          'rel_key' => '_bbp_favorite',
1030          'filter'  => 'user_favorites_topic_ids'
1031      ) );
1032  }
1033  
1034  /**
1035   * Get a user's subscribed forum ids
1036   *
1037   * @since 2.5.0 bbPress (r5156)
1038   *
1039   * @param int $user_id Optional. User id
1040   *
1041   * @return array Return array of subscribed forum ids, or empty array
1042   */
1043  function bbp_get_user_subscribed_forum_ids( $user_id = 0 ) {
1044      return bbp_get_user_object_ids( array(
1045          'user_id'     => $user_id,
1046          'rel_key'     => '_bbp_subscription',
1047          'object_type' => bbp_get_forum_post_type(),
1048          'filter'      => 'user_subscribed_forum_ids'
1049      ) );
1050  }
1051  
1052  /**
1053   * Get a user's subscribed topic ids
1054   *
1055   * @since 2.0.0 bbPress (r2668)
1056   *
1057   * @param int $user_id Optional. User id
1058   *
1059   * @return array Return array of subscribed topic ids, or empty array
1060   */
1061  function bbp_get_user_subscribed_topic_ids( $user_id = 0 ) {
1062      return bbp_get_user_object_ids( array(
1063          'user_id' => $user_id,
1064          'rel_key' => '_bbp_subscription',
1065          'filter'  => 'user_subscribed_topic_ids'
1066      ) );
1067  }
1068  
1069  /** Deprecated ****************************************************************/
1070  
1071  /**
1072   * Get a user's subscribed topics
1073   *
1074   * @since 2.0.0 bbPress (r2668)
1075   * @deprecated 2.5.0 bbPress (r5156)
1076   *
1077   * @param int $user_id Optional. User id
1078   *
1079   * @return array|bool Results if user has subscriptions, otherwise false
1080   */
1081  function bbp_get_user_subscriptions( $user_id = 0 ) {
1082      _deprecated_function( __FUNCTION__, '2.5', 'bbp_get_user_topic_subscriptions()' );
1083      $query = bbp_get_user_topic_subscriptions( $user_id );
1084  
1085      // Filter & return
1086      return apply_filters( 'bbp_get_user_subscriptions', $query, $user_id );
1087  }
1088  
1089  /**
1090   * Get the users who have subscribed to the forum
1091   *
1092   * @since 2.5.0 bbPress (r5156)
1093   * @deprecated 2.6.0 bbPress (r6543)
1094   *
1095   * @param int $forum_id Optional. forum id
1096   *
1097   * @return array|bool Results if the forum has any subscribers, otherwise false
1098   */
1099  function bbp_get_forum_subscribers( $forum_id = 0 ) {
1100      $users = bbp_get_users_for_object( $forum_id, '_bbp_subscription' );
1101  
1102      // Filter & return
1103      return (array) apply_filters( 'bbp_get_forum_subscribers', $users, $forum_id );
1104  }
1105  
1106  /**
1107   * Get the users who have subscribed to the topic
1108   *
1109   * @since 2.0.0 bbPress (r2668)
1110   * @deprecated 2.6.0 bbPress (r6543)
1111   *
1112   * @param int $topic_id Optional. Topic id
1113   *
1114   * @return array|bool Results if the topic has any subscribers, otherwise false
1115   */
1116  function bbp_get_topic_subscribers( $topic_id = 0 ) {
1117      $users = bbp_get_users_for_object( $topic_id, '_bbp_subscription' );
1118  
1119      // Filter & return
1120      return (array) apply_filters( 'bbp_get_topic_subscribers', $users, $topic_id );
1121  }
1122  
1123  /**
1124   * Check if a forum is in user's subscription list or not
1125   *
1126   * @since 2.5.0 bbPress (r5156)
1127   * @deprecated 2.6.0 bbPress (r6543)
1128   *
1129   * @param int $user_id  Optional. User id
1130   * @param int $forum_id Optional. Forum id
1131   *
1132   * @return bool True if the forum is in user's subscriptions, otherwise false
1133   */
1134  function bbp_is_user_subscribed_to_forum( $user_id = 0, $forum_id = 0 ) {
1135      return bbp_is_user_subscribed( $user_id, $forum_id );
1136  }
1137  
1138  /**
1139   * Check if a topic is in user's subscription list or not
1140   *
1141   * @since 2.5.0 bbPress (r5156)
1142   * @deprecated 2.6.0 bbPress (r6543) Use bbp_is_user_subscribed()
1143   *
1144   * @param int $user_id Optional. User id
1145   * @param int $topic_id Optional. Topic id
1146   * @return bool True if the topic is in user's subscriptions, otherwise false
1147   */
1148  function bbp_is_user_subscribed_to_topic( $user_id = 0, $topic_id = 0 ) {
1149      return bbp_is_user_subscribed( $user_id, $topic_id );
1150  }
1151  
1152  /**
1153   * Remove a forum from user's subscriptions
1154   *
1155   * @since 2.5.0 bbPress (r5156)
1156   * @deprecated 2.6.0 bbPress (r6543)
1157   *
1158   * @param int $user_id Optional. User id
1159   * @param int $forum_id Optional. forum id
1160   * @return bool True if the forum was removed from user's subscriptions,
1161   *               otherwise false
1162   */
1163  function bbp_remove_user_forum_subscription( $user_id = 0, $forum_id = 0 ) {
1164      return bbp_remove_user_subscription( $user_id, $forum_id );
1165  }
1166  
1167  /**
1168   * Remove a topic from user's subscriptions
1169   *
1170   * @since 2.5.0 bbPress (r5156)
1171   * @deprecated 2.6.0 bbPress (r6543)
1172   *
1173   * @param int $user_id Optional. User id
1174   * @param int $topic_id Optional. Topic id
1175   * @return bool True if the topic was removed from user's subscriptions,
1176   *               otherwise false
1177   */
1178  function bbp_remove_user_topic_subscription( $user_id = 0, $topic_id = 0 ) {
1179      return bbp_remove_user_subscription( $user_id, $topic_id );
1180  }
1181  
1182  /**
1183   * Add a forum to user's subscriptions
1184   *
1185   * @since 2.5.0 bbPress (r5156)
1186   * @deprecated 2.6.0 bbPress (r6543)
1187   *
1188   * @param int $user_id Optional. User id
1189   * @param int $forum_id Optional. forum id
1190   * @return bool Always true
1191   */
1192  function bbp_add_user_forum_subscription( $user_id = 0, $forum_id = 0 ) {
1193      return bbp_add_user_subscription( $user_id, $forum_id );
1194  }
1195  
1196  /**
1197   * Add a topic to user's subscriptions
1198   *
1199   * Note that both the User and Topic should be verified to exist before using
1200   * this function. Originally both were validated, but because this function is
1201   * frequently used within a loop, those verifications were moved upstream to
1202   * improve performance on topics with many engaged users.
1203   *
1204   * @since 2.0.0 bbPress (r2668)
1205   * @deprecated 2.6.0 bbPress (r6543)
1206   *
1207   * @param int $user_id Optional. User id
1208   * @param int $topic_id Optional. Topic id
1209   * @return bool Always true
1210   */
1211  function bbp_add_user_topic_subscription( $user_id = 0, $topic_id = 0 ) {
1212      return bbp_add_user_subscription( $user_id, $topic_id );
1213  }
1214  
1215  /**
1216   * Handles the front end toggling of forum subscriptions
1217   *
1218   * @since 2.5.0 bbPress (r5156)
1219   * @deprecated 2.6.0 bbPress (r6543)
1220   */
1221  function bbp_forum_subscriptions_handler( $action = '' ) {
1222      return bbp_subscriptions_handler( $action );
1223  }


Generated: Tue Jul 16 01:01:32 2019 Cross-referenced by PHPXref 0.7.1