PluginProbe
bbPress / 2.1
bbPress v2.1
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 2.2.2 All 71 releases
bbpress / bbp-includes / bbp-user-options.php

bbp-user-options.php in bbPress 2.1, at bbp-includes/bbp-user-options.php

282 lines 7.2 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 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_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' );
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' );
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 * @uses bbp_get_user_topic_count()
129 * @return string
130 */
131 function bbp_user_topic_count( $user_id = 0 ) {
132 echo bbp_get_user_topic_count( $user_id );
133 }
134 /**
135 * Return a users reply count
136 *
137 * @since bbPress (r3632)
138 *
139 * @param int $user_id
140 * @uses bbp_get_user_id()
141 * @uses get_user_option()
142 * @uses apply_filters()
143 * @return string
144 */
145 function bbp_get_user_topic_count( $user_id = 0 ) {
146
147 // Validate user id
148 $user_id = bbp_get_user_id( $user_id );
149 if ( empty( $user_id ) )
150 return false;
151
152 $count = get_user_option( '_bbp_topic_count', $user_id );
153
154 return apply_filters( 'bbp_get_user_topic_count', (int) $count, $user_id );
155 }
156
157 /**
158 * Output a users reply count
159 *
160 * @since bbPress (r3632)
161 *
162 * @param int $user_id
163 * @uses bbp_get_user_reply_count()
164 * @return string
165 */
166 function bbp_user_reply_count( $user_id = 0 ) {
167 echo bbp_get_user_reply_count( $user_id );
168 }
169 /**
170 * Return a users reply count
171 *
172 * @since bbPress (r3632)
173 *
174 * @param int $user_id
175 * @uses bbp_get_user_id()
176 * @uses get_user_option()
177 * @uses apply_filters()
178 * @return string
179 */
180 function bbp_get_user_reply_count( $user_id = 0 ) {
181
182 // Validate user id
183 $user_id = bbp_get_user_id( $user_id );
184 if ( empty( $user_id ) )
185 return false;
186
187 $count = get_user_option( '_bbp_reply_count', $user_id );
188
189 return apply_filters( 'bbp_get_user_reply_count', (int) $count, $user_id );
190 }
191
192 /**
193 * Output a users total post count
194 *
195 * @since bbPress (r3632)
196 *
197 * @param int $user_id
198 * @uses bbp_get_user_post_count()
199 * @return string
200 */
201 function bbp_user_post_count( $user_id = 0 ) {
202 echo bbp_get_user_post_count( $user_id );
203 }
204 /**
205 * Return a users total post count
206 *
207 * @since bbPress (r3632)
208 *
209 * @param int $user_id
210 * @uses bbp_get_user_id()
211 * @uses get_user_option()
212 * @uses apply_filters()
213 * @return string
214 */
215 function bbp_get_user_post_count( $user_id = 0 ) {
216
217 // Validate user id
218 $user_id = bbp_get_user_id( $user_id );
219 if ( empty( $user_id ) )
220 return false;
221
222 $topics = bbp_get_user_topic_count( $user_id );
223 $replies = bbp_get_user_reply_count( $user_id );
224 $count = (int) $topics + (int) $replies;
225
226 return apply_filters( 'bbp_get_user_post_count', (int) $count, $user_id );
227 }
228
229 /** Last Posted ***************************************************************/
230
231 /**
232 * Update a users last posted time, for use with post throttling
233 *
234 * @since bbPress (r3910)
235 * @param int $user_id User ID to update
236 * @param int $time Time in time() format
237 * @return bool False if no user or failure, true if successful
238 */
239 function bbp_update_user_last_posted( $user_id = 0, $time = 0 ) {
240
241 // Validate user id
242 $user_id = bbp_get_user_id( $user_id );
243 if ( empty( $user_id ) )
244 return false;
245
246 // Set time to now if nothing is passed
247 if ( empty( $time ) )
248 $time = time();
249
250 return update_user_option( $user_id, '_bbp_last_posted', $time );
251 }
252
253 /**
254 * Output the raw value of the last posted time.
255 *
256 * @since bbPress (r3910)
257 * @param int $user_id User ID to retrieve value for
258 * @uses bbp_get_user_last_posted() To output the last posted time
259 */
260 function bbp_user_last_posted( $user_id = 0 ) {
261 echo bbp_get_user_last_posted( $user_id );
262 }
263
264 /**
265 * Return the raw value of teh last posted time.
266 *
267 * @since bbPress (r3910)
268 * @param int $user_id User ID to retrieve value for
269 * @return mixed False if no user, time() format if exists
270 */
271 function bbp_get_user_last_posted( $user_id = 0 ) {
272
273 // Validate user id
274 $user_id = bbp_get_user_id( $user_id );
275 if ( empty( $user_id ) )
276 return false;
277
278 $time = get_user_option( '_bbp_last_posted', $user_id );
279
280 return apply_filters( 'bbp_get_user_last_posted', $time, $user_id );
281 }
282