[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * bbPress User Options
   5   *
   6   * @package bbPress
   7   * @subpackage UserOptions
   8   */
   9  
  10  // Exit if accessed directly
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /**
  14   * Get the default user options and their values
  15   *
  16   * @since 2.1.0 bbPress (r3910)
  17   *
  18   * @return array Filtered user option names and values
  19   */
  20  function bbp_get_default_user_options() {
  21  
  22      // Filter & return
  23      return (array) apply_filters( 'bbp_get_default_user_options', array(
  24          '_bbp_last_posted' => '0', // For checking flooding
  25          '_bbp_topic_count' => '0', // Total topics per site
  26          '_bbp_reply_count' => '0'  // Total replies per site
  27      ) );
  28  }
  29  
  30  /**
  31   * Add default user options
  32   *
  33   * This is destructive, so existing bbPress user options will be overridden.
  34   *
  35   * @since 2.1.0 bbPress (r3910)
  36   */
  37  function bbp_add_user_options( $user_id = 0 ) {
  38  
  39      // Validate user id
  40      $user_id = bbp_get_user_id( $user_id );
  41      if ( empty( $user_id ) ) {
  42          return;
  43      }
  44  
  45      // Add default options
  46      foreach ( bbp_get_default_user_options() as $key => $value ) {
  47          update_user_option( $user_id, $key, $value );
  48      }
  49  
  50      // Allow previously activated plugins to append their own user options.
  51      do_action( 'bbp_add_user_options', $user_id );
  52  }
  53  
  54  /**
  55   * Delete default user options
  56   *
  57   * Hooked to bbp_uninstall, it is only called once when bbPress is uninstalled.
  58   * This is destructive, so existing bbPress user options will be destroyed.
  59   *
  60   * @since 2.1.0 bbPress (r3910)
  61   */
  62  function bbp_delete_user_options( $user_id = 0 ) {
  63  
  64      // Validate user id
  65      $user_id = bbp_get_user_id( $user_id );
  66      if ( empty( $user_id ) ) {
  67          return;
  68      }
  69  
  70      // Add default options
  71      foreach ( array_keys( bbp_get_default_user_options() ) as $key ) {
  72          delete_user_option( $user_id, $key );
  73      }
  74  
  75      // Allow previously activated plugins to append their own options.
  76      do_action( 'bbp_delete_user_options', $user_id );
  77  }
  78  
  79  /**
  80   * Add filters to each bbPress option and allow them to be overloaded from
  81   * inside the $bbp->options array.
  82   *
  83   * @since 2.1.0 bbPress (r3910)
  84   */
  85  function bbp_setup_user_option_filters() {
  86  
  87      // Add filters to each bbPress option
  88      foreach ( array_keys( bbp_get_default_user_options() ) as $key ) {
  89          add_filter( 'get_user_option_' . $key, 'bbp_filter_get_user_option', 10, 3 );
  90      }
  91  
  92      // Allow previously activated plugins to append their own options.
  93      do_action( 'bbp_setup_user_option_filters' );
  94  }
  95  
  96  /**
  97   * Filter default options and allow them to be overloaded from inside the
  98   * $bbp->user_options array.
  99   *
 100   * @since 2.1.0 bbPress (r3910)
 101   *
 102   * @param bool $value Optional. Default value false
 103   * @return mixed false if not overloaded, mixed if set
 104   */
 105  function bbp_filter_get_user_option( $value = false, $option = '', $user = 0 ) {
 106      $bbp = bbpress();
 107  
 108      // Check the options global for preset value
 109      if ( isset( $user->ID ) && isset( $bbp->user_options[ $user->ID ] ) && ! empty( $bbp->user_options[ $user->ID ][ $option ] ) ) {
 110          $value = $bbp->user_options[ $user->ID ][ $option ];
 111      }
 112  
 113      // Always return a value, even if false
 114      return $value;
 115  }
 116  
 117  /** Post Counts ***************************************************************/
 118  
 119  /**
 120   * Update the topic count for a user
 121   *
 122   * @since 2.6.0 bbPress (r5309)
 123   *
 124   * @param int $user_id
 125   * @param mixed $count
 126   * @return boolean
 127   */
 128  function bbp_update_user_topic_count( $user_id = 0, $count = false ) {
 129  
 130      // Validate user id
 131      $user_id = bbp_get_user_id( $user_id );
 132      if ( empty( $user_id ) ) {
 133          return false;
 134      }
 135  
 136      // Just in time filtering of the user's topic count
 137      $count = apply_filters( 'bbp_update_user_topic_count', $count, $user_id );
 138  
 139      // Bail if no count was passed
 140      if ( false === $count ) {
 141          return false;
 142      }
 143  
 144      // Return the updated user option
 145      return update_user_option( $user_id, '_bbp_topic_count', $count );
 146  }
 147  
 148  /**
 149   * Update the reply count for a user
 150   *
 151   * @since 2.6.0 bbPress (r5309)
 152   *
 153   * @param int $user_id
 154   * @param mixed $count
 155   * @return boolean
 156   */
 157  function bbp_update_user_reply_count( $user_id = 0, $count = false ) {
 158  
 159      // Validate user id
 160      $user_id = bbp_get_user_id( $user_id );
 161      if ( empty( $user_id ) ) {
 162          return false;
 163      }
 164  
 165      // Just in time filtering of the user's reply count
 166      $count = apply_filters( 'bbp_update_user_reply_count', $count, $user_id );
 167  
 168      // Bail if no count was passed
 169      if ( false === $count ) {
 170          return false;
 171      }
 172  
 173      // Return the updated user option
 174      return update_user_option( $user_id, '_bbp_reply_count', $count );
 175  }
 176  
 177  /**
 178   * Output a users topic count
 179   *
 180   * @since 2.1.0 bbPress (r3632)
 181   *
 182   * @param int $user_id
 183   * @param boolean $integer Optional. Whether or not to format the result
 184   *
 185   * @return string
 186   */
 187  function bbp_user_topic_count( $user_id = 0, $integer = false ) {
 188      echo esc_html( bbp_get_user_topic_count( $user_id, $integer ) );
 189  }
 190      /**
 191       * Return a users reply count
 192       *
 193       * @since 2.1.0 bbPress (r3632)
 194       *
 195       * @param int $user_id
 196       * @param boolean $integer Optional. Whether or not to format the result
 197       *
 198       * @return string
 199       */
 200  	function bbp_get_user_topic_count( $user_id = 0, $integer = false ) {
 201  
 202          // Validate user id
 203          $user_id = bbp_get_user_id( $user_id );
 204          if ( empty( $user_id ) ) {
 205              return false;
 206          }
 207  
 208          $count  = get_user_option( '_bbp_topic_count', $user_id );
 209          $filter = ( true === $integer )
 210              ? 'bbp_get_user_topic_count_int'
 211              : 'bbp_get_user_topic_count';
 212  
 213          // Filter & return
 214          return apply_filters( $filter, $count, $user_id );
 215      }
 216  
 217  /**
 218   * Output a users reply count
 219   *
 220   * @since 2.1.0 bbPress (r3632)
 221   *
 222   * @param int $user_id
 223   * @param boolean $integer Optional. Whether or not to format the result
 224   *
 225   * @return string
 226   */
 227  function bbp_user_reply_count( $user_id = 0, $integer = false ) {
 228      echo esc_html( bbp_get_user_reply_count( $user_id, $integer ) );
 229  }
 230      /**
 231       * Return a users reply count
 232       *
 233       * @since 2.1.0 bbPress (r3632)
 234       *
 235       * @param int $user_id
 236       * @param boolean $integer Optional. Whether or not to format the result
 237       *
 238       * @return string
 239       */
 240  	function bbp_get_user_reply_count( $user_id = 0, $integer = false ) {
 241  
 242          // Validate user id
 243          $user_id = bbp_get_user_id( $user_id );
 244          if ( empty( $user_id ) ) {
 245              return false;
 246          }
 247  
 248          $count  = get_user_option( '_bbp_reply_count', $user_id );
 249          $filter = ( true === $integer )
 250              ? 'bbp_get_user_reply_count_int'
 251              : 'bbp_get_user_reply_count';
 252  
 253          return apply_filters( $filter, $count, $user_id );
 254      }
 255  
 256  /**
 257   * Output a users total post count
 258   *
 259   * @since 2.1.0 bbPress (r3632)
 260   *
 261   * @param int $user_id
 262   * @param boolean $integer Optional. Whether or not to format the result
 263   *
 264   * @return string
 265   */
 266  function bbp_user_post_count( $user_id = 0, $integer = false ) {
 267      echo esc_html( bbp_get_user_post_count( $user_id, $integer ) );
 268  }
 269      /**
 270       * Return a users total post count
 271       *
 272       * @since 2.1.0 bbPress (r3632)
 273       *
 274       * @param int $user_id
 275       * @param boolean $integer Optional. Whether or not to format the result
 276       *
 277       * @return string
 278       */
 279  	function bbp_get_user_post_count( $user_id = 0, $integer = false ) {
 280  
 281          // Validate user id
 282          $user_id = bbp_get_user_id( $user_id );
 283          if ( empty( $user_id ) ) {
 284              return false;
 285          }
 286  
 287          $topics  = bbp_get_user_topic_count( $user_id, true );
 288          $replies = bbp_get_user_reply_count( $user_id, true );
 289          $count   = $topics + $replies;
 290          $filter  = ( true === $integer )
 291              ? 'bbp_get_user_post_count_int'
 292              : 'bbp_get_user_post_count';
 293  
 294          return apply_filters( $filter, $count, $user_id );
 295      }
 296  
 297  /** Last Posted ***************************************************************/
 298  
 299  /**
 300   * Update a users last posted time, for use with post throttling
 301   *
 302   * @since 2.1.0 bbPress (r3910)
 303   *
 304   * @param int $user_id User ID to update
 305   * @param int $time Time in time() format
 306   * @return bool False if no user or failure, true if successful
 307   */
 308  function bbp_update_user_last_posted( $user_id = 0, $time = 0 ) {
 309  
 310      // Validate user id
 311      $user_id = bbp_get_user_id( $user_id );
 312      if ( empty( $user_id ) ) {
 313          return false;
 314      }
 315  
 316      // Set time to now if nothing is passed
 317      if ( empty( $time ) ) {
 318          $time = time();
 319      }
 320  
 321      return update_user_option( $user_id, '_bbp_last_posted', $time );
 322  }
 323  
 324  /**
 325   * Output the raw value of the last posted time.
 326   *
 327   * @since 2.1.0 bbPress (r3910)
 328   *
 329   * @param int $user_id User ID to retrieve value for
 330   */
 331  function bbp_user_last_posted( $user_id = 0 ) {
 332      echo esc_html( bbp_get_user_last_posted( $user_id ) );
 333  }
 334  
 335      /**
 336       * Return the raw value of the last posted time.
 337       *
 338       * @since 2.1.0 bbPress (r3910)
 339       *
 340       * @param int $user_id User ID to retrieve value for
 341       * @return mixed False if no user, time() format if exists
 342       */
 343  	function bbp_get_user_last_posted( $user_id = 0 ) {
 344  
 345          // Validate user id
 346          $user_id = bbp_get_user_id( $user_id );
 347          if ( empty( $user_id ) ) {
 348              return false;
 349          }
 350  
 351          $time = get_user_option( '_bbp_last_posted', $user_id );
 352  
 353          // Filter & return
 354          return apply_filters( 'bbp_get_user_last_posted', $time, $user_id );
 355      }


Generated: Fri Oct 18 01:01:25 2019 Cross-referenced by PHPXref 0.7.1