Completed
Push — master ( 2fd399...b41af7 )
by
unknown
08:39
created

ajax.php ➔ bp_nouveau_ajax_send_group_invites()   F

Complexity

Conditions 11
Paths 256

Size

Total Lines 81
Code Lines 42

Duplication

Lines 12
Ratio 14.81 %

Importance

Changes 0
Metric Value
cc 11
eloc 42
nc 256
nop 0
dl 12
loc 81
rs 3.8181
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Groups Ajax functions
4
 *
5
 * @since 1.0.0
6
 */
7
8
// Exit if accessed directly.
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * Registers groups AJAX actions.
13
 */
14
bp_nouveau_register_ajax_actions( array(
15
	array( 'groups_filter'                      => array( 'function' => 'bp_nouveau_ajax_object_template_loader', 'nopriv' => true  ) ),
16
	array( 'groups_join_group'                  => array( 'function' => 'bp_nouveau_ajax_joinleave_group',        'nopriv' => false ) ),
17
	array( 'groups_leave_group'                 => array( 'function' => 'bp_nouveau_ajax_joinleave_group',        'nopriv' => false ) ),
18
	array( 'groups_accept_invite'               => array( 'function' => 'bp_nouveau_ajax_joinleave_group',        'nopriv' => false ) ),
19
	array( 'groups_reject_invite'               => array( 'function' => 'bp_nouveau_ajax_joinleave_group',        'nopriv' => false ) ),
20
	array( 'groups_request_membership'          => array( 'function' => 'bp_nouveau_ajax_joinleave_group',        'nopriv' => false ) ),
21
	array( 'groups_get_group_potential_invites' => array( 'function' => 'bp_nouveau_ajax_get_users_to_invite',    'nopriv' => false ) ),
22
	array( 'groups_send_group_invites'          => array( 'function' => 'bp_nouveau_ajax_send_group_invites',     'nopriv' => false ) ),
23
	array( 'groups_delete_group_invite'         => array( 'function' => 'bp_nouveau_ajax_remove_group_invite',    'nopriv' => false ) ),
24
) );
25
26
/**
27
 * Join or leave a group when clicking the "join/leave" button via a POST request.
28
 *
29
 * @since 1.0.0
30
 *
31
 * @return string HTML
0 ignored issues
show
Documentation introduced by Michael Beckwith
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
32
 */
33
function bp_nouveau_ajax_joinleave_group() {
34
	$response = array(
35
		'feedback' => sprintf(
36
			'<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
37
			esc_html__( 'There was a problem performing this action. Please try again.', 'buddypress' )
38
		),
39
	);
40
41
	// Bail if not a POST action.
42
	if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) || empty( $_POST['action'] ) ) {
43
		wp_send_json_error( $response );
44
	}
45
46 View Code Duplication
	if ( empty( $_POST['nonce'] ) || empty( $_POST['item_id'] ) || ! bp_is_active( 'groups' ) ) {
47
		wp_send_json_error( $response );
48
	}
49
50
	// Use default nonce
51
	$nonce = $_POST['nonce'];
52
	$check = 'bp_nouveau_groups';
53
54
	// Use a specific one for actions needed it
55 View Code Duplication
	if ( ! empty( $_POST['_wpnonce'] ) && ! empty( $_POST['action'] ) ) {
56
		$nonce = $_POST['_wpnonce'];
57
		$check = $_POST['action'];
58
	}
59
60
	// Nonce check!
61
	if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, $check ) ) {
62
		wp_send_json_error( $response );
63
	}
64
65
	// Cast gid as integer.
66
	$group_id = (int) $_POST['item_id'];
67
68
	$errors = array(
69
		'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' ) ),
70
		'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' ) ),
71
	);
72
73
	if ( groups_is_user_banned( bp_loggedin_user_id(), $group_id ) ) {
74
		$response['feedback'] = $errors['cannot'];
75
76
		wp_send_json_error( $response );
77
	}
78
79
	// Validate and get the group
80
	$group = groups_get_group( array( 'group_id' => $group_id ) );
