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