[ Index ]

PHP Cross Reference of bbPress

title

Body

[close]

/bb-includes/ -> functions.bb-topic-tags.php (source)

   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      $defaults = array( 'tag_id' => false, 'page' => 1, 'number' => false, 'count' => true );
 358      if ( is_numeric( $args ) )
 359          $args = array( 'tag_id' => $args );
 360      else
 361          $args = wp_parse_args( $args ); // Make sure it's an array
 362      if ( 1 < func_num_args() )
 363          $args['page'] = func_get_arg(1);
 364      if ( 2 < func_num_args() )
 365          $args['number'] = func_get_arg(2);
 366  
 367      $args = wp_parse_args( $args, $defaults );
 368      extract( $args, EXTR_SKIP );
 369  
 370      $q = array('tag_id' => (int) $tag_id, 'page' => (int) $page, 'per_page' => (int) $number, 'count' => $count );
 371  
 372      $query = new BB_Query( 'topic', $q, 'get_tagged_topics' );
 373      $tagged_topic_count = $query->found_rows;
 374  
 375      return $query->results;
 376  }
 377  
 378  function get_tagged_topic_posts( $args ) {
 379      $defaults = array( 'tag_id' => false, 'page' => 1, 'number' => false );
 380      if ( is_numeric( $args ) )
 381          $args = array( 'tag_id' => $args );
 382      else
 383          $args = wp_parse_args( $args ); // Make sure it's an array
 384      if ( 1 < func_num_args() )
 385          $args['page'] = func_get_arg(1);
 386      if ( 2 < func_num_args() )
 387          $args['number'] = func_get_arg(2);
 388  
 389      $args = wp_parse_args( $args, $defaults );
 390      extract( $args, EXTR_SKIP );
 391  
 392      $q = array('tag_id' => (int) $tag_id, 'page' => (int) $page, 'per_page' => (int) $number);
 393  
 394      $query = new BB_Query( 'post', $q, 'get_tagged_topic_posts' );
 395      return $query->results;
 396  }
 397  
 398  /**
 399   * bb_get_top_tags() - Returns most popular tags.
 400   *
 401   * @param mixed $args
 402   * @return array|false Term objects (back-compat), false on failure
 403   */
 404  function bb_get_top_tags( $args = null ) {
 405      global $wp_taxonomy_object;
 406  
 407      $args = wp_parse_args( $args, array( 'number' => 40 ) );
 408      $args['order'] = 'DESC';
 409      $args['orderby'] = 'count';
 410  
 411      $terms = $wp_taxonomy_object->get_terms( 'bb_topic_tag', $args );
 412      if ( is_wp_error( $terms ) )
 413          return false;
 414  
 415      foreach ( $terms as $term )
 416                 _bb_make_tag_compat( $term );
 417  
 418      return $terms;
 419  }
 420  
 421  /**
 422   * Adds some back-compat properties/elements to a term.
 423   *
 424   * Casting $tag->term_taxonomy_id to an integer is important since
 425   * we check it against is_integer() in bb_get_tag()
 426   *
 427   * @internal
 428   */
 429  function _bb_make_tag_compat( &$tag ) {
 430      if ( is_object($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      } elseif ( is_array($tag) && isset($tag['term_id']) ) {
 437          $tag['term_taxonomy_id'] = (int) $tag['term_taxonomy_id'];
 438          $tag['tag_id']    =& $tag['term_taxonomy_id'];
 439          $tag['tag']       =& $tag['slug'];
 440          $tag['raw_tag']   =& $tag['name'];
 441          $tag['tag_count'] =& $tag['count'];
 442      }
 443  }
 444  
 445  function bb_rename_tag( $tag_id, $tag_name ) {
 446      if ( !bb_current_user_can( 'manage_tags' ) ) {
 447          return false;
 448      }
 449  
 450      $tag_id = (int) $tag_id;
 451      $raw_tag = bb_trim_for_db( $tag_name, 50 );
 452      $tag_name = tag_sanitize( $tag_name ); 
 453  
 454      if ( empty( $tag_name ) ) {
 455          return false;
 456      }
 457  
 458      if ( $existing_tag = bb_get_tag( $tag_name ) ) {
 459          if ( $existing_tag->term_id !== $tag_id ) {
 460              return false;
 461          }
 462      }
 463  
 464      if ( !$old_tag = bb_get_tag( $tag_id ) ) {
 465          return false;
 466      }
 467  
 468      global $wp_taxonomy_object;
 469      $ret = $wp_taxonomy_object->update_term( $tag_id, 'bb_topic_tag', array( 'name' => $raw_tag, 'slug' => $tag_name ) );
 470  
 471      if ( $ret && !is_wp_error( $ret ) ) {
 472          do_action( 'bb_tag_renamed', $tag_id, $old_tag->raw_tag, $raw_tag );
 473          return bb_get_tag( $tag_id );
 474      }
 475      return false;
 476  }
 477  
 478  // merge $old_id into $new_id.
 479  function bb_merge_tags( $old_id, $new_id ) {
 480      if ( !bb_current_user_can( 'manage_tags' ) ) {
 481          return false;
 482      }
 483  
 484      $old_id = (int) $old_id;
 485      $new_id = (int) $new_id;
 486  
 487      if ( $old_id == $new_id ) {
 488          return false;
 489      }
 490  
 491      do_action( 'bb_pre_merge_tags', $old_id, $new_id );
 492  
 493      // Get all topics tagged with old tag
 494      $old_topics = bb_get_tagged_topic_ids( $old_id );
 495  
 496      // Get all toics tagged with new tag
 497      $new_topics = bb_get_tagged_topic_ids( $new_id );
 498  
 499      // Get intersection of those topics
 500      $both_topics = array_intersect( $old_topics, $new_topics );
 501  
 502      // Discard the intersection from the old tags topics
 503      $old_topics = array_diff( $old_topics, $both_topics );
 504  
 505      // Add the remainder of the old tag topics to the new tag
 506      if ( count( $old_topics ) ) {
 507          $new_tag = bb_get_tag( $new_id );
 508          foreach ( $old_topics as $old_topic ) {
 509              bb_add_topic_tag( $old_topic, $new_tag->slug );
 510          }
 511      }
 512      
 513      // Destroy the old tag
 514      $old_tag = bb_destroy_tag( $old_id );
 515  
 516      return array( 'destroyed' => $old_tag, 'old_count' => count( $old_topics ), 'diff_count' => count( $both_topics ) );
 517  }


Generated: Thu May 23 03:58:37 2013 Hosted by follow the white rabbit.