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