PluginProbe
BuddyPress / trunk
BuddyPress vtrunk
11.6.2 12.7.2 14.5.2 11.6.0 12.7.0 2.5.0-rc1 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.0-beta1 2.6.0-rc1 2.6.1 2.6.1.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.0-beta1 2.7.0-rc1 2.7.0-rc2 2.7.1 2.7.2 All 274 releases
buddypress / cli / src / group-invite.php

group-invite.php in BuddyPress trunk, at cli/src/group-invite.php

423 lines 10.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Buddypress\CLI\Command;
4
5 use WP_CLI;
6
7 /**
8 * Manage BuddyPress group invites.
9 *
10 * ## EXAMPLES
11 *
12 * # Invite a member to a group.
13 * $ wp bp group invite add --group-id=40 --user-id=10 --inviter-id=1331
14 * Success: Member invited to the group.
15 *
16 * # Invite a member to a group.
17 * $ wp bp group invite create --group-id=40 --user-id=user_slug --inviter-id=804
18 * Success: Member invited to the group.
19 *
20 * @since 1.5.0
21 */
22 class Group_Invite extends BuddyPressCommand {
23
24 /**
25 * Group ID Object Key
26 *
27 * @var string
28 */
29 protected $obj_id_key = 'group_id';
30
31 /**
32 * Group Object Type
33 *
34 * @var string
35 */
36 protected $obj_type = 'group';
37
38 /**
39 * Invite a member to a group.
40 *
41 * ## OPTIONS
42 *
43 * --group-id=<group>
44 * : Identifier for the group. Accepts either a slug or a numeric ID.
45 *
46 * --user-id=<user>
47 * : Identifier for the user. Accepts either a user_login or a numeric ID.
48 *
49 * --inviter-id=<user>
50 * : Identifier for the inviter. Accepts either a user_login or a numeric ID.
51 *
52 * [--message=<value>]
53 * : Message to send with the invitation.
54 *
55 * [--porcelain]
56 * : Return only the invitation id.
57 *
58 * [--silent]
59 * : Whether to silent the invite creation.
60 *
61 * ## EXAMPLES
62 *
63 * # Invite a member to a group.
64 * $ wp bp group invite add --group-id=40 --user-id=10 --inviter-id=1331
65 * Success: Member invited to the group.
66 *
67 * # Invite a member to a group.
68 * $ wp bp group invite create --group-id=40 --user-id=user_slug --inviter-id=804
69 * Success: Member invited to the group.
70 *
71 * @alias add
72 */
73 public function create( $args, $assoc_args ) {
74
75 // Bail early.
76 if ( $assoc_args['user-id'] === $assoc_args['inviter-id'] ) {
77 return;
78 }
79
80 $message = isset( $assoc_args['message'] ) ? $assoc_args['message'] : '';
81 $group_id = $this->get_group_id_from_identifier( $assoc_args['group-id'] );
82 $user = $this->get_user_id_from_identifier( $assoc_args['user-id'] );
83 $inviter = $this->get_user_id_from_identifier( $assoc_args['inviter-id'] );
84 $invite_id = groups_invite_user(
85 [
86 'user_id' => $user->ID,
87 'group_id' => $group_id,
88 'inviter_id' => $inviter->ID,
89 'content' => $message,
90 ]
91 );
92
93 if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'silent' ) ) {
94 return;
95 }
96
97 if ( ! $invite_id ) {
98 WP_CLI::error( 'Could not invite the member.' );
99 }
100
101 if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
102 WP_CLI::log( $invite_id );
103 } else {
104 WP_CLI::success( 'Member invited to the group.' );
105 }
106 }
107
108 /**
109 * Uninvite a user from a group.
110 *
111 * ## OPTIONS
112 *
113 * --group-id=<group>
114 * : Identifier for the group. Accepts either a slug or a numeric ID.
115 *
116 * --user-id=<user>
117 * : Identifier for the user. Accepts either a user_login or a numeric ID.
118 *
119 * ## EXAMPLES
120 *
121 * # Uninvite a user from a group.
122 * $ wp bp group invite uninvite --group-id=3 --user-id=10
123 * Success: User uninvited from the group.
124 *
125 * # Uninvite a user from a group.
126 * $ wp bp group invite uninvite --group-id=foo --user-id=admin
127 * Success: User uninvited from the group.
128 */
129 public function uninvite( $args, $assoc_args ) {
130 $group_id = $this->get_group_id_from_identifier( $assoc_args['group-id'] );
131 $user = $this->get_user_id_from_identifier( $assoc_args['user-id'] );
132
133 if ( groups_uninvite_user( $user->ID, $group_id ) ) {
134 WP_CLI::success( 'User uninvited from the group.' );
135 } else {
136 WP_CLI::error( 'Could not remove the user.' );
137 }
138 }
139
140 /**
141 * Get a list of invitations from a group.
142 *
143 * ## OPTIONS
144 *
145 * --group-id=<group>
146 * : Identifier for the group. Accepts either a slug or a numeric ID.
147 *
148 * --user-id=<user>
149 * : Identifier for the user. Accepts either a user_login or a numeric ID.
150 *
151 * [--count=<number>]
152 * : How many invitations to list.
153 * ---
154 * default: 50
155 * ---
156 *
157 * [--format=<format>]
158 * : Render output in a particular format.
159 * ---
160 * default: table
161 * options:
162 * - table
163 * - csv
164 * - ids
165 * - json
166 * - count
167 * - yaml
168 * ---
169 *
170 * ## EXAMPLE
171 *
172 * # Get a list of invitations from a group.
173 * $ wp bp group invite list --group-id=56 --user-id=30
174 *
175 * @subcommand list
176 */
177 public function list_( $args, $assoc_args ) {
178 $group_id = $this->get_group_id_from_identifier( $assoc_args['group-id'] );
179 $user = $this->get_user_id_from_identifier( $assoc_args['user-id'] );
180 $user_id = $user->ID;
181
182 if ( $group_id ) {
183 $invite_query = new \BP_Group_Member_Query( [
184 'is_confirmed' => false,
185 'group_id' => $group_id,
186 'per_page' => $assoc_args['count'],
187 ] );
188
189 $invites = $invite_query->results;
190
191 // Manually filter out user ID - this is not supported by the API.
192 if ( $user_id ) {
193 $user_invites = [];
194
195 foreach ( $invites as $invite ) {
196 if ( $user_id === $invite->user_id ) {
197 $user_invites[] = $invite;
198 }
199 }
200
201 $invites = $user_invites;
202 }
203
204 if ( empty( $invites ) ) {
205 WP_CLI::error( 'No invitations found.' );
206 }
207
208 if ( empty( $assoc_args['fields'] ) ) {
209 $fields = [];
210
211 if ( ! $user_id ) {
212 $fields[] = 'user_id';
213 }
214
215 $fields[] = 'inviter_id';
216 $fields[] = 'invite_sent';
217 $fields[] = 'date_modified';
218
219 $assoc_args['fields'] = $fields;
220 }
221 } else {
222 $invite_query = groups_get_invites_for_user( $user_id, $assoc_args['count'], 1 );
223 $invites = $invite_query['groups'];
224
225 if ( empty( $assoc_args['fields'] ) ) {
226 $assoc_args['fields'] = [ 'id', 'name', 'slug' ];
227 }
228 }
229
230 $formatter = $this->get_formatter( $assoc_args );
231 $formatter->display_items( 'ids' === $formatter->format ? wp_list_pluck( $invites, 'id' ) : $invites );
232 }
233
234 /**
235 * Generate group invitations.
236 *
237 * ## OPTIONS
238 *
239 * [--count=<number>]
240 * : How many group invitations to generate.
241 * ---
242 * default: 100
243 * ---
244 *
245 * [--user-id=<user>]
246 * : ID of the first user. Accepts either a user_login or a numeric ID.
247 *
248 * [--inviter-id=<user>]
249 * : ID for the inviter. Accepts either a user_login or a numeric ID.
250 *
251 * [--format=<format>]
252 * : Render output in a particular format.
253 * ---
254 * default: progress
255 * options:
256 * - progress
257 * - ids
258 * ---
259 *
260 * ## EXAMPLES
261 *
262 * # Generate random group invitations.
263 * $ wp bp group invite generate --count=50
264 * Generating group invitations 100% [======================] 0:00 / 0:00
265 *
266 * # Generate random group invitations with a specific user.
267 * $ wp bp group invite generate --inviter-id=121 --count=5
268 * Generating group invitations 100% [======================] 0:00 / 0:00
269 *
270 * # Generate 5 random group invitations and output only the IDs.
271 * $ wp bp group invite generate --count=5 --format=ids
272 * 70 71 72 73 74
273 */
274 public function generate( $args, $assoc_args ) {
275 $user_id = null;
276 $inviter_id = null;
277
278 if ( isset( $assoc_args['user-id'] ) ) {
279 $user = $this->get_user_id_from_identifier( $assoc_args['user-id'] );
280 $user_id = $user->ID;
281 }
282
283 if ( isset( $assoc_args['inviter-id'] ) ) {
284 $user_2 = $this->get_user_id_from_identifier( $assoc_args['inviter-id'] );
285 $inviter_id = $user_2->ID;
286 }
287
288 $this->generate_callback(
289 'Generating group invitations',
290 $assoc_args,
291 function ( $assoc_args, $format ) use ( $user_id, $inviter_id ) {
292 $random_group = \BP_Groups_Group::get_random( 1, 1 );
293
294 if ( ! $user_id ) {
295 $user_id = $this->get_random_user_id();
296 }
297
298 if ( ! $inviter_id ) {
299 $inviter_id = $this->get_random_user_id();
300 }
301
302 $params = [
303 'user-id' => $user_id,
304 'group-id' => $random_group['groups'][0]->slug,
305 'inviter-id' => $inviter_id,
306 ];
307
308 if ( 'ids' === $format ) {
309 $params['porcelain'] = true;
310 } else {
311 $params['silent'] = true;
312 }
313
314 return $this->create( [], $params );
315 }
316 );
317 }
318
319 /**
320 * Accept a group invitation.
321 *
322 * ## OPTIONS
323 *
324 * --group-id=<group>
325 * : Identifier for the group. Accepts either a slug or a numeric ID.
326 *
327 * --user-id=<user>
328 * : Identifier for the user. Accepts either a user_login or a numeric ID.
329 *
330 * ## EXAMPLES
331 *
332 * # Accept a group invitation.
333 * $ wp bp group invite accept --group-id=3 --user-id=10
334 * Success: User is now a "member" of the group.
335 *
336 * # Accept a group invitation.
337 * $ wp bp group invite accept --group-id=foo --user-id=admin
338 * Success: User is now a "member" of the group.
339 */
340 public function accept( $args, $assoc_args ) {
341 $group_id = $this->get_group_id_from_identifier( $assoc_args['group-id'] );
342 $user = $this->get_user_id_from_identifier( $assoc_args['user-id'] );
343
344 if ( groups_accept_invite( $user->ID, $group_id ) ) {
345 WP_CLI::success( 'User is now a "member" of the group.' );
346 } else {
347 WP_CLI::error( 'Could not accept user invitation to the group.' );
348 }
349 }
350
351 /**
352 * Reject a group invitation.
353 *
354 * ## OPTIONS
355 *
356 * --group-id=<group>
357 * : Identifier for the group. Accepts either a slug or a numeric ID.
358 *
359 * --user-id=<user>
360 * : Identifier for the user. Accepts either a user_login or a numeric ID.
361 *
362 * ## EXAMPLES
363 *
364 * # Reject a group invitation.
365 * $ wp bp group invite reject --group-id=3 --user-id=10
366 * Success: Member invitation rejected.
367 *
368 * # Reject a group invitation.
369 * $ wp bp group invite reject --group-id=foo --user-id=admin
370 * Success: Member invitation rejected.
371 */
372 public function reject( $args, $assoc_args ) {
373 $group_id = $this->get_group_id_from_identifier( $assoc_args['group-id'] );
374 $user = $this->get_user_id_from_identifier( $assoc_args['user-id'] );
375
376 if ( groups_reject_invite( $user->ID, $group_id ) ) {
377 WP_CLI::success( 'Member invitation rejected.' );
378 } else {
379 WP_CLI::error( 'Could not reject member invitation.' );
380 }
381 }
382
383 /**
384 * Delete a group invitation.
385 *
386 * ## OPTIONS
387 *
388 * --group-id=<group>
389 * : Identifier for the group. Accepts either a slug or a numeric ID.
390 *
391 * --user-id=<user>
392 * : Identifier for the user. Accepts either a user_login or a numeric ID.
393 *
394 * [--yes]
395 * : Answer yes to the confirmation message.
396 *
397 * ## EXAMPLES
398 *
399 * # Delete a group invitation.
400 * $ wp bp group invite delete --group-id=3 --user-id=10 --yes
401 * Success: Group invitation deleted.
402 *
403 * # Delete a group invitation.
404 * $ wp bp group invite delete --group-id=foo --user-id=admin --yes
405 * Success: Group invitation deleted.
406 *
407 * @alias delete
408 * @alias trash
409 */
410 public function delete( $args, $assoc_args ) {
411 WP_CLI::confirm( 'Are you sure you want to delete this group invitation?', $assoc_args );
412
413 $group_id = $this->get_group_id_from_identifier( $assoc_args['group-id'] );
414 $user = $this->get_user_id_from_identifier( $assoc_args['user-id'] );
415
416 if ( groups_delete_invite( $user->ID, $group_id ) ) {
417 WP_CLI::success( 'Group invitation deleted.' );
418 } else {
419 WP_CLI::error( 'Could not delete group invitation.' );
420 }
421 }
422 }
423