[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * BuddyPress Forums Functions. 4 * 5 * @package BuddyPress 6 * @subpackage ForumsFunctions 7 * @since 1.5.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 /** bbPress 2.x ***************************************************************/ 14 15 /** 16 * Is see bbPress 2.x is installed and active? 17 * 18 * @since 1.6.0 19 * 20 * @return boolean True if bbPress 2.x is active, false if not. 21 */ 22 function bp_forums_is_bbpress_active() { 23 24 // Single site. 25 if ( is_plugin_active( 'bbpress/bbpress.php' ) ) 26 return true; 27 28 // Network active. 29 if ( is_plugin_active_for_network( 'bbpress/bbpress.php' ) ) 30 return true; 31 32 // Nope. 33 return false; 34 } 35 36 /** bbPress 1.x ***************************************************************/ 37 38 /** 39 * See if bbPress 1.x is installed correctly. 40 * 41 * "Installed correctly" means that the bb-config-location option is set, and 42 * the referenced file exists. 43 * 44 * @since 1.2.0 45 * 46 * @return boolean True if option exists, false if not. 47 */ 48 function bp_forums_is_installed_correctly() { 49 $bp = buddypress(); 50 51 if ( isset( $bp->forums->bbconfig ) && is_file( $bp->forums->bbconfig ) ) 52 return true; 53 54 return false; 55 } 56 57 /** 58 * Does the forums component have a directory page registered? 59 * 60 * Checks $bp pages global and looks for directory page. 61 * 62 * @since 1.5.0 63 * 64 * @global BuddyPress $bp The one true BuddyPress instance. 65 * 66 * @return bool True if set, False if empty. 67 */ 68 function bp_forums_has_directory() { 69 return (bool) !empty( buddypress()->pages->forums->id ); 70 } 71 72 /** Forum Functions ***********************************************************/ 73 74 /** 75 * Get a forum by ID. 76 * 77 * Wrapper for {@link bb_get_forum()}. 78 * 79 * @since 1.0.0 80 * 81 * @param int $forum_id ID of the forum being fetched. 82 * @return object bbPress forum object. 83 */ 84 function bp_forums_get_forum( $forum_id ) { 85 86 /** This action is documented in bp-forums/bp-forums-screens.php */ 87 do_action( 'bbpress_init' ); 88 return bb_get_forum( $forum_id ); 89 } 90 91 /** 92 * Create a forum. 93 * 94 * Wrapper for {@link bb_new_forum()}. 95 * 96 * @since 1.0.0 97 * 98 * @param array|string $args { 99 * Forum setup arguments. 100 * @type string $forum_name Name of the forum. 101 * @type string $forum_desc Description of the forum. 102 * @type int $forum_parent_id ID of the forum parent. Default: value of 103 * {@link bp_forums_parent_forums_id()}. 104 * @type bool $forum_order Order. 105 * @type int $forum_is_category Whether the forum is a category. Default: 0. 106 * } 107 * @return int ID of the newly created forum. 108 */ 109 function bp_forums_new_forum( $args = '' ) { 110 111 /** This action is documented in bp-forums/bp-forums-screens.php */ 112 do_action( 'bbpress_init' ); 113 114 $r = wp_parse_args( $args, array( 115 'forum_name' => '', 116 'forum_desc' => '', 117 'forum_parent_id' => bp_forums_parent_forum_id(), 118 'forum_order' => false, 119 'forum_is_category' => 0 120 ) ); 121 extract( $r, EXTR_SKIP ); 122 123 return bb_new_forum( array( 'forum_name' => stripslashes( $forum_name ), 'forum_desc' => stripslashes( $forum_desc ), 'forum_parent' => $forum_parent_id, 'forum_order' => $forum_order, 'forum_is_category' => $forum_is_category ) ); 124 } 125 126 /** 127 * Update a forum. 128 * 129 * Wrapper for {@link bb_update_forum(}. 130 * 131 * @since 1.2.5 132 * 133 * @param array|string $args { 134 * Forum setup arguments. 135 * @type int $forum_id ID of the forum to be updated. 136 * @type string $forum_name Name of the forum. 137 * @type string $forum_desc Description of the forum. 138 * @type int $forum_parent_id ID of the forum parent. Default: value of 139 * {@link bp_forums_parent_forums_id()}. 140 * @type bool $forum_order Order. 141 * @type int $forum_is_category Whether the forum is a category. Default: 0. 142 * } 143 * @return bool True on success, false on failure. 144 */ 145 function bp_forums_update_forum( $args = '' ) { 146 147 /** This action is documented in bp-forums/bp-forums-screens.php */ 148 do_action( 'bbpress_init' ); 149 150 $r = wp_parse_args( $args, array( 151 'forum_id' => '', 152 'forum_name' => '', 153 'forum_desc' => '', 154 'forum_slug' => '', 155 'forum_parent_id' => bp_forums_parent_forum_id(), 156 'forum_order' => false, 157 'forum_is_category' => 0 158 ) ); 159 extract( $r, EXTR_SKIP ); 160 161 return bb_update_forum( array( 'forum_id' => (int) $forum_id, 'forum_name' => stripslashes( $forum_name ), 'forum_desc' => stripslashes( $forum_desc ), 'forum_slug' => stripslashes( $forum_slug ), 'forum_parent' => $forum_parent_id, 'forum_order' => $forum_order, 'forum_is_category' => $forum_is_category ) ); 162 } 163 164 /** 165 * Delete a group forum by the group id. 166 * 167 * @since 1.6.0 168 * 169 * @param int $group_id ID of the group whose forum is to be deleted. 170 */ 171 function bp_forums_delete_group_forum( $group_id ) { 172 $forum_id = groups_get_groupmeta( $group_id, 'forum_id' ); 173 174 if ( !empty( $forum_id ) && is_int( $forum_id ) ) { 175 176 /** This action is documented in bp-forums/bp-forums-screens.php */ 177 do_action( 'bbpress_init' ); 178 bb_delete_forum( $forum_id ); 179 } 180 } 181 add_action( 'groups_delete_group', 'bp_forums_delete_group_forum' ); 182 183 /** Topic Functions ***********************************************************/ 184 185 /** 186 * Fetch a set of forum topics. 187 * 188 * @since 1.1.0 189 * 190 * @param array|string $args { 191 * @type string $type Order or filter type. Default: 'newest'. 192 * @type int $forum_id Optional. Pass a forum ID to limit results to topics 193 * associated with that forum. 194 * @type int $user_id Optional. Pass a user ID to limit results to topics 195 * belonging to that user. 196 * @type int $page Optional. Number of the results page to return. 197 * Default: 1. 198 * @type int $per_page Optional. Number of results to return per page. 199 * Default: 15. 200 * @type int $offset Optional. Numeric offset for results. 201 * @type int $number Amount to query for. 202 * @type array $exclude Optional. Topic IDs to exclude. 203 * @type string $show_stickies Whether to show sticky topics. 204 * @type mixed $filter If $type = 'tag', filter is the tag name. Otherwise, 205 * $filter is terms to search on. 206 * } 207 * @return array Found topics. 208 */ 209 function bp_forums_get_forum_topics( $args = '' ) { 210 211 /** This action is documented in bp-forums/bp-forums-screens.php */ 212 do_action( 'bbpress_init' ); 213 214 $r = wp_parse_args( $args, array( 215 'type' => 'newest', 216 'forum_id' => false, 217 'user_id' => false, 218 'page' => 1, 219 'per_page' => 15, 220 'offset' => false, 221 'number' => false, 222 'exclude' => false, 223 'show_stickies' => 'all', 224 'filter' => false // If $type = tag then filter is the tag name, otherwise it's terms to search on. 225 ) ); 226 extract( $r, EXTR_SKIP ); 227 228 if ( class_exists( 'BB_Query' ) ) { 229 switch ( $type ) { 230 case 'newest': 231 $query = new BB_Query( 'topic', array( 'forum_id' => $forum_id, 'topic_author_id' => $user_id, 'per_page' => $per_page, 'page' => $page, 'number' => $per_page, 'exclude' => $exclude, 'topic_title' => $filter, 'sticky' => $show_stickies, 'offset' => $offset, 'number' => $number ), 'get_latest_topics' ); 232 $topics =& $query->results; 233 break; 234 235 case 'popular': 236 $query = new BB_Query( 'topic', array( 'forum_id' => $forum_id, 'topic_author_id' => $user_id, 'per_page' => $per_page, 'page' => $page, 'order_by' => 't.topic_posts', 'topic_title' => $filter, 'sticky' => $show_stickies, 'offset' => $offset, 'number' => $number ) ); 237 $topics =& $query->results; 238 break; 239 240 case 'unreplied': 241 $query = new BB_Query( 'topic', array( 'forum_id' => $forum_id, 'topic_author_id' => $user_id, 'post_count' => 1, 'per_page' => $per_page, 'page' => $page, 'order_by' => 't.topic_time', 'topic_title' => $filter, 'sticky' => $show_stickies, 'offset' => $offset, 'number' => $number ) ); 242 $topics =& $query->results; 243 break; 244 245 case 'tags': 246 $query = new BB_Query( 'topic', array( 'forum_id' => $forum_id, 'topic_author_id' => $user_id, 'tag' => $filter, 'per_page' => $per_page, 'page' => $page, 'order_by' => 't.topic_time', 'sticky' => $show_stickies, 'offset' => $offset, 'number' => $number ) ); 247 $topics =& $query->results; 248 break; 249 } 250 } else { 251 $topics = array(); 252 } 253 254 /** 255 * Filters the found forum topics for provided arguments. 256 * 257 * @since 1.1.0 258 * 259 * @param array $topics Array of found topics. Passed by reference. 260 * @param array $r Array of parsed arguments for query. Passed by reference. 261 */ 262 return apply_filters_ref_array( 'bp_forums_get_forum_topics', array( &$topics, &$r ) ); 263 } 264 265 /** 266 * Get additional details about a given forum topic. 267 * 268 * @since 1.0.0 269 * 270 * @param int $topic_id ID of the topic for which you're fetching details. 271 * @return object Details about the topic. 272 */ 273 function bp_forums_get_topic_details( $topic_id ) { 274 275 /** This action is documented in bp-forums/bp-forums-screens.php */ 276 do_action( 'bbpress_init' ); 277 278 $query = new BB_Query( 'topic', 'topic_id=' . $topic_id . '&page=1' /* Page override so bbPress doesn't use the URI */ ); 279 280 return $query->results[0]; 281 } 282 283 /** 284 * Get the numeric ID of a topic from the topic slug. 285 * 286 * Wrapper for {@link bb_get_id_from_slug()}. 287 * 288 * @since 1.1.0 289 * 290 * @param string $topic_slug Slug of the topic. 291 * @return int|bool ID of the topic (if found), false on failure. 292 */ 293 function bp_forums_get_topic_id_from_slug( $topic_slug ) { 294 295 /** This action is documented in bp-forums/bp-forums-screens.php */ 296 do_action( 'bbpress_init' ); 297 298 if ( empty( $topic_slug ) ) 299 return false; 300 301 return bb_get_id_from_slug( 'topic', $topic_slug ); 302 } 303 304 /** 305 * Create a new forum topic. 306 * 307 * @since 1.0.0 308 * 309 * @param array|string $args { 310 * @type string $topic_title Title of the new topic. 311 * @type string $topic_slug Slug of the new topic. 312 * @type string $topic_text Text of the new topic. 313 * @type int $topic_poster ID of the user posting the topic. Default: ID of 314 * the logged-in user. 315 * @type string $topic_poster_name Display name of the user posting the 316 * topic. Default: 'fullname' of the logged-in user. 317 * @type int $topic_last_poster ID of the user who last posted to the topic. 318 * Default: ID of the logged-in user. 319 * @type string $topic_last_poster_name Display name of the user who last 320 * posted to the topic. Default: 'fullname' of the logged-in user. 321 * @type string $topic_start_time Date/time when the topic was created. 322 * Default: the current time, as reported by bp_core_current_time(). 323 * @type string $topic_time Date/time when the topic was created. 324 * Default: the current time, as reported by bp_core_current_time(). 325 * @type int $topic_open Whether the topic is open. Default: 1 (open). 326 * @type array|string|bool $topic_tags Array or comma-separated list of 327 * topic tags. False to leave empty. Default: false. 328 * @type int $forum_id ID of the forum to which the topic belongs. 329 * Default: 0. 330 * } 331 * @return object Details about the new topic, as returned by 332 * {@link bp_forums_get_topic_details()}. 333 */ 334 function bp_forums_new_topic( $args = '' ) { 335 $bp = buddypress(); 336 337 /** This action is documented in bp-forums/bp-forums-screens.php */ 338 do_action( 'bbpress_init' ); 339 340 $r = wp_parse_args( $args, array( 341 'topic_title' => '', 342 'topic_slug' => '', 343 'topic_text' => '', 344 'topic_poster' => bp_loggedin_user_id(), // Accepts ids. 345 'topic_poster_name' => $bp->loggedin_user->fullname, // Accept names. 346 'topic_last_poster' => bp_loggedin_user_id(), // Accepts ids. 347 'topic_last_poster_name' => $bp->loggedin_user->fullname, // Accept names. 348 'topic_start_time' => bp_core_current_time(), 349 'topic_time' => bp_core_current_time(), 350 'topic_open' => 1, 351 'topic_tags' => false, // Accepts array or comma delimited. 352 'forum_id' => 0 // Accepts ids or slugs. 353 ) ); 354 extract( $r, EXTR_SKIP ); 355 356 $topic_title = strip_tags( $topic_title ); 357 358 if ( empty( $topic_title ) || !strlen( trim( $topic_title ) ) ) 359 return false; 360 361 if ( empty( $topic_poster ) ) 362 return false; 363 364 if ( bp_is_user_inactive( $topic_poster ) ) 365 return false; 366 367 if ( empty( $topic_slug ) ) 368 $topic_slug = sanitize_title( $topic_title ); 369 370 if ( !$topic_id = bb_insert_topic( array( 'topic_title' => stripslashes( $topic_title ), 'topic_slug' => $topic_slug, 'topic_poster' => $topic_poster, 'topic_poster_name' => $topic_poster_name, 'topic_last_poster' => $topic_last_poster, 'topic_last_poster_name' => $topic_last_poster_name, 'topic_start_time' => $topic_start_time, 'topic_time' => $topic_time, 'topic_open' => $topic_open, 'forum_id' => (int) $forum_id, 'tags' => $topic_tags ) ) ) 371 return false; 372 373 // Now insert the first post. 374 if ( !bp_forums_insert_post( array( 'topic_id' => $topic_id, 'post_text' => $topic_text, 'post_time' => $topic_time, 'poster_id' => $topic_poster ) ) ) 375 return false; 376 377 /** 378 * Fires after a new forum topic has been created. 379 * 380 * @since 1.0.0 381 * 382 * @param int $topic_id ID of the newly created topic post. 383 */ 384 do_action( 'bp_forums_new_topic', $topic_id ); 385 386 return $topic_id; 387 } 388 389 /** 390 * Update a topic's details. 391 * 392 * @since 1.1.0 393 * 394 * @param array|string $args { 395 * Array of arguments. 396 * @type int $topic_id ID of the topic being updated. 397 * @type string $topic_title Updated title of the topic. 398 * @type string $topic_text Updated text of the topic. 399 * @type array|string|bool $topic_tags Array or comma-separated list of 400 * topic tags. False to leave empty. 401 * Default false. 402 * } 403 * @return object Details about the new topic, as returned by 404 * {@link bp_forums_get_topic_details()}. 405 */ 406 function bp_forums_update_topic( $args = '' ) { 407 408 /** This action is documented in bp-forums/bp-forums-screens.php */ 409 do_action( 'bbpress_init' ); 410 411 $r = wp_parse_args( $args, array( 412 'topic_id' => false, 413 'topic_title' => '', 414 'topic_text' => '', 415 'topic_tags' => false 416 ) ); 417 extract( $r, EXTR_SKIP ); 418 419 // Check if the user is a spammer. 420 if ( bp_is_user_inactive( bp_loggedin_user_id() ) ) 421 return false; 422 423 // The bb_insert_topic() function will append tags, but not remove them. So we remove all existing tags. 424 bb_remove_topic_tags( $topic_id ); 425 426 if ( !$topic_id = bb_insert_topic( array( 'topic_id' => $topic_id, 'topic_title' => stripslashes( $topic_title ), 'tags' => $topic_tags ) ) ) 427 return false; 428 429 if ( !$post = bb_get_first_post( $topic_id ) ) 430 return false; 431 432 // Update the first post. 433 if ( !$post = bp_forums_insert_post( array( 'post_id' => $post->post_id, 'topic_id' => $topic_id, 'post_text' => $topic_text, 'post_time' => $post->post_time, 'poster_id' => $post->poster_id, 'poster_ip' => $post->poster_ip, 'post_status' => $post->post_status, 'post_position' => $post->post_position ) ) ) 434 return false; 435 436 return bp_forums_get_topic_details( $topic_id ); 437 } 438 439 /** 440 * Set a topic as sticky/unsticky. 441 * 442 * @since 1.1.0 443 * 444 * @param string $args Array of arguments for sticky topic. 445 * @return bool 446 */ 447 function bp_forums_sticky_topic( $args = '' ) { 448 449 /** This action is documented in bp-forums/bp-forums-screens.php */ 450 do_action( 'bbpress_init' ); 451 452 $r = wp_parse_args( $args, array( 453 'topic_id' => false, 454 'mode' => 'stick' // Stick/unstick. 455 ) ); 456 extract( $r, EXTR_SKIP ); 457 458 if ( 'stick' == $mode ) 459 return bb_stick_topic( $topic_id ); 460 else if ( 'unstick' == $mode ) 461 return bb_unstick_topic( $topic_id ); 462 463 return false; 464 } 465 466 /** 467 * Set a topic's open/closed status. 468 * 469 * @since 1.1.0 470 * 471 * @param array|string $args { 472 * @type int $topic_id ID of the topic whose status is being changed. 473 * @type string $mode New status of the topic. 'open' or 'close'. 474 * Default: 'close'. 475 * } 476 * @return bool True on success, false on failure. 477 */ 478 function bp_forums_openclose_topic( $args = '' ) { 479 480 /** This action is documented in bp-forums/bp-forums-screens.php */ 481 do_action( 'bbpress_init' ); 482 483 $r = wp_parse_args( $args, array( 484 'topic_id' => false, 485 'mode' => 'close' // Stick/unstick. 486 ) ); 487 extract( $r, EXTR_SKIP ); 488 489 if ( 'close' == $mode ) 490 return bb_close_topic( $topic_id ); 491 else if ( 'open' == $mode ) 492 return bb_open_topic( $topic_id ); 493 494 return false; 495 } 496 497 /** 498 * Delete a topic. 499 * 500 * @since 1.1.0 501 * 502 * @param array|string $args { 503 * @type int $topic_id ID of the topic being deleted. 504 * } 505 * @return bool True on success, false on failure. 506 */ 507 function bp_forums_delete_topic( $args = '' ) { 508 509 /** This action is documented in bp-forums/bp-forums-screens.php */ 510 do_action( 'bbpress_init' ); 511 512 $r = wp_parse_args( $args, array( 513 'topic_id' => false 514 ) ); 515 extract( $r, EXTR_SKIP ); 516 517 return bb_delete_topic( $topic_id, 1 ); 518 } 519 520 /** 521 * Get a count of the total topics on the site. 522 * 523 * @since 1.2.0 524 * 525 * @return int $count Total topic count. 526 */ 527 function bp_forums_total_topic_count() { 528 global $bbdb; 529 530 /** This action is documented in bp-forums/bp-forums-screens.php */ 531 do_action( 'bbpress_init' ); 532 533 if ( isset( $bbdb ) ) { 534 if ( bp_is_active( 'groups' ) ) { 535 $groups_table_sql = groups_add_forum_tables_sql(); 536 $groups_where_sql = groups_add_forum_where_sql( "t.topic_status = 0" ); 537 } else { 538 $groups_table_sql = ''; 539 $groups_where_sql = "t.topic_status = 0"; 540 } 541 $count = $bbdb->get_results( "SELECT t.topic_id FROM {$bbdb->topics} AS t {$groups_table_sql} WHERE {$groups_where_sql}" ); 542 $count = count( (array) $count ); 543 } else { 544 $count = 0; 545 } 546 547 /** 548 * Filters the total topic count for the site. 549 * 550 * @since 1.5.0 551 * 552 * @param int $count Total topic count. 553 */ 554 return apply_filters( 'bp_forums_total_topic_count', $count ); 555 } 556 557 /** 558 * Check to see whether a user has already left this particular reply on a given post. 559 * 560 * Used to prevent dupes. 561 * 562 * @since 1.6.0 563 * 564 * @param string $text The text of the comment. 565 * @param int $topic_id The topic id. 566 * @param int $user_id The user id. 567 * @return bool True if a duplicate reply exists, otherwise false. 568 */ 569 function bp_forums_reply_exists( $text = '', $topic_id = 0, $user_id = 0 ) { 570 571 $reply_exists = false; 572 573 if ( $text && $topic_id && $user_id ) { 574 575 /** This action is documented in bp-forums/bp-forums-screens.php */ 576 do_action( 'bbpress_init' ); 577 578 $args = array( 579 'post_author_id' => $user_id, 580 'topic_id' => $topic_id 581 ); 582 583 // Set the reply_exists_text so we can check it in the filter below. 584 buddypress()->forums->reply_exists_text = $text; 585 586 // BB_Query's post_text parameter does a MATCH, while we need exact matches. 587 add_filter( 'get_posts_where', '_bp_forums_reply_exists_posts_where' ); 588 $query = new BB_Query( 'post', $args ); 589 remove_filter( 'get_posts_where', '_bp_forums_reply_exists_posts_where' ); 590 591 // Cleanup. 592 unset( buddypress()->forums->reply_exists_text ); 593 594 $reply_exists = (bool) !empty( $query->results ); 595 } 596 597 /** 598 * Filters whether a user has already left this particular reply on a given post. 599 * 600 * @since 1.6.0 601 * 602 * @param bool $reply_exists Whether or not a reply exists. 603 * @param string $text The text of the comment. 604 * @param int $topic_id The topic ID. 605 * @param int $user_id The user ID. 606 */ 607 return (bool) apply_filters( 'bp_forums_reply_exists', $reply_exists, $text, $topic_id, $user_id ); 608 } 609 /** 610 * Private one-time-use function used in conjunction with bp_forums_reply_exists(). 611 * 612 * @access private 613 * @since 1.7.0 614 * 615 * @global WPDB $wpdb WordPress database access object. 616 * 617 * @param string $where SQL fragment. 618 * @return string SQL fragment. 619 */ 620 function _bp_forums_reply_exists_posts_where( $where = '' ) { 621 return $where . " AND p.post_text = '" . buddypress()->forums->reply_exists_text . "'"; 622 } 623 624 /** 625 * Get a total "Topics Started" count for a given user. 626 * 627 * @since 1.2.0 628 * 629 * @param int $user_id ID of the user being queried. Falls back on displayed 630 * user, then loggedin. 631 * @param string $type The current filter/sort type. 'active', 'popular', 632 * 'unreplied'. 633 * @return int $count The topic count. 634 */ 635 function bp_forums_total_topic_count_for_user( $user_id = 0, $type = 'active' ) { 636 637 /** This action is documented in bp-forums/bp-forums-screens.php */ 638 do_action( 'bbpress_init' ); 639 640 if ( !$user_id ) 641 $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id(); 642 643 if ( class_exists( 'BB_Query' ) ) { 644 $args = array( 645 'topic_author_id' => $user_id, 646 'page' => 1, 647 'per_page' => -1, 648 'count' => true 649 ); 650 651 if ( 'unreplied' == $type ) 652 $args['post_count'] = 1; 653 654 $query = new BB_Query( 'topic', $args ); 655 $count = $query->count; 656 $query = null; 657 } else { 658 $count = 0; 659 } 660 661 return $count; 662 } 663 664 /** 665 * Return the total number of topics replied to by a given user. 666 * 667 * Uses an unfortunate technique to count unique topics, due to limitations in 668 * BB_Query. 669 * 670 * @since 1.5.0 671 * 672 * @param int $user_id ID of the user whose replied topics are being counted. 673 * Defaults to displayed user, then to logged-in user. 674 * @param string $type Forum thread type. 675 * @return int $count Topic count. 676 */ 677 function bp_forums_total_replied_count_for_user( $user_id = 0, $type = 'active' ) { 678 679 /** This action is documented in bp-forums/bp-forums-screens.php */ 680 do_action( 'bbpress_init' ); 681 682 if ( !$user_id ) 683 $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id(); 684 685 if ( !$user_id ) 686 return 0; 687 688 if ( class_exists( 'BB_Query' ) ) { 689 $query = new BB_Query( 'post', array( 'post_author_id' => $user_id, 'page' => 1, 'per_page' => -1, 'count' => true ) ); 690 691 // Count the unique topics. No better way to do this in the bbPress query API. 692 $topics = array(); 693 foreach( $query->results as $result ) { 694 if ( !in_array( $result->topic_id, $topics ) ) 695 $topics[] = $result->topic_id; 696 } 697 698 // Even more unfortunate. If this is filtered by 'unreplied', we have to requery. 699 if ( 'unreplied' == $type ) { 700 $topic_ids = implode( ',', $topics ); 701 $topics_query = new BB_Query( 'topic', array( 'topic_id' => $topic_ids, 'page' => 1, 'per_page' => -1, 'post_count' => 1 ) ); 702 $count = count( $topics_query->results ); 703 } else { 704 $count = count( $topics ); 705 } 706 $query = null; 707 } else { 708 $count = 0; 709 } 710 711 /** 712 * Filters the total number of topics replied to by a given user. 713 * 714 * @since 1.5.0 715 * 716 * @param int $count Total number of topics replied to by a given user. 717 * @param int $user_id The user ID. 718 */ 719 return apply_filters( 'bp_forums_total_replied_count_for_user', $count, $user_id ); 720 } 721 722 /** 723 * Fetch BP-specific details for an array of topics. 724 * 725 * Done in one fell swoop to reduce query overhead. Currently determines the 726 * following: 727 * - details about the last poster 728 * - information about topic users that may have been deleted/spammed 729 * 730 * @since 1.2.0 731 * 732 * @param array $topics Array of topics. 733 * @return array $topics Topics with BP details added. 734 */ 735 function bp_forums_get_topic_extras( $topics ) { 736 global $wpdb, $bbdb; 737 738 if ( empty( $topics ) ) 739 return $topics; 740 741 $bp = buddypress(); 742 743 // Get the topic ids. 744 foreach ( (array) $topics as $topic ) $topic_ids[] = $topic->topic_id; 745 $topic_ids = implode( ',', wp_parse_id_list( $topic_ids ) ); 746 747 // Fetch the topic's last poster details. 748 $poster_details = $wpdb->get_results( "SELECT t.topic_id, t.topic_last_poster, u.user_login, u.user_nicename, u.user_email, u.display_name FROM {$wpdb->users} u, {$bbdb->topics} t WHERE u.ID = t.topic_last_poster AND t.topic_id IN ( {$topic_ids} )" ); 749 for ( $i = 0, $count = count( $topics ); $i < $count; ++$i ) { 750 foreach ( (array) $poster_details as $poster ) { 751 if ( $poster->topic_id == $topics[$i]->topic_id ) { 752 $topics[$i]->topic_last_poster_email = $poster->user_email; 753 $topics[$i]->topic_last_poster_nicename = $poster->user_nicename; 754 $topics[$i]->topic_last_poster_login = $poster->user_login; 755 $topics[$i]->topic_last_poster_displayname = $poster->display_name; 756 } 757 } 758 } 759 760 // Fetch fullname for the topic's last poster. 761 if ( bp_is_active( 'xprofile' ) ) { 762 $poster_names = $wpdb->get_results( "SELECT t.topic_id, pd.value FROM {$bp->profile->table_name_data} pd, {$bbdb->topics} t WHERE pd.user_id = t.topic_last_poster AND pd.field_id = 1 AND t.topic_id IN ( {$topic_ids} )" ); 763 for ( $i = 0, $count = count( $topics ); $i < $count; ++$i ) { 764 foreach ( (array) $poster_names as $name ) { 765 if ( $name->topic_id == $topics[$i]->topic_id ) 766 $topics[$i]->topic_last_poster_displayname = $name->value; 767 } 768 } 769 } 770 771 // Loop through to make sure that each topic has the proper values set. This covers the 772 // case of deleted users. 773 foreach ( (array) $topics as $key => $topic ) { 774 if ( !isset( $topic->topic_last_poster_email ) ) 775 $topics[$key]->topic_last_poster_email = ''; 776 777 if ( !isset( $topic->topic_last_poster_nicename ) ) 778 $topics[$key]->topic_last_poster_nicename = ''; 779 780 if ( !isset( $topic->topic_last_poster_login ) ) 781 $topics[$key]->topic_last_poster_login = ''; 782 783 if ( !isset( $topic->topic_last_poster_displayname ) ) 784 $topics[$key]->topic_last_poster_displayname = ''; 785 } 786 787 return $topics; 788 } 789 790 /** Post Functions ************************************************************/ 791 792 /** 793 * Get the posts belonging to a topic. 794 * 795 * @since 1.1.0 796 * 797 * @param array|string $args { 798 * @type int $topic_id ID of the topic for which posts are being fetched. 799 * @type int $page Optional. Page of results to return. Default: 1. 800 * @type int $page Optional. Number of results to return per page. 801 * Default: 15. 802 * @type string $order 'ASC' or 'DESC'. Default: 'ASC'. 803 * } 804 * @return array List of posts. 805 */ 806 function bp_forums_get_topic_posts( $args = '' ) { 807 808 /** This action is documented in bp-forums/bp-forums-screens.php */ 809 do_action( 'bbpress_init' ); 810 811 $defaults = array( 812 'topic_id' => false, 813 'page' => 1, 814 'per_page' => 15, 815 'order' => 'ASC' 816 ); 817 818 $args = wp_parse_args( $args, $defaults ); 819 $query = new BB_Query( 'post', $args, 'get_thread' ); 820 821 return bp_forums_get_post_extras( $query->results ); 822 } 823 824 /** 825 * Get a single post object by ID. 826 * 827 * Wrapper for {@link bb_get_post()}. 828 * 829 * @since 1.0.0 830 * 831 * @param int $post_id ID of the post being fetched. 832 * @return object Post object. 833 */ 834 function bp_forums_get_post( $post_id ) { 835 836 /** This action is documented in bp-forums/bp-forums-screens.php */ 837 do_action( 'bbpress_init' ); 838 return bb_get_post( $post_id ); 839 } 840 841 /** 842 * Delete a post. 843 * 844 * Wrapper for {@link bb_delete_post()}. 845 * 846 * @since 1.1.0 847 * 848 * @param array|string $args { 849 * @type int $post_id ID of the post being deleted. 850 * } 851 * @return bool True on success, false on failure. 852 */ 853 function bp_forums_delete_post( $args = '' ) { 854 855 /** This action is documented in bp-forums/bp-forums-screens.php */ 856 do_action( 'bbpress_init' ); 857 858 $r = wp_parse_args( $args, array( 859 'post_id' => false 860 ) ); 861 862 extract( $r, EXTR_SKIP ); 863 864 return bb_delete_post( $post_id, 1 ); 865 } 866 867 /** 868 * Create a new post. 869 * 870 * @since 1.1.0 871 * 872 * @param array|string $args { 873 * @type int $post_id Optional. ID of an existing post, if you want to 874 * update rather than create. Default: false. 875 * @type int $topic_id ID of the topic to which the post belongs. 876 * @type string $post_text Contents of the post. 877 * @type string $post_time Optional. Time when the post was recorded. 878 * Default: current time, as reported by {@link bp_core_current_time()}. 879 * @type int $poster_id Optional. ID of the user creating the post. 880 * Default: ID of the logged-in user. 881 * @type string $poster_ip Optional. IP address of the user creating the 882 * post. Default: the IP address found in $_SERVER['REMOTE_ADDR']. 883 * @type int $post_status Post status. Default: 0. 884 * @type int $post_position Optional. Default: false (auto). 885 * } 886 * @return int|bool ID of the new post on success, false on failure. 887 */ 888 function bp_forums_insert_post( $args = '' ) { 889 890 /** This action is documented in bp-forums/bp-forums-screens.php */ 891 do_action( 'bbpress_init' ); 892 893 $defaults = array( 894 'post_id' => false, 895 'topic_id' => false, 896 'post_text' => '', 897 'post_time' => bp_core_current_time(), 898 'poster_id' => bp_loggedin_user_id(), // Accepts ids or names. 899 'poster_ip' => $_SERVER['REMOTE_ADDR'], 900 'post_status' => 0, // Use bb_delete_post() instead. 901 'post_position' => false 902 ); 903 904 $r = wp_parse_args( $args, $defaults ); 905 extract( $r, EXTR_SKIP ); 906 907 if ( !$post = bp_forums_get_post( $post_id ) ) 908 $post_id = false; 909 910 if ( !isset( $topic_id ) ) 911 $topic_id = $post->topic_id; 912 913 if ( empty( $post_text ) ) 914 $post_text = $post->post_text; 915 916 if ( !isset( $post_time ) ) 917 $post_time = $post->post_time; 918 919 if ( !isset( $post_position ) ) 920 $post_position = $post->post_position; 921 922 if ( empty( $poster_id ) ) 923 return false; 924 925 if ( bp_is_user_inactive( bp_loggedin_user_id() ) ) 926 return false; 927 928 $post_id = bb_insert_post( array( 'post_id' => $post_id, 'topic_id' => $topic_id, 'post_text' => stripslashes( trim( $post_text ) ), 'post_time' => $post_time, 'poster_id' => $poster_id, 'poster_ip' => $poster_ip, 'post_status' => $post_status, 'post_position' => $post_position ) ); 929 930 if ( !empty( $post_id ) ) { 931 932 /** 933 * Fires if there was a new post created. 934 * 935 * @since 1.0.0 936 * 937 * @param int $post_id ID of the newly created forum post. 938 */ 939 do_action( 'bp_forums_new_post', $post_id ); 940 } 941 942 return $post_id; 943 } 944 945 /** 946 * Get BP-specific details about a set of posts. 947 * 948 * Currently fetches the following: 949 * - WP userdata for each poster 950 * - BP fullname for each poster 951 * 952 * @since 1.2.0 953 * 954 * @param array $posts List of posts. 955 * @return array Posts with BP-data added. 956 */ 957 function bp_forums_get_post_extras( $posts ) { 958 global $wpdb; 959 960 if ( empty( $posts ) ) 961 return $posts; 962 963 $bp = buddypress(); 964 965 // Get the user ids. 966 foreach ( (array) $posts as $post ) $user_ids[] = $post->poster_id; 967 $user_ids = implode( ',', wp_parse_id_list( $user_ids ) ); 968 969 // Fetch the poster's user_email, user_nicename and user_login. 970 $poster_details = $wpdb->get_results( "SELECT u.ID as user_id, u.user_login, u.user_nicename, u.user_email, u.display_name FROM {$wpdb->users} u WHERE u.ID IN ( {$user_ids} )" ); 971 972 for ( $i = 0, $count = count( $posts ); $i < $count; ++$i ) { 973 foreach ( (array) $poster_details as $poster ) { 974 if ( $poster->user_id == $posts[$i]->poster_id ) { 975 $posts[$i]->poster_email = $poster->user_email; 976 $posts[$i]->poster_login = $poster->user_login; 977 $posts[$i]->poster_nicename = $poster->user_nicename; 978 $posts[$i]->poster_name = $poster->display_name; 979 } 980 } 981 } 982 983 // Fetch fullname for each poster. 984 if ( bp_is_active( 'xprofile' ) ) { 985 $poster_names = $wpdb->get_results( "SELECT pd.user_id, pd.value FROM {$bp->profile->table_name_data} pd WHERE pd.user_id IN ( {$user_ids} )" ); 986 for ( $i = 0, $count = count( $posts ); $i < $count; ++$i ) { 987 foreach ( (array) $poster_names as $name ) { 988 if ( isset( $topics[$i] ) && $name->user_id == $topics[$i]->user_id ) 989 $posts[$i]->poster_name = $poster->value; 990 } 991 } 992 } 993 994 /** 995 * Filters BP-specific details about a set of posts. 996 * 997 * @since 1.5.0 998 * 999 * @param array $posts Array of posts holding BP-specific details. 1000 */ 1001 return apply_filters( 'bp_forums_get_post_extras', $posts ); 1002 } 1003 1004 /** 1005 * Get topic and post counts for a given forum. 1006 * 1007 * @since 1.1.0 1008 * 1009 * @param int $forum_id ID of the forum. 1010 * @return object Object with properties $topics (topic count) and $posts 1011 * (post count). 1012 */ 1013 function bp_forums_get_forum_topicpost_count( $forum_id ) { 1014 global $wpdb, $bbdb; 1015 1016 /** This action is documented in bp-forums/bp-forums-screens.php */ 1017 do_action( 'bbpress_init' ); 1018 1019 // Need to find a bbPress function that does this. 1020 return $wpdb->get_results( $wpdb->prepare( "SELECT topics, posts from {$bbdb->forums} WHERE forum_id = %d", $forum_id ) ); 1021 } 1022 1023 /** 1024 * Map WordPress caps onto bbPress users, to ensure that they can post. 1025 * 1026 * @since 1.1.0 1027 * 1028 * @param array $allcaps Array of capabilities. 1029 * @return array Caps array with bbPress caps added. 1030 */ 1031 function bp_forums_filter_caps( $allcaps ) { 1032 global $wp_roles, $bb_table_prefix; 1033 1034 if ( !bp_loggedin_user_id() ) 1035 return $allcaps; 1036 1037 $bb_cap = bp_get_user_meta( bp_loggedin_user_id(), $bb_table_prefix . 'capabilities', true ); 1038 1039 if ( empty( $bb_cap ) ) 1040 return $allcaps; 1041 1042 $bb_cap = array_keys($bb_cap); 1043 $bb_cap = $wp_roles->get_role( $bb_cap[0] ); 1044 $bb_cap = $bb_cap->capabilities; 1045 1046 return array_merge( (array) $allcaps, (array) $bb_cap ); 1047 } 1048 add_filter( 'user_has_cap', 'bp_forums_filter_caps' ); 1049 1050 /** 1051 * Return the parent forum ID for the bbPress abstraction layer. 1052 * 1053 * @since 1.5.0 1054 * 1055 * @return int Forum ID. 1056 */ 1057 function bp_forums_parent_forum_id() { 1058 1059 /** 1060 * Filters the parent forum ID for the bbPress abstraction layer. 1061 * 1062 * @since 1.5.0 1063 * 1064 * @param int BP_FORUMS_PARENT_FORUM_ID The Parent forum ID constant. 1065 */ 1066 return apply_filters( 'bp_forums_parent_forum_id', BP_FORUMS_PARENT_FORUM_ID ); 1067 } 1068 1069 /** 1070 * Should sticky topics be broken out of regular topic order on forum directories? 1071 * 1072 * Defaults to false. Define BP_FORUMS_ENABLE_GLOBAL_DIRECTORY_STICKIES, or 1073 * filter 'bp_forums_enable_global_directory_stickies', to change this behavior. 1074 * 1075 * @since 1.5.0 1076 * 1077 * @return bool True if stickies should be displayed at the top of the global 1078 * directory, otherwise false. 1079 */ 1080 function bp_forums_enable_global_directory_stickies() { 1081 1082 /** 1083 * Filters whether or not sticky topics should be broken out of regular topic order. 1084 * 1085 * @since 1.5.0 1086 * 1087 * @param bool $value Whether or not to break out of topic order. 1088 */ 1089 return apply_filters( 'bp_forums_enable_global_directory_stickies', defined( 'BP_FORUMS_ENABLE_GLOBAL_DIRECTORY_STICKIES' ) && BP_FORUMS_ENABLE_GLOBAL_DIRECTORY_STICKIES ); 1090 } 1091 1092 1093 /** Caching ******************************************************************/ 1094 1095 /** 1096 * Caching functions handle the clearing of cached objects and pages on specific 1097 * actions throughout BuddyPress. 1098 */ 1099 1100 // List actions to clear super cached pages on, if super cache is installed. 1101 add_action( 'bp_forums_new_forum', 'bp_core_clear_cache' ); 1102 add_action( 'bp_forums_new_topic', 'bp_core_clear_cache' ); 1103 add_action( 'bp_forums_new_post', 'bp_core_clear_cache' ); 1104 1105 1106 /** Embeds *******************************************************************/ 1107 1108 /** 1109 * Attempt to retrieve the oEmbed cache for a forum topic. 1110 * 1111 * Grabs the topic post ID and attempts to retrieve the oEmbed cache (if it exists) 1112 * during the forum topic loop. If no cache and link is embeddable, cache it. 1113 * 1114 * @since 1.5.0 1115 * 1116 * @see BP_Embed 1117 * @see bp_embed_forum_cache() 1118 * @see bp_embed_forum_save_cache() 1119 */ 1120 function bp_forums_embed() { 1121 add_filter( 'embed_post_id', 'bp_get_the_topic_post_id' ); 1122 add_filter( 'bp_embed_get_cache', 'bp_embed_forum_cache', 10, 3 ); 1123 add_action( 'bp_embed_update_cache', 'bp_embed_forum_save_cache', 10, 3 ); 1124 } 1125 add_action( 'topic_loop_start', 'bp_forums_embed' ); 1126 1127 /** 1128 * Used during {@link BP_Embed::parse_oembed()} via {@link bp_forums_embed()}. 1129 * 1130 * Wrapper function for {@link bb_get_postmeta()}. 1131 * 1132 * @since 1.5.0 1133 * 1134 * @param object $cache Cache object. 1135 * @param int $id ID of the forum being cached. 1136 * @param string $cachekey Key to use with forum embed cache. 1137 */ 1138 function bp_embed_forum_cache( $cache, $id, $cachekey ) { 1139 return bb_get_postmeta( $id, $cachekey ); 1140 } 1141 1142 /** 1143 * Used during {@link BP_Embed::parse_oembed()} via {@link bp_forums_embed()}. 1144 * 1145 * Wrapper function for {@link bb_update_postmeta()}. 1146 * 1147 * @since 1.5.0 1148 * 1149 * @param object $cache Cache object. 1150 * @param string $cachekey Key to use with forum embed cache. 1151 * @param int $id ID of the forum being cached. 1152 */ 1153 function bp_embed_forum_save_cache( $cache, $cachekey, $id ) { 1154 bb_update_postmeta( $id, $cachekey, $cache ); 1155 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Dec 7 01:01:35 2017 | Cross-referenced by PHPXref 0.7.1 |