[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-templates/bp-nouveau/includes/friends/ -> ajax.php (source)

   1  <?php
   2  /**
   3   * Friends Ajax functions
   4   *
   5   * @since 3.0.0
   6   * @version 3.0.0
   7   */
   8  
   9  // Exit if accessed directly.
  10  defined( 'ABSPATH' ) || exit;
  11  
  12  add_action( 'admin_init', function() {
  13      $ajax_actions = array(
  14          array(
  15              'friends_remove_friend' => array(
  16                  'function' => 'bp_nouveau_ajax_addremove_friend',
  17                  'nopriv'   => false,
  18              ),
  19          ),
  20          array(
  21              'friends_add_friend' => array(
  22                  'function' => 'bp_nouveau_ajax_addremove_friend',
  23                  'nopriv'   => false,
  24              ),
  25          ),
  26          array(
  27              'friends_withdraw_friendship' => array(
  28                  'function' => 'bp_nouveau_ajax_addremove_friend',
  29                  'nopriv'   => false,
  30              ),
  31          ),
  32          array(
  33              'friends_accept_friendship' => array(
  34                  'function' => 'bp_nouveau_ajax_addremove_friend',
  35                  'nopriv'   => false,
  36              ),
  37          ),
  38          array(
  39              'friends_reject_friendship' => array(
  40                  'function' => 'bp_nouveau_ajax_addremove_friend',
  41                  'nopriv'   => false,
  42              ),
  43          ),
  44      );
  45  
  46      foreach ( $ajax_actions as $ajax_action ) {
  47          $action = key( $ajax_action );
  48  
  49          add_action( 'wp_ajax_' . $action, $ajax_action[ $action ]['function'] );
  50  
  51          if ( ! empty( $ajax_action[ $action ]['nopriv'] ) ) {
  52              add_action( 'wp_ajax_nopriv_' . $action, $ajax_action[ $action ]['function'] );
  53          }
  54      }
  55  }, 12 );
  56  
  57  /**
  58   * Friend/un-friend a user via a POST request.
  59   *
  60   * @since 3.0.0
  61   *
  62   * @return string HTML
  63   */
  64  function bp_nouveau_ajax_addremove_friend() {
  65      $response = array(
  66          'feedback' => sprintf(
  67              '<div class="bp-feedback error bp-ajax-message"><p>%s</p></div>',
  68              esc_html__( 'There was a problem performing this action. Please try again.', 'buddypress' )
  69          ),
  70      );
  71  
  72      // Bail if not a POST action.
  73      if ( ! bp_is_post_request() ) {
  74          wp_send_json_error( $response );
  75      }
  76  
  77      if ( empty( $_POST['nonce'] ) || empty( $_POST['item_id'] ) || ! bp_is_active( 'friends' ) ) {
  78          wp_send_json_error( $response );
  79      }
  80  
  81      // Use default nonce
  82      $nonce = $_POST['nonce'];
  83      $check = 'bp_nouveau_friends';
  84  
  85      // Use a specific one for actions needed it
  86      if ( ! empty( $_POST['_wpnonce'] ) && ! empty( $_POST['action'] ) ) {
  87          $nonce = $_POST['_wpnonce'];
  88          $check = $_POST['action'];
  89      }
  90  
  91      // Nonce check!
  92      if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, $check ) ) {
  93          wp_send_json_error( $response );
  94      }
  95  
  96      // Cast fid as an integer.
  97      $friend_id = (int) $_POST['item_id'];
  98  
  99      // Check if the user exists only when the Friend ID is not a Frienship ID.
 100      if ( isset( $_POST['action'] ) && $_POST['action'] !== 'friends_accept_friendship' && $_POST['action'] !== 'friends_reject_friendship' ) {
 101          $user = get_user_by( 'id', $friend_id );
 102          if ( ! $user ) {
 103              wp_send_json_error(
 104                  array(
 105                      'feedback' => sprintf(
 106                          '<div class="bp-feedback error">%s</div>',
 107                          esc_html__( 'No member found by that ID.', 'buddypress' )
 108                      ),
 109                  )
 110              );
 111          }
 112      }
 113  
 114      // In the 2 first cases the $friend_id is a friendship id.
 115      if ( ! empty( $_POST['action'] ) && 'friends_accept_friendship' === $_POST['action'] ) {
 116          if ( ! friends_accept_friendship( $friend_id ) ) {
 117              wp_send_json_error(
 118                  array(
 119                      'feedback' => sprintf(
 120                          '<div class="bp-feedback error">%s</div>',
 121                          esc_html__( 'There was a problem accepting that request. Please try again.', 'buddypress' )
 122                      ),
 123                  )
 124              );
 125          } else {
 126              wp_send_json_success(
 127                  array(
 128                      'feedback' => sprintf(
 129                          '<div class="bp-feedback success">%s</div>',
 130                          esc_html__( 'Friendship accepted.', 'buddypress' )
 131                      ),
 132                      'type'     => 'success',
 133                      'is_user'  => true,
 134                  )
 135              );
 136          }
 137  
 138      // Rejecting a friendship
 139      } elseif ( ! empty( $_POST['action'] ) && 'friends_reject_friendship' === $_POST['action'] ) {
 140          if ( ! friends_reject_friendship( $friend_id ) ) {
 141              wp_send_json_error(
 142                  array(
 143                      'feedback' => sprintf(
 144                          '<div class="bp-feedback error">%s</div>',
 145                          esc_html__( 'There was a problem rejecting that request. Please try again.', 'buddypress' )
 146                      ),
 147                  )
 148              );
 149          } else {
 150              wp_send_json_success(
 151                  array(
 152                      'feedback' => sprintf(
 153                          '<div class="bp-feedback success">%s</div>',
 154                          esc_html__( 'Friendship rejected.', 'buddypress' )
 155                      ),
 156                      'type'     => 'success',
 157                      'is_user'  => true,
 158                  )
 159              );
 160          }
 161  
 162      // Trying to cancel friendship.
 163      } elseif ( 'is_friend' === BP_Friends_Friendship::check_is_friend( bp_loggedin_user_id(), $friend_id ) ) {
 164          if ( ! friends_remove_friend( bp_loggedin_user_id(), $friend_id ) ) {
 165              $response['feedback'] = sprintf(
 166                  '<div class="bp-feedback error">%s</div>',
 167                  esc_html__( 'Friendship could not be cancelled.', 'buddypress' )
 168              );
 169  
 170              wp_send_json_error( $response );
 171          } else {
 172              $is_user = bp_is_my_profile();
 173  
 174              if ( ! $is_user ) {
 175                  $response = array( 'contents' => bp_get_add_friend_button( $friend_id ) );
 176              } else {
 177                  $response = array(
 178                      'feedback' => sprintf(
 179                          '<div class="bp-feedback success">%s</div>',
 180                          esc_html__( 'Friendship cancelled.', 'buddypress' )
 181                      ),
 182                      'type'     => 'success',
 183                      'is_user'  => $is_user,
 184                  );
 185              }
 186  
 187              wp_send_json_success( $response );
 188          }
 189  
 190      // Trying to request friendship.
 191      } elseif ( 'not_friends' === BP_Friends_Friendship::check_is_friend( bp_loggedin_user_id(), $friend_id ) ) {
 192          if ( ! friends_add_friend( bp_loggedin_user_id(), $friend_id ) ) {
 193              $response['feedback'] = sprintf(
 194                  '<div class="bp-feedback error">%s</div>',
 195                  esc_html__( 'Friendship could not be requested.', 'buddypress' )
 196              );
 197  
 198              wp_send_json_error( $response );
 199          } else {
 200              wp_send_json_success( array( 'contents' => bp_get_add_friend_button( $friend_id ) ) );
 201          }
 202  
 203      // Trying to cancel pending request.
 204      } elseif ( 'pending' === BP_Friends_Friendship::check_is_friend( bp_loggedin_user_id(), $friend_id ) ) {
 205          if ( friends_withdraw_friendship( bp_loggedin_user_id(), $friend_id ) ) {
 206              wp_send_json_success( array( 'contents' => bp_get_add_friend_button( $friend_id ) ) );
 207          } else {
 208              $response['feedback'] = sprintf(
 209                  '<div class="bp-feedback error">%s</div>',
 210                  esc_html__( 'Friendship request could not be cancelled.', 'buddypress' )
 211              );
 212  
 213              wp_send_json_error( $response );
 214          }
 215  
 216      // Request already pending.
 217      } else {
 218          $response['feedback'] = sprintf(
 219              '<div class="bp-feedback error">%s</div>',
 220              esc_html__( 'Request Pending', 'buddypress' )
 221          );
 222  
 223          wp_send_json_error( $response );
 224      }
 225  }


Generated: Fri Dec 13 01:01:36 2019 Cross-referenced by PHPXref 0.7.1