| 1 |
<?php |
| 2 |
|
| 3 |
namespace Buddypress\CLI\Command; |
| 4 |
|
| 5 |
use WP_CLI; |
| 6 |
|
| 7 |
/** |
| 8 |
* Manage BuddyPress activity favorites. |
| 9 |
* |
| 10 |
* ## EXAMPLES |
| 11 |
* |
| 12 |
* # Add an activity item as a favorite for a user. |
| 13 |
* $ wp bp activity favorite add 100 500 |
| 14 |
* Success: Activity item added as a favorite for the user. |
| 15 |
* |
| 16 |
* # Add an activity item as a favorite for a user using user_login. |
| 17 |
* $ wp bp activity favorite create 100 user_test |
| 18 |
* Success: Activity item added as a favorite for the user. |
| 19 |
* |
| 20 |
* @since 1.5.0 |
| 21 |
*/ |
| 22 |
class Activity_Favorite extends BuddyPressCommand { |
| 23 |
|
| 24 |
/** |
| 25 |
* Object fields. |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
protected $obj_fields = [ |
| 30 |
'id', |
| 31 |
'user_id', |
| 32 |
'component', |
| 33 |
'type', |
| 34 |
'action', |
| 35 |
'item_id', |
| 36 |
'primary_link', |
| 37 |
'secondary_item_id', |
| 38 |
'date_recorded', |
| 39 |
'hide_sitewide', |
| 40 |
'is_spam', |
| 41 |
]; |
| 42 |
|
| 43 |
/** |
| 44 |
* Add an activity item as a favorite for a user. |
| 45 |
* |
| 46 |
* ## OPTIONS |
| 47 |
* |
| 48 |
* <activity-id> |
| 49 |
* : ID of the activity. |
| 50 |
* |
| 51 |
* <user> |
| 52 |
* : Identifier for the user. Accepts either a user_login or a numeric ID. |
| 53 |
* |
| 54 |
* ## EXAMPLES |
| 55 |
* |
| 56 |
* # Add an activity item as a favorite. |
| 57 |
* $ wp bp activity favorite add 100 500 |
| 58 |
* Success: Activity item added as a favorite for the user. |
| 59 |
* |
| 60 |
* # Add an activity item as a favorite using a user_login identifier. |
| 61 |
* $ wp bp activity favorite create 100 user_test |
| 62 |
* Success: Activity item added as a favorite for the user. |
| 63 |
* |
| 64 |
* @alias add |
| 65 |
*/ |
| 66 |
public function create( $args ) { |
| 67 |
$activity_id = $args[0]; |
| 68 |
|
| 69 |
if ( ! is_numeric( $activity_id ) ) { |
| 70 |
WP_CLI::error( 'Please provide a numeric activity ID.' ); |
| 71 |
} |
| 72 |
|
| 73 |
$activity = bp_activity_get_specific( |
| 74 |
[ |
| 75 |
'activity_ids' => $activity_id, |
| 76 |
'spam' => null, |
| 77 |
'display_comments' => true, |
| 78 |
] |
| 79 |
); |
| 80 |
|
| 81 |
if ( ! isset( $activity['activities'][0] ) || ! is_object( $activity['activities'][0] ) ) { |
| 82 |
WP_CLI::error( 'No activity found.' ); |
| 83 |
} |
| 84 |
|
| 85 |
$activity = $activity['activities'][0]; |
| 86 |
$user = $this->get_user_id_from_identifier( $args[1] ); |
| 87 |
|
| 88 |
if ( bp_activity_add_user_favorite( $activity->id, $user->ID ) ) { |
| 89 |
WP_CLI::success( 'Activity item added as a favorite for the user.' ); |
| 90 |
} else { |
| 91 |
WP_CLI::error( 'Could not add the activity item.' ); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Remove an activity item as a favorite for a user. |
| 97 |
* |
| 98 |
* ## OPTIONS |
| 99 |
* |
| 100 |
* <activity-id> |
| 101 |
* : ID of the activity. |
| 102 |
* |
| 103 |
* <user> |
| 104 |
* : Identifier for the user. Accepts either a user_login or a numeric ID. |
| 105 |
* |
| 106 |
* [--yes] |
| 107 |
* : Answer yes to the confirmation message. |
| 108 |
* |
| 109 |
* ## EXAMPLES |
| 110 |
* |
| 111 |
* # Remove an activity item as a favorite for a user. |
| 112 |
* $ wp bp activity favorite remove 100 500 |
| 113 |
* Success: Activity item removed as a favorite for the user. |
| 114 |
* |
| 115 |
* # Remove an activity item as a favorite for a user. |
| 116 |
* $ wp bp activity favorite delete 100 user_test --yes |
| 117 |
* Success: Activity item removed as a favorite for the user. |
| 118 |
* |
| 119 |
* @alias remove |
| 120 |
* @alias trash |
| 121 |
*/ |
| 122 |
public function delete( $args, $assoc_args ) { |
| 123 |
$activity_id = $args[0]; |
| 124 |
|
| 125 |
if ( ! is_numeric( $activity_id ) ) { |
| 126 |
WP_CLI::error( 'Please provide a numeric activity ID.' ); |
| 127 |
} |
| 128 |
|
| 129 |
$activity = bp_activity_get_specific( |
| 130 |
[ |
| 131 |
'activity_ids' => $activity_id, |
| 132 |
'spam' => null, |
| 133 |
'display_comments' => true, |
| 134 |
] |
| 135 |
); |
| 136 |
|
| 137 |
if ( ! isset( $activity['activities'][0] ) || ! is_object( $activity['activities'][0] ) ) { |
| 138 |
WP_CLI::error( 'No activity found.' ); |
| 139 |
} |
| 140 |
|
| 141 |
$activity = $activity['activities'][0]; |
| 142 |
$user = $this->get_user_id_from_identifier( $args[1] ); |
| 143 |
|
| 144 |
WP_CLI::confirm( 'Are you sure you want to remove this activity item?', $assoc_args ); |
| 145 |
|
| 146 |
if ( bp_activity_remove_user_favorite( $activity->id, $user->ID ) ) { |
| 147 |
WP_CLI::success( 'Activity item removed as a favorite for the user.' ); |
| 148 |
} else { |
| 149 |
WP_CLI::error( 'Could not remove the activity item.' ); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Get a user's favorite activity items. |
| 155 |
* |
| 156 |
* ## OPTIONS |
| 157 |
* |
| 158 |
* <user> |
| 159 |
* : Identifier for the user. Accepts either a user_login or a numeric ID. |
| 160 |
* |
| 161 |
* [--<field>=<value>] |
| 162 |
* : One or more parameters to pass to \BP_Activity_Activity::get() |
| 163 |
* |
| 164 |
* [--count=<number>] |
| 165 |
* : How many activity favorites to list. |
| 166 |
* --- |
| 167 |
* default: 50 |
| 168 |
* --- |
| 169 |
* |
| 170 |
* [--format=<format>] |
| 171 |
* : Render output in a particular format. |
| 172 |
* --- |
| 173 |
* default: table |
| 174 |
* options: |
| 175 |
* - table |
| 176 |
* - csv |
| 177 |
* - ids |
| 178 |
* - json |
| 179 |
* - count |
| 180 |
* - yaml |
| 181 |
* --- |
| 182 |
* |
| 183 |
* ## EXAMPLE |
| 184 |
* |
| 185 |
* # Get a user's favorite activity items. |
| 186 |
* $ wp bp activity favorite list 315 |
| 187 |
* |
| 188 |
* @subcommand list |
| 189 |
* @alias user-items |
| 190 |
*/ |
| 191 |
public function list_( $args, $assoc_args ) { |
| 192 |
$user = $this->get_user_id_from_identifier( $args[0] ); |
| 193 |
$favorites = bp_activity_get_user_favorites( $user->ID ); |
| 194 |
|
| 195 |
if ( empty( $favorites ) ) { |
| 196 |
WP_CLI::error( 'No favorite found for this user.' ); |
| 197 |
} |
| 198 |
|
| 199 |
$activities = bp_activity_get_specific( |
| 200 |
[ |
| 201 |
'activity_ids' => (array) $favorites, |
| 202 |
'per_page' => $assoc_args['count'], |
| 203 |
] |
| 204 |
); |
| 205 |
|
| 206 |
// Sanity check. |
| 207 |
if ( empty( $activities['activities'] ) ) { |
| 208 |
WP_CLI::error( 'No favorite found for this user.' ); |
| 209 |
} |
| 210 |
|
| 211 |
$activities = $activities['activities']; |
| 212 |
$formatter = $this->get_formatter( $assoc_args ); |
| 213 |
$formatter->display_items( 'ids' === $formatter->format ? wp_list_pluck( $activities, 'id' ) : $activities ); |
| 214 |
} |
| 215 |
} |
| 216 |
|