| [ Root ] [ Search ] [ Index ] |
PHP Cross Reference of bbPress TrunkProvided by Yoast |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* Tags */ 4 5 /** 6 * bb_add_topic_tag() - Adds a single tag to a topic. 7 * 8 * @param int $topic_id 9 * @param string $tag The (unsanitized) full name of the tag to be added 10 * @return int|bool The TT_ID of the new bb_topic_tag or false on failure 11 */ 12 function bb_add_topic_tag( $topic_id, $tag ) { 13 $tt_ids = bb_add_topic_tags( $topic_id, $tag ); 14 if ( is_array( $tt_ids ) ) 15 return $tt_ids[0]; 16 return false; 17 } 18 19 /** 20 * bb_add_topic_tag() - Adds a multiple tags to a topic. 21 * 22 * @param int $topic_id 23 * @param array|string $tags The (unsanitized) full names of the tag to be added. CSV or array. 24 * @return array|bool The TT_IDs of the new bb_topic_tags or false on failure 25 */ 26 function bb_add_topic_tags( $topic_id, $tags ) { 27 global $wp_taxonomy_object; 28 $topic_id = (int) $topic_id; 29 if ( !$topic = get_topic( $topic_id ) ) 30 return false; 31 if ( !bb_current_user_can( 'add_tag_to', $topic_id ) ) 32 return false; 33 34 $user_id = bb_get_current_user_info( 'id' ); 35 36 $tags = apply_filters( 'bb_add_topic_tags', $tags, $topic_id ); 37 38 if ( !is_array( $tags ) ) 39 $tags = explode(',', (string) $tags); 40 41 $tt_ids = $wp_taxonomy_object->set_object_terms( $topic->topic_id, $tags, 'bb_topic_tag', array( 'append' => true, 'user_id' => $user_id ) ); 42 43 if ( is_array($tt_ids) ) { 44 global $bbdb; 45 $bbdb->query( $bbdb->prepare( 46 "UPDATE $bbdb->topics SET tag_count = tag_count + %d WHERE topic_id = %d", count( $tt_ids ), $topic->topic_id 47 ) ); 48 wp_cache_delete( $topic->topic_id, 'bb_topic' ); 49 foreach ( $tt_ids as $tt_id ) 50 do_action('bb_tag_added', $tt_id, $user_id, $topic_id); 51 return $tt_ids; 52 } 53 return false; 54 } 55 56 /** 57 * bb_create_tag() - Creates a single bb_topic_tag. 58 * 59 * @param string $tag The (unsanitized) full name of the tag to be created 60 * @return int|bool The TT_ID of the new bb_topic_tags or false on failure 61 */ 62 function bb_create_tag( $tag ) { 63 global $wp_taxonomy_object; 64 65 if ( list($term_id, $tt_id) = $wp_taxonomy_object->is_term( $tag, 'bb_topic_tag' ) ) 66 return $tt_id; 67 68 list($term_id, $tt_id) = $wp_taxonomy_object->insert_term( $tag, 'bb_topic_tag' ); 69 70 if ( is_wp_error($term_id) || is_wp_error($tt_id) || !$tt_id ) 71 return false; 72 73 return $tt_id; 74 } 75 76 /** 77 * bb_remove_topic_tag() - Removes a single bb_topic_tag by a user from a topic. 78 * 79 * @param int $tt_id The TT_ID of the bb_topic_tag to be removed 80 * @param int $user_id 81 * @param int $topic_id 82 * @return array|false The TT_IDs of the users bb_topic_tags on that topic or false on failure 83 */ 84 function bb_remove_topic_tag( $tt_id, $user_id, $topic_id ) { 85 global $wp_taxonomy_object; 86 $tt_id = (int) $tt_id; 87 $user_id = (int) $user_id; 88 $topic_id = (int) $topic_id; 89 if ( !$topic = get_topic( $topic_id ) ) 90 return false; 91 if ( !bb_current_user_can( 'edit_tag_by_on', $user_id, $topic_id ) ) 92 return false; 93 94 $_tag = bb_get_tag( $tt_id ); 95 96 do_action('bb_pre_tag_removed', $tt_id, $user_id, $topic_id); 97 $currents = $wp_taxonomy_object->get_object_terms( $topic_id, 'bb_topic_tag', array( 'user_id' => $user_id, 'fields' => 'all' ) ); 98 if ( !is_array( $currents ) ) 99 return false; 100 101 $found_tag_to_remove = false; 102 $current_tag_term_ids = array(); 103 foreach ( $currents as $current ) { 104 if ( $current->term_taxonomy_id == $tt_id ) { 105 $found_tag_to_remove = true; 106 continue; 107 } 108 $current_tag_term_ids[] = $current->term_id; 109 } 110 111 if ( !$found_tag_to_remove ) 112 return false; 113 114 $current_tag_term_ids = array_map( 'intval', $current_tag_term_ids ); 115 116 $tt_ids = $wp_taxonomy_object->set_object_terms( $topic_id, array_values($current_tag_term_ids), 'bb_topic_tag', array( 'user_id' => $user_id ) ); 117 if ( is_array( $tt_ids ) ) { 118 global $bbdb; 119 $bbdb->query( $bbdb->prepare( 120 "UPDATE $bbdb->topics SET tag_count = %d WHERE topic_id = %d", count( $tt_ids ), $topic_id 121 ) ); 122 wp_cache_delete( $topic_id, 'bb_topic' ); 123 124 // Count is updated at set_object_terms() 125 if ( $_tag && 2 > $_tag->tag_count ) { 126 bb_destroy_tag( $_tag->term_taxonomy_id ); 127 } 128 } elseif ( is_wp_error( $tt_ids ) ) { 129 return false; 130 } 131 return $tt_ids; 132 } 133 134 /** 135 * bb_remove_topic_tag() - Removes all bb_topic_tags from a topic. 136 * 137 * @param int $topic_id 138 * @return bool 139 */ 140 function bb_remove_topic_tags( $topic_id ) { 141 global $wp_taxonomy_object; 142 $topic_id = (int) $topic_id; 143 if ( !$topic_id || !get_topic( $topic_id ) ) 144 return false; 145 146 $_tags = bb_get_topic_tags( $topic_id ); 147 148 do_action( 'bb_pre_remove_topic_tags', $topic_id ); 149 150 $wp_taxonomy_object->delete_object_term_relationships( $topic_id, 'bb_topic_tag' ); 151 152 global $bbdb; 153 $bbdb->query( $bbdb->prepare( 154 "UPDATE $bbdb->topics SET tag_count = 0 WHERE topic_id = %d", $topic_id 155 ) ); 156 wp_cache_delete( $topic_id, 'bb_topic' ); 157 158 if ( $_tags ) { 159 foreach ( $_tags as $_tag ) { 160 // Count is updated at delete_object_term_relationships() 161 if ( 2 > $_tag->tag_count ) { 162 bb_destroy_tag( $_tag->term_taxonomy_id ); 163 } 164 } 165 } 166 167 return true; 168 } 169 170 /** 171 * bb_destroy_tag() - Completely removes a bb_topic_tag. 172 * 173 * @param int $tt_id The TT_ID of the tag to destroy 174 * @return bool 175 */ 176 function bb_destroy_tag( $tt_id, $recount_topics = true ) { 177 global $wp_taxonomy_object; 178 179 $tt_id = (int) $tt_id; 180 181 if ( !$tag = bb_get_tag( $tt_id ) ) 182 return false; 183 184 $topic_ids = bb_get_tagged_topic_ids( $tag->term_id ); 185 186 $return = $wp_taxonomy_object->delete_term( $tag->term_id, 'bb_topic_tag' ); 187 188 if ( is_wp_error($return) ) 189 return false; 190 191 if ( !is_wp_error( $topic_ids ) && is_array( $topic_ids ) ) { 192 global $bbdb; 193 $bbdb->query( 194 "UPDATE $bbdb->topics SET tag_count = tag_count - 1 WHERE topic_id IN (" . join( ',', $topic_ids ) . ")" 195 ); 196 foreach ( $topic_ids as $topic_id ) { 197 wp_cache_delete( $topic_id, 'bb_topic' ); 198 } 199 } 200 201 return $return; 202 } 203 204 /** 205 * bb_get_tag_id() - Returns the id of the specified or global tag. 206 * 207 * @param mixed $id The TT_ID, tag name of the desired tag, or 0 for the global tag 208 * @return int 209 */ 210 function bb_get_tag_id( $id = 0 ) { 211 global $tag; 212 if ( $id ) { 213 $_tag = bb_get_tag( $id ); 214 } else { 215 $_tag =& $tag; 216 } 217 return (int) $_tag->tag_id; 218 } 219 220 /** 221 * bb_get_tag() - Returns the specified tag. If $user_id and $topic_id are passed, will check to see if that tag exists on that topic by that user. 222 * 223 * @param mixed $id The TT_ID or tag name of the desired tag 224 * @param int $user_id (optional) 225 * @param int $topic_id (optional) 226 * @return object Term object (back-compat) 227 */ 228 function bb_get_tag( $id, $user_id = 0, $topic_id = 0 ) { 229 global $wp_taxonomy_object; 230 $user_id = (int) $user_id; 231 $topic_id = (int) $topic_id; 232 233 $term = false; 234 if ( is_integer( $id ) ) { 235 $tt_id = (int) $id; 236 } else { 237 if ( !$term = $wp_taxonomy_object->get_term_by( 'slug', $id, 'bb_topic_tag' ) ) 238 return false; 239 $tt_id = (int) $term->term_taxonomy_id; 240 } 241 242 if ( $user_id && $topic_id ) { 243 $args = array( 'user_id' => $user_id, 'fields' => 'tt_ids' ); 244 $cache_id = $topic_id . serialize( $args ); 245 246 $tt_ids = wp_cache_get( $cache_id, 'bb_topic_tag_terms' ); 247 if ( empty( $tt_ids ) ) { 248 $tt_ids = $wp_taxonomy_object->get_object_terms( $topic_id, 'bb_topic_tag', $args ); 249 wp_cache_set( $cache_id, $tt_ids, 'bb_topic_tag_terms' ); 250 } 251 if ( !in_array( $tt_id, $tt_ids ) ) 252 return false; 253 } 254 255 if ( !$term ) 256 $term = $wp_taxonomy_object->get_term_by( 'tt_id', $tt_id, 'bb_topic_tag' ); 257 258 _bb_make_tag_compat( $term ); 259 260 return $term; 261 } 262 263 /** 264 * bb_get_topic_tags() - Returns all of the bb_topic_tags associated with the specified topic. 265 * 266 * @param int $topic_id 267 * @param mixed $args 268 * @return array|false Term objects (back-compat), false on failure 269 */ 270 function bb_get_topic_tags( $topic_id = 0, $args = null ) { 271 global $wp_taxonomy_object; 272 273 if ( !$topic = get_topic( get_topic_id( $topic_id ) ) ) 274 return false; 275 276 $topic_id = (int) $topic->topic_id; 277 278 $cache_id = $topic_id . serialize( $args ); 279 280 $terms = wp_cache_get( $cache_id, 'bb_topic_tag_terms' ); 281 if ( false === $terms ) { 282 $terms = $wp_taxonomy_object->get_object_terms( (int) $topic->topic_id, 'bb_topic_tag', $args ); 283 wp_cache_set( $cache_id, $terms, 'bb_topic_tag_terms' ); 284 } 285 286 if ( is_wp_error( $terms ) ) 287 return false; 288 289 for ( $i = 0; isset($terms[$i]); $i++ ) 290 _bb_make_tag_compat( $terms[$i] ); 291 292 return $terms; 293 } 294 295 function bb_get_user_tags( $topic_id, $user_id ) { 296 $tags = bb_get_topic_tags( $topic_id ); 297 if ( !is_array( $tags ) ) 298 return; 299 $user_tags = array(); 300 301 foreach ( $tags as $tag ) : 302 if ( $tag->user_id == $user_id ) 303 $user_tags[] = $tag; 304 endforeach; 305 return $user_tags; 306 } 307 308 function bb_get_other_tags( $topic_id, $user_id ) { 309 $tags = bb_get_topic_tags( $topic_id ); 310 if ( !is_array( $tags ) ) 311 return; 312 $other_tags = array(); 313 314 foreach ( $tags as $tag ) : 315 if ( $tag->user_id != $user_id ) 316 $other_tags[] = $tag; 317 endforeach; 318 return $other_tags; 319 } 320 321 function bb_get_public_tags( $topic_id ) { 322 $tags = bb_get_topic_tags( $topic_id ); 323 if ( !is_array( $tags ) ) 324 return; 325 $used_tags = array(); 326 $public_tags = array(); 327 328 foreach ( $tags as $tag ) : 329 if ( !in_array($tag->tag_id, $used_tags) ) : 330 $public_tags[] = $tag; 331 $used_tags[] = $tag->tag_id; 332 endif; 333 endforeach; 334 return $public_tags; 335 } 336 337 function bb_get_tagged_topic_ids( $tag_id ) { 338 global $wp_taxonomy_object, $tagged_topic_count; 339 340 if ( $topic_ids = (array) $wp_taxonomy_object->get_objects_in_term( $tag_id, 'bb_topic_tag', array( 'field' => 'tt_id' ) ) ) { 341 $tagged_topic_count = count($topic_ids); 342 return apply_filters('get_tagged_topic_ids', $topic_ids); 343 } else { 344 $tagged_topic_count = 0; 345 return false; 346 } 347 } 348 349 function get_tagged_topics( $args ) { 350 global $tagged_topic_count; 351 $defaults = array( 'tag_id' => false, 'page' => 1, 'number' => false, 'count' => true ); 352 if ( is_numeric( $args ) ) 353 $args = array( 'tag_id' => $args ); 354 else 355 $args = wp_parse_args( $args ); // Make sure it's an array 356 if ( 1 < func_num_args() ) 357 $args['page'] = func_get_arg(1); 358 if ( 2 < func_num_args() ) 359 $args['number'] = func_get_arg(2); 360 361 $args = wp_parse_args( $args, $defaults ); 362 extract( $args, EXTR_SKIP ); 363 364 $q = array('tag_id' => (int) $tag_id, 'page' => (int) $page, 'per_page' => (int) $number, 'count' => $count ); 365 366 $query = new BB_Query( 'topic', $q, 'get_tagged_topics' ); 367 $tagged_topic_count = $query->found_rows; 368 369 return $query->results; 370 } 371 372 function get_tagged_topic_posts( $args ) { 373 $defaults = array( 'tag_id' => false, 'page' => 1, 'number' => false ); 374 if ( is_numeric( $args ) ) 375 $args = array( 'tag_id' => $args ); 376 else 377 $args = wp_parse_args( $args ); // Make sure it's an array 378 if ( 1 < func_num_args() ) 379 $args['page'] = func_get_arg(1); 380 if ( 2 < func_num_args() ) 381 $args['number'] = func_get_arg(2); 382 383 $args = wp_parse_args( $args, $defaults ); 384 extract( $args, EXTR_SKIP ); 385 386 $q = array('tag_id' => (int) $tag_id, 'page' => (int) $page, 'per_page' => (int) $number); 387 388 $query = new BB_Query( 'post', $q, 'get_tagged_topic_posts' ); 389 return $query->results; 390 } 391 392 /** 393 * bb_get_top_tags() - Returns most popular tags. 394 * 395 * @param mixed $args 396 * @return array|false Term objects (back-compat), false on failure 397 */ 398 function bb_get_top_tags( $args = null ) { 399 global $wp_taxonomy_object; 400 401 $args = wp_parse_args( $args, array( 'number' => 40 ) ); 402 $args['order'] = 'DESC'; 403 $args['orderby'] = 'count'; 404 405 $terms = $wp_taxonomy_object->get_terms( 'bb_topic_tag', $args ); 406 if ( is_wp_error( $terms ) ) 407 return false; 408 409 foreach ( $terms as $term ) 410 _bb_make_tag_compat( $term ); 411 412 return $terms; 413 } 414 415 /** 416 * Adds some back-compat properties/elements to a term. 417 * 418 * Casting $tag->term_taxonomy_id to an integer is important since 419 * we check it against is_integer() in bb_get_tag() 420 * 421 * @internal 422 */ 423 function _bb_make_tag_compat( &$tag ) { 424 if ( is_object($tag) && isset($tag->term_id) ) { 425 $tag->term_taxonomy_id = (int) $tag->term_taxonomy_id; 426 $tag->tag_id =& $tag->term_taxonomy_id; 427 $tag->tag =& $tag->slug; 428 $tag->raw_tag =& $tag->name; 429 $tag->tag_count =& $tag->count; 430 } elseif ( is_array($tag) && isset($tag['term_id']) ) { 431 $tag['term_taxonomy_id'] = (int) $tag['term_taxonomy_id']; 432 $tag['tag_id'] =& $tag['term_taxonomy_id']; 433 $tag['tag'] =& $tag['slug']; 434 $tag['raw_tag'] =& $tag['name']; 435 $tag['tag_count'] =& $tag['count']; 436 } 437 } 438 439 function bb_rename_tag( $tag_id, $tag_name ) { 440 if ( !bb_current_user_can( 'manage_tags' ) ) { 441 return false; 442 } 443 444 $tag_id = (int) $tag_id; 445 $raw_tag = bb_trim_for_db( $tag_name, 50 ); 446 $tag_name = tag_sanitize( $tag_name ); 447 448 if ( empty( $tag_name ) ) { 449 return false; 450 } 451 452 if ( $existing_tag = bb_get_tag( $tag_name ) ) { 453 if ( $existing_tag->term_id !== $tag_id ) { 454 return false; 455 } 456 } 457 458 if ( !$old_tag = bb_get_tag( $tag_id ) ) { 459 return false; 460 } 461 462 global $wp_taxonomy_object; 463 $ret = $wp_taxonomy_object->update_term( $tag_id, 'bb_topic_tag', array( 'name' => $raw_tag, 'slug' => $tag_name ) ); 464 465 if ( $ret && !is_wp_error( $ret ) ) { 466 do_action( 'bb_tag_renamed', $tag_id, $old_tag->raw_tag, $raw_tag ); 467 return bb_get_tag( $tag_id ); 468 } 469 return false; 470 } 471 472 // merge $old_id into $new_id. 473 function bb_merge_tags( $old_id, $new_id ) { 474 if ( !bb_current_user_can( 'manage_tags' ) ) { 475 return false; 476 } 477 478 $old_id = (int) $old_id; 479 $new_id = (int) $new_id; 480 481 if ( $old_id == $new_id ) { 482 return false; 483 } 484 485 do_action( 'bb_pre_merge_tags', $old_id, $new_id ); 486 487 // Get all topics tagged with old tag 488 $old_topics = bb_get_tagged_topic_ids( $old_id ); 489 490 // Get all toics tagged with new tag 491 $new_topics = bb_get_tagged_topic_ids( $new_id ); 492 493 // Get intersection of those topics 494 $both_topics = array_intersect( $old_topics, $new_topics ); 495 496 // Discard the intersection from the old tags topics 497 $old_topics = array_diff( $old_topics, $both_topics ); 498 499 // Add the remainder of the old tag topics to the new tag 500 if ( count( $old_topics ) ) { 501 $new_tag = bb_get_tag( $new_id ); 502 foreach ( $old_topics as $old_topic ) { 503 bb_add_topic_tag( $old_topic, $new_tag->slug ); 504 } 505 } 506 507 // Destroy the old tag 508 $old_tag = bb_destroy_tag( $old_id ); 509 510 return array( 'destroyed' => $old_tag, 'old_count' => count( $old_topics ), 'diff_count' => count( $both_topics ) ); 511 }
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 |