[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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


Generated: Thu Dec 7 01:01:35 2017 Cross-referenced by PHPXref 0.7.1