| 1 |
<?php |
| 2 |
|
| 3 |
namespace Buddypress\CLI\Command; |
| 4 |
|
| 5 |
use WP_CLI; |
| 6 |
|
| 7 |
/** |
| 8 |
* Manage BuddyPress Groups. |
| 9 |
* |
| 10 |
* ## EXAMPLES |
| 11 |
* |
| 12 |
* # Create a public group. |
| 13 |
* $ wp bp group create --name="Totally Cool Group" |
| 14 |
* Success: Group (ID 5465) created: http://example.com/groups/totally-cool-group/ |
| 15 |
* |
| 16 |
* # Create a private group. |
| 17 |
* $ wp bp group create --name="Another Cool Group" --description="Cool Group" --creator-id=54 --status=private |
| 18 |
* Success: Group (ID 6454)6 created: http://example.com/groups/another-cool-group/ |
| 19 |
* |
| 20 |
* @since 1.5.0 |
| 21 |
*/ |
| 22 |
class Group extends BuddyPressCommand { |
| 23 |
|
| 24 |
/** |
| 25 |
* Object fields. |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
protected $obj_fields = [ |
| 30 |
'id', |
| 31 |
'name', |
| 32 |
'slug', |
| 33 |
'status', |
| 34 |
'date_created', |
| 35 |
]; |
| 36 |
|
| 37 |
/** |
| 38 |
* Group ID Object Key |
| 39 |
* |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
protected $obj_id_key = 'group_id'; |
| 43 |
|
| 44 |
/** |
| 45 |
* Group Object Type |
| 46 |
* |
| 47 |
* @var string |
| 48 |
*/ |
| 49 |
protected $obj_type = 'group'; |
| 50 |
|
| 51 |
/** |
| 52 |
* Dependency check for this CLI command. |
| 53 |
*/ |
| 54 |
public static function check_dependencies() { |
| 55 |
parent::check_dependencies(); |
| 56 |
|
| 57 |
if ( ! bp_is_active( 'groups' ) ) { |
| 58 |
WP_CLI::error( 'The Groups component is not active.' ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Create a group. |
| 64 |
* |
| 65 |
* ## OPTIONS |
| 66 |
* |
| 67 |
* --name=<name> |
| 68 |
* : Name of the group. |
| 69 |
* |
| 70 |
* [--slug=<slug>] |
| 71 |
* : URL-safe slug for the group. If not provided, one will be generated automatically. |
| 72 |
* |
| 73 |
* [--description=<description>] |
| 74 |
* : Group description. |
| 75 |
* |
| 76 |
* [--creator-id=<creator-id>] |
| 77 |
* : ID of the group creator. |
| 78 |
* --- |
| 79 |
* default: 1 |
| 80 |
* --- |
| 81 |
* |
| 82 |
* [--slug=<slug>] |
| 83 |
* : URL-safe slug for the group. |
| 84 |
* |
| 85 |
* [--status=<status>] |
| 86 |
* : Group status. |
| 87 |
* --- |
| 88 |
* default: public |
| 89 |
* options: |
| 90 |
* - public |
| 91 |
* - private |
| 92 |
* - hidden |
| 93 |
* --- |
| 94 |
* |
| 95 |
* [--enable-forum=<enable-forum>] |
| 96 |
* : Whether to enable legacy bbPress forums. |
| 97 |
* |
| 98 |
* [--date-created=<date-created>] |
| 99 |
* : GMT timestamp, in Y-m-d h:i:s format. |
| 100 |
* |
| 101 |
* [--silent] |
| 102 |
* : Whether to silent the group creation. |
| 103 |
* |
| 104 |
* [--porcelain] |
| 105 |
* : Return only the new group id. |
| 106 |
* |
| 107 |
* ## EXAMPLES |
| 108 |
* |
| 109 |
* # Create a public group. |
| 110 |
* $ wp bp group create --name="Totally Cool Group" |
| 111 |
* Success: Successfully created new group (ID 5465) |
| 112 |
* |
| 113 |
* # Create a private group. |
| 114 |
* $ wp bp group create --name="Another Cool Group" --description="Cool Group" --creator-id=54 --status=private |
| 115 |
* Success: Successfully created new group (ID 6454) |
| 116 |
* |
| 117 |
* @alias add |
| 118 |
*/ |
| 119 |
public function create( $args, $assoc_args ) { |
| 120 |
$r = wp_parse_args( |
| 121 |
$assoc_args, |
| 122 |
[ |
| 123 |
'name' => '', |
| 124 |
'slug' => '', |
| 125 |
'description' => '', |
| 126 |
'creator-id' => 1, |
| 127 |
'enable-forum' => 0, |
| 128 |
'date-created' => bp_core_current_time(), |
| 129 |
] |
| 130 |
); |
| 131 |
|
| 132 |
// Auto-generate slug. |
| 133 |
if ( empty( $r['slug'] ) ) { |
| 134 |
$r['slug'] = groups_check_slug( sanitize_title( $r['name'] ) ); |
| 135 |
} |
| 136 |
|
| 137 |
// Auto-generate description. |
| 138 |
if ( empty( $r['description'] ) ) { |
| 139 |
$r['description'] = sprintf( 'Description for group "%s"', $r['name'] ); |
| 140 |
} |
| 141 |
|
| 142 |
$group_id = groups_create_group( |
| 143 |
[ |
| 144 |
'name' => $r['name'], |
| 145 |
'slug' => $r['slug'], |
| 146 |
'description' => $r['description'], |
| 147 |
'creator_id' => $r['creator-id'], |
| 148 |
'status' => $r['status'], |
| 149 |
'enable_forum' => $r['enable-forum'], |
| 150 |
'date_created' => $r['date-created'], |
| 151 |
] |
| 152 |
); |
| 153 |
|
| 154 |
// Silent it before it errors. |
| 155 |
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'silent' ) ) { |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
if ( ! is_numeric( $group_id ) ) { |
| 160 |
WP_CLI::error( 'Could not create group.' ); |
| 161 |
} |
| 162 |
|
| 163 |
groups_update_groupmeta( $group_id, 'total_member_count', 1 ); |
| 164 |
|
| 165 |
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) { |
| 166 |
WP_CLI::log( $group_id ); |
| 167 |
} else { |
| 168 |
WP_CLI::success( sprintf( 'Successfully created new group (ID #%d)', $group_id ) ); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Generate random groups. |
| 174 |
* |
| 175 |
* ## OPTIONS |
| 176 |
* |
| 177 |
* [--count=<number>] |
| 178 |
* : How many groups to generate. |
| 179 |
* --- |
| 180 |
* default: 100 |
| 181 |
* --- |
| 182 |
* |
| 183 |
* [--status=<status>] |
| 184 |
* : The status of the generated groups. |
| 185 |
* --- |
| 186 |
* default: mixed |
| 187 |
* options: |
| 188 |
* - public |
| 189 |
* - private |
| 190 |
* - hidden |
| 191 |
* - mixed |
| 192 |
* --- |
| 193 |
* |
| 194 |
* [--creator-id=<creator-id>] |
| 195 |
* : ID of the group creator. |
| 196 |
* --- |
| 197 |
* |
| 198 |
* [--format=<format>] |
| 199 |
* : Render output in a particular format. |
| 200 |
* --- |
| 201 |
* default: progress |
| 202 |
* options: |
| 203 |
* - progress |
| 204 |
* - ids |
| 205 |
* --- |
| 206 |
* |
| 207 |
* ## EXAMPLES |
| 208 |
* |
| 209 |
* # Generate 50 random groups. |
| 210 |
* $ wp bp group generate --count=50 |
| 211 |
* Generating groups 100% [======================] 0:00 / 0:00 |
| 212 |
* |
| 213 |
* # Generate 5 groups with mixed status. |
| 214 |
* $ wp bp group generate --count=5 --status=mixed |
| 215 |
* Generating groups 100% [======================] 0:00 / 0:00 |
| 216 |
* |
| 217 |
* # Generate 10 hidden groups with a specific creator. |
| 218 |
* $ wp bp group generate --count=10 --status=hidden --creator-id=30 |
| 219 |
* Generating groups 100% [======================] 0:00 / 0:00 |
| 220 |
* |
| 221 |
* # Generate 5 random groups and output only the IDs. |
| 222 |
* $ wp bp group generate --count=5 --format=ids |
| 223 |
* 70 71 72 73 74 |
| 224 |
*/ |
| 225 |
public function generate( $args, $assoc_args ) { |
| 226 |
$creator_id = null; |
| 227 |
|
| 228 |
if ( isset( $assoc_args['creator-id'] ) ) { |
| 229 |
$user = $this->get_user_id_from_identifier( $assoc_args['creator-id'] ); |
| 230 |
$creator_id = $user->ID; |
| 231 |
} |
| 232 |
|
| 233 |
$this->generate_callback( |
| 234 |
'Generating groups', |
| 235 |
$assoc_args, |
| 236 |
function ( $assoc_args, $format ) use ( $creator_id ) { |
| 237 |
|
| 238 |
if ( ! $creator_id ) { |
| 239 |
$creator_id = $this->get_random_user_id(); |
| 240 |
} |
| 241 |
|
| 242 |
$params = [ |
| 243 |
'name' => sprintf( 'Group name - #%d', wp_rand() ), |
| 244 |
'creator-id' => $creator_id, |
| 245 |
'status' => $this->random_group_status( $assoc_args['status'] ), |
| 246 |
]; |
| 247 |
|
| 248 |
if ( 'ids' === $format ) { |
| 249 |
$params['porcelain'] = true; |
| 250 |
} else { |
| 251 |
$params['silent'] = true; |
| 252 |
} |
| 253 |
|
| 254 |
return $this->create( [], $params ); |
| 255 |
} |
| 256 |
); |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Get a group. |
| 261 |
* |
| 262 |
* ## OPTIONS |
| 263 |
* |
| 264 |
* <group-id> |
| 265 |
* : Identifier for the group. Can be a numeric ID or the group slug. |
| 266 |
* |
| 267 |
* [--fields=<fields>] |
| 268 |
* : Limit the output to specific fields. |
| 269 |
* |
| 270 |
* [--format=<format>] |
| 271 |
* : Render output in a particular format. |
| 272 |
* --- |
| 273 |
* default: table |
| 274 |
* options: |
| 275 |
* - table |
| 276 |
* - json |
| 277 |
* - csv |
| 278 |
* - yaml |
| 279 |
* --- |
| 280 |
* |
| 281 |
* ## EXAMPLES |
| 282 |
* |
| 283 |
* # Get group by ID. |
| 284 |
* $ wp bp group get 500 |
| 285 |
* |
| 286 |
* # Get group by group slug. |
| 287 |
* $ wp bp group get group-slug |
| 288 |
* |
| 289 |
* @alias see |
| 290 |
*/ |
| 291 |
public function get( $args, $assoc_args ) { |
| 292 |
$group_id = $this->get_group_id_from_identifier( $args[0] ); |
| 293 |
$group = groups_get_group( $group_id ); |
| 294 |
$group_arr = get_object_vars( $group ); |
| 295 |
$group_arr['url'] = bp_get_group_url( $group ); |
| 296 |
|
| 297 |
if ( empty( $assoc_args['fields'] ) ) { |
| 298 |
$assoc_args['fields'] = array_keys( $group_arr ); |
| 299 |
} |
| 300 |
|
| 301 |
$this->get_formatter( $assoc_args )->display_item( $group_arr ); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Delete a group. |
| 306 |
* |
| 307 |
* ## OPTIONS |
| 308 |
* |
| 309 |
* <group-id>... |
| 310 |
* : ID or IDs of group(s) to delete. Can be a numeric ID or the group slug. |
| 311 |
* |
| 312 |
* [--yes] |
| 313 |
* : Answer yes to the confirmation message. |
| 314 |
* |
| 315 |
* ## EXAMPLES |
| 316 |
* |
| 317 |
* # Delete a group. |
| 318 |
* $ wp bp group delete 500 --yes |
| 319 |
* Success: Deleted group 500. |
| 320 |
* |
| 321 |
* # Delete a group and its metadata. |
| 322 |
* $ wp bp group delete group-slug --yes |
| 323 |
* Success: Deleted group group-slug. |
| 324 |
* |
| 325 |
* # Delete multiple groups. |
| 326 |
* $ wp bp group delete 55654 54564 --yes |
| 327 |
* Success: Deleted group 55654. |
| 328 |
* Success: Deleted group 54564. |
| 329 |
* |
| 330 |
* @alias remove |
| 331 |
* @alias trash |
| 332 |
*/ |
| 333 |
public function delete( $args, $assoc_args ) { |
| 334 |
$groups = wp_parse_id_list( $args ); |
| 335 |
|
| 336 |
if ( count( $groups ) > 1 ) { |
| 337 |
WP_CLI::confirm( 'Are you sure you want to delete these groups and their metadata?', $assoc_args ); |
| 338 |
} else { |
| 339 |
WP_CLI::confirm( 'Are you sure you want to delete this group and its metadata?', $assoc_args ); |
| 340 |
} |
| 341 |
|
| 342 |
parent::_delete( |
| 343 |
$groups, |
| 344 |
$assoc_args, |
| 345 |
function ( $group_id ) { |
| 346 |
if ( groups_delete_group( $group_id ) ) { |
| 347 |
return [ 'success', sprintf( 'Deleted group %d.', $group_id ) ]; |
| 348 |
} |
| 349 |
|
| 350 |
return [ 'error', sprintf( 'Could not delete group %s.', $group_id ) ]; |
| 351 |
} |
| 352 |
); |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* Update a group. |
| 357 |
* |
| 358 |
* ## OPTIONS |
| 359 |
* |
| 360 |
* <group-id>... |
| 361 |
* : Identifier(s) for the group(s). Can be a numeric ID or the group slug. |
| 362 |
* |
| 363 |
* [--<field>=<value>] |
| 364 |
* : One or more fields to update. See groups_create_group() |
| 365 |
* |
| 366 |
* ## EXAMPLES |
| 367 |
* |
| 368 |
* # Update a group. |
| 369 |
* $ wp bp group update 35 --description="What a cool group!" --name="Group of Cool People" |
| 370 |
* Success: Group updated. |
| 371 |
*/ |
| 372 |
public function update( $args, $assoc_args ) { |
| 373 |
parent::_update( |
| 374 |
$args, |
| 375 |
$assoc_args, |
| 376 |
function ( $group_id, $fields = [] ) { |
| 377 |
$fields['group_id'] = $group_id; |
| 378 |
|
| 379 |
if ( groups_create_group( $fields ) ) { |
| 380 |
return [ 'success', 'Group updated.' ]; |
| 381 |
} |
| 382 |
|
| 383 |
return [ 'error', 'Group could not be updated.' ]; |
| 384 |
} |
| 385 |
); |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Get a list of groups. |
| 390 |
* |
| 391 |
* ## OPTIONS |
| 392 |
* |
| 393 |
* [--<field>=<value>] |
| 394 |
* : One or more parameters to pass. See groups_get_groups() |
| 395 |
* |
| 396 |
* [--fields=<fields>] |
| 397 |
* : Fields to display. |
| 398 |
* |
| 399 |
* [--user-id=<user>] |
| 400 |
* : Limit results to groups of which a specific user is a member. Accepts either a user_login or a numeric ID. |
| 401 |
* |
| 402 |
* [--orderby=<orderby>] |
| 403 |
* : Sort order for results. |
| 404 |
* --- |
| 405 |
* default: name |
| 406 |
* options: |
| 407 |
* - name |
| 408 |
* - date_created |
| 409 |
* - last_activity |
| 410 |
* - total_member_count |
| 411 |
* |
| 412 |
* [--count=<number>] |
| 413 |
* : Number of group to list. |
| 414 |
* --- |
| 415 |
* default: 50 |
| 416 |
* --- |
| 417 |
* |
| 418 |
* [--format=<format>] |
| 419 |
* : Render output in a particular format. |
| 420 |
* --- |
| 421 |
* default: table |
| 422 |
* options: |
| 423 |
* - table |
| 424 |
* - ids |
| 425 |
* - count |
| 426 |
* - csv |
| 427 |
* - json |
| 428 |
* - yaml |
| 429 |
* --- |
| 430 |
* |
| 431 |
* ## AVAILABLE FIELDS |
| 432 |
* |
| 433 |
* These fields will be displayed by default for each group: |
| 434 |
* |
| 435 |
* * id |
| 436 |
* * name |
| 437 |
* * slug |
| 438 |
* * status |
| 439 |
* * date_created |
| 440 |
* |
| 441 |
* ## EXAMPLES |
| 442 |
* |
| 443 |
* # List groups and get the count. |
| 444 |
* $ wp bp group list --format=count |
| 445 |
* 100 |
| 446 |
* |
| 447 |
* # List groups and get the IDs. |
| 448 |
* $ wp bp group list --format=ids |
| 449 |
* 70 71 72 73 74 |
| 450 |
* |
| 451 |
* # List groups. |
| 452 |
* $ wp bp group list |
| 453 |
* +----+------------+---------+---------+---------------------+ |
| 454 |
* | id | name | slug | status | date_created | |
| 455 |
* +----+------------+---------+---------+---------------------+ |
| 456 |
* | 1 | Group - #0 | group-0 | hidden | 2022-07-04 02:12:02 | |
| 457 |
* | 2 | Group - #1 | group-1 | hidden | 2022-07-04 02:12:02 | |
| 458 |
* | 4 | Group - #3 | group-3 | private | 2022-07-04 02:12:02 | |
| 459 |
* | 5 | Group - #4 | group-4 | private | 2022-07-04 02:12:02 | |
| 460 |
* | 3 | Group – #2 | group-2 | public | 2022-07-04 02:12:02 | |
| 461 |
* +----+------------+---------+---------+---------------------+ |
| 462 |
* |
| 463 |
* @subcommand list |
| 464 |
*/ |
| 465 |
public function list_( $args, $assoc_args ) { |
| 466 |
$formatter = $this->get_formatter( $assoc_args ); |
| 467 |
$query_args = [ |
| 468 |
'show_hidden' => true, |
| 469 |
'orderby' => $assoc_args['orderby'], |
| 470 |
'per_page' => $assoc_args['count'], |
| 471 |
]; |
| 472 |
|
| 473 |
if ( isset( $assoc_args['user-id'] ) ) { |
| 474 |
$user = $this->get_user_id_from_identifier( $assoc_args['user-id'] ); |
| 475 |
$query_args['user_id'] = $user->ID; |
| 476 |
} |
| 477 |
|
| 478 |
$query_args = self::process_csv_arguments_to_arrays( $query_args ); |
| 479 |
|
| 480 |
// If count or ids, no need for group objects. |
| 481 |
if ( in_array( $formatter->format, [ 'ids', 'count' ], true ) ) { |
| 482 |
$query_args['fields'] = 'ids'; |
| 483 |
} |
| 484 |
|
| 485 |
$groups = groups_get_groups( $query_args ); |
| 486 |
|
| 487 |
if ( empty( $groups['groups'] ) ) { |
| 488 |
WP_CLI::error( 'No groups found.' ); |
| 489 |
} |
| 490 |
|
| 491 |
$formatter->display_items( $groups['groups'] ); |
| 492 |
} |
| 493 |
|
| 494 |
/** |
| 495 |
* Gets a randon group status. |
| 496 |
* |
| 497 |
* @since 1.5.0 |
| 498 |
* |
| 499 |
* @param string $status Group status. |
| 500 |
* @return string |
| 501 |
*/ |
| 502 |
protected function random_group_status( $status ) { |
| 503 |
$core_status = [ 'public', 'private', 'hidden' ]; |
| 504 |
|
| 505 |
if ( 'mixed' === $status ) { |
| 506 |
$status = $core_status[ array_rand( $core_status ) ]; |
| 507 |
} |
| 508 |
|
| 509 |
return $status; |
| 510 |
} |
| 511 |
} |
| 512 |
|