0 ignored issues
show
Documentation introduced by Hugo Ashmore
array('group_id' => $group_id) is of type array<string,integer,{"group_id":"integer"}>, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81
82
	if ( empty( $group->id ) ) {
83
		wp_send_json_error( $response );
84
	}
85
86
	// Manage all button's possible actions here.
87
	switch ( $_POST['action'] ) {
88
89
		case 'groups_accept_invite':
90
			if ( ! groups_accept_invite( bp_loggedin_user_id(), $group_id ) ) {
91
				$response = array(
92
					'feedback' => sprintf(
93
						'<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
94
						esc_html__( 'Group invite could not be accepted.', 'buddypress' )
95
					),
96
					'type'     => 'error',
97
				);
98
99
			} else {
100
				groups_record_activity(
101
					array(
102
						'type'    => 'joined_group',
103
						'item_id' => $group->id,
104
					)
105
				);
106
107
				// User is now a member of the group
108
				$group->is_member = '1';
0 ignored issues
show
Documentation introduced by Hugo Ashmore
The property $is_member is declared protected in BP_Groups_Group. Since you implemented __set(), maybe consider adding a @property or @property-write annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation Bug introduced by Hugo Ashmore
The property $is_member was declared of type boolean, but '1' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
109
110
				$response = array(
111
					'feedback' => sprintf(
112
						'<div class="bp-feedback success"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
113
						esc_html__( 'Group invite accepted.', 'buddypress' )
114
					),
115
					'type'     => 'success',
116
					'is_user'  => bp_is_user(),
117
					'contents' => bp_get_group_join_button( $group ),
118
					'is_group' => bp_is_group(),
119
				);
120
			}
121
			break;
122
123
		case 'groups_reject_invite':
124
			if ( ! groups_reject_invite( bp_loggedin_user_id(), $group_id ) ) {
125
				$response = array(
126
					'feedback' => sprintf(
127
						'<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
128
						esc_html__( 'Group invite could not be rejected', 'buddypress' )
129
					),
130
					'type'     => 'error',
131
				);
132 View Code Duplication
			} else {
133
				$response = array(
134
					'feedback' => sprintf(
135
						'<div class="bp-feedback success"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
136
						esc_html__( 'Group invite rejected', 'buddypress' )
137
					),
138
					'type'     => 'success',
139
					'is_user'  => bp_is_user(),
140
				);
141
			}
142
			break;
143
144
		case 'groups_join_group':
145
			if ( groups_is_user_member( bp_loggedin_user_id(), $group->id ) ) {
146
				$response = array(
147
					'feedback' => $errors['member'],
148
					'type'     => 'error',
149
				);
150
			} elseif ( 'public' !== $group->status ) {
151
				$response = array(
152
					'feedback' => $errors['cannot'],
153
					'type'     => 'error',
154
				);
155
			} elseif ( ! groups_join_group( $group->id ) ) {
156
				$response = array(
157
					'feedback' => sprintf(
158
						'<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
159
						esc_html__( 'Error joining this group.', 'buddypress' )
160
					),
161
					'type'     => 'error',
162
				);
163 View Code Duplication
			} else {
164
				// User is now a member of the group
165
				$group->is_member = '1';
0 ignored issues
show
Documentation introduced by Hugo Ashmore
The property $is_member is declared protected in BP_Groups_Group. Since you implemented __set(), maybe consider adding a @property or @property-write annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
166
167
				$response = array(
168
					'contents' => bp_get_group_join_button( $group ),
169
					'is_group' => bp_is_group(),
170
					'type'     => 'success',
171
				);
172
			}
173
			break;
174
175
			case 'groups_request_membership' :
176
				if ( ! groups_send_membership_request( bp_loggedin_user_id(), $group->id ) ) {
177
					$response = array(
178
						'feedback' => sprintf(
179
							'<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
180
							esc_html__( 'Error requesting membership.', 'buddypress' )
181
						),
182
						'type'     => 'error',
183
					);
184 View Code Duplication
				} else {
185
					// Request is pending
186
					$group->is_pending = '1';
0 ignored issues
show
Documentation introduced by Hugo Ashmore
The property $is_pending is declared protected in BP_Groups_Group. Since you implemented __set(), maybe consider adding a @property or @property-write annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation Bug introduced by Hugo Ashmore
The property $is_pending was declared of type boolean, but '1' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
187
188
					$response = array(
189
						'contents' => bp_get_group_join_button( $group ),
190
						'is_group' => bp_is_group(),
191
						'type'     => 'success',
192
					);
193
				}
194
				break;
195
196
			case 'groups_leave_group' :
197
				if (  groups_leave_group( $group->id ) ) {
198
					$response = array(
199
						'feedback' => sprintf(
200
							'<div class="bp-feedback error"><span class="bp-icon" aria-hidden="true"></span><p>%s</p></div>',
201
							esc_html__( 'Error leaving group.', 'buddypress' )
202
						),
203
						'type'     => 'error',
204
					);
205
				} else {
206
					// User is no more a member of the group
207
					$group->is_member = '0';
0 ignored issues
show
Documentation introduced by Hugo Ashmore
The property $is_member is declared protected in BP_Groups_Group. Since you implemented __set(), maybe consider adding a @property or @property-write annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation Bug introduced by Hugo Ashmore
The property $is_member was declared of type boolean, but '0' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
208
					$bp               = buddypress();
209
210
					/**
211
					 * When inside the group or in the loggedin user's group memberships screen
212
					 * we need to reload the page.
213
					 */
214
					$bp_is_group = bp_is_group() || ( bp_is_user_groups() && bp_is_my_profile() );
215
216
					$response = array(
217
						'contents' => bp_get_group_join_button( $group ),
218
						'is_group' => $bp_is_group,
219
						'type'     => 'success',
220
					);
221
222
					// Reset the message if not in a Group or in a loggedin user's group memberships one!
223
					if ( ! $bp_is_group && isset( $bp->template_message ) && isset( $bp->template_message_type ) ) {
224
						unset( $bp->template_message, $bp->template_message_type );
225
226
						@setcookie( 'bp-message', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
0 ignored issues
show
Coding Style introduced by Hugo Ashmore
Silencing errors is discouraged
Loading history...
introduced by Hugo Ashmore
Due to using Batcache, server side based client related logic will not work, use JS instead.
Loading history...
227
						@setcookie( 'bp-message-type', false, time() - 1000, COOKIEPATH, COOKIE_DOMAIN, is_ssl() );
0 ignored issues
show
Coding Style introduced by Hugo Ashmore
Silencing errors is discouraged
Loading history...
introduced by Hugo Ashmore
Due to using Batcache, server side based client related logic will not work, use JS instead.
Loading history...
228
					}
229
				}
230
				break;
231
	}
232
233
	if ( 'error' === $response['type'] ) {
234
		wp_send_json_error( $response );
235
	}
236
237
	wp_send_json_success( $response );
238
}
239
240
/**
241
 * @since 1.0.0
242
 */
