[ Index ] |
PHP Cross Reference of BuddyPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Groups Ajax functions 4 * 5 * @since 3.0.0 6 * @version 6.3.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_displayed_user_id(), $group_id ) ) { 101 wp_send_json_error( $response ); 102 } 103 104 if ( ! groups_accept_invite( bp_displayed_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_displayed_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( [ 'user_id' => bp_loggedin_user_id(), 'group_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 if ( ! bp_is_group_create() && ! bp_groups_user_can_send_invites( bp_get_current_group_id(), bp_loggedin_user_id() ) ) { 287 $invite_status = bp_group_get_invite_status( bp_get_current_group_id() ); 288 if ( 'admins' === $invite_status ) { 289 $message = __( 'Inviting members to join this group is restricted to Group Administrators.', 'buddypress' ); 290 } else { 291 $message = __( 'Inviting members to join this group is restricted to Group Moderators and Administrators.', 'buddypress' ); 292 } 293 294 wp_send_json_error( 295 array( 296 'feedback' => $message, 297 'type' => 'error', 298 ) 299 ); 300 } 301 302 $request = bp_parse_args( 303 $_POST, 304 array( 305 'scope' => 'members', 306 ), 307 'nouveau_ajax_get_users_to_invite' 308 ); 309 310 $bp->groups->invites_scope = 'members'; 311 $message = __( 'Select members to invite by clicking the + button. Once you\'ve made your selection, use the "Send Invites" navigation item to continue.', 'buddypress' ); 312 313 if ( 'friends' === $request['scope'] ) { 314 $request['user_id'] = bp_loggedin_user_id(); 315 $bp->groups->invites_scope = 'friends'; 316 $message = __( 'Select friends to invite by clicking the + button. Once you\'ve made your selection, use the "Send Invites" navigation item to continue.', 'buddypress' ); 317 } 318 319 if ( 'invited' === $request['scope'] ) { 320 321 if ( ! bp_group_has_invites( array( 'user_id' => 'any' ) ) ) { 322 wp_send_json_error( array( 323 'feedback' => __( 'No pending group invitations found.', 'buddypress' ), 324 'type' => 'info', 325 ) ); 326 } 327 328 $request['is_confirmed'] = false; 329 $bp->groups->invites_scope = 'invited'; 330 $message = __( 'You can view the group\'s pending invitations from this screen.', 'buddypress' ); 331 } 332 333 $potential_invites = bp_nouveau_get_group_potential_invites( $request ); 334 335 if ( empty( $potential_invites->users ) ) { 336 $error = array( 337 'feedback' => __( 'No members were found. Try another filter.', 'buddypress' ), 338 'type' => 'info', 339 ); 340 341 if ( 'friends' === $bp->groups->invites_scope ) { 342 $error = array( 343 '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' ), 344 'type' => 'info', 345 ); 346 347 if ( 0 === (int) bp_get_total_friend_count( bp_loggedin_user_id() ) ) { 348 $error = array( 349 'feedback' => __( 'You have no friends!', 'buddypress' ), 350 'type' => 'info', 351 ); 352 } else { 353 $error = array( 354 'feedback' => __( 'No friends were found. Try another filter.', 'buddypress' ), 355 'type' => 'info', 356 ); 357 } 358 } 359 360 unset( $bp->groups->invites_scope ); 361 362 wp_send_json_error( $error ); 363 } 364 365 $potential_invites->users = array_map( 'bp_nouveau_prepare_group_potential_invites_for_js', array_values( $potential_invites->users ) ); 366 $potential_invites->users = array_filter( $potential_invites->users ); 367 368 // Set a message to explain use of the current scope 369 $potential_invites->feedback = $message; 370 371 unset( $bp->groups->invites_scope ); 372 373 wp_send_json_success( $potential_invites ); 374 } 375 376 /** 377 * @since 3.0.0 378 */ 379 function bp_nouveau_ajax_send_group_invites() { 380 $bp = buddypress(); 381 382 $response = array( 383 'feedback' => __( 'Invites could not be sent. Please try again.', 'buddypress' ), 384 'type' => 'error', 385 ); 386 387 // Verify nonce 388 if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'groups_send_invites' ) ) { 389 wp_send_json_error( $response ); 390 } 391 392 $group_id = bp_get_current_group_id(); 393 394 if ( bp_is_group_create() && ! empty( $_POST['group_id'] ) ) { 395 $group_id = (int) $_POST['group_id']; 396 } 397 398 if ( ! bp_groups_user_can_send_invites( $group_id ) ) { 399 $response['feedback'] = __( 'You are not allowed to send invitations for this group.', 'buddypress' ); 400 wp_send_json_error( $response ); 401 } 402 403 if ( empty( $_POST['users'] ) ) { 404 wp_send_json_error( $response ); 405 } 406 407 // For feedback 408 $invited = array(); 409 $is_friends_active = bp_is_active( 'friends' ); 410 411 foreach ( (array) $_POST['users'] as $user_id ) { 412 $user_id = (int) $user_id; 413 414 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 ) ) { 415 continue; 416 } else { 417 $invited[ $user_id ] = groups_invite_user( 418 array( 419 'user_id' => $user_id, 420 'group_id' => $group_id, 421 'content' => $_POST['message'], 422 ) 423 ); 424 } 425 } 426 427 if ( ! $invited ) { 428 wp_send_json_error( $response ); 429 } 430 431 // Send the invites. 432 groups_send_invites( array( 'group_id' => $group_id ) ); 433 434 if ( array_search( false, $invited ) ) { 435 $errors = array_keys( $invited, false ); 436 437 $error_count = count( $errors ); 438 $error_message = sprintf( 439 /* translators: count of users affected */ 440 _n( 441 'Invitation failed for %s user.', 442 'Invitation failed for %s users.', 443 $error_count, 'buddypress' 444 ), 445 number_format_i18n( $error_count ) 446 ); 447 448 wp_send_json_error( 449 array( 450 'feedback' => $error_message, 451 'users' => $errors, 452 'type' => 'error', 453 ) 454 ); 455 } 456 457 wp_send_json_success( 458 array( 459 'feedback' => __( 'Invitations sent.', 'buddypress' ), 460 'type' => 'success', 461 ) 462 ); 463 } 464 465 /** 466 * @since 3.0.0 467 */ 468 function bp_nouveau_ajax_remove_group_invite() { 469 $user_id = (int) $_POST['user']; 470 $group_id = bp_get_current_group_id(); 471 472 $response = array( 473 'feedback' => __( 'Group invitation could not be removed.', 'buddypress' ), 474 'type' => 'error', 475 ); 476 477 // Verify nonce 478 if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'groups_invite_uninvite_user' ) ) { 479 wp_send_json_error( $response ); 480 } 481 482 // Verify that a sent invite exists. 483 $inviter_ids = groups_get_invites( array( 484 'user_id' => $user_id, 485 'item_id' => $group_id, 486 'invite_sent' => 'sent', 487 'fields' => 'inviter_ids' 488 ) ); 489 490 if ( empty( $inviter_ids ) ) { 491 wp_send_json_error( $response ); 492 } 493 494 // Is the current user the inviter? 495 $inviter_id = in_array( bp_loggedin_user_id(), $inviter_ids, true ) ? bp_loggedin_user_id() : false; 496 497 // A site moderator, group admin or the inviting user should be able to remove an invitation. 498 if ( ! bp_is_item_admin() && ! $inviter_id ) { 499 wp_send_json_error( $response ); 500 } 501 502 if ( groups_is_user_member( $user_id, $group_id ) ) { 503 wp_send_json_error( 504 array( 505 'feedback' => __( 'The member is already a member of the group.', 'buddypress' ), 506 'type' => 'warning', 507 'code' => 1, 508 ) 509 ); 510 } 511 512 // Remove the invitation. 513 if ( ! groups_uninvite_user( $user_id, $group_id, $inviter_id ) ) { 514 wp_send_json_error( 515 array( 516 'feedback' => __( 'Group invitation could not be removed.', 'buddypress' ), 517 'type' => 'error', 518 'code' => 0, 519 ) 520 ); 521 } 522 523 wp_send_json_success( 524 array( 525 'feedback' => __( 'There are no more pending invitations for the group.', 'buddypress' ), 526 'type' => 'info', 527 'has_invites' => bp_group_has_invites( array( 'user_id' => 'any' ) ), 528 ) 529 ); 530 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Oct 16 01:00:54 2024 | Cross-referenced by PHPXref 0.7.1 |