Skip to content

Commit

Permalink
Support for the --format flag of the generate commands added
Browse files Browse the repository at this point in the history
  • Loading branch information
renatonascalves committed Feb 12, 2024
1 parent 101de21 commit 088664a
Show file tree
Hide file tree
Showing 10 changed files with 415 additions and 181 deletions.
26 changes: 15 additions & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,32 @@ This library adheres to [Semantic Versioning](https://semver.org/) and [Keep a C

* Support for the `--silence` flag introduced to a few `create` commands.
* Support for the `--silence` flag introduced to the `wp bp activity comment` command.
* Support for the `--format` flag of the `generate` commands. One can render an output in a particular format.
* New commands:
* `wp bp notice` - Used to manage Sitewide notices.
* `wp bp tool reinstall` - Alias of the `wp bp email reinstall` command, we will deprecate the latter in the future.
* `delete-comment` and `remove-comment` alias added for the `wp bp activity` command.
* `wp bp notice` - Use it to manage Sitewide notices.
* `wp bp tool reinstall` - Alias of the `wp bp email reinstall` command, we will deprecate the latter in the future.
* `delete-comment` and `remove-comment` aliases added for the `wp bp activity` command.

### Changed

* Prefer short array syntax (This is different from WCS supports)
* Composer: packages upgraded
* Misc linting updates
* Github Action: Testing against PHP 8.3
* Composer packages upgraded to their latest versions
* Several linting updates
* CI tests against PHP 8.3 now
* Updated deprecated function from `bp_get_group_permalink` into `bp_get_group_url`
* Activity: make tests more deterministic
* Activity command: make tests more deterministic
* PHPDoc improvements
* All `create` commands' output were standardized
* All `delete` commands' output were standardized
* All `generate` commands' output were standardized
* All `create` commands' output were standardized
* All `delete` commands' output were standardized
* All `delete` commands' alias were standardized
* Invalid `format` option `haml` removed
* `post-update` command updated to remove mention of default text creation
* Confirmation message updated for `delete` commands that accepts multiple values
* `wp bp group invite remove` updated to `wp bp group invite uninvite` to avoid conflict with the delete/remove command
* `delete_comment` updated to `delete-comment`
* `wp bp group invite remove` updated to `wp bp group invite uninvite` to avoid conflict with the `delete/remove` command
* `wp bp activity delete_comment` updated to `wp bp activity delete-comment`
* `generate` commands: avoid refetching user objects
* All `generate` command pass the `silent` flag

## 2.0.2

Expand Down
59 changes: 35 additions & 24 deletions src/activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public function list_( $args, $assoc_args ) { // phpcs:ignore PSR2.Methods.Metho
* ## OPTIONS
*
* [--count=<number>]
* : How many activity items to generate.
* : How many activities to generate.
* ---
* default: 100
* ---
Expand All @@ -335,41 +335,52 @@ public function list_( $args, $assoc_args ) { // phpcs:ignore PSR2.Methods.Metho
* default: 1
* ---
*
* ## EXAMPLE
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: progress
* options:
* - progress
* - ids
* ---
*
* ## EXAMPLES
*
* # Generate 5 activity items.
* $ wp bp activity generate --count=5
* Success: Successfully created new activity item (ID #57)
* Success: Successfully created new activity item (ID #58)
* Success: Successfully created new activity item (ID #59)
* Success: Successfully created new activity item (ID #60)
* Success: Successfully created new activity item (ID #61)
* Generating activities 100% [======================] 0:00 / 0:00
*
* # Generate 5 activity items and output only the IDs.
* $ wp bp activity generate --count=5 --format=ids
* 70 71 72 73 74
*/
public function generate( $args, $assoc_args ) {
$component = $this->get_random_component();
$type = $this->get_random_type_from_component( $component );

if ( (bool) $assoc_args['skip-activity-comments'] && 'activity_comment' === $type ) {
$type = 'activity_update';
}
$this->generate_callback(
'Generating activities',
$assoc_args,
function ( $assoc_args, $format ) {
$component = $this->get_random_component();
$type = $this->get_random_type_from_component( $component );

$notify = WP_CLI\Utils\make_progress_bar( 'Generating activity items', $assoc_args['count'] );
if ( (bool) $assoc_args['skip-activity-comments'] && 'activity_comment' === $type ) {
$type = 'activity_update';
}

for ( $i = 0; $i < $assoc_args['count']; $i++ ) {
$this->create(
[],
[
$params = [
'component' => $component,
'type' => $type,
'content' => $this->generate_random_text(),
'silent' => true,
]
);
];

$notify->tick();
}
if ( 'ids' === $format ) {
$params['porcelain'] = true;
} else {
$params['silent'] = true;
}

$notify->finish();
return $this->create( [], $params );
}
);
}

/**
Expand Down
36 changes: 35 additions & 1 deletion src/command.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ protected function get_random_component() {
$c = buddypress()->active_components;
$ca = $this->get_components_and_actions();

return array_rand( array_flip( array_intersect( array_keys( $c ), array_keys( $ca ) ) ) );
return array_rand( (array) array_flip( array_intersect( array_keys( $c ), array_keys( $ca ) ) ) );
}

/**
Expand All @@ -185,4 +185,38 @@ function ( $component ) {
(array) bp_activity_get_actions()
);
}

/**
* Generate callback.
*
* @param string $message Message to display.
* @param array $assoc_args Command arguments.
* @param callable $callback Callback to execute.
*/
public function generate_callback( $message, $assoc_args, $callback ) {
$format = WP_CLI\Utils\get_flag_value( $assoc_args, 'format', 'progress' );
$limit = $assoc_args['count'];
$notify = false;

if ( 'progress' === $format ) {
$notify = WP_CLI\Utils\make_progress_bar( $message, $limit );
}

for ( $index = 0; $index < $limit; $index++ ) {
$object_id = call_user_func( $callback, $assoc_args, $format );

if ( 'progress' === $format ) {
$notify->tick();
} elseif ( 'ids' === $format ) {
echo $object_id;
if ( $index < $limit - 1 ) {
echo ' ';
}
}
}

if ( 'progress' === $format ) {
$notify->finish();
}
}
}
73 changes: 47 additions & 26 deletions src/friends.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,44 +299,65 @@ public function list_( $args, $assoc_args ) { // phpcs:ignore PSR2.Methods.Metho
* [--friend=<user>]
* : ID of the second user. Accepts either a user_login or a numeric ID.
*
* [--force-accept]
* : Whether to force acceptance.
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: progress
* options:
* - progress
* - ids
* ---
*
* ## EXAMPLES
*
* # Generate 50 random friendships.
* $ wp bp friend generate --count=50
* Generating friendships 100% [======================] 0:00 / 0:00
*
* # Generate 50 friendships with a specific user.
* $ wp bp friend generate --initiator=121 --count=50
* Generating friendships 100% [======================] 0:00 / 0:00
*
* # Generate 5 random friendships and output only the IDs.
* $ wp bp friend generate --count=5 --format=ids
* 70 71 72 73 74
*/
public function generate( $args, $assoc_args ) {
$notify = WP_CLI\Utils\make_progress_bar( 'Generating friendships', $assoc_args['count'] );
$member_id = null;
$friend_id = null;

if ( isset( $assoc_args['initiator'] ) ) {
$user = $this->get_user_id_from_identifier( $assoc_args['initiator'] );
$member_id = $user->ID;
}

for ( $i = 0; $i < $assoc_args['count']; $i++ ) {
if ( isset( $assoc_args['friend'] ) ) {
$user_2 = $this->get_user_id_from_identifier( $assoc_args['friend'] );
$friend_id = $user_2->ID;
}

if ( isset( $assoc_args['initiator'] ) ) {
$user = $this->get_user_id_from_identifier( $assoc_args['initiator'] );
$member = $user->ID;
} else {
$member = $this->get_random_user_id();
}
$this->generate_callback(
'Generating friendships',
$assoc_args,
function ( $assoc_args, $format ) use ( $member_id, $friend_id ) {
if ( ! $member_id ) {
$member_id = $this->get_random_user_id();
}

if ( isset( $assoc_args['friend'] ) ) {
$user_2 = $this->get_user_id_from_identifier( $assoc_args['friend'] );
$friend = $user_2->ID;
} else {
$friend = $this->get_random_user_id();
}
if ( ! $friend_id ) {
$friend_id = $this->get_random_user_id();
}

$this->create(
[ $member, $friend ],
[
'silent',
'force-accept',
]
);
$params = [ 'force-accept' => true ];

$notify->tick();
}
if ( 'ids' === $format ) {
$params['porcelain'] = true;
} else {
$params['silent'] = true;
}

$notify->finish();
return $this->create( [ $member_id, $friend_id ], $params );
}
);
}
}
82 changes: 65 additions & 17 deletions src/group-invite.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,40 +232,88 @@ public function list_( $args, $assoc_args ) { // phpcs:ignore PSR2.Methods.Metho
}

/**
* Generate random group invitations.
* Generate group invitations.
*
* ## OPTIONS
*
* [--count=<number>]
* : How many groups invitations to generate.
* : How many group invitations to generate.
* ---
* default: 100
* ---
*
* ## EXAMPLE
* [--user-id=<user>]
* : ID of the first user. Accepts either a user_login or a numeric ID.
*
* [--inviter-id=<user>]
* : ID for the inviter. Accepts either a user_login or a numeric ID.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: progress
* options:
* - progress
* - ids
* ---
*
* ## EXAMPLES
*
* # Generate random group invitations.
* $ wp bp group invite generate --count=50
* Generating group invitations 100% [======================] 0:00 / 0:00
*
* # Generate random group invitations with a specific user.
* $ wp bp group invite generate --inviter-id=121 --count=5
* Generating group invitations 100% [======================] 0:00 / 0:00
*
* # Generate 5 random group invitations and output only the IDs.
* $ wp bp group invite generate --count=5 --format=ids
* 70 71 72 73 74
*/
public function generate( $args, $assoc_args ) {
$notify = WP_CLI\Utils\make_progress_bar( 'Generating random group invitations', $assoc_args['count'] );
$user_id = null;
$inviter_id = null;

for ( $i = 0; $i < $assoc_args['count']; $i++ ) {
$random_group = \BP_Groups_Group::get_random( 1, 1 );
if ( isset( $assoc_args['user-id'] ) ) {
$user = $this->get_user_id_from_identifier( $assoc_args['user-id'] );
$user_id = $user->ID;
}

$this->create(
[],
[
'user-id' => $this->get_random_user_id(),
if ( isset( $assoc_args['inviter-id'] ) ) {
$user_2 = $this->get_user_id_from_identifier( $assoc_args['inviter-id'] );
$inviter_id = $user_2->ID;
}

$this->generate_callback(
'Generating group invitations',
$assoc_args,
function ( $assoc_args, $format ) use ( $user_id, $inviter_id ) {
$random_group = \BP_Groups_Group::get_random( 1, 1 );

if ( ! $user_id ) {
$user_id = $this->get_random_user_id();
}

if ( ! $inviter_id ) {
$inviter_id = $this->get_random_user_id();
}

$params = [
'user-id' => $user_id,
'group-id' => $random_group['groups'][0]->slug,
'inviter-id' => $this->get_random_user_id(),
'silent' => true,
]
);
'inviter-id' => $inviter_id,
];

$notify->tick();
}
if ( 'ids' === $format ) {
$params['porcelain'] = true;
} else {
$params['silent'] = true;
}

$notify->finish();
return $this->create( [], $params );
}
);
}

/**
Expand Down
Loading

0 comments on commit 088664a

Please sign in to comment.