243
function bp_nouveau_ajax_get_users_to_invite() {
244
	$bp = buddypress();
245
246
	$response = array(
247
		'feedback' => bp_nouveau_message_markup_wrapper( esc_html__( 'There was a problem performing this action. Please try again.', 'buddypress' ), 'error' ),
248
		'type'     => 'error',
249
	);
250
251
	if ( empty( $_POST['nonce'] ) ) {
252
		wp_send_json_error( $response );
253
	}
254
255
	// Use default nonce
256
	$nonce = $_POST['nonce'];
257
	$check = 'bp_nouveau_groups';
258
259
	// Use a specific one for actions needed it
260 View Code Duplication
	if ( ! empty( $_POST['_wpnonce'] ) && ! empty( $_POST['action'] ) ) {
261
		$nonce = $_POST['_wpnonce'];
262
		$check = $_POST['action'];
263
	}
264
265
	// Nonce check!
266
	if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, $check ) ) {
267
		wp_send_json_error( $response );
268
	}
269
270
	$request = wp_parse_args( $_POST, array(
271
		'scope' => 'members',
272
	) );
273
274
	$bp->groups->invites_scope = 'members';
275
	$message = bp_nouveau_message_markup_wrapper( __( 'You can invite members using the + button, a new nav will appear to let you send your invites', 'buddypress' ), 'info' );
