| 1 |
<?php |
| 2 |
|
| 3 |
namespace Buddypress\CLI\Command; |
| 4 |
|
| 5 |
use WP_CLI; |
| 6 |
|
| 7 |
/** |
| 8 |
* Manage BuddyPress Notifications. |
| 9 |
* |
| 10 |
* ## EXAMPLES |
| 11 |
* |
| 12 |
* # Create notification item. |
| 13 |
* $ wp bp notification create |
| 14 |
* Success: Successfully created new notification. (ID #5464) |
| 15 |
* |
| 16 |
* # Delete a notification item. |
| 17 |
* $ wp bp notification delete 520 |
| 18 |
* Success: Notification deleted. |
| 19 |
* |
| 20 |
* @since 1.8.0 |
| 21 |
*/ |
| 22 |
class Notification extends BuddyPressCommand { |
| 23 |
|
| 24 |
/** |
| 25 |
* Object fields. |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
protected $obj_fields = [ |
| 30 |
'id', |
| 31 |
'user_id', |
| 32 |
'item_id', |
| 33 |
'secondary_item_id', |
| 34 |
'component_name', |
| 35 |
'component_action', |
| 36 |
'date_notified', |
| 37 |
'is_new', |
| 38 |
]; |
| 39 |
|
| 40 |
/** |
| 41 |
* Dependency check for this CLI command. |
| 42 |
*/ |
| 43 |
public static function check_dependencies() { |
| 44 |
parent::check_dependencies(); |
| 45 |
|
| 46 |
if ( ! bp_is_active( 'notifications' ) ) { |
| 47 |
WP_CLI::error( 'The Notification component is not active.' ); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Create a notification. |
| 53 |
* |
| 54 |
* ## OPTIONS |
| 55 |
* |
| 56 |
* [--component=<component>] |
| 57 |
* : The component for the notification item (groups, activity, etc). If |
| 58 |
* none is provided, a component will be randomly selected from the |
| 59 |
* active components. |
| 60 |
* |
| 61 |
* [--action=<action>] |
| 62 |
* : Name of the action to associate the notification. (comment_reply, update_reply, etc). |
| 63 |
* |
| 64 |
* [--user-id=<user>] |
| 65 |
* : ID of the user associated with the new notification. |
| 66 |
* |
| 67 |
* [--item-id=<item>] |
| 68 |
* : ID of the associated notification. |
| 69 |
* |
| 70 |
* [--secondary-item-id=<item>] |
| 71 |
* : ID of the secondary associated notification. |
| 72 |
* |
| 73 |
* [--date=<date>] |
| 74 |
* : GMT timestamp, in Y-m-d h:i:s format. |
| 75 |
* |
| 76 |
* [--silent] |
| 77 |
* : Whether to silent the notification creation. |
| 78 |
* |
| 79 |
* [--porcelain] |
| 80 |
* : Output only the new notification id. |
| 81 |
* |
| 82 |
* ## EXAMPLES |
| 83 |
* |
| 84 |
* # Create a `update_reply` notification. |
| 85 |
* $ wp bp notification create --component=messages --action=update_reply --user-id=523 |
| 86 |
* Success: Successfully created new notification. (ID #5464) |
| 87 |
* |
| 88 |
* # Create a `comment_reply` notification. |
| 89 |
* $ wp bp notification add --component=groups --action=comment_reply --user-id=10 |
| 90 |
* Success: Successfully created new notification (ID #48949) |
| 91 |
* |
| 92 |
* @alias add |
| 93 |
*/ |
| 94 |
public function create( $args, $assoc_args ) { |
| 95 |
$r = wp_parse_args( |
| 96 |
$assoc_args, |
| 97 |
[ |
| 98 |
'component' => '', |
| 99 |
'action' => '', |
| 100 |
'user-id' => 0, |
| 101 |
'item-id' => 0, |
| 102 |
'secondary-item-id' => 0, |
| 103 |
'date' => bp_core_current_time(), |
| 104 |
] |
| 105 |
); |
| 106 |
|
| 107 |
$notification_id = bp_notifications_add_notification( |
| 108 |
[ |
| 109 |
'user_id' => $r['user-id'], |
| 110 |
'item_id' => $r['item-id'], |
| 111 |
'secondary_item_id' => $r['secondary-item-id'], |
| 112 |
'component_name' => $r['component'], |
| 113 |
'component_action' => $r['action'], |
| 114 |
'date_notified' => $r['date'], |
| 115 |
] |
| 116 |
); |
| 117 |
|
| 118 |
// Silent it before it errors. |
| 119 |
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'silent' ) ) { |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
if ( ! is_numeric( $notification_id ) ) { |
| 124 |
WP_CLI::error( 'Could not create notification.' ); |
| 125 |
} |
| 126 |
|
| 127 |
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) { |
| 128 |
WP_CLI::log( $notification_id ); |
| 129 |
} else { |
| 130 |
WP_CLI::success( sprintf( 'Successfully created new notification (ID #%d)', $notification_id ) ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Get specific notification. |
| 136 |
* |
| 137 |
* ## OPTIONS |
| 138 |
* |
| 139 |
* <notification-id> |
| 140 |
* : Identifier for the notification. |
| 141 |
* |
| 142 |
* [--fields=<fields>] |
| 143 |
* : Limit the output to specific fields. |
| 144 |
* |
| 145 |
* [--format=<format>] |
| 146 |
* : Render output in a particular format. |
| 147 |
* --- |
| 148 |
* default: table |
| 149 |
* options: |
| 150 |
* - table |
| 151 |
* - json |
| 152 |
* - csv |
| 153 |
* - yaml |
| 154 |
* --- |
| 155 |
* |
| 156 |
* ## EXAMPLES |
| 157 |
* |
| 158 |
* # Get a notification by ID. |
| 159 |
* $ wp bp notification get 10071 |
| 160 |
* +-------------------+---------------------+ |
| 161 |
* | Field | Value | |
| 162 |
* +-------------------+---------------------+ |
| 163 |
* | id | 10071 | |
| 164 |
* | item_id | 0 | |
| 165 |
* | secondary_item_id | 0 | |
| 166 |
* | user_id | 7 | |
| 167 |
* | component_name | activity | |
| 168 |
* | component_action | comment_reply | |
| 169 |
* | date_notified | 2024-02-06 00:28:45 | |
| 170 |
* | is_new | 1 | |
| 171 |
* +-------------------+---------------------+ |
| 172 |
* |
| 173 |
* # Get a notification in JSON format. |
| 174 |
* $ wp bp notification get 10071 --format=json |
| 175 |
* {"id":10071,"item_id":0,"secondary_item_id":0,"user_id":7,"component_name":"activity","component_action":"comment_reply","date_notified":"2024-02-06 00:28:45","is_new":1} |
| 176 |
* |
| 177 |
* # Get a notification using a invalid ID. |
| 178 |
* $ wp bp notification see buddypress |
| 179 |
* Error: Please provide a numeric notification ID. |
| 180 |
* |
| 181 |
* @alias see |
| 182 |
*/ |
| 183 |
public function get( $args, $assoc_args ) { |
| 184 |
$notification_id = $args[0]; |
| 185 |
|
| 186 |
if ( ! is_numeric( $notification_id ) ) { |
| 187 |
WP_CLI::error( 'Please provide a numeric notification ID.' ); |
| 188 |
} |
| 189 |
|
| 190 |
$notification = bp_notifications_get_notification( $notification_id ); |
| 191 |
|
| 192 |
if ( empty( $notification->id ) || ! is_object( $notification ) ) { |
| 193 |
WP_CLI::error( 'No notification found.' ); |
| 194 |
} |
| 195 |
|
| 196 |
$notification_arr = get_object_vars( $notification ); |
| 197 |
|
| 198 |
if ( empty( $assoc_args['fields'] ) ) { |
| 199 |
$assoc_args['fields'] = array_keys( $notification_arr ); |
| 200 |
} |
| 201 |
|
| 202 |
$this->get_formatter( $assoc_args )->display_item( $notification_arr ); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Delete a notification. |
| 207 |
* |
| 208 |
* ## OPTIONS |
| 209 |
* |
| 210 |
* <notification-id>... |
| 211 |
* : ID or IDs of notification to delete. |
| 212 |
* |
| 213 |
* [--yes] |
| 214 |
* : Answer yes to the confirmation message. |
| 215 |
* |
| 216 |
* ## EXAMPLES |
| 217 |
* |
| 218 |
* # Delete a notification. |
| 219 |
* $ wp bp notification delete 520 --yes |
| 220 |
* Success: Deleted notification 520. |
| 221 |
* |
| 222 |
* # Delete multiple notifications. |
| 223 |
* $ wp bp notification delete 55654 54564 --yes |
| 224 |
* Success: Deleted notification 55654. |
| 225 |
* Success: Deleted notification 54564. |
| 226 |
* |
| 227 |
* @alias remove |
| 228 |
* @alias trash |
| 229 |
*/ |
| 230 |
public function delete( $args, $assoc_args ) { |
| 231 |
$notifications = wp_parse_id_list( $args ); |
| 232 |
|
| 233 |
if ( count( $notifications ) > 1 ) { |
| 234 |
WP_CLI::confirm( 'Are you sure you want to delete these notifications?', $assoc_args ); |
| 235 |
} else { |
| 236 |
WP_CLI::confirm( 'Are you sure you want to delete this notification?', $assoc_args ); |
| 237 |
} |
| 238 |
|
| 239 |
parent::_delete( |
| 240 |
$notifications, |
| 241 |
$assoc_args, |
| 242 |
function ( $notification_id ) { |
| 243 |
if ( \BP_Notifications_Notification::delete( [ 'id' => $notification_id ] ) ) { |
| 244 |
return [ 'success', sprintf( 'Deleted notification %d.', $notification_id ) ]; |
| 245 |
} |
| 246 |
|
| 247 |
return [ 'error', sprintf( 'Could not delete notification %d.', $notification_id ) ]; |
| 248 |
} |
| 249 |
); |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Generate random notifications. |
| 254 |
* |
| 255 |
* ## OPTIONS |
| 256 |
* |
| 257 |
* [--count=<number>] |
| 258 |
* : How many notifications to generate. |
| 259 |
* --- |
| 260 |
* default: 100 |
| 261 |
* --- |
| 262 |
* |
| 263 |
* [--user-id=<user>] |
| 264 |
* : ID of the user. Accepts either a user_login or a numeric ID. |
| 265 |
* |
| 266 |
* [--format=<format>] |
| 267 |
* : Render output in a particular format. |
| 268 |
* --- |
| 269 |
* default: progress |
| 270 |
* options: |
| 271 |
* - progress |
| 272 |
* - ids |
| 273 |
* --- |
| 274 |
* |
| 275 |
* ## EXAMPLES |
| 276 |
* |
| 277 |
* # Generate 5 random notifications. |
| 278 |
* $ wp bp notification generate --count=5 |
| 279 |
* Generating notifications 100% [======================] 0:00 / 0:00 |
| 280 |
* |
| 281 |
* # Generate 5 random notifications and output only the IDs. |
| 282 |
* $ wp bp notification generate --count=5 --format=ids |
| 283 |
* 70 71 72 73 74 |
| 284 |
*/ |
| 285 |
public function generate( $args, $assoc_args ) { |
| 286 |
$user_id = null; |
| 287 |
|
| 288 |
if ( isset( $assoc_args['user-id'] ) ) { |
| 289 |
$user = $this->get_user_id_from_identifier( $assoc_args['user-id'] ); |
| 290 |
$user_id = $user->ID; |
| 291 |
} |
| 292 |
|
| 293 |
$this->generate_callback( |
| 294 |
'Generating notifications', |
| 295 |
$assoc_args, |
| 296 |
function ( $assoc_args, $format ) use ( $user_id ) { |
| 297 |
$component = $this->get_random_component(); |
| 298 |
|
| 299 |
if ( ! $user_id ) { |
| 300 |
$user_id = $this->get_random_user_id(); |
| 301 |
} |
| 302 |
|
| 303 |
$params = [ |
| 304 |
'user-id' => $user_id, |
| 305 |
'component' => $component, |
| 306 |
'action' => $this->get_random_action( $component ), |
| 307 |
]; |
| 308 |
|
| 309 |
if ( 'ids' === $format ) { |
| 310 |
$params['porcelain'] = true; |
| 311 |
} else { |
| 312 |
$params['silent'] = true; |
| 313 |
} |
| 314 |
|
| 315 |
return $this->create( [], $params ); |
| 316 |
} |
| 317 |
); |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Get a list of notifications. |
| 322 |
* |
| 323 |
* ## OPTIONS |
| 324 |
* |
| 325 |
* [--<field>=<value>] |
| 326 |
* : One or more parameters to pass. |
| 327 |
* |
| 328 |
* [--fields=<fields>] |
| 329 |
* : Fields to display. |
| 330 |
* |
| 331 |
* [--user-id=<user>] |
| 332 |
* : Limit results to a specific member. Accepts either a user_login or a numeric ID. |
| 333 |
* |
| 334 |
* [--component=<component>] |
| 335 |
* : The component to fetch notifications (groups, activity, etc). |
| 336 |
* |
| 337 |
* [--action=<action>] |
| 338 |
* : Name of the action to fetch notifications. (comment_reply, update_reply, etc). |
| 339 |
* |
| 340 |
* [--count=<number>] |
| 341 |
* : How many notification items to list. |
| 342 |
* --- |
| 343 |
* default: 50 |
| 344 |
* --- |
| 345 |
* |
| 346 |
* [--format=<format>] |
| 347 |
* : Render output in a particular format. |
| 348 |
* --- |
| 349 |
* default: table |
| 350 |
* options: |
| 351 |
* - table |
| 352 |
* - ids |
| 353 |
* - count |
| 354 |
* - csv |
| 355 |
* - json |
| 356 |
* - yaml |
| 357 |
* --- |
| 358 |
|
| 359 |
* ## EXAMPLES |
| 360 |
* |
| 361 |
* # List all notifications and output only the IDs. |
| 362 |
* $ wp bp notification list --format=ids |
| 363 |
* 15 25 34 37 198 |
| 364 |
* |
| 365 |
* # List all notifications and output the count. |
| 366 |
* $ wp bp notification list --format=count |
| 367 |
* 10 |
| 368 |
* |
| 369 |
* # List all notifications and output the IDs and user_id. |
| 370 |
* $ wp bp notification list --fields=id,user_id |
| 371 |
* | id | user_id | |
| 372 |
* | 66546 | 656 | |
| 373 |
* | 54554 | 646546 | |
| 374 |
* |
| 375 |
* @subcommand list |
| 376 |
*/ |
| 377 |
public function list_( $args, $assoc_args ) { |
| 378 |
$formatter = $this->get_formatter( $assoc_args ); |
| 379 |
$query_args = [ |
| 380 |
'update_meta_cache' => false, |
| 381 |
]; |
| 382 |
|
| 383 |
if ( isset( $assoc_args['user-id'] ) ) { |
| 384 |
$user = $this->get_user_id_from_identifier( $assoc_args['user-id'] ); |
| 385 |
$query_args['user_id'] = $user->ID; |
| 386 |
} |
| 387 |
|
| 388 |
if ( isset( $assoc_args['action'] ) ) { |
| 389 |
$query_args['component_action'] = $assoc_args['action']; |
| 390 |
} |
| 391 |
|
| 392 |
if ( isset( $assoc_args['component'] ) ) { |
| 393 |
$query_args['component_name'] = $assoc_args['component']; |
| 394 |
} |
| 395 |
|
| 396 |
$query_args['page'] = 1; |
| 397 |
$query_args['per_page'] = (int) $assoc_args['count']; |
| 398 |
|
| 399 |
unset( $query_args['count'] ); |
| 400 |
|
| 401 |
$query_args = self::process_csv_arguments_to_arrays( $query_args ); |
| 402 |
$notifications = \BP_Notifications_Notification::get( $query_args ); |
| 403 |
|
| 404 |
if ( empty( $notifications ) ) { |
| 405 |
WP_CLI::error( 'No notification items found.' ); |
| 406 |
} |
| 407 |
|
| 408 |
$formatter->display_items( 'ids' === $formatter->format ? wp_list_pluck( $notifications, 'id' ) : $notifications ); |
| 409 |
} |
| 410 |
|
| 411 |
/** |
| 412 |
* Get random notification actions based on component. |
| 413 |
* |
| 414 |
* @since 1.8.0 |
| 415 |
* |
| 416 |
* @param string $component BuddyPress Component. |
| 417 |
* @return string |
| 418 |
*/ |
| 419 |
protected function get_random_action( $component ) { |
| 420 |
$bp = buddypress(); |
| 421 |
$actions = ''; |
| 422 |
|
| 423 |
// Activity. |
| 424 |
if ( $bp->activity->id === $component ) { |
| 425 |
$actions = [ 'comment_reply', 'update_reply', 'new_at_mention' ]; |
| 426 |
} |
| 427 |
|
| 428 |
// Friendship. |
| 429 |
if ( $bp->friends->id === $component ) { |
| 430 |
$actions = [ |
| 431 |
'friendship_request', |
| 432 |
'friendship_accepted', |
| 433 |
]; |
| 434 |
} |
| 435 |
|
| 436 |
// Groups. |
| 437 |
if ( $bp->groups->id === $component ) { |
| 438 |
$actions = [ |
| 439 |
'new_membership_request', |
| 440 |
'membership_request_accepted', |
| 441 |
'membership_request_rejected', |
| 442 |
'member_promoted_to_admin', |
| 443 |
'member_promoted_to_mod', |
| 444 |
'group_invite', |
| 445 |
]; |
| 446 |
} |
| 447 |
|
| 448 |
// Messages. |
| 449 |
if ( $bp->messages->id === $component ) { |
| 450 |
$actions = [ 'new_message' ]; |
| 451 |
} |
| 452 |
|
| 453 |
return array_rand( $actions ); |
| 454 |
} |
| 455 |
} |
| 456 |
|