[ Root ] [ Search ] [ Index ]

PHP Cross Reference of bbPress Trunk

Provided by Yoast

title

Body

[close]

/bb-includes/ -> functions.bb-posts.php (source)

   1  <?php
   2  
   3  /* Posts */
   4  
   5  /**
   6   * Check to make sure that a user is not making too many posts in a short amount of time.
   7   */
   8  function bb_check_post_flood() {
   9      global $bbdb;
  10      $user_id = (int) $user_id;
  11      $throttle_time = bb_get_option( 'throttle_time' );
  12  
  13      if ( bb_current_user_can( 'manage_options' ) || empty( $throttle_time ) )
  14          return;
  15  
  16      if ( bb_is_user_logged_in() ) {
  17          $bb_current_user = bb_get_current_user();
  18          
  19          if ( isset($bb_current_user->data->last_posted) && time() < $bb_current_user->data->last_posted + $throttle_time && ! bb_current_user_can( 'throttle' ) )
  20              if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
  21                  die( __( 'Slow down; you move too fast.' ) );
  22              else
  23                  bb_die( __( 'Slow down; you move too fast.' ) );
  24      } else {
  25          if ( ( $last_posted = bb_get_transient($_SERVER['REMOTE_ADDR'] . '_last_posted') ) && time() < $last_posted + $throttle_time )
  26              if ( defined('DOING_AJAX') && DOING_AJAX )
  27                  die( __( 'Slow down; you move too fast.' ) );
  28              else
  29                  bb_die( __( 'Slow down; you move too fast.' ) );
  30      }
  31  }
  32  
  33  /**
  34   * Get the current, non-logged-in poster data.
  35   * @return array The associative array of author, email, and url data.
  36   */
  37  function bb_get_current_poster() {
  38      // Cookies should already be sanitized.
  39      $post_author = '';
  40      if ( isset( $_COOKIE['post_author_' . BB_HASH] ) )
  41          $post_author = $_COOKIE['post_author_' . BB_HASH];
  42  
  43      $post_author_email = '';
  44      if ( isset( $_COOKIE['post_author_email_' . BB_HASH] ) )
  45          $post_author_email = $_COOKIE['post_author_email_' . BB_HASH];
  46  
  47      $post_author_url = '';
  48      if ( isset( $_COOKIE['post_author_url_' . BB_HASH] ) )
  49          $post_author_url = $_COOKIE['post_author_url_' . BB_HASH];
  50  
  51      return compact( 'post_author', 'post_author_email', 'post_author_url' );
  52  }
  53  
  54  function bb_get_post( $post_id ) {
  55      global $bbdb;
  56      $post_id = (int) $post_id;
  57      if ( false === $post = wp_cache_get( $post_id, 'bb_post' ) ) {
  58          $post = $bbdb->get_row( $bbdb->prepare( "SELECT * FROM $bbdb->posts WHERE post_id = %d", $post_id ) );
  59          $post = bb_append_meta( $post, 'post' );
  60          wp_cache_set( $post_id, $post, 'bb_post' );
  61      }
  62      return $post;
  63  }
  64  
  65  // NOT bbdb::prepared
  66  function bb_is_first( $post_id ) { // First post in thread
  67      global $bbdb;
  68      if ( !$bb_post = bb_get_post( $post_id ) )
  69          return false;
  70      $post_id = (int) $bb_post->post_id;
  71      $topic_id = (int) $bb_post->topic_id;
  72  
  73      static $first_post;
  74      if ( !isset( $first_post ) ) {
  75          $where = apply_filters('bb_is_first_where', 'AND post_status = 0');
  76          $first_post = (int) $bbdb->get_var("SELECT post_id FROM $bbdb->posts WHERE topic_id = $topic_id $where ORDER BY post_id ASC LIMIT 1");
  77      }
  78  
  79      return $post_id == $first_post;
  80  }
  81  
  82  // Globalizes the result.
  83  function bb_get_first_post( $_topic = false, $author_cache = true ) {
  84      global $topic, $bb_first_post_cache, $bb_post;
  85      if ( !$_topic )
  86          $topic_id = (int) $topic->topic_id;
  87      else if ( is_object($_topic) )
  88          $topic_id = (int) $_topic->topic_id;
  89      else if ( is_numeric($_topic) )
  90          $topic_id = (int) $_topic;
  91  
  92      if ( !$topic_id )
  93          return false;
  94  
  95      if ( isset($bb_first_post_cache[$topic_id]) ) {
  96          $post = bb_get_post( $bb_first_post_cache[$topic_id] );
  97      } else {
  98          $first_posts = bb_cache_first_posts( array($topic_id), $author_cache );
  99          if ( isset($first_posts[$topic_id]) )
 100              $post = $first_posts[$topic_id];
 101      }
 102  
 103      if ( $post ) {
 104          $bb_post = $post;
 105          return $bb_post;
 106      }
 107  
 108      return false;
 109  }
 110  
 111  // Ignore the return value.  Cache first posts with this function and use bb_get_first_post to grab each.
 112  // NOT bbdb::prepared
 113  function bb_cache_first_posts( $_topics = false, $author_cache = true ) {
 114      global $topics, $bb_first_post_cache, $bbdb;
 115      if ( !$_topics )
 116          $_topics =& $topics;
 117      if ( !is_array($_topics) )
 118          return false;
 119  
 120      $topic_ids = array();
 121      foreach ( $_topics as $topic )
 122          if ( is_object($topic) )
 123              $topic_ids[] = (int) $topic->topic_id;
 124          else if ( is_numeric($topic) )
 125              $topic_ids[] = (int) $topic;
 126  
 127      $_topic_ids = join(',', $topic_ids);
 128  
 129      $posts = (array) bb_cache_posts( "SELECT post_id FROM $bbdb->posts WHERE topic_id IN ($_topic_ids) AND post_position = 1", true );
 130  
 131      $first_posts = array();
 132      foreach ( $posts as $post ) {
 133          $bb_first_post_cache[(int) $post->topic_id] = (int) $post->post_id;
 134          $first_posts[(int) $post->topic_id] = $post;
 135      }
 136  
 137      if ( $author_cache )
 138          bb_post_author_cache( $posts );
 139  
 140      return $first_posts;
 141  }
 142  
 143  function bb_cache_posts( $query, $post_id_query = false ) {
 144      global $bbdb;
 145  
 146      $_query_post_ids = array();
 147      $_query_posts = array();
 148      $_cached_posts = array();
 149      $ordered_post_ids = array();
 150  
 151      if ( $post_id_query && is_string( $query ) ) {
 152          // The query is a SQL query to retrieve post_ids only
 153          $key = md5( $query );
 154          if ( false === $post_ids = wp_cache_get( $key, 'bb_cache_posts_post_ids' ) ) {
 155              if ( !$post_ids = (array) $bbdb->get_col( $query, 0 ) ) {
 156                  return array();
 157              }
 158              wp_cache_add( $key, $post_ids, 'bb_cache_posts_post_ids' );
 159          }
 160          $query = $post_ids;
 161      }
 162  
 163      if ( is_array( $query ) ) {
 164          $get_order_from_query = false;
 165  
 166          foreach ( $query as $_post_id ) {
 167              $ordered_post_ids[] = $_post_id;
 168              if ( false === $_post = wp_cache_get( $_post_id, 'bb_post' ) ) {
 169                  $_query_post_ids[] = $_post_id;
 170              } else {
 171                  $_cached_posts[$_post->post_id] = $_post;
 172              }
 173          }
 174  
 175          if ( count( $_query_post_ids ) ) {
 176              // Escape the ids for the SQL query
 177              $_query_post_ids = $bbdb->escape_deep( $_query_post_ids );
 178  
 179              // Sort the ids so the MySQL will more consistently cache the query
 180              sort( $_query_post_ids );
 181  
 182              $_query = "SELECT * FROM $bbdb->posts WHERE post_id IN ('" . join( "','", $_query_post_ids ) . "')";
 183          }
 184      } else {
 185          // The query is a full SQL query which needs to be executed
 186          $get_order_from_query = true;
 187          $_query = $query;
 188      }
 189  
 190      if ( $_query_posts = (array) $bbdb->get_results( $_query ) ) {
 191          $_appendable_posts = array();
 192          foreach ( $_query_posts as $_query_post ) {
 193              if ( $get_order_from_query ) {
 194                  $ordered_post_ids[] = $_query_post->post_id;
 195              }
 196              if ( false === $_post = wp_cache_get( $_query_post->post_id, 'bb_post' ) ) {
 197                  $_appendable_posts[] = $_query_post;
 198              } else {
 199                  $_cached_posts[$_query_post->post_id] = $_post;
 200              }
 201          }
 202          if ( count( $_appendable_posts ) ) {
 203              $_query_posts = bb_append_meta( $_appendable_posts, 'post' );
 204              foreach( $_query_posts as $_query_post ) {
 205                  wp_cache_add( $_query_post->post_id, $_query_post, 'bb_post' );
 206              }
 207          } else {
 208              $_query_posts = array();
 209          }
 210      } else {
 211          $_query_posts = array();
 212      }
 213  
 214      foreach ( array_merge( $_cached_posts, $_query_posts ) as $_post ) {
 215          $keyed_posts[$_post->post_id] = $_post;
 216      }
 217  
 218      $the_posts = array();
 219      foreach ( $ordered_post_ids as $ordered_post_id ) {
 220          $the_posts[] = $keyed_posts[$ordered_post_id];
 221      }
 222  
 223      return $the_posts;
 224  }
 225  
 226  // Globalizes the result
 227  function bb_get_last_post( $_topic = false, $author_cache = true ) {
 228      global $topic, $bb_post;
 229      if ( !$_topic )
 230          $topic_id = (int) $topic->topic_id;
 231      else if ( is_object($_topic) )
 232          $topic_id = (int) $_topic->topic_id;
 233      else if ( is_numeric($_topic) )
 234          $topic_id = (int) $_topic;
 235  
 236      if ( !$topic_id )
 237          return false;
 238  
 239      $_topic = get_topic( $topic_id );
 240  
 241      if ( $post = bb_get_post( $_topic->topic_last_post_id ) ) {
 242          if ( $author_cache )
 243              bb_post_author_cache( array($post) );
 244          $bb_post = $post;
 245      }
 246  
 247      return $post;
 248  }
 249  
 250  // No return value. Cache last posts with this function and use bb_get_last_post to grab each.
 251  // NOT bbdb::prepared
 252  function bb_cache_last_posts( $_topics = false, $author_cache = true ) {
 253      global $topics, $bbdb;
 254      if ( !$_topics )
 255          $_topics =& $topics;
 256      if ( !is_array($_topics) )
 257          return false;
 258  
 259      $last_post_ids = array();
 260      $topic_ids = array();
 261      foreach ( $_topics as $topic )
 262          if ( is_object($topic) )
 263              $last_post_ids[] = (int) $topic->topic_last_post_id;
 264          else if ( is_numeric($topic) && false !== $cached_topic = wp_cache_get( $topic, 'bb_topic' ) )
 265              $last_post_ids[] = (int) $cached_topic->topic_last_post_id;
 266          else if ( is_numeric($topic) )
 267              $topic_ids[] = (int) $topic;
 268  
 269      if ( !empty($last_post_ids) ) {
 270          $_last_post_ids = join(',', $last_post_ids);
 271          $posts = (array) bb_cache_posts( "SELECT post_id FROM $bbdb->posts WHERE post_id IN ($_last_post_ids) AND post_status = 0", true );
 272          if ( $author_cache )
 273              bb_post_author_cache( $posts );
 274      }
 275  
 276      if ( !empty($topic_ids) ) {    
 277          $_topic_ids = join(',', $topic_ids);
 278          $posts = (array) bb_cache_posts( "SELECT p.post_id FROM $bbdb->topics AS t LEFT JOIN $bbdb->posts AS p ON ( t.topic_last_post_id = p.post_id ) WHERE t.topic_id IN ($_topic_ids) AND p.post_status = 0", true );
 279          if ( $author_cache )
 280              bb_post_author_cache( $posts );
 281      }
 282  }
 283  
 284  // NOT bbdb::prepared
 285  function bb_cache_post_topics( $posts ) {
 286      global $bbdb;
 287  
 288      if ( !$posts )
 289          return;
 290  
 291      $topic_ids = array();
 292      foreach ( $posts as $post )
 293          if ( false === wp_cache_get( $post->topic_id, 'bb_topic' ) )
 294              $topic_ids[] = (int) $post->topic_id;
 295  
 296      if ( !$topic_ids )
 297          return;
 298  
 299      sort( $topic_ids );
 300      $topic_ids = join(',', $topic_ids);
 301  
 302      if ( $topics = $bbdb->get_results( "SELECT * FROM $bbdb->topics WHERE topic_id IN($topic_ids)" ) )
 303          bb_append_meta( $topics, 'topic' );
 304  }
 305  
 306  function bb_get_latest_posts( $limit = 0, $page = 1 ) {
 307      $limit = (int) $limit;
 308      $post_query = new BB_Query( 'post', array( 'page' => $page, 'per_page' => $limit ), 'get_latest_posts' );
 309      return $post_query->results;
 310  }
 311  
 312  function bb_get_latest_forum_posts( $forum_id, $limit = 0, $page = 1 ) {
 313      $forum_id = (int) $forum_id;
 314      $limit    = (int) $limit;
 315      $post_query = new BB_Query( 'post', array( 'forum_id' => $forum_id, 'page' => $page, 'per_page' => $limit ), 'get_latest_forum_posts' );
 316      return $post_query->results;
 317  }
 318  
 319  function bb_insert_post( $args = null ) {
 320      global $bbdb, $bb_current_user, $bb;
 321  
 322      if ( !$args = wp_parse_args( $args ) )
 323          return false;
 324  
 325      $fields = array_keys( $args );
 326  
 327      if ( isset($args['post_id']) && false !== $args['post_id'] ) {
 328          $update = true;
 329          if ( !$post_id = (int) get_post_id( $args['post_id'] ) )
 330              return false;
 331          // Get from db, not cache.  Good idea?
 332          $post = $bbdb->get_row( $bbdb->prepare( "SELECT * FROM $bbdb->posts WHERE post_id = %d", $post_id ) );
 333          $defaults = get_object_vars( $post );
 334          unset( $defaults['post_id'] );
 335  
 336          // Only update the args we passed
 337          $fields = array_intersect( $fields, array_keys($defaults) );
 338          if ( in_array( 'topic_id', $fields ) )
 339              $fields[] = 'forum_id';
 340  
 341          // No need to run filters if these aren't changing
 342          // bb_new_post() and bb_update_post() will always run filters
 343          $run_filters = (bool) array_intersect( array( 'post_status', 'post_text' ), $fields );
 344      } else {
 345          $post_id = false;
 346          $update = false;
 347          $now = bb_current_time( 'mysql' );
 348          $current_user_id = bb_get_current_user_info( 'id' );
 349          $ip_address = $_SERVER['REMOTE_ADDR'];
 350  
 351          $defaults = array(
 352              'topic_id' => 0,
 353              'post_text' => '',
 354              'post_time' => $now,
 355              'poster_id' => $current_user_id, // accepts ids or names
 356              'poster_ip' => $ip_address,
 357              'post_status' => 0, // use bb_delete_post() instead
 358              'post_position' => false
 359          );
 360  
 361          // Insert all args
 362          $fields = array_keys($defaults);
 363          $fields[] = 'forum_id';
 364  
 365          $run_filters = true;
 366      }
 367  
 368      $defaults['throttle'] = true;
 369      extract( wp_parse_args( $args, $defaults ) );
 370      
 371      // If the user is not logged in and loginless posting is ON, then this function expects $post_author, $post_email and $post_url to be sanitized (check bb-post.php for example)
 372  
 373      if ( !$topic = get_topic( $topic_id ) )
 374          return false;
 375  
 376      if ( bb_is_login_required() && ! $user = bb_get_user( $poster_id ) )
 377          return false;
 378  
 379      $topic_id = (int) $topic->topic_id;
 380      $forum_id = (int) $topic->forum_id;
 381  
 382      if ( $run_filters && !$post_text = apply_filters('pre_post', $post_text, $post_id, $topic_id) )
 383          return false;
 384  
 385      if ( $update ) // Don't change post_status with this function.  Use bb_delete_post().
 386          $post_status = $post->post_status;
 387  
 388      if ( $run_filters )
 389          $post_status = (int) apply_filters('pre_post_status', $post_status, $post_id, $topic_id);
 390  
 391      if ( false === $post_position )
 392          $post_position = $topic_posts = intval( ( 0 == $post_status ) ? $topic->topic_posts + 1 : $topic->topic_posts );
 393  
 394      unset($defaults['throttle']);
 395  
 396      if ( $update ) {
 397          $bbdb->update( $bbdb->posts, compact( $fields ), compact( 'post_id' ) );
 398          wp_cache_delete( $post_id, 'bb_post' );
 399      } else {
 400          $bbdb->insert( $bbdb->posts, compact( $fields ) );
 401          $post_id = $topic_last_post_id = (int) $bbdb->insert_id;
 402  
 403          // if user not logged in, save user data as meta data
 404          if ( !$user ) {
 405              bb_update_meta($post_id, 'post_author', $post_author, 'post');
 406              bb_update_meta($post_id, 'post_email', $post_email, 'post');
 407              bb_update_meta($post_id, 'post_url', $post_url, 'post');
 408          }
 409  
 410          if ( 0 == $post_status ) {
 411              $topic_time = $post_time;
 412              $topic_last_poster = ( ! bb_is_user_logged_in() && ! bb_is_login_required() ) ? -1 : $poster_id;
 413              $topic_last_poster_name = ( ! bb_is_user_logged_in() && ! bb_is_login_required() ) ? $post_author : $user->user_login;
 414  
 415              $bbdb->query( $bbdb->prepare( "UPDATE $bbdb->forums SET posts = posts + 1 WHERE forum_id = %d;", $topic->forum_id ) );
 416              $bbdb->update(
 417                  $bbdb->topics,
 418                  compact( 'topic_time', 'topic_last_poster', 'topic_last_poster_name', 'topic_last_post_id', 'topic_posts' ),
 419                  compact ( 'topic_id' )
 420              );
 421  
 422              $query = new BB_Query( 'post', array( 'post_author_id' => $poster_id, 'topic_id' => $topic_id, 'post_id' => "-$post_id" ) );
 423              if ( !$query->results )
 424                  bb_update_usermeta( $poster_id, $bbdb->prefix . 'topics_replied', $user->topics_replied + 1 );
 425  
 426          } else {
 427              bb_update_topicmeta( $topic->topic_id, 'deleted_posts', isset($topic->deleted_posts) ? $topic->deleted_posts + 1 : 1 );
 428          }
 429      }
 430      bb_update_topic_voices( $topic_id );
 431      
 432      if ( $throttle && !bb_current_user_can( 'throttle' ) ) {
 433          if ( $user )
 434              bb_update_usermeta( $poster_id, 'last_posted', time() );
 435          else
 436              bb_set_transient( $_SERVER['REMOTE_ADDR'] . '_last_posted', time() );
 437      }
 438      
 439      if ( !bb_is_login_required() && !$user = bb_get_user( $poster_id ) ) {
 440          $post_cookie_lifetime = apply_filters( 'bb_post_cookie_lifetime', 30000000 );
 441          setcookie( 'post_author_' . BB_HASH, $post_author, time() + $post_cookie_lifetime, $bb->cookiepath, $bb->cookiedomain );
 442          setcookie( 'post_author_email_' . BB_HASH, $post_email, time() + $post_cookie_lifetime, $bb->cookiepath, $bb->cookiedomain );
 443          setcookie( 'post_author_url_' . BB_HASH, $post_url, time() + $post_cookie_lifetime, $bb->cookiepath, $bb->cookiedomain );
 444      }
 445  
 446      wp_cache_delete( $topic_id, 'bb_topic' );
 447      wp_cache_delete( $topic_id, 'bb_thread' );
 448      wp_cache_delete( $forum_id, 'bb_forum' );
 449      wp_cache_flush( 'bb_forums' );
 450      wp_cache_flush( 'bb_query' );
 451      wp_cache_flush( 'bb_cache_posts_post_ids' );
 452  
 453      if ( $update ) // fire actions after cache is flushed
 454          do_action( 'bb_update_post', $post_id );
 455      else
 456          do_action( 'bb_new_post', $post_id );
 457  
 458      do_action( 'bb_insert_post', $post_id, $args, compact( array_keys($args) ) ); // post_id, what was passed, what was used
 459  
 460      if (bb_get_option('enable_pingback')) {
 461          bb_update_postmeta($post_id, 'pingback_queued', '');
 462          wp_schedule_single_event(time(), 'do_pingbacks');
 463      }
 464  
 465      return $post_id;
 466  }
 467  
 468  // Deprecated: expects $post_text to be pre-escaped
 469  function bb_new_post( $topic_id, $post_text ) {
 470      $post_text = stripslashes( $post_text );
 471      return bb_insert_post( compact( 'topic_id', 'post_text' ) );
 472  }
 473  
 474  // Deprecated: expects $post_text to be pre-escaped
 475  function bb_update_post( $post_text, $post_id, $topic_id ) {
 476      $post_text = stripslashes( $post_text );
 477      return bb_insert_post( compact( 'post_text', 'post_id', 'topic_id' ) );
 478  }
 479  
 480  function bb_update_post_positions( $topic_id ) {
 481      global $bbdb;
 482      $topic_id = (int) $topic_id;
 483      $posts = get_thread( $topic_id, array( 'per_page' => '-1' ) );
 484      if ( $posts ) {
 485          foreach ( $posts as $i => $post ) {
 486              $bbdb->query( $bbdb->prepare( "UPDATE $bbdb->posts SET post_position = %d WHERE post_id = %d", $i + 1, $post->post_id ) );
 487              wp_cache_delete( (int) $post->post_id, 'bb_post' );
 488          }
 489          wp_cache_delete( $topic_id, 'bb_thread' );
 490          wp_cache_flush( 'bb_query' );
 491          wp_cache_flush( 'bb_cache_posts_post_ids' );
 492          return true;
 493      } else {
 494          return false;
 495      }
 496  }
 497  
 498  function bb_delete_post( $post_id, $new_status = 0 ) {
 499      global $bbdb, $topic, $bb_post;
 500      $post_id = (int) $post_id;
 501      $bb_post    = bb_get_post ( $post_id );
 502      $new_status = (int) $new_status;
 503      $old_status = (int) $bb_post->post_status;
 504      add_filter( 'get_topic_where', 'bb_no_where' );
 505      $topic   = get_topic( $bb_post->topic_id );
 506      $topic_id = (int) $topic->topic_id;
 507  
 508      if ( $bb_post ) {
 509          $uid = (int) $bb_post->poster_id;
 510          if ( $new_status == $old_status )
 511              return;
 512          _bb_delete_post( $post_id, $new_status );
 513          if ( 0 == $old_status ) {
 514              bb_update_topicmeta( $topic_id, 'deleted_posts', $topic->deleted_posts + 1 );
 515              $bbdb->query( $bbdb->prepare( "UPDATE $bbdb->forums SET posts = posts - 1 WHERE forum_id = %d", $topic->forum_id ) );
 516          } else if ( 0 == $new_status ) {
 517              bb_update_topicmeta( $topic_id, 'deleted_posts', $topic->deleted_posts - 1 );
 518              $bbdb->query( $bbdb->prepare( "UPDATE $bbdb->forums SET posts = posts + 1 WHERE forum_id = %d", $topic->forum_id ) );
 519          }
 520          $posts = (int) $bbdb->get_var( $bbdb->prepare( "SELECT COUNT(*) FROM $bbdb->posts WHERE topic_id = %d AND post_status = 0", $topic_id ) );
 521          $bbdb->update( $bbdb->topics, array( 'topic_posts' => $posts ), compact( 'topic_id' ) );
 522  
 523          if ( 0 == $posts ) {
 524              if ( 0 == $topic->topic_status || 1 == $new_status )
 525                  bb_delete_topic( $topic_id, $new_status );
 526          } else {
 527              if ( 0 != $topic->topic_status ) {
 528                  $bbdb->update( $bbdb->topics, array( 'topic_status' => 0 ), compact( 'topic_id' ) );
 529                  $bbdb->query( $bbdb->prepare( "UPDATE $bbdb->forums SET topics = topics + 1 WHERE forum_id = %d", $topic->forum_id ) );
 530              }
 531              bb_topic_set_last_post( $topic_id );
 532              bb_update_post_positions( $topic_id );
 533              bb_update_topic_voices( $topic_id );
 534          }
 535  
 536          $user = bb_get_user( $uid );
 537  
 538          $user_posts = new BB_Query( 'post', array( 'post_author_id' => $user->ID, 'topic_id' => $topic_id ) );
 539          if ( $new_status && !$user_posts->results )
 540              bb_update_usermeta( $user->ID, $bbdb->prefix . 'topics_replied', $user->topics_replied - 1 );
 541          wp_cache_delete( $topic_id, 'bb_topic' );
 542          wp_cache_delete( $topic_id, 'bb_thread' );
 543          wp_cache_flush( 'bb_forums' );
 544          wp_cache_flush( 'bb_query' );
 545          wp_cache_flush( 'bb_cache_posts_post_ids' );
 546          do_action( 'bb_delete_post', $post_id, $new_status, $old_status );
 547          return $post_id;
 548      } else {
 549          return false;
 550      }
 551  }
 552  
 553  function _bb_delete_post( $post_id, $post_status ) {
 554      global $bbdb;
 555      $post_id = (int) $post_id;
 556      $post_status = (int) $post_status;
 557      $bbdb->update( $bbdb->posts, compact( 'post_status' ), compact( 'post_id' ) );
 558      wp_cache_delete( $post_id, 'bb_post' );
 559      do_action( '_bb_delete_post', $post_id, $post_status );
 560  }
 561  
 562  function bb_topics_replied_on_undelete_post( $post_id ) {
 563      global $bbdb;
 564      $bb_post = bb_get_post( $post_id );
 565      $topic = get_topic( $bb_post->topic_id );
 566  
 567      $user_posts = new BB_Query( 'post', array( 'post_author_id' => $bb_post->poster_id, 'topic_id' => $topic->topic_id ) );
 568  
 569      if ( 1 == count($user_posts) && $user = bb_get_user( $bb_post->poster_id ) )
 570          bb_update_usermeta( $user->ID, $bbdb->prefix . 'topics_replied', $user->topics_replied + 1 );
 571  }
 572  
 573  function bb_post_author_cache($posts) {
 574      if ( !$posts )
 575          return;
 576  
 577      $ids = array();
 578      foreach ($posts as $bb_post)
 579          $ids[] = $bb_post->poster_id;
 580  
 581      if ( $ids )
 582          bb_cache_users(array_unique($ids));
 583  }
 584  
 585  // These two filters are lame.  It'd be nice if we could do this in the query parameters
 586  function bb_get_recent_user_replies_fields( $fields ) {
 587      return $fields . ', MAX(post_time) as post_time';
 588  }
 589  
 590  function bb_get_recent_user_replies_group_by() {
 591      return 'p.topic_id';
 592  }
 593  
 594  function bb_get_recent_user_replies( $user_id ) {
 595      global $bbdb;
 596      $user_id = (int) $user_id;
 597  
 598      $post_query = new BB_Query( 'post', array( 'post_author_id' => $user_id, 'order_by' => 'post_time' ), 'get_recent_user_replies' );
 599  
 600      return $post_query->results;
 601  }
 602  
 603  /**
 604   * Sends notification emails for new posts.
 605   *
 606   * Gets new post's ID and check if there are subscribed
 607   * user to that topic, and if there are, send notifications
 608   *
 609   * @since 1.1
 610   *
 611   * @param int $post_id ID of new post
 612   */
 613  function bb_notify_subscribers( $post_id ) {
 614      global $bbdb, $bb_ksd_pre_post_status;
 615  
 616      if ( !empty( $bb_ksd_pre_post_status ) )
 617          return false;
 618  
 619      if ( !$post = bb_get_post( $post_id ) )
 620          return false;
 621  
 622      if ( !$topic = get_topic( $post->topic_id ) )
 623          return false;
 624      
 625      $post_id = $post->post_id;
 626      $topic_id = $topic->topic_id;
 627  
 628      if ( !$poster_name = get_post_author( $post_id ) )
 629          return false;
 630      
 631      do_action( 'bb_pre_notify_subscribers', $post_id, $topic_id );
 632  
 633      if ( !$user_ids = $bbdb->get_col( $bbdb->prepare( "SELECT `$bbdb->term_relationships`.`object_id`
 634          FROM $bbdb->term_relationships, $bbdb->term_taxonomy, $bbdb->terms
 635          WHERE `$bbdb->term_relationships`.`term_taxonomy_id` = `$bbdb->term_taxonomy`.`term_taxonomy_id`
 636          AND `$bbdb->term_taxonomy`.`term_id` = `$bbdb->terms`.`term_id`
 637          AND `$bbdb->term_taxonomy`.`taxonomy` = 'bb_subscribe'
 638          AND `$bbdb->terms`.`slug` = 'topic-%d'",
 639          $topic_id ) ) )
 640          return false;
 641  
 642      foreach ( (array) $user_ids as $user_id ) {
 643          if ( $user_id == $post->poster_id )
 644              continue; // don't send notifications to the person who made the post
 645          
 646          $user = bb_get_user( $user_id );
 647          
 648          if ( !$message = apply_filters( 'bb_subscription_mail_message', __( "%1\$s wrote:\n\n%2\$s\n\nPost Link: %3\$s\n\nYou're getting this mail because you subscribed to the topic, visit the topic and login to unsubscribe." ), $post_id, $topic_id ) )
 649              continue; /* For plugins */
 650          
 651          bb_mail(
 652              $user->user_email,
 653              apply_filters( 'bb_subscription_mail_title', '[' . bb_get_option( 'name' ) . '] ' . $topic->topic_title, $post_id, $topic_id ),
 654              sprintf( $message, $poster_name, strip_tags( $post->post_text ), get_post_link( $post_id ) )
 655          );
 656      }
 657      
 658      do_action( 'bb_post_notify_subscribers', $post_id, $topic_id );
 659  }
 660  
 661  /**
 662   * Updates user's subscription status in database.
 663   *
 664   * Gets user's new subscription status for topic and
 665   * adds new status to database.
 666   *
 667   * @since 1.1
 668   *
 669   * @param int $topic_id ID of topic for subscription
 670   * @param string $new_status New subscription status
 671   * @param int $user_id Optional. ID of user for subscription
 672   */
 673  function bb_subscription_management( $topic_id, $new_status, $user_id = '' ) {
 674      global $bbdb, $wp_taxonomy_object;
 675      
 676      $topic = get_topic( $topic_id );
 677      if (!$user_id) {
 678          $user_id = bb_get_current_user_info( 'id' );
 679      }
 680      
 681      do_action( 'bb_subscripton_management', $topic_id, $new_status, $user_id );
 682      
 683      switch ( $new_status ) {
 684          case 'add':
 685              $tt_ids = $wp_taxonomy_object->set_object_terms( $user_id, 'topic-' . $topic->topic_id, 'bb_subscribe', array( 'append' => true, 'user_id' => $user_id ) );
 686              break;
 687          case 'remove':
 688              // I hate this with the passion of a thousand suns
 689              $term_id = $bbdb->get_var( "SELECT term_id FROM $bbdb->terms WHERE slug = 'topic-$topic->topic_id'" );
 690              $term_taxonomy_id = $bbdb->get_var( "SELECT term_taxonomy_id FROM $bbdb->term_taxonomy WHERE term_id = $term_id AND taxonomy = 'bb_subscribe'" );
 691              $bbdb->query( "DELETE FROM $bbdb->term_relationships WHERE object_id = $user_id AND term_taxonomy_id = $term_taxonomy_id" );
 692              $bbdb->query( "DELETE FROM $bbdb->term_taxonomy WHERE term_id = $term_id AND taxonomy = 'bb_subscribe'" );
 693              break;
 694      }
 695      
 696  }
 697  
 698  /**
 699   * Process subscription checkbox submission.
 700   *
 701   * Get ID of and new subscription status and pass values to
 702   * bb_user_subscribe_checkbox_update function
 703   *
 704   * @since 1.1
 705   *
 706   * @param int $post_id ID of new/edited post
 707   */
 708  function bb_user_subscribe_checkbox_update( $post_id ) {
 709      if ( !bb_is_user_logged_in() )
 710          return false;
 711      
 712      $post        = bb_get_post( $post_id );
 713      $topic_id    = (int) $post->topic_id;
 714      $subscribed    = bb_is_user_subscribed( array( 'topic_id' => $topic_id, 'user_id' => $post->poster_id ) ) ? true : false;
 715      $check        = $_REQUEST['subscription_checkbox'];
 716      
 717      do_action( 'bb_user_subscribe_checkbox_update', $post_id, $topic_id, $subscribe, $check );
 718      
 719      if ( 'subscribe' == $check && !$subscribed )
 720          bb_subscription_management( $topic_id, 'add' );
 721      elseif ( !$check && $subscribed )
 722          bb_subscription_management( $topic_id, 'remove' );
 723      
 724  }


Generated: Mon Nov 15 04:45:27 2010 Cross-referenced by PHPXref 0.7