PluginProbe
bbPress / 2.6.17
bbPress v2.6.17
2.6.17 trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 2.2 2.2.1 All 72 releases
bbpress / includes / users / options.php

options.php in bbPress 2.6.17, at includes/users/options.php

383 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @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 /**
205 * Output a users topic count
206 *
207 * @since 2.1.0 bbPress (r3632)
208 *
209 * @param int $user_id
210 * @param boolean $integer Optional. Whether or not to format the result
211 *
212 * @return string
213 */
214 function bbp_user_topic_count( $user_id = 0, $integer = false ) {
215 echo esc_html( bbp_get_user_topic_count( $user_id, $integer ) );
216 }
217 /**
218 * Return 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_get_user_topic_count( $user_id = 0, $integer = false ) {
228
229 // Validate user id
230 $user_id = bbp_get_user_id( $user_id );
231 if ( empty( $user_id ) ) {
232 return false;
233 }
234
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';
239
240 // Filter & return
241 return apply_filters( $filter, $count, $user_id );
242 }
243
244 /**
245 * Output a users reply count
246 *
247 * @since 2.1.0 bbPress (r3632)
248 *
249 * @param int $user_id
250 * @param boolean $integer Optional. Whether or not to format the result
251 *
252 * @return string
253 */
254 function bbp_user_reply_count( $user_id = 0, $integer = false ) {
255 echo esc_html( bbp_get_user_reply_count( $user_id, $integer ) );
256 }
257 /**
258 * Return a users reply count
259 *
260 * @since 2.1.0 bbPress (r3632)
261 *
262 * @param int $user_id
263 * @param boolean $integer Optional. Whether or not to format the result
264 *
265 * @return string
266 */
267 function bbp_get_user_reply_count( $user_id = 0, $integer = false ) {
268
269 // Validate user id
270 $user_id = bbp_get_user_id( $user_id );
271 if ( empty( $user_id ) ) {
272 return false;
273 }
274
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';
279
280 return apply_filters( $filter, $count, $user_id );
281 }
282
283 /**
284 * Output a users total post count
285 *
286 * @since 2.1.0 bbPress (r3632)
287 *
288 * @param int $user_id
289 * @param boolean $integer Optional. Whether or not to format the result
290 *
291 * @return string
292 */
293 function bbp_user_post_count( $user_id = 0, $integer = false ) {
294 echo esc_html( bbp_get_user_post_count( $user_id, $integer ) );
295 }
296 /**
297 * Return a users total post count
298 *
299 * @since 2.1.0 bbPress (r3632)
300 *
301 * @param int $user_id
302 * @param boolean $integer Optional. Whether or not to format the result
303 *
304 * @return string
305 */
306 function bbp_get_user_post_count( $user_id = 0, $integer = false ) {
307
308 // Validate user id
309 $user_id = bbp_get_user_id( $user_id );
310 if ( empty( $user_id ) ) {
311 return false;
312 }
313
314 $topics = bbp_get_user_topic_count( $user_id, true );
315 $replies = bbp_get_user_reply_count( $user_id, true );
316 $count = $topics + $replies;
317 $filter = ( true === $integer )
318 ? 'bbp_get_user_post_count_int'
319 : 'bbp_get_user_post_count';
320
321 return apply_filters( $filter, $count, $user_id );
322 }
323
324 /** Last Posted ***************************************************************/
325
326 /**
327 * Update a users last posted time, for use with post throttling
328 *
329 * @since 2.1.0 bbPress (r3910)
330 *
331 * @param int $user_id User ID to update
332 * @param int $time Time in time() format
333 * @return bool False if no user or failure, true if successful
334 */
335 function bbp_update_user_last_posted( $user_id = 0, $time = 0 ) {
336
337 // Validate user id
338 $user_id = bbp_get_user_id( $user_id );
339 if ( empty( $user_id ) ) {
340 return false;
341 }
342
343 // Set time to now if nothing is passed
344 if ( empty( $time ) ) {
345 $time = time();
346 }
347
348 return update_user_option( $user_id, '_bbp_last_posted', $time );
349 }
350
351 /**
352 * Output the raw value of the last posted time.
353 *
354 * @since 2.1.0 bbPress (r3910)
355 *
356 * @param int $user_id User ID to retrieve value for
357 */
358 function bbp_user_last_posted( $user_id = 0 ) {
359 echo esc_html( bbp_get_user_last_posted( $user_id ) );
360 }
361
362 /**
363 * Return the raw value of the last posted time.
364 *
365 * @since 2.1.0 bbPress (r3910)
366 *
367 * @param int $user_id User ID to retrieve value for
368 * @return mixed False if no user, time() format if exists
369 */
370 function bbp_get_user_last_posted( $user_id = 0 ) {
371
372 // Validate user id
373 $user_id = bbp_get_user_id( $user_id );
374 if ( empty( $user_id ) ) {
375 return false;
376 }
377
378 $time = get_user_option( '_bbp_last_posted', $user_id );
379
380 // Filter & return
381 return apply_filters( 'bbp_get_user_last_posted', $time, $user_id );
382 }
383