[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Groups Ajax functions
   4   *
   5   * @since 3.0.0
   6   * @version 4.4.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( 'groups_filter'                      => array( 'function' => 'bp_nouveau_ajax_object_template_loader', 'nopriv' => true  ) ),
  15          array( 'groups_join_group'                  => array( 'function' => 'bp_nouveau_ajax_joinleave_group', 'nopriv' => false ) ),
  16          array( 'groups_leave_group'                 => array( 'function' => 'bp_nouveau_ajax_joinleave_group', 'nopriv' => false ) ),
  17          array( 'groups_accept_invite'               => array( 'function' => 'bp_nouveau_ajax_joinleave_group', 'nopriv' => false ) ),
  18          array( 'groups_reject_invite'               => array( 'function' => 'bp_nouveau_ajax_joinleave_group', 'nopriv' => false ) ),
  19          array( 'groups_request_membership'          => array( 'function' => 'bp_nouveau_ajax_joinleave_group', 'nopriv' => false ) ),
  20          array( 'groups_get_group_potential_invites' => array( 'function' => 'bp_nouveau_ajax_get_users_to_invite', 'nopriv' => false ) ),
  21          array( 'groups_send_group_invites'          => array( 'function' => 'bp_nouveau_ajax_send_group_invites', 'nopriv' => false ) ),
  22          array( 'groups_delete_group_invite'         => array( 'function' => 'bp_nouveau_ajax_remove_group_invite', 'nopriv' => false ) ),
  23      );
  24  
  25      foreach ( $ajax_actions as $ajax_action ) {
  26          $action = key( $ajax_action );
  27  
  28          add_action( 'wp_ajax_' . $action, $ajax_action[ $action ]['function'] );
  29  
  30          if ( ! empty( $ajax_action[ $action ]['nopriv'] ) ) {
  31              add_action( 'wp_ajax_nopriv_' . $action, $ajax_action[ $action ]['function'] );
  32          }
  33      }
  34  }, 12 );
  35  
  36  /**
  37   * Join or leave a group when clicking the "join/leave" button via a POST request.
  38   *
  39   * @since 3.0.0
  40   *
  41   * @return string HTML
  42   */
  43  function bp_nouveau_ajax_joinleave_group() {
  44      $response = array(
  45          'feedback' => sprintf(
  46              '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
  47              esc_html__( 'There was a problem performing this action. Please try again.', 'buddypress' )
  48          ),
  49      );
  50  
  51      // Bail if not a POST action.
  52      if ( ! bp_is_post_request() || empty( $_POST['action'] ) ) {
  53          wp_send_json_error( $response );
  54      }
  55  
  56      if ( empty( $_POST['nonce'] ) || empty( $_POST['item_id'] ) || ! bp_is_active( 'groups' ) ) {
  57          wp_send_json_error( $response );
  58      }
  59  
  60      // Use default nonce
  61      $nonce = $_POST['nonce'];
  62      $check = 'bp_nouveau_groups';
  63  
  64      // Use a specific one for actions needed it
  65      if ( ! empty( $_POST['_wpnonce'] ) && ! empty( $_POST['action'] ) ) {
  66          $nonce = $_POST['_wpnonce'];
  67          $check = $_POST['action'];
  68      }
  69  
  70      // Nonce check!
  71      if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, $check ) ) {
  72          wp_send_json_error( $response );
  73      }
  74  
  75      // Cast gid as integer.
  76      $group_id = (int) $_POST['item_id'];
  77  
  78      $errors = array(
  79          'cannot' => sprintf( '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>', esc_html__( 'You cannot join this group.', 'buddypress' ) ),
  80          'member' => sprintf( '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>', esc_html__( 'You are already a member of the group.', 'buddypress' ) ),
  81      );
  82  
  83      if ( groups_is_user_banned( bp_loggedin_user_id(), $group_id ) ) {
  84          $response['feedback'] = $errors['cannot'];
  85  
  86          wp_send_json_error( $response );
  87      }
  88  
  89      // Validate and get the group
  90      $group = groups_get_group( array( 'group_id' => $group_id ) );
  91  
  92      if ( empty( $group->id ) ) {
  93          wp_send_json_error( $response );
  94      }
  95  
  96      // Manage all button's possible actions here.
  97      switch ( $_POST['action'] ) {
  98  
  99          case 'groups_accept_invite':
 100              if ( ! groups_check_user_has_invite( bp_loggedin_user_id(), $group_id ) ) {
 101                  wp_send_json_error( $response );
 102              }
 103  
 104              if ( ! groups_accept_invite( bp_loggedin_user_id(), $group_id ) ) {
 105                  $response = array(
 106                      'feedback' => sprintf(
 107                          '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
 108                          esc_html__( 'Group invitation could not be accepted.', 'buddypress' )
 109                      ),
 110                      'type'     => 'error',
 111                  );
 112  
 113              } else {
 114                  if ( bp_is_active( 'activity' ) ) {
 115                      groups_record_activity(
 116                          array(
 117                              'type'    => 'joined_group',
 118                              'item_id' => $group->id,
 119                          )
 120                      );
 121                  }
 122  
 123                  // User is now a member of the group
 124                  $group->is_member = '1';
 125  
 126                  $response = array(
 127                      'feedback' => sprintf(
 128                          '<div class="bp-feedback success"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
 129                          esc_html__( 'Group invite accepted.', 'buddypress' )
 130                      ),
 131                      'type'     => 'success',
 132                      'is_user'  => bp_is_user(),
 133                      'contents' => bp_get_group_join_button( $group ),
 134                      'is_group' => bp_is_group(),
 135                  );
 136              }
 137              break;
 138  
 139          case 'groups_reject_invite':
 140              if ( ! groups_reject_invite( bp_loggedin_user_id(), $group_id ) ) {
 141                  $response = array(
 142                      'feedback' => sprintf(
 143                          '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
 144                          esc_html__( 'Group invite could not be rejected', 'buddypress' )
 145                      ),
 146                      'type'     => 'error',
 147                  );
 148              } else {
 149                  $response = array(
 150                      'feedback' => sprintf(
 151                          '<div class="bp-feedback success"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
 152                          esc_html__( 'Group invite rejected', 'buddypress' )
 153                      ),
 154                      'type'     => 'success',
 155                      'is_user'  => bp_is_user(),
 156                  );
 157              }
 158              break;
 159  
 160          case 'groups_join_group':
 161              if ( groups_is_user_member( bp_loggedin_user_id(), $group->id ) ) {
 162                  $response = array(
 163                      'feedback' => $errors['member'],
 164                      'type'     => 'error',
 165                  );
 166              } elseif ( 'public' !== $group->status ) {
 167                  $response = array(
 168                      'feedback' => $errors['cannot'],
 169                      'type'     => 'error',
 170                  );
 171              } elseif ( ! groups_join_group( $group->id ) ) {
 172                  $response = array(
 173                      'feedback' => sprintf(
 174                          '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
 175                          esc_html__( 'Error joining this group.', 'buddypress' )
 176                      ),
 177                      'type'     => 'error',
 178                  );
 179              } else {
 180                  // User is now a member of the group
 181                  $group->is_member = '1';
 182  
 183                  $response = array(
 184                      'contents' => bp_get_group_join_button( $group ),
 185                      'is_group' => bp_is_group(),
 186                      'type'     => 'success',
 187                  );
 188              }
 189              break;
 190  
 191              case 'groups_request_membership' :
 192                  if ( ! groups_send_membership_request( bp_loggedin_user_id(), $group->id ) ) {
 193                      $response = array(
 194                          'feedback' => sprintf(
 195                              '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
 196                              esc_html__( 'Error requesting membership.', 'buddypress' )
 197                          ),
 198                          'type'     => 'error',
 199                      );
 200                  } else {
 201                      // Request is pending
 202                      $group->is_pending = '1';
 203  
 204                      $response = array(
 205                          'contents' => bp_get_group_join_button( $group ),
 206                          'is_group' => bp_is_group(),
 207                          'type'     => 'success',
 208                      );
 209                  }
 210                  break;
 211  
 212              case 'groups_leave_group' :
 213                  if (  ! groups_leave_group( $group->id ) ) {
 214                      $response = array(
 215                          'feedback' => sprintf(
 216                              '<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
 217                              esc_html__( 'Error leaving group.', 'buddypress' )
 218                          ),
 219                          'type'     => 'error',
 220                      );
 221                  } else {
 222                      // User is no more a member of the group
 223                      $group->is_member = '0';
 224                      $bp               = buddypress();
 225  
 226                      /**
 227                       * When inside the group or in the loggedin user's group memberships screen
 228                       * we need to reload the page.
 229                       */
 230                      $bp_is_group = bp_is_group() || ( bp_is_user_groups() && bp_is_my_profile() );
 231  
 232                      $response = array(
 233                          'contents' => bp_get_group_join_button( $group ),
 234                          'is_group' => $bp_is_group,
 235                          'type'     => 'success',
 236                      );
 237  
 238                      // Reset the message if not in a Group or in a loggedin user's group memberships one!
 239                      if ( ! $bp_is_group && isset( $bp->template_message ) && isset( $bp->template_message_type ) ) {
 240                          unset( $bp->template_message, $bp->template_message_type );
 241  
 242                          @setcookie( 'bp-message', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
 243                          @setcookie( 'bp-message-type', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
 244                      }
 245                  }
 246                  break;
 247      }
 248  
 249      if ( 'error' === $response['type'] ) {
 250          wp_send_json_error( $response );
 251      }
 252  
 253      wp_send_json_success( $response );
 254  }
 255  
 256  /**
 257   * @since 3.0.0
 258   */
 259  function bp_nouveau_ajax_get_users_to_invite() {
 260      $bp = buddypress();
 261  
 262      $response = array(
 263          'feedback' => __( 'There was a problem performing this action. Please try again.', 'buddypress' ),
 264          'type'     => 'error',
 265      );
 266  
 267      if ( empty( $_POST['nonce'] ) ) {
 268          wp_send_json_error( $response );
 269      }
 270  
 271      // Use default nonce
 272      $nonce = $_POST['nonce'];
 273      $check = 'bp_nouveau_groups';
 274  
 275      // Use a specific one for actions needed it
 276      if ( ! empty( $_POST['_wpnonce'] ) && ! empty( $_POST['action'] ) ) {
 277          $nonce = $_POST['_wpnonce'];
 278          $check = $_POST['action'];
 279      }
 280  
 281      // Nonce check!
 282      if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, $check ) ) {
 283          wp_send_json_error( $response );
 284      }
 285  
 286      $request = bp_parse_args(
 287          $_POST,
 288          array(
 289              'scope' => 'members',
 290          ),
 291          'nouveau_ajax_get_users_to_invite'
 292      );
 293  
 294      $bp->groups->invites_scope = 'members';
 295      $message = __( 'Select members to invite by clicking the + button. Once you\'ve made your selection, use the "Send Invites" navigation item to continue.', 'buddypress' );
 296  
 297      if ( 'friends' === $request['scope'] ) {
 298          $request['user_id'] = bp_loggedin_user_id();
 299          $bp->groups->invites_scope = 'friends';
 300          $message = __( 'Select friends to invite by clicking the + button. Once you\'ve made your selection, use the "Send Invites" navigation item to continue.', 'buddypress' );
 301      }
 302  
 303      if ( 'invited' === $request['scope'] ) {
 304  
 305          if ( ! bp_group_has_invites( array( 'user_id' => 'any' ) ) ) {
 306              wp_send_json_error( array(
 307                  'feedback' => __( 'No pending group invitations found.', 'buddypress' ),
 308                  'type'     => 'info',
 309              ) );
 310          }
 311  
 312          $request['is_confirmed'] = false;
 313          $bp->groups->invites_scope = 'invited';
 314          $message = __( 'You can view the group\'s pending invitations from this screen.', 'buddypress' );
 315      }
 316  
 317      $potential_invites = bp_nouveau_get_group_potential_invites( $request );
 318  
 319      if ( empty( $potential_invites->users ) ) {
 320          $error = array(
 321              'feedback' => __( 'No members were found. Try another filter.', 'buddypress' ),
 322              'type'     => 'info',
 323          );
 324  
 325          if ( 'friends' === $bp->groups->invites_scope ) {
 326              $error = array(
 327                  'feedback' => __( 'All your friends are already members of this group, or have already received an invite to join this group, or have requested to join it.', 'buddypress' ),
 328                  'type'     => 'info',
 329              );
 330  
 331              if ( 0 === (int) bp_get_total_friend_count( bp_loggedin_user_id() ) ) {
 332                  $error = array(
 333                      'feedback' => __( 'You have no friends!', 'buddypress' ),
 334                      'type'     => 'info',
 335                  );
 336              }
 337          }
 338  
 339          unset( $bp->groups->invites_scope );
 340  
 341          wp_send_json_error( $error );
 342      }
 343  
 344      $potential_invites->users = array_map( 'bp_nouveau_prepare_group_potential_invites_for_js', array_values( $potential_invites->users ) );
 345      $potential_invites->users = array_filter( $potential_invites->users );
 346  
 347      // Set a message to explain use of the current scope
 348      $potential_invites->feedback = $message;
 349  
 350      unset( $bp->groups->invites_scope );
 351  
 352      wp_send_json_success( $potential_invites );
 353  }
 354  
 355  /**
 356   * @since 3.0.0
 357   */
 358  function bp_nouveau_ajax_send_group_invites() {
 359      $bp = buddypress();
 360  
 361      $response = array(
 362          'feedback' => __( 'Invites could not be sent. Please try again.', 'buddypress' ),
 363          'type'     => 'error',
 364      );
 365  
 366      // Verify nonce
 367      if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'groups_send_invites' ) ) {
 368          wp_send_json_error( $response );
 369      }
 370  
 371      $group_id = bp_get_current_group_id();
 372  
 373      if ( bp_is_group_create() && ! empty( $_POST['group_id'] ) ) {
 374          $group_id = (int) $_POST['group_id'];
 375      }
 376  
 377      if ( ! bp_groups_user_can_send_invites( $group_id ) ) {
 378          $response['feedback'] = __( 'You are not allowed to send invitations for this group.', 'buddypress' );
 379          wp_send_json_error( $response );
 380      }
 381  
 382      if ( empty( $_POST['users'] ) ) {
 383          wp_send_json_error( $response );
 384      }
 385  
 386      // For feedback
 387      $invited           = array();
 388      $is_friends_active = bp_is_active( 'friends' );
 389  
 390      foreach ( (array) $_POST['users'] as $user_id ) {
 391          $user_id = (int) $user_id;
 392  
 393          if ( $is_friends_active && bp_nouveau_groups_get_group_invites_setting( $user_id ) && 'is_friend' !== BP_Friends_Friendship::check_is_friend( bp_loggedin_user_id(), $user_id ) ) {
 394              continue;
 395          } else {
 396              $invited[ $user_id ] = groups_invite_user(
 397                  array(
 398                      'user_id'  => $user_id,
 399                      'group_id' => $group_id,
 400                      'content'  => $_POST['message'],
 401                  )
 402              );
 403          }
 404      }
 405  
 406      if ( ! $invited ) {
 407          wp_send_json_error( $response );
 408      }
 409  
 410      if ( ! empty( $_POST['message'] ) ) {
 411          $bp->groups->invites_message = wp_kses( wp_unslash( $_POST['message'] ), array() );
 412  
 413          add_filter( 'groups_notification_group_invites_message', 'bp_nouveau_groups_invites_custom_message', 10, 1 );
 414      }
 415  
 416      // Send the invites.
 417      groups_send_invites( array(    'group_id' => $group_id ) );
 418  
 419      if ( ! empty( $_POST['message'] ) ) {
 420          unset( $bp->groups->invites_message );
 421  
 422          remove_filter( 'groups_notification_group_invites_message', 'bp_nouveau_groups_invites_custom_message', 10, 1 );
 423      }
 424  
 425      if ( array_search( false, $invited ) ) {
 426          $errors = array_keys( $invited, false );
 427  
 428          $error_count   = count( $errors );
 429          $error_message = sprintf(
 430              /* translators: count of users affected */
 431              _n(
 432                  'Invitation failed for %s user.',
 433                  'Invitation failed for %s users.',
 434                  $error_count, 'buddypress'
 435              ),
 436              number_format_i18n( $error_count )
 437          );
 438  
 439          wp_send_json_error(
 440              array(
 441                  'feedback' => $error_message,
 442                  'users'    => $errors,
 443                  'type'     => 'error',
 444              )
 445          );
 446      }
 447  
 448      wp_send_json_success(
 449          array(
 450              'feedback' => __( 'Invitations sent.', 'buddypress' ),
 451              'type'     => 'success',
 452          )
 453      );
 454  }
 455  
 456  /**
 457   * @since 3.0.0
 458   */
 459  function bp_nouveau_ajax_remove_group_invite() {
 460      $user_id  = (int) $_POST['user'];
 461      $group_id = bp_get_current_group_id();
 462  
 463      $response = array(
 464          'feedback' => __( 'Group invitation could not be removed.', 'buddypress' ),
 465          'type'     => 'error',
 466      );
 467  
 468      // Verify nonce
 469      if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'groups_invite_uninvite_user' ) ) {
 470          wp_send_json_error( $response );
 471      }
 472  
 473      // Verify pending invite.
 474      $invites_args = array(
 475          'is_confirmed' => false,
 476          'is_banned'    => null,
 477          'is_admin'     => null,
 478          'is_mod'       => null,
 479      );
 480      $invites = bp_get_user_groups( $user_id, $invites_args );
 481      if ( empty( $invites ) ) {
 482          wp_send_json_error( $response );
 483      }
 484  
 485      if ( ! groups_is_user_admin( bp_loggedin_user_id(), $group_id ) ) {
 486          wp_send_json_error( $response );
 487      }
 488  
 489      if ( BP_Groups_Member::check_for_membership_request( $user_id, $group_id ) ) {
 490          wp_send_json_error(
 491              array(
 492                  'feedback' => __( 'The member is already a member of the group.', 'buddypress' ),
 493                  'type'     => 'warning',
 494                  'code'     => 1,
 495              )
 496          );
 497      }
 498  
 499      // Remove the unsent invitation.
 500      if ( ! groups_uninvite_user( $user_id, $group_id ) ) {
 501          wp_send_json_error(
 502              array(
 503                  'feedback' => __( 'Group invitation could not be removed.', 'buddypress' ),
 504                  'type'     => 'error',
 505                  'code'     => 0,
 506              )
 507          );
 508      }
 509  
 510      wp_send_json_success(
 511          array(
 512              'feedback'    => __( 'There are no more pending invitations for the group.', 'buddypress' ),
 513              'type'        => 'info',
 514              'has_invites' => bp_group_has_invites( array( 'user_id' => 'any' ) ),
 515          )
 516      );
 517  }


Generated: Fri Oct 18 01:01:36 2019 Cross-referenced by PHPXref 0.7.1