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