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