| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* bbPress Forum Functions |
| 5 |
* |
| 6 |
* @package bbPress |
| 7 |
* @subpackage Functions |
| 8 |
*/ |
| 9 |
|
| 10 |
// Exit if accessed directly |
| 11 |
if ( !defined( 'ABSPATH' ) ) exit; |
| 12 |
|
| 13 |
/** Insert ********************************************************************/ |
| 14 |
|
| 15 |
/** |
| 16 |
* A wrapper for wp_insert_post() that also includes the necessary meta values |
| 17 |
* for the forum to function properly. |
| 18 |
* |
| 19 |
* @since bbPress (r3349) |
| 20 |
* |
| 21 |
* @uses bbp_parse_args() |
| 22 |
* @uses bbp_get_forum_post_type() |
| 23 |
* @uses wp_insert_post() |
| 24 |
* @uses update_post_meta() |
| 25 |
* |
| 26 |
* @param array $forum_data Forum post data |
| 27 |
* @param arrap $forum_meta Forum meta data |
| 28 |
*/ |
| 29 |
function bbp_insert_forum( $forum_data = array(), $forum_meta = array() ) { |
| 30 |
|
| 31 |
// Forum |
| 32 |
$default_forum = array( |
| 33 |
'post_parent' => 0, // forum ID |
| 34 |
'post_status' => bbp_get_public_status_id(), |
| 35 |
'post_type' => bbp_get_forum_post_type(), |
| 36 |
'post_author' => bbp_get_current_user_id(), |
| 37 |
'post_password' => '', |
| 38 |
'post_content' => '', |
| 39 |
'post_title' => '', |
| 40 |
'menu_order' => 0, |
| 41 |
'comment_status' => 'closed' |
| 42 |
); |
| 43 |
$forum_data = bbp_parse_args( $forum_data, $default_forum, 'insert_forum' ); |
| 44 |
|
| 45 |
// Insert forum |
| 46 |
$forum_id = wp_insert_post( $forum_data ); |
| 47 |
|
| 48 |
// Bail if no forum was added |
| 49 |
if ( empty( $forum_id ) ) |
| 50 |
return false; |
| 51 |
|
| 52 |
// Forum meta |
| 53 |
$default_meta = array( |
| 54 |
'reply_count' => 0, |
| 55 |
'topic_count' => 0, |
| 56 |
'topic_count_hidden' => 0, |
| 57 |
'total_reply_count' => 0, |
| 58 |
'total_topic_count' => 0, |
| 59 |
'last_topic_id' => 0, |
| 60 |
'last_reply_id' => 0, |
| 61 |
'last_active_id' => 0, |
| 62 |
'last_active_time' => 0, |
| 63 |
'forum_subforum_count' => 0, |
| 64 |
); |
| 65 |
$forum_meta = bbp_parse_args( $forum_meta, $default_meta, 'insert_forum_meta' ); |
| 66 |
|
| 67 |
// Insert forum meta |
| 68 |
foreach ( $forum_meta as $meta_key => $meta_value ) |
| 69 |
update_post_meta( $forum_id, '_bbp_' . $meta_key, $meta_value ); |
| 70 |
|
| 71 |
// Return new forum ID |
| 72 |
return $forum_id; |
| 73 |
} |
| 74 |
|
| 75 |
/** Post Form Handlers ********************************************************/ |
| 76 |
|
| 77 |
/** |
| 78 |
* Handles the front end forum submission |
| 79 |
* |
| 80 |
* @uses bbPress:errors::add() To log various error messages |
| 81 |
* @uses bbp_verify_nonce_request() To verify the nonce and check the request |
| 82 |
* @uses bbp_is_anonymous() To check if an anonymous post is being made |
| 83 |
* @uses current_user_can() To check if the current user can publish forum |
| 84 |
* @uses bbp_get_current_user_id() To get the current user id |
| 85 |
* @uses bbp_filter_anonymous_post_data() To filter anonymous data |
| 86 |
* @uses bbp_set_current_anonymous_user_data() To set the anonymous user cookies |
| 87 |
* @uses is_wp_error() To check if the value retrieved is a {@link WP_Error} |
| 88 |
* @uses esc_attr() For sanitization |
| 89 |
* @uses bbp_is_forum_category() To check if the forum is a category |
| 90 |
* @uses bbp_is_forum_closed() To check if the forum is closed |
| 91 |
* @uses bbp_is_forum_private() To check if the forum is private |
| 92 |
* @uses bbp_check_for_flood() To check for flooding |
| 93 |
* @uses bbp_check_for_duplicate() To check for duplicates |
| 94 |
* @uses bbp_get_forum_post_type() To get the forum post type |
| 95 |
* @uses remove_filter() To remove 'wp_filter_kses' filters if needed |
| 96 |
* @uses apply_filters() Calls 'bbp_new_forum_pre_title' with the content |
| 97 |
* @uses apply_filters() Calls 'bbp_new_forum_pre_content' with the content |
| 98 |
* @uses bbPress::errors::get_error_codes() To get the {@link WP_Error} errors |
| 99 |
* @uses wp_insert_post() To insert the forum |
| 100 |
* @uses do_action() Calls 'bbp_new_forum' with the forum id, forum id, |
| 101 |
* anonymous data and reply author |
| 102 |
* @uses bbp_stick_forum() To stick or super stick the forum |
| 103 |
* @uses bbp_unstick_forum() To unstick the forum |
| 104 |
* @uses bbp_get_forum_permalink() To get the forum permalink |
| 105 |
* @uses wp_safe_redirect() To redirect to the forum link |
| 106 |
* @uses bbPress::errors::get_error_messages() To get the {@link WP_Error} error |
| 107 |
* messages |
| 108 |
*/ |
| 109 |
function bbp_new_forum_handler() { |
| 110 |
|
| 111 |
// Bail if not a POST action |
| 112 |
if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) |
| 113 |
return; |
| 114 |
|
| 115 |
// Bail if action is not bbp-new-forum |
| 116 |
if ( empty( $_POST['action'] ) || ( 'bbp-new-forum' !== $_POST['action'] ) ) |
| 117 |
return; |
| 118 |
|
| 119 |
// Nonce check |
| 120 |
if ( ! bbp_verify_nonce_request( 'bbp-new-forum' ) ) { |
| 121 |
bbp_add_error( 'bbp_new_forum_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
// Define local variable(s) |
| 126 |
$view_all = $anonymous_data = false; |
| 127 |
$forum_parent_id = $forum_author = 0; |
| 128 |
$forum_title = $forum_content = ''; |
| 129 |
|
| 130 |
/** Forum Author **********************************************************/ |
| 131 |
|
| 132 |
// User cannot create forums |
| 133 |
if ( !current_user_can( 'publish_forums' ) ) { |
| 134 |
bbp_add_error( 'bbp_forum_permissions', __( '<strong>ERROR</strong>: You do not have permission to create new forums.', 'bbpress' ) ); |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
// Forum author is current user |
| 139 |
$forum_author = bbp_get_current_user_id(); |
| 140 |
|
| 141 |
// Remove wp_filter_kses filters from title and content for capable users and if the nonce is verified |
| 142 |
if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_forum'] ) && wp_create_nonce( 'bbp-unfiltered-html-forum_new' ) == $_POST['_bbp_unfiltered_html_forum'] ) { |
| 143 |
remove_filter( 'bbp_new_forum_pre_title', 'wp_filter_kses' ); |
| 144 |
remove_filter( 'bbp_new_forum_pre_content', 'wp_filter_kses' ); |
| 145 |
} |
| 146 |
|
| 147 |
/** Forum Title ***********************************************************/ |
| 148 |
|
| 149 |
if ( !empty( $_POST['bbp_forum_title'] ) ) |
| 150 |
$forum_title = esc_attr( strip_tags( $_POST['bbp_forum_title'] ) ); |
| 151 |
|
| 152 |
// Filter and sanitize |
| 153 |
$forum_title = apply_filters( 'bbp_new_forum_pre_title', $forum_title ); |
| 154 |
|
| 155 |
// No forum title |
| 156 |
if ( empty( $forum_title ) ) |
| 157 |
bbp_add_error( 'bbp_forum_title', __( '<strong>ERROR</strong>: Your forum needs a title.', 'bbpress' ) ); |
| 158 |
|
| 159 |
/** Forum Content *********************************************************/ |
| 160 |
|
| 161 |
if ( !empty( $_POST['bbp_forum_content'] ) ) |
| 162 |
$forum_content = $_POST['bbp_forum_content']; |
| 163 |
|
| 164 |
// Filter and sanitize |
| 165 |
$forum_content = apply_filters( 'bbp_new_forum_pre_content', $forum_content ); |
| 166 |
|
| 167 |
// No forum content |
| 168 |
if ( empty( $forum_content ) ) |
| 169 |
bbp_add_error( 'bbp_forum_content', __( '<strong>ERROR</strong>: Your forum description cannot be empty.', 'bbpress' ) ); |
| 170 |
|
| 171 |
/** Forum Parent **********************************************************/ |
| 172 |
|
| 173 |
// Forum parent was passed (the norm) |
| 174 |
if ( !empty( $_POST['bbp_forum_parent_id'] ) ) |
| 175 |
$forum_parent_id = (int) $_POST['bbp_forum_parent_id']; |
| 176 |
|
| 177 |
// Filter and sanitize |
| 178 |
$forum_parent_id = apply_filters( 'bbp_new_forum_pre_parent_id', $forum_parent_id ); |
| 179 |
|
| 180 |
// No forum parent was passed (should never happen) |
| 181 |
if ( empty( $forum_parent_id ) ) { |
| 182 |
bbp_add_error( 'bbp_new_forum_missing_parent', __( '<strong>ERROR</strong>: Your forum must have a parent.', 'bbpress' ) ); |
| 183 |
|
| 184 |
// Forum exists |
| 185 |
} elseif ( !empty( $forum_parent_id ) ) { |
| 186 |
|
| 187 |
// Forum is a category |
| 188 |
if ( bbp_is_forum_category( $forum_parent_id ) ) { |
| 189 |
bbp_add_error( 'bbp_new_forum_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No forums can be created in this forum.', 'bbpress' ) ); |
| 190 |
} |
| 191 |
|
| 192 |
// Forum is closed and user cannot access |
| 193 |
if ( bbp_is_forum_closed( $forum_parent_id ) && !current_user_can( 'edit_forum', $forum_parent_id ) ) { |
| 194 |
bbp_add_error( 'bbp_new_forum_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new forums.', 'bbpress' ) ); |
| 195 |
} |
| 196 |
|
| 197 |
// Forum is private and user cannot access |
| 198 |
if ( bbp_is_forum_private( $forum_parent_id ) && !current_user_can( 'read_private_forums' ) ) { |
| 199 |
bbp_add_error( 'bbp_new_forum_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new forums in it.', 'bbpress' ) ); |
| 200 |
} |
| 201 |
|
| 202 |
// Forum is hidden and user cannot access |
| 203 |
if ( bbp_is_forum_hidden( $forum_parent_id ) && !current_user_can( 'read_hidden_forums' ) ) { |
| 204 |
bbp_add_error( 'bbp_new_forum_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new forums in it.', 'bbpress' ) ); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/** Forum Flooding ********************************************************/ |
| 209 |
|
| 210 |
if ( !bbp_check_for_flood( $anonymous_data, $forum_author ) ) |
| 211 |
bbp_add_error( 'bbp_forum_flood', __( '<strong>ERROR</strong>: Slow down; you move too fast.', 'bbpress' ) ); |
| 212 |
|
| 213 |
/** Forum Duplicate *******************************************************/ |
| 214 |
|
| 215 |
if ( !bbp_check_for_duplicate( array( 'post_type' => bbp_get_forum_post_type(), 'post_author' => $forum_author, 'post_content' => $forum_content, 'anonymous_data' => $anonymous_data ) ) ) |
| 216 |
bbp_add_error( 'bbp_forum_duplicate', __( '<strong>ERROR</strong>: This forum already exists.', 'bbpress' ) ); |
| 217 |
|
| 218 |
/** Forum Blacklist *******************************************************/ |
| 219 |
|
| 220 |
if ( !bbp_check_for_blacklist( $anonymous_data, $forum_author, $forum_title, $forum_content ) ) |
| 221 |
bbp_add_error( 'bbp_forum_blacklist', __( '<strong>ERROR</strong>: Your forum cannot be created at this time.', 'bbpress' ) ); |
| 222 |
|
| 223 |
/** Forum Moderation ******************************************************/ |
| 224 |
|
| 225 |
$post_status = bbp_get_public_status_id(); |
| 226 |
if ( !bbp_check_for_moderation( $anonymous_data, $forum_author, $forum_title, $forum_content ) ) |
| 227 |
$post_status = bbp_get_pending_status_id(); |
| 228 |
|
| 229 |
/** Additional Actions (Before Save) **************************************/ |
| 230 |
|
| 231 |
do_action( 'bbp_new_forum_pre_extras', $forum_parent_id ); |
| 232 |
|
| 233 |
// Bail if errors |
| 234 |
if ( bbp_has_errors() ) |
| 235 |
return; |
| 236 |
|
| 237 |
/** No Errors *************************************************************/ |
| 238 |
|
| 239 |
// Add the content of the form to $forum_data as an array |
| 240 |
// Just in time manipulation of forum data before being created |
| 241 |
$forum_data = apply_filters( 'bbp_new_forum_pre_insert', array( |
| 242 |
'post_author' => $forum_author, |
| 243 |
'post_title' => $forum_title, |
| 244 |
'post_content' => $forum_content, |
| 245 |
'post_parent' => $forum_parent_id, |
| 246 |
'post_status' => $post_status, |
| 247 |
'post_type' => bbp_get_forum_post_type(), |
| 248 |
'comment_status' => 'closed' |
| 249 |
) ); |
| 250 |
|
| 251 |
// Insert forum |
| 252 |
$forum_id = wp_insert_post( $forum_data ); |
| 253 |
|
| 254 |
/** No Errors *************************************************************/ |
| 255 |
|
| 256 |
if ( !empty( $forum_id ) && !is_wp_error( $forum_id ) ) { |
| 257 |
|
| 258 |
/** Trash Check *******************************************************/ |
| 259 |
|
| 260 |
// If the forum is trash, or the forum_status is switched to |
| 261 |
// trash, trash it properly |
| 262 |
if ( ( get_post_field( 'post_status', $forum_id ) == bbp_get_trash_status_id() ) || ( $forum_data['post_status'] == bbp_get_trash_status_id() ) ) { |
| 263 |
|
| 264 |
// Trash the reply |
| 265 |
wp_trash_post( $forum_id ); |
| 266 |
|
| 267 |
// Force view=all |
| 268 |
$view_all = true; |
| 269 |
} |
| 270 |
|
| 271 |
/** Spam Check ********************************************************/ |
| 272 |
|
| 273 |
// If reply or forum are spam, officially spam this reply |
| 274 |
if ( $forum_data['post_status'] == bbp_get_spam_status_id() ) { |
| 275 |
add_post_meta( $forum_id, '_bbp_spam_meta_status', bbp_get_public_status_id() ); |
| 276 |
|
| 277 |
// Force view=all |
| 278 |
$view_all = true; |
| 279 |
} |
| 280 |
|
| 281 |
/** Update counts, etc... *********************************************/ |
| 282 |
|
| 283 |
$forum_args = array( |
| 284 |
'forum_id' => $forum_id, |
| 285 |
'post_parent' => $forum_parent_id, |
| 286 |
'forum_author' => $forum_author, |
| 287 |
'last_topic_id' => 0, |
| 288 |
'last_reply_id' => 0, |
| 289 |
'last_active_id' => 0, |
| 290 |
'last_active_time' => 0, |
| 291 |
'last_active_status' => bbp_get_public_status_id() |
| 292 |
); |
| 293 |
do_action( 'bbp_new_forum', $forum_args ); |
| 294 |
|
| 295 |
/** Additional Actions (After Save) ***********************************/ |
| 296 |
|
| 297 |
do_action( 'bbp_new_forum_post_extras', $forum_id ); |
| 298 |
|
| 299 |
/** Redirect **********************************************************/ |
| 300 |
|
| 301 |
// Redirect to |
| 302 |
$redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : ''; |
| 303 |
|
| 304 |
// Get the forum URL |
| 305 |
$redirect_url = bbp_get_forum_permalink( $forum_id, $redirect_to ); |
| 306 |
|
| 307 |
// Add view all? |
| 308 |
if ( bbp_get_view_all() || !empty( $view_all ) ) { |
| 309 |
|
| 310 |
// User can moderate, so redirect to forum with view all set |
| 311 |
if ( current_user_can( 'moderate' ) ) { |
| 312 |
$redirect_url = bbp_add_view_all( $redirect_url ); |
| 313 |
|
| 314 |
// User cannot moderate, so redirect to forum |
| 315 |
} else { |
| 316 |
$redirect_url = bbp_get_forum_permalink( $forum_id ); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
// Allow to be filtered |
| 321 |
$redirect_url = apply_filters( 'bbp_new_forum_redirect_to', $redirect_url, $redirect_to ); |
| 322 |
|
| 323 |
/** Successful Save ***************************************************/ |
| 324 |
|
| 325 |
// Redirect back to new forum |
| 326 |
wp_safe_redirect( $redirect_url ); |
| 327 |
|
| 328 |
// For good measure |
| 329 |
exit(); |
| 330 |
|
| 331 |
// Errors |
| 332 |
} else { |
| 333 |
$append_error = ( is_wp_error( $forum_id ) && $forum_id->get_error_message() ) ? $forum_id->get_error_message() . ' ' : ''; |
| 334 |
bbp_add_error( 'bbp_forum_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your forum:' . $append_error, 'bbpress' ) ); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Handles the front end edit forum submission |
| 340 |
* |
| 341 |
* @uses bbPress:errors::add() To log various error messages |
| 342 |
* @uses bbp_get_forum() To get the forum |
| 343 |
* @uses bbp_verify_nonce_request() To verify the nonce and check the request |
| 344 |
* @uses bbp_is_forum_anonymous() To check if forum is by an anonymous user |
| 345 |
* @uses current_user_can() To check if the current user can edit the forum |
| 346 |
* @uses bbp_filter_anonymous_post_data() To filter anonymous data |
| 347 |
* @uses is_wp_error() To check if the value retrieved is a {@link WP_Error} |
| 348 |
* @uses esc_attr() For sanitization |
| 349 |
* @uses bbp_is_forum_category() To check if the forum is a category |
| 350 |
* @uses bbp_is_forum_closed() To check if the forum is closed |
| 351 |
* @uses bbp_is_forum_private() To check if the forum is private |
| 352 |
* @uses remove_filter() To remove 'wp_filter_kses' filters if needed |
| 353 |
* @uses apply_filters() Calls 'bbp_edit_forum_pre_title' with the title and |
| 354 |
* forum id |
| 355 |
* @uses apply_filters() Calls 'bbp_edit_forum_pre_content' with the content |
| 356 |
* and forum id |
| 357 |
* @uses bbPress::errors::get_error_codes() To get the {@link WP_Error} errors |
| 358 |
* @uses wp_save_post_revision() To save a forum revision |
| 359 |
* @uses bbp_update_forum_revision_log() To update the forum revision log |
| 360 |
* @uses wp_update_post() To update the forum |
| 361 |
* @uses do_action() Calls 'bbp_edit_forum' with the forum id, forum id, |
| 362 |
* anonymous data and reply author |
| 363 |
* @uses bbp_move_forum_handler() To handle movement of a forum from one forum |
| 364 |
* to another |
| 365 |
* @uses bbp_get_forum_permalink() To get the forum permalink |
| 366 |
* @uses wp_safe_redirect() To redirect to the forum link |
| 367 |
* @uses bbPress::errors::get_error_messages() To get the {@link WP_Error} error |
| 368 |
* messages |
| 369 |
*/ |
| 370 |
function bbp_edit_forum_handler() { |
| 371 |
|
| 372 |
// Bail if not a POST action |
| 373 |
if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) |
| 374 |
return; |
| 375 |
|
| 376 |
// Bail if action is not bbp-edit-forum |
| 377 |
if ( empty( $_POST['action'] ) || ( 'bbp-edit-forum' !== $_POST['action'] ) ) |
| 378 |
return; |
| 379 |
|
| 380 |
// Define local variable(s) |
| 381 |
$anonymous_data = array(); |
| 382 |
$forum = $forum_id = $forum_parent_id = 0; |
| 383 |
$forum_title = $forum_content = $forum_edit_reason = ''; |
| 384 |
|
| 385 |
/** Forum *****************************************************************/ |
| 386 |
|
| 387 |
// Forum id was not passed |
| 388 |
if ( empty( $_POST['bbp_forum_id'] ) ) { |
| 389 |
bbp_add_error( 'bbp_edit_forum_id', __( '<strong>ERROR</strong>: Forum ID not found.', 'bbpress' ) ); |
| 390 |
return; |
| 391 |
|
| 392 |
// Forum id was passed |
| 393 |
} elseif ( is_numeric( $_POST['bbp_forum_id'] ) ) { |
| 394 |
$forum_id = (int) $_POST['bbp_forum_id']; |
| 395 |
$forum = bbp_get_forum( $forum_id ); |
| 396 |
} |
| 397 |
|
| 398 |
// Nonce check |
| 399 |
if ( ! bbp_verify_nonce_request( 'bbp-edit-forum_' . $forum_id ) ) { |
| 400 |
bbp_add_error( 'bbp_edit_forum_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) ); |
| 401 |
return; |
| 402 |
|
| 403 |
// Forum does not exist |
| 404 |
} elseif ( empty( $forum ) ) { |
| 405 |
bbp_add_error( 'bbp_edit_forum_not_found', __( '<strong>ERROR</strong>: The forum you want to edit was not found.', 'bbpress' ) ); |
| 406 |
return; |
| 407 |
|
| 408 |
// User cannot edit this forum |
| 409 |
} elseif ( !current_user_can( 'edit_forum', $forum_id ) ) { |
| 410 |
bbp_add_error( 'bbp_edit_forum_permissions', __( '<strong>ERROR</strong>: You do not have permission to edit that forum.', 'bbpress' ) ); |
| 411 |
return; |
| 412 |
} |
| 413 |
|
| 414 |
// Remove wp_filter_kses filters from title and content for capable users and if the nonce is verified |
| 415 |
if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_forum'] ) && ( wp_create_nonce( 'bbp-unfiltered-html-forum_' . $forum_id ) == $_POST['_bbp_unfiltered_html_forum'] ) ) { |
| 416 |
remove_filter( 'bbp_edit_forum_pre_title', 'wp_filter_kses' ); |
| 417 |
remove_filter( 'bbp_edit_forum_pre_content', 'wp_filter_kses' ); |
| 418 |
} |
| 419 |
|
| 420 |
/** Forum Parent ***********************************************************/ |
| 421 |
|
| 422 |
// Forum parent id was passed |
| 423 |
if ( is_numeric( $_POST['bbp_forum_parent_id'] ) ) { |
| 424 |
$forum_parent_id = (int) $_POST['bbp_forum_parent_id']; |
| 425 |
} |
| 426 |
|
| 427 |
// Current forum this forum is in |
| 428 |
$current_parent_forum_id = bbp_get_forum_parent_id( $forum_id ); |
| 429 |
|
| 430 |
// Forum exists |
| 431 |
if ( !empty( $forum_parent_id ) && ( $forum_parent_id !== $current_parent_forum_id ) ) { |
| 432 |
|
| 433 |
// Forum is closed and user cannot access |
| 434 |
if ( bbp_is_forum_closed( $forum_parent_id ) && !current_user_can( 'edit_forum', $forum_parent_id ) ) { |
| 435 |
bbp_add_error( 'bbp_edit_forum_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new forums.', 'bbpress' ) ); |
| 436 |
} |
| 437 |
|
| 438 |
// Forum is private and user cannot access |
| 439 |
if ( bbp_is_forum_private( $forum_parent_id ) && !current_user_can( 'read_private_forums' ) ) { |
| 440 |
bbp_add_error( 'bbp_edit_forum_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new forums in it.', 'bbpress' ) ); |
| 441 |
} |
| 442 |
|
| 443 |
// Forum is hidden and user cannot access |
| 444 |
if ( bbp_is_forum_hidden( $forum_parent_id ) && !current_user_can( 'read_hidden_forums' ) ) { |
| 445 |
bbp_add_error( 'bbp_edit_forum_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new forums in it.', 'bbpress' ) ); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
/** Forum Title ***********************************************************/ |
| 450 |
|
| 451 |
if ( !empty( $_POST['bbp_forum_title'] ) ) |
| 452 |
$forum_title = esc_attr( strip_tags( $_POST['bbp_forum_title'] ) ); |
| 453 |
|
| 454 |
// Filter and sanitize |
| 455 |
$forum_title = apply_filters( 'bbp_edit_forum_pre_title', $forum_title, $forum_id ); |
| 456 |
|
| 457 |
// No forum title |
| 458 |
if ( empty( $forum_title ) ) |
| 459 |
bbp_add_error( 'bbp_edit_forum_title', __( '<strong>ERROR</strong>: Your forum needs a title.', 'bbpress' ) ); |
| 460 |
|
| 461 |
/** Forum Content *********************************************************/ |
| 462 |
|
| 463 |
if ( !empty( $_POST['bbp_forum_content'] ) ) |
| 464 |
$forum_content = $_POST['bbp_forum_content']; |
| 465 |
|
| 466 |
// Filter and sanitize |
| 467 |
$forum_content = apply_filters( 'bbp_edit_forum_pre_content', $forum_content, $forum_id ); |
| 468 |
|
| 469 |
// No forum content |
| 470 |
if ( empty( $forum_content ) ) |
| 471 |
bbp_add_error( 'bbp_edit_forum_content', __( '<strong>ERROR</strong>: Your forum description cannot be empty.', 'bbpress' ) ); |
| 472 |
|
| 473 |
/** Forum Blacklist *******************************************************/ |
| 474 |
|
| 475 |
if ( !bbp_check_for_blacklist( $anonymous_data, bbp_get_forum_author_id( $forum_id ), $forum_title, $forum_content ) ) |
| 476 |
bbp_add_error( 'bbp_forum_blacklist', __( '<strong>ERROR</strong>: Your forum cannot be edited at this time.', 'bbpress' ) ); |
| 477 |
|
| 478 |
/** Forum Moderation ******************************************************/ |
| 479 |
|
| 480 |
$post_status = bbp_get_public_status_id(); |
| 481 |
if ( !bbp_check_for_moderation( $anonymous_data, bbp_get_forum_author_id( $forum_id ), $forum_title, $forum_content ) ) |
| 482 |
$post_status = bbp_get_pending_status_id(); |
| 483 |
|
| 484 |
/** Additional Actions (Before Save) **************************************/ |
| 485 |
|
| 486 |
do_action( 'bbp_edit_forum_pre_extras', $forum_id ); |
| 487 |
|
| 488 |
// Bail if errors |
| 489 |
if ( bbp_has_errors() ) |
| 490 |
return; |
| 491 |
|
| 492 |
/** No Errors *************************************************************/ |
| 493 |
|
| 494 |
// Add the content of the form to $forum_data as an array |
| 495 |
// Just in time manipulation of forum data before being edited |
| 496 |
$forum_data = apply_filters( 'bbp_edit_forum_pre_insert', array( |
| 497 |
'ID' => $forum_id, |
| 498 |
'post_title' => $forum_title, |
| 499 |
'post_content' => $forum_content, |
| 500 |
'post_status' => $post_status, |
| 501 |
'post_parent' => $forum_parent_id |
| 502 |
) ); |
| 503 |
|
| 504 |
// Insert forum |
| 505 |
$forum_id = wp_update_post( $forum_data ); |
| 506 |
|
| 507 |
/** Revisions *************************************************************/ |
| 508 |
|
| 509 |
/** |
| 510 |
* @todo omitted for 2.1 |
| 511 |
// Revision Reason |
| 512 |
if ( !empty( $_POST['bbp_forum_edit_reason'] ) ) |
| 513 |
$forum_edit_reason = esc_attr( strip_tags( $_POST['bbp_forum_edit_reason'] ) ); |
| 514 |
|
| 515 |
// Update revision log |
| 516 |
if ( !empty( $_POST['bbp_log_forum_edit'] ) && ( 1 == $_POST['bbp_log_forum_edit'] ) && ( $revision_id = wp_save_post_revision( $forum_id ) ) ) { |
| 517 |
bbp_update_forum_revision_log( array( |
| 518 |
'forum_id' => $forum_id, |
| 519 |
'revision_id' => $revision_id, |
| 520 |
'author_id' => bbp_get_current_user_id(), |
| 521 |
'reason' => $forum_edit_reason |
| 522 |
) ); |
| 523 |
} |
| 524 |
*/ |
| 525 |
|
| 526 |
/** No Errors *************************************************************/ |
| 527 |
|
| 528 |
if ( !empty( $forum_id ) && !is_wp_error( $forum_id ) ) { |
| 529 |
|
| 530 |
// Update counts, etc... |
| 531 |
$forum_args = array( |
| 532 |
'forum_id' => $forum_id, |
| 533 |
'post_parent' => $forum_parent_id, |
| 534 |
'forum_author' => $forum->post_author, |
| 535 |
'last_topic_id' => 0, |
| 536 |
'last_reply_id' => 0, |
| 537 |
'last_active_id' => 0, |
| 538 |
'last_active_time' => 0, |
| 539 |
'last_active_status' => bbp_get_public_status_id() |
| 540 |
); |
| 541 |
do_action( 'bbp_edit_forum', $forum_args ); |
| 542 |
|
| 543 |
// If the new forum parent id is not equal to the old forum parent |
| 544 |
// id, run the bbp_move_forum action and pass the forum's parent id |
| 545 |
// as the first arg and new forum parent id as the second. |
| 546 |
// @todo implement |
| 547 |
//if ( $forum_id != $forum->post_parent ) |
| 548 |
// bbp_move_forum_handler( $forum_parent_id, $forum->post_parent, $forum_id ); |
| 549 |
|
| 550 |
/** Additional Actions (After Save) ***********************************/ |
| 551 |
|
| 552 |
do_action( 'bbp_edit_forum_post_extras', $forum_id ); |
| 553 |
|
| 554 |
/** Redirect **********************************************************/ |
| 555 |
|
| 556 |
// Redirect to |
| 557 |
$redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : ''; |
| 558 |
|
| 559 |
// View all? |
| 560 |
$view_all = bbp_get_view_all(); |
| 561 |
|
| 562 |
// Get the forum URL |
| 563 |
$forum_url = bbp_get_forum_permalink( $forum_id, $redirect_to ); |
| 564 |
|
| 565 |
// Add view all? |
| 566 |
if ( !empty( $view_all ) ) |
| 567 |
$forum_url = bbp_add_view_all( $forum_url ); |
| 568 |
|
| 569 |
// Allow to be filtered |
| 570 |
$forum_url = apply_filters( 'bbp_edit_forum_redirect_to', $forum_url, $view_all, $redirect_to ); |
| 571 |
|
| 572 |
/** Successful Edit ***************************************************/ |
| 573 |
|
| 574 |
// Redirect back to new forum |
| 575 |
wp_safe_redirect( $forum_url ); |
| 576 |
|
| 577 |
// For good measure |
| 578 |
exit(); |
| 579 |
|
| 580 |
/** Errors ****************************************************************/ |
| 581 |
|
| 582 |
} else { |
| 583 |
$append_error = ( is_wp_error( $forum_id ) && $forum_id->get_error_message() ) ? $forum_id->get_error_message() . ' ' : ''; |
| 584 |
bbp_add_error( 'bbp_forum_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your forum:' . $append_error . 'Please try again.', 'bbpress' ) ); |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
/** |
| 589 |
* Handle the saving of core forum metadata (Status, Visibility, and Type) |
| 590 |
* |
| 591 |
* @since bbPress (r3678) |
| 592 |
* @param int $forum_id |
| 593 |
* @uses bbp_is_forum_closed() To check if forum is closed |
| 594 |
* @uses bbp_close_forum() To close forum |
| 595 |
* @uses bbp_open_forum() To open forum |
| 596 |
* @uses bbp_is_forum_category() To check if forum is a category |
| 597 |
* @uses bbp_categorize_forum() To turn forum into a category |
| 598 |
* @uses bbp_normalize_forum() To turn category into forum |
| 599 |
* @uses bbp_get_public_status_id() To get the public status ID |
| 600 |
* @uses bbp_get_private_status_id() To get the private status ID |
| 601 |
* @uses bbp_get_hidden_status_id() To get the hidden status ID |
| 602 |
* @uses bbp_get_forum_visibility() To get the forums visibility |
| 603 |
* @uses bbp_hide_forum() To hide a forum |
| 604 |
* @uses bbp_privatize_forum() To make a forum private |
| 605 |
* @uses bbp_publicize_forum() To make a forum public |
| 606 |
* @return If forum ID is empty |
| 607 |
*/ |
| 608 |
function bbp_save_forum_extras( $forum_id = 0 ) { |
| 609 |
|
| 610 |
// Validate the forum ID |
| 611 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 612 |
|
| 613 |
// Bail if forum ID is empty |
| 614 |
if ( empty( $forum_id ) || ! bbp_is_forum( $forum_id ) ) |
| 615 |
return; |
| 616 |
|
| 617 |
/** Forum Status ******************************************************/ |
| 618 |
|
| 619 |
if ( !empty( $_POST['bbp_forum_status'] ) && in_array( $_POST['bbp_forum_status'], array( 'open', 'closed' ) ) ) { |
| 620 |
if ( 'closed' == $_POST['bbp_forum_status'] && !bbp_is_forum_closed( $forum_id, false ) ) { |
| 621 |
bbp_close_forum( $forum_id ); |
| 622 |
} elseif ( 'open' == $_POST['bbp_forum_status'] && bbp_is_forum_closed( $forum_id, false ) ) { |
| 623 |
bbp_open_forum( $forum_id ); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
/** Forum Type ********************************************************/ |
| 628 |
|
| 629 |
if ( !empty( $_POST['bbp_forum_type'] ) && in_array( $_POST['bbp_forum_type'], array( 'forum', 'category' ) ) ) { |
| 630 |
if ( 'category' == $_POST['bbp_forum_type'] && !bbp_is_forum_category( $forum_id ) ) { |
| 631 |
bbp_categorize_forum( $forum_id ); |
| 632 |
} elseif ( 'forum' == $_POST['bbp_forum_type'] && bbp_is_forum_category( $forum_id ) ) { |
| 633 |
bbp_normalize_forum( $forum_id ); |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
/** Forum Visibility **************************************************/ |
| 638 |
|
| 639 |
if ( !empty( $_POST['bbp_forum_visibility'] ) && in_array( $_POST['bbp_forum_visibility'], array( bbp_get_public_status_id(), bbp_get_private_status_id(), bbp_get_hidden_status_id() ) ) ) { |
| 640 |
|
| 641 |
// Get forums current visibility |
| 642 |
$visibility = bbp_get_forum_visibility( $forum_id ); |
| 643 |
|
| 644 |
// What is the new forum visibility setting? |
| 645 |
switch ( $_POST['bbp_forum_visibility'] ) { |
| 646 |
|
| 647 |
// Hidden |
| 648 |
case bbp_get_hidden_status_id() : |
| 649 |
bbp_hide_forum( $forum_id, $visibility ); |
| 650 |
break; |
| 651 |
|
| 652 |
// Private |
| 653 |
case bbp_get_private_status_id() : |
| 654 |
bbp_privatize_forum( $forum_id, $visibility ); |
| 655 |
break; |
| 656 |
|
| 657 |
// Publish (default) |
| 658 |
case bbp_get_public_status_id() : |
| 659 |
default : |
| 660 |
bbp_publicize_forum( $forum_id, $visibility ); |
| 661 |
break; |
| 662 |
} |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
/** Walk **********************************************************************/ |
| 667 |
|
| 668 |
/** |
| 669 |
* Walk the forum tree |
| 670 |
* |
| 671 |
* @param object $forums Forums |
| 672 |
* @param int $depth Depth |
| 673 |
* @param int $current Current forum |
| 674 |
* @param array $r Parsed arguments, supported by the walker. If you want to |
| 675 |
* use your own walker, pass the 'walker' arg with the walker. |
| 676 |
* The walker defaults to {@link BBP_Walker_Forum} |
| 677 |
* @return object Walked forum tree |
| 678 |
*/ |
| 679 |
function bbp_walk_forum( $forums, $depth, $current, $r ) { |
| 680 |
$walker = empty( $r['walker'] ) ? new BBP_Walker_Forum : $r['walker']; |
| 681 |
$args = array( $forums, $depth, $r, $current ); |
| 682 |
return call_user_func_array( array( &$walker, 'walk' ), $args ); |
| 683 |
} |
| 684 |
|
| 685 |
/** Forum Actions *************************************************************/ |
| 686 |
|
| 687 |
/** |
| 688 |
* Closes a forum |
| 689 |
* |
| 690 |
* @since bbPress (r2746) |
| 691 |
* |
| 692 |
* @param int $forum_id forum id |
| 693 |
* @uses do_action() Calls 'bbp_close_forum' with the forum id |
| 694 |
* @uses update_post_meta() To add the previous status to a meta |
| 695 |
* @uses do_action() Calls 'bbp_opened_forum' with the forum id |
| 696 |
* @return mixed False or {@link WP_Error} on failure, forum id on success |
| 697 |
*/ |
| 698 |
function bbp_close_forum( $forum_id = 0 ) { |
| 699 |
|
| 700 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 701 |
|
| 702 |
do_action( 'bbp_close_forum', $forum_id ); |
| 703 |
|
| 704 |
update_post_meta( $forum_id, '_bbp_status', 'closed' ); |
| 705 |
|
| 706 |
do_action( 'bbp_closed_forum', $forum_id ); |
| 707 |
|
| 708 |
return $forum_id; |
| 709 |
} |
| 710 |
|
| 711 |
/** |
| 712 |
* Opens a forum |
| 713 |
* |
| 714 |
* @since bbPress (r2746) |
| 715 |
* |
| 716 |
* @param int $forum_id forum id |
| 717 |
* @uses do_action() Calls 'bbp_open_forum' with the forum id |
| 718 |
* @uses get_post_meta() To get the previous status |
| 719 |
* @uses update_post_meta() To delete the previous status meta |
| 720 |
* @uses do_action() Calls 'bbp_opened_forum' with the forum id |
| 721 |
* @return mixed False or {@link WP_Error} on failure, forum id on success |
| 722 |
*/ |
| 723 |
function bbp_open_forum( $forum_id = 0 ) { |
| 724 |
|
| 725 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 726 |
|
| 727 |
do_action( 'bbp_open_forum', $forum_id ); |
| 728 |
|
| 729 |
update_post_meta( $forum_id, '_bbp_status', 'open' ); |
| 730 |
|
| 731 |
do_action( 'bbp_opened_forum', $forum_id ); |
| 732 |
|
| 733 |
return $forum_id; |
| 734 |
} |
| 735 |
|
| 736 |
/** |
| 737 |
* Make the forum a category |
| 738 |
* |
| 739 |
* @since bbPress (r2746) |
| 740 |
* |
| 741 |
* @param int $forum_id Optional. Forum id |
| 742 |
* @uses update_post_meta() To update the forum category meta |
| 743 |
* @return bool False on failure, true on success |
| 744 |
*/ |
| 745 |
function bbp_categorize_forum( $forum_id = 0 ) { |
| 746 |
|
| 747 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 748 |
|
| 749 |
do_action( 'bbp_categorize_forum', $forum_id ); |
| 750 |
|
| 751 |
update_post_meta( $forum_id, '_bbp_forum_type', 'category' ); |
| 752 |
|
| 753 |
do_action( 'bbp_categorized_forum', $forum_id ); |
| 754 |
|
| 755 |
return $forum_id; |
| 756 |
} |
| 757 |
|
| 758 |
/** |
| 759 |
* Remove the category status from a forum |
| 760 |
* |
| 761 |
* @since bbPress (r2746) |
| 762 |
* |
| 763 |
* @param int $forum_id Optional. Forum id |
| 764 |
* @uses delete_post_meta() To delete the forum category meta |
| 765 |
* @return bool False on failure, true on success |
| 766 |
*/ |
| 767 |
function bbp_normalize_forum( $forum_id = 0 ) { |
| 768 |
|
| 769 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 770 |
|
| 771 |
do_action( 'bbp_normalize_forum', $forum_id ); |
| 772 |
|
| 773 |
update_post_meta( $forum_id, '_bbp_forum_type', 'forum' ); |
| 774 |
|
| 775 |
do_action( 'bbp_normalized_forum', $forum_id ); |
| 776 |
|
| 777 |
return $forum_id; |
| 778 |
} |
| 779 |
|
| 780 |
/** |
| 781 |
* Mark the forum as public |
| 782 |
* |
| 783 |
* @since bbPress (r2746) |
| 784 |
* |
| 785 |
* @param int $forum_id Optional. Forum id |
| 786 |
* @uses update_post_meta() To update the forum private meta |
| 787 |
* @return bool False on failure, true on success |
| 788 |
*/ |
| 789 |
function bbp_publicize_forum( $forum_id = 0, $current_visibility = '' ) { |
| 790 |
|
| 791 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 792 |
|
| 793 |
do_action( 'bbp_publicize_forum', $forum_id ); |
| 794 |
|
| 795 |
// Get private forums |
| 796 |
$private = bbp_get_private_forum_ids(); |
| 797 |
|
| 798 |
// Find this forum in the array |
| 799 |
if ( in_array( $forum_id, $private ) ) { |
| 800 |
|
| 801 |
$offset = array_search( $forum_id, $private ); |
| 802 |
|
| 803 |
// Splice around it |
| 804 |
array_splice( $private, $offset, 1 ); |
| 805 |
|
| 806 |
// Update private forums minus this one |
| 807 |
update_option( '_bbp_private_forums', array_unique( array_filter( array_values( $private ) ) ) ); |
| 808 |
} |
| 809 |
|
| 810 |
// Get hidden forums |
| 811 |
$hidden = bbp_get_hidden_forum_ids(); |
| 812 |
|
| 813 |
// Find this forum in the array |
| 814 |
if ( in_array( $forum_id, $hidden ) ) { |
| 815 |
|
| 816 |
$offset = array_search( $forum_id, $hidden ); |
| 817 |
|
| 818 |
// Splice around it |
| 819 |
array_splice( $hidden, $offset, 1 ); |
| 820 |
|
| 821 |
// Update hidden forums minus this one |
| 822 |
update_option( '_bbp_hidden_forums', array_unique( array_filter( array_values( $hidden ) ) ) ); |
| 823 |
} |
| 824 |
|
| 825 |
// Only run queries if visibility is changing |
| 826 |
if ( bbp_get_public_status_id() != $current_visibility ) { |
| 827 |
|
| 828 |
// Update forum post_status |
| 829 |
global $wpdb; |
| 830 |
$wpdb->update( $wpdb->posts, array( 'post_status' => bbp_get_public_status_id() ), array( 'ID' => $forum_id ) ); |
| 831 |
wp_transition_post_status( bbp_get_public_status_id(), $current_visibility, get_post( $forum_id ) ); |
| 832 |
} |
| 833 |
|
| 834 |
do_action( 'bbp_publicized_forum', $forum_id ); |
| 835 |
|
| 836 |
return $forum_id; |
| 837 |
} |
| 838 |
|
| 839 |
/** |
| 840 |
* Mark the forum as private |
| 841 |
* |
| 842 |
* @since bbPress (r2746) |
| 843 |
* |
| 844 |
* @param int $forum_id Optional. Forum id |
| 845 |
* @uses update_post_meta() To update the forum private meta |
| 846 |
* @return bool False on failure, true on success |
| 847 |
*/ |
| 848 |
function bbp_privatize_forum( $forum_id = 0, $current_visibility = '' ) { |
| 849 |
|
| 850 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 851 |
|
| 852 |
do_action( 'bbp_privatize_forum', $forum_id ); |
| 853 |
|
| 854 |
// Only run queries if visibility is changing |
| 855 |
if ( bbp_get_private_status_id() != $current_visibility ) { |
| 856 |
|
| 857 |
// Get hidden forums |
| 858 |
$hidden = bbp_get_hidden_forum_ids(); |
| 859 |
|
| 860 |
// Find this forum in the array |
| 861 |
if ( in_array( $forum_id, $hidden ) ) { |
| 862 |
|
| 863 |
$offset = array_search( $forum_id, $hidden ); |
| 864 |
|
| 865 |
// Splice around it |
| 866 |
array_splice( $hidden, $offset, 1 ); |
| 867 |
|
| 868 |
// Update hidden forums minus this one |
| 869 |
update_option( '_bbp_hidden_forums', array_unique( array_filter( array_values( $hidden ) ) ) ); |
| 870 |
} |
| 871 |
|
| 872 |
// Add to '_bbp_private_forums' site option |
| 873 |
$private = bbp_get_private_forum_ids(); |
| 874 |
$private[] = $forum_id; |
| 875 |
update_option( '_bbp_private_forums', array_unique( array_filter( array_values( $private ) ) ) ); |
| 876 |
|
| 877 |
// Update forums visibility setting |
| 878 |
global $wpdb; |
| 879 |
$wpdb->update( $wpdb->posts, array( 'post_status' => bbp_get_private_status_id() ), array( 'ID' => $forum_id ) ); |
| 880 |
wp_transition_post_status( bbp_get_private_status_id(), $current_visibility, get_post( $forum_id ) ); |
| 881 |
} |
| 882 |
|
| 883 |
do_action( 'bbp_privatized_forum', $forum_id ); |
| 884 |
|
| 885 |
return $forum_id; |
| 886 |
} |
| 887 |
|
| 888 |
/** |
| 889 |
* Mark the forum as hidden |
| 890 |
* |
| 891 |
* @since bbPress (r2996) |
| 892 |
* |
| 893 |
* @param int $forum_id Optional. Forum id |
| 894 |
* @uses update_post_meta() To update the forum private meta |
| 895 |
* @return bool False on failure, true on success |
| 896 |
*/ |
| 897 |
function bbp_hide_forum( $forum_id = 0, $current_visibility = '' ) { |
| 898 |
|
| 899 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 900 |
|
| 901 |
do_action( 'bbp_hide_forum', $forum_id ); |
| 902 |
|
| 903 |
// Only run queries if visibility is changing |
| 904 |
if ( bbp_get_hidden_status_id() != $current_visibility ) { |
| 905 |
|
| 906 |
// Get private forums |
| 907 |
$private = bbp_get_private_forum_ids(); |
| 908 |
|
| 909 |
// Find this forum in the array |
| 910 |
if ( in_array( $forum_id, $private ) ) { |
| 911 |
|
| 912 |
$offset = array_search( $forum_id, $private ); |
| 913 |
|
| 914 |
// Splice around it |
| 915 |
array_splice( $private, $offset, 1 ); |
| 916 |
|
| 917 |
// Update private forums minus this one |
| 918 |
update_option( '_bbp_private_forums', array_unique( array_filter( array_values( $private ) ) ) ); |
| 919 |
} |
| 920 |
|
| 921 |
// Add to '_bbp_hidden_forums' site option |
| 922 |
$hidden = bbp_get_hidden_forum_ids(); |
| 923 |
$hidden[] = $forum_id; |
| 924 |
update_option( '_bbp_hidden_forums', array_unique( array_filter( array_values( $hidden ) ) ) ); |
| 925 |
|
| 926 |
// Update forums visibility setting |
| 927 |
global $wpdb; |
| 928 |
$wpdb->update( $wpdb->posts, array( 'post_status' => bbp_get_hidden_status_id() ), array( 'ID' => $forum_id ) ); |
| 929 |
wp_transition_post_status( bbp_get_hidden_status_id(), $current_visibility, get_post( $forum_id ) ); |
| 930 |
} |
| 931 |
|
| 932 |
do_action( 'bbp_hid_forum', $forum_id ); |
| 933 |
|
| 934 |
return $forum_id; |
| 935 |
} |
| 936 |
|
| 937 |
/** Count Bumpers *************************************************************/ |
| 938 |
|
| 939 |
/** |
| 940 |
* Bump the total topic count of a forum |
| 941 |
* |
| 942 |
* @since bbPress (r3825) |
| 943 |
* |
| 944 |
* @param int $forum_id Optional. Forum id. |
| 945 |
* @param int $difference Optional. Default 1 |
| 946 |
* @param bool $update_ancestors Optional. Default true |
| 947 |
* @uses bbp_get_forum_id() To get the forum id |
| 948 |
* @uses update_post_meta() To update the forum's topic count meta |
| 949 |
* @uses apply_filters() Calls 'bbp_bump_forum_topic_count' with the topic |
| 950 |
* count, forum id, and difference |
| 951 |
* @return int Forum topic count |
| 952 |
*/ |
| 953 |
function bbp_bump_forum_topic_count( $forum_id = 0, $difference = 1, $update_ancestors = true ) { |
| 954 |
|
| 955 |
// Get some counts |
| 956 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 957 |
$topic_count = bbp_get_forum_topic_count( $forum_id, false, false ); |
| 958 |
$total_topic_count = bbp_get_forum_topic_count( $forum_id, true, false ); |
| 959 |
|
| 960 |
// Update this forum id |
| 961 |
update_post_meta( $forum_id, '_bbp_topic_count', (int) $topic_count + (int) $difference ); |
| 962 |
update_post_meta( $forum_id, '_bbp_total_topic_count', (int) $total_topic_count + (int) $difference ); |
| 963 |
|
| 964 |
// Check for ancestors |
| 965 |
if ( true === $update_ancestors ) { |
| 966 |
|
| 967 |
// Get post ancestors |
| 968 |
$forum = get_post( $forum_id ); |
| 969 |
$ancestors = get_post_ancestors( $forum ); |
| 970 |
|
| 971 |
// If has ancestors, loop through them... |
| 972 |
if ( !empty( $ancestors ) ) { |
| 973 |
foreach ( (array) $ancestors as $parent_forum_id ) { |
| 974 |
|
| 975 |
// Get forum counts |
| 976 |
$parent_topic_count = bbp_get_forum_topic_count( $parent_forum_id, false, false ); |
| 977 |
$parent_total_topic_count = bbp_get_forum_topic_count( $parent_forum_id, true, false ); |
| 978 |
|
| 979 |
// Update counts |
| 980 |
update_post_meta( $parent_forum_id, '_bbp_topic_count', (int) $parent_topic_count + (int) $difference ); |
| 981 |
update_post_meta( $parent_forum_id, '_bbp_total_topic_count', (int) $parent_total_topic_count + (int) $difference ); |
| 982 |
} |
| 983 |
} |
| 984 |
} |
| 985 |
|
| 986 |
return (int) apply_filters( 'bbp_bump_forum_topic_count', (int) $total_topic_count + (int) $difference, $forum_id, (int) $difference, (bool) $update_ancestors ); |
| 987 |
} |
| 988 |
|
| 989 |
/** |
| 990 |
* Bump the total hidden topic count of a forum |
| 991 |
* |
| 992 |
* @since bbPress (r3825) |
| 993 |
* |
| 994 |
* @param int $forum_id Optional. Forum id. |
| 995 |
* @param int $difference Optional. Default 1 |
| 996 |
* @uses bbp_get_forum_id() To get the forum id |
| 997 |
* @uses update_post_meta() To update the forum's topic count meta |
| 998 |
* @uses apply_filters() Calls 'bbp_bump_forum_topic_count_hidden' with the |
| 999 |
* topic count, forum id, and difference |
| 1000 |
* @return int Forum hidden topic count |
| 1001 |
*/ |
| 1002 |
function bbp_bump_forum_topic_count_hidden( $forum_id = 0, $difference = 1 ) { |
| 1003 |
|
| 1004 |
// Get some counts |
| 1005 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 1006 |
$topic_count = bbp_get_forum_topic_count_hidden( $forum_id, false ); |
| 1007 |
$new_count = (int) $topic_count + (int) $difference; |
| 1008 |
|
| 1009 |
// Update this forum id |
| 1010 |
update_post_meta( $forum_id, '_bbp_topic_count_hidden', (int) $new_count ); |
| 1011 |
|
| 1012 |
return (int) apply_filters( 'bbp_bump_forum_topic_count_hidden', (int) $new_count, $forum_id, (int) $difference ); |
| 1013 |
} |
| 1014 |
|
| 1015 |
/** |
| 1016 |
* Bump the total topic count of a forum |
| 1017 |
* |
| 1018 |
* @since bbPress (r3825) |
| 1019 |
* |
| 1020 |
* @param int $forum_id Optional. Forum id. |
| 1021 |
* @param int $difference Optional. Default 1 |
| 1022 |
* @param bool $update_ancestors Optional. Default true |
| 1023 |
* @uses bbp_get_forum_id() To get the forum id |
| 1024 |
* @uses update_post_meta() To update the forum's topic count meta |
| 1025 |
* @uses apply_filters() Calls 'bbp_bump_forum_reply_count' with the topic |
| 1026 |
* count, forum id, and difference |
| 1027 |
* @return int Forum topic count |
| 1028 |
*/ |
| 1029 |
function bbp_bump_forum_reply_count( $forum_id = 0, $difference = 1, $update_ancestors = true ) { |
| 1030 |
|
| 1031 |
// Get some counts |
| 1032 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 1033 |
$topic_count = bbp_get_forum_reply_count( $forum_id, false ); |
| 1034 |
$total_reply_count = bbp_get_forum_reply_count( $forum_id, true ); |
| 1035 |
|
| 1036 |
// Update this forum id |
| 1037 |
update_post_meta( $forum_id, '_bbp_reply_count', (int) $topic_count + (int) $difference ); |
| 1038 |
update_post_meta( $forum_id, '_bbp_total_reply_count', (int) $total_reply_count + (int) $difference ); |
| 1039 |
|
| 1040 |
// Check for ancestors |
| 1041 |
if ( true === $update_ancestors ) { |
| 1042 |
|
| 1043 |
// Get post ancestors |
| 1044 |
$forum = get_post( $forum_id ); |
| 1045 |
$ancestors = get_post_ancestors( $forum ); |
| 1046 |
|
| 1047 |
// If has ancestors, loop through them... |
| 1048 |
if ( !empty( $ancestors ) ) { |
| 1049 |
foreach ( (array) $ancestors as $parent_forum_id ) { |
| 1050 |
|
| 1051 |
// Get forum counts |
| 1052 |
$parent_topic_count = bbp_get_forum_reply_count( $parent_forum_id, false ); |
| 1053 |
$parent_total_reply_count = bbp_get_forum_reply_count( $parent_forum_id, true ); |
| 1054 |
|
| 1055 |
// Update counts |
| 1056 |
update_post_meta( $parent_forum_id, '_bbp_reply_count', (int) $parent_topic_count + (int) $difference ); |
| 1057 |
update_post_meta( $parent_forum_id, '_bbp_total_reply_count', (int) $parent_total_reply_count + (int) $difference ); |
| 1058 |
} |
| 1059 |
} |
| 1060 |
} |
| 1061 |
|
| 1062 |
return (int) apply_filters( 'bbp_bump_forum_reply_count', (int) $total_reply_count + (int) $difference, $forum_id, (int) $difference, (bool) $update_ancestors ); |
| 1063 |
} |
| 1064 |
|
| 1065 |
/** Forum Updaters ************************************************************/ |
| 1066 |
|
| 1067 |
/** |
| 1068 |
* Update the forum last topic id |
| 1069 |
* |
| 1070 |
* @since bbPress (r2625) |
| 1071 |
* |
| 1072 |
* @param int $forum_id Optional. Forum id |
| 1073 |
* @param int $topic_id Optional. Topic id |
| 1074 |
* @uses bbp_get_forum_id() To get the forum id |
| 1075 |
* @uses bbp_forum_query_subforum_ids() To get the subforum ids |
| 1076 |
* @uses bbp_update_forum_last_topic_id() To update the last topic id of child |
| 1077 |
* forums |
| 1078 |
* @uses get_posts() To get the most recent topic in the forum |
| 1079 |
* @uses update_post_meta() To update the forum's last active id meta |
| 1080 |
* @uses apply_filters() Calls 'bbp_update_forum_last_topic_id' with the last |
| 1081 |
* reply id and forum id |
| 1082 |
* @return bool True on success, false on failure |
| 1083 |
*/ |
| 1084 |
function bbp_update_forum_last_topic_id( $forum_id = 0, $topic_id = 0 ) { |
| 1085 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 1086 |
|
| 1087 |
// Define local variable(s) |
| 1088 |
$children_last_topic = 0; |
| 1089 |
|
| 1090 |
// Do some calculation if not manually set |
| 1091 |
if ( empty( $topic_id ) ) { |
| 1092 |
|
| 1093 |
// Loop through children and add together forum reply counts |
| 1094 |
$children = bbp_forum_query_subforum_ids( $forum_id ); |
| 1095 |
if ( !empty( $children ) ) { |
| 1096 |
foreach ( (array) $children as $child ) { |
| 1097 |
$children_last_topic = bbp_update_forum_last_topic_id( $child ); // Recursive |
| 1098 |
} |
| 1099 |
} |
| 1100 |
|
| 1101 |
// Setup recent topic query vars |
| 1102 |
$post_vars = array( |
| 1103 |
'post_parent' => $forum_id, |
| 1104 |
'post_type' => bbp_get_topic_post_type(), |
| 1105 |
'meta_key' => '_bbp_last_active_time', |
| 1106 |
'orderby' => 'meta_value', |
| 1107 |
'numberposts' => 1 |
| 1108 |
); |
| 1109 |
|
| 1110 |
// Get the most recent topic in this forum_id |
| 1111 |
$recent_topic = get_posts( $post_vars ); |
| 1112 |
if ( !empty( $recent_topic ) ) { |
| 1113 |
$topic_id = $recent_topic[0]->ID; |
| 1114 |
} |
| 1115 |
} |
| 1116 |
|
| 1117 |
// Cast as integer in case of empty or string |
| 1118 |
$topic_id = (int) $topic_id; |
| 1119 |
$children_last_topic = (int) $children_last_topic; |
| 1120 |
|
| 1121 |
// If child forums have higher id, use that instead |
| 1122 |
if ( !empty( $children ) && ( $children_last_topic > $topic_id ) ) |
| 1123 |
$topic_id = $children_last_topic; |
| 1124 |
|
| 1125 |
// Update the last public topic ID |
| 1126 |
if ( bbp_is_topic_published( $topic_id ) ) |
| 1127 |
update_post_meta( $forum_id, '_bbp_last_topic_id', $topic_id ); |
| 1128 |
|
| 1129 |
return (int) apply_filters( 'bbp_update_forum_last_topic_id', $topic_id, $forum_id ); |
| 1130 |
} |
| 1131 |
|
| 1132 |
/** |
| 1133 |
* Update the forum last reply id |
| 1134 |
* |
| 1135 |
* @since bbPress (r2625) |
| 1136 |
* |
| 1137 |
* @param int $forum_id Optional. Forum id |
| 1138 |
* @param int $reply_id Optional. Reply id |
| 1139 |
* @uses bbp_get_forum_id() To get the forum id |
| 1140 |
* @uses bbp_forum_query_subforum_ids() To get the subforum ids |
| 1141 |
* @uses bbp_update_forum_last_reply_id() To update the last reply id of child |
| 1142 |
* forums |
| 1143 |
* @uses bbp_forum_query_topic_ids() To get the topic ids in the forum |
| 1144 |
* @uses bbp_forum_query_last_reply_id() To get the forum's last reply id |
| 1145 |
* @uses bbp_is_reply_published() To make sure the reply is published |
| 1146 |
* @uses update_post_meta() To update the forum's last active id meta |
| 1147 |
* @uses apply_filters() Calls 'bbp_update_forum_last_reply_id' with the last |
| 1148 |
* reply id and forum id |
| 1149 |
* @return bool True on success, false on failure |
| 1150 |
*/ |
| 1151 |
function bbp_update_forum_last_reply_id( $forum_id = 0, $reply_id = 0 ) { |
| 1152 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 1153 |
|
| 1154 |
// Define local variable(s) |
| 1155 |
$children_last_reply = 0; |
| 1156 |
|
| 1157 |
// Do some calculation if not manually set |
| 1158 |
if ( empty( $reply_id ) ) { |
| 1159 |
|
| 1160 |
// Loop through children and get the most recent reply id |
| 1161 |
$children = bbp_forum_query_subforum_ids( $forum_id ); |
| 1162 |
if ( !empty( $children ) ) { |
| 1163 |
foreach ( (array) $children as $child ) { |
| 1164 |
$children_last_reply = bbp_update_forum_last_reply_id( $child ); // Recursive |
| 1165 |
} |
| 1166 |
} |
| 1167 |
|
| 1168 |
// If this forum has topics... |
| 1169 |
$topic_ids = bbp_forum_query_topic_ids( $forum_id ); |
| 1170 |
if ( !empty( $topic_ids ) ) { |
| 1171 |
|
| 1172 |
// ...get the most recent reply from those topics... |
| 1173 |
$reply_id = bbp_forum_query_last_reply_id( $forum_id, $topic_ids ); |
| 1174 |
|
| 1175 |
// ...and compare it to the most recent topic id... |
| 1176 |
$reply_id = ( $reply_id > max( $topic_ids ) ) ? $reply_id : max( $topic_ids ); |
| 1177 |
} |
| 1178 |
} |
| 1179 |
|
| 1180 |
// Cast as integer in case of empty or string |
| 1181 |
$reply_id = (int) $reply_id; |
| 1182 |
$children_last_reply = (int) $children_last_reply; |
| 1183 |
|
| 1184 |
// If child forums have higher ID, check for newer reply id |
| 1185 |
if ( !empty( $children ) && ( $children_last_reply > $reply_id ) ) |
| 1186 |
$reply_id = $children_last_reply; |
| 1187 |
|
| 1188 |
// Update the last public reply ID |
| 1189 |
if ( bbp_is_reply_published( $reply_id ) ) |
| 1190 |
update_post_meta( $forum_id, '_bbp_last_reply_id', $reply_id ); |
| 1191 |
|
| 1192 |
return (int) apply_filters( 'bbp_update_forum_last_reply_id', $reply_id, $forum_id ); |
| 1193 |
} |
| 1194 |
|
| 1195 |
/** |
| 1196 |
* Update the forum last active post id |
| 1197 |
* |
| 1198 |
* @since bbPress (r2860) |
| 1199 |
* |
| 1200 |
* @param int $forum_id Optional. Forum id |
| 1201 |
* @param int $active_id Optional. Active post id |
| 1202 |
* @uses bbp_get_forum_id() To get the forum id |
| 1203 |
* @uses bbp_forum_query_subforum_ids() To get the subforum ids |
| 1204 |
* @uses bbp_update_forum_last_active_id() To update the last active id of |
| 1205 |
* child forums |
| 1206 |
* @uses bbp_forum_query_topic_ids() To get the topic ids in the forum |
| 1207 |
* @uses bbp_forum_query_last_reply_id() To get the forum's last reply id |
| 1208 |
* @uses get_post_status() To make sure the reply is published |
| 1209 |
* @uses update_post_meta() To update the forum's last active id meta |
| 1210 |
* @uses apply_filters() Calls 'bbp_update_forum_last_active_id' with the last |
| 1211 |
* active post id and forum id |
| 1212 |
* @return bool True on success, false on failure |
| 1213 |
*/ |
| 1214 |
function bbp_update_forum_last_active_id( $forum_id = 0, $active_id = 0 ) { |
| 1215 |
|
| 1216 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 1217 |
|
| 1218 |
// Define local variable(s) |
| 1219 |
$children_last_active = 0; |
| 1220 |
|
| 1221 |
// Do some calculation if not manually set |
| 1222 |
if ( empty( $active_id ) ) { |
| 1223 |
|
| 1224 |
// Loop through children and add together forum reply counts |
| 1225 |
$children = bbp_forum_query_subforum_ids( $forum_id ); |
| 1226 |
if ( !empty( $children ) ) { |
| 1227 |
foreach ( (array) $children as $child ) { |
| 1228 |
$children_last_active = bbp_update_forum_last_active_id( $child, $active_id ); |
| 1229 |
} |
| 1230 |
} |
| 1231 |
|
| 1232 |
// Don't count replies if the forum is a category |
| 1233 |
$topic_ids = bbp_forum_query_topic_ids( $forum_id ); |
| 1234 |
if ( !empty( $topic_ids ) ) { |
| 1235 |
$active_id = bbp_forum_query_last_reply_id( $forum_id, $topic_ids ); |
| 1236 |
$active_id = $active_id > max( $topic_ids ) ? $active_id : max( $topic_ids ); |
| 1237 |
|
| 1238 |
// Forum has no topics |
| 1239 |
} else { |
| 1240 |
$active_id = 0; |
| 1241 |
} |
| 1242 |
} |
| 1243 |
|
| 1244 |
// Cast as integer in case of empty or string |
| 1245 |
$active_id = (int) $active_id; |
| 1246 |
$children_last_active = (int) $children_last_active; |
| 1247 |
|
| 1248 |
// If child forums have higher id, use that instead |
| 1249 |
if ( !empty( $children ) && ( $children_last_active > $active_id ) ) |
| 1250 |
$active_id = $children_last_active; |
| 1251 |
|
| 1252 |
// Update only if published |
| 1253 |
if ( bbp_get_public_status_id() == get_post_status( $active_id ) ) |
| 1254 |
update_post_meta( $forum_id, '_bbp_last_active_id', (int) $active_id ); |
| 1255 |
|
| 1256 |
return (int) apply_filters( 'bbp_update_forum_last_active_id', (int) $active_id, $forum_id ); |
| 1257 |
} |
| 1258 |
|
| 1259 |
/** |
| 1260 |
* Update the forums last active date/time (aka freshness) |
| 1261 |
* |
| 1262 |
* @since bbPress (r2680) |
| 1263 |
* |
| 1264 |
* @param int $forum_id Optional. Topic id |
| 1265 |
* @param string $new_time Optional. New time in mysql format |
| 1266 |
* @uses bbp_get_forum_id() To get the forum id |
| 1267 |
* @uses bbp_get_forum_last_active_id() To get the forum's last post id |
| 1268 |
* @uses get_post_field() To get the post date of the forum's last post |
| 1269 |
* @uses update_post_meta() To update the forum last active time |
| 1270 |
* @uses apply_filters() Calls 'bbp_update_forum_last_active' with the new time |
| 1271 |
* and forum id |
| 1272 |
* @return bool True on success, false on failure |
| 1273 |
*/ |
| 1274 |
function bbp_update_forum_last_active_time( $forum_id = 0, $new_time = '' ) { |
| 1275 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 1276 |
|
| 1277 |
// Check time and use current if empty |
| 1278 |
if ( empty( $new_time ) ) |
| 1279 |
$new_time = get_post_field( 'post_date', bbp_get_forum_last_active_id( $forum_id ) ); |
| 1280 |
|
| 1281 |
// Update only if there is a time |
| 1282 |
if ( !empty( $new_time ) ) |
| 1283 |
update_post_meta( $forum_id, '_bbp_last_active_time', $new_time ); |
| 1284 |
|
| 1285 |
return (int) apply_filters( 'bbp_update_forum_last_active', $new_time, $forum_id ); |
| 1286 |
} |
| 1287 |
|
| 1288 |
/** |
| 1289 |
* Update the forum sub-forum count |
| 1290 |
* |
| 1291 |
* @since bbPress (r2625) |
| 1292 |
* |
| 1293 |
* @param int $forum_id Optional. Forum id |
| 1294 |
* @uses bbp_get_forum_id() To get the forum id |
| 1295 |
* @return bool True on success, false on failure |
| 1296 |
*/ |
| 1297 |
function bbp_update_forum_subforum_count( $forum_id = 0, $subforums = 0 ) { |
| 1298 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 1299 |
|
| 1300 |
if ( empty( $subforums ) ) |
| 1301 |
$subforums = count( bbp_forum_query_subforum_ids( $forum_id ) ); |
| 1302 |
|
| 1303 |
update_post_meta( $forum_id, '_bbp_forum_subforum_count', (int) $subforums ); |
| 1304 |
|
| 1305 |
return (int) apply_filters( 'bbp_update_forum_subforum_count', (int) $subforums, $forum_id ); |
| 1306 |
} |
| 1307 |
|
| 1308 |
/** |
| 1309 |
* Adjust the total topic count of a forum |
| 1310 |
* |
| 1311 |
* @since bbPress (r2464) |
| 1312 |
* |
| 1313 |
* @param int $forum_id Optional. Forum id or topic id. It is checked whether it |
| 1314 |
* is a topic or a forum. If it's a topic, its parent, |
| 1315 |
* i.e. the forum is automatically retrieved. |
| 1316 |
* @param bool $total_count Optional. To return the total count or normal |
| 1317 |
* count? |
| 1318 |
* @uses bbp_get_forum_id() To get the forum id |
| 1319 |
* @uses bbp_forum_query_subforum_ids() To get the subforum ids |
| 1320 |
* @uses bbp_update_forum_topic_count() To update the forum topic count |
| 1321 |
* @uses bbp_forum_query_topic_ids() To get the forum topic ids |
| 1322 |
* @uses update_post_meta() To update the forum's topic count meta |
| 1323 |
* @uses apply_filters() Calls 'bbp_update_forum_topic_count' with the topic |
| 1324 |
* count and forum id |
| 1325 |
* @return int Forum topic count |
| 1326 |
*/ |
| 1327 |
function bbp_update_forum_topic_count( $forum_id = 0 ) { |
| 1328 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 1329 |
$children_topic_count = 0; |
| 1330 |
|
| 1331 |
// Loop through subforums and add together forum topic counts |
| 1332 |
$children = bbp_forum_query_subforum_ids( $forum_id ); |
| 1333 |
if ( !empty( $children ) ) { |
| 1334 |
foreach ( (array) $children as $child ) { |
| 1335 |
$children_topic_count += bbp_update_forum_topic_count( $child ); // Recursive |
| 1336 |
} |
| 1337 |
} |
| 1338 |
|
| 1339 |
// Get total topics for this forum |
| 1340 |
$topics = (int) count( bbp_forum_query_topic_ids( $forum_id ) ); |
| 1341 |
|
| 1342 |
// Calculate total topics in this forum |
| 1343 |
$total_topics = $topics + $children_topic_count; |
| 1344 |
|
| 1345 |
// Update the count |
| 1346 |
update_post_meta( $forum_id, '_bbp_topic_count', (int) $topics ); |
| 1347 |
update_post_meta( $forum_id, '_bbp_total_topic_count', (int) $total_topics ); |
| 1348 |
|
| 1349 |
return (int) apply_filters( 'bbp_update_forum_topic_count', (int) $total_topics, $forum_id ); |
| 1350 |
} |
| 1351 |
|
| 1352 |
/** |
| 1353 |
* Adjust the total hidden topic count of a forum (hidden includes trashed and spammed topics) |
| 1354 |
* |
| 1355 |
* @since bbPress (r2888) |
| 1356 |
* |
| 1357 |
* @param int $forum_id Optional. Topic id to update |
| 1358 |
* @param int $topic_count Optional. Set the topic count manually |
| 1359 |
* @uses bbp_is_topic() To check if the supplied id is a topic |
| 1360 |
* @uses bbp_get_topic_id() To get the topic id |
| 1361 |
* @uses bbp_get_topic_forum_id() To get the topic forum id |
| 1362 |
* @uses bbp_get_forum_id() To get the forum id |
| 1363 |
* @uses wpdb::prepare() To prepare our sql query |
| 1364 |
* @uses wpdb::get_col() To execute our query and get the column back |
| 1365 |
* @uses update_post_meta() To update the forum hidden topic count meta |
| 1366 |
* @uses apply_filters() Calls 'bbp_update_forum_topic_count_hidden' with the |
| 1367 |
* hidden topic count and forum id |
| 1368 |
* @return int Topic hidden topic count |
| 1369 |
*/ |
| 1370 |
function bbp_update_forum_topic_count_hidden( $forum_id = 0, $topic_count = 0 ) { |
| 1371 |
global $wpdb; |
| 1372 |
|
| 1373 |
// If topic_id was passed as $forum_id, then get its forum |
| 1374 |
if ( bbp_is_topic( $forum_id ) ) { |
| 1375 |
$topic_id = bbp_get_topic_id( $forum_id ); |
| 1376 |
$forum_id = bbp_get_topic_forum_id( $topic_id ); |
| 1377 |
|
| 1378 |
// $forum_id is not a topic_id, so validate and proceed |
| 1379 |
} else { |
| 1380 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 1381 |
} |
| 1382 |
|
| 1383 |
// Can't update what isn't there |
| 1384 |
if ( !empty( $forum_id ) ) { |
| 1385 |
|
| 1386 |
// Get topics of forum |
| 1387 |
if ( empty( $topic_count ) ) |
| 1388 |
$topic_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( '" . join( '\',\'', array( bbp_get_trash_status_id(), bbp_get_spam_status_id() ) ) . "') AND post_type = '%s';", $forum_id, bbp_get_topic_post_type() ) ); |
| 1389 |
|
| 1390 |
// Update the count |
| 1391 |
update_post_meta( $forum_id, '_bbp_topic_count_hidden', (int) $topic_count ); |
| 1392 |
} |
| 1393 |
|
| 1394 |
return (int) apply_filters( 'bbp_update_forum_topic_count_hidden', (int) $topic_count, $forum_id ); |
| 1395 |
} |
| 1396 |
|
| 1397 |
/** |
| 1398 |
* Adjust the total reply count of a forum |
| 1399 |
* |
| 1400 |
* @since bbPress (r2464) |
| 1401 |
* |
| 1402 |
* @param int $forum_id Optional. Forum id or topic id. It is checked whether it |
| 1403 |
* is a topic or a forum. If it's a topic, its parent, |
| 1404 |
* i.e. the forum is automatically retrieved. |
| 1405 |
* @param bool $total_count Optional. To return the total count or normal |
| 1406 |
* count? |
| 1407 |
* @uses bbp_get_forum_id() To get the forum id |
| 1408 |
* @uses bbp_forum_query_subforum_ids() To get the subforum ids |
| 1409 |
* @uses bbp_update_forum_reply_count() To update the forum reply count |
| 1410 |
* @uses bbp_forum_query_topic_ids() To get the forum topic ids |
| 1411 |
* @uses wpdb::prepare() To prepare the sql statement |
| 1412 |
* @uses wpdb::get_var() To execute the query and get the var back |
| 1413 |
* @uses update_post_meta() To update the forum's reply count meta |
| 1414 |
* @uses apply_filters() Calls 'bbp_update_forum_topic_count' with the reply |
| 1415 |
* count and forum id |
| 1416 |
* @return int Forum reply count |
| 1417 |
*/ |
| 1418 |
function bbp_update_forum_reply_count( $forum_id = 0 ) { |
| 1419 |
global $wpdb; |
| 1420 |
|
| 1421 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 1422 |
$children_reply_count = 0; |
| 1423 |
|
| 1424 |
// Loop through children and add together forum reply counts |
| 1425 |
$children = bbp_forum_query_subforum_ids( $forum_id ); |
| 1426 |
if ( !empty( $children ) ) { |
| 1427 |
foreach ( (array) $children as $child ) { |
| 1428 |
$children_reply_count += bbp_update_forum_reply_count( $child ); |
| 1429 |
} |
| 1430 |
} |
| 1431 |
|
| 1432 |
// Don't count replies if the forum is a category |
| 1433 |
$topic_ids = bbp_forum_query_topic_ids( $forum_id ); |
| 1434 |
if ( !empty( $topic_ids ) ) |
| 1435 |
$reply_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent IN ( " . join( ',', $topic_ids ) . " ) AND post_status = '%s' AND post_type = '%s';", bbp_get_public_status_id(), bbp_get_reply_post_type() ) ); |
| 1436 |
else |
| 1437 |
$reply_count = 0; |
| 1438 |
|
| 1439 |
// Calculate total replies in this forum |
| 1440 |
$total_replies = (int) $reply_count + $children_reply_count; |
| 1441 |
|
| 1442 |
// Update the count |
| 1443 |
update_post_meta( $forum_id, '_bbp_reply_count', (int) $reply_count ); |
| 1444 |
update_post_meta( $forum_id, '_bbp_total_reply_count', (int) $total_replies ); |
| 1445 |
|
| 1446 |
return (int) apply_filters( 'bbp_update_forum_reply_count', (int) $total_replies, $forum_id ); |
| 1447 |
} |
| 1448 |
|
| 1449 |
/** |
| 1450 |
* Updates the counts of a forum. |
| 1451 |
* |
| 1452 |
* This calls a few internal functions that all run manual queries against the |
| 1453 |
* database to get their results. As such, this function can be costly to run |
| 1454 |
* but is necessary to keep everything accurate. |
| 1455 |
* |
| 1456 |
* @since bbPress (r2908) |
| 1457 |
* |
| 1458 |
* @param mixed $args Supports these arguments: |
| 1459 |
* - forum_id: Forum id |
| 1460 |
* - last_topic_id: Last topic id |
| 1461 |
* - last_reply_id: Last reply id |
| 1462 |
* - last_active_id: Last active post id |
| 1463 |
* - last_active_time: last active time |
| 1464 |
* @uses bbp_update_forum_last_topic_id() To update the forum last topic id |
| 1465 |
* @uses bbp_update_forum_last_reply_id() To update the forum last reply id |
| 1466 |
* @uses bbp_update_forum_last_active_id() To update the last active post id |
| 1467 |
* @uses get_post_field() To get the post date of the last active id |
| 1468 |
* @uses bbp_update_forum_last_active_time() To update the last active time |
| 1469 |
* @uses bbp_update_forum_subforum_count() To update the subforum count |
| 1470 |
* @uses bbp_update_forum_topic_count() To update the forum topic count |
| 1471 |
* @uses bbp_update_forum_reply_count() To update the forum reply count |
| 1472 |
* @uses bbp_update_forum_topic_count_hidden() To update the hidden topic count |
| 1473 |
*/ |
| 1474 |
function bbp_update_forum( $args = '' ) { |
| 1475 |
$defaults = array( |
| 1476 |
'forum_id' => 0, |
| 1477 |
'post_parent' => 0, |
| 1478 |
'last_topic_id' => 0, |
| 1479 |
'last_reply_id' => 0, |
| 1480 |
'last_active_id' => 0, |
| 1481 |
'last_active_time' => 0, |
| 1482 |
'last_active_status' => bbp_get_public_status_id() |
| 1483 |
); |
| 1484 |
$r = bbp_parse_args( $args, $defaults, 'update_forum' ); |
| 1485 |
extract( $r ); |
| 1486 |
|
| 1487 |
// Last topic and reply ID's |
| 1488 |
bbp_update_forum_last_topic_id( $forum_id, $last_topic_id ); |
| 1489 |
bbp_update_forum_last_reply_id( $forum_id, $last_reply_id ); |
| 1490 |
|
| 1491 |
// Active dance |
| 1492 |
$last_active_id = bbp_update_forum_last_active_id( $forum_id, $last_active_id ); |
| 1493 |
|
| 1494 |
// If no active time was passed, get it from the last_active_id |
| 1495 |
if ( empty( $last_active_time ) ) |
| 1496 |
$last_active_time = get_post_field( 'post_date', $last_active_id ); |
| 1497 |
|
| 1498 |
if ( bbp_get_public_status_id() == $last_active_status ) { |
| 1499 |
bbp_update_forum_last_active_time( $forum_id, $last_active_time ); |
| 1500 |
} |
| 1501 |
|
| 1502 |
// Counts |
| 1503 |
bbp_update_forum_subforum_count ( $forum_id ); |
| 1504 |
bbp_update_forum_reply_count ( $forum_id ); |
| 1505 |
bbp_update_forum_topic_count ( $forum_id ); |
| 1506 |
bbp_update_forum_topic_count_hidden( $forum_id ); |
| 1507 |
|
| 1508 |
// Update the parent forum if one was passed |
| 1509 |
if ( !empty( $post_parent ) && is_numeric( $post_parent ) ) { |
| 1510 |
bbp_update_forum( array( |
| 1511 |
'forum_id' => $post_parent, |
| 1512 |
'post_parent' => get_post_field( 'post_parent', $post_parent ) |
| 1513 |
) ); |
| 1514 |
} |
| 1515 |
} |
| 1516 |
|
| 1517 |
/** Queries *******************************************************************/ |
| 1518 |
|
| 1519 |
/** |
| 1520 |
* Returns the hidden forum ids |
| 1521 |
* |
| 1522 |
* Only hidden forum ids are returned. Public and private ids are not. |
| 1523 |
* |
| 1524 |
* @since bbPress (r3007) |
| 1525 |
* |
| 1526 |
* @uses get_option() Returns the unserialized array of hidden forum ids |
| 1527 |
* @uses apply_filters() Calls 'bbp_forum_query_topic_ids' with the topic ids |
| 1528 |
* and forum id |
| 1529 |
*/ |
| 1530 |
function bbp_get_hidden_forum_ids() { |
| 1531 |
$forum_ids = get_option( '_bbp_hidden_forums', array() ); |
| 1532 |
|
| 1533 |
return apply_filters( 'bbp_get_hidden_forum_ids', (array) $forum_ids ); |
| 1534 |
} |
| 1535 |
|
| 1536 |
/** |
| 1537 |
* Returns the private forum ids |
| 1538 |
* |
| 1539 |
* Only private forum ids are returned. Public and hidden ids are not. |
| 1540 |
* |
| 1541 |
* @since bbPress (r3007) |
| 1542 |
* |
| 1543 |
* @uses get_option() Returns the unserialized array of private forum ids |
| 1544 |
* @uses apply_filters() Calls 'bbp_forum_query_topic_ids' with the topic ids |
| 1545 |
* and forum id |
| 1546 |
*/ |
| 1547 |
function bbp_get_private_forum_ids() { |
| 1548 |
$forum_ids = get_option( '_bbp_private_forums', array() ); |
| 1549 |
|
| 1550 |
return apply_filters( 'bbp_get_private_forum_ids', (array) $forum_ids ); |
| 1551 |
} |
| 1552 |
|
| 1553 |
/** |
| 1554 |
* Returns a meta_query that either includes or excludes hidden forum IDs |
| 1555 |
* from a query. |
| 1556 |
* |
| 1557 |
* @since bbPress (r3291) |
| 1558 |
* |
| 1559 |
* @param string Optional. The type of value to return. (string|array|meta_query) |
| 1560 |
* |
| 1561 |
* @uses is_super_admin() |
| 1562 |
* @uses bbp_get_hidden_forum_ids() |
| 1563 |
* @uses bbp_get_private_forum_ids() |
| 1564 |
* @uses apply_filters() |
| 1565 |
*/ |
| 1566 |
function bbp_exclude_forum_ids( $type = 'string' ) { |
| 1567 |
|
| 1568 |
// Setup arrays |
| 1569 |
$private = $hidden = $meta_query = $forum_ids = array(); |
| 1570 |
|
| 1571 |
// Default return value |
| 1572 |
switch ( $type ) { |
| 1573 |
case 'string' : |
| 1574 |
$retval = ''; |
| 1575 |
break; |
| 1576 |
|
| 1577 |
case 'array' : |
| 1578 |
$retval = array(); |
| 1579 |
break; |
| 1580 |
|
| 1581 |
case 'meta_query' : |
| 1582 |
$retval = array( array() ) ; |
| 1583 |
break; |
| 1584 |
} |
| 1585 |
|
| 1586 |
// Exclude for everyone but super admins |
| 1587 |
if ( !is_super_admin() ) { |
| 1588 |
|
| 1589 |
// Private forums |
| 1590 |
if ( !current_user_can( 'read_private_forums' ) ) |
| 1591 |
$private = bbp_get_private_forum_ids(); |
| 1592 |
|
| 1593 |
// Hidden forums |
| 1594 |
if ( !current_user_can( 'read_hidden_forums' ) ) |
| 1595 |
$hidden = bbp_get_hidden_forum_ids(); |
| 1596 |
|
| 1597 |
// Merge private and hidden forums together |
| 1598 |
$forum_ids = (array) array_filter( array_merge( $private, $hidden ) ); |
| 1599 |
|
| 1600 |
// There are forums that need to be excluded |
| 1601 |
if ( !empty( $forum_ids ) ) { |
| 1602 |
|
| 1603 |
switch ( $type ) { |
| 1604 |
|
| 1605 |
// Separate forum ID's into a comma separated string |
| 1606 |
case 'string' : |
| 1607 |
$retval = implode( ',', $forum_ids ); |
| 1608 |
break; |
| 1609 |
|
| 1610 |
// Use forum_ids array |
| 1611 |
case 'array' : |
| 1612 |
$retval = $forum_ids; |
| 1613 |
break; |
| 1614 |
|
| 1615 |
// Build a meta_query |
| 1616 |
case 'meta_query' : |
| 1617 |
$retval = array( |
| 1618 |
'key' => '_bbp_forum_id', |
| 1619 |
'value' => implode( ',', $forum_ids ), |
| 1620 |
'type' => 'numeric', |
| 1621 |
'compare' => ( 1 < count( $forum_ids ) ) ? 'NOT IN' : '!=' |
| 1622 |
); |
| 1623 |
break; |
| 1624 |
} |
| 1625 |
} |
| 1626 |
} |
| 1627 |
|
| 1628 |
// Filter and return the results |
| 1629 |
return apply_filters( 'bbp_exclude_forum_ids', $retval, $forum_ids, $type ); |
| 1630 |
} |
| 1631 |
|
| 1632 |
/** |
| 1633 |
* Adjusts topic and reply queries to exclude items that might be contained |
| 1634 |
* inside hidden or private forums that the user does not have the capability |
| 1635 |
* to view. |
| 1636 |
* |
| 1637 |
* @since bbPress (r3291) |
| 1638 |
* |
| 1639 |
* @param WP_Query $posts_query |
| 1640 |
* |
| 1641 |
* @uses apply_filters() |
| 1642 |
* @uses bbp_exclude_forum_ids() |
| 1643 |
* @uses bbp_get_topic_post_type() |
| 1644 |
* @uses bbp_get_reply_post_type() |
| 1645 |
* @return WP_Query |
| 1646 |
*/ |
| 1647 |
function bbp_pre_get_posts_exclude_forums( $posts_query ) { |
| 1648 |
|
| 1649 |
// Bail if all forums are explicitly allowed |
| 1650 |
if ( true === apply_filters( 'bbp_include_all_forums', $posts_query ) ) |
| 1651 |
return; |
| 1652 |
|
| 1653 |
// Bail if $posts_query is not an object or of incorrect class |
| 1654 |
if ( !is_object( $posts_query ) || !is_a( $posts_query, 'WP_Query' ) ) |
| 1655 |
return; |
| 1656 |
|
| 1657 |
// Bail if filters are suppressed on this query |
| 1658 |
if ( true == $posts_query->get( 'suppress_filters' ) ) |
| 1659 |
return; |
| 1660 |
|
| 1661 |
// Only exclude forums on bbPress queries |
| 1662 |
switch ( $posts_query->get( 'post_type' ) ) { |
| 1663 |
|
| 1664 |
// Forums |
| 1665 |
case bbp_get_forum_post_type() : |
| 1666 |
|
| 1667 |
// Prevent accidental wp-admin post_row override |
| 1668 |
if ( is_admin() && isset( $_REQUEST['post_status'] ) ) |
| 1669 |
break; |
| 1670 |
|
| 1671 |
// Define local variable |
| 1672 |
$status = array(); |
| 1673 |
|
| 1674 |
// All users can see published forums |
| 1675 |
$status[] = bbp_get_public_status_id(); |
| 1676 |
|
| 1677 |
// Add bbp_get_private_status_id() if user is capable |
| 1678 |
if ( current_user_can( 'read_private_forums' ) ) { |
| 1679 |
$status[] = bbp_get_private_status_id(); |
| 1680 |
} |
| 1681 |
|
| 1682 |
// Add bbp_get_hidden_status_id() if user is capable |
| 1683 |
if ( current_user_can( 'read_hidden_forums' ) ) { |
| 1684 |
$status[] = bbp_get_hidden_status_id(); |
| 1685 |
} |
| 1686 |
|
| 1687 |
// Implode and add the statuses |
| 1688 |
$posts_query->set( 'post_status', implode( ',', $status ) ); |
| 1689 |
|
| 1690 |
break; |
| 1691 |
|
| 1692 |
// Topics |
| 1693 |
case bbp_get_topic_post_type() : |
| 1694 |
|
| 1695 |
// Replies |
| 1696 |
case bbp_get_reply_post_type() : |
| 1697 |
|
| 1698 |
// Get forums to exclude |
| 1699 |
$forum_ids = bbp_exclude_forum_ids( 'meta_query' ); |
| 1700 |
|
| 1701 |
// Bail if no forums to exclude |
| 1702 |
if ( empty( $forum_ids ) ) |
| 1703 |
return; |
| 1704 |
|
| 1705 |
// Get any existing meta queries |
| 1706 |
$meta_query = $posts_query->get( 'meta_query' ); |
| 1707 |
|
| 1708 |
// Add our meta query to existing |
| 1709 |
$meta_query[] = $forum_ids; |
| 1710 |
|
| 1711 |
// Set the meta_query var |
| 1712 |
$posts_query->set( 'meta_query', $meta_query ); |
| 1713 |
|
| 1714 |
break; |
| 1715 |
} |
| 1716 |
} |
| 1717 |
|
| 1718 |
/** |
| 1719 |
* Returns the forum's topic ids |
| 1720 |
* |
| 1721 |
* Only topics with published and closed statuses are returned |
| 1722 |
* |
| 1723 |
* @since bbPress (r2908) |
| 1724 |
* |
| 1725 |
* @param int $forum_id Forum id |
| 1726 |
* @uses bbp_get_topic_post_type() To get the topic post type |
| 1727 |
* @uses bbp_get_public_child_ids() To get the topic ids |
| 1728 |
* @uses apply_filters() Calls 'bbp_forum_query_topic_ids' with the topic ids |
| 1729 |
* and forum id |
| 1730 |
*/ |
| 1731 |
function bbp_forum_query_topic_ids( $forum_id ) { |
| 1732 |
$topic_ids = bbp_get_public_child_ids( $forum_id, bbp_get_topic_post_type() ); |
| 1733 |
|
| 1734 |
return apply_filters( 'bbp_forum_query_topic_ids', $topic_ids, $forum_id ); |
| 1735 |
} |
| 1736 |
|
| 1737 |
/** |
| 1738 |
* Returns the forum's subforum ids |
| 1739 |
* |
| 1740 |
* Only forums with published status are returned |
| 1741 |
* |
| 1742 |
* @since bbPress (r2908) |
| 1743 |
* |
| 1744 |
* @param int $forum_id Forum id |
| 1745 |
* @uses bbp_get_forum_post_type() To get the forum post type |
| 1746 |
* @uses bbp_get_public_child_ids() To get the forum ids |
| 1747 |
* @uses apply_filters() Calls 'bbp_forum_query_subforum_ids' with the subforum |
| 1748 |
* ids and forum id |
| 1749 |
*/ |
| 1750 |
function bbp_forum_query_subforum_ids( $forum_id ) { |
| 1751 |
$subforum_ids = bbp_get_public_child_ids( $forum_id, bbp_get_forum_post_type() ); |
| 1752 |
//usort( $subforum_ids, '_bbp_forum_query_usort_subforum_ids' ); |
| 1753 |
|
| 1754 |
return apply_filters( 'bbp_get_forum_subforum_ids', $subforum_ids, $forum_id ); |
| 1755 |
} |
| 1756 |
|
| 1757 |
/** |
| 1758 |
* Callback to sort forum ID's based on last active time |
| 1759 |
* |
| 1760 |
* @since bbPress (r3789) |
| 1761 |
* @param int $a First forum ID to compare |
| 1762 |
* @param int $b Second forum ID to compare |
| 1763 |
* @return Position change based on sort |
| 1764 |
*/ |
| 1765 |
function _bbp_forum_query_usort_subforum_ids( $a = 0, $b = 0 ) { |
| 1766 |
$ta = get_post_meta( $a, '_bbp_last_active_time', true ); |
| 1767 |
$tb = get_post_meta( $b, '_bbp_last_active_time', true ); |
| 1768 |
return ( $ta < $tb ) ? -1 : 1; |
| 1769 |
} |
| 1770 |
|
| 1771 |
/** |
| 1772 |
* Returns the forum's last reply id |
| 1773 |
* |
| 1774 |
* @since bbPress (r2908) |
| 1775 |
* |
| 1776 |
* @param int $forum_id Forum id |
| 1777 |
* @param int $topic_ids Optional. Topic ids |
| 1778 |
* @uses wp_cache_get() To check for cache and retrieve it |
| 1779 |
* @uses bbp_forum_query_topic_ids() To get the forum's topic ids |
| 1780 |
* @uses wpdb::prepare() To prepare the query |
| 1781 |
* @uses wpdb::get_var() To execute the query and get the var back |
| 1782 |
* @uses bbp_get_reply_post_type() To get the reply post type |
| 1783 |
* @uses wp_cache_set() To set the cache for future use |
| 1784 |
* @uses apply_filters() Calls 'bbp_forum_query_last_reply_id' with the reply id |
| 1785 |
* and forum id |
| 1786 |
*/ |
| 1787 |
function bbp_forum_query_last_reply_id( $forum_id, $topic_ids = 0 ) { |
| 1788 |
global $wpdb; |
| 1789 |
|
| 1790 |
$cache_id = 'bbp_get_forum_' . $forum_id . '_reply_id'; |
| 1791 |
$reply_id = (int) wp_cache_get( $cache_id, 'bbpress' ); |
| 1792 |
|
| 1793 |
if ( empty( $reply_id ) ) { |
| 1794 |
|
| 1795 |
if ( empty( $topic_ids ) ) { |
| 1796 |
$topic_ids = bbp_forum_query_topic_ids( $forum_id ); |
| 1797 |
} |
| 1798 |
|
| 1799 |
if ( !empty( $topic_ids ) ) { |
| 1800 |
$reply_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent IN ( " . join( ',', $topic_ids ) . " ) AND post_status = '%s' AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", bbp_get_public_status_id(), bbp_get_reply_post_type() ) ); |
| 1801 |
wp_cache_set( $cache_id, $reply_id, 'bbpress' ); // May be (int) 0 |
| 1802 |
} else { |
| 1803 |
wp_cache_set( $cache_id, '0', 'bbpress' ); |
| 1804 |
} |
| 1805 |
} |
| 1806 |
|
| 1807 |
return (int) apply_filters( 'bbp_get_forum_last_reply_id', (int) $reply_id, $forum_id ); |
| 1808 |
} |
| 1809 |
|
| 1810 |
/** Listeners *****************************************************************/ |
| 1811 |
|
| 1812 |
/** |
| 1813 |
* Check if it's a hidden forum or a topic or reply of a hidden forum and if |
| 1814 |
* the user can't view it, then sets a 404 |
| 1815 |
* |
| 1816 |
* @since bbPress (r2996) |
| 1817 |
* |
| 1818 |
* @uses current_user_can() To check if the current user can read private forums |
| 1819 |
* @uses is_singular() To check if it's a singular page |
| 1820 |
* @uses bbp_get_forum_post_type() To get the forum post type |
| 1821 |
* @uses bbp_get_topic_post_type() To get the topic post type |
| 1822 |
* @uses bbp_get_reply_post_type() TO get the reply post type |
| 1823 |
* @uses bbp_get_topic_forum_id() To get the topic forum id |
| 1824 |
* @uses bbp_get_reply_forum_id() To get the reply forum id |
| 1825 |
* @uses bbp_is_forum_hidden() To check if the forum is hidden or not |
| 1826 |
* @uses bbp_set_404() To set a 404 status |
| 1827 |
*/ |
| 1828 |
function bbp_forum_enforce_hidden() { |
| 1829 |
|
| 1830 |
// Bail if not viewing a single item or if user has caps |
| 1831 |
if ( !is_singular() || is_super_admin() || current_user_can( 'read_hidden_forums' ) ) |
| 1832 |
return; |
| 1833 |
|
| 1834 |
global $wp_query; |
| 1835 |
|
| 1836 |
// Define local variable |
| 1837 |
$forum_id = 0; |
| 1838 |
|
| 1839 |
// Check post type |
| 1840 |
switch ( $wp_query->get( 'post_type' ) ) { |
| 1841 |
|
| 1842 |
// Forum |
| 1843 |
case bbp_get_forum_post_type() : |
| 1844 |
$forum_id = bbp_get_forum_id( $wp_query->post->ID ); |
| 1845 |
break; |
| 1846 |
|
| 1847 |
// Topic |
| 1848 |
case bbp_get_topic_post_type() : |
| 1849 |
$forum_id = bbp_get_topic_forum_id( $wp_query->post->ID ); |
| 1850 |
break; |
| 1851 |
|
| 1852 |
// Reply |
| 1853 |
case bbp_get_reply_post_type() : |
| 1854 |
$forum_id = bbp_get_reply_forum_id( $wp_query->post->ID ); |
| 1855 |
break; |
| 1856 |
|
| 1857 |
} |
| 1858 |
|
| 1859 |
// If forum is explicitly hidden and user not capable, set 404 |
| 1860 |
if ( !empty( $forum_id ) && bbp_is_forum_hidden( $forum_id ) && !current_user_can( 'read_hidden_forums' ) ) |
| 1861 |
bbp_set_404(); |
| 1862 |
} |
| 1863 |
|
| 1864 |
/** |
| 1865 |
* Check if it's a private forum or a topic or reply of a private forum and if |
| 1866 |
* the user can't view it, then sets a 404 |
| 1867 |
* |
| 1868 |
* @since bbPress (r2996) |
| 1869 |
* |
| 1870 |
* @uses current_user_can() To check if the current user can read private forums |
| 1871 |
* @uses is_singular() To check if it's a singular page |
| 1872 |
* @uses bbp_get_forum_post_type() To get the forum post type |
| 1873 |
* @uses bbp_get_topic_post_type() To get the topic post type |
| 1874 |
* @uses bbp_get_reply_post_type() TO get the reply post type |
| 1875 |
* @uses bbp_get_topic_forum_id() To get the topic forum id |
| 1876 |
* @uses bbp_get_reply_forum_id() To get the reply forum id |
| 1877 |
* @uses bbp_is_forum_private() To check if the forum is private or not |
| 1878 |
* @uses bbp_set_404() To set a 404 status |
| 1879 |
*/ |
| 1880 |
function bbp_forum_enforce_private() { |
| 1881 |
|
| 1882 |
// Bail if not viewing a single item or if user has caps |
| 1883 |
if ( !is_singular() || is_super_admin() || current_user_can( 'read_private_forums' ) ) |
| 1884 |
return; |
| 1885 |
|
| 1886 |
global $wp_query; |
| 1887 |
|
| 1888 |
// Define local variable |
| 1889 |
$forum_id = 0; |
| 1890 |
|
| 1891 |
// Check post type |
| 1892 |
switch ( $wp_query->get( 'post_type' ) ) { |
| 1893 |
|
| 1894 |
// Forum |
| 1895 |
case bbp_get_forum_post_type() : |
| 1896 |
$forum_id = bbp_get_forum_id( $wp_query->post->ID ); |
| 1897 |
break; |
| 1898 |
|
| 1899 |
// Topic |
| 1900 |
case bbp_get_topic_post_type() : |
| 1901 |
$forum_id = bbp_get_topic_forum_id( $wp_query->post->ID ); |
| 1902 |
break; |
| 1903 |
|
| 1904 |
// Reply |
| 1905 |
case bbp_get_reply_post_type() : |
| 1906 |
$forum_id = bbp_get_reply_forum_id( $wp_query->post->ID ); |
| 1907 |
break; |
| 1908 |
|
| 1909 |
} |
| 1910 |
|
| 1911 |
// If forum is explicitly hidden and user not capable, set 404 |
| 1912 |
if ( !empty( $forum_id ) && bbp_is_forum_private( $forum_id ) && !current_user_can( 'read_private_forums' ) ) |
| 1913 |
bbp_set_404(); |
| 1914 |
} |
| 1915 |
|
| 1916 |
/** Permissions ***************************************************************/ |
| 1917 |
|
| 1918 |
/** |
| 1919 |
* Redirect if unathorized user is attempting to edit a forum |
| 1920 |
* |
| 1921 |
* @since bbPress (r3607) |
| 1922 |
* |
| 1923 |
* @uses bbp_is_forum_edit() |
| 1924 |
* @uses current_user_can() |
| 1925 |
* @uses bbp_get_forum_id() |
| 1926 |
* @uses wp_safe_redirect() |
| 1927 |
* @uses bbp_get_forum_permalink() |
| 1928 |
*/ |
| 1929 |
function bbp_check_forum_edit() { |
| 1930 |
|
| 1931 |
// Bail if not editing a topic |
| 1932 |
if ( !bbp_is_forum_edit() ) |
| 1933 |
return; |
| 1934 |
|
| 1935 |
// User cannot edit topic, so redirect back to reply |
| 1936 |
if ( !current_user_can( 'edit_forum', bbp_get_forum_id() ) ) { |
| 1937 |
wp_safe_redirect( bbp_get_forum_permalink() ); |
| 1938 |
exit(); |
| 1939 |
} |
| 1940 |
} |
| 1941 |
|
| 1942 |
/** |
| 1943 |
* Delete all topics (and their replies) for a specific forum ID |
| 1944 |
* |
| 1945 |
* @since bbPress (r3668) |
| 1946 |
* |
| 1947 |
* @param int $forum_id |
| 1948 |
* @uses bbp_get_forum_id() To validate the forum ID |
| 1949 |
* @uses bbp_is_forum() To make sure it's a forum |
| 1950 |
* @uses bbp_get_topic_post_type() To get the topic post type |
| 1951 |
* @uses bbp_topics() To make sure there are topics to loop through |
| 1952 |
* @uses wp_trash_post() To trash the post |
| 1953 |
* @uses update_post_meta() To update the forum meta of trashed topics |
| 1954 |
* @return If forum is not valid |
| 1955 |
*/ |
| 1956 |
function bbp_delete_forum_topics( $forum_id = 0 ) { |
| 1957 |
|
| 1958 |
// Validate forum ID |
| 1959 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 1960 |
if ( empty( $forum_id ) ) |
| 1961 |
return; |
| 1962 |
|
| 1963 |
// Forum is being permanently deleted, so its topics gotta go too |
| 1964 |
if ( $topics = new WP_Query( array( |
| 1965 |
'suppress_filters' => true, |
| 1966 |
'post_type' => bbp_get_topic_post_type(), |
| 1967 |
'post_parent' => $forum_id, |
| 1968 |
'post_status' => 'any', |
| 1969 |
'posts_per_page' => -1, |
| 1970 |
'nopaging' => true, |
| 1971 |
'fields' => 'id=>parent' |
| 1972 |
) ) ) { |
| 1973 |
foreach ( $topics->posts as $topic ) { |
| 1974 |
wp_delete_post( $topic->ID, true ); |
| 1975 |
} |
| 1976 |
|
| 1977 |
// Reset the $post global |
| 1978 |
wp_reset_postdata(); |
| 1979 |
} |
| 1980 |
} |
| 1981 |
|
| 1982 |
/** |
| 1983 |
* Trash all topics inside a forum |
| 1984 |
* |
| 1985 |
* @since bbPress (r3668) |
| 1986 |
* |
| 1987 |
* @param int $forum_id |
| 1988 |
* @uses bbp_get_forum_id() To validate the forum ID |
| 1989 |
* @uses bbp_is_forum() To make sure it's a forum |
| 1990 |
* @uses bbp_get_public_status_id() To return public post status |
| 1991 |
* @uses bbp_get_closed_status_id() To return closed post status |
| 1992 |
* @uses bbp_get_pending_status_id() To return pending post status |
| 1993 |
* @uses bbp_get_topic_post_type() To get the topic post type |
| 1994 |
* @uses wp_trash_post() To trash the post |
| 1995 |
* @uses update_post_meta() To update the forum meta of trashed topics |
| 1996 |
* @return If forum is not valid |
| 1997 |
*/ |
| 1998 |
function bbp_trash_forum_topics( $forum_id = 0 ) { |
| 1999 |
|
| 2000 |
// Validate forum ID |
| 2001 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 2002 |
if ( empty( $forum_id ) ) |
| 2003 |
return; |
| 2004 |
|
| 2005 |
// Allowed post statuses to pre-trash |
| 2006 |
$post_stati = join( ',', array( |
| 2007 |
bbp_get_public_status_id(), |
| 2008 |
bbp_get_closed_status_id(), |
| 2009 |
bbp_get_pending_status_id() |
| 2010 |
) ); |
| 2011 |
|
| 2012 |
// Forum is being trashed, so its topics are trashed too |
| 2013 |
if ( $topics = new WP_Query( array( |
| 2014 |
'suppress_filters' => true, |
| 2015 |
'post_type' => bbp_get_topic_post_type(), |
| 2016 |
'post_parent' => $forum_id, |
| 2017 |
'post_status' => $post_stati, |
| 2018 |
'posts_per_page' => -1, |
| 2019 |
'nopaging' => true, |
| 2020 |
'fields' => 'id=>parent' |
| 2021 |
) ) ) { |
| 2022 |
|
| 2023 |
// Prevent debug notices |
| 2024 |
$pre_trashed_topics = array(); |
| 2025 |
|
| 2026 |
// Loop through topics, trash them, and add them to array |
| 2027 |
foreach ( $topics->posts as $topic ) { |
| 2028 |
wp_trash_post( $topic->ID, true ); |
| 2029 |
$pre_trashed_topics[] = $topic->ID; |
| 2030 |
} |
| 2031 |
|
| 2032 |
// Set a post_meta entry of the topics that were trashed by this action. |
| 2033 |
// This is so we can possibly untrash them, without untrashing topics |
| 2034 |
// that were purposefully trashed before. |
| 2035 |
update_post_meta( $forum_id, '_bbp_pre_trashed_topics', $pre_trashed_topics ); |
| 2036 |
|
| 2037 |
// Reset the $post global |
| 2038 |
wp_reset_postdata(); |
| 2039 |
} |
| 2040 |
} |
| 2041 |
|
| 2042 |
/** |
| 2043 |
* Trash all topics inside a forum |
| 2044 |
* |
| 2045 |
* @since bbPress (r3668) |
| 2046 |
* |
| 2047 |
* @param int $forum_id |
| 2048 |
* @uses bbp_get_forum_id() To validate the forum ID |
| 2049 |
* @uses bbp_is_forum() To make sure it's a forum |
| 2050 |
* @uses get_post_meta() To update the forum meta of trashed topics |
| 2051 |
* @uses wp_untrash_post() To trash the post |
| 2052 |
* @return If forum is not valid |
| 2053 |
*/ |
| 2054 |
function bbp_untrash_forum_topics( $forum_id = 0 ) { |
| 2055 |
|
| 2056 |
// Validate forum ID |
| 2057 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 2058 |
|
| 2059 |
if ( empty( $forum_id ) ) |
| 2060 |
return; |
| 2061 |
|
| 2062 |
// Get the topics that were not previously trashed |
| 2063 |
$pre_trashed_topics = get_post_meta( $forum_id, '_bbp_pre_trashed_topics', true ); |
| 2064 |
|
| 2065 |
// There are topics to untrash |
| 2066 |
if ( !empty( $pre_trashed_topics ) ) { |
| 2067 |
|
| 2068 |
// Maybe reverse the trashed topics array |
| 2069 |
if ( is_array( $pre_trashed_topics ) ) |
| 2070 |
$pre_trashed_topics = array_reverse( $pre_trashed_topics ); |
| 2071 |
|
| 2072 |
// Loop through topics |
| 2073 |
foreach ( (array) $pre_trashed_topics as $topic ) { |
| 2074 |
wp_untrash_post( $topic ); |
| 2075 |
} |
| 2076 |
} |
| 2077 |
} |
| 2078 |
|
| 2079 |
/** Before Delete/Trash/Untrash ***********************************************/ |
| 2080 |
|
| 2081 |
/** |
| 2082 |
* Called before deleting a forum. |
| 2083 |
* |
| 2084 |
* This function is supplemental to the actual forum deletion which is |
| 2085 |
* handled by WordPress core API functions. It is used to clean up after |
| 2086 |
* a forum that is being deleted. |
| 2087 |
* |
| 2088 |
* @since bbPress (r3668) |
| 2089 |
* @uses bbp_get_forum_id() To get the forum id |
| 2090 |
* @uses bbp_is_forum() To check if the passed id is a forum |
| 2091 |
* @uses do_action() Calls 'bbp_delete_forum' with the forum id |
| 2092 |
*/ |
| 2093 |
function bbp_delete_forum( $forum_id = 0 ) { |
| 2094 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 2095 |
|
| 2096 |
if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) ) |
| 2097 |
return false; |
| 2098 |
|
| 2099 |
do_action( 'bbp_delete_forum', $forum_id ); |
| 2100 |
} |
| 2101 |
|
| 2102 |
/** |
| 2103 |
* Called before trashing a forum |
| 2104 |
* |
| 2105 |
* This function is supplemental to the actual forum being trashed which is |
| 2106 |
* handled by WordPress core API functions. It is used to clean up after |
| 2107 |
* a forum that is being trashed. |
| 2108 |
* |
| 2109 |
* @since bbPress (r3668) |
| 2110 |
* @uses bbp_get_forum_id() To get the forum id |
| 2111 |
* @uses bbp_is_forum() To check if the passed id is a forum |
| 2112 |
* @uses do_action() Calls 'bbp_trash_forum' with the forum id |
| 2113 |
*/ |
| 2114 |
function bbp_trash_forum( $forum_id = 0 ) { |
| 2115 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 2116 |
|
| 2117 |
if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) ) |
| 2118 |
return false; |
| 2119 |
|
| 2120 |
do_action( 'bbp_trash_forum', $forum_id ); |
| 2121 |
} |
| 2122 |
|
| 2123 |
/** |
| 2124 |
* Called before untrashing a forum |
| 2125 |
* |
| 2126 |
* @since bbPress (r3668) |
| 2127 |
* @uses bbp_get_forum_id() To get the forum id |
| 2128 |
* @uses bbp_is_forum() To check if the passed id is a forum |
| 2129 |
* @uses do_action() Calls 'bbp_untrash_forum' with the forum id |
| 2130 |
*/ |
| 2131 |
function bbp_untrash_forum( $forum_id = 0 ) { |
| 2132 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 2133 |
|
| 2134 |
if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) ) |
| 2135 |
return false; |
| 2136 |
|
| 2137 |
do_action( 'bbp_untrash_forum', $forum_id ); |
| 2138 |
} |
| 2139 |
|
| 2140 |
/** After Delete/Trash/Untrash ************************************************/ |
| 2141 |
|
| 2142 |
/** |
| 2143 |
* Called after deleting a forum |
| 2144 |
* |
| 2145 |
* @since bbPress (r3668) |
| 2146 |
* @uses bbp_get_forum_id() To get the forum id |
| 2147 |
* @uses bbp_is_forum() To check if the passed id is a forum |
| 2148 |
* @uses do_action() Calls 'bbp_deleted_forum' with the forum id |
| 2149 |
*/ |
| 2150 |
function bbp_deleted_forum( $forum_id = 0 ) { |
| 2151 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 2152 |
|
| 2153 |
if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) ) |
| 2154 |
return false; |
| 2155 |
|
| 2156 |
do_action( 'bbp_deleted_forum', $forum_id ); |
| 2157 |
} |
| 2158 |
|
| 2159 |
/** |
| 2160 |
* Called after trashing a forum |
| 2161 |
* |
| 2162 |
* @since bbPress (r3668) |
| 2163 |
* @uses bbp_get_forum_id() To get the forum id |
| 2164 |
* @uses bbp_is_forum() To check if the passed id is a forum |
| 2165 |
* @uses do_action() Calls 'bbp_trashed_forum' with the forum id |
| 2166 |
*/ |
| 2167 |
function bbp_trashed_forum( $forum_id = 0 ) { |
| 2168 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 2169 |
|
| 2170 |
if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) ) |
| 2171 |
return false; |
| 2172 |
|
| 2173 |
do_action( 'bbp_trashed_forum', $forum_id ); |
| 2174 |
} |
| 2175 |
|
| 2176 |
/** |
| 2177 |
* Called after untrashing a forum |
| 2178 |
* |
| 2179 |
* @since bbPress (r3668) |
| 2180 |
* @uses bbp_get_forum_id() To get the forum id |
| 2181 |
* @uses bbp_is_forum() To check if the passed id is a forum |
| 2182 |
* @uses do_action() Calls 'bbp_untrashed_forum' with the forum id |
| 2183 |
*/ |
| 2184 |
function bbp_untrashed_forum( $forum_id = 0 ) { |
| 2185 |
$forum_id = bbp_get_forum_id( $forum_id ); |
| 2186 |
|
| 2187 |
if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) ) |
| 2188 |
return false; |
| 2189 |
|
| 2190 |
do_action( 'bbp_untrashed_forum', $forum_id ); |
| 2191 |
} |
| 2192 |
|