Skip to content

Commit

Permalink
Merge pull request #106 from buddypress/feature/generate-format
Browse files Browse the repository at this point in the history
Support for the `--format` flag of the `generate` commands added
  • Loading branch information
renatonascalves authored Feb 15, 2024
2 parents 101de21 + 6ca3401 commit bab292d
Show file tree
Hide file tree
Showing 15 changed files with 547 additions and 263 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
12 changes: 12 additions & 0 deletions features/activity.feature
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ Feature: Manage BuddyPress Activities
| id | user_id | component |
| {ACTIVITY_ID} | {MEMBER_ID} | activity |

When I run `wp bp activity list --format=count`
Then STDOUT should be:
"""
1
"""

When I run `wp bp activity list --format=ids`
Then STDOUT should be:
"""
{ACTIVITY_ID}
"""

When I run `wp bp activity comment {ACTIVITY_ID} --user-id={MEMBER_ID} --content="Activity Comment" --skip-notification --porcelain`
Then STDOUT should be a number
And save STDOUT as {COMMENT_ID}
Expand Down
41 changes: 25 additions & 16 deletions src/activity-favorite.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Activity_Favorite extends BuddyPressCommand {
* ## OPTIONS
*
* <activity-id>
* : ID of the activity to add an item to.
* : ID of the activity.
*
* <user>
* : Identifier for the user. Accepts either a user_login or a numeric ID.
Expand All @@ -64,20 +64,25 @@ class Activity_Favorite extends BuddyPressCommand {
* @alias add
*/
public function create( $args ) {
$activity_id = $args[0];

if ( ! is_numeric( $activity_id ) ) {
WP_CLI::error( 'Please provide a numeric activity ID.' );
}

$activity = bp_activity_get_specific(
[
'activity_ids' => $args[0],
'activity_ids' => $activity_id,
'spam' => null,
]
);

$activity = $activity['activities'][0];

if ( ! is_object( $activity ) ) {
WP_CLI::error( 'Could not find the activity.' );
if ( ! isset( $activity['activities'][0] ) || ! is_object( $activity['activities'][0] ) ) {
WP_CLI::error( 'No activity found.' );
}

$user = $this->get_user_id_from_identifier( $args[1] );
$activity = $activity['activities'][0];
$user = $this->get_user_id_from_identifier( $args[1] );

if ( bp_activity_add_user_favorite( $activity->id, $user->ID ) ) {
WP_CLI::success( 'Activity item added as a favorite for the user.' );
Expand All @@ -92,7 +97,7 @@ public function create( $args ) {
* ## OPTIONS
*
* <activity-id>
* : ID of the activity to remove a item to.
* : ID of the activity.
*
* <user>
* : Identifier for the user. Accepts either a user_login or a numeric ID.
Expand All @@ -114,21 +119,25 @@ public function create( $args ) {
* @alias trash
*/
public function delete( $args, $assoc_args ) {
$activity_id = $args[0];

if ( ! is_numeric( $activity_id ) ) {
WP_CLI::error( 'Please provide a numeric activity ID.' );
}

$activity = bp_activity_get_specific(
[
'activity_ids' => $args[0],
'spam' => null,
'display_comments' => true,
'activity_ids' => $activity_id,
'spam' => null,
]
);

$activity = $activity['activities'][0];

if ( ! is_object( $activity ) ) {
WP_CLI::error( 'Could not find the activity.' );
if ( ! isset( $activity['activities'][0] ) || ! is_object( $activity['activities'][0] ) ) {
WP_CLI::error( 'No activity found.' );
}

$user = $this->get_user_id_from_identifier( $args[1] );
$activity = $activity['activities'][0];
$user = $this->get_user_id_from_identifier( $args[1] );

WP_CLI::confirm( 'Are you sure you want to remove this activity item?', $assoc_args );

Expand Down
92 changes: 50 additions & 42 deletions src/activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,13 @@ public function create( $args, $assoc_args ) {
*
* ## EXAMPLES
*
* $ wp bp activity list --format=ids
* # List activities and get the count.
* $ wp bp activity list --format=count
* $ wp bp activity list --per_page=5
* $ wp bp activity list --search_terms="Activity Comment"
* $ wp bp activity list --user-id=10
* $ wp bp activity list --user-id=123 --component=groups
* 100
*
* # List activities and get the IDs.
* $ wp bp activity list --format=ids
* 70 71 72 73 74
*
* @subcommand list
*/
Expand All @@ -267,7 +268,6 @@ public function list_( $args, $assoc_args ) { // phpcs:ignore PSR2.Methods.Metho
[
'page' => 1,
'count' => 50,
'count_total' => false,
'show_hidden' => true,
'filter' => false,
]
Expand Down Expand Up @@ -304,17 +304,12 @@ public function list_( $args, $assoc_args ) { // phpcs:ignore PSR2.Methods.Metho
}

$activities = bp_activity_get( $r );

if ( empty( $activities['activities'] ) ) {
WP_CLI::error( 'No activities found.' );
}

if ( 'ids' === $formatter->format ) {
echo implode( ' ', $activities['activities'] );
} elseif ( 'count' === $formatter->format ) {
$formatter->display_items( $activities['total'] );
} else {
$formatter->display_items( $activities['activities'] );
}
$formatter->display_items( $activities['activities'] );
}

/**
Expand All @@ -323,7 +318,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 +330,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 Expand Up @@ -433,11 +439,13 @@ public function get( $args, $assoc_args ) {
WP_CLI::error( 'Please provide a numeric activity ID.' );
}

$activity = bp_activity_get_specific( [
'activity_ids' => $activity_id,
'spam' => null,
'display_comments' => true,
] );
$activity = bp_activity_get_specific(
[
'activity_ids' => $activity_id,
'spam' => null,
'display_comments' => true,
]
);

if ( ! isset( $activity['activities'][0] ) || ! is_object( $activity['activities'][0] ) ) {
WP_CLI::error( 'No activity found.' );
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();
}
}
}
Loading

0 comments on commit bab292d

Please sign in to comment.