| 1 |
<?php |
| 2 |
|
| 3 |
namespace Buddypress\CLI\Command; |
| 4 |
|
| 5 |
use WP_CLI; |
| 6 |
|
| 7 |
/** |
| 8 |
* Manage BuddyPress Activities. |
| 9 |
* |
| 10 |
* ## EXAMPLES |
| 11 |
* |
| 12 |
* # Create an activity marked as spam. |
| 13 |
* $ wp bp activity create --is-spam=1 |
| 14 |
* Success: Successfully created new activity item (ID #5464) |
| 15 |
* |
| 16 |
* # Create an activity in a group. |
| 17 |
* $ wp bp activity add --component=groups --item-id=2 --user-id=10 |
| 18 |
* Success: Successfully created new activity item (ID #48949) |
| 19 |
* |
| 20 |
* @since 1.5.0 |
| 21 |
*/ |
| 22 |
class Activity extends BuddyPressCommand { |
| 23 |
|
| 24 |
/** |
| 25 |
* Object fields. |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
protected $obj_fields = [ |
| 30 |
'id', |
| 31 |
'user_id', |
| 32 |
'component', |
| 33 |
'type', |
| 34 |
'action', |
| 35 |
'item_id', |
| 36 |
'primary_link', |
| 37 |
'secondary_item_id', |
| 38 |
'date_recorded', |
| 39 |
'hide_sitewide', |
| 40 |
'is_spam', |
| 41 |
]; |
| 42 |
|
| 43 |
/** |
| 44 |
* Dependency check for this CLI command. |
| 45 |
*/ |
| 46 |
public static function check_dependencies() { |
| 47 |
parent::check_dependencies(); |
| 48 |
|
| 49 |
if ( ! bp_is_active( 'activity' ) ) { |
| 50 |
WP_CLI::error( 'The Activity component is not active.' ); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Create an activity item. |
| 56 |
* |
| 57 |
* ## OPTIONS |
| 58 |
* |
| 59 |
* [--component=<component>] |
| 60 |
* : The component for the activity item (groups, activity, etc). If |
| 61 |
* none is provided, a component will be randomly selected from the |
| 62 |
* active components. |
| 63 |
* |
| 64 |
* [--type=<type>] |
| 65 |
* : Activity type (activity_update, group_created, etc). If none is |
| 66 |
* provided, a type will be randomly chose from those natively |
| 67 |
* associated with your <component>. |
| 68 |
* |
| 69 |
* [--action=<action>] |
| 70 |
* : Action text (eg "Joe created a new group Foo"). If none is |
| 71 |
* provided, one will be generated automatically based on other params. |
| 72 |
* |
| 73 |
* [--content=<content>] |
| 74 |
* : Activity content text. If none is provided, default text will be |
| 75 |
* generated. |
| 76 |
* |
| 77 |
* [--primary-link=<primary-link>] |
| 78 |
* : URL of the item, as used in RSS feeds. If none is provided, a URL |
| 79 |
* will be generated based on passed parameters. |
| 80 |
* |
| 81 |
* [--user-id=<user>] |
| 82 |
* : ID of the user associated with the new item. If none is provided, |
| 83 |
* a user will be randomly selected. |
| 84 |
* |
| 85 |
* [--item-id=<item-id>] |
| 86 |
* : ID of the associated item. If none is provided, one will be |
| 87 |
* generated automatically, if your activity type requires it. |
| 88 |
* |
| 89 |
* [--secondary-item-id=<secondary-item-id>] |
| 90 |
* : ID of the secondary associated item. If none is provided, one will |
| 91 |
* be generated automatically, if your activity type requires it. |
| 92 |
* |
| 93 |
* [--date-recorded=<date-recorded>] |
| 94 |
* : GMT timestamp, in Y-m-d h:i:s format. |
| 95 |
* |
| 96 |
* [--hide-sitewide=<hide-sitewide>] |
| 97 |
* : Whether to hide in sitewide streams. |
| 98 |
* |
| 99 |
* [--is-spam=<is-spam>] |
| 100 |
* : Whether the item should be marked as spam. |
| 101 |
* |
| 102 |
* [--silent] |
| 103 |
* : Whether to silent the activity creation. |
| 104 |
* |
| 105 |
* [--porcelain] |
| 106 |
* : Output only the new activity id. |
| 107 |
* |
| 108 |
* ## EXAMPLES |
| 109 |
* |
| 110 |
* # Create an activity marked as spam. |
| 111 |
* $ wp bp activity create --is-spam=1 |
| 112 |
* Success: Successfully created new activity item (ID #5464) |
| 113 |
* |
| 114 |
* # Create an activity. |
| 115 |
* $ wp bp activity add --component=groups --item-id=564 --user-id=10 |
| 116 |
* Success: Successfully created new activity item (ID #48949) |
| 117 |
* |
| 118 |
* @alias add |
| 119 |
*/ |
| 120 |
public function create( $args, $assoc_args ) { |
| 121 |
$r = wp_parse_args( |
| 122 |
$assoc_args, |
| 123 |
[ |
| 124 |
'component' => '', |
| 125 |
'type' => '', |
| 126 |
'action' => '', |
| 127 |
'content' => '', |
| 128 |
'primary-link' => '', |
| 129 |
'user-id' => '', |
| 130 |
'item-id' => '', |
| 131 |
'secondary-item-id' => '', |
| 132 |
'date-recorded' => bp_core_current_time(), |
| 133 |
'hide-sitewide' => 0, |
| 134 |
'is-spam' => 0, |
| 135 |
] |
| 136 |
); |
| 137 |
|
| 138 |
// Fill in any missing information. |
| 139 |
if ( empty( $r['component'] ) ) { |
| 140 |
$r['component'] = $this->get_random_component(); |
| 141 |
} |
| 142 |
|
| 143 |
if ( empty( $r['type'] ) ) { |
| 144 |
$r['type'] = $this->get_random_type_from_component( $r['component'] ); |
| 145 |
} |
| 146 |
|
| 147 |
if ( 'groups' === $r['component'] ) { |
| 148 |
$r['item-id'] = $this->get_group_id_from_identifier( $r['item-id'] ); |
| 149 |
} |
| 150 |
|
| 151 |
// If some data is not set, we have to generate it. |
| 152 |
if ( empty( $r['item-id'] ) || empty( $r['secondary-item-id'] ) ) { |
| 153 |
$r = $this->generate_item_details( $r ); |
| 154 |
} |
| 155 |
|
| 156 |
$activity_id = bp_activity_add( |
| 157 |
[ |
| 158 |
'action' => $r['action'], |
| 159 |
'content' => $r['content'], |
| 160 |
'component' => $r['component'], |
| 161 |
'type' => $r['type'], |
| 162 |
'primary_link' => $r['primary-link'], |
| 163 |
'user_id' => $r['user-id'], |
| 164 |
'item_id' => $r['item-id'], |
| 165 |
'secondary_item_id' => $r['secondary-item-id'], |
| 166 |
'date_recorded' => $r['date-recorded'], |
| 167 |
'hide_sitewide' => (bool) $r['hide-sitewide'], |
| 168 |
'is_spam' => (bool) $r['is-spam'], |
| 169 |
] |
| 170 |
); |
| 171 |
|
| 172 |
// Silent it before it errors. |
| 173 |
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'silent' ) ) { |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
if ( ! is_numeric( $activity_id ) ) { |
| 178 |
WP_CLI::error( 'Could not create activity item.' ); |
| 179 |
} |
| 180 |
|
| 181 |
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) { |
| 182 |
WP_CLI::log( $activity_id ); |
| 183 |
} else { |
| 184 |
WP_CLI::success( sprintf( 'Successfully created new activity item (ID #%d)', $activity_id ) ); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Retrieve a list of activities. |
| 190 |
* |
| 191 |
* ## OPTIONS |
| 192 |
* |
| 193 |
* [--<field>=<value>] |
| 194 |
* : One or more parameters to pass to \BP_Activity_Activity::get() |
| 195 |
* |
| 196 |
* [--user-id=<user>] |
| 197 |
* : Limit activities to a specific user id. Accepts a numeric ID. |
| 198 |
* |
| 199 |
* [--component=<component>] |
| 200 |
* : Limit activities to a specific or certain components. |
| 201 |
* |
| 202 |
* [--type=<type>] |
| 203 |
* : Type of the activity. Ex.: activity_update, profile_updated. |
| 204 |
* |
| 205 |
* [--primary-id=<primary-id>] |
| 206 |
* : Object ID to filter the activities. Ex.: group_id or forum_id or blog_id, etc. |
| 207 |
* |
| 208 |
* [--secondary-id=<secondary-id>] |
| 209 |
* : Secondary object ID to filter the activities. Ex.: a post_id. |
| 210 |
* |
| 211 |
* [--count=<number>] |
| 212 |
* : How many activities to list. |
| 213 |
* --- |
| 214 |
* default: 50 |
| 215 |
* --- |
| 216 |
* |
| 217 |
* [--format=<format>] |
| 218 |
* : Render output in a particular format. |
| 219 |
* --- |
| 220 |
* default: table |
| 221 |
* options: |
| 222 |
* - table |
| 223 |
* - csv |
| 224 |
* - ids |
| 225 |
* - json |
| 226 |
* - count |
| 227 |
* - yaml |
| 228 |
* --- |
| 229 |
* |
| 230 |
* ## AVAILABLE FIELDS |
| 231 |
* |
| 232 |
* These fields will be displayed by default for each activity: |
| 233 |
* |
| 234 |
* * id |
| 235 |
* * user_id |
| 236 |
* * component |
| 237 |
* * type |
| 238 |
* * action |
| 239 |
* * content |
| 240 |
* * item_id |
| 241 |
* * secondary_item_id |
| 242 |
* * primary_link |
| 243 |
* * date_recorded |
| 244 |
* * is_spam |
| 245 |
* * user_email |
| 246 |
* * user_nicename |
| 247 |
* * user_login |
| 248 |
* * display_name |
| 249 |
* * user_fullname |
| 250 |
* |
| 251 |
* ## EXAMPLES |
| 252 |
* |
| 253 |
* # List activities and get the count. |
| 254 |
* $ wp bp activity list --format=count |
| 255 |
* 100 |
| 256 |
* |
| 257 |
* # List activities and get the IDs. |
| 258 |
* $ wp bp activity list --format=ids |
| 259 |
* 70 71 72 73 74 |
| 260 |
* |
| 261 |
* @subcommand list |
| 262 |
*/ |
| 263 |
public function list_( $args, $assoc_args ) { |
| 264 |
$formatter = $this->get_formatter( $assoc_args ); |
| 265 |
|
| 266 |
$r = wp_parse_args( |
| 267 |
$assoc_args, |
| 268 |
[ |
| 269 |
'page' => 1, |
| 270 |
'count' => 50, |
| 271 |
'show_hidden' => true, |
| 272 |
'filter' => false, |
| 273 |
] |
| 274 |
); |
| 275 |
|
| 276 |
// Activities to list. |
| 277 |
$r['per_page'] = $r['count']; |
| 278 |
|
| 279 |
if ( isset( $assoc_args['user-id'] ) && is_numeric( $assoc_args['user-id'] ) ) { |
| 280 |
$r['filter']['user_id'] = $assoc_args['user-id']; |
| 281 |
} |
| 282 |
|
| 283 |
if ( isset( $assoc_args['component'] ) ) { |
| 284 |
$r['filter']['object'] = $assoc_args['component']; |
| 285 |
} |
| 286 |
|
| 287 |
if ( isset( $assoc_args['type'] ) ) { |
| 288 |
$r['filter']['action'] = $assoc_args['type']; |
| 289 |
} |
| 290 |
|
| 291 |
if ( isset( $assoc_args['primary-id'] ) ) { |
| 292 |
$r['filter']['primary_id'] = $assoc_args['primary-id']; |
| 293 |
} |
| 294 |
|
| 295 |
if ( isset( $assoc_args['secondary-id'] ) ) { |
| 296 |
$r['filter']['secondary_id'] = $assoc_args['secondary-id']; |
| 297 |
} |
| 298 |
|
| 299 |
$r = self::process_csv_arguments_to_arrays( $r ); |
| 300 |
|
| 301 |
// If count or ids, no need for activity objects. |
| 302 |
if ( in_array( $formatter->format, [ 'ids', 'count' ], true ) ) { |
| 303 |
$r['fields'] = 'ids'; |
| 304 |
} |
| 305 |
|
| 306 |
$activities = bp_activity_get( $r ); |
| 307 |
|
| 308 |
if ( empty( $activities['activities'] ) ) { |
| 309 |
WP_CLI::error( 'No activities found.' ); |
| 310 |
} |
| 311 |
|
| 312 |
$formatter->display_items( $activities['activities'] ); |
| 313 |
} |
| 314 |
|
| 315 |
/** |
| 316 |
* Generate random activity items. |
| 317 |
* |
| 318 |
* ## OPTIONS |
| 319 |
* |
| 320 |
* [--count=<number>] |
| 321 |
* : How many activities to generate. |
| 322 |
* --- |
| 323 |
* default: 100 |
| 324 |
* --- |
| 325 |
* |
| 326 |
* [--skip-activity-comments=<skip-activity-comments>] |
| 327 |
* : Whether to skip activity comments. Recording activity_comment |
| 328 |
* items requires a resource-intensive tree rebuild. |
| 329 |
* --- |
| 330 |
* default: 1 |
| 331 |
* --- |
| 332 |
* |
| 333 |
* [--format=<format>] |
| 334 |
* : Render output in a particular format. |
| 335 |
* --- |
| 336 |
* default: progress |
| 337 |
* options: |
| 338 |
* - progress |
| 339 |
* - ids |
| 340 |
* --- |
| 341 |
* |
| 342 |
* ## EXAMPLES |
| 343 |
* |
| 344 |
* # Generate 5 activity items. |
| 345 |
* $ wp bp activity generate --count=5 |
| 346 |
* Generating activities 100% [======================] 0:00 / 0:00 |
| 347 |
* |
| 348 |
* # Generate 5 activity items and output only the IDs. |
| 349 |
* $ wp bp activity generate --count=5 --format=ids |
| 350 |
* 70 71 72 73 74 |
| 351 |
*/ |
| 352 |
public function generate( $args, $assoc_args ) { |
| 353 |
$this->generate_callback( |
| 354 |
'Generating activities', |
| 355 |
$assoc_args, |
| 356 |
function ( $assoc_args, $format ) { |
| 357 |
$component = $this->get_random_component(); |
| 358 |
$type = $this->get_random_type_from_component( $component ); |
| 359 |
|
| 360 |
if ( (bool) $assoc_args['skip-activity-comments'] && 'activity_comment' === $type ) { |
| 361 |
$type = 'activity_update'; |
| 362 |
} |
| 363 |
|
| 364 |
$params = [ |
| 365 |
'component' => $component, |
| 366 |
'type' => $type, |
| 367 |
'content' => $this->generate_random_text(), |
| 368 |
]; |
| 369 |
|
| 370 |
if ( 'ids' === $format ) { |
| 371 |
$params['porcelain'] = true; |
| 372 |
} else { |
| 373 |
$params['silent'] = true; |
| 374 |
} |
| 375 |
|
| 376 |
return $this->create( [], $params ); |
| 377 |
} |
| 378 |
); |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Fetch specific activity. |
| 383 |
* |
| 384 |
* ## OPTIONS |
| 385 |
* |
| 386 |
* <activity-id> |
| 387 |
* : Identifier for the activity. |
| 388 |
* |
| 389 |
* [--fields=<fields>] |
| 390 |
* : Limit the output to specific fields. |
| 391 |
* |
| 392 |
* [--format=<format>] |
| 393 |
* : Render output in a particular format. |
| 394 |
* --- |
| 395 |
* default: table |
| 396 |
* options: |
| 397 |
* - table |
| 398 |
* - json |
| 399 |
* - csv |
| 400 |
* - yaml |
| 401 |
* --- |
| 402 |
* |
| 403 |
* ## EXAMPLE |
| 404 |
* |
| 405 |
* # Get activity by ID. |
| 406 |
* $ wp bp activity get 58 |
| 407 |
* +-------------------+----------------------------------------------------------------------------------------------+ |
| 408 |
* | Field | Value | |
| 409 |
* +-------------------+----------------------------------------------------------------------------------------------+ |
| 410 |
* | id | 58 | |
| 411 |
* | user_id | 7 | |
| 412 |
* | component | xprofile | |
| 413 |
* | type | updated_profile | |
| 414 |
* | action | <a href="https://wp.test/members/user_1_4/profile/">User 4</a>'s profile was updated | |
| 415 |
* | content | Here is some random text | |
| 416 |
* | primary_link | | |
| 417 |
* | item_id | 0 | |
| 418 |
* | secondary_item_id | 0 | |
| 419 |
* | date_recorded | 2024-02-08 01:53:59 | |
| 420 |
* | hide_sitewide | 0 | |
| 421 |
* | mptt_left | 0 | |
| 422 |
* | mptt_right | 0 | |
| 423 |
* | is_spam | 0 | |
| 424 |
* | user_email | | |
| 425 |
* | user_nicename | user_1_4 | |
| 426 |
* | user_login | user_1_4 | |
| 427 |
* | display_name | User 4 | |
| 428 |
* | user_fullname | User 4 | |
| 429 |
* | children | [] | |
| 430 |
* | url | https://wp.test/activity/p/58/ | |
| 431 |
* +-------------------+----------------------------------------------------------------------------------------------+ |
| 432 |
* |
| 433 |
* @alias see |
| 434 |
*/ |
| 435 |
public function get( $args, $assoc_args ) { |
| 436 |
$activity_id = $args[0]; |
| 437 |
|
| 438 |
if ( ! is_numeric( $activity_id ) ) { |
| 439 |
WP_CLI::error( 'Please provide a numeric activity ID.' ); |
| 440 |
} |
| 441 |
|
| 442 |
$activity = bp_activity_get_specific( |
| 443 |
[ |
| 444 |
'activity_ids' => $activity_id, |
| 445 |
'spam' => null, |
| 446 |
'display_comments' => true, |
| 447 |
] |
| 448 |
); |
| 449 |
|
| 450 |
if ( ! isset( $activity['activities'][0] ) || ! is_object( $activity['activities'][0] ) ) { |
| 451 |
WP_CLI::error( 'No activity found.' ); |
| 452 |
} |
| 453 |
|
| 454 |
$activity_arr = get_object_vars( $activity['activities'][0] ); |
| 455 |
$activity_arr['url'] = bp_activity_get_permalink( $activity_id ); |
| 456 |
|
| 457 |
if ( empty( $assoc_args['fields'] ) ) { |
| 458 |
$assoc_args['fields'] = array_keys( $activity_arr ); |
| 459 |
} |
| 460 |
|
| 461 |
$this->get_formatter( $assoc_args )->display_item( $activity_arr ); |
| 462 |
} |
| 463 |
|
| 464 |
/** |
| 465 |
* Delete an activity. |
| 466 |
* |
| 467 |
* ## OPTIONS |
| 468 |
* |
| 469 |
* <activity-id>... |
| 470 |
* : ID or IDs of activities to delete. |
| 471 |
* |
| 472 |
* [--yes] |
| 473 |
* : Answer yes to the confirmation message. |
| 474 |
* |
| 475 |
* ## EXAMPLES |
| 476 |
* |
| 477 |
* # Delete an activity. |
| 478 |
* $ wp bp activity delete 958695 --yes |
| 479 |
* Success: Deleted activity 958695. |
| 480 |
* |
| 481 |
* # Delete multiple activities. |
| 482 |
* $ wp bp activity delete 500 600 --yes |
| 483 |
* Success: Deleted activity 500. |
| 484 |
* Success: Deleted activity 600. |
| 485 |
* |
| 486 |
* @alias remove |
| 487 |
* @alias trash |
| 488 |
*/ |
| 489 |
public function delete( $args, $assoc_args ) { |
| 490 |
$activities = wp_parse_id_list( $args ); |
| 491 |
|
| 492 |
if ( count( $activities ) > 1 ) { |
| 493 |
WP_CLI::confirm( 'Are you sure you want to delete these activities?', $assoc_args ); |
| 494 |
} else { |
| 495 |
WP_CLI::confirm( 'Are you sure you want to delete this activity?', $assoc_args ); |
| 496 |
} |
| 497 |
|
| 498 |
parent::_delete( |
| 499 |
$activities, |
| 500 |
$assoc_args, |
| 501 |
function ( $activity_id ) { |
| 502 |
$id = $this->get_activity_id_from_identifier( $activity_id ); |
| 503 |
|
| 504 |
if ( ! $id ) { |
| 505 |
return [ 'error', sprintf( 'No activity found by ID %d.', $activity_id ) ]; |
| 506 |
} |
| 507 |
|
| 508 |
if ( bp_activity_delete( [ 'id' => $id ] ) ) { |
| 509 |
return [ 'success', sprintf( 'Deleted activity %d.', $activity_id ) ]; |
| 510 |
} |
| 511 |
|
| 512 |
return [ 'error', sprintf( 'Could not delete the activity %d.', $activity_id ) ]; |
| 513 |
} |
| 514 |
); |
| 515 |
} |
| 516 |
|
| 517 |
/** |
| 518 |
* Spam an activity. |
| 519 |
* |
| 520 |
* ## OPTIONS |
| 521 |
* |
| 522 |
* <activity-id> |
| 523 |
* : Identifier for the activity. |
| 524 |
* |
| 525 |
* ## EXAMPLES |
| 526 |
* |
| 527 |
* # Spam an activity. |
| 528 |
* $ wp bp activity spam 500 |
| 529 |
* Success: Activity marked as spam. |
| 530 |
* |
| 531 |
* # Spam an activity. |
| 532 |
* $ wp bp activity unham 165165 |
| 533 |
* Success: Activity marked as spam. |
| 534 |
* |
| 535 |
* @alias unham |
| 536 |
*/ |
| 537 |
public function spam( $args ) { |
| 538 |
$activity_id = $args[0]; |
| 539 |
|
| 540 |
if ( ! is_numeric( $activity_id ) ) { |
| 541 |
WP_CLI::error( 'Please provide a numeric activity ID.' ); |
| 542 |
} |
| 543 |
|
| 544 |
$activity = $this->get_activity_id_from_identifier( $activity_id, true ); |
| 545 |
|
| 546 |
// Mark as spam. |
| 547 |
bp_activity_mark_as_spam( $activity ); |
| 548 |
|
| 549 |
if ( $activity->save() ) { |
| 550 |
WP_CLI::success( 'Activity marked as spam.' ); |
| 551 |
} else { |
| 552 |
WP_CLI::error( 'Could not mark the activity as spam.' ); |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
/** |
| 557 |
* Ham an activity. |
| 558 |
* |
| 559 |
* ## OPTIONS |
| 560 |
* |
| 561 |
* <activity-id> |
| 562 |
* : Identifier for the activity. |
| 563 |
* |
| 564 |
* ## EXAMPLES |
| 565 |
* |
| 566 |
* # Mark an activity as ham. |
| 567 |
* $ wp bp activity ham 500 |
| 568 |
* Success: Activity marked as ham. |
| 569 |
* |
| 570 |
* # Mark an activity as ham. |
| 571 |
* $ wp bp activity unspam 4679 |
| 572 |
* Success: Activity marked as ham. |
| 573 |
* |
| 574 |
* @alias unspam |
| 575 |
*/ |
| 576 |
public function ham( $args ) { |
| 577 |
$activity_id = $args[0]; |
| 578 |
|
| 579 |
if ( ! is_numeric( $activity_id ) ) { |
| 580 |
WP_CLI::error( 'Please provide a numeric activity ID.' ); |
| 581 |
} |
| 582 |
|
| 583 |
$activity = $this->get_activity_id_from_identifier( $activity_id, true ); |
| 584 |
|
| 585 |
// Mark as ham. |
| 586 |
bp_activity_mark_as_ham( $activity ); |
| 587 |
|
| 588 |
if ( $activity->save() ) { |
| 589 |
WP_CLI::success( 'Activity marked as ham.' ); |
| 590 |
} else { |
| 591 |
WP_CLI::error( 'Could not mark the activity as ham.' ); |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
/** |
| 596 |
* Post an activity update. |
| 597 |
* |
| 598 |
* ## OPTIONS |
| 599 |
* |
| 600 |
* --user-id=<user> |
| 601 |
* : ID of the user. |
| 602 |
* |
| 603 |
* --content=<content> |
| 604 |
* : Activity content text. |
| 605 |
* |
| 606 |
* [--silent] |
| 607 |
* : Whether to silent the activity update. |
| 608 |
* |
| 609 |
* [--porcelain] |
| 610 |
* : Output only the new activity id. |
| 611 |
* |
| 612 |
* ## EXAMPLES |
| 613 |
* |
| 614 |
* # Post an activity update. |
| 615 |
* $ wp bp activity post-update --user-id=50 --content="Content to update" |
| 616 |
* Success: Successfully updated with a new activity item (ID #13165) |
| 617 |
* |
| 618 |
* # Post an activity update. |
| 619 |
* $ wp bp activity post-update --user-id=140 |
| 620 |
* Success: Successfully updated with a new activity item (ID #4548) |
| 621 |
* |
| 622 |
* @alias post-update |
| 623 |
*/ |
| 624 |
public function post_update( $args, $assoc_args ) { |
| 625 |
$user = $this->get_user_id_from_identifier( $assoc_args['user-id'] ); |
| 626 |
|
| 627 |
// Post the activity update. |
| 628 |
$activity_id = bp_activity_post_update( |
| 629 |
[ |
| 630 |
'content' => $assoc_args['content'], |
| 631 |
'user_id' => $user->ID, |
| 632 |
] |
| 633 |
); |
| 634 |
|
| 635 |
// Silent it before it errors. |
| 636 |
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'silent' ) ) { |
| 637 |
return; |
| 638 |
} |
| 639 |
|
| 640 |
// Activity ID returned on success update. |
| 641 |
if ( ! is_numeric( $activity_id ) ) { |
| 642 |
WP_CLI::error( 'Could not post the activity update.' ); |
| 643 |
} |
| 644 |
|
| 645 |
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) { |
| 646 |
WP_CLI::log( $activity_id ); |
| 647 |
} else { |
| 648 |
WP_CLI::success( sprintf( 'Successfully updated with a new activity item (ID #%d)', $activity_id ) ); |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
/** |
| 653 |
* Add an activity comment. |
| 654 |
* |
| 655 |
* ## OPTIONS |
| 656 |
* |
| 657 |
* <activity-id> |
| 658 |
* : ID of the activity to add the comment. |
| 659 |
* |
| 660 |
* --user-id=<user> |
| 661 |
* : ID of the user. If none is provided, a user will be randomly selected. |
| 662 |
* |
| 663 |
* --content=<content> |
| 664 |
* : Activity content text. If none is provided, default text will be generated. |
| 665 |
* |
| 666 |
* [--skip-notification] |
| 667 |
* : Whether to skip notification. |
| 668 |
* |
| 669 |
* [--silent] |
| 670 |
* : Whether to silent the activity comment. |
| 671 |
* |
| 672 |
* [--porcelain] |
| 673 |
* : Output only the new activity comment id. |
| 674 |
* |
| 675 |
* ## EXAMPLES |
| 676 |
* |
| 677 |
* # Add an activity comment. |
| 678 |
* $ wp bp activity comment 560 --user-id=50 --content="New activity comment" |
| 679 |
* Success: Successfully added a new activity comment (ID #4645) |
| 680 |
* |
| 681 |
* # Add an activity comment, skipping notification. |
| 682 |
* $ wp bp activity comment 459 --user-id=140 --skip-notification=1 |
| 683 |
* Success: Successfully added a new activity comment (ID #494) |
| 684 |
*/ |
| 685 |
public function comment( $args, $assoc_args ) { |
| 686 |
$activity_id = $this->get_activity_id_from_identifier( $args[0] ); |
| 687 |
$user = $this->get_user_id_from_identifier( $assoc_args['user-id'] ); |
| 688 |
$skip_notification = WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-notification' ); |
| 689 |
|
| 690 |
// Add activity comment. |
| 691 |
$activity_comment_id = bp_activity_new_comment( [ |
| 692 |
'content' => $assoc_args['content'], |
| 693 |
'user_id' => $user->ID, |
| 694 |
'activity_id' => $activity_id, |
| 695 |
'skip_notification' => $skip_notification, |
| 696 |
] ); |
| 697 |
|
| 698 |
// Silent it before it errors. |
| 699 |
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'silent' ) ) { |
| 700 |
return; |
| 701 |
} |
| 702 |
|
| 703 |
// Activity Comment ID returned on success. |
| 704 |
if ( ! is_numeric( $activity_comment_id ) ) { |
| 705 |
WP_CLI::error( 'Could not post a new activity comment.' ); |
| 706 |
} |
| 707 |
|
| 708 |
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) { |
| 709 |
WP_CLI::log( $activity_comment_id ); |
| 710 |
} else { |
| 711 |
WP_CLI::success( sprintf( 'Successfully added a new activity comment (ID #%d)', $activity_comment_id ) ); |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
/** |
| 716 |
* Delete an activity comment. |
| 717 |
* |
| 718 |
* ## OPTIONS |
| 719 |
* |
| 720 |
* <activity-id> |
| 721 |
* : Identifier for the activity. |
| 722 |
* |
| 723 |
* --comment-id=<comment-id> |
| 724 |
* : ID of the comment to delete. |
| 725 |
* |
| 726 |
* [--yes] |
| 727 |
* : Answer yes to the confirmation message. |
| 728 |
* |
| 729 |
* ## EXAMPLES |
| 730 |
* |
| 731 |
* # Delete an activity comment. |
| 732 |
* $ wp bp activity delete-comment 100 --comment-id=500 --yes |
| 733 |
* Success: Activity comment deleted. |
| 734 |
* |
| 735 |
* # Delete an activity comment. |
| 736 |
* $ wp bp activity delete-comment 165 --comment-id=35435 --yes |
| 737 |
* Success: Activity comment deleted. |
| 738 |
* |
| 739 |
* @alias remove-comment |
| 740 |
* @alias delete-comment |
| 741 |
*/ |
| 742 |
public function delete_comment( $args, $assoc_args ) { |
| 743 |
$activity_id = $args[0]; |
| 744 |
|
| 745 |
if ( ! is_numeric( $activity_id ) ) { |
| 746 |
WP_CLI::error( 'Please provide a numeric activity ID.' ); |
| 747 |
} |
| 748 |
|
| 749 |
WP_CLI::confirm( 'Are you sure you want to delete this activity comment?', $assoc_args ); |
| 750 |
|
| 751 |
$activity_id = $this->get_activity_id_from_identifier( $activity_id ); |
| 752 |
|
| 753 |
// Delete Comment. True if deleted. |
| 754 |
if ( bp_activity_delete_comment( $activity_id, $assoc_args['comment-id'] ) ) { |
| 755 |
WP_CLI::success( 'Activity comment deleted.' ); |
| 756 |
} else { |
| 757 |
WP_CLI::error( 'Could not delete the activity comment.' ); |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
/** |
| 762 |
* Get a random type from a component. |
| 763 |
* |
| 764 |
* @since 1.1 |
| 765 |
* |
| 766 |
* @param string $component Component name. |
| 767 |
* @return string |
| 768 |
*/ |
| 769 |
protected function get_random_type_from_component( $component ) { |
| 770 |
$ca = $this->get_components_and_actions(); |
| 771 |
return array_rand( array_flip( $ca[ $component ] ) ); |
| 772 |
} |
| 773 |
|
| 774 |
/** |
| 775 |
* Generate item details. |
| 776 |
* |
| 777 |
* @global wpdb $wpdb WordPress database abstraction object. |
| 778 |
* |
| 779 |
* @since 1.1 |
| 780 |
* |
| 781 |
* @param array $r Params. |
| 782 |
* @return array |
| 783 |
*/ |
| 784 |
protected function generate_item_details( $r ) { |
| 785 |
global $wpdb; |
| 786 |
|
| 787 |
$bp = buddypress(); |
| 788 |
|
| 789 |
switch ( $r['type'] ) { |
| 790 |
case 'activity_update': |
| 791 |
if ( empty( $r['user-id'] ) ) { |
| 792 |
$r['user-id'] = $this->get_random_user_id(); |
| 793 |
} |
| 794 |
|
| 795 |
// Make group updates look more like actual group updates. |
| 796 |
// i.e. give them links to their groups. |
| 797 |
if ( 'groups' === $r['component'] ) { |
| 798 |
|
| 799 |
if ( empty( $r['item-id'] ) ) { |
| 800 |
WP_CLI::error( 'No group found by that ID.' ); |
| 801 |
} |
| 802 |
|
| 803 |
// get the group. |
| 804 |
$group_obj = groups_get_group( [ |
| 805 |
'group_id' => $r['item-id'], |
| 806 |
] ); |
| 807 |
|
| 808 |
// make sure such a group exists. |
| 809 |
if ( empty( $group_obj->id ) ) { |
| 810 |
WP_CLI::error( 'No group found by that slug or id.' ); |
| 811 |
} |
| 812 |
|
| 813 |
// stolen from groups_join_group. |
| 814 |
$r['action'] = sprintf( '%1$s posted an update in the group %2$s', bp_core_get_userlink( $r['user-id'] ), '<a href="' . bp_get_group_url( $group_obj ) . '">' . esc_attr( $group_obj->name ) . '</a>' ); |
| 815 |
} else { |
| 816 |
// old way, for some other kind of update. |
| 817 |
$r['action'] = sprintf( '%s posted an update', bp_core_get_userlink( $r['user-id'] ) ); |
| 818 |
} |
| 819 |
if ( empty( $r['content'] ) ) { |
| 820 |
$r['content'] = $this->generate_random_text(); |
| 821 |
} |
| 822 |
|
| 823 |
$r['primary-link'] = bp_core_get_userlink( $r['user-id'] ); |
| 824 |
|
| 825 |
break; |
| 826 |
|
| 827 |
case 'activity_comment': |
| 828 |
if ( empty( $r['user-id'] ) ) { |
| 829 |
$r['user-id'] = $this->get_random_user_id(); |
| 830 |
} |
| 831 |
|
| 832 |
$parent_item = $wpdb->get_row( "SELECT * FROM {$bp->activity->table_name} ORDER BY RAND() LIMIT 1" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 833 |
|
| 834 |
if ( \is_object( $parent_item ) ) { |
| 835 |
if ( 'activity_comment' === $parent_item->type ) { |
| 836 |
$r['item-id'] = $parent_item->id; |
| 837 |
$r['secondary-item-id'] = $parent_item->secondary_item_id; |
| 838 |
} else { |
| 839 |
$r['item-id'] = $parent_item->id; |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
$r['action'] = sprintf( '%s posted a new activity comment', bp_core_get_userlink( $r['user-id'] ) ); |
| 844 |
$r['content'] = $this->generate_random_text(); |
| 845 |
$r['primary-link'] = bp_core_get_userlink( $r['user-id'] ); |
| 846 |
|
| 847 |
break; |
| 848 |
|
| 849 |
case 'new_blog': |
| 850 |
case 'new_blog_post': |
| 851 |
case 'new_blog_comment': |
| 852 |
if ( ! bp_is_active( 'blogs' ) ) { |
| 853 |
return $r; |
| 854 |
} |
| 855 |
|
| 856 |
if ( is_multisite() ) { |
| 857 |
$r['item-id'] = $wpdb->get_var( "SELECT blog_id FROM {$wpdb->blogs} ORDER BY RAND() LIMIT 1" ); |
| 858 |
} else { |
| 859 |
$r['item-id'] = 1; |
| 860 |
} |
| 861 |
|
| 862 |
// Need blog content for posts/comments. |
| 863 |
if ( 'new_blog_post' === $r['type'] || 'new_blog_comment' === $r['type'] ) { |
| 864 |
|
| 865 |
if ( is_multisite() ) { |
| 866 |
switch_to_blog( $r['item-id'] ); |
| 867 |
} |
| 868 |
|
| 869 |
$comment_info = $wpdb->get_results( "SELECT comment_id, comment_post_id FROM {$wpdb->comments} ORDER BY RAND() LIMIT 1" ); |
| 870 |
$comment_id = $comment_info[0]->comment_id; |
| 871 |
$comment = get_comment( $comment_id ); |
| 872 |
|
| 873 |
$post_id = $comment_info[0]->comment_post_id; |
| 874 |
$post = get_post( $post_id ); |
| 875 |
|
| 876 |
if ( is_multisite() ) { |
| 877 |
restore_current_blog(); |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
// new_blog. |
| 882 |
if ( 'new_blog' === $r['type'] ) { |
| 883 |
if ( '' === $r['user-id'] ) { |
| 884 |
$r['user-id'] = $this->get_random_user_id(); |
| 885 |
} |
| 886 |
|
| 887 |
if ( ! $r['action'] ) { |
| 888 |
$r['action'] = sprintf( '%s created the site %s', bp_core_get_userlink( $r['user-id'] ), '<a href="' . get_home_url( $r['item-id'] ) . '">' . esc_attr( get_blog_option( $r['item-id'], 'blogname' ) ) . '</a>' ); |
| 889 |
} |
| 890 |
|
| 891 |
if ( ! $r['primary-link'] ) { |
| 892 |
$r['primary-link'] = get_home_url( $r['item-id'] ); |
| 893 |
} |
| 894 |
|
| 895 |
// new_blog_post. |
| 896 |
} elseif ( 'new_blog_post' === $r['type'] ) { |
| 897 |
if ( '' === $r['user-id'] ) { |
| 898 |
$r['user-id'] = $post->post_author; |
| 899 |
} |
| 900 |
|
| 901 |
if ( '' === $r['primary-link'] ) { |
| 902 |
$r['primary-link'] = add_query_arg( 'p', $post->ID, trailingslashit( get_home_url( $r['item-id'] ) ) ); |
| 903 |
} |
| 904 |
|
| 905 |
if ( '' === $r['action'] ) { |
| 906 |
$r['action'] = sprintf( '%1$s wrote a new post, %2$s', bp_core_get_userlink( (int) $post->post_author ), '<a href="' . $r['primary-link'] . '">' . $post->post_title . '</a>' ); |
| 907 |
} |
| 908 |
|
| 909 |
if ( '' === $r['content'] ) { |
| 910 |
$r['content'] = $post->post_content; |
| 911 |
} |
| 912 |
|
| 913 |
if ( '' === $r['secondary-item-id'] ) { |
| 914 |
$r['secondary-item-id'] = $post->ID; |
| 915 |
} |
| 916 |
|
| 917 |
// new_blog_comment. |
| 918 |
} else { |
| 919 |
// groan - have to fake this. |
| 920 |
if ( '' === $r['user-id'] ) { |
| 921 |
$user = get_user_by( 'email', $comment->comment_author_email ); |
| 922 |
$r['user-id'] = ( empty( $user ) ) |
| 923 |
? $this->get_random_user_id() |
| 924 |
: $user->ID; |
| 925 |
} |
| 926 |
|
| 927 |
$post_permalink = get_permalink( $comment->comment_post_ID ); |
| 928 |
$comment_link = get_comment_link( $comment->comment_ID ); |
| 929 |
|
| 930 |
if ( '' === $r['primary-link'] ) { |
| 931 |
$r['primary-link'] = $comment_link; |
| 932 |
} |
| 933 |
|
| 934 |
if ( '' === $r['action'] ) { |
| 935 |
$r['action'] = sprintf( '%1$s commented on the post, %2$s', bp_core_get_userlink( $r['user-id'] ), '<a href="' . $post_permalink . '">' . apply_filters( 'the_title', $post->post_title ) . '</a>' ); |
| 936 |
} |
| 937 |
|
| 938 |
if ( '' === $r['content'] ) { |
| 939 |
$r['content'] = $comment->comment_content; |
| 940 |
} |
| 941 |
|
| 942 |
if ( '' === $r['secondary-item-id'] ) { |
| 943 |
$r['secondary-item-id'] = $comment->ID; |
| 944 |
} |
| 945 |
} |
| 946 |
|
| 947 |
$r['content'] = ''; |
| 948 |
|
| 949 |
break; |
| 950 |
|
| 951 |
case 'friendship_created': |
| 952 |
if ( empty( $r['user-id'] ) ) { |
| 953 |
$r['user-id'] = $this->get_random_user_id(); |
| 954 |
} |
| 955 |
|
| 956 |
if ( empty( $r['item-id'] ) ) { |
| 957 |
$r['item-id'] = $this->get_random_user_id(); |
| 958 |
} |
| 959 |
|
| 960 |
$r['action'] = sprintf( '%1$s and %2$s are now friends', bp_core_get_userlink( $r['user-id'] ), bp_core_get_userlink( $r['item-id'] ) ); |
| 961 |
|
| 962 |
break; |
| 963 |
|
| 964 |
case 'created_group': |
| 965 |
if ( empty( $r['item-id'] ) ) { |
| 966 |
$random_group = \BP_Groups_Group::get_random( 1, 1 ); |
| 967 |
$r['item-id'] = $random_group['groups'][0]->slug; |
| 968 |
} |
| 969 |
|
| 970 |
$group = groups_get_group( [ |
| 971 |
'group_id' => $r['item-id'], |
| 972 |
] ); |
| 973 |
|
| 974 |
// @todo what if it's not a group? ugh |
| 975 |
if ( empty( $r['user-id'] ) ) { |
| 976 |
$r['user-id'] = $group->creator_id; |
| 977 |
} |
| 978 |
|
| 979 |
$group_permalink = bp_get_group_url( $group ); |
| 980 |
|
| 981 |
if ( empty( $r['action'] ) ) { |
| 982 |
$r['action'] = sprintf( '%1$s created the group %2$s', bp_core_get_userlink( $r['user-id'] ), '<a href="' . $group_permalink . '">' . esc_attr( $group->name ) . '</a>' ); |
| 983 |
} |
| 984 |
|
| 985 |
if ( empty( $r['primary-link'] ) ) { |
| 986 |
$r['primary-link'] = $group_permalink; |
| 987 |
} |
| 988 |
|
| 989 |
break; |
| 990 |
|
| 991 |
case 'joined_group': |
| 992 |
if ( empty( $r['item-id'] ) ) { |
| 993 |
$random_group = \BP_Groups_Group::get_random( 1, 1 ); |
| 994 |
if ( ! empty( $random_group['groups'][0]->slug ) ) { |
| 995 |
$r['item-id'] = $random_group['groups'][0]->slug; |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
$group = groups_get_group( [ |
| 1000 |
'group_id' => $r['item-id'], |
| 1001 |
] ); |
| 1002 |
|
| 1003 |
if ( empty( $r['user-id'] ) ) { |
| 1004 |
$r['user-id'] = $this->get_random_user_id(); |
| 1005 |
} |
| 1006 |
|
| 1007 |
if ( empty( $r['action'] ) ) { |
| 1008 |
$r['action'] = sprintf( '%1$s joined the group %2$s', bp_core_get_userlink( $r['user-id'] ), '<a href="' . bp_get_group_url( $group ) . '">' . esc_attr( $group->name ) . '</a>' ); |
| 1009 |
} |
| 1010 |
|
| 1011 |
if ( empty( $r['primary-link'] ) ) { |
| 1012 |
$r['primary-link'] = bp_get_group_url( $group ); |
| 1013 |
} |
| 1014 |
|
| 1015 |
break; |
| 1016 |
|
| 1017 |
case 'new_avatar': |
| 1018 |
case 'new_member': |
| 1019 |
case 'updated_profile': |
| 1020 |
if ( empty( $r['user-id'] ) ) { |
| 1021 |
$r['user-id'] = $this->get_random_user_id(); |
| 1022 |
} |
| 1023 |
|
| 1024 |
$userlink = bp_core_get_userlink( $r['user-id'] ); |
| 1025 |
|
| 1026 |
// new_avatar. |
| 1027 |
if ( 'new_avatar' === $r['type'] ) { |
| 1028 |
$r['action'] = sprintf( '%s changed their profile picture', $userlink ); |
| 1029 |
|
| 1030 |
// new_member. |
| 1031 |
} elseif ( 'new_member' === $r['type'] ) { |
| 1032 |
$r['action'] = sprintf( '%s became a registered member', $userlink ); |
| 1033 |
|
| 1034 |
// updated_profile. |
| 1035 |
} else { |
| 1036 |
$r['action'] = sprintf( '%s updated their profile', $userlink ); |
| 1037 |
} |
| 1038 |
|
| 1039 |
break; |
| 1040 |
} |
| 1041 |
|
| 1042 |
return $r; |
| 1043 |
} |
| 1044 |
} |
| 1045 |
|