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