PluginProbe
bbPress / 2.1-beta-1
bbPress v2.1-beta-1
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 / bbp-includes / bbp-common-functions.php

bbp-common-functions.php in bbPress 2.1-beta-1, at bbp-includes/bbp-common-functions.php

1,634 lines 53.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Common Functions
5 *
6 * Common functions are ones that are used by more than one component, like
7 * forums, topics, replies, users, topic tags, etc...
8 *
9 * @package bbPress
10 * @subpackage Functions
11 */
12
13 // Exit if accessed directly
14 if ( !defined( 'ABSPATH' ) ) exit;
15
16 /** Formatting ****************************************************************/
17
18 /**
19 * A bbPress specific method of formatting numeric values
20 *
21 * @since bbPress (r2486)
22 *
23 * @param string $number Number to format
24 * @param string $decimals Optional. Display decimals
25 * @uses apply_filters() Calls 'bbp_number_format' with the formatted values,
26 * number and display decimals bool
27 * @return string Formatted string
28 */
29 function bbp_number_format( $number = 0, $decimals = false, $dec_point = '.', $thousands_sep = ',' ) {
30
31 // If empty, set $number to (int) 0
32 if ( ! is_numeric( $number ) )
33 $number = 0;
34
35 return apply_filters( 'bbp_number_format', number_format( $number, $decimals, $dec_point, $thousands_sep ), $number, $decimals, $dec_point, $thousands_sep );
36 }
37
38 /**
39 * A bbPress specific method of formatting numeric values
40 *
41 * @since bbPress (r3857)
42 *
43 * @param string $number Number to format
44 * @param string $decimals Optional. Display decimals
45 * @uses apply_filters() Calls 'bbp_number_format' with the formatted values,
46 * number and display decimals bool
47 * @return string Formatted string
48 */
49 function bbp_number_format_i18n( $number = 0, $decimals = false ) {
50
51 // If empty, set $number to (int) 0
52 if ( ! is_numeric( $number ) )
53 $number = 0;
54
55 return apply_filters( 'bbp_number_format_i18n', number_format_i18n( $number, $decimals ), $number, $decimals );
56 }
57
58 /**
59 * Convert time supplied from database query into specified date format.
60 *
61 * @since bbPress (r2455)
62 *
63 * @param int|object $post Optional. Default is global post object. A post_id or
64 * post object
65 * @param string $d Optional. Default is 'U'. Either 'G', 'U', or php date
66 * format
67 * @param bool $translate Optional. Default is false. Whether to translate the
68 * result
69 * @uses mysql2date() To convert the format
70 * @uses apply_filters() Calls 'bbp_convert_date' with the time, date format
71 * and translate bool
72 * @return string Returns timestamp
73 */
74 function bbp_convert_date( $time, $d = 'U', $translate = false ) {
75 $time = mysql2date( $d, $time, $translate );
76
77 return apply_filters( 'bbp_convert_date', $time, $d, $translate );
78 }
79
80 /**
81 * Output formatted time to display human readable time difference.
82 *
83 * @since bbPress (r2544)
84 *
85 * @param string $older_date Unix timestamp from which the difference begins.
86 * @param string $newer_date Optional. Unix timestamp from which the
87 * difference ends. False for current time.
88 * @uses bbp_get_time_since() To get the formatted time
89 */
90 function bbp_time_since( $older_date, $newer_date = false ) {
91 echo bbp_get_time_since( $older_date, $newer_date = false );
92 }
93 /**
94 * Return formatted time to display human readable time difference.
95 *
96 * @since bbPress (r2544)
97 *
98 * @param string $older_date Unix timestamp from which the difference begins.
99 * @param string $newer_date Optional. Unix timestamp from which the
100 * difference ends. False for current time.
101 * @uses current_time() To get the current time in mysql format
102 * @uses human_time_diff() To get the time differene in since format
103 * @uses apply_filters() Calls 'bbp_get_time_since' with the time
104 * difference and time
105 * @return string Formatted time
106 */
107 function bbp_get_time_since( $older_date, $newer_date = false ) {
108
109 // Setup the strings
110 $unknown_text = apply_filters( 'bbp_core_time_since_unknown_text', __( 'sometime', 'bbpress' ) );
111 $right_now_text = apply_filters( 'bbp_core_time_since_right_now_text', __( 'right now', 'bbpress' ) );
112 $ago_text = apply_filters( 'bbp_core_time_since_ago_text', __( '%s ago', 'bbpress' ) );
113
114 // array of time period chunks
115 $chunks = array(
116 array( 60 * 60 * 24 * 365 , __( 'year', 'bbpress' ), __( 'years', 'bbpress' ) ),
117 array( 60 * 60 * 24 * 30 , __( 'month', 'bbpress' ), __( 'months', 'bbpress' ) ),
118 array( 60 * 60 * 24 * 7, __( 'week', 'bbpress' ), __( 'weeks', 'bbpress' ) ),
119 array( 60 * 60 * 24 , __( 'day', 'bbpress' ), __( 'days', 'bbpress' ) ),
120 array( 60 * 60 , __( 'hour', 'bbpress' ), __( 'hours', 'bbpress' ) ),
121 array( 60 , __( 'minute', 'bbpress' ), __( 'minutes', 'bbpress' ) ),
122 array( 1, __( 'second', 'bbpress' ), __( 'seconds', 'bbpress' ) )
123 );
124
125 if ( !empty( $older_date ) && !is_numeric( $older_date ) ) {
126 $time_chunks = explode( ':', str_replace( ' ', ':', $older_date ) );
127 $date_chunks = explode( '-', str_replace( ' ', '-', $older_date ) );
128 $older_date = gmmktime( (int) $time_chunks[1], (int) $time_chunks[2], (int) $time_chunks[3], (int) $date_chunks[1], (int) $date_chunks[2], (int) $date_chunks[0] );
129 }
130
131 // $newer_date will equal false if we want to know the time elapsed
132 // between a date and the current time. $newer_date will have a value if
133 // we want to work out time elapsed between two known dates.
134 $newer_date = ( !$newer_date ) ? strtotime( current_time( 'mysql' ) ) : $newer_date;
135
136 // Difference in seconds
137 $since = $newer_date - $older_date;
138
139 // Something went wrong with date calculation and we ended up with a negative date.
140 if ( 0 > $since ) {
141 $output = $unknown_text;
142
143 // We only want to output two chunks of time here, eg:
144 // x years, xx months
145 // x days, xx hours
146 // so there's only two bits of calculation below:
147 } else {
148
149 // Step one: the first chunk
150 for ( $i = 0, $j = count( $chunks ); $i < $j; ++$i ) {
151 $seconds = $chunks[$i][0];
152
153 // Finding the biggest chunk (if the chunk fits, break)
154 $count = floor( $since / $seconds );
155 if ( 0 != $count ) {
156 break;
157 }
158 }
159
160 // If $i iterates all the way to $j, then the event happened 0 seconds ago
161 if ( !isset( $chunks[$i] ) ) {
162 $output = $right_now_text;
163
164 } else {
165
166 // Set output var
167 $output = ( 1 == $count ) ? '1 '. $chunks[$i][1] : $count . ' ' . $chunks[$i][2];
168
169 // Step two: the second chunk
170 if ( $i + 2 < $j ) {
171 $seconds2 = $chunks[$i + 1][0];
172 $name2 = $chunks[$i + 1][1];
173 $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 );
174
175 // Add to output var
176 if ( 0 != $count2 ) {
177 $output .= ( 1 == $count2 ) ? _x( ',', 'Separator in time since', 'bbpress' ) . ' 1 '. $name2 : _x( ',', 'Separator in time since', 'bbpress' ) . ' ' . $count2 . ' ' . $chunks[$i + 1][2];
178 }
179 }
180
181 // No output, so happened right now
182 if ( ! (int) trim( $output ) ) {
183 $output = $right_now_text;
184 }
185 }
186 }
187
188 // Append 'ago' to the end of time-since if not 'right now'
189 if ( $output != $right_now_text ) {
190 $output = sprintf( $ago_text, $output );
191 }
192
193 return apply_filters( 'bbp_get_time_since', $output, $older_date, $newer_date );
194 }
195
196 /**
197 * Formats the reason for editing the topic/reply.
198 *
199 * Does these things:
200 * - Trimming
201 * - Removing periods from the end of the string
202 * - Trimming again
203 *
204 * @since bbPress (r2782)
205 *
206 * @param int $topic_id Optional. Topic id
207 * @return string Status of topic
208 */
209 function bbp_format_revision_reason( $reason = '' ) {
210 $reason = (string) $reason;
211
212 // Format reason for proper display
213 if ( empty( $reason ) )
214 return $reason;
215
216 // Trimming
217 $reason = trim( $reason );
218
219 // We add our own full stop.
220 while ( substr( $reason, -1 ) == '.' )
221 $reason = substr( $reason, 0, -1 );
222
223 // Trim again
224 $reason = trim( $reason );
225
226 return $reason;
227 }
228
229 /** Misc **********************************************************************/
230
231 /**
232 * Append 'view=all' to query string if it's already there from referer
233 *
234 * @since bbPress (r3325)
235 *
236 * @param string $original_link Original Link to be modified
237 * @param bool $force Override bbp_get_view_all() check
238 * @uses current_user_can() To check if the current user can moderate
239 * @uses add_query_arg() To add args to the url
240 * @uses apply_filters() Calls 'bbp_add_view_all' with the link and original link
241 * @return string The link with 'view=all' appended if necessary
242 */
243 function bbp_add_view_all( $original_link = '', $force = false ) {
244
245 // Are we appending the view=all vars?
246 if ( bbp_get_view_all() || !empty( $force ) )
247 $link = add_query_arg( array( 'view' => 'all' ), $original_link );
248 else
249 $link = $original_link;
250
251 return apply_filters( 'bbp_add_view_all', $link, $original_link );
252 }
253
254 /**
255 * Remove 'view=all' from query string
256 *
257 * @since bbPress (r3325)
258 *
259 * @param string $original_link Original Link to be modified
260 * @uses current_user_can() To check if the current user can moderate
261 * @uses add_query_arg() To add args to the url
262 * @uses apply_filters() Calls 'bbp_add_view_all' with the link and original link
263 * @return string The link with 'view=all' appended if necessary
264 */
265 function bbp_remove_view_all( $original_link = '' ) {
266 return apply_filters( 'bbp_add_view_all', remove_query_arg( 'view', $original_link ), $original_link );
267 }
268
269 /**
270 * If current user can and is vewing all topics/replies
271 *
272 * @since bbPress (r3325)
273 *
274 * @uses current_user_can() To check if the current user can moderate
275 * @uses apply_filters() Calls 'bbp_get_view_all' with the link and original link
276 * @return bool Whether current user can and is viewing all
277 */
278 function bbp_get_view_all( $cap = 'moderate' ) {
279 $retval = ( ( !empty( $_GET['view'] ) && ( 'all' == $_GET['view'] ) && current_user_can( $cap ) ) );
280 return apply_filters( 'bbp_get_view_all', (bool) $retval );
281 }
282
283 /**
284 * Assist pagination by returning correct page number
285 *
286 * @since bbPress (r2628)
287 *
288 * @uses get_query_var() To get the 'paged' value
289 * @return int Current page number
290 */
291 function bbp_get_paged() {
292 global $wp_query;
293
294 // Make sure to not paginate widget queries
295 if ( !bbp_is_query_name( 'bbp_widget' ) ) {
296
297 // Check the query var
298 if ( get_query_var( 'paged' ) ) {
299 $paged = get_query_var( 'paged' );
300
301 // Check query paged
302 } elseif ( !empty( $wp_query->query['paged'] ) ) {
303 $paged = $wp_query->query['paged'];
304 }
305
306 // Paged found
307 if ( !empty( $paged ) ) {
308 return (int) $paged;
309 }
310 }
311
312 // Default to first page
313 return 1;
314 }
315
316 /**
317 * Fix post author id on post save
318 *
319 * When a logged in user changes the status of an anonymous reply or topic, or
320 * edits it, the post_author field is set to the logged in user's id. This
321 * function fixes that.
322 *
323 * @since bbPress (r2734)
324 *
325 * @param array $data Post data
326 * @param array $postarr Original post array (includes post id)
327 * @uses bbp_get_topic_post_type() To get the topic post type
328 * @uses bbp_get_reply_post_type() To get the reply post type
329 * @uses bbp_is_topic_anonymous() To check if the topic is by an anonymous user
330 * @uses bbp_is_reply_anonymous() To check if the reply is by an anonymous user
331 * @return array Data
332 */
333 function bbp_fix_post_author( $data = array(), $postarr = array() ) {
334
335 // Post is not being updated or the post_author is already 0, return
336 if ( empty( $postarr['ID'] ) || empty( $data['post_author'] ) )
337 return $data;
338
339 // Post is not a topic or reply, return
340 if ( !in_array( $data['post_type'], array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ) ) )
341 return $data;
342
343 // Is the post by an anonymous user?
344 if ( ( bbp_get_topic_post_type() == $data['post_type'] && !bbp_is_topic_anonymous( $postarr['ID'] ) ) ||
345 ( bbp_get_reply_post_type() == $data['post_type'] && !bbp_is_reply_anonymous( $postarr['ID'] ) ) )
346 return $data;
347
348 // The post is being updated. It is a topic or a reply and is written by an anonymous user.
349 // Set the post_author back to 0
350 $data['post_author'] = 0;
351
352 return $data;
353 }
354
355 /**
356 * Check the date against the _bbp_edit_lock setting.
357 *
358 * @since bbPress (r3133)
359 *
360 * @param string $post_date_gmt
361 *
362 * @uses get_option() Get the edit lock time
363 * @uses current_time() Get the current time
364 * @uses strtotime() Convert strings to time
365 * @uses apply_filters() Allow output to be manipulated
366 *
367 * @return bool
368 */
369 function bbp_past_edit_lock( $post_date_gmt ) {
370
371 // Assume editing is allowed
372 $retval = false;
373
374 // Bail if empty date
375 if ( ! empty( $post_date_gmt ) ) {
376
377 // Period of time
378 $lockable = '+' . get_option( '_bbp_edit_lock', '5' ) . ' minutes';
379
380 // Now
381 $cur_time = current_time( 'timestamp', true );
382
383 // Add lockable time to post time
384 $lock_time = strtotime( $lockable, strtotime( $post_date_gmt ) );
385
386 // Compare
387 if ( $cur_time >= $lock_time ) {
388 $retval = true;
389 }
390 }
391
392 return apply_filters( 'bbp_past_edit_lock', (bool) $retval, $cur_time, $lock_time, $post_date_gmt );
393 }
394
395 /** Statistics ****************************************************************/
396
397 /**
398 * Get the forum statistics
399 *
400 * @since bbPress (r2769)
401 *
402 * @param mixed $args Optional. The function supports these arguments (all
403 * default to true):
404 * - count_users: Count users?
405 * - count_forums: Count forums?
406 * - count_topics: Count topics? If set to false, private, spammed and trashed
407 * topics are also not counted.
408 * - count_private_topics: Count private topics? (only counted if the current
409 * user has read_private_topics cap)
410 * - count_spammed_topics: Count spammed topics? (only counted if the current
411 * user has edit_others_topics cap)
412 * - count_trashed_topics: Count trashed topics? (only counted if the current
413 * user has view_trash cap)
414 * - count_replies: Count replies? If set to false, private, spammed and
415 * trashed replies are also not counted.
416 * - count_private_replies: Count private replies? (only counted if the current
417 * user has read_private_replies cap)
418 * - count_spammed_replies: Count spammed replies? (only counted if the current
419 * user has edit_others_replies cap)
420 * - count_trashed_replies: Count trashed replies? (only counted if the current
421 * user has view_trash cap)
422 * - count_tags: Count tags? If set to false, empty tags are also not counted
423 * - count_empty_tags: Count empty tags?
424 * @uses bbp_count_users() To count the number of registered users
425 * @uses bbp_get_forum_post_type() To get the forum post type
426 * @uses bbp_get_topic_post_type() To get the topic post type
427 * @uses bbp_get_reply_post_type() To get the reply post type
428 * @uses wp_count_posts() To count the number of forums, topics and replies
429 * @uses wp_count_terms() To count the number of topic tags
430 * @uses current_user_can() To check if the user is capable of doing things
431 * @uses number_format_i18n() To format the number
432 * @uses apply_filters() Calls 'bbp_get_statistics' with the statistics and args
433 * @return object Walked forum tree
434 */
435 function bbp_get_statistics( $args = '' ) {
436
437 $defaults = array (
438 'count_users' => true,
439 'count_forums' => true,
440 'count_topics' => true,
441 'count_private_topics' => true,
442 'count_spammed_topics' => true,
443 'count_trashed_topics' => true,
444 'count_replies' => true,
445 'count_private_replies' => true,
446 'count_spammed_replies' => true,
447 'count_trashed_replies' => true,
448 'count_tags' => true,
449 'count_empty_tags' => true
450 );
451 $r = bbp_parse_args( $args, $defaults, 'get_statistics' );
452 extract( $r );
453
454 // Users
455 if ( !empty( $count_users ) )
456 $user_count = bbp_get_total_users();
457
458 // Forums
459 if ( !empty( $count_forums ) ) {
460 $forum_count = wp_count_posts( bbp_get_forum_post_type() );
461 $forum_count = $forum_count->publish;
462 }
463
464 // Post statuses
465 $private = bbp_get_private_status_id();
466 $spam = bbp_get_spam_status_id();
467 $trash = bbp_get_trash_status_id();
468 $closed = bbp_get_closed_status_id();
469
470 // Topics
471 if ( !empty( $count_topics ) ) {
472
473 $all_topics = wp_count_posts( bbp_get_topic_post_type() );
474
475 // Published (publish + closed)
476 $topic_count = $all_topics->publish + $all_topics->{$closed};
477
478 if ( current_user_can( 'read_private_topics' ) || current_user_can( 'edit_others_topics' ) || current_user_can( 'view_trash' ) ) {
479
480 // Private
481 $topics['private'] = ( !empty( $count_private_topics ) && current_user_can( 'read_private_topics' ) ) ? (int) $all_topics->{$private} : 0;
482
483 // Spam
484 $topics['spammed'] = ( !empty( $count_spammed_topics ) && current_user_can( 'edit_others_topics' ) ) ? (int) $all_topics->{$spam} : 0;
485
486 // Trash
487 $topics['trashed'] = ( !empty( $count_trashed_topics ) && current_user_can( 'view_trash' ) ) ? (int) $all_topics->{$trash} : 0;
488
489 // Total hidden (private + spam + trash)
490 $topic_count_hidden = $topics['private'] + $topics['spammed'] + $topics['trashed'];
491
492 // Generate the hidden topic count's title attribute
493 $topic_titles[] = !empty( $topics['private'] ) ? sprintf( __( 'Private: %s', 'bbpress' ), number_format_i18n( $topics['private'] ) ) : '';
494 $topic_titles[] = !empty( $topics['spammed'] ) ? sprintf( __( 'Spammed: %s', 'bbpress' ), number_format_i18n( $topics['spammed'] ) ) : '';
495 $topic_titles[] = !empty( $topics['trashed'] ) ? sprintf( __( 'Trashed: %s', 'bbpress' ), number_format_i18n( $topics['trashed'] ) ) : '';
496
497 // Compile the hidden topic title
498 $hidden_topic_title = implode( ' | ', array_filter( $topic_titles ) );
499 }
500 }
501
502 // Replies
503 if ( !empty( $count_replies ) ) {
504
505 $all_replies = wp_count_posts( bbp_get_reply_post_type() );
506
507 // Published
508 $reply_count = $all_replies->publish;
509
510 if ( current_user_can( 'read_private_replies' ) || current_user_can( 'edit_others_replies' ) || current_user_can( 'view_trash' ) ) {
511
512 // Private
513 $replies['private'] = ( !empty( $count_private_replies ) && current_user_can( 'read_private_replies' ) ) ? (int) $all_replies->{$private} : 0;
514
515 // Spam
516 $replies['spammed'] = ( !empty( $count_spammed_replies ) && current_user_can( 'edit_others_replies' ) ) ? (int) $all_replies->{$spam} : 0;
517
518 // Trash
519 $replies['trashed'] = ( !empty( $count_trashed_replies ) && current_user_can( 'view_trash' ) ) ? (int) $all_replies->{$trash} : 0;
520
521 // Total hidden (private + spam + trash)
522 $reply_count_hidden = $replies['private'] + $replies['spammed'] + $replies['trashed'];
523
524 // Generate the hidden topic count's title attribute
525 $reply_titles[] = !empty( $replies['private'] ) ? sprintf( __( 'Private: %s', 'bbpress' ), number_format_i18n( $replies['private'] ) ) : '';
526 $reply_titles[] = !empty( $replies['spammed'] ) ? sprintf( __( 'Spammed: %s', 'bbpress' ), number_format_i18n( $replies['spammed'] ) ) : '';
527 $reply_titles[] = !empty( $replies['trashed'] ) ? sprintf( __( 'Trashed: %s', 'bbpress' ), number_format_i18n( $replies['trashed'] ) ) : '';
528
529 // Compile the hidden replies title
530 $hidden_reply_title = implode( ' | ', array_filter( $reply_titles ) );
531
532 }
533 }
534
535 // Topic Tags
536 if ( !empty( $count_tags ) ) {
537
538 // Get the count
539 $topic_tag_count = wp_count_terms( bbp_get_topic_tag_tax_id(), array( 'hide_empty' => true ) );
540
541 // Empty tags
542 if ( !empty( $count_empty_tags ) && current_user_can( 'edit_topic_tags' ) ) {
543 $empty_topic_tag_count = wp_count_terms( bbp_get_topic_tag_tax_id() ) - $topic_tag_count;
544 }
545 }
546
547 // Tally the tallies
548 $statistics = compact( 'user_count', 'forum_count', 'topic_count', 'topic_count_hidden', 'reply_count', 'reply_count_hidden', 'topic_tag_count', 'empty_topic_tag_count' );
549 $statistics = array_map( 'absint', $statistics );
550 $statistics = array_map( 'number_format_i18n', $statistics );
551
552 // Add the hidden (topic/reply) count title attribute strings because we don't need to run the math functions on these (see above)
553 if ( isset( $hidden_topic_title ) )
554 $statistics['hidden_topic_title'] = $hidden_topic_title;
555
556 if ( isset( $hidden_reply_title ) )
557 $statistics['hidden_reply_title'] = $hidden_reply_title;
558
559 return apply_filters( 'bbp_get_statistics', $statistics, $args );
560 }
561
562 /** New/edit topic/reply helpers **********************************************/
563
564 /**
565 * Filter anonymous post data
566 *
567 * We use REMOTE_ADDR here directly. If you are behind a proxy, you should
568 * ensure that it is properly set, such as in wp-config.php, for your
569 * environment. See {@link http://core.trac.wordpress.org/ticket/9235}
570 *
571 * Note that bbp_pre_anonymous_filters() is responsible for sanitizing each
572 * of the filtered core anonymous values here.
573 *
574 * If there are any errors, those are directly added to {@link bbPress:errors}
575 *
576 * @since bbPress (r2734)
577 *
578 * @param mixed $args Optional. If no args are there, then $_POST values are
579 * used.
580 * @uses apply_filters() Calls 'bbp_pre_anonymous_post_author_name' with the
581 * anonymous user name
582 * @uses apply_filters() Calls 'bbp_pre_anonymous_post_author_email' with the
583 * anonymous user email
584 * @uses apply_filters() Calls 'bbp_pre_anonymous_post_author_website' with the
585 * anonymous user website
586 * @return bool|array False on errors, values in an array on success
587 */
588 function bbp_filter_anonymous_post_data( $args = '' ) {
589
590 // Assign variables
591 $defaults = array (
592 'bbp_anonymous_name' => !empty( $_POST['bbp_anonymous_name'] ) ? $_POST['bbp_anonymous_name'] : false,
593 'bbp_anonymous_email' => !empty( $_POST['bbp_anonymous_email'] ) ? $_POST['bbp_anonymous_email'] : false,
594 'bbp_anonymous_website' => !empty( $_POST['bbp_anonymous_website'] ) ? $_POST['bbp_anonymous_website'] : false,
595 );
596 $r = bbp_parse_args( $args, $defaults, 'filter_anonymous_post_data' );
597 extract( $r );
598
599 // Filter variables and add errors if necessary
600 $bbp_anonymous_name = apply_filters( 'bbp_pre_anonymous_post_author_name', $bbp_anonymous_name );
601 if ( empty( $bbp_anonymous_name ) )
602 bbp_add_error( 'bbp_anonymous_name', __( '<strong>ERROR</strong>: Invalid author name submitted!', 'bbpress' ) );
603
604 $bbp_anonymous_email = apply_filters( 'bbp_pre_anonymous_post_author_email', $bbp_anonymous_email );
605 if ( empty( $bbp_anonymous_email ) )
606 bbp_add_error( 'bbp_anonymous_email', __( '<strong>ERROR</strong>: Invalid email address submitted!', 'bbpress' ) );
607
608 // Website is optional
609 $bbp_anonymous_website = apply_filters( 'bbp_pre_anonymous_post_author_website', $bbp_anonymous_website );
610
611 if ( !bbp_has_errors() )
612 $retval = compact( 'bbp_anonymous_name', 'bbp_anonymous_email', 'bbp_anonymous_website' );
613 else
614 $retval = false;
615
616 // Finally, return sanitized data or false
617 return apply_filters( 'bbp_filter_anonymous_post_data', $retval, $args );
618 }
619
620 /**
621 * Check for duplicate topics/replies
622 *
623 * Check to make sure that a user is not making a duplicate post
624 *
625 * @since bbPress (r2763)
626 *
627 * @param array $post_data Contains information about the comment
628 * @uses current_user_can() To check if the current user can throttle
629 * @uses get_meta_sql() To generate the meta sql for checking anonymous email
630 * @uses apply_filters() Calls 'bbp_check_for_duplicate_query' with the
631 * duplicate check query and post data
632 * @uses wpdb::get_var() To execute our query and get the var back
633 * @uses get_post_meta() To get the anonymous user email post meta
634 * @uses do_action() Calls 'bbp_post_duplicate_trigger' with the post data when
635 * it is found that it is a duplicate
636 * @return bool True if it is not a duplicate, false if it is
637 */
638 function bbp_check_for_duplicate( $post_data ) {
639
640 // No duplicate checks for those who can throttle
641 if ( current_user_can( 'throttle' ) )
642 return true;
643
644 global $wpdb;
645
646 extract( $post_data, EXTR_SKIP );
647
648 // Check for anonymous post
649 if ( empty( $post_author ) && ( isset( $anonymous_data ) && !empty( $anonymous_data['bbp_anonymous_email'] ) ) ) {
650 $clauses = get_meta_sql( array( array(
651 'key' => '_bbp_anonymous_email',
652 'value' => $anonymous_data['bbp_anonymous_email']
653 ) ), 'post', $wpdb->posts, 'ID' );
654
655 $join = $clauses['join'];
656 $where = $clauses['where'];
657 } else{
658 $join = $where = '';
659 }
660
661 // Simple duplicate check
662 // Expected slashed ($post_type, $post_parent, $post_author, $post_content, $anonymous_data)
663 $status = bbp_get_trash_status_id();
664 $dupe = "SELECT ID FROM {$wpdb->posts} {$join} WHERE post_type = '{$post_type}' AND post_status != '{$status}' AND post_author = {$post_author} AND post_content = '{$post_content}' {$where}";
665 $dupe .= !empty( $post_parent ) ? " AND post_parent = '{$post_parent}'" : '';
666 $dupe .= " LIMIT 1";
667 $dupe = apply_filters( 'bbp_check_for_duplicate_query', $dupe, $post_data );
668
669 if ( $wpdb->get_var( $dupe ) ) {
670 do_action( 'bbp_check_for_duplicate_trigger', $post_data );
671 return false;
672 }
673
674 return true;
675 }
676
677 /**
678 * Check for flooding
679 *
680 * Check to make sure that a user is not making too many posts in a short amount
681 * of time.
682 *
683 * @since bbPress (r2734)
684 *
685 * @param false|array $anonymous_data Optional - if it's an anonymous post. Do
686 * not supply if supplying $author_id.
687 * Should have key 'bbp_author_ip'.
688 * Should be sanitized (see
689 * {@link bbp_filter_anonymous_post_data()}
690 * for sanitization)
691 * @param int $author_id Optional. Supply if it's a post by a logged in user.
692 * Do not supply if supplying $anonymous_data.
693 * @uses get_option() To get the throttle time
694 * @uses get_transient() To get the last posted transient of the ip
695 * @uses bbp_get_user_last_posted() To get the last posted time of the user
696 * @uses current_user_can() To check if the current user can throttle
697 * @return bool True if there is no flooding, false if there is
698 */
699 function bbp_check_for_flood( $anonymous_data = false, $author_id = 0 ) {
700
701 // Option disabled. No flood checks.
702 $throttle_time = get_option( '_bbp_throttle_time' );
703 if ( empty( $throttle_time ) )
704 return true;
705
706 // User is anonymous, so check a transient based on the IP
707 if ( !empty( $anonymous_data ) && is_array( $anonymous_data ) ) {
708 $last_posted = get_transient( '_bbp_' . bbp_current_author_ip() . '_last_posted' );
709
710 if ( !empty( $last_posted ) && time() < $last_posted + $throttle_time ) {
711 return false;
712 }
713
714 // User is logged in, so check their last posted time
715 } elseif ( !empty( $author_id ) ) {
716 $author_id = (int) $author_id;
717 $last_posted = bbp_get_user_last_posted( $author_id );
718
719 if ( isset( $last_posted ) && time() < $last_posted + $throttle_time && !current_user_can( 'throttle' ) ) {
720 return false;
721 }
722 } else {
723 return false;
724 }
725
726 return true;
727 }
728
729 /**
730 * Checks topics and replies against the discussion moderation of blocked keys
731 *
732 * @since bbPress (r3581)
733 *
734 * @param array $anonymous_data Anonymous user data
735 * @param int $author_id Topic or reply author ID
736 * @param string $title The title of the content
737 * @param string $content The content being posted
738 * @uses is_super_admin() Allow super admins to bypass blacklist
739 * @uses bbp_current_author_ip() To get current user IP address
740 * @uses bbp_current_author_ua() To get current user agent
741 * @return bool True if test is passed, false if fail
742 */
743 function bbp_check_for_moderation( $anonymous_data = false, $author_id = 0, $title = '', $content = '' ) {
744
745 // Bail if super admin is author
746 if ( is_super_admin( $author_id ) )
747 return true;
748
749 // Define local variable(s)
750 $_post = array();
751 $match_out = '';
752
753 /** Blacklist *************************************************************/
754
755 // Get the moderation keys
756 $blacklist = trim( get_option( 'moderation_keys' ) );
757
758 // Bail if blacklist is empty
759 if ( empty( $blacklist ) )
760 return true;
761
762 /** User Data *************************************************************/
763
764 // Map anonymous user data
765 if ( !empty( $anonymous_data ) ) {
766 $_post['author'] = $anonymous_data['bbp_anonymous_name'];
767 $_post['email'] = $anonymous_data['bbp_anonymous_email'];
768 $_post['url'] = $anonymous_data['bbp_anonymous_website'];
769
770 // Map current user data
771 } elseif ( !empty( $author_id ) ) {
772
773 // Get author data
774 $user = get_userdata( $author_id );
775
776 // If data exists, map it
777 if ( !empty( $user ) ) {
778 $_post['author'] = $user->display_name;
779 $_post['email'] = $user->user_email;
780 $_post['url'] = $user->user_url;
781 }
782 }
783
784 // Current user IP and user agent
785 $_post['user_ip'] = bbp_current_author_ip();
786 $_post['user_ua'] = bbp_current_author_ua();
787
788 // Post title and content
789 $_post['title'] = $title;
790 $_post['content'] = $content;
791
792 /** Max Links *************************************************************/
793
794 $max_links = get_option( 'comment_max_links' );
795 if ( !empty( $max_links ) ) {
796
797 // How many links?
798 $num_links = preg_match_all( '/<a [^>]*href/i', $content, $match_out );
799
800 // Allow for bumping the max to include the user's URL
801 $num_links = apply_filters( 'comment_max_links_url', $num_links, $_post['url'] );
802
803 // Das ist zu viele links!
804 if ( $num_links >= $max_links ) {
805 return false;
806 }
807 }
808
809 /** Words *****************************************************************/
810
811 // Get words separated by new lines
812 $words = explode( "\n", $blacklist );
813
814 // Loop through words
815 foreach ( (array) $words as $word ) {
816
817 // Trim the whitespace from the word
818 $word = trim( $word );
819
820 // Skip empty lines
821 if ( empty( $word ) ) { continue; }
822
823 // Do some escaping magic so that '#' chars in the
824 // spam words don't break things:
825 $word = preg_quote( $word, '#' );
826 $pattern = "#$word#i";
827
828 // Loop through post data
829 foreach( $_post as $post_data ) {
830
831 // Check each user data for current word
832 if ( preg_match( $pattern, $post_data ) ) {
833
834 // Post does not pass
835 return false;
836 }
837 }
838 }
839
840 // Check passed successfully
841 return true;
842 }
843
844 /**
845 * Checks topics and replies against the discussion blacklist of blocked keys
846 *
847 * @since bbPress (r3446)
848 *
849 * @param array $anonymous_data Anonymous user data
850 * @param int $author_id Topic or reply author ID
851 * @param string $title The title of the content
852 * @param string $content The content being posted
853 * @uses is_super_admin() Allow super admins to bypass blacklist
854 * @uses bbp_current_author_ip() To get current user IP address
855 * @uses bbp_current_author_ua() To get current user agent
856 * @return bool True if test is passed, false if fail
857 */
858 function bbp_check_for_blacklist( $anonymous_data = false, $author_id = 0, $title = '', $content = '' ) {
859
860 // Bail if super admin is author
861 if ( is_super_admin( $author_id ) )
862 return true;
863
864 // Define local variable
865 $_post = array();
866
867 /** Blacklist *************************************************************/
868
869 // Get the moderation keys
870 $blacklist = trim( get_option( 'blacklist_keys' ) );
871
872 // Bail if blacklist is empty
873 if ( empty( $blacklist ) )
874 return true;
875
876 /** User Data *************************************************************/
877
878 // Map anonymous user data
879 if ( !empty( $anonymous_data ) ) {
880 $_post['author'] = $anonymous_data['bbp_anonymous_name'];
881 $_post['email'] = $anonymous_data['bbp_anonymous_email'];
882 $_post['url'] = $anonymous_data['bbp_anonymous_website'];
883
884 // Map current user data
885 } elseif ( !empty( $author_id ) ) {
886
887 // Get author data
888 $user = get_userdata( $author_id );
889
890 // If data exists, map it
891 if ( !empty( $user ) ) {
892 $_post['author'] = $user->display_name;
893 $_post['email'] = $user->user_email;
894 $_post['url'] = $user->user_url;
895 }
896 }
897
898 // Current user IP and user agent
899 $_post['user_ip'] = bbp_current_author_ip();
900 $_post['user_ua'] = bbp_current_author_ua();
901
902 // Post title and content
903 $_post['title'] = $title;
904 $_post['content'] = $content;
905
906 /** Words *****************************************************************/
907
908 // Get words separated by new lines
909 $words = explode( "\n", $blacklist );
910
911 // Loop through words
912 foreach ( (array) $words as $word ) {
913
914 // Trim the whitespace from the word
915 $word = trim( $word );
916
917 // Skip empty lines
918 if ( empty( $word ) ) { continue; }
919
920 // Do some escaping magic so that '#' chars in the
921 // spam words don't break things:
922 $word = preg_quote( $word, '#' );
923 $pattern = "#$word#i";
924
925 // Loop through post data
926 foreach( $_post as $post_data ) {
927
928 // Check each user data for current word
929 if ( preg_match( $pattern, $post_data ) ) {
930
931 // Post does not pass
932 return false;
933 }
934 }
935 }
936
937 // Check passed successfully
938 return true;
939 }
940
941 /** Subscriptions *************************************************************/
942
943 /**
944 * Sends notification emails for new posts
945 *
946 * Gets new post's ID and check if there are subscribed users to that topic, and
947 * if there are, send notifications
948 *
949 * @since bbPress (r2668)
950 *
951 * @param int $reply_id ID of the newly made reply
952 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
953 * @uses bbp_get_reply_id() To validate the reply ID
954 * @uses bbp_get_reply() To get the reply
955 * @uses bbp_get_reply_topic_id() To get the topic ID of the reply
956 * @uses bbp_is_reply_published() To make sure the reply is published
957 * @uses bbp_get_topic_id() To validate the topic ID
958 * @uses bbp_get_topic() To get the reply's topic
959 * @uses bbp_is_topic_published() To make sure the topic is published
960 * @uses get_the_author_meta() To get the author's display name
961 * @uses do_action() Calls 'bbp_pre_notify_subscribers' with the reply id and
962 * topic id
963 * @uses bbp_get_topic_subscribers() To get the topic subscribers
964 * @uses apply_filters() Calls 'bbp_subscription_mail_message' with the
965 * message, reply id, topic id and user id
966 * @uses get_userdata() To get the user data
967 * @uses wp_mail() To send the mail
968 * @uses do_action() Calls 'bbp_post_notify_subscribers' with the reply id
969 * and topic id
970 * @return bool True on success, false on failure
971 */
972 function bbp_notify_subscribers( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = false, $reply_author = 0 ) {
973
974 // Bail if subscriptions are turned off
975 if ( !bbp_is_subscriptions_active() )
976 return false;
977
978 /** Validation ************************************************************/
979
980 $reply_id = bbp_get_reply_id( $reply_id );
981 $topic_id = bbp_get_topic_id( $topic_id );
982 $forum_id = bbp_get_forum_id( $forum_id );
983
984 /** Reply *****************************************************************/
985
986 // Bail if reply is not published
987 if ( !bbp_is_reply_published( $reply_id ) )
988 return false;
989
990 /** Topic *****************************************************************/
991
992 // Bail if topic is not published
993 if ( !bbp_is_topic_published( $topic_id ) )
994 return false;
995
996 /** User ******************************************************************/
997
998 // Get subscribers and bail if empty
999 $user_ids = bbp_get_topic_subscribers( $topic_id, true );
1000 if ( empty( $user_ids ) )
1001 return false;
1002
1003 // Poster name
1004 $reply_author_name = bbp_get_reply_author_display_name( $reply_id );
1005
1006 /** Mail ******************************************************************/
1007
1008 do_action( 'bbp_pre_notify_subscribers', $reply_id, $topic_id, $user_ids );
1009
1010 // Remove filters from reply content and topic title to prevent content
1011 // from being encoded with HTML entities, wrapped in paragraph tags, etc...
1012 remove_all_filters( 'bbp_get_reply_content' );
1013 remove_all_filters( 'bbp_get_topic_title' );
1014
1015 // Strip tags from text
1016 $topic_title = strip_tags( bbp_get_topic_title( $topic_id ) );
1017 $reply_content = strip_tags( bbp_get_reply_content( $reply_id ) );
1018 $reply_url = bbp_get_reply_url( $reply_id );
1019 $blog_name = get_option( 'blogname' );
1020
1021 // Loop through users
1022 foreach ( (array) $user_ids as $user_id ) {
1023
1024 // Don't send notifications to the person who made the post
1025 if ( !empty( $reply_author ) && (int) $user_id == (int) $reply_author )
1026 continue;
1027
1028 // For plugins to filter messages per reply/topic/user
1029 $message = sprintf( __( '%1$s wrote:
1030
1031 %2$s
1032
1033 Post Link: %3$s
1034
1035 -----------
1036
1037 You are recieving this email because you subscribed to a forum topic.
1038
1039 Login and visit the topic to unsubscribe from these emails.', 'bbpress' ),
1040
1041 $reply_author_name,
1042 $reply_content,
1043 $reply_url
1044 );
1045
1046 $message = apply_filters( 'bbp_subscription_mail_message', $message, $reply_id, $topic_id, $user_id );
1047 if ( empty( $message ) )
1048 continue;
1049
1050 // For plugins to filter titles per reply/topic/user
1051 $subject = apply_filters( 'bbp_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $reply_id, $topic_id, $user_id );
1052 if ( empty( $subject ) )
1053 continue;
1054
1055 // Get user data of this user
1056 $user = get_userdata( $user_id );
1057
1058 // Send notification email
1059 wp_mail( $user->user_email, $subject, $message );
1060 }
1061
1062 do_action( 'bbp_post_notify_subscribers', $reply_id, $topic_id, $user_ids );
1063
1064 return true;
1065 }
1066
1067 /** Login *********************************************************************/
1068
1069 /**
1070 * Return a clean and reliable logout URL
1071 *
1072 * @param string $url URL
1073 * @param string $redirect_to Where to redirect to?
1074 * @uses add_query_arg() To add args to the url
1075 * @uses apply_filters() Calls 'bbp_logout_url' with the url and redirect to
1076 * @return string The url
1077 */
1078 function bbp_logout_url( $url = '', $redirect_to = '' ) {
1079
1080 // Make sure we are directing somewhere
1081 if ( empty( $redirect_to ) && !strstr( $url, 'redirect_to' ) ) {
1082
1083 // Rejig the $redirect_to
1084 if ( !isset( $_SERVER['REDIRECT_URL'] ) || ( $redirect_to != home_url( $_SERVER['REDIRECT_URL'] ) ) ) {
1085 $redirect_to = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : '';
1086 }
1087
1088 $redirect_to = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
1089
1090 // Sanitize $redirect_to and add it to full $url
1091 $redirect_to = add_query_arg( array( 'loggedout' => 'true' ), esc_url( $redirect_to ) );
1092 $url = add_query_arg( array( 'redirect_to' => urlencode( $redirect_to ) ), $url );
1093 }
1094
1095 // Filter and return
1096 return apply_filters( 'bbp_logout_url', $url, $redirect_to );
1097 }
1098
1099 /** Queries *******************************************************************/
1100
1101 /**
1102 * Merge user defined arguments into defaults array.
1103 *
1104 * This function is used throughout bbPress to allow for either a string or array
1105 * to be merged into another array. It is identical to wp_parse_args() except
1106 * it allows for arguments to be passively or aggressively filtered using the
1107 * optional $filter_key parameter.
1108 *
1109 * @since bbPress (r3839)
1110 *
1111 * @param string|array $args Value to merge with $defaults
1112 * @param array $defaults Array that serves as the defaults.
1113 * @param string $filter_key String to key the filters from
1114 * @return array Merged user defined values with defaults.
1115 */
1116 function bbp_parse_args( $args, $defaults = '', $filter_key = '' ) {
1117
1118 // Setup a temporary array from $args
1119 if ( is_object( $args ) )
1120 $r = get_object_vars( $args );
1121 elseif ( is_array( $args ) )
1122 $r =& $args;
1123 else
1124 wp_parse_str( $args, $r );
1125
1126 // Passively filter the args before the parse
1127 if ( !empty( $filter_key ) )
1128 $r = apply_filters( 'bbp_before_' . $filter_key . '_parse_args', $r );
1129
1130 // Parse
1131 if ( is_array( $defaults ) )
1132 $r = array_merge( $defaults, $r );
1133
1134 // Aggressively filter the args after the parse
1135 if ( !empty( $filter_key ) )
1136 $r = apply_filters( 'bbp_after_' . $filter_key . '_parse_args', $r );
1137
1138 // Return the parsed results
1139 return $r;
1140 }
1141
1142 /**
1143 * Adds ability to include or exclude specific post_parent ID's
1144 *
1145 * @since bbPress (r2996)
1146 *
1147 * @global DB $wpdb
1148 * @global WP $wp
1149 * @param string $where
1150 * @param WP_Query $object
1151 * @return string
1152 */
1153 function bbp_query_post_parent__in( $where, $object = '' ) {
1154 global $wpdb, $wp;
1155
1156 // Noop if WP core supports this already
1157 if ( in_array( 'post_parent__in', $wp->private_query_vars ) )
1158 return $where;
1159
1160 // Bail if no object passed
1161 if ( empty( $object ) )
1162 return $where;
1163
1164 // Only 1 post_parent so return $where
1165 if ( is_numeric( $object->query_vars['post_parent'] ) )
1166 return $where;
1167
1168 // Including specific post_parent's
1169 if ( ! empty( $object->query_vars['post_parent__in'] ) ) {
1170 $ids = implode( ',', array_map( 'absint', $object->query_vars['post_parent__in'] ) );
1171 $where .= " AND $wpdb->posts.post_parent IN ($ids)";
1172
1173 // Excluding specific post_parent's
1174 } elseif ( ! empty( $object->query_vars['post_parent__not_in'] ) ) {
1175 $ids = implode( ',', array_map( 'absint', $object->query_vars['post_parent__not_in'] ) );
1176 $where .= " AND $wpdb->posts.post_parent NOT IN ($ids)";
1177 }
1178
1179 // Return possibly modified $where
1180 return $where;
1181 }
1182
1183 /**
1184 * Query the DB and get the last public post_id that has parent_id as post_parent
1185 *
1186 * @param int $parent_id Parent id
1187 * @param string $post_type Post type. Defaults to 'post'
1188 * @uses bbp_get_topic_post_type() To get the topic post type
1189 * @uses wp_cache_get() To check if there is a cache of the last child id
1190 * @uses wpdb::prepare() To prepare the query
1191 * @uses wpdb::get_var() To get the result of the query in a variable
1192 * @uses wp_cache_set() To set the cache for future use
1193 * @uses apply_filters() Calls 'bbp_get_public_child_last_id' with the child
1194 * id, parent id and post type
1195 * @return int The last active post_id
1196 */
1197 function bbp_get_public_child_last_id( $parent_id = 0, $post_type = 'post' ) {
1198 global $wpdb;
1199
1200 // Bail if nothing passed
1201 if ( empty( $parent_id ) )
1202 return false;
1203
1204 // The ID of the cached query
1205 $cache_id = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_last_id';
1206 $post_status = array( bbp_get_public_status_id() );
1207
1208 // Add closed status if topic post type
1209 if ( $post_type == bbp_get_topic_post_type() )
1210 $post_status[] = bbp_get_closed_status_id();
1211
1212 // Join post statuses together
1213 $post_status = "'" . join( "', '", $post_status ) . "'";
1214
1215 // Check for cache and set if needed
1216 $child_id = wp_cache_get( $cache_id, 'bbpress' );
1217 if ( empty( $child_id ) ) {
1218 $child_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", $parent_id, $post_type ) );
1219 wp_cache_set( $cache_id, $child_id, 'bbpress' );
1220 }
1221
1222 // Filter and return
1223 return apply_filters( 'bbp_get_public_child_last_id', (int) $child_id, (int) $parent_id, $post_type );
1224 }
1225
1226 /**
1227 * Query the DB and get a count of public children
1228 *
1229 * @param int $parent_id Parent id
1230 * @param string $post_type Post type. Defaults to 'post'
1231 * @uses bbp_get_topic_post_type() To get the topic post type
1232 * @uses wp_cache_get() To check if there is a cache of the children count
1233 * @uses wpdb::prepare() To prepare the query
1234 * @uses wpdb::get_var() To get the result of the query in a variable
1235 * @uses wp_cache_set() To set the cache for future use
1236 * @uses apply_filters() Calls 'bbp_get_public_child_count' with the child
1237 * count, parent id and post type
1238 * @return int The number of children
1239 */
1240 function bbp_get_public_child_count( $parent_id = 0, $post_type = 'post' ) {
1241 global $wpdb;
1242
1243 // Bail if nothing passed
1244 if ( empty( $parent_id ) )
1245 return false;
1246
1247 // The ID of the cached query
1248 $cache_id = 'bbp_parent_' . $parent_id . '_type_' . $post_type . '_child_count';
1249 $post_status = array( bbp_get_public_status_id() );
1250
1251 // Add closed status if topic post type
1252 if ( $post_type == bbp_get_topic_post_type() )
1253 $post_status[] = bbp_get_closed_status_id();
1254
1255 // Join post statuses together
1256 $post_status = "'" . join( "', '", $post_status ) . "'";
1257
1258 // Check for cache and set if needed
1259 $child_count = wp_cache_get( $cache_id, 'bbpress' );
1260 if ( empty( $child_count ) ) {
1261 $child_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s';", $parent_id, $post_type ) );
1262 wp_cache_set( $cache_id, $child_count, 'bbpress' );
1263 }
1264
1265 // Filter and return
1266 return apply_filters( 'bbp_get_public_child_count', (int) $child_count, (int) $parent_id, $post_type );
1267 }
1268
1269 /**
1270 * Query the DB and get a the child id's of public children
1271 *
1272 * @param int $parent_id Parent id
1273 * @param string $post_type Post type. Defaults to 'post'
1274 * @uses bbp_get_topic_post_type() To get the topic post type
1275 * @uses wp_cache_get() To check if there is a cache of the children
1276 * @uses wpdb::prepare() To prepare the query
1277 * @uses wpdb::get_col() To get the result of the query in an array
1278 * @uses wp_cache_set() To set the cache for future use
1279 * @uses apply_filters() Calls 'bbp_get_public_child_ids' with the child ids,
1280 * parent id and post type
1281 * @return array The array of children
1282 */
1283 function bbp_get_public_child_ids( $parent_id = 0, $post_type = 'post' ) {
1284 global $wpdb;
1285
1286 // Bail if nothing passed
1287 if ( empty( $parent_id ) )
1288 return false;
1289
1290 // The ID of the cached query
1291 $cache_id = 'bbp_parent_public_' . $parent_id . '_type_' . $post_type . '_child_ids';
1292 $post_status = array( bbp_get_public_status_id() );
1293
1294 // Add closed status if topic post type
1295 if ( $post_type == bbp_get_topic_post_type() )
1296 $post_status[] = bbp_get_closed_status_id();
1297
1298 // Join post statuses together
1299 $post_status = "'" . join( "', '", $post_status ) . "'";
1300
1301 // Check for cache and set if needed
1302 $child_ids = wp_cache_get( $cache_id, 'bbpress' );
1303 if ( empty( $child_ids ) ) {
1304 $child_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type ) );
1305 wp_cache_set( $cache_id, $child_ids, 'bbpress' );
1306 }
1307
1308 // Filter and return
1309 return apply_filters( 'bbp_get_public_child_ids', $child_ids, (int) $parent_id, $post_type );
1310 }
1311 /**
1312 * Query the DB and get a the child id's of all children
1313 *
1314 * @param int $parent_id Parent id
1315 * @param string $post_type Post type. Defaults to 'post'
1316 * @uses bbp_get_topic_post_type() To get the topic post type
1317 * @uses wp_cache_get() To check if there is a cache of the children
1318 * @uses wpdb::prepare() To prepare the query
1319 * @uses wpdb::get_col() To get the result of the query in an array
1320 * @uses wp_cache_set() To set the cache for future use
1321 * @uses apply_filters() Calls 'bbp_get_public_child_ids' with the child ids,
1322 * parent id and post type
1323 * @return array The array of children
1324 */
1325 function bbp_get_all_child_ids( $parent_id = 0, $post_type = 'post' ) {
1326 global $wpdb;
1327
1328 // Bail if nothing passed
1329 if ( empty( $parent_id ) )
1330 return false;
1331
1332 // The ID of the cached query
1333 $cache_id = 'bbp_parent_all_' . $parent_id . '_type_' . $post_type . '_child_ids';
1334 $post_status = array( bbp_get_public_status_id() );
1335
1336 // Extra post statuses based on post type
1337 switch ( $post_type ) {
1338
1339 // Forum
1340 case bbp_get_forum_post_type() :
1341 $post_status[] = bbp_get_private_status_id();
1342 $post_status[] = bbp_get_hidden_status_id();
1343 break;
1344
1345 // Topic
1346 case bbp_get_topic_post_type() :
1347 $post_status[] = bbp_get_closed_status_id();
1348 $post_status[] = bbp_get_trash_status_id();
1349 $post_status[] = bbp_get_spam_status_id();
1350 break;
1351
1352 // Reply
1353 case bbp_get_reply_post_type() :
1354 $post_status[] = bbp_get_trash_status_id();
1355 $post_status[] = bbp_get_spam_status_id();
1356 break;
1357 }
1358
1359 // Join post statuses together
1360 $post_status = "'" . join( "', '", $post_status ) . "'";
1361
1362 // Check for cache and set if needed
1363 $child_ids = wp_cache_get( $cache_id, 'bbpress' );
1364 if ( empty( $child_ids ) ) {
1365 $child_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( {$post_status} ) AND post_type = '%s' ORDER BY ID DESC;", $parent_id, $post_type ) );
1366 wp_cache_set( $cache_id, $child_ids, 'bbpress' );
1367 }
1368
1369 // Filter and return
1370 return apply_filters( 'bbp_get_all_child_ids', $child_ids, (int) $parent_id, $post_type );
1371 }
1372
1373 /** Globals *******************************************************************/
1374
1375 /**
1376 * Get the unfiltered value of a global $post's key
1377 *
1378 * Used most frequently when editing a forum/topic/reply
1379 *
1380 * @since bbPress (r3694)
1381 *
1382 * @global WP_Query $post
1383 * @param string $field Name of the key
1384 * @param string $context How to sanitize - raw|edit|db|display|attribute|js
1385 * @return string Field value
1386 */
1387 function bbp_get_global_post_field( $field = 'ID', $context = 'edit' ) {
1388 global $post;
1389
1390 $retval = isset( $post->$field ) ? $post->$field : '';
1391 $retval = sanitize_post_field( $field, $retval, $post->ID, $context );
1392
1393 return apply_filters( 'bbp_get_global_post_field', $retval, $post );
1394 }
1395
1396 /** Feeds *********************************************************************/
1397
1398 /**
1399 * This function is hooked into the WordPress 'request' action and is
1400 * responsible for sniffing out the query vars and serving up RSS2 feeds if
1401 * the stars align and the user has requested a feed of any bbPress type.
1402 *
1403 * @since bbPress (r3171)
1404 *
1405 * @param array $query_vars
1406 * @return array
1407 */
1408 function bbp_request_feed_trap( $query_vars = array() ) {
1409
1410 // Looking at a feed
1411 if ( isset( $query_vars['feed'] ) ) {
1412
1413 // Forum/Topic/Reply Feed
1414 if ( isset( $query_vars['post_type'] ) ) {
1415
1416 // What bbPress post type are we looking for feeds on?
1417 switch ( $query_vars['post_type'] ) {
1418
1419 // Forum
1420 case bbp_get_forum_post_type() :
1421
1422 // Define local variable(s)
1423 $meta_query = array();
1424
1425 // Single forum
1426 if ( isset( $query_vars[bbp_get_forum_post_type()] ) ) {
1427
1428 // Get the forum by the path
1429 $forum = get_page_by_path( $query_vars[bbp_get_forum_post_type()], OBJECT, bbp_get_forum_post_type() );
1430 $forum_id = $forum->ID;
1431
1432 // Load up our own query
1433 query_posts( array(
1434 'post_type' => bbp_get_forum_post_type(),
1435 'ID' => $forum_id,
1436 'feed' => true
1437 ) );
1438
1439 // Restrict to specific forum ID
1440 $meta_query = array( array(
1441 'key' => '_bbp_forum_id',
1442 'value' => $forum_id,
1443 'compare' => '='
1444 ) );
1445 }
1446
1447 // Only forum replies
1448 if ( !empty( $_GET['type'] ) && ( bbp_get_reply_post_type() == $_GET['type'] ) ) {
1449
1450 // The query
1451 $the_query = array(
1452 'author' => 0,
1453 'feed' => true,
1454 'post_type' => bbp_get_reply_post_type(),
1455 'post_parent' => 'any',
1456 'post_status' => join( ',', array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ),
1457 'posts_per_page' => bbp_get_replies_per_rss_page(),
1458 'order' => 'DESC',
1459 'meta_query' => $meta_query
1460 );
1461
1462 // Output the feed
1463 bbp_display_replies_feed_rss2( $the_query );
1464
1465 // Only forum topics
1466 } elseif ( !empty( $_GET['type'] ) && ( bbp_get_topic_post_type() == $_GET['type'] ) ) {
1467
1468 // The query
1469 $the_query = array(
1470 'author' => 0,
1471 'feed' => true,
1472 'post_type' => bbp_get_topic_post_type(),
1473 'post_parent' => 'any',
1474 'post_status' => join( ',', array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ),
1475 'posts_per_page' => bbp_get_topics_per_rss_page(),
1476 'order' => 'DESC',
1477 'meta_query' => $meta_query
1478 );
1479
1480 // Output the feed
1481 bbp_display_topics_feed_rss2( $the_query );
1482
1483 // All forum topics and replies
1484 } else {
1485
1486 // Exclude private/hidden forums if not looking at single
1487 if ( empty( $query_vars['forum'] ) )
1488 $meta_query = array( bbp_exclude_forum_ids( 'meta_query' ) );
1489
1490 // The query
1491 $the_query = array(
1492 'author' => 0,
1493 'feed' => true,
1494 'post_type' => array( bbp_get_reply_post_type(), bbp_get_topic_post_type() ),
1495 'post_parent' => 'any',
1496 'post_status' => join( ',', array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) ),
1497 'posts_per_page' => bbp_get_replies_per_rss_page(),
1498 'order' => 'DESC',
1499 'meta_query' => $meta_query
1500 );
1501
1502 // Output the feed
1503 bbp_display_replies_feed_rss2( $the_query );
1504 }
1505
1506 break;
1507
1508 // Topic feed - Show replies
1509 case bbp_get_topic_post_type() :
1510
1511 // Single topic
1512 if ( isset( $query_vars[bbp_get_topic_post_type()] ) ) {
1513
1514 // Load up our own query
1515 query_posts( array(
1516 'post_type' => bbp_get_topic_post_type(),
1517 'name' => $query_vars[bbp_get_topic_post_type()],
1518 'feed' => true
1519 ) );
1520
1521 // Output the feed
1522 bbp_display_replies_feed_rss2();
1523
1524 // All topics
1525 } else {
1526
1527 // The query
1528 $the_query = array(
1529 'author' => 0,
1530 'feed' => true,
1531 'post_parent' => 'any',
1532 'posts_per_page' => bbp_get_topics_per_rss_page(),
1533 'show_stickies' => false
1534 );
1535
1536 // Output the feed
1537 bbp_display_topics_feed_rss2( $the_query );
1538 }
1539
1540 break;
1541
1542 // Replies
1543 case bbp_get_reply_post_type() :
1544
1545 // The query
1546 $the_query = array(
1547 'posts_per_page' => bbp_get_replies_per_rss_page(),
1548 'meta_query' => array( array( ) ),
1549 'feed' => true
1550 );
1551
1552 // All replies
1553 if ( !isset( $query_vars[bbp_get_reply_post_type()] ) ) {
1554 bbp_display_replies_feed_rss2( $the_query );
1555 }
1556
1557 break;
1558 }
1559
1560 // Single Topic Vview
1561 } elseif ( isset( $query_vars['bbp_view'] ) ) {
1562
1563 // Get the view
1564 $view = $query_vars['bbp_view'];
1565
1566 // We have a view to display a feed
1567 if ( !empty( $view ) ) {
1568
1569 // Get the view query
1570 $the_query = bbp_get_view_query_args( $view );
1571
1572 // Output the feed
1573 bbp_display_topics_feed_rss2( $the_query );
1574 }
1575 }
1576
1577 // @todo User profile feeds
1578 }
1579
1580 // No feed so continue on
1581 return $query_vars;
1582 }
1583
1584 /** Templates ******************************************************************/
1585
1586 /**
1587 * Used to guess if page exists at requested path
1588 *
1589 * @since bbPress (r3304)
1590 *
1591 * @uses get_option() To see if pretty permalinks are enabled
1592 * @uses get_page_by_path() To see if page exists at path
1593 *
1594 * @param string $path
1595 * @return mixed False if no page, Page object if true
1596 */
1597 function bbp_get_page_by_path( $path = '' ) {
1598
1599 // Default to false
1600 $retval = false;
1601
1602 // Path is not empty
1603 if ( !empty( $path ) ) {
1604
1605 // Pretty permalinks are on so path might exist
1606 if ( get_option( 'permalink_structure' ) ) {
1607 $retval = get_page_by_path( $path );
1608 }
1609 }
1610
1611 return apply_filters( 'bbp_get_page_by_path', $retval, $path );
1612 }
1613
1614 /**
1615 * Sets the 404 status.
1616 *
1617 * Used primarily with topics/replies inside hidden forums.
1618 *
1619 * @since bbPress (r3051)
1620 *
1621 * @global WP_Query $wp_query
1622 * @uses WP_Query::set_404()
1623 */
1624 function bbp_set_404() {
1625 global $wp_query;
1626
1627 if ( ! isset( $wp_query ) ) {
1628 _doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '3.1' );
1629 return false;
1630 }
1631
1632 $wp_query->set_404();
1633 }
1634