| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* bbPress Forum Functions |
| 5 |
* |
| 6 |
* @package bbPress |
| 7 |
* @subpackage Functions |
| 8 |
*/ |
| 9 |
|
| 10 |
// Redirect if accessed directly |
| 11 |
if ( !defined( 'ABSPATH' ) ) exit; |
| 12 |
|
| 13 |
/** Walk **********************************************************************/ |
| 14 |
|
| 15 |
/** |
| 16 |
* Walk the forum tree |
| 17 |
* |
| 18 |
* @param object $forums Forums |
| 19 |
* @param int $depth Depth |
| 20 |
* @param int $current Current forum |
| 21 |
* @param array $r Parsed arguments, supported by the walker. If you want to |
| 22 |
* use your own walker, pass the 'walker' arg with the walker. |
| 23 |
* The walker defaults to {@link BBP_Walker_Forum} |
| 24 |
* @return object Walked forum tree |
| 25 |
*/ |
| 26 |
function bbp_walk_forum( $forums, $depth, $current, $r ) { |
| 27 |
$walker = empty( $r['walker'] ) ? new BBP_Walker_Forum : $r['walker']; |
| 28 |
$args = array( $forums, $depth, $r, $current ); |
| 29 |
return call_user_func_array( array( &$walker, 'walk' ), $args ); |
| 30 |
} |
| 31 |
|
| 32 |
/** Forum Handlers ************************************************************/ |
| 33 |
|
| 34 |
/** |
| 35 |
* Handles new forum craetion from within wp-admin |
| 36 |
* |
| 37 |
* @since bbPress (r2613) |
| 38 |
* |
| 39 |
* @param int $forum_id |
| 40 |
* @param obj $forum |
| 41 |
* @uses bbp_get_forum_post_type() To get the forum post type |
| 42 |
* @uses bbp_update_forum() To update the forum |
| 43 |
*/ |
| 44 |
function bbp_new_forum_admin_handler( $forum_id, $forum ) { |
| 45 |
|
| 46 |
if ( // Check if POST action |
| 47 |
'POST' === $_SERVER['REQUEST_METHOD'] && |
| 48 |
|
| 49 |
// Check Actions exist in POST |
| 50 |
!empty( $_POST['action'] ) && |
| 51 |
!empty( $_POST['post_type'] ) && |
| 52 |
|
| 53 |
// Check that actions match what we need |
| 54 |
'editpost' === $_POST['action'] && |
| 55 |
bbp_get_forum_post_type() === $_POST['post_type'] |
| 56 |
) { |
| 57 |
|
| 58 |
// Update the forum meta bidness |
| 59 |
bbp_update_forum( array( 'forum_id' => $forum_id, 'post_parent' => (int) $_POST['parent_id'] ) ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
/** Forum Actions *************************************************************/ |
| 65 |
|
| 66 |
/** |
| 67 |
* Closes a forum |
| 68 |
* |
| 69 |
* @since bbPress (r2746) |
| 70 |
* |
| 71 |
* @param int $forum_id forum id |
| 72 |
* @uses wp_get_single_post() To get the forum |
| 73 |
* @uses do_action() Calls 'bbp_close_forum' with the forum id |
| 74 |
* @uses add_post_meta() To add the previous status to a meta |
| 75 |
* @uses wp_insert_post() To update the forum with the new status |
| 76 |
* @uses do_action() Calls 'bbp_opened_forum' with the forum id |
| 77 |
* @return mixed False or {@link WP_Error} on failure, forum id on success |
| 78 |
*/ |
| 79 |
function bbp_close_forum( $forum_id = 0 ) { |
| 80 |
|
| 81 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 82 |
|
| 83 |
do_action( 'bbp_close_forum', $forum_id ); |
| 84 |
|
| 85 |
update_post_meta( $forum_id, '_bbp_status', 'closed' ); |
| 86 |
|
| 87 |
do_action( 'bbp_closed_forum', $forum_id ); |
| 88 |
|
| 89 |
return $forum_id; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Opens a forum |
| 94 |
* |
| 95 |
* @since bbPress (r2746) |
| 96 |
* |
| 97 |
* @param int $forum_id forum id |
| 98 |
* @uses wp_get_single_post() To get the forum |
| 99 |
* @uses do_action() Calls 'bbp_open_forum' with the forum id |
| 100 |
* @uses get_post_meta() To get the previous status |
| 101 |
* @uses delete_post_meta() To delete the previous status meta |
| 102 |
* @uses wp_insert_post() To update the forum with the new status |
| 103 |
* @uses do_action() Calls 'bbp_opened_forum' with the forum id |
| 104 |
* @return mixed False or {@link WP_Error} on failure, forum id on success |
| 105 |
*/ |
| 106 |
function bbp_open_forum( $forum_id = 0 ) { |
| 107 |
|
| 108 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 109 |
|
| 110 |
do_action( 'bbp_open_forum', $forum_id ); |
| 111 |
|
| 112 |
update_post_meta( $forum_id, '_bbp_status', 'open' ); |
| 113 |
|
| 114 |
do_action( 'bbp_opened_forum', $forum_id ); |
| 115 |
|
| 116 |
return $forum_id; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Make the forum a category |
| 121 |
* |
| 122 |
* @since bbPress (r2746) |
| 123 |
* |
| 124 |
* @param int $forum_id Optional. Forum id |
| 125 |
* @uses update_post_meta() To update the forum category meta |
| 126 |
* @return bool False on failure, true on success |
| 127 |
*/ |
| 128 |
function bbp_categorize_forum( $forum_id = 0 ) { |
| 129 |
|
| 130 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 131 |
|
| 132 |
do_action( 'bbp_categorize_forum', $forum_id ); |
| 133 |
|
| 134 |
update_post_meta( $forum_id, '_bbp_forum_type', 'category' ); |
| 135 |
|
| 136 |
do_action( 'bbp_categorized_forum', $forum_id ); |
| 137 |
|
| 138 |
return $forum_id; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Remove the category status from a forum |
| 143 |
* |
| 144 |
* @since bbPress (r2746) |
| 145 |
* |
| 146 |
* @param int $forum_id Optional. Forum id |
| 147 |
* @uses delete_post_meta() To delete the forum category meta |
| 148 |
* @return bool False on failure, true on success |
| 149 |
*/ |
| 150 |
function bbp_normalize_forum( $forum_id = 0 ) { |
| 151 |
|
| 152 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 153 |
|
| 154 |
do_action( 'bbp_normalize_forum', $forum_id ); |
| 155 |
|
| 156 |
update_post_meta( $forum_id, '_bbp_forum_type', 'forum' ); |
| 157 |
|
| 158 |
do_action( 'bbp_normalized_forum', $forum_id ); |
| 159 |
|
| 160 |
return $forum_id; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Mark the forum as public |
| 165 |
* |
| 166 |
* @since bbPress (r2746) |
| 167 |
* |
| 168 |
* @param int $forum_id Optional. Forum id |
| 169 |
* @uses update_post_meta() To update the forum private meta |
| 170 |
* @return bool False on failure, true on success |
| 171 |
*/ |
| 172 |
function bbp_publicize_forum( $forum_id = 0, $current_visibility = '' ) { |
| 173 |
|
| 174 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 175 |
|
| 176 |
do_action( 'bbp_publicize_forum', $forum_id ); |
| 177 |
|
| 178 |
// Only run queries if visibility is changing |
| 179 |
if ( 'publish' != $current_visibility ) { |
| 180 |
|
| 181 |
// Remove from _bbp_private_forums site option |
| 182 |
if ( 'private' == $current_visibility ) { |
| 183 |
|
| 184 |
// Get private forums |
| 185 |
$private = bbp_get_private_forum_ids(); |
| 186 |
|
| 187 |
// Find this forum in the array |
| 188 |
if ( in_array( $forum_id, $private ) ) { |
| 189 |
|
| 190 |
$offset = array_search( $forum_id, (array) $private ); |
| 191 |
|
| 192 |
// Splice around it |
| 193 |
array_splice( $private, $offset, 1 ); |
| 194 |
|
| 195 |
// Update private forums minus this one |
| 196 |
update_option( '_bbp_private_forums', array_unique( array_values( $private ) ) ); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
// Remove from _bbp_hidden_forums site option |
| 201 |
if ( 'hidden' == $current_visibility ) { |
| 202 |
|
| 203 |
// Get hidden forums |
| 204 |
$hidden = bbp_get_hidden_forum_ids(); |
| 205 |
|
| 206 |
// Find this forum in the array |
| 207 |
if ( in_array( $forum_id, $hidden ) ) { |
| 208 |
|
| 209 |
$offset = array_search( $forum_id, (array) $hidden ); |
| 210 |
|
| 211 |
// Splice around it |
| 212 |
array_splice( $hidden, $offset, 1 ); |
| 213 |
|
| 214 |
// Update hidden forums minus this one |
| 215 |
update_option( '_bbp_hidden_forums', array_unique( array_values( $hidden ) ) ); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
// Update forum post_status |
| 220 |
global $wpdb; |
| 221 |
$wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $forum_id ) ); |
| 222 |
wp_transition_post_status( 'publish', $current_visibility, get_post( $forum_id ) ); |
| 223 |
} |
| 224 |
|
| 225 |
do_action( 'bbp_publicized_forum', $forum_id ); |
| 226 |
|
| 227 |
return $forum_id; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Mark the forum as private |
| 232 |
* |
| 233 |
* @since bbPress (r2746) |
| 234 |
* |
| 235 |
* @param int $forum_id Optional. Forum id |
| 236 |
* @uses update_post_meta() To update the forum private meta |
| 237 |
* @return bool False on failure, true on success |
| 238 |
*/ |
| 239 |
function bbp_privatize_forum( $forum_id = 0, $current_visibility = '' ) { |
| 240 |
|
| 241 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 242 |
|
| 243 |
do_action( 'bbp_privatize_forum', $forum_id ); |
| 244 |
|
| 245 |
// Only run queries if visibility is changing |
| 246 |
if ( 'private' != $current_visibility ) { |
| 247 |
|
| 248 |
// Remove from _bbp_hidden_forums site option |
| 249 |
if ( 'hidden' == $current_visibility ) { |
| 250 |
|
| 251 |
// Get hidden forums |
| 252 |
$hidden = bbp_get_hidden_forum_ids(); |
| 253 |
|
| 254 |
// Find this forum in the array |
| 255 |
if ( in_array( $forum_id, $hidden ) ) { |
| 256 |
|
| 257 |
$offset = array_search( $forum_id, (array) $hidden ); |
| 258 |
|
| 259 |
// Splice around it |
| 260 |
array_splice( $hidden, $offset, 1 ); |
| 261 |
|
| 262 |
// Update hidden forums minus this one |
| 263 |
update_option( '_bbp_hidden_forums', array_unique( array_values( $hidden ) ) ); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
// Add to '_bbp_private_forums' site option |
| 268 |
$private = bbp_get_private_forum_ids(); |
| 269 |
$private[] = $forum_id; |
| 270 |
update_option( '_bbp_private_forums', array_unique( array_values( $private ) ) ); |
| 271 |
|
| 272 |
// Update forums visibility setting |
| 273 |
global $wpdb; |
| 274 |
$wpdb->update( $wpdb->posts, array( 'post_status' => 'private' ), array( 'ID' => $forum_id ) ); |
| 275 |
wp_transition_post_status( 'private', $current_visibility, get_post( $forum_id ) ); |
| 276 |
} |
| 277 |
|
| 278 |
do_action( 'bbp_privatized_forum', $forum_id ); |
| 279 |
|
| 280 |
return $forum_id; |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Mark the forum as hidden |
| 285 |
* |
| 286 |
* @since bbPress (r2996) |
| 287 |
* |
| 288 |
* @param int $forum_id Optional. Forum id |
| 289 |
* @uses update_post_meta() To update the forum private meta |
| 290 |
* @return bool False on failure, true on success |
| 291 |
*/ |
| 292 |
function bbp_hide_forum( $forum_id = 0, $current_visibility = '' ) { |
| 293 |
|
| 294 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 295 |
|
| 296 |
do_action( 'bbp_hide_forum', $forum_id ); |
| 297 |
|
| 298 |
// Only run queries if visibility is changing |
| 299 |
if ( 'hidden' != $current_visibility ) { |
| 300 |
|
| 301 |
// Remove from _bbp_private_forums site option |
| 302 |
if ( 'private' == $current_visibility ) { |
| 303 |
|
| 304 |
// Get private forums |
| 305 |
$private = bbp_get_private_forum_ids(); |
| 306 |
|
| 307 |
// Find this forum in the array |
| 308 |
if ( in_array( $forum_id, $private ) ) { |
| 309 |
|
| 310 |
$offset = array_search( $forum_id, (array) $private ); |
| 311 |
|
| 312 |
// Splice around it |
| 313 |
array_splice( $private, $offset, 1 ); |
| 314 |
|
| 315 |
// Update private forums minus this one |
| 316 |
update_option( '_bbp_private_forums', array_unique( array_values( $private ) ) ); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
// Add to '_bbp_hidden_forums' site option |
| 321 |
$hidden = bbp_get_hidden_forum_ids(); |
| 322 |
$hidden[] = $forum_id; |
| 323 |
update_option( '_bbp_hidden_forums', array_unique( array_values( $hidden ) ) ); |
| 324 |
|
| 325 |
// Update forums visibility setting |
| 326 |
global $bbp, $wpdb; |
| 327 |
$wpdb->update( $wpdb->posts, array( 'post_status' => 'hidden' ), array( 'ID' => $forum_id ) ); |
| 328 |
wp_transition_post_status( $bbp->hidden_status_id, $current_visibility, get_post( $forum_id ) ); |
| 329 |
} |
| 330 |
|
| 331 |
do_action( 'bbp_hid_forum', $forum_id ); |
| 332 |
|
| 333 |
return $forum_id; |
| 334 |
} |
| 335 |
|
| 336 |
/** Forum Updaters ************************************************************/ |
| 337 |
|
| 338 |
/** |
| 339 |
* Update the forum last topic id |
| 340 |
* |
| 341 |
* @since bbPress (r2625) |
| 342 |
* |
| 343 |
* @param int $forum_id Optional. Forum id |
| 344 |
* @param int $topic_id Optional. Topic id |
| 345 |
* @uses bbp_get_forum_id() To get the forum id |
| 346 |
* @uses bbp_forum_query_subforum_ids() To get the subforum ids |
| 347 |
* @uses bbp_update_forum_last_topic_id() To update the last topic id of child |
| 348 |
* forums |
| 349 |
* @uses get_posts() To get the most recent topic in the forum |
| 350 |
* @uses update_post_meta() To update the forum's last active id meta |
| 351 |
* @uses apply_filters() Calls 'bbp_update_forum_last_topic_id' with the last |
| 352 |
* reply id and forum id |
| 353 |
* @return bool True on success, false on failure |
| 354 |
*/ |
| 355 |
function bbp_update_forum_last_topic_id( $forum_id = 0, $topic_id = 0 ) { |
| 356 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 357 |
|
| 358 |
// Do some calculation if not manually set |
| 359 |
if ( empty( $topic_id ) ) { |
| 360 |
|
| 361 |
// Loop through children and add together forum reply counts |
| 362 |
if ( $children = bbp_forum_query_subforum_ids( $forum_id ) ) |
| 363 |
foreach ( (array) $children as $child ) |
| 364 |
$children_last_topic = bbp_update_forum_last_topic_id ( $child ); |
| 365 |
|
| 366 |
// Setup recent topic query vars |
| 367 |
$post_vars = array( |
| 368 |
'post_parent' => $forum_id, |
| 369 |
'post_type' => bbp_get_topic_post_type(), |
| 370 |
'meta_key' => '_bbp_last_active_time', |
| 371 |
'orderby' => 'meta_value', |
| 372 |
'numberposts' => 1 |
| 373 |
); |
| 374 |
|
| 375 |
// Get the most recent topic in this forum_id |
| 376 |
if ( $recent_topic = get_posts( $post_vars ) ) |
| 377 |
$topic_id = $recent_topic[0]->ID; |
| 378 |
} |
| 379 |
|
| 380 |
// If child forums have higher id, use that instead |
| 381 |
if ( !empty( $children ) && ( $children_last_topic > $topic_id ) ) |
| 382 |
$topic_id = $children_last_topic; |
| 383 |
|
| 384 |
// Update the last topic id |
| 385 |
update_post_meta( $forum_id, '_bbp_last_topic_id', (int) $topic_id ); |
| 386 |
|
| 387 |
return apply_filters( 'bbp_update_forum_last_topic_id', (int) $topic_id, $forum_id ); |
| 388 |
} |
| 389 |
|
| 390 |
/** |
| 391 |
* Update the forum last reply id |
| 392 |
* |
| 393 |
* @since bbPress (r2625) |
| 394 |
* |
| 395 |
* @param int $forum_id Optional. Forum id |
| 396 |
* @param int $reply_id Optional. Reply id |
| 397 |
* @uses bbp_get_forum_id() To get the forum id |
| 398 |
* @uses bbp_forum_query_subforum_ids() To get the subforum ids |
| 399 |
* @uses bbp_update_forum_last_reply_id() To update the last reply id of child |
| 400 |
* forums |
| 401 |
* @uses bbp_forum_query_topic_ids() To get the topic ids in the forum |
| 402 |
* @uses bbp_forum_query_last_reply_id() To get the forum's last reply id |
| 403 |
* @uses update_post_meta() To update the forum's last active id meta |
| 404 |
* @uses apply_filters() Calls 'bbp_update_forum_last_reply_id' with the last |
| 405 |
* reply id and forum id |
| 406 |
* @return bool True on success, false on failure |
| 407 |
*/ |
| 408 |
function bbp_update_forum_last_reply_id( $forum_id = 0, $reply_id = 0 ) { |
| 409 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 410 |
|
| 411 |
// Do some calculation if not manually set |
| 412 |
if ( empty( $reply_id ) ) { |
| 413 |
|
| 414 |
// Loop through children and get the most recent reply id |
| 415 |
if ( $children = bbp_forum_query_subforum_ids( $forum_id ) ) |
| 416 |
foreach ( (array) $children as $child ) |
| 417 |
$children_last_reply = bbp_update_forum_last_reply_id ( $child ); |
| 418 |
|
| 419 |
// If this forum has topics... |
| 420 |
if ( $topic_ids = bbp_forum_query_topic_ids( $forum_id ) ) { |
| 421 |
|
| 422 |
// ...get the most recent reply from those topics... |
| 423 |
$reply_id = bbp_forum_query_last_reply_id( $forum_id, $topic_ids ); |
| 424 |
|
| 425 |
// ...and compare it to the most recent topic id... |
| 426 |
$reply_id = ( $reply_id > max( $topic_ids ) ) ? $reply_id : max( $topic_ids ); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
// If child forums have higher ID, check for newer reply id |
| 431 |
if ( !empty( $children ) && ( (int) $children_last_reply > (int) $reply_id ) ) |
| 432 |
$reply_id = $children_last_reply; |
| 433 |
|
| 434 |
// Update the last reply id with what was passed |
| 435 |
update_post_meta( $forum_id, '_bbp_last_reply_id', (int) $reply_id ); |
| 436 |
|
| 437 |
return apply_filters( 'bbp_update_forum_last_reply_id', (int) $reply_id, $forum_id ); |
| 438 |
} |
| 439 |
|
| 440 |
/** |
| 441 |
* Update the forum last active post id |
| 442 |
* |
| 443 |
* @since bbPress (r2860) |
| 444 |
* |
| 445 |
* @param int $forum_id Optional. Forum id |
| 446 |
* @param int $active_id Optional. Active post id |
| 447 |
* @uses bbp_get_forum_id() To get the forum id |
| 448 |
* @uses bbp_forum_query_subforum_ids() To get the subforum ids |
| 449 |
* @uses bbp_update_forum_last_active_id() To update the last active id of |
| 450 |
* child forums |
| 451 |
* @uses bbp_forum_query_topic_ids() To get the topic ids in the forum |
| 452 |
* @uses bbp_forum_query_last_reply_id() To get the forum's last reply id |
| 453 |
* @uses update_post_meta() To update the forum's last active id meta |
| 454 |
* @uses apply_filters() Calls 'bbp_update_forum_last_active_id' with the last |
| 455 |
* active post id and forum id |
| 456 |
* @return bool True on success, false on failure |
| 457 |
*/ |
| 458 |
function bbp_update_forum_last_active_id( $forum_id = 0, $active_id = 0 ) { |
| 459 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 460 |
|
| 461 |
// Do some calculation if not manually set |
| 462 |
if ( empty( $active_id ) ) { |
| 463 |
|
| 464 |
// Loop through children and add together forum reply counts |
| 465 |
if ( $children = bbp_forum_query_subforum_ids( $forum_id ) ) |
| 466 |
foreach ( (array) $children as $child ) |
| 467 |
$children_last_active = bbp_update_forum_last_active_id ( $child, $active_id ); |
| 468 |
|
| 469 |
// Don't count replies if the forum is a category |
| 470 |
if ( $topic_ids = bbp_forum_query_topic_ids( $forum_id ) ) { |
| 471 |
$active_id = bbp_forum_query_last_reply_id( $forum_id, $topic_ids ); |
| 472 |
$active_id = $active_id > max( $topic_ids ) ? $active_id : max( $topic_ids ); |
| 473 |
|
| 474 |
// Forum has no topics |
| 475 |
} else { |
| 476 |
$active_id = 0; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
// If child forums have higher id, use that instead |
| 481 |
if ( !empty( $children ) && ( $children_last_active > $active_id ) ) |
| 482 |
$active_id = $children_last_active; |
| 483 |
|
| 484 |
update_post_meta( $forum_id, '_bbp_last_active_id', (int) $active_id ); |
| 485 |
|
| 486 |
return apply_filters( 'bbp_update_forum_last_active_id', (int) $active_id, $forum_id ); |
| 487 |
} |
| 488 |
|
| 489 |
/** |
| 490 |
* Update the forums last active date/time (aka freshness) |
| 491 |
* |
| 492 |
* @since bbPress (r2680) |
| 493 |
* |
| 494 |
* @param int $forum_id Optional. Topic id |
| 495 |
* @param string $new_time Optional. New time in mysql format |
| 496 |
* @uses bbp_get_forum_id() To get the forum id |
| 497 |
* @uses bbp_get_forum_last_active_id() To get the forum's last post id |
| 498 |
* @uses get_post_field() To get the post date of the forum's last post |
| 499 |
* @uses update_post_meta() To update the forum last active time |
| 500 |
* @uses apply_filters() Calls 'bbp_update_forum_last_active' with the new time |
| 501 |
* and forum id |
| 502 |
* @return bool True on success, false on failure |
| 503 |
*/ |
| 504 |
function bbp_update_forum_last_active_time( $forum_id = 0, $new_time = '' ) { |
| 505 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 506 |
|
| 507 |
// Check time and use current if empty |
| 508 |
if ( empty( $new_time ) ) |
| 509 |
$new_time = get_post_field( 'post_date', bbp_get_forum_last_active_id( $forum_id ) ); |
| 510 |
|
| 511 |
update_post_meta( $forum_id, '_bbp_last_active_time', $new_time ); |
| 512 |
|
| 513 |
return apply_filters( 'bbp_update_forum_last_active', $new_time, $forum_id ); |
| 514 |
} |
| 515 |
|
| 516 |
/** |
| 517 |
* Update the forum sub-forum count |
| 518 |
* |
| 519 |
* @since bbPress (r2625) |
| 520 |
* |
| 521 |
* @param int $forum_id Optional. Forum id |
| 522 |
* @uses bbp_get_forum_id() To get the forum id |
| 523 |
* @return bool True on success, false on failure |
| 524 |
*/ |
| 525 |
function bbp_update_forum_subforum_count( $forum_id = 0, $subforums = 0 ) { |
| 526 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 527 |
|
| 528 |
if ( empty( $subforums ) ) |
| 529 |
$subforums = count( bbp_forum_query_subforum_ids( $forum_id ) ); |
| 530 |
|
| 531 |
update_post_meta( $forum_id, '_bbp_forum_subforum_count', (int) $subforums ); |
| 532 |
|
| 533 |
return apply_filters( 'bbp_update_forum_subforum_count', (int) $subforums, $forum_id ); |
| 534 |
} |
| 535 |
|
| 536 |
/** |
| 537 |
* Adjust the total topic count of a forum |
| 538 |
* |
| 539 |
* @since bbPress (r2464) |
| 540 |
* |
| 541 |
* @param int $forum_id Optional. Forum id or topic id. It is checked whether it |
| 542 |
* is a topic or a forum. If it's a topic, its parent, |
| 543 |
* i.e. the forum is automatically retrieved. |
| 544 |
* @param bool $total_count Optional. To return the total count or normal |
| 545 |
* count? |
| 546 |
* @uses bbp_get_forum_id() To get the forum id |
| 547 |
* @uses bbp_forum_query_subforum_ids() To get the subforum ids |
| 548 |
* @uses bbp_update_forum_topic_count() To update the forum topic count |
| 549 |
* @uses bbp_forum_query_topic_ids() To get the forum topic ids |
| 550 |
* @uses update_post_meta() To update the forum's topic count meta |
| 551 |
* @uses apply_filters() Calls 'bbp_update_forum_topic_count' with the topic |
| 552 |
* count and forum id |
| 553 |
* @return int Forum topic count |
| 554 |
*/ |
| 555 |
function bbp_update_forum_topic_count( $forum_id = 0 ) { |
| 556 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 557 |
$children_topic_count = 0; |
| 558 |
|
| 559 |
// Loop through subforums and add together forum topic counts |
| 560 |
if ( $children = bbp_forum_query_subforum_ids( $forum_id ) ) |
| 561 |
foreach ( (array) $children as $child ) |
| 562 |
$children_topic_count += bbp_update_forum_topic_count( $child ); |
| 563 |
|
| 564 |
// Get total topics for this forum |
| 565 |
$topics = (int) count( bbp_forum_query_topic_ids( $forum_id ) ); |
| 566 |
|
| 567 |
// Calculate total topics in this forum |
| 568 |
$total_topics = $topics + $children_topic_count; |
| 569 |
|
| 570 |
// Update the count |
| 571 |
update_post_meta( $forum_id, '_bbp_forum_topic_count', (int) $topics ); |
| 572 |
update_post_meta( $forum_id, '_bbp_total_topic_count', (int) $total_topics ); |
| 573 |
|
| 574 |
return apply_filters( 'bbp_update_forum_topic_count', (int) $total_topics, $forum_id ); |
| 575 |
} |
| 576 |
|
| 577 |
/** |
| 578 |
* Adjust the total hidden topic count of a forum (hidden includes trashed and spammed topics) |
| 579 |
* |
| 580 |
* @since bbPress (r2888) |
| 581 |
* |
| 582 |
* @param int $forum_id Optional. Topic id to update |
| 583 |
* @param int $topic_count Optional. Set the topic count manually |
| 584 |
* @uses bbp_is_topic() To check if the supplied id is a topic |
| 585 |
* @uses bbp_get_topic_id() To get the topic id |
| 586 |
* @uses bbp_get_topic_forum_id() To get the topic forum id |
| 587 |
* @uses bbp_get_forum_id() To get the forum id |
| 588 |
* @uses wpdb::prepare() To prepare our sql query |
| 589 |
* @uses wpdb::get_col() To execute our query and get the column back |
| 590 |
* @uses update_post_meta() To update the forum hidden topic count meta |
| 591 |
* @uses apply_filters() Calls 'bbp_update_forum_hidden_topic_count' with the |
| 592 |
* hidden topic count and forum id |
| 593 |
* @return int Topic hidden topic count |
| 594 |
*/ |
| 595 |
function bbp_update_forum_hidden_topic_count( $forum_id = 0, $topic_count = 0 ) { |
| 596 |
global $wpdb, $bbp; |
| 597 |
|
| 598 |
// If topic_id was passed as $forum_id, then get its forum |
| 599 |
if ( bbp_is_topic( $forum_id ) ) { |
| 600 |
$topic_id = bbp_get_topic_id( $forum_id ); |
| 601 |
$forum_id = bbp_get_topic_forum_id( $topic_id ); |
| 602 |
|
| 603 |
// $forum_id is not a topic_id, so validate and proceed |
| 604 |
} else { |
| 605 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 606 |
} |
| 607 |
|
| 608 |
// Can't update what isn't there |
| 609 |
if ( !empty( $forum_id ) ) { |
| 610 |
|
| 611 |
// Get topics of forum |
| 612 |
if ( empty( $topic_count ) ) |
| 613 |
$topic_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( '" . join( '\',\'', array( $bbp->trash_status_id, $bbp->spam_status_id ) ) . "') AND post_type = '%s';", $forum_id, bbp_get_topic_post_type() ) ); |
| 614 |
|
| 615 |
// Update the count |
| 616 |
update_post_meta( $forum_id, '_bbp_topic_count_hidden', (int) $topic_count ); |
| 617 |
} |
| 618 |
|
| 619 |
return apply_filters( 'bbp_update_forum_hidden_topic_count', (int) $topic_count, $forum_id ); |
| 620 |
} |
| 621 |
|
| 622 |
/** |
| 623 |
* Adjust the total reply count of a forum |
| 624 |
* |
| 625 |
* @since bbPress (r2464) |
| 626 |
* |
| 627 |
* @param int $forum_id Optional. Forum id or topic id. It is checked whether it |
| 628 |
* is a topic or a forum. If it's a topic, its parent, |
| 629 |
* i.e. the forum is automatically retrieved. |
| 630 |
* @param bool $total_count Optional. To return the total count or normal |
| 631 |
* count? |
| 632 |
* @uses bbp_get_forum_id() To get the forum id |
| 633 |
* @uses bbp_forum_query_subforum_ids() To get the subforum ids |
| 634 |
* @uses bbp_update_forum_reply_count() To update the forum reply count |
| 635 |
* @uses bbp_forum_query_topic_ids() To get the forum topic ids |
| 636 |
* @uses wpdb::prepare() To prepare the sql statement |
| 637 |
* @uses wpdb::get_var() To execute the query and get the var back |
| 638 |
* @uses update_post_meta() To update the forum's reply count meta |
| 639 |
* @uses apply_filters() Calls 'bbp_update_forum_topic_count' with the reply |
| 640 |
* count and forum id |
| 641 |
* @return int Forum reply count |
| 642 |
*/ |
| 643 |
function bbp_update_forum_reply_count( $forum_id = 0 ) { |
| 644 |
global $wpdb, $bbp; |
| 645 |
|
| 646 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 647 |
$children_reply_count = 0; |
| 648 |
|
| 649 |
// Loop through children and add together forum reply counts |
| 650 |
if ( $children = bbp_forum_query_subforum_ids( $forum_id ) ) |
| 651 |
foreach ( (array) $children as $child ) |
| 652 |
$children_reply_count += bbp_update_forum_reply_count( $child ); |
| 653 |
|
| 654 |
// Don't count replies if the forum is a category |
| 655 |
if ( $topic_ids = bbp_forum_query_topic_ids( $forum_id ) ) |
| 656 |
$reply_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent IN ( " . join( ',', $topic_ids ) . " ) AND post_status = 'publish' AND post_type = '%s';", bbp_get_reply_post_type() ) ); |
| 657 |
else |
| 658 |
$reply_count = 0; |
| 659 |
|
| 660 |
// Calculate total replies in this forum |
| 661 |
$total_replies = (int) $reply_count + $children_reply_count; |
| 662 |
|
| 663 |
// Update the count |
| 664 |
update_post_meta( $forum_id, '_bbp_reply_count', $reply_count ); |
| 665 |
update_post_meta( $forum_id, '_bbp_total_reply_count', $total_replies ); |
| 666 |
|
| 667 |
return apply_filters( 'bbp_update_forum_reply_count', $total_replies, $forum_id ); |
| 668 |
} |
| 669 |
|
| 670 |
/** |
| 671 |
* Updates the counts of a forum. |
| 672 |
* |
| 673 |
* This calls a few internal functions that all run manual queries against the |
| 674 |
* database to get their results. As such, this function can be costly to run |
| 675 |
* but is necessary to keep everything accurate. |
| 676 |
* |
| 677 |
* @since bbPress (r2908) |
| 678 |
* |
| 679 |
* @param mixed $args Supports these arguments: |
| 680 |
* - forum_id: Forum id |
| 681 |
* - last_topic_id: Last topic id |
| 682 |
* - last_reply_id: Last reply id |
| 683 |
* - last_active_id: Last active post id |
| 684 |
* - last_active_time: last active time |
| 685 |
* @uses bbp_update_forum_last_topic_id() To update the forum last topic id |
| 686 |
* @uses bbp_update_forum_last_reply_id() To update the forum last reply id |
| 687 |
* @uses bbp_update_forum_last_active_id() To update the last active post id |
| 688 |
* @uses get_post_field() To get the post date of the last active id |
| 689 |
* @uses bbp_update_forum_last_active_time() To update the last active time |
| 690 |
* @uses bbp_update_forum_subforum_count() To update the subforum count |
| 691 |
* @uses bbp_update_forum_topic_count() To update the forum topic count |
| 692 |
* @uses bbp_update_forum_reply_count() To update the forum reply count |
| 693 |
* @uses bbp_update_forum_hidden_topic_count() To update the hidden topic count |
| 694 |
*/ |
| 695 |
function bbp_update_forum( $args = '' ) { |
| 696 |
$defaults = array( |
| 697 |
'forum_id' => 0, |
| 698 |
'post_parent' => 0, |
| 699 |
'last_topic_id' => 0, |
| 700 |
'last_reply_id' => 0, |
| 701 |
'last_active_id' => 0, |
| 702 |
'last_active_time' => 0, |
| 703 |
); |
| 704 |
|
| 705 |
$r = wp_parse_args( $args, $defaults ); |
| 706 |
extract( $r ); |
| 707 |
|
| 708 |
// Last topic and reply ID's |
| 709 |
bbp_update_forum_last_topic_id( $forum_id, $last_topic_id ); |
| 710 |
bbp_update_forum_last_reply_id( $forum_id, $last_reply_id ); |
| 711 |
|
| 712 |
// Active dance |
| 713 |
$last_active_id = bbp_update_forum_last_active_id( $forum_id, $last_active_id ); |
| 714 |
|
| 715 |
// If no active time was passed, get it from the last_active_id |
| 716 |
if ( empty( $last_active_time ) ) |
| 717 |
$last_active_time = get_post_field( 'post_date', $last_active_id ); |
| 718 |
|
| 719 |
bbp_update_forum_last_active_time( $forum_id, $last_active_time ); |
| 720 |
|
| 721 |
// Counts |
| 722 |
bbp_update_forum_subforum_count ( $forum_id ); |
| 723 |
bbp_update_forum_reply_count ( $forum_id ); |
| 724 |
bbp_update_forum_topic_count ( $forum_id ); |
| 725 |
bbp_update_forum_hidden_topic_count( $forum_id ); |
| 726 |
|
| 727 |
// Update the parent forum if one was passed |
| 728 |
if ( !empty( $post_parent ) && is_numeric( $post_parent ) ) { |
| 729 |
bbp_update_forum( array( |
| 730 |
'forum_id' => $post_parent, |
| 731 |
'post_parent' => get_post_field( 'post_parent', $post_parent ) |
| 732 |
) ); |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
/** Queries *******************************************************************/ |
| 737 |
|
| 738 |
/** |
| 739 |
* Returns the hidden forum ids |
| 740 |
* |
| 741 |
* Only hidden forum ids are returned. Public and private ids are not. |
| 742 |
* |
| 743 |
* @since bbPress (r3007) |
| 744 |
* |
| 745 |
* @uses get_option() Returns the unserialized array of hidden forum ids |
| 746 |
* @uses apply_filters() Calls 'bbp_forum_query_topic_ids' with the topic ids |
| 747 |
* and forum id |
| 748 |
*/ |
| 749 |
function bbp_get_hidden_forum_ids() { |
| 750 |
$forum_ids = get_option( '_bbp_hidden_forums', array() ); |
| 751 |
|
| 752 |
return apply_filters( 'bbp_get_hidden_forum_ids', (array) $forum_ids ); |
| 753 |
} |
| 754 |
|
| 755 |
/** |
| 756 |
* Returns the private forum ids |
| 757 |
* |
| 758 |
* Only private forum ids are returned. Public and hidden ids are not. |
| 759 |
* |
| 760 |
* @since bbPress (r3007) |
| 761 |
* |
| 762 |
* @uses get_option() Returns the unserialized array of private forum ids |
| 763 |
* @uses apply_filters() Calls 'bbp_forum_query_topic_ids' with the topic ids |
| 764 |
* and forum id |
| 765 |
*/ |
| 766 |
function bbp_get_private_forum_ids() { |
| 767 |
$forum_ids = get_option( '_bbp_private_forums', array() ); |
| 768 |
|
| 769 |
return apply_filters( 'bbp_get_private_forum_ids', (array) $forum_ids ); |
| 770 |
} |
| 771 |
|
| 772 |
/** |
| 773 |
* Returns a meta_query that either includes or excludes hidden forum IDs |
| 774 |
* from a query. |
| 775 |
* |
| 776 |
* @uses is_super_admin() |
| 777 |
* @uses bbp_is_user_home() |
| 778 |
* @uses bbp_get_hidden_forum_ids() |
| 779 |
* @uses bbp_get_private_forum_ids() |
| 780 |
*/ |
| 781 |
function bbp_exclude_forum_ids( $query = array() ) { |
| 782 |
|
| 783 |
// Do not exclude for super admins |
| 784 |
if ( is_super_admin() ) |
| 785 |
return $query; |
| 786 |
|
| 787 |
// Setup arrays |
| 788 |
$private = $hidden = array(); |
| 789 |
|
| 790 |
// Private forums |
| 791 |
if ( !current_user_can( 'read_private_forums' ) ) |
| 792 |
$private = bbp_get_private_forum_ids(); |
| 793 |
|
| 794 |
// Hidden forums |
| 795 |
if ( !current_user_can( 'read_hidden_forums' ) ) |
| 796 |
$hidden = bbp_get_hidden_forum_ids(); |
| 797 |
|
| 798 |
// Merge private and hidden forums together |
| 799 |
$forum_ids = (array) array_filter( array_merge( $private, $hidden ) ); |
| 800 |
|
| 801 |
// There are forums that need to be excluded |
| 802 |
if ( !empty( $forum_ids ) ) { |
| 803 |
|
| 804 |
// Setup a meta_query to remove hidden forums |
| 805 |
$value = implode( ',', $forum_ids ); |
| 806 |
$compare = ( 1 < count( $forum_ids ) ) ? 'NOT IN' : '!='; |
| 807 |
|
| 808 |
// Add meta_query to $replies_query |
| 809 |
$meta_query['meta_query'] = array( array( |
| 810 |
'key' => '_bbp_forum_id', |
| 811 |
'value' => $value, |
| 812 |
'compare' => $compare |
| 813 |
) ); |
| 814 |
|
| 815 |
// Merge the queries together |
| 816 |
if ( !empty( $meta_query ) ) { |
| 817 |
$query = array_merge( $query, $meta_query ); |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
return apply_filters( 'bbp_exclude_forum_ids', $query, $meta_query ); |
| 822 |
} |
| 823 |
|
| 824 |
/** |
| 825 |
* Returns the forum's topic ids |
| 826 |
* |
| 827 |
* Only topics with published and closed statuses are returned |
| 828 |
* |
| 829 |
* @since bbPress (r2908) |
| 830 |
* |
| 831 |
* @param int $forum_id Forum id |
| 832 |
* @uses bbp_get_topic_post_type() To get the topic post type |
| 833 |
* @uses bbp_get_public_child_ids() To get the topic ids |
| 834 |
* @uses apply_filters() Calls 'bbp_forum_query_topic_ids' with the topic ids |
| 835 |
* and forum id |
| 836 |
*/ |
| 837 |
function bbp_forum_query_topic_ids( $forum_id ) { |
| 838 |
global $bbp; |
| 839 |
|
| 840 |
$topic_ids = bbp_get_public_child_ids( $forum_id, bbp_get_topic_post_type() ); |
| 841 |
|
| 842 |
return apply_filters( 'bbp_forum_query_topic_ids', $topic_ids, $forum_id ); |
| 843 |
} |
| 844 |
|
| 845 |
/** |
| 846 |
* Returns the forum's subforum ids |
| 847 |
* |
| 848 |
* Only forums with published status are returned |
| 849 |
* |
| 850 |
* @since bbPress (r2908) |
| 851 |
* |
| 852 |
* @param int $forum_id Forum id |
| 853 |
* @uses bbp_get_forum_post_type() To get the forum post type |
| 854 |
* @uses bbp_get_public_child_ids() To get the forum ids |
| 855 |
* @uses apply_filters() Calls 'bbp_forum_query_subforum_ids' with the subforum |
| 856 |
* ids and forum id |
| 857 |
*/ |
| 858 |
function bbp_forum_query_subforum_ids( $forum_id ) { |
| 859 |
global $bbp, $wpdb; |
| 860 |
|
| 861 |
$subforum_ids = bbp_get_public_child_ids( $forum_id, bbp_get_forum_post_type() ); |
| 862 |
|
| 863 |
return apply_filters( 'bbp_get_forum_subforum_ids', $subforum_ids, $forum_id ); |
| 864 |
} |
| 865 |
|
| 866 |
/** |
| 867 |
* Returns the forum's last reply id |
| 868 |
* |
| 869 |
* @since bbPress (r2908) |
| 870 |
* |
| 871 |
* @param int $forum_id Forum id |
| 872 |
* @param int $topic_ids Optional. Topic ids |
| 873 |
* @uses wp_cache_get() To check for cache and retrieve it |
| 874 |
* @uses bbp_forum_query_topic_ids() To get the forum's topic ids |
| 875 |
* @uses wpdb::prepare() To prepare the query |
| 876 |
* @uses wpdb::get_var() To execute the query and get the var back |
| 877 |
* @uses bbp_get_reply_post_type() To get the reply post type |
| 878 |
* @uses wp_cache_set() To set the cache for future use |
| 879 |
* @uses apply_filters() Calls 'bbp_forum_query_last_reply_id' with the reply id |
| 880 |
* and forum id |
| 881 |
*/ |
| 882 |
function bbp_forum_query_last_reply_id( $forum_id, $topic_ids = 0 ) { |
| 883 |
global $bbp, $wpdb; |
| 884 |
|
| 885 |
$cache_id = 'bbp_get_forum_' . $forum_id . '_reply_id'; |
| 886 |
|
| 887 |
if ( !$reply_id = (int) wp_cache_get( $cache_id, 'bbpress' ) ) { |
| 888 |
|
| 889 |
if ( empty( $topic_ids ) ) |
| 890 |
$topic_ids = bbp_forum_query_topic_ids( $forum_id ); |
| 891 |
|
| 892 |
if ( !empty( $topic_ids ) && ( $reply_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent IN ( " . join( ',', $topic_ids ) . " ) AND post_status = 'publish' AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", bbp_get_reply_post_type() ) ) ) ) |
| 893 |
wp_cache_set( $cache_id, $reply_id, 'bbpress' ); |
| 894 |
else |
| 895 |
wp_cache_set( $cache_id, '0', 'bbpress' ); |
| 896 |
} |
| 897 |
|
| 898 |
return apply_filters( 'bbp_get_forum_last_reply_id', (int) $reply_id, $forum_id ); |
| 899 |
} |
| 900 |
|
| 901 |
/** Listeners *****************************************************************/ |
| 902 |
|
| 903 |
/** |
| 904 |
* Check if it's a private forum or a topic or reply of a private forum and if |
| 905 |
* the user can't view it, then sets a 404 |
| 906 |
* |
| 907 |
* @since bbPress (r2996) |
| 908 |
* |
| 909 |
* @uses current_user_can() To check if the current user can read private forums |
| 910 |
* @uses is_singular() To check if it's a singular page |
| 911 |
* @uses bbp_get_forum_post_type() To get the forum post type |
| 912 |
* @uses bbp_get_topic_post_type() To get the topic post type |
| 913 |
* @uses bbp_get_reply_post_type() TO get the reply post type |
| 914 |
* @uses bbp_get_topic_forum_id() To get the topic forum id |
| 915 |
* @uses bbp_get_reply_forum_id() To get the reply forum id |
| 916 |
* @uses bbp_is_forum_private() To check if the forum is private or not |
| 917 |
* @uses bbp_set_404() To set a 404 status |
| 918 |
*/ |
| 919 |
function bbp_forum_visibility_check() { |
| 920 |
global $wp_query; |
| 921 |
|
| 922 |
// Bail if not viewing a single item or if user has caps |
| 923 |
if ( !is_singular() || is_super_admin() || ( current_user_can( 'read_private_forums' ) && current_user_can( 'read_hidden_forums' ) ) ) |
| 924 |
return; |
| 925 |
|
| 926 |
// Check post type |
| 927 |
switch ( $wp_query->get( 'post_type' ) ) { |
| 928 |
|
| 929 |
// Forum |
| 930 |
case bbp_get_forum_post_type() : |
| 931 |
$forum_id = bbp_get_forum_id( $wp_query->post->ID ); |
| 932 |
break; |
| 933 |
|
| 934 |
// Topic |
| 935 |
case bbp_get_topic_post_type() : |
| 936 |
$forum_id = bbp_get_topic_forum_id( $wp_query->post->ID ); |
| 937 |
break; |
| 938 |
|
| 939 |
// Reply |
| 940 |
case bbp_get_reply_post_type() : |
| 941 |
$forum_id = bbp_get_reply_forum_id( $wp_query->post->ID ); |
| 942 |
break; |
| 943 |
|
| 944 |
} |
| 945 |
|
| 946 |
// If forum is explicitly hidden and user not capable, set 404 |
| 947 |
if ( !empty( $forum_id ) && bbp_is_forum_hidden( $forum_id ) && !current_user_can( 'read_hidden_forums' ) ) |
| 948 |
bbp_set_404(); |
| 949 |
} |
| 950 |
|
| 951 |
?> |
| 952 |
|