| [ Index ] |
PHP Cross Reference of bbPress |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* Topics */ 4 5 function get_topic( $id, $cache = true ) { 6 global $bbdb; 7 8 if ( !is_numeric($id) ) { 9 list($slug, $sql) = bb_get_sql_from_slug( 'topic', $id ); 10 $id = wp_cache_get( $slug, 'bb_topic_slug' ); 11 } 12 13 // not else 14 if ( is_numeric($id) ) { 15 $id = (int) $id; 16 $sql = "topic_id = $id"; 17 } 18 19 if ( 0 === $id || !$sql ) 20 return false; 21 22 // &= not =& 23 $cache &= 'AND topic_status = 0' == $where = apply_filters( 'get_topic_where', 'AND topic_status = 0' ); 24 25 if ( ( $cache || !$where ) && is_numeric($id) && false !== $topic = wp_cache_get( $id, 'bb_topic' ) ) 26 return $topic; 27 28 // $where is NOT bbdb:prepared 29 $topic = $bbdb->get_row( "SELECT * FROM $bbdb->topics WHERE $sql $where" ); 30 $topic = bb_append_meta( $topic, 'topic' ); 31 32 if ( $cache ) { 33 wp_cache_set( $topic->topic_id, $topic, 'bb_topic' ); 34 wp_cache_add( $topic->topic_slug, $topic->topic_id, 'bb_topic_slug' ); 35 } 36 37 return $topic; 38 } 39 40 function bb_get_topic_from_uri( $uri ) { 41 // Extract the topic id or slug of the uri 42 if ( 'topic' === bb_get_path(0, false, $uri) ) { 43 $topic_id_or_slug = bb_get_path(1, false, $uri); 44 } else { 45 if ($parsed_uri = parse_url($uri)) { 46 if (preg_match('@/topic\.php$@' ,$parsed_uri['path'])) { 47 $args = wp_parse_args($parsed_uri['query']); 48 if (isset($args['id'])) { 49 $topic_id_or_slug = $args['id']; 50 } 51 } 52 } 53 } 54 55 if ( !$topic_id_or_slug ) 56 return false; 57 58 return get_topic( $topic_id_or_slug ); 59 } 60 61 function get_latest_topics( $args = null ) { 62 $defaults = array( 'forum' => false, 'page' => 1, 'exclude' => false, 'number' => false ); 63 if ( is_numeric( $args ) ) 64 $args = array( 'forum' => $args ); 65 else 66 $args = wp_parse_args( $args ); // Make sure it's an array 67 if ( 1 < func_num_args() ) 68 $args['page'] = func_get_arg(1); 69 if ( 2 < func_num_args() ) 70 $args['exclude'] = func_get_arg(2); 71 72 $args = wp_parse_args( $args, $defaults ); 73 extract( $args, EXTR_SKIP ); 74 75 if ( $exclude ) { 76 $exclude = '-' . str_replace(',', ',-', $exclude); 77 $exclude = str_replace('--', '-', $exclude); 78 if ( $forum ) 79 $forum = (string) $forum . ",$exclude"; 80 else 81 $forum = $exclude; 82 } 83 84 $q = array( 85 'forum_id' => $forum, 86 'page' => $page, 87 'per_page' => $number, 88 'index_hint' => 'USE INDEX (`forum_time`)' 89 ); 90 91 if ( bb_is_front() ) 92 $q['sticky'] = '-2'; 93 elseif ( bb_is_forum() || bb_is_view() ) 94 $q['sticky'] = 0; 95 96 // Last param makes filters back compat 97 $query = new BB_Query( 'topic', $q, 'get_latest_topics' ); 98 return $query->results; 99 } 100 101 function get_sticky_topics( $forum = false, $display = 1 ) { 102 if ( 1 != $display ) // Why is this even here? 103 return false; 104 105 $q = array( 106 'forum_id' => $forum, 107 'sticky' => bb_is_front() ? 'super' : 'sticky' 108 ); 109 110 $query = new BB_Query( 'topic', $q, 'get_sticky_topics' ); 111 return $query->results; 112 } 113 114 function get_recent_user_threads( $user_id ) { 115 global $page; 116 $q = array( 'page' => $page, 'topic_author_id' => $user_id, 'order_by' => 't.topic_time'); 117 118 $query = new BB_Query( 'topic', $q, 'get_recent_user_threads' ); 119 120 return $query->results; 121 } 122 123 function bb_insert_topic( $args = null ) { 124 global $bbdb; 125 126 if ( !$args = wp_parse_args( $args ) ) 127 return false; 128 129 $fields = array_keys( $args ); 130 131 if ( isset($args['topic_id']) && false !== $args['topic_id'] ) { 132 $update = true; 133 if ( !$topic_id = (int) get_topic_id( $args['topic_id'] ) ) 134 return false; 135 // Get from db, not cache. Good idea? Prevents trying to update meta_key names in the topic table (get_topic() returns appended topic obj) 136 $topic = $bbdb->get_row( $bbdb->prepare( "SELECT * FROM $bbdb->topics WHERE topic_id = %d", $topic_id ) ); 137 $defaults = get_object_vars( $topic ); 138 unset($defaults['topic_id']); 139 140 // Only update the args we passed 141 $fields = array_intersect( $fields, array_keys($defaults) ); 142 if ( in_array( 'topic_poster', $fields ) ) 143 $fields[] = 'topic_poster_name'; 144 if ( in_array( 'topic_last_poster', $fields ) ) 145 $fields[] = 'topic_last_poster_name'; 146 } else { 147 $topic_id = false; 148 $update = false; 149 150 $now = bb_current_time('mysql'); 151 $current_user_id = bb_get_current_user_info( 'id' ); 152 153 $defaults = array( 154 'topic_title' => '', 155 'topic_slug' => '', 156 'topic_poster' => $current_user_id, // accepts ids 157 'topic_poster_name' => '', // accept names 158 'topic_last_poster' => $current_user_id, // accepts ids 159 'topic_last_poster_name' => '', // accept names 160 'topic_start_time' => $now, 161 'topic_time' => $now, 162 'topic_open' => 1, 163 'forum_id' => 0 // accepts ids or slugs 164 ); 165 166 // Insert all args 167 $fields = array_keys($defaults); 168 } 169 170 $defaults['tags'] = false; // accepts array or comma delimited string 171 extract( wp_parse_args( $args, $defaults ) ); 172 unset($defaults['tags']); 173 174 if ( !$forum = bb_get_forum( $forum_id ) ) 175 return false; 176 $forum_id = (int) $forum->forum_id; 177 178 if ( !$user = bb_get_user( $topic_poster ) ) 179 $user = bb_get_user( $topic_poster_name, array( 'by' => 'login' ) ); 180 181 if ( !empty( $user ) ) { 182 $topic_poster = $user->ID; 183 $topic_poster_name = $user->user_login; 184 } 185 186 if ( !$last_user = bb_get_user( $topic_last_poster ) ) 187 $last_user = bb_get_user( $topic_last_poster_name, array( 'by' => 'login' ) ); 188 189 if ( !empty( $last_user ) ) { 190 $topic_last_poster = $last_user->ID; 191 $topic_last_poster_name = $last_user->user_login; 192 } 193 194 if ( in_array( 'topic_title', $fields ) ) { 195 $topic_title = apply_filters( 'pre_topic_title', $topic_title, $topic_id ); 196 if ( strlen($topic_title) < 1 ) 197 return false; 198 } 199 200 if ( in_array( 'topic_slug', $fields ) ) { 201 $slug_sql = $update ? 202 "SELECT topic_slug FROM $bbdb->topics WHERE topic_slug = %s AND topic_id != %d" : 203 "SELECT topic_slug FROM $bbdb->topics WHERE topic_slug = %s"; 204 205 $topic_slug = $_topic_slug = bb_slug_sanitize( $topic_slug ? $topic_slug : wp_specialchars_decode( $topic_title, ENT_QUOTES ) ); 206 if ( strlen( $_topic_slug ) < 1 ) 207 $topic_slug = $_topic_slug = '0'; 208 209 while ( is_numeric($topic_slug) || $existing_slug = $bbdb->get_var( $bbdb->prepare( $slug_sql, $topic_slug, $topic_id ) ) ) 210 $topic_slug = bb_slug_increment( $_topic_slug, $existing_slug ); 211 } 212 213 if ( $update ) { 214 $bbdb->update( $bbdb->topics, compact( $fields ), compact( 'topic_id' ) ); 215 wp_cache_delete( $topic_id, 'bb_topic' ); 216 if ( in_array( 'topic_slug', $fields ) ) 217 wp_cache_delete( $topic->topic_slug, 'bb_topic_slug' ); 218 wp_cache_flush( 'bb_query' ); 219 wp_cache_flush( 'bb_cache_posts_post_ids' ); 220 do_action( 'bb_update_topic', $topic_id ); 221 } else { 222 $bbdb->insert( $bbdb->topics, compact( $fields ) ); 223 $topic_id = $bbdb->insert_id; 224 $bbdb->query( $bbdb->prepare( "UPDATE $bbdb->forums SET topics = topics + 1 WHERE forum_id = %d", $forum_id ) ); 225 wp_cache_delete( $forum_id, 'bb_forum' ); 226 wp_cache_flush( 'bb_forums' ); 227 wp_cache_flush( 'bb_query' ); 228 wp_cache_flush( 'bb_cache_posts_post_ids' ); 229 do_action( 'bb_new_topic', $topic_id ); 230 } 231 232 if ( !empty( $tags ) ) 233 bb_add_topic_tags( $topic_id, $tags ); 234 235 do_action( 'bb_insert_topic', $topic_id, $args, compact( array_keys($args) ) ); // topic_id, what was passed, what was used 236 237 return $topic_id; 238 } 239 240 // Deprecated: expects $title to be pre-escaped 241 function bb_new_topic( $title, $forum, $tags = '', $args = '' ) { 242 $title = stripslashes( $title ); 243 $tags = stripslashes( $tags ); 244 $forum = (int) $forum; 245 return bb_insert_topic( wp_parse_args( $args ) + array( 'topic_title' => $title, 'forum_id' => $forum, 'tags' => $tags ) ); 246 } 247 248 // Deprecated: expects $title to be pre-escaped 249 function bb_update_topic( $title, $topic_id ) { 250 $title = stripslashes( $title ); 251 return bb_insert_topic( array( 'topic_title' => $title, 'topic_id' => $topic_id ) ); 252 } 253 254 function bb_delete_topic( $topic_id, $new_status = 0 ) { 255 global $bbdb; 256 $topic_id = (int) $topic_id; 257 add_filter( 'get_topic_where', 'bb_no_where' ); 258 if ( $topic = get_topic( $topic_id ) ) { 259 $new_status = (int) $new_status; 260 $old_status = (int) $topic->topic_status; 261 if ( $new_status == $old_status ) 262 return; 263 264 $thread_args = array( 'per_page' => -1, 'order' => 'DESC' ); 265 if ( 0 != $old_status && 0 == $new_status ) 266 $thread_args['post_status'] = 'all'; 267 $poster_ids = array(); 268 $posts = get_thread( $topic_id, $thread_args ); 269 if ( $posts && count( $posts ) ) { 270 foreach ( $posts as $post ) { 271 _bb_delete_post( $post->post_id, $new_status ); 272 $poster_ids[] = $post->poster_id; 273 } 274 } 275 276 if ( count( $poster_ids ) ) { 277 foreach ( array_unique( $poster_ids ) as $id ) { 278 if ( $user = bb_get_user( $id ) ) { 279 $topics_replied_key = $bbdb->prefix . 'topics_replied'; 280 bb_update_usermeta( $user->ID, $topics_replied_key, ( $old_status ? $user->$topics_replied_key + 1 : $user->$topics_replied_key - 1 ) ); 281 } 282 } 283 } 284 285 if ( $ids = $bbdb->get_col( "SELECT user_id, meta_value FROM $bbdb->usermeta WHERE meta_key = 'favorites' and FIND_IN_SET('$topic_id', meta_value) > 0" ) ) 286 foreach ( $ids as $id ) 287 bb_remove_user_favorite( $id, $topic_id ); 288 289 switch ( $new_status ) { 290 case 0: // Undeleting 291 $bbdb->update( $bbdb->topics, array( 'topic_status' => $new_status ), compact( 'topic_id' ) ); 292 $topic_posts = (int) $bbdb->get_var( $bbdb->prepare( 293 "SELECT COUNT(*) FROM $bbdb->posts WHERE topic_id = %d AND post_status = 0", $topic_id 294 ) ); 295 $all_posts = (int) $bbdb->get_var( $bbdb->prepare( 296 "SELECT COUNT(*) FROM $bbdb->posts WHERE topic_id = %d", $topic_id 297 ) ); 298 bb_update_topicmeta( $topic_id, 'deleted_posts', $all_posts - $topic_posts ); 299 $bbdb->query( $bbdb->prepare( 300 "UPDATE $bbdb->forums SET topics = topics + 1, posts = posts + %d WHERE forum_id = %d", $topic_posts, $topic->forum_id 301 ) ); 302 $bbdb->update( $bbdb->topics, compact( 'topic_posts' ), compact( 'topic_id' ) ); 303 bb_topic_set_last_post( $topic_id ); 304 bb_update_post_positions( $topic_id ); 305 break; 306 307 default: // Other statuses (like Delete and Bozo) 308 bb_remove_topic_tags( $topic_id ); 309 $bbdb->update( $bbdb->topics, array( 'topic_status' => $new_status, 'tag_count' => 0 ), compact( 'topic_id' ) ); 310 $bbdb->query( $bbdb->prepare( 311 "UPDATE $bbdb->forums SET topics = topics - 1, posts = posts - %d WHERE forum_id = %d", $topic->topic_posts, $topic->forum_id 312 ) ); 313 break; 314 } 315 316 do_action( 'bb_delete_topic', $topic_id, $new_status, $old_status ); 317 wp_cache_delete( $topic_id, 'bb_topic' ); 318 wp_cache_delete( $topic->topic_slug, 'bb_topic_slug' ); 319 wp_cache_delete( $topic_id, 'bb_thread' ); 320 wp_cache_delete( $topic->forum_id, 'bb_forum' ); 321 wp_cache_flush( 'bb_forums' ); 322 wp_cache_flush( 'bb_query' ); 323 wp_cache_flush( 'bb_cache_posts_post_ids' ); 324 return $topic_id; 325 } else { 326 return false; 327 } 328 } 329 330 function bb_move_topic( $topic_id, $forum_id ) { 331 global $bbdb; 332 $topic = get_topic( $topic_id ); 333 $forum = bb_get_forum( $forum_id ); 334 $topic_id = (int) $topic->topic_id; 335 $forum_id = (int) $forum->forum_id; 336 337 if ( $topic && $forum && $topic->forum_id != $forum_id ) { 338 $bbdb->update( $bbdb->posts, compact( 'forum_id' ), compact( 'topic_id' ) ); 339 $bbdb->update( $bbdb->topics, compact( 'forum_id' ), compact( 'topic_id' ) ); 340 $bbdb->query( $bbdb->prepare( 341 "UPDATE $bbdb->forums SET topics = topics + 1, posts = posts + %d WHERE forum_id = %d", $topic->topic_posts, $forum_id 342 ) ); 343 $bbdb->query( $bbdb->prepare( 344 "UPDATE $bbdb->forums SET topics = topics - 1, posts = posts - %d WHERE forum_id = %d", $topic->topic_posts, $topic->forum_id 345 ) ); 346 wp_cache_flush( 'bb_post' ); 347 wp_cache_delete( $topic_id, 'bb_topic' ); 348 wp_cache_delete( $forum_id, 'bb_forum' ); 349 wp_cache_flush( 'bb_forums' ); 350 wp_cache_flush( 'bb_query' ); 351 wp_cache_flush( 'bb_cache_posts_post_ids' ); 352 353 do_action( 'bb_move_topic', $topic_id, $forum_id, $topic->forum_id ); 354 355 return $forum_id; 356 } 357 return false; 358 } 359 360 function bb_topic_set_last_post( $topic_id ) { 361 global $bbdb; 362 $topic_id = (int) $topic_id; 363 $old_post = $bbdb->get_row( $bbdb->prepare( 364 "SELECT post_id, poster_id, post_time FROM $bbdb->posts WHERE topic_id = %d AND post_status = 0 ORDER BY post_time DESC LIMIT 1", $topic_id 365 ) ); 366 $old_poster = bb_get_user( $old_post->poster_id ); 367 wp_cache_delete( $topic_id, 'bb_topic' ); 368 return $bbdb->update( $bbdb->topics, array( 'topic_time' => $old_post->post_time, 'topic_last_poster' => $old_post->poster_id, 'topic_last_poster_name' => $old_poster->login_name, 'topic_last_post_id' => $old_post->post_id ), compact( 'topic_id' ) ); 369 } 370 371 function bb_close_topic( $topic_id ) { 372 global $bbdb; 373 $topic_id = (int) $topic_id; 374 wp_cache_delete( $topic_id, 'bb_topic' ); 375 $r = $bbdb->update( $bbdb->topics, array( 'topic_open' => 0 ), compact( 'topic_id' ) ); 376 do_action('close_topic', $topic_id, $r); 377 return $r; 378 } 379 380 function bb_open_topic( $topic_id ) { 381 global $bbdb; 382 $topic_id = (int) $topic_id; 383 wp_cache_delete( $topic_id, 'bb_topic' ); 384 $r = $bbdb->update( $bbdb->topics, array( 'topic_open' => 1 ), compact( 'topic_id' ) ); 385 do_action('open_topic', $topic_id, $r); 386 return $r; 387 } 388 389 function bb_stick_topic( $topic_id, $super = 0 ) { 390 global $bbdb; 391 $topic_id = (int) $topic_id; 392 $stick = 1 + abs((int) $super); 393 wp_cache_delete( $topic_id, 'bb_topic' ); 394 $r = $bbdb->update( $bbdb->topics, array( 'topic_sticky' => $stick ), compact( 'topic_id' ) ); 395 do_action('stick_topic', $topic_id, $r); 396 return $r; 397 } 398 399 function bb_unstick_topic( $topic_id ) { 400 global $bbdb; 401 $topic_id = (int) $topic_id; 402 wp_cache_delete( $topic_id, 'bb_topic' ); 403 $r = $bbdb->update( $bbdb->topics, array( 'topic_sticky' => 0 ), compact( 'topic_id' ) ); 404 do_action('unstick_topic', $topic_id, $r); 405 return $r; 406 } 407 408 function topic_is_open( $topic_id = 0 ) { 409 $topic = get_topic( get_topic_id( $topic_id ) ); 410 return 1 == $topic->topic_open; 411 } 412 413 function topic_is_sticky( $topic_id = 0 ) { 414 $topic = get_topic( get_topic_id( $topic_id ) ); 415 return '0' !== $topic->topic_sticky; 416 } 417 418 function bb_update_topic_voices( $topic_id ) 419 { 420 if ( !$topic_id ) { 421 return; 422 } 423 424 $topic_id = abs( (int) $topic_id ); 425 426 global $bbdb; 427 if ( $voices = $bbdb->get_col( $bbdb->prepare( "SELECT DISTINCT poster_id FROM $bbdb->posts WHERE topic_id = %s AND post_status = '0';", $topic_id ) ) ) { 428 $voices = count( $voices ); 429 bb_update_topicmeta( $topic_id, 'voices_count', $voices ); 430 } 431 } 432 433 /* Thread */ 434 435 // Thread, topic? Guh-wah? 436 // A topic is the container, the thread is it's contents (the posts) 437 438 function get_thread( $topic_id, $args = null ) { 439 $defaults = array( 'page' => 1, 'order' => 'ASC' ); 440 if ( is_numeric( $args ) ) 441 $args = array( 'page' => $args ); 442 if ( @func_get_arg(2) ) 443 $defaults['order'] = 'DESC'; 444 445 $args = wp_parse_args( $args, $defaults ); 446 $args['topic_id'] = $topic_id; 447 448 $query = new BB_Query( 'post', $args, 'get_thread' ); 449 return $query->results; 450 } 451 452 // deprecated 453 function get_thread_post_ids( $topic_id ) { 454 $return = array( 'post' => array(), 'poster' => array() ); 455 foreach ( get_thread( $topic_id, array( 'per_page' => -1 ) ) as $post ) { 456 $return['post'][] = $post->post_id; 457 $return['poster'][] = $post->poster_id; 458 } 459 return $return; 460 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Tue May 21 03:58:37 2013 | Hosted by follow the white rabbit. |