|
1 <?php |
|
2 |
|
3 /** |
|
4 * bbPress User Functions |
|
5 * |
|
6 * @package bbPress |
|
7 * @subpackage Functions |
|
8 */ |
|
9 |
|
10 // Exit if accessed directly |
|
11 if ( !defined( 'ABSPATH' ) ) exit; |
|
12 |
|
13 /** |
|
14 * Redirect back to $url when attempting to use the login page |
|
15 * |
|
16 * @since bbPress (r2815) |
|
17 * |
|
18 * @param string $url The url |
|
19 * @param string $raw_url Raw url |
|
20 * @param object $user User object |
|
21 * @uses is_wp_error() To check if the user param is a {@link WP_Error} |
|
22 * @uses admin_url() To get the admin url |
|
23 * @uses home_url() To get the home url |
|
24 * @uses esc_url() To escape the url |
|
25 * @uses wp_safe_redirect() To redirect |
|
26 */ |
|
27 function bbp_redirect_login( $url = '', $raw_url = '', $user = '' ) { |
|
28 |
|
29 // Raw redirect_to was passed, so use it |
|
30 if ( !empty( $raw_url ) ) |
|
31 $url = $raw_url; |
|
32 |
|
33 // $url was manually set in wp-login.php to redirect to admin |
|
34 elseif ( admin_url() == $url ) |
|
35 $url = home_url(); |
|
36 |
|
37 // $url is empty |
|
38 elseif ( empty( $url ) ) |
|
39 $url = home_url(); |
|
40 |
|
41 return apply_filters( 'bbp_redirect_login', $url, $raw_url, $user ); |
|
42 } |
|
43 |
|
44 /** |
|
45 * Is an anonymous topic/reply being made? |
|
46 * |
|
47 * @since bbPres (r2688) |
|
48 * |
|
49 * @uses is_user_logged_in() Is the user logged in? |
|
50 * @uses bbp_allow_anonymous() Is anonymous posting allowed? |
|
51 * @uses apply_filters() Calls 'bbp_is_anonymous' with the return value |
|
52 * @return bool True if anonymous is allowed and user is not logged in, false if |
|
53 * anonymous is not allowed or user is logged in |
|
54 */ |
|
55 function bbp_is_anonymous() { |
|
56 if ( !is_user_logged_in() && bbp_allow_anonymous() ) |
|
57 $is_anonymous = true; |
|
58 else |
|
59 $is_anonymous = false; |
|
60 |
|
61 return apply_filters( 'bbp_is_anonymous', $is_anonymous ); |
|
62 } |
|
63 |
|
64 /** |
|
65 * Echoes the values for current poster (uses WP comment cookies) |
|
66 * |
|
67 * @since bbPress (r2734) |
|
68 * |
|
69 * @param string $key Which value to echo? |
|
70 * @uses bbp_get_current_anonymous_user_data() To get the current anonymous user |
|
71 * data |
|
72 */ |
|
73 function bbp_current_anonymous_user_data( $key = '' ) { |
|
74 echo bbp_get_current_anonymous_user_data( $key ); |
|
75 } |
|
76 |
|
77 /** |
|
78 * Get the cookies for current poster (uses WP comment cookies). |
|
79 * |
|
80 * @since bbPress (r2734) |
|
81 * |
|
82 * @param string $key Optional. Which value to get? If not given, then |
|
83 * an array is returned. |
|
84 * @uses sanitize_comment_cookies() To sanitize the current poster data |
|
85 * @uses wp_get_current_commenter() To get the current poster data * |
|
86 * @return string|array Cookie(s) for current poster |
|
87 */ |
|
88 function bbp_get_current_anonymous_user_data( $key = '' ) { |
|
89 $cookie_names = array( |
|
90 'name' => 'comment_author', |
|
91 'email' => 'comment_author_email', |
|
92 'website' => 'comment_author_url', |
|
93 |
|
94 // Here just for the sake of them, use the above ones |
|
95 'comment_author' => 'comment_author', |
|
96 'comment_author_email' => 'comment_author_email', |
|
97 'comment_author_url' => 'comment_author_url', |
|
98 ); |
|
99 |
|
100 sanitize_comment_cookies(); |
|
101 |
|
102 $bbp_current_poster = wp_get_current_commenter(); |
|
103 |
|
104 if ( !empty( $key ) && in_array( $key, array_keys( $cookie_names ) ) ) |
|
105 return $bbp_current_poster[$cookie_names[$key]]; |
|
106 |
|
107 return $bbp_current_poster; |
|
108 } |
|
109 |
|
110 /** |
|
111 * Set the cookies for current poster (uses WP comment cookies) |
|
112 * |
|
113 * @since bbPress (r2734) |
|
114 * |
|
115 * @param array $anonymous_data With keys 'bbp_anonymous_name', |
|
116 * 'bbp_anonymous_email', 'bbp_anonymous_website'. |
|
117 * Should be sanitized (see |
|
118 * {@link bbp_filter_anonymous_post_data()} for |
|
119 * sanitization) |
|
120 * @uses apply_filters() Calls 'comment_cookie_lifetime' for cookie lifetime. |
|
121 * Defaults to 30000000. |
|
122 */ |
|
123 function bbp_set_current_anonymous_user_data( $anonymous_data = array() ) { |
|
124 if ( empty( $anonymous_data ) || !is_array( $anonymous_data ) ) |
|
125 return; |
|
126 |
|
127 $comment_cookie_lifetime = apply_filters( 'comment_cookie_lifetime', 30000000 ); |
|
128 |
|
129 setcookie( 'comment_author_' . COOKIEHASH, $anonymous_data['bbp_anonymous_name'], time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN ); |
|
130 setcookie( 'comment_author_email_' . COOKIEHASH, $anonymous_data['bbp_anonymous_email'], time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN ); |
|
131 setcookie( 'comment_author_url_' . COOKIEHASH, $anonymous_data['bbp_anonymous_website'], time() + $comment_cookie_lifetime, COOKIEPATH, COOKIE_DOMAIN ); |
|
132 } |
|
133 |
|
134 /** |
|
135 * Get the poster IP address |
|
136 * |
|
137 * @since bbPress (r3120) |
|
138 * |
|
139 * @return string |
|
140 */ |
|
141 function bbp_current_author_ip() { |
|
142 $retval = preg_replace( '/[^0-9a-fA-F:., ]/', '', $_SERVER['REMOTE_ADDR'] ); |
|
143 |
|
144 return apply_filters( 'bbp_current_author_ip', $retval ); |
|
145 } |
|
146 |
|
147 /** |
|
148 * Get the poster user agent |
|
149 * |
|
150 * @since bbPress (r3446) |
|
151 * |
|
152 * @return string |
|
153 */ |
|
154 function bbp_current_author_ua() { |
|
155 $retval = !empty( $_SERVER['HTTP_USER_AGENT'] ) ? substr( $_SERVER['HTTP_USER_AGENT'], 0, 254 ) : ''; |
|
156 |
|
157 return apply_filters( 'bbp_current_author_ua', $retval ); |
|
158 } |
|
159 |
|
160 /** Post Counts ***************************************************************/ |
|
161 |
|
162 /** |
|
163 * Return the raw database count of topics by a user |
|
164 * |
|
165 * @since bbPress (r3633) |
|
166 * @global WPDB $wpdb |
|
167 * @uses bbp_get_user_id() |
|
168 * @uses get_posts_by_author_sql() |
|
169 * @uses bbp_get_topic_post_type() |
|
170 * @uses apply_filters() |
|
171 * @return int Raw DB count of topics |
|
172 */ |
|
173 function bbp_get_user_topic_count_raw( $user_id = 0 ) { |
|
174 $user_id = bbp_get_user_id( $user_id ); |
|
175 if ( empty( $user_id ) ) |
|
176 return false; |
|
177 |
|
178 global $wpdb; |
|
179 |
|
180 $where = get_posts_by_author_sql( bbp_get_topic_post_type(), true, $user_id ); |
|
181 $count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} {$where}" ); |
|
182 |
|
183 return (int) apply_filters( 'bbp_get_user_topic_count_raw', $count, $user_id ); |
|
184 } |
|
185 |
|
186 /** |
|
187 * Return the raw database count of replies by a user |
|
188 * |
|
189 * @since bbPress (r3633) |
|
190 * @global WPDB $wpdb |
|
191 * @uses bbp_get_user_id() |
|
192 * @uses get_posts_by_author_sql() |
|
193 * @uses bbp_get_reply_post_type() |
|
194 * @uses apply_filters() |
|
195 * @return int Raw DB count of replies |
|
196 */ |
|
197 function bbp_get_user_reply_count_raw( $user_id = 0 ) { |
|
198 $user_id = bbp_get_user_id( $user_id ); |
|
199 if ( empty( $user_id ) ) |
|
200 return false; |
|
201 |
|
202 global $wpdb; |
|
203 |
|
204 $where = get_posts_by_author_sql( bbp_get_reply_post_type(), true, $user_id ); |
|
205 $count = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->posts} {$where}" ); |
|
206 |
|
207 return (int) apply_filters( 'bbp_get_user_reply_count_raw', $count, $user_id ); |
|
208 } |
|
209 |
|
210 /** Favorites *****************************************************************/ |
|
211 |
|
212 /** |
|
213 * Get the users who have made the topic favorite |
|
214 * |
|
215 * @since bbPress (r2658) |
|
216 * |
|
217 * @param int $topic_id Optional. Topic id |
|
218 * @uses wpdb::get_col() To execute our query and get the column back |
|
219 * @uses apply_filters() Calls 'bbp_get_topic_favoriters' with the users and |
|
220 * topic id |
|
221 * @return array|bool Results if the topic has any favoriters, otherwise false |
|
222 */ |
|
223 function bbp_get_topic_favoriters( $topic_id = 0 ) { |
|
224 if ( empty( $topic_id ) ) |
|
225 return; |
|
226 |
|
227 global $wpdb; |
|
228 |
|
229 // Get the users who have favorited the topic |
|
230 $key = $wpdb->prefix . '_bbp_favorites'; |
|
231 $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$topic_id}', meta_value) > 0" ); |
|
232 $users = apply_filters( 'bbp_get_topic_favoriters', $users, $topic_id ); |
|
233 |
|
234 if ( !empty( $users ) ) |
|
235 return $users; |
|
236 |
|
237 return false; |
|
238 } |
|
239 |
|
240 /** |
|
241 * Get a user's favorite topics |
|
242 * |
|
243 * @since bbPress (r2652) |
|
244 * |
|
245 * @param int $user_id Optional. User id |
|
246 * @uses bbp_get_user_favorites_topic_ids() To get the user's favorites |
|
247 * @uses bbp_has_topics() To get the topics |
|
248 * @uses apply_filters() Calls 'bbp_get_user_favorites' with the topic query and |
|
249 * user id |
|
250 * @return array|bool Results if user has favorites, otherwise false |
|
251 */ |
|
252 function bbp_get_user_favorites( $user_id = 0 ) { |
|
253 $user_id = bbp_get_user_id( $user_id ); |
|
254 if ( empty( $user_id ) ) |
|
255 return false; |
|
256 |
|
257 // If user has favorites, load them |
|
258 $favorites = bbp_get_user_favorites_topic_ids( $user_id ); |
|
259 if ( !empty( $favorites ) ) { |
|
260 |
|
261 // Setup the topics query |
|
262 $topics_query = bbp_has_topics( array( 'post__in' => $favorites ) ); |
|
263 |
|
264 return apply_filters( 'bbp_get_user_favorites', $topics_query, $user_id ); |
|
265 } |
|
266 |
|
267 return false; |
|
268 } |
|
269 |
|
270 /** |
|
271 * Get a user's favorite topics' ids |
|
272 * |
|
273 * @since bbPress (r2652) |
|
274 * |
|
275 * @param int $user_id Optional. User id |
|
276 * @uses bbp_get_user_id() To get the user id |
|
277 * @uses get_user_option() To get the user favorites |
|
278 * @uses apply_filters() Calls 'bbp_get_user_favorites_topic_ids' with |
|
279 * the favorites and user id |
|
280 * @return array|bool Results if user has favorites, otherwise false |
|
281 */ |
|
282 function bbp_get_user_favorites_topic_ids( $user_id = 0 ) { |
|
283 $user_id = bbp_get_user_id( $user_id ); |
|
284 if ( empty( $user_id ) ) |
|
285 return false; |
|
286 |
|
287 $favorites = (string) get_user_option( '_bbp_favorites', $user_id ); |
|
288 $favorites = (array) explode( ',', $favorites ); |
|
289 $favorites = array_filter( $favorites ); |
|
290 |
|
291 return apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites, $user_id ); |
|
292 } |
|
293 |
|
294 /** |
|
295 * Check if a topic is in user's favorites or not |
|
296 * |
|
297 * @since bbPress (r2652) |
|
298 * |
|
299 * @param int $user_id Optional. User id |
|
300 * @param int $topic_id Optional. Topic id |
|
301 * @uses bbp_get_user_id() To get the user id |
|
302 * @uses bbp_get_user_favorites_topic_ids() To get the user favorites |
|
303 * @uses bbp_get_topic() To get the topic |
|
304 * @uses bbp_get_topic_id() To get the topic id |
|
305 * @uses apply_filters() Calls 'bbp_is_user_favorite' with the bool, user id, |
|
306 * topic id and favorites |
|
307 * @return bool True if the topic is in user's favorites, otherwise false |
|
308 */ |
|
309 function bbp_is_user_favorite( $user_id = 0, $topic_id = 0 ) { |
|
310 |
|
311 $user_id = bbp_get_user_id( $user_id, true, true ); |
|
312 if ( empty( $user_id ) ) |
|
313 return false; |
|
314 |
|
315 $retval = false; |
|
316 $favorites = bbp_get_user_favorites_topic_ids( $user_id ); |
|
317 |
|
318 if ( !empty( $favorites ) ) { |
|
319 |
|
320 // Checking a specific topic id |
|
321 if ( !empty( $topic_id ) ) { |
|
322 $topic = bbp_get_topic( $topic_id ); |
|
323 $topic_id = !empty( $topic ) ? $topic->ID : 0; |
|
324 |
|
325 // Using the global topic id |
|
326 } elseif ( bbp_get_topic_id() ) { |
|
327 $topic_id = bbp_get_topic_id(); |
|
328 |
|
329 // Use the current post id |
|
330 } elseif ( !bbp_get_topic_id() ) { |
|
331 $topic_id = get_the_ID(); |
|
332 } |
|
333 |
|
334 // Is topic_id in the user's favorites |
|
335 if ( !empty( $topic_id ) ) { |
|
336 $retval = in_array( $topic_id, $favorites ); |
|
337 } |
|
338 } |
|
339 |
|
340 return (bool) apply_filters( 'bbp_is_user_favorite', (bool) $retval, $user_id, $topic_id, $favorites ); |
|
341 } |
|
342 |
|
343 /** |
|
344 * Add a topic to user's favorites |
|
345 * |
|
346 * @since bbPress (r2652) |
|
347 * |
|
348 * @param int $user_id Optional. User id |
|
349 * @param int $topic_id Optional. Topic id |
|
350 * @uses bbp_get_user_favorites_topic_ids() To get the user favorites |
|
351 * @uses update_user_option() To update the user favorites |
|
352 * @uses do_action() Calls 'bbp_add_user_favorite' with the user id and topic id |
|
353 * @return bool Always true |
|
354 */ |
|
355 function bbp_add_user_favorite( $user_id = 0, $topic_id = 0 ) { |
|
356 if ( empty( $user_id ) || empty( $topic_id ) ) |
|
357 return false; |
|
358 |
|
359 $favorites = (array) bbp_get_user_favorites_topic_ids( $user_id ); |
|
360 $topic = bbp_get_topic( $topic_id ); |
|
361 if ( empty( $topic ) ) |
|
362 return false; |
|
363 |
|
364 if ( !in_array( $topic_id, $favorites ) ) { |
|
365 $favorites[] = $topic_id; |
|
366 $favorites = array_filter( $favorites ); |
|
367 $favorites = (string) implode( ',', $favorites ); |
|
368 update_user_option( $user_id, '_bbp_favorites', $favorites ); |
|
369 } |
|
370 |
|
371 do_action( 'bbp_add_user_favorite', $user_id, $topic_id ); |
|
372 |
|
373 return true; |
|
374 } |
|
375 |
|
376 /** |
|
377 * Remove a topic from user's favorites |
|
378 * |
|
379 * @since bbPress (r2652) |
|
380 * |
|
381 * @param int $user_id Optional. User id |
|
382 * @param int $topic_id Optional. Topic id |
|
383 * @uses bbp_get_user_favorites_topic_ids() To get the user favorites |
|
384 * @uses update_user_option() To update the user favorites |
|
385 * @uses delete_user_option() To delete the user favorites meta |
|
386 * @uses do_action() Calls 'bbp_remove_user_favorite' with the user & topic id |
|
387 * @return bool True if the topic was removed from user's favorites, otherwise |
|
388 * false |
|
389 */ |
|
390 function bbp_remove_user_favorite( $user_id, $topic_id ) { |
|
391 if ( empty( $user_id ) || empty( $topic_id ) ) |
|
392 return false; |
|
393 |
|
394 $favorites = (array) bbp_get_user_favorites_topic_ids( $user_id ); |
|
395 if ( empty( $favorites ) ) |
|
396 return false; |
|
397 |
|
398 $pos = array_search( $topic_id, $favorites ); |
|
399 if ( is_numeric( $pos ) ) { |
|
400 array_splice( $favorites, $pos, 1 ); |
|
401 $favorites = array_filter( $favorites ); |
|
402 |
|
403 if ( !empty( $favorites ) ) { |
|
404 $favorites = implode( ',', $favorites ); |
|
405 update_user_option( $user_id, '_bbp_favorites', $favorites ); |
|
406 } else { |
|
407 delete_user_option( $user_id, '_bbp_favorites' ); |
|
408 } |
|
409 } |
|
410 |
|
411 do_action( 'bbp_remove_user_favorite', $user_id, $topic_id ); |
|
412 |
|
413 return true; |
|
414 } |
|
415 |
|
416 /** |
|
417 * Handles the front end adding and removing of favorite topics |
|
418 * |
|
419 * @uses bbp_get_user_id() To get the user id |
|
420 * @uses bbp_verify_nonce_request() To verify the nonce and check the request |
|
421 * @uses current_user_can() To check if the current user can edit the user |
|
422 * @uses bbPress:errors:add() To log the error messages |
|
423 * @uses bbp_is_user_favorite() To check if the topic is in user's favorites |
|
424 * @uses bbp_remove_user_favorite() To remove the user favorite |
|
425 * @uses bbp_add_user_favorite() To add the user favorite |
|
426 * @uses do_action() Calls 'bbp_favorites_handler' with success, user id, topic |
|
427 * id and action |
|
428 * @uses bbp_is_favorites() To check if it's the favorites page |
|
429 * @uses bbp_get_favorites_link() To get the favorites page link |
|
430 * @uses bbp_get_topic_permalink() To get the topic permalink |
|
431 * @uses wp_safe_redirect() To redirect to the url |
|
432 */ |
|
433 function bbp_favorites_handler() { |
|
434 |
|
435 if ( !bbp_is_favorites_active() ) |
|
436 return false; |
|
437 |
|
438 // Bail if not a GET action |
|
439 if ( 'GET' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) |
|
440 return; |
|
441 |
|
442 // Bail if required GET actions aren't passed |
|
443 if ( empty( $_GET['topic_id'] ) || empty( $_GET['action'] ) ) |
|
444 return; |
|
445 |
|
446 // Setup possible get actions |
|
447 $possible_actions = array( |
|
448 'bbp_favorite_add', |
|
449 'bbp_favorite_remove', |
|
450 ); |
|
451 |
|
452 // Bail if actions aren't meant for this function |
|
453 if ( !in_array( $_GET['action'], $possible_actions ) ) |
|
454 return; |
|
455 |
|
456 // What action is taking place? |
|
457 $action = $_GET['action']; |
|
458 $topic_id = intval( $_GET['topic_id'] ); |
|
459 $user_id = bbp_get_user_id( 0, true, true ); |
|
460 |
|
461 // Check for empty topic |
|
462 if ( empty( $topic_id ) ) { |
|
463 bbp_add_error( 'bbp_favorite_topic_id', __( '<strong>ERROR</strong>: No topic was found! Which topic are you marking/unmarking as favorite?', 'bbpress' ) ); |
|
464 |
|
465 // Check nonce |
|
466 } elseif ( ! bbp_verify_nonce_request( 'toggle-favorite_' . $topic_id ) ) { |
|
467 bbp_add_error( 'bbp_favorite_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); |
|
468 |
|
469 // Check current user's ability to edit the user |
|
470 } elseif ( !current_user_can( 'edit_user', $user_id ) ) { |
|
471 bbp_add_error( 'bbp_favorite_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) ); |
|
472 } |
|
473 |
|
474 // Bail if errors |
|
475 if ( bbp_has_errors() ) |
|
476 return; |
|
477 |
|
478 /** No errors *************************************************************/ |
|
479 |
|
480 $is_favorite = bbp_is_user_favorite( $user_id, $topic_id ); |
|
481 $success = false; |
|
482 |
|
483 if ( true == $is_favorite && 'bbp_favorite_remove' == $action ) |
|
484 $success = bbp_remove_user_favorite( $user_id, $topic_id ); |
|
485 elseif ( false == $is_favorite && 'bbp_favorite_add' == $action ) |
|
486 $success = bbp_add_user_favorite( $user_id, $topic_id ); |
|
487 |
|
488 // Do additional favorites actions |
|
489 do_action( 'bbp_favorites_handler', $success, $user_id, $topic_id, $action ); |
|
490 |
|
491 // Success! |
|
492 if ( true == $success ) { |
|
493 |
|
494 // Redirect back from whence we came |
|
495 if ( bbp_is_favorites() ) { |
|
496 $redirect = bbp_get_favorites_permalink( $user_id ); |
|
497 } elseif ( bbp_is_single_user() ) { |
|
498 $redirect = bbp_get_user_profile_url(); |
|
499 } elseif ( is_singular( bbp_get_topic_post_type() ) ) { |
|
500 $redirect = bbp_get_topic_permalink( $topic_id ); |
|
501 } elseif ( is_single() || is_page() ) { |
|
502 $redirect = get_permalink(); |
|
503 } |
|
504 |
|
505 wp_safe_redirect( $redirect ); |
|
506 |
|
507 // For good measure |
|
508 exit(); |
|
509 |
|
510 // Fail! Handle errors |
|
511 } elseif ( true == $is_favorite && 'bbp_favorite_remove' == $action ) { |
|
512 bbp_add_error( 'bbp_favorite_remove', __( '<strong>ERROR</strong>: There was a problem removing that topic from favorites!', 'bbpress' ) ); |
|
513 } elseif ( false == $is_favorite && 'bbp_favorite_add' == $action ) { |
|
514 bbp_add_error( 'bbp_favorite_add', __( '<strong>ERROR</strong>: There was a problem favoriting that topic!', 'bbpress' ) ); |
|
515 } |
|
516 } |
|
517 |
|
518 /** Subscriptions *************************************************************/ |
|
519 |
|
520 /** |
|
521 * Get the users who have subscribed to the topic |
|
522 * |
|
523 * @since bbPress (r2668) |
|
524 * |
|
525 * @param int $topic_id Optional. Topic id |
|
526 * @uses wpdb::get_col() To execute our query and get the column back |
|
527 * @uses apply_filters() Calls 'bbp_get_topic_subscribers' with the subscribers |
|
528 * @return array|bool Results if the topic has any subscribers, otherwise false |
|
529 */ |
|
530 function bbp_get_topic_subscribers( $topic_id = 0 ) { |
|
531 if ( empty( $topic_id ) ) return; |
|
532 |
|
533 global $wpdb; |
|
534 |
|
535 $key = $wpdb->prefix . '_bbp_subscriptions'; |
|
536 $users = wp_cache_get( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress' ); |
|
537 if ( empty( $users ) ) { |
|
538 $users = $wpdb->get_col( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = '{$key}' and FIND_IN_SET('{$topic_id}', meta_value) > 0" ); |
|
539 wp_cache_set( 'bbp_get_topic_subscribers_' . $topic_id, $users, 'bbpress' ); |
|
540 } |
|
541 |
|
542 if ( !empty( $users ) ) { |
|
543 $users = apply_filters( 'bbp_get_topic_subscribers', $users ); |
|
544 return $users; |
|
545 } |
|
546 |
|
547 return false; |
|
548 } |
|
549 |
|
550 /** |
|
551 * Get a user's subscribed topics |
|
552 * |
|
553 * @since bbPress (r2668) |
|
554 * |
|
555 * @param int $user_id Optional. User id |
|
556 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions |
|
557 * @uses bbp_has_topics() To get the topics |
|
558 * @uses apply_filters() Calls 'bbp_get_user_subscriptions' with the topic query |
|
559 * and user id |
|
560 * @return array|bool Results if user has subscriptions, otherwise false |
|
561 */ |
|
562 function bbp_get_user_subscriptions( $user_id = 0 ) { |
|
563 |
|
564 // Default to the displayed user |
|
565 $user_id = bbp_get_user_id( $user_id ); |
|
566 if ( empty( $user_id ) ) |
|
567 return false; |
|
568 |
|
569 // If user has subscriptions, load them |
|
570 $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id ); |
|
571 if ( !empty( $subscriptions ) ) { |
|
572 $query = bbp_has_topics( array( 'post__in' => $subscriptions ) ); |
|
573 return apply_filters( 'bbp_get_user_subscriptions', $query, $user_id ); |
|
574 } |
|
575 |
|
576 return false; |
|
577 } |
|
578 |
|
579 /** |
|
580 * Get a user's subscribed topics' ids |
|
581 * |
|
582 * @since bbPress (r2668) |
|
583 * |
|
584 * @param int $user_id Optional. User id |
|
585 * @uses bbp_get_user_id() To get the user id |
|
586 * @uses get_user_option() To get the user's subscriptions |
|
587 * @uses apply_filters() Calls 'bbp_get_user_subscribed_topic_ids' with |
|
588 * the subscriptions and user id |
|
589 * @return array|bool Results if user has subscriptions, otherwise false |
|
590 */ |
|
591 function bbp_get_user_subscribed_topic_ids( $user_id = 0 ) { |
|
592 $user_id = bbp_get_user_id( $user_id ); |
|
593 if ( empty( $user_id ) ) |
|
594 return false; |
|
595 |
|
596 $subscriptions = (string) get_user_option( '_bbp_subscriptions', $user_id ); |
|
597 $subscriptions = (array) explode( ',', $subscriptions ); |
|
598 $subscriptions = array_filter( $subscriptions ); |
|
599 |
|
600 return apply_filters( 'bbp_get_user_subscribed_topic_ids', $subscriptions, $user_id ); |
|
601 } |
|
602 |
|
603 /** |
|
604 * Check if a topic is in user's subscription list or not |
|
605 * |
|
606 * @since bbPress (r2668) |
|
607 * |
|
608 * @param int $user_id Optional. User id |
|
609 * @param int $topic_id Optional. Topic id |
|
610 * @uses bbp_get_user_id() To get the user id |
|
611 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions |
|
612 * @uses bbp_get_topic() To get the topic |
|
613 * @uses bbp_get_topic_id() To get the topic id |
|
614 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id, |
|
615 * topic id and subsriptions |
|
616 * @return bool True if the topic is in user's subscriptions, otherwise false |
|
617 */ |
|
618 function bbp_is_user_subscribed( $user_id = 0, $topic_id = 0 ) { |
|
619 |
|
620 // Validate user |
|
621 $user_id = bbp_get_user_id( $user_id, true, true ); |
|
622 if ( empty( $user_id ) ) |
|
623 return false; |
|
624 |
|
625 $retval = false; |
|
626 $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id ); |
|
627 |
|
628 if ( !empty( $subscriptions ) ) { |
|
629 |
|
630 // Checking a specific topic id |
|
631 if ( !empty( $topic_id ) ) { |
|
632 $topic = bbp_get_topic( $topic_id ); |
|
633 $topic_id = !empty( $topic ) ? $topic->ID : 0; |
|
634 |
|
635 // Using the global topic id |
|
636 } elseif ( bbp_get_topic_id() ) { |
|
637 $topic_id = bbp_get_topic_id(); |
|
638 |
|
639 // Use the current post id |
|
640 } elseif ( !bbp_get_topic_id() ) { |
|
641 $topic_id = get_the_ID(); |
|
642 } |
|
643 |
|
644 // Is topic_id in the user's favorites |
|
645 if ( !empty( $topic_id ) ) { |
|
646 $retval = in_array( $topic_id, $subscriptions ); |
|
647 } |
|
648 } |
|
649 |
|
650 return (bool) apply_filters( 'bbp_is_user_subscribed', (bool) $retval, $user_id, $topic_id, $subscriptions ); |
|
651 } |
|
652 |
|
653 /** |
|
654 * Add a topic to user's subscriptions |
|
655 * |
|
656 * @since bbPress (r2668) |
|
657 * |
|
658 * @param int $user_id Optional. User id |
|
659 * @param int $topic_id Optional. Topic id |
|
660 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions |
|
661 * @uses bbp_get_topic() To get the topic |
|
662 * @uses update_user_option() To update the user's subscriptions |
|
663 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id |
|
664 * @return bool Always true |
|
665 */ |
|
666 function bbp_add_user_subscription( $user_id = 0, $topic_id = 0 ) { |
|
667 if ( empty( $user_id ) || empty( $topic_id ) ) |
|
668 return false; |
|
669 |
|
670 $subscriptions = (array) bbp_get_user_subscribed_topic_ids( $user_id ); |
|
671 |
|
672 $topic = bbp_get_topic( $topic_id ); |
|
673 if ( empty( $topic ) ) |
|
674 return false; |
|
675 |
|
676 if ( !in_array( $topic_id, $subscriptions ) ) { |
|
677 $subscriptions[] = $topic_id; |
|
678 $subscriptions = array_filter( $subscriptions ); |
|
679 $subscriptions = (string) implode( ',', $subscriptions ); |
|
680 update_user_option( $user_id, '_bbp_subscriptions', $subscriptions ); |
|
681 |
|
682 wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress' ); |
|
683 } |
|
684 |
|
685 do_action( 'bbp_add_user_subscription', $user_id, $topic_id ); |
|
686 |
|
687 return true; |
|
688 } |
|
689 |
|
690 /** |
|
691 * Remove a topic from user's subscriptions |
|
692 * |
|
693 * @since bbPress (r2668) |
|
694 * |
|
695 * @param int $user_id Optional. User id |
|
696 * @param int $topic_id Optional. Topic id |
|
697 * @uses bbp_get_user_subscribed_topic_ids() To get the user's subscriptions |
|
698 * @uses update_user_option() To update the user's subscriptions |
|
699 * @uses delete_user_option() To delete the user's subscriptions meta |
|
700 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and |
|
701 * topic id |
|
702 * @return bool True if the topic was removed from user's subscriptions, |
|
703 * otherwise false |
|
704 */ |
|
705 function bbp_remove_user_subscription( $user_id, $topic_id ) { |
|
706 if ( empty( $user_id ) || empty( $topic_id ) ) |
|
707 return false; |
|
708 |
|
709 $subscriptions = (array) bbp_get_user_subscribed_topic_ids( $user_id ); |
|
710 |
|
711 if ( empty( $subscriptions ) ) |
|
712 return false; |
|
713 |
|
714 $pos = array_search( $topic_id, $subscriptions ); |
|
715 if ( is_numeric( $pos ) ) { |
|
716 array_splice( $subscriptions, $pos, 1 ); |
|
717 $subscriptions = array_filter( $subscriptions ); |
|
718 |
|
719 if ( !empty( $subscriptions ) ) { |
|
720 $subscriptions = implode( ',', $subscriptions ); |
|
721 update_user_option( $user_id, '_bbp_subscriptions', $subscriptions ); |
|
722 } else { |
|
723 delete_user_option( $user_id, '_bbp_subscriptions' ); |
|
724 } |
|
725 |
|
726 wp_cache_delete( 'bbp_get_topic_subscribers_' . $topic_id, 'bbpress' ); |
|
727 } |
|
728 |
|
729 do_action( 'bbp_remove_user_subscription', $user_id, $topic_id ); |
|
730 |
|
731 return true; |
|
732 } |
|
733 |
|
734 /** |
|
735 * Handles the front end subscribing and unsubscribing topics |
|
736 * |
|
737 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active |
|
738 * @uses bbp_get_user_id() To get the user id |
|
739 * @uses bbp_verify_nonce_request() To verify the nonce and check the request |
|
740 * @uses current_user_can() To check if the current user can edit the user |
|
741 * @uses bbPress:errors:add() To log the error messages |
|
742 * @uses bbp_is_user_subscribed() To check if the topic is in user's |
|
743 * subscriptions |
|
744 * @uses bbp_remove_user_subscription() To remove the user subscription |
|
745 * @uses bbp_add_user_subscription() To add the user subscription |
|
746 * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id, |
|
747 * topic id and action |
|
748 * @uses bbp_is_subscription() To check if it's the subscription page |
|
749 * @uses bbp_get_subscription_link() To get the subscription page link |
|
750 * @uses bbp_get_topic_permalink() To get the topic permalink |
|
751 * @uses wp_safe_redirect() To redirect to the url |
|
752 */ |
|
753 function bbp_subscriptions_handler() { |
|
754 |
|
755 if ( !bbp_is_subscriptions_active() ) |
|
756 return false; |
|
757 |
|
758 // Bail if not a GET action |
|
759 if ( 'GET' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) |
|
760 return; |
|
761 |
|
762 // Bail if required GET actions aren't passed |
|
763 if ( empty( $_GET['topic_id'] ) || empty( $_GET['action'] ) ) |
|
764 return; |
|
765 |
|
766 // Setup possible get actions |
|
767 $possible_actions = array( |
|
768 'bbp_subscribe', |
|
769 'bbp_unsubscribe', |
|
770 ); |
|
771 |
|
772 // Bail if actions aren't meant for this function |
|
773 if ( !in_array( $_GET['action'], $possible_actions ) ) |
|
774 return; |
|
775 |
|
776 // Get required data |
|
777 $action = $_GET['action']; |
|
778 $user_id = bbp_get_user_id( 0, true, true ); |
|
779 $topic_id = intval( $_GET['topic_id'] ); |
|
780 |
|
781 // Check for empty topic |
|
782 if ( empty( $topic_id ) ) { |
|
783 bbp_add_error( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: No topic was found! Which topic are you subscribing/unsubscribing to?', 'bbpress' ) ); |
|
784 |
|
785 // Check nonce |
|
786 } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $topic_id ) ) { |
|
787 bbp_add_error( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); |
|
788 |
|
789 // Check current user's ability to edit the user |
|
790 } elseif ( !current_user_can( 'edit_user', $user_id ) ) { |
|
791 bbp_add_error( 'bbp_subscription_permissions', __( '<strong>ERROR</strong>: You don\'t have the permission to edit favorites of that user!', 'bbpress' ) ); |
|
792 } |
|
793 |
|
794 // Bail if we have errors |
|
795 if ( bbp_has_errors() ) |
|
796 return; |
|
797 |
|
798 /** No errors *************************************************************/ |
|
799 |
|
800 $is_subscription = bbp_is_user_subscribed( $user_id, $topic_id ); |
|
801 $success = false; |
|
802 |
|
803 if ( true == $is_subscription && 'bbp_unsubscribe' == $action ) |
|
804 $success = bbp_remove_user_subscription( $user_id, $topic_id ); |
|
805 elseif ( false == $is_subscription && 'bbp_subscribe' == $action ) |
|
806 $success = bbp_add_user_subscription( $user_id, $topic_id ); |
|
807 |
|
808 // Do additional subscriptions actions |
|
809 do_action( 'bbp_subscriptions_handler', $success, $user_id, $topic_id, $action ); |
|
810 |
|
811 // Success! |
|
812 if ( true == $success ) { |
|
813 |
|
814 // Redirect back from whence we came |
|
815 if ( bbp_is_subscriptions() ) { |
|
816 $redirect = bbp_get_subscriptions_permalink( $user_id ); |
|
817 } elseif ( bbp_is_single_user() ) { |
|
818 $redirect = bbp_get_user_profile_url(); |
|
819 } elseif ( is_singular( bbp_get_topic_post_type() ) ) { |
|
820 $redirect = bbp_get_topic_permalink( $topic_id ); |
|
821 } elseif ( is_single() || is_page() ) { |
|
822 $redirect = get_permalink(); |
|
823 } |
|
824 |
|
825 wp_safe_redirect( $redirect ); |
|
826 |
|
827 // For good measure |
|
828 exit(); |
|
829 |
|
830 // Fail! Handle errors |
|
831 } elseif ( true == $is_subscription && 'bbp_unsubscribe' == $action ) { |
|
832 bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that topic!', 'bbpress' ) ); |
|
833 } elseif ( false == $is_subscription && 'bbp_subscribe' == $action ) { |
|
834 bbp_add_error( 'bbp_subscribe', __( '<strong>ERROR</strong>: There was a problem subscribing to that topic!', 'bbpress' ) ); |
|
835 } |
|
836 } |
|
837 |
|
838 /** Edit **********************************************************************/ |
|
839 |
|
840 /** |
|
841 * Handles the front end user editing |
|
842 * |
|
843 * @uses is_multisite() To check if it's a multisite |
|
844 * @uses bbp_is_user_home() To check if the user is at home (the display page |
|
845 * is the one of the logged in user) |
|
846 * @uses get_option() To get the displayed user's new email id option |
|
847 * @uses wpdb::prepare() To sanitize our sql query |
|
848 * @uses wpdb::get_var() To execute our query and get back the variable |
|
849 * @uses wpdb::query() To execute our query |
|
850 * @uses wp_update_user() To update the user |
|
851 * @uses delete_option() To delete the displayed user's email id option |
|
852 * @uses bbp_get_user_profile_edit_url() To get the edit profile url |
|
853 * @uses wp_safe_redirect() To redirect to the url |
|
854 * @uses bbp_verify_nonce_request() To verify the nonce and check the request |
|
855 * @uses current_user_can() To check if the current user can edit the user |
|
856 * @uses do_action() Calls 'personal_options_update' or |
|
857 * 'edit_user_options_update' (based on if it's the user home) |
|
858 * with the displayed user id |
|
859 * @uses edit_user() To edit the user based on the post data |
|
860 * @uses get_userdata() To get the user data |
|
861 * @uses is_email() To check if the string is an email id or not |
|
862 * @uses wpdb::get_blog_prefix() To get the blog prefix |
|
863 * @uses is_network_admin() To check if the user is the network admin |
|
864 * @uses is_super_admin() To check if the user is super admin |
|
865 * @uses revoke_super_admin() To revoke super admin priviledges |
|
866 * @uses grant_super_admin() To grant super admin priviledges |
|
867 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error} |
|
868 */ |
|
869 function bbp_edit_user_handler() { |
|
870 |
|
871 // Bail if not a POST action |
|
872 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) |
|
873 return; |
|
874 |
|
875 // Bail if action is not 'bbp-update-user' |
|
876 if ( empty( $_POST['action'] ) || ( 'bbp-update-user' !== $_POST['action'] ) ) |
|
877 return; |
|
878 |
|
879 // Get the displayed user ID |
|
880 $user_id = bbp_get_displayed_user_id(); |
|
881 |
|
882 // Execute confirmed email change. See send_confirmation_on_profile_email(). |
|
883 if ( is_multisite() && bbp_is_user_home_edit() && isset( $_GET['newuseremail'] ) ) { |
|
884 |
|
885 $new_email = get_option( $user_id . '_new_email' ); |
|
886 |
|
887 if ( $new_email['hash'] == $_GET['newuseremail'] ) { |
|
888 $user = new stdClass(); |
|
889 $user->ID = $user_id; |
|
890 $user->user_email = esc_html( trim( $new_email['newemail'] ) ); |
|
891 |
|
892 global $wpdb; |
|
893 |
|
894 if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->signups} WHERE user_login = %s", bbp_get_displayed_user_field( 'user_login' ) ) ) ) { |
|
895 $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->signups} SET user_email = %s WHERE user_login = %s", $user->user_email, bbp_get_displayed_user_field( 'user_login' ) ) ); |
|
896 } |
|
897 |
|
898 wp_update_user( get_object_vars( $user ) ); |
|
899 delete_option( $user_id . '_new_email' ); |
|
900 |
|
901 wp_safe_redirect( add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $user_id ) ) ); |
|
902 exit(); |
|
903 } |
|
904 |
|
905 // Delete new email address from user options |
|
906 } elseif ( is_multisite() && bbp_is_user_home_edit() && !empty( $_GET['dismiss'] ) && ( $user_id . '_new_email' == $_GET['dismiss'] ) ) { |
|
907 delete_option( $user_id . '_new_email' ); |
|
908 wp_safe_redirect( add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $user_id ) ) ); |
|
909 exit(); |
|
910 } |
|
911 |
|
912 // Nonce check |
|
913 if ( ! bbp_verify_nonce_request( 'update-user_' . $user_id ) ) { |
|
914 bbp_add_error( 'bbp_update_user_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); |
|
915 return; |
|
916 } |
|
917 |
|
918 // Cap check |
|
919 if ( ! current_user_can( 'edit_user', $user_id ) ) { |
|
920 bbp_add_error( 'bbp_update_user_capability', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); |
|
921 return; |
|
922 } |
|
923 |
|
924 // Do action based on who's profile you're editing |
|
925 $edit_action = bbp_is_user_home_edit() ? 'personal_options_update' : 'edit_user_profile_update'; |
|
926 do_action( $edit_action, $user_id ); |
|
927 |
|
928 // Handle user edit |
|
929 $edit_user = edit_user( $user_id ); |
|
930 |
|
931 // Error(s) editng the user, so copy them into the global |
|
932 if ( is_wp_error( $edit_user ) ) { |
|
933 bbpress()->errors = $edit_user; |
|
934 |
|
935 // Successful edit to redirect |
|
936 } elseif ( is_integer( $edit_user ) ) { |
|
937 |
|
938 // Maybe update super admin ability |
|
939 if ( is_multisite() && ! bbp_is_user_home_edit() ) { |
|
940 empty( $_POST['super_admin'] ) ? revoke_super_admin( $edit_user ) : grant_super_admin( $edit_user ); |
|
941 } |
|
942 |
|
943 $redirect = add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $edit_user ) ); |
|
944 |
|
945 wp_safe_redirect( $redirect ); |
|
946 exit; |
|
947 } |
|
948 } |
|
949 |
|
950 /** |
|
951 * Conditionally hook the core WordPress output actions to the end of the |
|
952 * default user's edit profile template. |
|
953 * |
|
954 * This allows clever plugin authors to conditionally unhook the WordPress core |
|
955 * output actions if they don't want any unexpected junk to appear there, and |
|
956 * also avoids needing to pollute the templates with additional logic and actions. |
|
957 * |
|
958 * @since bbPress (r4273) |
|
959 * |
|
960 * @uses bbp_is_user_home_edit() To switch the action fired |
|
961 * @uses get_userdata() To get the current user's data |
|
962 * @uses bbp_get_displayed_user_id() To get the currently displayed user ID |
|
963 */ |
|
964 function bbp_user_edit_after() { |
|
965 $action = bbp_is_user_home_edit() ? 'show_user_profile' : 'edit_user_profile'; |
|
966 |
|
967 do_action( $action, get_userdata( bbp_get_displayed_user_id() ) ); |
|
968 } |
|
969 |
|
970 /** User Queries **************************************************************/ |
|
971 |
|
972 /** |
|
973 * Get the topics that a user created |
|
974 * |
|
975 * @since bbPress (r2660) |
|
976 * |
|
977 * @param int $user_id Optional. User id |
|
978 * @uses bbp_get_user_id() To get the topic id |
|
979 * @uses bbp_has_topics() To get the topics created by the user |
|
980 * @return array|bool Results if the user has created topics, otherwise false |
|
981 */ |
|
982 function bbp_get_user_topics_started( $user_id = 0 ) { |
|
983 |
|
984 // Validate user |
|
985 $user_id = bbp_get_user_id( $user_id ); |
|
986 if ( empty( $user_id ) ) |
|
987 return false; |
|
988 |
|
989 // Query defaults |
|
990 $default_query = array( |
|
991 'author' => $user_id, |
|
992 'show_stickies' => false, |
|
993 'order' => 'DESC', |
|
994 ); |
|
995 |
|
996 // Try to get the topics |
|
997 $query = bbp_has_topics( $default_query ); |
|
998 if ( empty( $query ) ) |
|
999 return false; |
|
1000 |
|
1001 return apply_filters( 'bbp_get_user_topics_started', $query, $user_id ); |
|
1002 } |
|
1003 |
|
1004 /** |
|
1005 * Get the replies that a user created |
|
1006 * |
|
1007 * @since bbPress (r4225) |
|
1008 * |
|
1009 * @param int $user_id Optional. User id |
|
1010 * @uses bbp_get_user_id() To get the topic id |
|
1011 * @uses bbp_has_replies() To get the topics created by the user |
|
1012 * @return array|bool Results if the user has created topics, otherwise false |
|
1013 */ |
|
1014 function bbp_get_user_replies_created( $user_id = 0 ) { |
|
1015 |
|
1016 // Validate user |
|
1017 $user_id = bbp_get_user_id( $user_id ); |
|
1018 if ( empty( $user_id ) ) |
|
1019 return false; |
|
1020 |
|
1021 // Try to get the topics |
|
1022 $query = bbp_has_replies( array( |
|
1023 'post_type' => array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ), |
|
1024 'post_parent' => 'any', |
|
1025 'posts_per_page' => bbp_get_replies_per_page(), |
|
1026 'paged' => bbp_get_paged(), |
|
1027 'orderby' => 'date', |
|
1028 'order' => 'DESC', |
|
1029 'author' => $user_id, |
|
1030 'show_stickies' => false, |
|
1031 ) ); |
|
1032 |
|
1033 return apply_filters( 'bbp_get_user_replies_created', $query, $user_id ); |
|
1034 } |
|
1035 |
|
1036 /** |
|
1037 * Get the total number of users on the forums |
|
1038 * |
|
1039 * @since bbPress (r2769) |
|
1040 * @uses wp_cache_get() Check if query is in cache |
|
1041 * @uses get_users() To execute our query and get the var back |
|
1042 * @uses wp_cache_set() Set the query in the cache |
|
1043 * @uses apply_filters() Calls 'bbp_get_total_users' with number of users |
|
1044 * @return int Total number of users |
|
1045 */ |
|
1046 function bbp_get_total_users() { |
|
1047 $user_count = count_users(); |
|
1048 return apply_filters( 'bbp_get_total_users', (int) $user_count['total_users'] ); |
|
1049 } |
|
1050 |
|
1051 /** Premissions ***************************************************************/ |
|
1052 |
|
1053 /** |
|
1054 * Redirect if unathorized user is attempting to edit another user |
|
1055 * |
|
1056 * This is hooked to 'bbp_template_redirect' and controls the conditions under |
|
1057 * which a user can edit another user (or themselves.) If these conditions are |
|
1058 * met. We assume a user cannot perform this task, and look for ways they can |
|
1059 * earn the ability to access this template. |
|
1060 * |
|
1061 * @since bbPress (r3605) |
|
1062 * |
|
1063 * @uses bbp_is_topic_edit() |
|
1064 * @uses current_user_can() |
|
1065 * @uses bbp_get_topic_id() |
|
1066 * @uses wp_safe_redirect() |
|
1067 * @uses bbp_get_topic_permalink() |
|
1068 */ |
|
1069 function bbp_check_user_edit() { |
|
1070 |
|
1071 // Bail if not editing a topic |
|
1072 if ( ! bbp_is_single_user_edit() ) |
|
1073 return; |
|
1074 |
|
1075 // Default to false |
|
1076 $redirect = true; |
|
1077 |
|
1078 // Allow user to edit their own profile |
|
1079 if ( bbp_is_user_home_edit() ) { |
|
1080 $redirect = false; |
|
1081 |
|
1082 // Allow if current user can edit the displayed user |
|
1083 } elseif ( current_user_can( 'edit_user', bbp_get_displayed_user_id() ) ) { |
|
1084 $redirect = false; |
|
1085 |
|
1086 // Allow if user can manage network users, or edit-any is enabled |
|
1087 } elseif ( current_user_can( 'manage_network_users' ) || apply_filters( 'enable_edit_any_user_configuration', false ) ) { |
|
1088 $redirect = false; |
|
1089 } |
|
1090 |
|
1091 // Maybe redirect back to profile page |
|
1092 if ( true === $redirect ) { |
|
1093 wp_safe_redirect( bbp_get_user_profile_url( bbp_get_displayed_user_id() ) ); |
|
1094 exit(); |
|
1095 } |
|
1096 } |
|
1097 |
|
1098 /** |
|
1099 * Check if a user is blocked, or cannot spectate the forums. |
|
1100 * |
|
1101 * @since bbPress (r2996) |
|
1102 * |
|
1103 * @uses is_user_logged_in() To check if user is logged in |
|
1104 * @uses is_super_admin() To check if user is a super admin |
|
1105 * @uses current_user_can() To check if the current user can spectate |
|
1106 * @uses is_bbpress() To check if in a bbPress section of the site |
|
1107 * @uses bbp_set_404() To set a 404 status |
|
1108 */ |
|
1109 function bbp_forum_enforce_blocked() { |
|
1110 |
|
1111 // Bail if not logged in or super admin |
|
1112 if ( ! is_user_logged_in() || is_super_admin() ) { |
|
1113 return; |
|
1114 } |
|
1115 |
|
1116 // Set 404 if in bbPress and user cannot spectate |
|
1117 if ( is_bbpress() && ! current_user_can( 'spectate' ) ) { |
|
1118 bbp_set_404(); |
|
1119 } |
|
1120 } |
|
1121 |
|
1122 /** Converter *****************************************************************/ |
|
1123 |
|
1124 /** |
|
1125 * Convert passwords from previous platfrom encryption to WordPress encryption. |
|
1126 * |
|
1127 * @since bbPress (r3813) |
|
1128 * @global WPDB $wpdb |
|
1129 */ |
|
1130 function bbp_user_maybe_convert_pass() { |
|
1131 |
|
1132 // Bail if no username |
|
1133 $username = !empty( $_POST['log'] ) ? $_POST['log'] : ''; |
|
1134 if ( empty( $username ) ) |
|
1135 return; |
|
1136 |
|
1137 global $wpdb; |
|
1138 |
|
1139 // Bail if no user password to convert |
|
1140 $row = $wpdb->get_row( "SELECT * FROM {$wpdb->users} INNER JOIN {$wpdb->usermeta} ON user_id = ID WHERE meta_key = '_bbp_class' AND user_login = '{$username}' LIMIT 1" ); |
|
1141 if ( empty( $row ) || is_wp_error( $row ) ) |
|
1142 return; |
|
1143 |
|
1144 // Setup admin (to include converter) |
|
1145 require_once( bbpress()->includes_dir . 'admin/admin.php' ); |
|
1146 |
|
1147 // Create the admin object |
|
1148 bbp_admin(); |
|
1149 |
|
1150 // Convert password |
|
1151 require_once( bbpress()->admin->admin_dir . 'converter.php' ); |
|
1152 require_once( bbpress()->admin->admin_dir . 'converters/' . $row->meta_value . '.php' ); |
|
1153 |
|
1154 // Create the converter |
|
1155 $converter = bbp_new_converter( $row->meta_value ); |
|
1156 |
|
1157 // Try to call the conversion method |
|
1158 if ( is_a( $converter, 'BBP_Converter_Base' ) && method_exists( $converter, 'callback_pass' ) ) { |
|
1159 $converter->callback_pass( $username, $_POST['pwd'] ); |
|
1160 } |
|
1161 } |