Changeset 3654 for branches/plugin/bbp-includes/bbp-forum-functions.php
- Timestamp:
- 01/08/2012 10:32:12 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/plugin/bbp-includes/bbp-forum-functions.php
r3646 r3654 75 75 // Return new forum ID 76 76 return $forum_id; 77 } 78 79 /** Post Form Handlers ********************************************************/ 80 81 /** 82 * Handles the front end forum submission 83 * 84 * @uses bbPress:errors::add() To log various error messages 85 * @uses check_admin_referer() To verify the nonce and check the referer 86 * @uses bbp_is_anonymous() To check if an anonymous post is being made 87 * @uses current_user_can() To check if the current user can publish forum 88 * @uses bbp_get_current_user_id() To get the current user id 89 * @uses bbp_filter_anonymous_post_data() To filter anonymous data 90 * @uses bbp_set_current_anonymous_user_data() To set the anonymous user cookies 91 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error} 92 * @uses esc_attr() For sanitization 93 * @uses bbp_is_forum_category() To check if the forum is a category 94 * @uses bbp_is_forum_closed() To check if the forum is closed 95 * @uses bbp_is_forum_private() To check if the forum is private 96 * @uses bbp_check_for_flood() To check for flooding 97 * @uses bbp_check_for_duplicate() To check for duplicates 98 * @uses bbp_get_forum_post_type() To get the forum post type 99 * @uses remove_filter() To remove 'wp_filter_kses' filters if needed 100 * @uses apply_filters() Calls 'bbp_new_forum_pre_title' with the content 101 * @uses apply_filters() Calls 'bbp_new_forum_pre_content' with the content 102 * @uses bbPress::errors::get_error_codes() To get the {@link WP_Error} errors 103 * @uses wp_insert_post() To insert the forum 104 * @uses do_action() Calls 'bbp_new_forum' with the forum id, forum id, 105 * anonymous data and reply author 106 * @uses bbp_stick_forum() To stick or super stick the forum 107 * @uses bbp_unstick_forum() To unstick the forum 108 * @uses bbp_get_forum_permalink() To get the forum permalink 109 * @uses wp_safe_redirect() To redirect to the forum link 110 * @uses bbPress::errors::get_error_messages() To get the {@link WP_Error} error 111 * messages 112 */ 113 function bbp_new_forum_handler() { 114 115 // Bail if not a POST action 116 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) 117 return; 118 119 // Bail if action is not bbp-new-forum 120 if ( empty( $_POST['action'] ) || ( 'bbp-new-forum' !== $_POST['action'] ) ) 121 return; 122 123 // Nonce check 124 check_admin_referer( 'bbp-new-forum' ); 125 126 // Define local variable(s) 127 $view_all = $anonymous_data = false; 128 $forum_parent_id = $forum_author = 0; 129 $forum_title = $forum_content = ''; 130 131 /** Forum Author **********************************************************/ 132 133 // User cannot create forums 134 if ( !current_user_can( 'publish_forums' ) ) { 135 bbp_add_error( 'bbp_forum_permissions', __( '<strong>ERROR</strong>: You do not have permission to create new forums.', 'bbpress' ) ); 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 cannot be empty.', 'bbpress' ) ); 170 171 /** Forum Parent **********************************************************/ 172 173 // Cast Forum parent id to int 174 $forum_parent_id = (int) $_POST['bbp_forum_parent_id']; 175 176 // Forum exists 177 if ( !empty( $forum_parent_id ) ) { 178 179 // Forum is a category 180 if ( bbp_is_forum_category( $forum_parent_id ) ) 181 bbp_add_error( 'bbp_edit_forum_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No forums can be created in this forum.', 'bbpress' ) ); 182 183 // Forum is closed and user cannot access 184 if ( bbp_is_forum_closed( $forum_parent_id ) && !current_user_can( 'edit_forum', $forum_parent_id ) ) 185 bbp_add_error( 'bbp_edit_forum_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new forums.', 'bbpress' ) ); 186 187 // Forum is private and user cannot access 188 if ( bbp_is_forum_private( $forum_parent_id ) && !current_user_can( 'read_private_forums' ) ) 189 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' ) ); 190 191 // Forum is hidden and user cannot access 192 if ( bbp_is_forum_hidden( $forum_parent_id ) && !current_user_can( 'read_hidden_forums' ) ) 193 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' ) ); 194 } 195 196 /** Forum Flooding ********************************************************/ 197 198 if ( !bbp_check_for_flood( $anonymous_data, $forum_author ) ) 199 bbp_add_error( 'bbp_forum_flood', __( '<strong>ERROR</strong>: Slow down; you move too fast.', 'bbpress' ) ); 200 201 /** Forum Duplicate *******************************************************/ 202 203 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 ) ) ) 204 bbp_add_error( 'bbp_forum_duplicate', __( '<strong>ERROR</strong>: This forum already exists.', 'bbpress' ) ); 205 206 /** Forum Blacklist *******************************************************/ 207 208 if ( !bbp_check_for_blacklist( $anonymous_data, $forum_author, $forum_title, $forum_content ) ) 209 bbp_add_error( 'bbp_forum_blacklist', __( '<strong>ERROR</strong>: Your forum cannot be created at this time.', 'bbpress' ) ); 210 211 /** Forum Moderation ******************************************************/ 212 213 $post_status = bbp_get_public_status_id(); 214 if ( !bbp_check_for_moderation( $anonymous_data, $forum_author, $forum_title, $forum_content ) ) 215 $post_status = bbp_get_pending_status_id(); 216 217 /** Additional Actions (Before Save) **************************************/ 218 219 do_action( 'bbp_new_forum_pre_extras' ); 220 221 /** No Errors *************************************************************/ 222 223 if ( !bbp_has_errors() ) { 224 225 /** Create new forum **************************************************/ 226 227 // Add the content of the form to $post as an array 228 $forum_data = array( 229 'post_author' => $forum_author, 230 'post_title' => $forum_title, 231 'post_content' => $forum_content, 232 'post_parent' => $forum_parent_id, 233 'post_status' => $post_status, 234 'post_type' => bbp_get_forum_post_type(), 235 'comment_status' => 'closed' 236 ); 237 238 // Just in time manipulation of forum data before being created 239 $forum_data = apply_filters( 'bbp_new_forum_pre_insert', $forum_data ); 240 241 // Insert forum 242 $forum_id = wp_insert_post( $forum_data ); 243 244 /** No Errors *********************************************************/ 245 246 if ( !empty( $forum_id ) && !is_wp_error( $forum_id ) ) { 247 248 /** Trash Check ***************************************************/ 249 250 // If the forum is trash, or the forum_status is switched to 251 // trash, trash it properly 252 if ( ( get_post_field( 'post_status', $forum_id ) == bbp_get_trash_status_id() ) || ( $forum_data['post_status'] == bbp_get_trash_status_id() ) ) { 253 254 // Trash the reply 255 wp_trash_post( $forum_id ); 256 257 // Force view=all 258 $view_all = true; 259 } 260 261 /** Spam Check ****************************************************/ 262 263 // If reply or forum are spam, officially spam this reply 264 if ( $forum_data['post_status'] == bbp_get_spam_status_id() ) { 265 add_post_meta( $forum_id, '_bbp_spam_meta_status', bbp_get_public_status_id() ); 266 267 // Force view=all 268 $view_all = true; 269 } 270 271 /** Update counts, etc... *****************************************/ 272 273 $forum_args = array( 274 'forum_id' => $forum_id, 275 'post_parent' => $forum_parent_id, 276 'forum_author' => $forum_author, 277 'last_topic_id' => 0, 278 'last_reply_id' => 0, 279 'last_active_id' => 0, 280 'last_active_time' => 0, 281 'last_active_status' => bbp_get_public_status_id() 282 ); 283 do_action( 'bbp_new_forum', $forum_args ); 284 285 /** Redirect ******************************************************/ 286 287 // Redirect to 288 $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : ''; 289 290 // Get the forum URL 291 $redirect_url = bbp_get_forum_permalink( $forum_id, $redirect_to ); 292 293 // Add view all? 294 if ( bbp_get_view_all() || !empty( $view_all ) ) { 295 296 // User can moderate, so redirect to forum with view all set 297 if ( current_user_can( 'moderate' ) ) { 298 $redirect_url = bbp_add_view_all( $redirect_url ); 299 300 // User cannot moderate, so redirect to forum 301 } else { 302 $redirect_url = bbp_get_forum_permalink( $forum_id ); 303 } 304 } 305 306 // Allow to be filtered 307 $redirect_url = apply_filters( 'bbp_new_forum_redirect_to', $redirect_url, $redirect_to ); 308 309 /** Successful Save ***********************************************/ 310 311 // Redirect back to new forum 312 wp_safe_redirect( $redirect_url ); 313 314 // For good measure 315 exit(); 316 317 // Errors 318 } else { 319 $append_error = ( is_wp_error( $forum_id ) && $forum_id->get_error_message() ) ? $forum_id->get_error_message() . ' ' : ''; 320 bbp_add_error( 'bbp_forum_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your forum:' . $append_error, 'bbpress' ) ); 321 } 322 } 323 } 324 325 /** 326 * Handles the front end edit forum submission 327 * 328 * @uses bbPress:errors::add() To log various error messages 329 * @uses bbp_get_forum() To get the forum 330 * @uses check_admin_referer() To verify the nonce and check the referer 331 * @uses bbp_is_forum_anonymous() To check if forum is by an anonymous user 332 * @uses current_user_can() To check if the current user can edit the forum 333 * @uses bbp_filter_anonymous_post_data() To filter anonymous data 334 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error} 335 * @uses esc_attr() For sanitization 336 * @uses bbp_is_forum_category() To check if the forum is a category 337 * @uses bbp_is_forum_closed() To check if the forum is closed 338 * @uses bbp_is_forum_private() To check if the forum is private 339 * @uses remove_filter() To remove 'wp_filter_kses' filters if needed 340 * @uses apply_filters() Calls 'bbp_edit_forum_pre_title' with the title and 341 * forum id 342 * @uses apply_filters() Calls 'bbp_edit_forum_pre_content' with the content 343 * and forum id 344 * @uses bbPress::errors::get_error_codes() To get the {@link WP_Error} errors 345 * @uses wp_save_post_revision() To save a forum revision 346 * @uses bbp_update_forum_revision_log() To update the forum revision log 347 * @uses bbp_stick_forum() To stick or super stick the forum 348 * @uses bbp_unstick_forum() To unstick the forum 349 * @uses wp_update_post() To update the forum 350 * @uses do_action() Calls 'bbp_edit_forum' with the forum id, forum id, 351 * anonymous data and reply author 352 * @uses bbp_move_forum_handler() To handle movement of a forum from one forum 353 * to another 354 * @uses bbp_get_forum_permalink() To get the forum permalink 355 * @uses wp_safe_redirect() To redirect to the forum link 356 * @uses bbPress::errors::get_error_messages() To get the {@link WP_Error} error 357 * messages 358 */ 359 function bbp_edit_forum_handler() { 360 361 // Bail if not a POST action 362 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) 363 return; 364 365 // Bail if action is not bbp-edit-forum 366 if ( empty( $_POST['action'] ) || ( 'bbp-edit-forum' !== $_POST['action'] ) ) 367 return; 368 369 // Define local variable(s) 370 $forum = $forum_id = $forum_parent_id = $anonymous_data = 0; 371 $forum_title = $forum_content = $forum_edit_reason = ''; 372 $terms = array( bbp_get_forum_tag_tax_id() => array() ); 373 374 /** Forum *****************************************************************/ 375 376 // Forum id was not passed 377 if ( empty( $_POST['bbp_forum_id'] ) ) { 378 bbp_add_error( 'bbp_edit_forum_id', __( '<strong>ERROR</strong>: Forum ID not found.', 'bbpress' ) ); 379 380 // Forum id was passed 381 } elseif ( is_numeric( $_POST['bbp_forum_id'] ) ) { 382 $forum_id = (int) $_POST['bbp_forum_id']; 383 $forum = bbp_get_forum( $forum_id ); 384 } 385 386 // Forum does not exist 387 if ( empty( $forum ) ) { 388 bbp_add_error( 'bbp_edit_forum_not_found', __( '<strong>ERROR</strong>: The forum you want to edit was not found.', 'bbpress' ) ); 389 390 // Forum exists 391 } else { 392 393 // Nonce check 394 check_admin_referer( 'bbp-edit-forum_' . $forum_id ); 395 396 // Check users ability to create new forum 397 if ( !bbp_is_forum_anonymous( $forum_id ) ) { 398 399 // User cannot edit this forum 400 if ( !current_user_can( 'edit_forum', $forum_id ) ) { 401 bbp_add_error( 'bbp_edit_forum_permissions', __( '<strong>ERROR</strong>: You do not have permission to edit that forum.', 'bbpress' ) ); 402 } 403 404 // It is an anonymous post 405 } else { 406 407 // Filter anonymous data 408 $anonymous_data = bbp_filter_anonymous_post_data( array(), true ); 409 } 410 } 411 412 // Remove wp_filter_kses filters from title and content for capable users and if the nonce is verified 413 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'] ) ) { 414 remove_filter( 'bbp_edit_forum_pre_title', 'wp_filter_kses' ); 415 remove_filter( 'bbp_edit_forum_pre_content', 'wp_filter_kses' ); 416 } 417 418 /** Forum Parent ***********************************************************/ 419 420 // Forum id was passed 421 if ( is_numeric( $_POST['bbp_forum_parent_id'] ) ) { 422 $forum_parent_id = (int) $_POST['bbp_forum_parent_id']; 423 } 424 425 // Current forum this forum is in 426 $current_parent_forum_id = bbp_get_forum_parent( $forum_id ); 427 428 // Forum exists 429 if ( !empty( $forum_parent_id ) && ( $forum_parent_id !== $current_parent_forum_id ) ) { 430 431 // Forum is closed and user cannot access 432 if ( bbp_is_forum_closed( $forum_parent_id ) && !current_user_can( 'edit_forum', $forum_parent_id ) ) { 433 bbp_add_error( 'bbp_edit_forum_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new forums.', 'bbpress' ) ); 434 } 435 436 // Forum is private and user cannot access 437 if ( bbp_is_forum_private( $forum_parent_id ) && !current_user_can( 'read_private_forums' ) ) { 438 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' ) ); 439 } 440 441 // Forum is hidden and user cannot access 442 if ( bbp_is_forum_hidden( $forum_parent_id ) && !current_user_can( 'read_hidden_forums' ) ) { 443 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' ) ); 444 } 445 } 446 447 /** Forum Title ***********************************************************/ 448 449 if ( !empty( $_POST['bbp_forum_title'] ) ) 450 $forum_title = esc_attr( strip_tags( $_POST['bbp_forum_title'] ) ); 451 452 // Filter and sanitize 453 $forum_title = apply_filters( 'bbp_edit_forum_pre_title', $forum_title, $forum_id ); 454 455 // No forum title 456 if ( empty( $forum_title ) ) 457 bbp_add_error( 'bbp_edit_forum_title', __( '<strong>ERROR</strong>: Your forum needs a title.', 'bbpress' ) ); 458 459 /** Forum Content *********************************************************/ 460 461 if ( !empty( $_POST['bbp_forum_content'] ) ) 462 $forum_content = $_POST['bbp_forum_content']; 463 464 // Filter and sanitize 465 $forum_content = apply_filters( 'bbp_edit_forum_pre_content', $forum_content, $forum_id ); 466 467 // No forum content 468 if ( empty( $forum_content ) ) 469 bbp_add_error( 'bbp_edit_forum_content', __( '<strong>ERROR</strong>: Your forum cannot be empty.', 'bbpress' ) ); 470 471 /** forum Blacklist *******************************************************/ 472 473 if ( !bbp_check_for_blacklist( $anonymous_data, bbp_get_forum_author_id( $forum_id ), $forum_title, $forum_content ) ) 474 bbp_add_error( 'bbp_forum_blacklist', __( '<strong>ERROR</strong>: Your forum cannot be edited at this time.', 'bbpress' ) ); 475 476 /** Forum Moderation ******************************************************/ 477 478 $post_status = bbp_get_public_status_id(); 479 if ( !bbp_check_for_moderation( $anonymous_data, bbp_get_forum_author_id( $forum_id ), $forum_title, $forum_content ) ) 480 $post_status = bbp_get_pending_status_id(); 481 482 /** Additional Actions (Before Save) **************************************/ 483 484 do_action( 'bbp_edit_forum_pre_extras', $forum_id ); 485 486 /** No Errors *************************************************************/ 487 488 if ( !bbp_has_errors() ) { 489 490 /** Update the forum **************************************************/ 491 492 // Add the content of the form to $post as an array 493 $forum_data = array( 494 'ID' => $forum_id, 495 'post_title' => $forum_title, 496 'post_content' => $forum_content, 497 'post_status' => $post_status, 498 'post_parent' => $forum_parent_id 499 ); 500 501 // Just in time manipulation of forum data before being edited 502 $forum_data = apply_filters( 'bbp_edit_forum_pre_insert', $forum_data ); 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 527 /** No Errors *********************************************************/ 528 529 if ( !empty( $forum_id ) && !is_wp_error( $forum_id ) ) { 530 531 // Update counts, etc... 532 $forum_args = array( 533 'forum_id' => $forum_id, 534 'post_parent' => $forum_parent_id, 535 'forum_author' => $forum->post_author, 536 'last_topic_id' => 0, 537 'last_reply_id' => 0, 538 'last_active_id' => 0, 539 'last_active_time' => 0, 540 'last_active_status' => bbp_get_public_status_id() 541 ); 542 do_action( 'bbp_edit_forum', $forum_args ); 543 544 // If the new forum parent id is not equal to the old forum parent 545 // id, run the bbp_move_forum action and pass the forum's parent id 546 // as the first arg and new forum parent id as the second. 547 // @todo implement 548 //if ( $forum_id != $forum->post_parent ) 549 // bbp_move_forum_handler( $forum_parent_id, $forum->post_parent, $forum_id ); 550 551 /** Additional Actions (After Save) *******************************/ 552 553 do_action( 'bbp_edit_forum_post_extras', $forum_id ); 554 555 /** Redirect ******************************************************/ 556 557 // Redirect to 558 $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : ''; 559 560 // View all? 561 $view_all = bbp_get_view_all(); 562 563 // Get the forum URL 564 $forum_url = bbp_get_forum_permalink( $forum_id, $redirect_to ); 565 566 // Add view all? 567 if ( !empty( $view_all ) ) 568 $forum_url = bbp_add_view_all( $forum_url ); 569 570 // Allow to be filtered 571 $forum_url = apply_filters( 'bbp_edit_forum_redirect_to', $forum_url, $view_all, $redirect_to ); 572 573 /** Successful Edit ***********************************************/ 574 575 // Redirect back to new forum 576 wp_safe_redirect( $forum_url ); 577 578 // For good measure 579 exit(); 580 581 /** Errors ************************************************************/ 582 583 } else { 584 $append_error = ( is_wp_error( $forum_id ) && $forum_id->get_error_message() ) ? $forum_id->get_error_message() . ' ' : ''; 585 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' ) ); 586 } 587 } 77 588 } 78 589
Note: See TracChangeset
for help on using the changeset viewer.