From e67aef06d21ecbeeda91e87eb4d1eb1187fcb62a Mon Sep 17 00:00:00 2001 From: Renato Alves Date: Fri, 31 Aug 2018 11:11:20 -0300 Subject: [PATCH] Notification: Adding code review suggestions --- component.php | 6 +- components/notification.php | 100 +++++++++++++++++++++++----------- features/notification.feature | 7 ++- 3 files changed, 75 insertions(+), 38 deletions(-) diff --git a/component.php b/component.php index 8049e59..14fe28c 100644 --- a/component.php +++ b/component.php @@ -113,16 +113,14 @@ protected function sanitize_string( $type ) { } /** - * Pull up a random active component for usage. + * Pull up a random active component. * * @since 1.1 * * @return string */ protected function get_random_component() { - $c = buddypress()->active_components; - - // Core components that accept activity items. + $c = buddypress()->active_components; $ca = $this->get_components_and_actions(); return array_rand( array_flip( array_intersect( array_keys( $c ), array_keys( $ca ) ) ) ); diff --git a/components/notification.php b/components/notification.php index 516e5bb..a6b3f18 100644 --- a/components/notification.php +++ b/components/notification.php @@ -90,11 +90,6 @@ public function create( $args, $assoc_args ) { 'date' => bp_core_current_time(), ) ); - // Fill in the component. - if ( empty( $r['component'] ) ) { - $r['component'] = $this->get_random_component(); - } - $id = bp_notifications_add_notification( array( 'user_id' => $r['user-id'], 'item_id' => $r['item-id'], @@ -185,35 +180,45 @@ public function get( $args, $assoc_args ) { * * ## EXAMPLES * - * $ wp bp notification delete 520 - * Success: Notification deleted. + * $ wp bp notification delete 520 --yes + * Success: Deleted notification 520. * * $ wp bp notification delete 55654 54564 --yes - * Success: Notification deleted. + * Success: Deleted notification 55654. + * Success: Deleted notification 54564. + * + * $ wp bp notification delete $(wp bp notification list --format=ids) --yes + * Success: Deleted notification 35456465. + * Success: Deleted notification 46546546. + * Success: Deleted notification 46465465. * * @alias trash */ public function delete( $args, $assoc_args ) { - $notification_id = $args[0]; + $notifications = $args; - WP_CLI::confirm( 'Are you sure you want to delete this notification?', $assoc_args ); + if ( count( $notifications ) > 1 ) { + WP_CLI::confirm( 'Are you sure want to delete these notifications?', $assoc_args ); + } else { + WP_CLI::confirm( 'Are you sure you want to delete this notification?', $assoc_args ); + } - parent::_delete( array( $notification_id ), $assoc_args, function( $notification_id ) { + parent::_delete( $notifications, $assoc_args, function( $notification_id ) { $notification = bp_notifications_get_notification( $notification_id ); if ( empty( $notification->id ) ) { - WP_CLI::error( 'No notification found by that ID.' ); + WP_CLI::error( sprintf( 'No notification found by ID %d.', $notification_id ) ); } if ( ! is_object( $notification ) ) { - WP_CLI::error( 'Could not find the notification.' ); + WP_CLI::error( sprintf( 'Could not find the notification %d.', $notification_id ) ); } if ( \BP_Notifications_Notification::delete( array( 'id' => $notification_id ) ) ) { - return array( 'success', 'Notification deleted.' ); + return array( 'success', sprintf( 'Deleted notification %d.', $notification_id ) ); } else { - return array( 'error', 'Could not delete notification.' ); + return array( 'error', sprintf( 'Could not delete notification %d.', $notification_id ) ); } } ); } @@ -223,18 +228,6 @@ public function delete( $args, $assoc_args ) { * * ## OPTIONS * - * [--action=] - * : Name of the action to associate the notification. (comment_reply, update_reply, etc). - * --- - * default: comment_reply - * --- - * - * [--component=] - * : The component for the notification item (groups, activity, etc). - * --- - * default: groups - * --- - * * [--count=] * : How many notifications to generate. * --- @@ -249,10 +242,13 @@ public function generate( $args, $assoc_args ) { $notify = WP_CLI\Utils\make_progress_bar( 'Generating notifications', $assoc_args['count'] ); for ( $i = 0; $i < $assoc_args['count']; $i++ ) { + + $component = $this->get_random_component(); + $this->create( array(), array( 'user-id' => $this->get_random_user_id(), - 'component' => $assoc_args['component'], - 'action' => $assoc_args['action'], + 'component' => $component, + 'action' => $this->get_random_action( $component ), 'silent', ) ); @@ -303,9 +299,15 @@ public function generate( $args, $assoc_args ) { * ## EXAMPLES * * $ wp bp notification list --format=ids + * 15 25 34 37 198 + * * $ wp bp notification list --format=count - * $ wp bp notification list --user-id=123 - * $ wp bp notification list --user-id=user_login --format=ids + * 10 + * + * $ wp bp notification list --fields=id,user_id + * | id | user_id | + * | 66546 | 656 | + * | 54554 | 646546 | * * @subcommand list */ @@ -347,4 +349,40 @@ public function _list( $args, $assoc_args ) { $formatter->display_items( $notifications ); } } + + /** + * Get random notification actions based on component. + * + * @since 1.8.0 + * + * @param string $component BuddyPress Component. + * + * @return string + */ + protected function get_random_action( $component ) { + $bp = buddypress(); + $actions = ''; + + // Activity. + if ( $bp->activity->id === $component ) { + $actions = [ 'comment_reply', 'update_reply', 'new_at_mention' ]; + } + + // Friendship. + if ( $bp->friends->id === $component ) { + $actions = [ 'friendship_request', 'friendship_accepted' ]; + } + + // Groups. + if ( $bp->groups->id === $component ) { + $actions = [ 'new_membership_request', 'membership_request_accepted', 'membership_request_rejected', 'member_promoted_to_admin', 'member_promoted_to_mod', 'group_invite' ]; + } + + // Messages. + if ( $bp->messages->id === $component ) { + $actions = [ 'new_message' ]; + } + + return array_rand( $actions ); + } } diff --git a/features/notification.feature b/features/notification.feature index dab99ec..9c01a98 100644 --- a/features/notification.feature +++ b/features/notification.feature @@ -33,7 +33,7 @@ Feature: Manage BuddyPress Notifications When I run `wp bp notification delete {NOTIFICATION_ID} --yes` Then STDOUT should contain: """ - Success: Notification deleted. + Success: Deleted notification {NOTIFICATION_ID}. """ Scenario: Notification list @@ -61,8 +61,9 @@ Feature: Manage BuddyPress Notifications | {NOTIFICATION_ONE_ID} | {MEMBER_ONE_ID} | | {NOTIFICATION_TWO_ID} | {MEMBER_TWO_ID} | - When I run `wp bp notification delete {NOTIFICATION_ONE_ID} --yes` + When I run `wp bp notification delete {NOTIFICATION_ONE_ID} {NOTIFICATION_TWO_ID} --yes` Then STDOUT should contain: """ - Success: Notification deleted. + Success: Deleted notification {NOTIFICATION_ONE_ID}. + Success: Deleted notification {NOTIFICATION_TWO_ID}. """