276
277
	if ( 'friends' === $request['scope'] ) {
278
		$request['user_id'] = bp_loggedin_user_id();
279
		$bp->groups->invites_scope = 'friends';
280
		$message = bp_nouveau_message_markup_wrapper( __( 'You can invite friends using the + button, a new nav will appear to let you send your invites', 'buddypress' ), 'info' );
281
	}
282
283
	if ( 'invited' === $request['scope'] ) {
284
285 View Code Duplication
		if ( ! bp_group_has_invites( array( 'user_id' => 'any' ) ) ) {
0 ignored issues
show
Documentation introduced by Hugo Ashmore
array('user_id' => 'any') is of type array<string,string,{"user_id":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
286
			wp_send_json_error( array(
287
				'feedback' => bp_nouveau_message_markup_wrapper( __( 'No pending invites found.', 'buddypress' ), 'info' ),
288
				'type'     => 'info',
289
			) );
290
		}
291
292
		$request['is_confirmed'] = false;
293
		$bp->groups->invites_scope = 'invited';
294
		$message = bp_nouveau_message_markup_wrapper( __( 'You can view all the group\'s pending invites from this screen.', 'buddypress' ), 'info' );
295
	}
296
297
	$potential_invites = bp_nouveau_get_group_potential_invites( $request );
298
299
	if ( empty( $potential_invites->users ) ) {
300
		$error = array(
301
			'feedback' => bp_nouveau_message_markup_wrapper( __( 'No members were found, try another filter.', 'buddypress' ), 'info' ),
302
			'type'     => 'info',
303
		);
304
305
		if ( 'friends' === $bp->groups->invites_scope ) {
306
			$error = array(
307
				'feedback' => bp_nouveau_message_markup_wrapper( __( 'All your friends are already members of this group or already received an invite to join this group or requested to join it.', 'buddypress' ), 'info' ),
308
				'type'     => 'info',
309
			);
310
311 View Code Duplication
			if ( 0 === (int) bp_get_total_friend_count( bp_loggedin_user_id() ) ) {
312
				$error = array(
313
					'feedback' => bp_nouveau_message_markup_wrapper( __( 'You have no friends!', 'buddypress' ), 'info' ),
314
					'type'     => 'info',
315
				);
316
			}
317
		}
318
319
		unset( $bp->groups->invites_scope );
320
321
		wp_send_json_error( $error );
322
	}
323
324
	$potential_invites->users = array_map( 'bp_nouveau_prepare_group_potential_invites_for_js', array_values( $potential_invites->users ) );
325
	$potential_invites->users = array_filter( $potential_invites->users );
326
327
	// Set a message to explain use of the current scope
328
	$potential_invites->feedback = $message;
329
330
	unset( $bp->groups->invites_scope );
331
332
	wp_send_json_success( $potential_invites );
333
}
334
335
/**
336
 * @since 1.0.0
337
 */
338
function bp_nouveau_ajax_send_group_invites() {
339
	$bp = buddypress();
340
341
	$response = array(
342
		'feedback' => bp_nouveau_message_markup_wrapper( __( 'Invites could not be sent, please try again.', 'buddypress' ), 'error' ),
343
	);
344
345
	// Verify nonce
346 View Code Duplication
	if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'groups_send_invites' ) ) {
347
		wp_send_json_error(
348
			array(
349
				'feedback' => bp_nouveau_message_markup_wrapper( __( 'Invites could not be sent, please try again.', 'buddypress' ), 'error' ),
350
				'type'     => 'error',
351
			)
352
		);
353
	}
354
355
	$group_id = bp_get_current_group_id();
356
357
	if ( bp_is_group_create() && ! empty( $_POST['group_id'] ) ) {
358
		$group_id = (int) $_POST['group_id'];
359
	}
