Skip to content

Commit

Permalink
Notification: Adding code review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
renatonascalves committed Aug 31, 2018
1 parent dfd1066 commit e67aef0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 38 deletions.
6 changes: 2 additions & 4 deletions component.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) ) );
Expand Down
100 changes: 69 additions & 31 deletions components/notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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 ) );
}
} );
}
Expand All @@ -223,18 +228,6 @@ public function delete( $args, $assoc_args ) {
*
* ## OPTIONS
*
* [--action=<action>]
* : Name of the action to associate the notification. (comment_reply, update_reply, etc).
* ---
* default: comment_reply
* ---
*
* [--component=<component>]
* : The component for the notification item (groups, activity, etc).
* ---
* default: groups
* ---
*
* [--count=<number>]
* : How many notifications to generate.
* ---
Expand All @@ -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',
) );

Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 );
}
}
7 changes: 4 additions & 3 deletions features/notification.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}.
"""

0 comments on commit e67aef0

Please sign in to comment.