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 / messages.php

messages.php in BuddyPress trunk, at cli/src/messages.php

665 lines 16.0 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 Messages.
9 *
10 * ## EXAMPLES
11 *
12 * # Create message.
13 * $ wp bp message create --from=user1 --to=user2 --subject="Message Title" --content="We are ready"
14 * Success: Message successfully created.
15 *
16 * # Delete a thread.
17 * $ wp bp message delete-thread 564 5465465 456456 --user-id=user_login --yes
18 * Success: Thread successfully deleted.
19 *
20 * @since 1.6.0
21 */
22 class Messages extends BuddyPressCommand {
23
24 /**
25 * Dependency check for this CLI command.
26 */
27 public static function check_dependencies() {
28 parent::check_dependencies();
29
30 if ( ! bp_is_active( 'messages' ) ) {
31 WP_CLI::error( 'The Message component is not active.' );
32 }
33 }
34
35 /**
36 * Object fields.
37 *
38 * @var array
39 */
40 protected $obj_fields = [
41 'id',
42 'subject',
43 'message',
44 'thread_id',
45 'sender_id',
46 'date_sent',
47 ];
48
49 /**
50 * Add a message.
51 *
52 * ## OPTIONS
53 *
54 * --from=<user>
55 * : Identifier for the user. Accepts either a user_login or a numeric ID.
56 *
57 * [--to=<user>]
58 * : Identifier for the recipient. To is not required when thread id is set.
59 * Accepts either a user_login or a numeric ID.
60 *
61 * --subject=<subject>
62 * : Subject of the message.
63 *
64 * --content=<content>
65 * : Content of the message.
66 *
67 * [--thread-id=<thread-id>]
68 * : Thread ID.
69 *
70 * [--date-sent=<date-sent>]
71 * : GMT timestamp, in Y-m-d h:i:s format.
72 *
73 * [--silent]
74 * : Whether to silent the message creation.
75 *
76 * [--porcelain]
77 * : Return the thread id of the message.
78 *
79 * ## EXAMPLES
80 *
81 * # Add a message.
82 * $ wp bp message add --from=user1 --to=user2 --subject="Message Title" --content="We are ready"
83 * Success: Message successfully created.
84 *
85 * # Create a message.
86 * $ wp bp message create --from=545 --to=313 --subject="Another Message Title" --content="Message OK"
87 * Success: Message successfully created.
88 *
89 * @alias add
90 */
91 public function create( $args, $assoc_args ) {
92 $r = wp_parse_args(
93 $assoc_args,
94 [
95 'to' => '',
96 'thread-id' => false,
97 'date-sent' => bp_core_current_time(),
98 ]
99 );
100
101 $user = $this->get_user_id_from_identifier( $assoc_args['from'] );
102
103 // To is not required when thread id is set.
104 if ( ! empty( $r['to'] ) ) {
105 $recipient = $this->get_user_id_from_identifier( $r['to'] );
106 }
107
108 // Existing thread recipients will be assumed.
109 $recipient = ! empty( $r['thread-id'] ) ? [] : [ $recipient->ID ];
110
111 $thread_id = messages_new_message(
112 [
113 'sender_id' => $user->ID,
114 'thread_id' => $r['thread-id'],
115 'recipients' => $recipient,
116 'subject' => $assoc_args['subject'],
117 'content' => $assoc_args['content'],
118 'date_sent' => $r['date-sent'],
119 ]
120 );
121
122 // Silent it before it errors.
123 if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'silent' ) ) {
124 return;
125 }
126
127 if ( ! is_numeric( $thread_id ) ) {
128 WP_CLI::error( 'Could not add a message.' );
129 }
130
131 if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
132 WP_CLI::log( $thread_id );
133 } else {
134 WP_CLI::success( 'Message successfully created.' );
135 }
136 }
137
138 /**
139 * Delete thread(s) for a given user.
140 *
141 * ## OPTIONS
142 *
143 * <thread-id>...
144 * : Thread ID(s).
145 *
146 * --user-id=<user>
147 * : Identifier for the user. Accepts either a user_login or a numeric ID.
148 *
149 * [--yes]
150 * : Answer yes to the confirmation message.
151 *
152 * ## EXAMPLES
153 *
154 * $ wp bp message delete-thread 500 687867 --user-id=40
155 * Success: Thread successfully deleted.
156 *
157 * $ wp bp message delete-thread 564 5465465 456456 --user-id=user_logon --yes
158 * Success: Thread successfully deleted.
159 *
160 * @alias delete-thread
161 * @alias remove-thread
162 */
163 public function delete_thread( $args, $assoc_args ) {
164 $user = $this->get_user_id_from_identifier( $assoc_args['user-id'] );
165
166 WP_CLI::confirm( 'Are you sure you want to delete this thread(s)?', $assoc_args );
167
168 parent::_delete(
169 $args,
170 $assoc_args,
171 function ( $thread_id ) use ( $user ) {
172 if ( messages_delete_thread( $thread_id, $user->ID ) ) {
173 return [ 'success', 'Thread successfully deleted.' ];
174 }
175
176 return [ 'error', 'Could not delete the thread.' ];
177 }
178 );
179 }
180
181 /**
182 * Get a message.
183 *
184 * ## OPTIONS
185 *
186 * <message-id>
187 * : Identifier for the message.
188 *
189 * [--fields=<fields>]
190 * : Limit the output to specific fields.
191 *
192 * [--format=<format>]
193 * : Render output in a particular format.
194 * ---
195 * default: table
196 * options:
197 * - table
198 * - json
199 * - csv
200 * - yaml
201 * ---
202 *
203 * ## EXAMPLES
204 *
205 * # Get message by ID.
206 * $ wp bp message get 5465
207 *
208 * # Get message with a string
209 * $ wp bp message get invalid-id
210 * Error: Please provide a numeric message ID.
211 *
212 * @alias see
213 */
214 public function get( $args, $assoc_args ) {
215 $message_id = $args[0];
216
217 if ( ! is_numeric( $message_id ) ) {
218 WP_CLI::error( 'Please provide a numeric message ID.' );
219 }
220
221 $message = new \BP_Messages_Message( $message_id );
222
223 if ( empty( $message->id ) ) {
224 WP_CLI::error( 'No message found.' );
225 }
226
227 $message_arr = get_object_vars( $message );
228
229 if ( empty( $assoc_args['fields'] ) ) {
230 $assoc_args['fields'] = array_keys( $message_arr );
231 }
232
233 $this->get_formatter( $assoc_args )->display_item( $message_arr );
234 }
235
236 /**
237 * Get a list of messages for a specific user.
238 *
239 * ## OPTIONS
240 *
241 * --user-id=<user>
242 * : Identifier for the user. Accepts either a user_login or a numeric ID.
243 *
244 * [--<field>=<value>]
245 * : One or more parameters to pass. See \BP_Messages_Box_Template()
246 *
247 * [--fields=<fields>]
248 * : Fields to display.
249 *
250 * [--box=<box>]
251 * : Box of the message.
252 * ---
253 * default: sentbox
254 * options:
255 * - sentbox
256 * - inbox
257 * - notices
258 * ---
259 *
260 * [--type=<type>]
261 * : Type of the message.
262 * ---
263 * default: all
264 * options:
265 * - unread
266 * - read
267 * - all
268 * ---
269 *
270 * [--count=<number>]
271 * : How many messages to list.
272 * ---
273 * default: 50
274 * ---
275 *
276 * [--format=<format>]
277 * : Render output in a particular format.
278 * ---
279 * default: table
280 * options:
281 * - table
282 * - ids
283 * - count
284 * - csv
285 * - json
286 * - yaml
287 * ---
288 *
289 * ## AVAILABLE FIELDS
290 *
291 * These fields will be displayed by default for each message:
292 *
293 * * id
294 * * subject
295 * * message
296 * * thread_id
297 * * sender_id
298 * * date_sent
299 *
300 * ## EXAMPLES
301 *
302 * # Get a list of messages for a specific user.
303 * $ wp bp message list --user-id=544 --format=count
304 * 10
305 *
306 * # Get a list of messages for a specific user and output only the IDs.
307 * $ wp bp message list --user-id=user_login --count=3 --format=ids
308 * 5454 45454 4545 465465
309 *
310 * # Get a list of messages.
311 * # wp bp message list --user-id=1 --count=2
312 * +----+----------------------+--------------------------+-----------+-----------+---------------------+
313 * | id | subject | message | thread_id | sender_id | date_sent |
314 * +----+----------------------+--------------------------+-----------+-----------+---------------------+
315 * | 35 | Another Thread | <p>Another thread</p> | 2 | 1 | 2022-10-27 16:29:29 |
316 * | 37 | Message Subject - #0 | Here is some random text | 2 | 7 | 2022-10-27 19:06:54 |
317 * +----+----------------------+--------------------------+-----------+-----------+---------------------+
318 *
319 * @subcommand list
320 */
321 public function list_( $args, $assoc_args ) {
322 $formatter = $this->get_formatter( $assoc_args );
323 $user = $this->get_user_id_from_identifier( $assoc_args['user-id'] );
324 $inbox = new \BP_Messages_Box_Template(
325 [
326 'user_id' => $user->ID,
327 'box' => $assoc_args['box'],
328 'type' => $assoc_args['type'],
329 'messages_page' => 1,
330 'messages_per_page' => $assoc_args['count'],
331 ]
332 );
333
334 if ( ! $inbox->has_threads() ) {
335 WP_CLI::error( 'No messages found.' );
336 }
337
338 $messages = $inbox->threads[0]->messages;
339
340 $formatter->display_items( 'ids' === $formatter->format ? wp_list_pluck( $messages, 'id' ) : $messages );
341 }
342
343 /**
344 * Generate random messages.
345 *
346 * ## OPTIONS
347 *
348 * [--count=<number>]
349 * : How many messages to generate.
350 * ---
351 * default: 100
352 * ---
353 *
354 * [--from=<user>]
355 * : Identifier for the user. Accepts either a user_login or a numeric ID.
356 *
357 * [--to=<user>]
358 * : Identifier for the recipient. To is not required when thread id is set.
359 * Accepts either a user_login or a numeric ID.
360 *
361 * [--thread-id=<thread-id>]
362 * : Thread ID to generate messages against.
363 * ---
364 *
365 * [--format=<format>]
366 * : Render output in a particular format.
367 * ---
368 * default: progress
369 * options:
370 * - progress
371 * - ids
372 * ---
373 *
374 * ## EXAMPLES
375 *
376 * # Generate messages against a thread with a specific user.
377 * $ wp bp message generate --from=1 --to=2 --thread-id=6465 --count=30
378 * Generating messages 100% [======================] 0:00 / 0:00
379 *
380 * # Generate messages against a thread.
381 * $ wp bp message generate --thread-id=6465 --count=10
382 * Generating messages 100% [======================] 0:00 / 0:00
383 *
384 * # Generate 5 messages against a thread and output only the IDs.
385 * $ wp bp message generate --thread-id=5665456 --count=5 --format=ids
386 * 70 71 72 73 74
387 */
388 public function generate( $args, $assoc_args ) {
389 $from_user_id = null;
390 $to_user_id = null;
391
392 if ( isset( $assoc_args['from'] ) ) {
393 $user = $this->get_user_id_from_identifier( $assoc_args['from'] );
394 $from_user_id = $user->ID;
395 }
396
397 if ( isset( $assoc_args['to'] ) ) {
398 $user = $this->get_user_id_from_identifier( $assoc_args['to'] );
399 $to_user_id = $user->ID;
400 }
401
402 $this->generate_callback(
403 'Generating messages',
404 $assoc_args,
405 function ( $assoc_args, $format ) use ( $from_user_id, $to_user_id ) {
406
407 if ( ! $from_user_id ) {
408 $from_user_id = $this->get_random_user_id();
409 }
410
411 if ( ! $to_user_id ) {
412 $to_user_id = $this->get_random_user_id();
413 }
414
415 $params = [
416 'from' => $from_user_id,
417 'to' => $to_user_id,
418 'subject' => sprintf( 'Message Subject - #%d', wp_rand() ),
419 'content' => $this->generate_random_text(),
420 'thread-id' => empty( $assoc_args['thread-id'] ) ? false : $assoc_args['thread-id'],
421 ];
422
423 if ( 'ids' === $format ) {
424 $params['porcelain'] = true;
425 } else {
426 $params['silent'] = true;
427 }
428
429 return $this->create( [], $params );
430 }
431 );
432 }
433
434 /**
435 * Star a message.
436 *
437 * ## OPTIONS
438 *
439 * <message-id>
440 * : Message ID to star.
441 *
442 * --user-id=<user>
443 * : User that is starring the message. Accepts either a user_login or a numeric ID.
444 *
445 * ## EXAMPLE
446 *
447 * # Star a message.
448 * $ wp bp message star 3543 --user-id=user_login
449 * Success: Message was successfully starred.
450 */
451 public function star( $args, $assoc_args ) {
452 $message_id = $args[0];
453
454 if ( ! is_numeric( $message_id ) ) {
455 WP_CLI::error( 'Please provide a numeric message ID.' );
456 }
457
458 $user = $this->get_user_id_from_identifier( $assoc_args['user-id'] );
459 $user_id = $user->ID;
460
461 if ( bp_messages_is_message_starred( $message_id, $user_id ) ) {
462 WP_CLI::error( 'The message is already starred.' );
463 }
464
465 $star_args = [
466 'action' => 'star',
467 'message_id' => $message_id,
468 'user_id' => $user_id,
469 ];
470
471 if ( bp_messages_star_set_action( $star_args ) ) {
472 WP_CLI::success( 'Message was successfully starred.' );
473 } else {
474 WP_CLI::error( 'Message was not starred.' );
475 }
476 }
477
478 /**
479 * Unstar a message.
480 *
481 * ## OPTIONS
482 *
483 * <message-id>
484 * : Message ID to unstar.
485 *
486 * --user-id=<user>
487 * : User that is unstarring the message. Accepts either a user_login or a numeric ID.
488 *
489 * ## EXAMPLE
490 *
491 * # Unstar a message.
492 * $ wp bp message unstar 212 --user-id=another_user_login
493 * Success: Message was successfully unstarred.
494 */
495 public function unstar( $args, $assoc_args ) {
496 $message_id = $args[0];
497
498 if ( ! is_numeric( $message_id ) ) {
499 WP_CLI::error( 'Please provide a numeric message ID.' );
500 }
501
502 $user = $this->get_user_id_from_identifier( $assoc_args['user-id'] );
503 $user_id = $user->ID;
504
505 // Check if the message is starred first.
506 if ( ! bp_messages_is_message_starred( $message_id, $user_id ) ) {
507 WP_CLI::error( 'You need to star a message first before unstarring it.' );
508 }
509
510 $star_args = [
511 'action' => 'unstar',
512 'message_id' => $message_id,
513 'user_id' => $user_id,
514 ];
515
516 if ( bp_messages_star_set_action( $star_args ) ) {
517 WP_CLI::success( 'Message was successfully unstarred.' );
518 } else {
519 WP_CLI::error( 'Message was not unstarred.' );
520 }
521 }
522
523 /**
524 * Star a thread.
525 *
526 * ## OPTIONS
527 *
528 * <thread-id>
529 * : Thread ID to star.
530 *
531 * --user-id=<user>
532 * : User that is starring the thread. Accepts either a user_login or a numeric ID.
533 *
534 * ## EXAMPLE
535 *
536 * # Star a thread.
537 * $ wp bp message star-thread 212 --user-id=another_user_login
538 * Success: Thread was successfully starred.
539 *
540 * @alias star-thread
541 */
542 public function star_thread( $args, $assoc_args ) {
543 $thread_id = $args[0];
544
545 if ( ! is_numeric( $thread_id ) ) {
546 WP_CLI::error( 'Please provide a numeric thread ID.' );
547 }
548
549 $user = $this->get_user_id_from_identifier( $assoc_args['user-id'] );
550
551 // Check if it is a valid thread.
552 if ( ! messages_is_valid_thread( $thread_id ) ) {
553 WP_CLI::error( 'This is not a valid thread ID.' );
554 }
555
556 // Check if the user has access to this thread.
557 $id = messages_check_thread_access( $thread_id, $user->ID );
558
559 if ( ! is_numeric( $id ) ) {
560 WP_CLI::error( 'User has no access to this thread.' );
561 }
562
563 $star_args = [
564 'action' => 'star',
565 'thread_id' => $thread_id,
566 'user_id' => $user->ID,
567 'bulk' => true,
568 ];
569
570 if ( bp_messages_star_set_action( $star_args ) ) {
571 WP_CLI::success( 'Thread was successfully starred.' );
572 } else {
573 WP_CLI::error( 'Something wrong while trying to star the thread.' );
574 }
575 }
576
577 /**
578 * Unstar a thread.
579 *
580 * ## OPTIONS
581 *
582 * <thread-id>
583 * : Thread ID to unstar.
584 *
585 * --user-id=<user>
586 * : User that is unstarring the thread. Accepts either a user_login or a numeric ID.
587 *
588 * ## EXAMPLE
589 *
590 * # Unstar a thread.
591 * $ wp bp message unstar-thread 212 --user-id=another_user_login
592 * Success: Thread was successfully unstarred.
593 *
594 * @alias unstar-thread
595 */
596 public function unstar_thread( $args, $assoc_args ) {
597 $thread_id = $args[0];
598
599 if ( ! is_numeric( $thread_id ) ) {
600 WP_CLI::error( 'Please provide a numeric thread ID.' );
601 }
602
603 $user = $this->get_user_id_from_identifier( $assoc_args['user-id'] );
604
605 // Check if it is a valid thread.
606 if ( ! messages_is_valid_thread( $thread_id ) ) {
607 WP_CLI::error( 'This is not a valid thread ID.' );
608 }
609
610 // Check if the user has access to this thread.
611 $id = messages_check_thread_access( $thread_id, $user->ID );
612
613 if ( ! is_numeric( $id ) ) {
614 WP_CLI::error( 'User has no access to this thread.' );
615 }
616
617 $star_args = [
618 'action' => 'unstar',
619 'thread_id' => $thread_id,
620 'user_id' => $user->ID,
621 'bulk' => true,
622 ];
623
624 if ( bp_messages_star_set_action( $star_args ) ) {
625 WP_CLI::success( 'Thread was successfully unstarred.' );
626 } else {
627 WP_CLI::error( 'Something wrong while trying to unstar the thread.' );
628 }
629 }
630
631 /**
632 * Send a notice.
633 *
634 * ## OPTIONS
635 *
636 * --subject=<subject>
637 * : Subject of the notice/message.
638 *
639 * --content=<content>
640 * : Content of the notice.
641 *
642 * ## EXAMPLE
643 *
644 * # Send a notice.
645 * $ wp bp message send-notice --subject="Important notice" --content="We need to improve"
646 * Success: Notice was successfully sent.
647 *
648 * @alias send-notice
649 */
650 public function send_notice( $args, $assoc_args ) {
651 $notice = new \BP_Messages_Notice();
652 $notice->subject = $assoc_args['subject'];
653 $notice->message = $assoc_args['content'];
654 $notice->date_sent = bp_core_current_time();
655 $notice->is_active = 1;
656
657 // Send it.
658 if ( $notice->save() ) {
659 WP_CLI::success( 'Notice was successfully sent.' );
660 } else {
661 WP_CLI::error( 'Notice was not sent.' );
662 }
663 }
664 }
665