| [ Root ] [ Search ] [ Index ] |
PHP Cross Reference of bbPress TrunkProvided by Yoast |
[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 ( bb_is_user_logged_in() || bb_is_login_required() ) { 179 if ( !$user = bb_get_user( $topic_poster ) ) 180 if ( !$user = bb_get_user( $topic_poster_name, array( 'by' => 'login' ) ) ) 181 return false; 182 $topic_poster = $user->ID; 183 $topic_poster_name = $user->user_login; 184 185 if ( !$last_user = bb_get_user( $topic_last_poster ) ) 186 if ( !$last_user = bb_get_user( $topic_last_poster_name, array( 'by' => 'login' ) ) ) 187 return false; 188 $topic_last_poster = $last_user->ID; 189 $topic_last_poster_name = $last_user->user_login; 190 } 191 192 if ( in_array( 'topic_title', $fields ) ) { 193 $topic_title = apply_filters( 'pre_topic_title', $topic_title, $topic_id ); 194 if ( strlen($topic_title) < 1 ) 195 return false; 196 } 197 198 if ( in_array( 'topic_slug', $fields ) ) { 199 $slug_sql = $update ? 200 "SELECT topic_slug FROM $bbdb->topics WHERE topic_slug = %s AND topic_id != %d" : 201 "SELECT topic_slug FROM $bbdb->topics WHERE topic_slug = %s"; 202 203 $topic_slug = $_topic_slug = bb_slug_sanitize( $topic_slug ? $topic_slug : wp_specialchars_decode( $topic_title, ENT_QUOTES ) ); 204 if ( strlen( $_topic_slug ) < 1 ) 205 $topic_slug = $_topic_slug = '0'; 206 207 while ( is_numeric($topic_slug) || $existing_slug = $bbdb->get_var( $bbdb->prepare( $slug_sql, $topic_slug, $topic_id ) ) ) 208 $topic_slug = bb_slug_increment( $_topic_slug, $existing_slug ); 209 } 210 211 if ( $update ) { 212 $bbdb->update( $bbdb->topics, compact( $fields ), compact( 'topic_id' ) ); 213 wp_cache_delete( $topic_id, 'bb_topic' ); 214 if ( in_array( 'topic_slug', $fields ) ) 215 wp_cache_delete( $topic->topic_slug, 'bb_topic_slug' ); 216 wp_cache_flush( 'bb_query' ); 217 wp_cache_flush( 'bb_cache_posts_post_ids' ); 218 do_action( 'bb_update_topic', $topic_id ); 219 } else { 220 $bbdb->insert( $bbdb->topics, compact( $fields ) ); 221 $topic_id = $bbdb->insert_id; 222 $bbdb->query( $bbdb->prepare( "UPDATE $bbdb->forums SET topics = topics + 1 WHERE forum_id = %d", $forum_id ) ); 223 wp_cache_delete( $forum_id, 'bb_forum' ); 224 wp_cache_flush( 'bb_forums' ); 225 wp_cache_flush( 'bb_query' ); 226 wp_cache_flush( 'bb_cache_posts_post_ids' ); 227 do_action( 'bb_new_topic', $topic_id ); 228 } 229 230 if ( !empty( $tags ) ) 231 bb_add_topic_tags( $topic_id, $tags ); 232 233 do_action( 'bb_insert_topic', $topic_id, $args, compact( array_keys($args) ) ); // topic_id, what was passed, what was used 234 235 return $topic_id; 236 } 237 238 // Deprecated: expects $title to be pre-escaped 239 function bb_new_topic( $title, $forum, $tags = '', $args = '' ) { 240 $title = stripslashes( $title ); 241 $tags = stripslashes( $tags ); 242 $forum = (int) $forum; 243 return bb_insert_topic( wp_parse_args( $args ) + array( 'topic_title' => $title, 'forum_id' => $forum, 'tags' => $tags ) ); 244 } 245 246 // Deprecated: expects $title to be pre-escaped 247 function bb_update_topic( $title, $topic_id ) { 248 $title = stripslashes( $title ); 249 return bb_insert_topic( array( 'topic_title' => $title, 'topic_id' => $topic_id ) ); 250 } 251 252 function bb_delete_topic( $topic_id, $new_status = 0 ) { 253 global $bbdb; 254 $topic_id = (int) $topic_id; 255 add_filter( 'get_topic_where', 'bb_no_where' ); 256 if ( $topic = get_topic( $topic_id ) ) { 257 $new_status = (int) $new_status; 258 $old_status = (int) $topic->topic_status; 259 if ( $new_status == $old_status ) 260 return; 261 262 $thread_args = array( 'per_page' => -1, 'order' => 'DESC' ); 263 if ( 0 != $old_status && 0 == $new_status ) 264 $thread_args['post_status'] = 'all'; 265 $poster_ids = array(); 266 $posts = get_thread( $topic_id, $thread_args ); 267 if ( $posts && count( $posts ) ) { 268 foreach ( $posts as $post ) { 269 _bb_delete_post( $post->post_id, $new_status ); 270 $poster_ids[] = $post->poster_id; 271 } 272 } 273 274 if ( count( $poster_ids ) ) 275 foreach ( array_unique( $poster_ids ) as $id ) 276 if ( $user = bb_get_user( $id ) ) 277 bb_update_usermeta( $user->ID, $bbdb->prefix . 'topics_replied', ( $old_status ? $user->topics_replied + 1 : $user->topics_replied - 1 ) ); 278 279 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" ) ) 280 foreach ( $ids as $id ) 281 bb_remove_user_favorite( $id, $topic_id ); 282 283 switch ( $new_status ) { 284 case 0: // Undeleting 285 $bbdb->update( $bbdb->topics, array( 'topic_status' => $new_status ), compact( 'topic_id' ) ); 286 $topic_posts = (int) $bbdb->get_var( $bbdb->prepare( 287 "SELECT COUNT(*) FROM $bbdb->posts WHERE topic_id = %d AND post_status = 0", $topic_id 288 ) ); 289 $all_posts = (int) $bbdb->get_var( $bbdb->prepare( 290 "SELECT COUNT(*) FROM $bbdb->posts WHERE topic_id = %d", $topic_id 291 ) ); 292 bb_update_topicmeta( $topic_id, 'deleted_posts', $all_posts - $topic_posts ); 293 $bbdb->query( $bbdb->prepare( 294 "UPDATE $bbdb->forums SET topics = topics + 1, posts = posts + %d WHERE forum_id = %d", $topic_posts, $topic->forum_id 295 ) ); 296 $bbdb->update( $bbdb->topics, compact( 'topic_posts' ), compact( 'topic_id' ) ); 297 bb_topic_set_last_post( $topic_id ); 298 bb_update_post_positions( $topic_id ); 299 break; 300 301 default: // Other statuses (like Delete and Bozo) 302 bb_remove_topic_tags( $topic_id ); 303 $bbdb->update( $bbdb->topics, array( 'topic_status' => $new_status, 'tag_count' => 0 ), compact( 'topic_id' ) ); 304 $bbdb->query( $bbdb->prepare( 305 "UPDATE $bbdb->forums SET topics = topics - 1, posts = posts - %d WHERE forum_id = %d", $topic->topic_posts, $topic->forum_id 306 ) ); 307 break; 308 } 309 310 do_action( 'bb_delete_topic', $topic_id, $new_status, $old_status ); 311 wp_cache_delete( $topic_id, 'bb_topic' ); 312 wp_cache_delete( $topic->topic_slug, 'bb_topic_slug' ); 313 wp_cache_delete( $topic_id, 'bb_thread' ); 314 wp_cache_delete( $topic->forum_id, 'bb_forum' ); 315 wp_cache_flush( 'bb_forums' ); 316 wp_cache_flush( 'bb_query' ); 317 wp_cache_flush( 'bb_cache_posts_post_ids' ); 318 return $topic_id; 319 } else { 320 return false; 321 } 322 } 323 324 function bb_move_topic( $topic_id, $forum_id ) { 325 global $bbdb; 326 $topic = get_topic( $topic_id ); 327 $forum = bb_get_forum( $forum_id ); 328 $topic_id = (int) $topic->topic_id; 329 $forum_id = (int) $forum->forum_id; 330 331 if ( $topic && $forum && $topic->forum_id != $forum_id ) { 332 $bbdb->update( $bbdb->posts, compact( 'forum_id' ), compact( 'topic_id' ) ); 333 $bbdb->update( $bbdb->topics, compact( 'forum_id' ), compact( 'topic_id' ) ); 334 $bbdb->query( $bbdb->prepare( 335 "UPDATE $bbdb->forums SET topics = topics + 1, posts = posts + %d WHERE forum_id = %d", $topic->topic_posts, $forum_id 336 ) ); 337 $bbdb->query( $bbdb->prepare( 338 "UPDATE $bbdb->forums SET topics = topics - 1, posts = posts - %d WHERE forum_id = %d", $topic->topic_posts, $topic->forum_id 339 ) ); 340 wp_cache_flush( 'bb_post' ); 341 wp_cache_delete( $topic_id, 'bb_topic' ); 342 wp_cache_delete( $forum_id, 'bb_forum' ); 343 wp_cache_flush( 'bb_forums' ); 344 wp_cache_flush( 'bb_query' ); 345 wp_cache_flush( 'bb_cache_posts_post_ids' ); 346 347 do_action( 'bb_move_topic', $topic_id, $forum_id, $topic->forum_id ); 348 349 return $forum_id; 350 } 351 return false; 352 } 353 354 function bb_topic_set_last_post( $topic_id ) { 355 global $bbdb; 356 $topic_id = (int) $topic_id; 357 $old_post = $bbdb->get_row( $bbdb->prepare( 358 "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 359 ) ); 360 $old_poster = bb_get_user( $old_post->poster_id ); 361 wp_cache_delete( $topic_id, 'bb_topic' ); 362 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' ) ); 363 } 364 365 function bb_close_topic( $topic_id ) { 366 global $bbdb; 367 $topic_id = (int) $topic_id; 368 wp_cache_delete( $topic_id, 'bb_topic' ); 369 $r = $bbdb->update( $bbdb->topics, array( 'topic_open' => 0 ), compact( 'topic_id' ) ); 370 do_action('close_topic', $topic_id, $r); 371 return $r; 372 } 373 374 function bb_open_topic( $topic_id ) { 375 global $bbdb; 376 $topic_id = (int) $topic_id; 377 wp_cache_delete( $topic_id, 'bb_topic' ); 378 $r = $bbdb->update( $bbdb->topics, array( 'topic_open' => 1 ), compact( 'topic_id' ) ); 379 do_action('open_topic', $topic_id, $r); 380 return $r; 381 } 382 383 function bb_stick_topic( $topic_id, $super = 0 ) { 384 global $bbdb; 385 $topic_id = (int) $topic_id; 386 $stick = 1 + abs((int) $super); 387 wp_cache_delete( $topic_id, 'bb_topic' ); 388 $r = $bbdb->update( $bbdb->topics, array( 'topic_sticky' => $stick ), compact( 'topic_id' ) ); 389 do_action('stick_topic', $topic_id, $r); 390 return $r; 391 } 392 393 function bb_unstick_topic( $topic_id ) { 394 global $bbdb; 395 $topic_id = (int) $topic_id; 396 wp_cache_delete( $topic_id, 'bb_topic' ); 397 $r = $bbdb->update( $bbdb->topics, array( 'topic_sticky' => 0 ), compact( 'topic_id' ) ); 398 do_action('unstick_topic', $topic_id, $r); 399 return $r; 400 } 401 402 function topic_is_open( $topic_id = 0 ) { 403 $topic = get_topic( get_topic_id( $topic_id ) ); 404 return 1 == $topic->topic_open; 405 } 406 407 function topic_is_sticky( $topic_id = 0 ) { 408 $topic = get_topic( get_topic_id( $topic_id ) ); 409 return '0' !== $topic->topic_sticky; 410 } 411 412 function bb_update_topic_voices( $topic_id ) 413 { 414 if ( !$topic_id ) { 415 return; 416 } 417 418 $topic_id = abs( (int) $topic_id ); 419 420 global $bbdb; 421 if ( $voices = $bbdb->get_col( $bbdb->prepare( "SELECT DISTINCT poster_id FROM $bbdb->posts WHERE topic_id = %s AND post_status = '0';", $topic_id ) ) ) { 422 $voices = count( $voices ); 423 bb_update_topicmeta( $topic_id, 'voices_count', $voices ); 424 } 425 } 426 427 /* Thread */ 428 429 // Thread, topic? Guh-wah? 430 // A topic is the container, the thread is it's contents (the posts) 431 432 function get_thread( $topic_id, $args = null ) { 433 $defaults = array( 'page' => 1, 'order' => 'ASC' ); 434 if ( is_numeric( $args ) ) 435 $args = array( 'page' => $args ); 436 if ( @func_get_arg(2) ) 437 $defaults['order'] = 'DESC'; 438 439 $args = wp_parse_args( $args, $defaults ); 440 $args['topic_id'] = $topic_id; 441 442 $query = new BB_Query( 'post', $args, 'get_thread' ); 443 return $query->results; 444 } 445 446 // deprecated 447 function get_thread_post_ids( $topic_id ) { 448 $return = array( 'post' => array(), 'poster' => array() ); 449 foreach ( get_thread( $topic_id, array( 'per_page' => -1 ) ) as $post ) { 450 $return['post'][] = $post->post_id; 451 $return['poster'][] = $post->poster_id; 452 } 453 return $return; 454 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Nov 15 04:45:27 2010 | Cross-referenced by PHPXref 0.7 |