[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-forums/bbpress/bb-admin/ -> admin-ajax.php (source)

   1  <?php
   2  
   3  define( 'BB_IS_ADMIN', true );
   4  define( 'DOING_AJAX', true );
   5  
   6  require_once ('../bb-load.php');
   7  
   8  if ( !class_exists( 'WP_Ajax_Response' ) )
   9      require_once( BACKPRESS_PATH . 'class.wp-ajax-response.php' );
  10  
  11  require_once ( BB_PATH . 'bb-admin/includes/functions.bb-admin.php' );
  12  
  13  if ( !$bb_current_id = bb_get_current_user_info( 'id' ) )
  14      die('-1');
  15  
  16  function bb_grab_results() {
  17      global $ajax_results;
  18      $ajax_results = @ unserialize(func_get_arg(0));
  19      if ( false === $ajax_results )
  20          $ajax_results = func_get_args();
  21      return;
  22  }
  23  
  24  $id = (int) @$_POST['id'];
  25  
  26  switch ( $action = $_POST['action'] ) :
  27  case 'add-tag' : // $id is topic_id
  28      if ( !bb_current_user_can('edit_tag_by_on', $bb_current_id, $id) )
  29          die('-1');
  30  
  31      bb_check_ajax_referer( "add-tag_$id" );
  32  
  33      global $tag, $topic;
  34      add_action('bb_tag_added', 'bb_grab_results', 10, 3);
  35      add_action('bb_already_tagged', 'bb_grab_results', 10, 3);
  36      $tag_name = @$_POST['tag'];
  37      $tag_name = stripslashes( $tag_name );
  38  
  39      $topic = get_topic( $id );
  40      if ( !$topic )
  41          die('0');
  42  
  43      $tag_name = rawurldecode($tag_name);
  44      $x = new WP_Ajax_Response();
  45      foreach ( bb_add_topic_tags( $id, $tag_name ) as $tag_id ) {
  46          if ( !is_numeric($tag_id) || !$tag = bb_get_tag( (int) $tag_id, bb_get_current_user_info( 'id' ), $topic->topic_id ) ) {
  47              if ( !$tag = bb_get_tag( $tag_id ) ) {
  48                  continue;
  49              }
  50          }
  51          $tag->tag_id  = $tag_id;
  52          $tag->user_id = bb_get_current_user_info( 'id' );
  53          $tag_id_val   = $tag->tag_id . '_' . $tag->user_id;
  54          $tag->raw_tag = esc_attr( $tag_name );
  55          $x->add( array(
  56              'what' => 'tag',
  57              'id'   => $tag_id_val,
  58              'data' => _bb_list_tag_item( $tag, array( 'list_id' => 'tags-list', 'format' => 'list' ) )
  59          ) );
  60      }
  61      $x->send();
  62      break;
  63  
  64  case 'delete-tag' :
  65      list($tag_id, $user_id) = explode('_', $_POST['id']);
  66      $tag_id   = (int) $tag_id;
  67      $user_id  = (int) $user_id;
  68      $topic_id = (int) $_POST['topic_id'];
  69  
  70      if ( !bb_current_user_can('edit_tag_by_on', $user_id, $topic_id) )
  71          die('-1');
  72  
  73      bb_check_ajax_referer( "remove-tag_$tag_id|$topic_id" );
  74  
  75      add_action('bb_rpe_tag_removed', 'bb_grab_results', 10, 3);
  76  
  77      $tag   = bb_get_tag( $tag_id );
  78      $user  = bb_get_user( $user_id );
  79      $topic = get_topic ( $topic_id );
  80      if ( !$tag || !$topic )
  81          die('0');
  82      if ( false !== bb_remove_topic_tag( $tag_id, $user_id, $topic_id ) )
  83          die('1');
  84      break;
  85  
  86  case 'dim-favorite' :
  87      $user_id  = bb_get_current_user_info( 'id' );
  88  
  89      if ( !$topic = get_topic( $id ) )
  90          die('0');
  91  
  92      if ( !bb_current_user_can( 'edit_favorites_of', $user_id ) )
  93          die('-1');
  94  
  95      bb_check_ajax_referer( "toggle-favorite_$topic->topic_id" );
  96  
  97      $is_fav = is_user_favorite( $user_id, $topic->topic_id );
  98  
  99      if ( 1 == $is_fav ) {
 100          if ( bb_remove_user_favorite( $user_id, $topic->topic_id ) )
 101              die('1');
 102      } elseif ( false === $is_fav ) {
 103          if ( bb_add_user_favorite( $user_id, $topic->topic_id ) )
 104              die('1');
 105      }
 106      break;
 107  
 108  case 'delete-post' : // $id is post_id
 109      if ( !bb_current_user_can( 'delete_post', $id ) )
 110          die('-1');
 111  
 112      bb_check_ajax_referer( "delete-post_$id" );
 113  
 114      $status = (int) $_POST['status'];
 115  
 116      if ( !$bb_post = bb_get_post( $id ) )
 117          die('0');
 118  
 119      if ( $status == $bb_post->post_status )
 120          die('1'); // We're already there
 121  
 122      if ( bb_delete_post( $id, $status ) ) {
 123          $topic = get_topic( $bb_post->topic_id );
 124          if ( 0 == $topic->topic_posts ) {
 125              // If we deleted the only post, send back a WP_Ajax_Response object with a URL to redirect to
 126              if ( $ref = wp_get_referer() ) {
 127                  $ref_topic = bb_get_topic_from_uri( $ref );
 128                  if ( $ref_topic && $ref_topic->topic_id == $topic->topic_id )
 129                      $ref = add_query_arg( 'view', 'all', $ref );
 130                  if ( false === strpos( $ref, '#' ) )
 131                      $ref .= "#post-{$bb_post->post_id}";
 132              } else {
 133                  $ref = add_query_arg( 'view', 'all', get_post_link( $topic->topic_id ) );
 134              }
 135              $x = new WP_Ajax_Response( array(
 136                  'what' => 'post',
 137                  'id' => $bb_post->post_id,
 138                  'data' => $ref,
 139              ) );
 140              $x->send();
 141          }
 142          die('1');
 143      }
 144      break;
 145  /*
 146  case 'add-post' : // Can put last_modified stuff back in later
 147      bb_check_ajax_referer( $action );
 148      $error = false;
 149      $post_id = 0;
 150      $topic_id = (int) $_POST['topic_id'];
 151      $last_mod = (int) $_POST['last_mod'];
 152      if ( !$post_content = trim($_POST['post_content']) )
 153          $error = new WP_Error( 'no-content', __('You need to actually submit some content!') );
 154      if ( !bb_current_user_can( 'write_post', $topic_id ) )
 155          die('-1');
 156      if ( !$topic = get_topic( $topic_id ) )
 157          die('0');
 158      if ( !topic_is_open( $topic_id ) )
 159          $error = new WP_Error( 'topic-closed', __('This topic is closed.') );
 160      if ( $throttle_time = bb_get_option( 'throttle_time' ) )
 161          if ( isset($bb_current_user->data->last_posted) && time() < $bb_current_user->data->last_posted + $throttle_time && !bb_current_user_can('throttle') )
 162              $error = new WP_Error( 'throttle-limit', sprintf( __('Slow down!  You can only post every %d seconds.'), $throttle_time );
 163  
 164      if ( !$error ) :
 165          if ( !$post_id = bb_new_post( $topic_id, rawurldecode($_POST['post_content']) ) )
 166              die('0');
 167  
 168          $bb_post = bb_get_post( $post_id );
 169  
 170          $new_page = bb_get_page_number( $bb_post->post_position );
 171  
 172          ob_start();
 173              echo "<li id='post-$post_id'>";
 174              bb_post_template();
 175              echo '</li>';
 176          $data = ob_get_contents();
 177          ob_end_clean();
 178      endif;
 179      $x = new WP_Ajax_Response( array(
 180          'what' => 'post',
 181          'id' => $post_id,
 182          'data' => is_wp_error($error) ? $error : $data
 183      ) );
 184      $x->send();
 185      break;
 186  */
 187  case 'add-forum' :
 188      if ( !bb_current_user_can( 'manage_forums' ) )
 189          die('-1');
 190  
 191      bb_check_ajax_referer( $action );
 192  
 193      if ( !$forum_id = bb_new_forum( $_POST ) )
 194          die('0');
 195  
 196      global $forums_count;
 197      $forums_count = 2; // Hack
 198  
 199      $data = bb_forum_row( $forum_id, false, true );
 200  
 201      $forum = bb_get_forum( $forum_id );
 202      if ( $forum->forum_parent ) {
 203          $siblings = bb_get_forums( $forum->forum_parent );
 204          $last_sibling = array_pop( $siblings );
 205          if ( $last_sibling->forum_id == $forum_id )
 206              $last_sibling = array_pop( $siblings );
 207          if ( $last_sibling ) {
 208              $position = "forum-$last_sibling->forum_id";
 209          } else {
 210              $position = "+forum-$forum->forum_parent";
 211              $data = "<ul id='forum-root-$forum->forum_parent' class='list-block holder'>$data</ul>";
 212          }
 213      } else {
 214          $position = 1;
 215      }
 216  
 217      $x = new WP_Ajax_Response( array(
 218          'what' => 'forum',
 219          'id' => $forum_id,
 220          'data' => $data,
 221          'position' => $position,
 222          'supplemental' => array( 'name' => $forum->forum_name )
 223      ) );
 224      $x->send();
 225      break;
 226  
 227  case 'order-forums' :
 228      if ( !bb_current_user_can( 'manage_forums' ) )
 229          die('-1');
 230  
 231      bb_check_ajax_referer( $action );
 232  
 233      if ( !is_array($_POST['order']) )
 234          die('0');
 235  
 236      global $bbdb;
 237  
 238      $forums = array();
 239  
 240      bb_get_forums(); // cache
 241  
 242      foreach ( $_POST['order'] as $pos => $forum_id ) :
 243          $forum = $bbdb->escape_deep( get_object_vars( bb_get_forum( $forum_id ) ) );
 244          $forum['forum_order'] = $pos;
 245          $forums[(int) $forum_id] = $forum;
 246      endforeach;
 247  
 248      foreach ( $_POST['root'] as $root => $ids )
 249          foreach ( $ids as $forum_id )
 250              $forums[(int) $forum_id]['forum_parent'] = (int) $root;
 251  
 252      foreach ( $forums as $forum )
 253          bb_update_forum( $forum );
 254  
 255      die('1');
 256      break;
 257  
 258  default :
 259      do_action( 'bb_ajax_' . $_POST['action'] );
 260      break;
 261  endswitch;
 262  
 263  die('0');
 264  ?>


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