360
361 View Code Duplication
	if ( ! bp_groups_user_can_send_invites( $group_id ) ) {
362
		wp_send_json_error(
363
			array(
364
				'feedback' => bp_nouveau_message_markup_wrapper( __( 'You are not allowed to send invites for this group.', 'buddypress' ), 'error' ),
365
				'type'     => 'error',
366
			)
367
		);
368
	}
369
370
	if ( empty( $_POST['users'] ) ) {
371
		wp_send_json_error( $response );
372
	}
373
374
	// For feedback
375
	$invited = array();
376
377
	foreach ( (array) $_POST['users'] as $user_id ) {
378
		$invited[ $user_id ] = groups_invite_user(
379
			array(
380
				'user_id'  => $user_id,
381
				'group_id' => $group_id,
382
			)
383
		);
384
	}
385
386
	if ( ! empty( $_POST['message'] ) ) {
387
		$bp->groups->invites_message = wp_kses( wp_unslash( $_POST['message'] ), array() );
388
389
		add_filter( 'groups_notification_group_invites_message', 'bp_nouveau_groups_invites_custom_message', 10, 1 );
390
	}
391
392
	// Send the invites.
393
	groups_send_invites( bp_loggedin_user_id(), $group_id );
394
395
	if ( ! empty( $_POST['message'] ) ) {
396
		unset( $bp->groups->invites_message );
397
398
		remove_filter( 'groups_notification_group_invites_message', 'bp_nouveau_groups_invites_custom_message', 10, 1 );
399
	}
400
401
	if ( array_search( false, $invited ) ) {
402
		$errors = array_keys( $invited, false );
403
404
		wp_send_json_error(
405
			array(
406
				'feedback' => bp_nouveau_message_markup_wrapper( sprintf( __( 'Invites failed for %d user(s).', 'buddypress' ), count( $errors ) ), 'error' ),
407
				'users'    => $errors,
408
				'type'     => 'error',
409
			)
410
		);
411
	}
412
413
	wp_send_json_success(
414
		array(
415
			'feedback' => bp_nouveau_message_markup_wrapper( __( 'Invites sent.', 'buddypress' ), 'success' ),
416
		)
417
	);
418
}
419
420
/**
421
 * @since 1.0.0
422
 */
423
function bp_nouveau_ajax_remove_group_invite() {
424
	$user_id  = $_POST['user'];
425
	$group_id = bp_get_current_group_id();
426
427
	// Verify nonce
428 View Code Duplication
	if ( empty( $_POST['_wpnonce'] ) || ! wp_verify_nonce( $_POST['_wpnonce'], 'groups_invite_uninvite_user' ) ) {
429
		wp_send_json_error(
430
			array(
431
				'feedback' => bp_nouveau_message_markup_wrapper( __( 'Invites could not be removed, please try again.', 'buddypress' ), 'error' ),
432
			)
433
		);
434
	}
435
436
	if ( BP_Groups_Member::check_for_membership_request( $user_id, $group_id ) ) {
437
		wp_send_json_error(
438
			array(
439
				'feedback' => bp_nouveau_message_markup_wrapper( __( 'Too late, the user is now a member of the group.', 'buddypress' ), 'warning' ),
440
				'code'     => 1,
441
			)
442
		);
443
	}
444
445
	// Remove the unsent invitation.
446 View Code Duplication
	if ( ! groups_uninvite_user( $user_id, $group_id ) ) {
447
		wp_send_json_error(
448
			array(
449
				'feedback' => bp_nouveau_message_markup_wrapper( __( 'Removing the invite for the user failed.', 'buddypress' ), 'error' ),
450
				'code'     => 0,
451
			)
452
		);
453
	}
454
455
	wp_send_json_success(
456
		array(
457
			'feedback'    => bp_nouveau_message_markup_wrapper( __( 'No more pending invites for the group.', 'buddypress' ), 'info' ),
458
			'has_invites' => bp_group_has_invites( array( 'user_id' => 'any' ) ),
0 ignored issues
show
Documentation introduced by Hugo Ashmore
array('user_id' => 'any') is of type array<string,string,{"user_id":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
459
		)
460
	);
461
}
462