Skip to content

Commit

Permalink
Notification: Adding wp bp notification create command
Browse files Browse the repository at this point in the history
  • Loading branch information
renatonascalves committed Jul 25, 2018
1 parent d5634e1 commit 96d2adf
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 16 deletions.
16 changes: 16 additions & 0 deletions component.php
Expand Up @@ -111,4 +111,20 @@ protected function get_field_id( $field_id ) {
protected function sanitize_string( $type ) {
return strtolower( str_replace( '-', '_', $type ) );
}

/**
* Pull up a random active component for usage.
*
* @since 1.1
*
* @return string
*/
protected function get_random_component() {
$c = buddypress()->active_components;

// Core components that accept activity items.
$ca = $this->get_components_and_actions();

return array_rand( array_flip( array_intersect( array_keys( $c ), array_keys( $ca ) ) ) );
}
}
16 changes: 0 additions & 16 deletions components/activity.php
Expand Up @@ -669,22 +669,6 @@ public function delete_comment( $args, $assoc_args ) {
}
}

/**
* Pull up a random active component for use in activity items.
*
* @since 1.1
*
* @return string
*/
protected function get_random_component() {
$c = buddypress()->active_components;

// Core components that accept activity items.
$ca = $this->get_components_and_actions();

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

/**
* Get a random type from a component.
*
Expand Down
101 changes: 101 additions & 0 deletions components/notification.php
@@ -0,0 +1,101 @@
<?php
namespace Buddypress\CLI\Command;

use WP_CLI;

/**
* Manage BuddyPress Notifications.
*
* @since 1.8.0
*/
class Notification extends BuddypressCommand {

/**
* Create an notification item.
*
* ## OPTIONS
*
* [--component=<component>]
* : The component for the notification item (groups, activity, etc). If
* none is provided, a component will be randomly selected from the
* active components.
*
* [--action=<action>]
* : Action text (eg "Joe created a new group Foo"). If none is
* provided, one will be generated automatically based on other params.
*
* [--user-id=<user>]
* : ID of the user associated with the new notification. If none is provided,
* a user will be randomly selected.
*
* [--item-id=<item-id>]
* : ID of the associated notification. If none is provided, one will be
* generated automatically.
*
* [--secondary-item-id=<secondary-item-id>]
* : ID of the secondary associated notification. If none is provided, one will
* be generated automatically.
*
* [--date-notified=<date-notified>]
* : GMT timestamp, in Y-m-d h:i:s format.
* ---
* Default: Current time
* ---
*
* [--silent]
* : Whether to silent the notification creation.
*
* [--porcelain]
* : Output only the new notification id.
*
* ## EXAMPLES
*
* $ wp bp notification create
* Success: Successfully created new notification. (ID #5464)
*
* $ wp bp notification add --component=groups --user-id=10
* Success: Successfully created new notification (ID #48949)
*
* @alias add
*/
public function create( $args, $assoc_args ) {
$r = wp_parse_args( $assoc_args, array(
'component' => '',
'action' => '',
'user-id' => '',
'item-id' => '',
'secondary-item-id' => '',
'date-notified' => bp_core_current_time(),
) );

// Fill in any missing information.
if ( empty( $r['component'] ) ) {
$r['component'] = $this->get_random_component();
}

$id = bp_notifications_add_notification( array(
'component_name' => $r['component'],
'component_action' => $r['action'],
'user_id' => $r['user-id'],
'item_id' => $r['item-id'],
'secondary_item_id' => $r['secondary-item-id'],
'date_notified' => $r['date-notified'],
) );

// Silent it before it errors.
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'silent' ) ) {
return;
}

if ( ! is_numeric( $id ) ) {
WP_CLI::error( 'Could not create notification item.' );
}

if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
WP_CLI::line( $id );
} else {
WP_CLI::success( sprintf( 'Successfully created new notification (ID #%d)', $id ) );
}
}

}
12 changes: 12 additions & 0 deletions features/notification.feature
@@ -0,0 +1,12 @@
Feature: Manage BuddyPress Notifications

Scenario: Notifications CRUD Operations
Given a BP install

When I run `wp user create testuser2 testuser2@example.com --first_name=test --last_name=user --role=subscriber --porcelain`
Then STDOUT should be a number
And save STDOUT as {MEMBER_ID}

When I run `wp bp notification create --component=groups --user-id={MEMBER_ID} --porcelain`
Then STDOUT should be a number
And save STDOUT as {NOTIFICATION_ID}
13 changes: 13 additions & 0 deletions wp-cli-bp.php
Expand Up @@ -26,6 +26,7 @@
require_once( __DIR__ . '/components/xprofile-data.php' );
require_once( __DIR__ . '/components/tool.php' );
require_once( __DIR__ . '/components/message.php' );
require_once( __DIR__ . '/components/notification.php' );
require_once( __DIR__ . '/components/email.php' );

WP_CLI::add_command( 'bp', __NAMESPACE__ . '\\Command\\Buddypress', array(
Expand All @@ -44,6 +45,18 @@
},
) );

WP_CLI::add_command( 'bp notification', __NAMESPACE__ . '\\Command\\Notification', array(
'before_invoke' => function() {
if ( ! class_exists( 'Buddypress' ) ) {
WP_CLI::error( 'The BuddyPress plugin is not active.' );
}

if ( ! bp_is_active( 'notifications' ) ) {
WP_CLI::error( 'The Notification component is not active.' );
}
},
) );

WP_CLI::add_command( 'bp email', __NAMESPACE__ . '\\Command\\Email', array(
'before_invoke' => function() {
if ( ! class_exists( 'Buddypress' ) ) {
Expand Down

0 comments on commit 96d2adf

Please sign in to comment.