| 1 |
<?php |
| 2 |
|
| 3 |
namespace Buddypress\CLI\Command; |
| 4 |
|
| 5 |
use WP_CLI; |
| 6 |
|
| 7 |
/** |
| 8 |
* Manage BuddyPress Friends. |
| 9 |
* |
| 10 |
* ## EXAMPLES |
| 11 |
* |
| 12 |
* $ wp bp friend create user1 another_use |
| 13 |
* Success: Friendship successfully created. |
| 14 |
* |
| 15 |
* $ wp bp friend create user1 another_use --force-accept |
| 16 |
* Success: Friendship successfully created. |
| 17 |
* |
| 18 |
* @since 1.6.0 |
| 19 |
*/ |
| 20 |
class Friends extends BuddyPressCommand { |
| 21 |
|
| 22 |
/** |
| 23 |
* Object fields. |
| 24 |
* |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
protected $obj_fields = [ |
| 28 |
'id', |
| 29 |
'initiator_user_id', |
| 30 |
'friend_user_id', |
| 31 |
'is_confirmed', |
| 32 |
'is_limited', |
| 33 |
]; |
| 34 |
|
| 35 |
/** |
| 36 |
* Dependency check for this CLI command. |
| 37 |
*/ |
| 38 |
public static function check_dependencies() { |
| 39 |
parent::check_dependencies(); |
| 40 |
|
| 41 |
if ( ! bp_is_active( 'friends' ) ) { |
| 42 |
WP_CLI::error( 'The Friends component is not active.' ); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Create a new friendship. |
| 48 |
* |
| 49 |
* ## OPTIONS |
| 50 |
* |
| 51 |
* <initiator> |
| 52 |
* : ID of the user who is sending the friendship request. Accepts either a user_login or a numeric ID. |
| 53 |
* |
| 54 |
* <friend> |
| 55 |
* : ID of the user whose friendship is being requested. Accepts either a user_login or a numeric ID. |
| 56 |
* |
| 57 |
* [--force-accept] |
| 58 |
* : Whether to force acceptance. |
| 59 |
* |
| 60 |
* [--silent] |
| 61 |
* : Whether to silent the message creation. |
| 62 |
* |
| 63 |
* [--porcelain] |
| 64 |
* : Return only the friendship id. |
| 65 |
* |
| 66 |
* ## EXAMPLES |
| 67 |
* |
| 68 |
* # Create a new friendship. |
| 69 |
* $ wp bp friend create user1 another_use |
| 70 |
* Success: Friendship successfully created. |
| 71 |
* |
| 72 |
* # Create a new friendship, forcing acceptance. |
| 73 |
* $ wp bp friend create user1 another_use --force-accept |
| 74 |
* Success: Friendship successfully created. |
| 75 |
* |
| 76 |
* @alias add |
| 77 |
*/ |
| 78 |
public function create( $args, $assoc_args ) { |
| 79 |
$initiator = $this->get_user_id_from_identifier( $args[0] ); |
| 80 |
$friend = $this->get_user_id_from_identifier( $args[1] ); |
| 81 |
|
| 82 |
// Silent it before it errors. |
| 83 |
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'silent' ) ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
// Check if users are already friends, and bail if they do. |
| 88 |
if ( friends_check_friendship( $initiator->ID, $friend->ID ) ) { |
| 89 |
WP_CLI::error( 'These users are already friends.' ); |
| 90 |
} |
| 91 |
|
| 92 |
$force = (bool) WP_CLI\Utils\get_flag_value( $assoc_args, 'force-accept' ); |
| 93 |
|
| 94 |
if ( ! friends_add_friend( $initiator->ID, $friend->ID, $force ) ) { |
| 95 |
WP_CLI::error( 'There was a problem while creating the friendship.' ); |
| 96 |
} |
| 97 |
|
| 98 |
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) { |
| 99 |
WP_CLI::log( \BP_Friends_Friendship::get_friendship_id( $initiator->ID, $friend->ID ) ); |
| 100 |
} elseif ( $force ) { |
| 101 |
WP_CLI::success( 'Friendship successfully created.' ); |
| 102 |
} else { |
| 103 |
WP_CLI::success( 'Friendship successfully created but not accepted.' ); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Remove a friendship. |
| 109 |
* |
| 110 |
* ## OPTIONS |
| 111 |
* |
| 112 |
* <initiator> |
| 113 |
* : ID of the friendship initiator. Accepts either a user_login or a numeric ID. |
| 114 |
* |
| 115 |
* <friend> |
| 116 |
* : ID of the friend user. Accepts either a user_login or a numeric ID. |
| 117 |
* |
| 118 |
* ## EXAMPLE |
| 119 |
* |
| 120 |
* # Remove a friendship. |
| 121 |
* $ wp bp friend remove user_1 user_2 |
| 122 |
* Success: Friendship successfully removed. |
| 123 |
* |
| 124 |
* @alias remove |
| 125 |
* @alias trash |
| 126 |
*/ |
| 127 |
public function delete( $args ) { |
| 128 |
$initiator = $this->get_user_id_from_identifier( $args[0] ); |
| 129 |
$friend = $this->get_user_id_from_identifier( $args[1] ); |
| 130 |
|
| 131 |
// Check if users are already friends, if not, bail. |
| 132 |
if ( ! friends_check_friendship( $initiator->ID, $friend->ID ) ) { |
| 133 |
WP_CLI::error( 'These users are not friends.' ); |
| 134 |
} |
| 135 |
|
| 136 |
if ( friends_remove_friend( $initiator->ID, $friend->ID ) ) { |
| 137 |
WP_CLI::success( 'Friendship successfully removed.' ); |
| 138 |
} else { |
| 139 |
WP_CLI::error( 'There was a problem while removing the friendship.' ); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Mark a friendship request as accepted. |
| 145 |
* |
| 146 |
* ## OPTIONS |
| 147 |
* |
| 148 |
* <friendship>... |
| 149 |
* : ID(s) of the friendship(s). |
| 150 |
* |
| 151 |
* ## EXAMPLES |
| 152 |
* |
| 153 |
* $ wp bp friend accept-invitation 2161 |
| 154 |
* Success: Friendship successfully accepted. |
| 155 |
* |
| 156 |
* $ wp bp friend accept 2161 |
| 157 |
* Success: Friendship successfully accepted. |
| 158 |
* |
| 159 |
* @alias accept-invitation |
| 160 |
*/ |
| 161 |
public function accept( $args, $assoc_args ) { |
| 162 |
parent::_update( |
| 163 |
wp_parse_id_list( $args ), |
| 164 |
$assoc_args, |
| 165 |
function ( $friendship_id ) { |
| 166 |
if ( friends_accept_friendship( $friendship_id ) ) { |
| 167 |
return [ 'success', 'Friendship successfully accepted.' ]; |
| 168 |
} |
| 169 |
|
| 170 |
return [ 'error', 'There was a problem accepting the friendship.' ]; |
| 171 |
} |
| 172 |
); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Mark a friendship request as rejected. |
| 177 |
* |
| 178 |
* ## OPTIONS |
| 179 |
* |
| 180 |
* <friendship>... |
| 181 |
* : ID(s) of the friendship(s). |
| 182 |
* |
| 183 |
* ## EXAMPLES |
| 184 |
* |
| 185 |
* $ wp bp friend reject-invitation 2161 |
| 186 |
* Success: Friendship successfully accepted. |
| 187 |
* |
| 188 |
* $ wp bp friend reject 2161 151 2121 |
| 189 |
* Success: Friendship successfully accepted. |
| 190 |
* |
| 191 |
* @alias reject-invitation |
| 192 |
*/ |
| 193 |
public function reject( $args, $assoc_args ) { |
| 194 |
parent::_update( |
| 195 |
wp_parse_id_list( $args ), |
| 196 |
$assoc_args, |
| 197 |
function ( $friendship_id ) { |
| 198 |
if ( friends_reject_friendship( $friendship_id ) ) { |
| 199 |
return [ 'success', 'Friendship successfully rejected.' ]; |
| 200 |
} |
| 201 |
|
| 202 |
return [ 'error', 'There was a problem rejecting the friendship.' ]; |
| 203 |
} |
| 204 |
); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Check whether two users are friends. |
| 209 |
* |
| 210 |
* ## OPTIONS |
| 211 |
* |
| 212 |
* <user> |
| 213 |
* : ID of the first user. Accepts either a user_login or a numeric ID. |
| 214 |
* |
| 215 |
* <friend> |
| 216 |
* : ID of the other user. Accepts either a user_login or a numeric ID. |
| 217 |
* |
| 218 |
* ## EXAMPLES |
| 219 |
* |
| 220 |
* $ wp bp friend check 2161 65465 |
| 221 |
* Success: Yes, they are friends. |
| 222 |
* |
| 223 |
* $ wp bp friend see 2121 65456 |
| 224 |
* Success: Yes, they are friends. |
| 225 |
* |
| 226 |
* @alias see |
| 227 |
*/ |
| 228 |
public function check( $args ) { |
| 229 |
$user = $this->get_user_id_from_identifier( $args[0] ); |
| 230 |
$friend = $this->get_user_id_from_identifier( $args[1] ); |
| 231 |
|
| 232 |
if ( friends_check_friendship( $user->ID, $friend->ID ) ) { |
| 233 |
WP_CLI::success( 'Yes, they are friends.' ); |
| 234 |
} else { |
| 235 |
WP_CLI::error( 'No, they are not friends.' ); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Get a list of user's friends. |
| 241 |
* |
| 242 |
* ## OPTIONS |
| 243 |
* |
| 244 |
* <user> |
| 245 |
* : ID of the user. Accepts either a user_login or a numeric ID. |
| 246 |
* |
| 247 |
* [--fields=<fields>] |
| 248 |
* : Fields to display. |
| 249 |
* |
| 250 |
* [--count=<number>] |
| 251 |
* : How many user's friends to list. |
| 252 |
* --- |
| 253 |
* default: 50 |
| 254 |
* --- |
| 255 |
* |
| 256 |
* [--format=<format>] |
| 257 |
* : Render output in a particular format. |
| 258 |
* --- |
| 259 |
* default: table |
| 260 |
* options: |
| 261 |
* - table |
| 262 |
* - ids |
| 263 |
* - count |
| 264 |
* - csv |
| 265 |
* - json |
| 266 |
* - yaml |
| 267 |
* --- |
| 268 |
* |
| 269 |
* ## EXAMPLES |
| 270 |
* |
| 271 |
* # List a user's friends and get the count. |
| 272 |
* $ wp bp friend list 65465 --format=count |
| 273 |
* 100 |
| 274 |
* |
| 275 |
* # List a user's friends and get the IDs. |
| 276 |
* $ wp bp friend list 2422 --format=ids |
| 277 |
* 70 71 72 73 74 |
| 278 |
* |
| 279 |
* @subcommand list |
| 280 |
*/ |
| 281 |
public function list_( $args, $assoc_args ) { |
| 282 |
$formatter = $this->get_formatter( $assoc_args ); |
| 283 |
$user = $this->get_user_id_from_identifier( $args[0] ); |
| 284 |
$friends = \BP_Friends_Friendship::get_friendships( |
| 285 |
$user->ID, |
| 286 |
[ |
| 287 |
'page' => 1, |
| 288 |
'per_page' => $assoc_args['count'], |
| 289 |
] |
| 290 |
); |
| 291 |
|
| 292 |
if ( empty( $friends ) ) { |
| 293 |
WP_CLI::error( 'This member has no friends.' ); |
| 294 |
} |
| 295 |
|
| 296 |
$formatter->display_items( 'ids' === $formatter->format ? wp_list_pluck( $friends, 'friend_user_id' ) : $friends ); |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Generate random friendships. |
| 301 |
* |
| 302 |
* ## OPTIONS |
| 303 |
* |
| 304 |
* [--initiator=<user>] |
| 305 |
* : ID of the first user. Accepts either a user_login or a numeric ID. |
| 306 |
* |
| 307 |
* [--friend=<user>] |
| 308 |
* : ID of the second user. Accepts either a user_login or a numeric ID. |
| 309 |
* |
| 310 |
* [--count=<number>] |
| 311 |
* : How many friendships to generate. |
| 312 |
* --- |
| 313 |
* default: 100 |
| 314 |
* --- |
| 315 |
* |
| 316 |
* [--format=<format>] |
| 317 |
* : Render output in a particular format. |
| 318 |
* --- |
| 319 |
* default: progress |
| 320 |
* options: |
| 321 |
* - progress |
| 322 |
* - ids |
| 323 |
* --- |
| 324 |
* |
| 325 |
* ## EXAMPLES |
| 326 |
* |
| 327 |
* # Generate 50 random friendships. |
| 328 |
* $ wp bp friend generate --count=50 |
| 329 |
* Generating friendships 100% [======================] 0:00 / 0:00 |
| 330 |
* |
| 331 |
* # Generate 50 friendships with a specific user. |
| 332 |
* $ wp bp friend generate --initiator=121 --count=50 |
| 333 |
* Generating friendships 100% [======================] 0:00 / 0:00 |
| 334 |
* |
| 335 |
* # Generate 5 random friendships and output only the IDs. |
| 336 |
* $ wp bp friend generate --count=5 --format=ids |
| 337 |
* 70 71 72 73 74 |
| 338 |
*/ |
| 339 |
public function generate( $args, $assoc_args ) { |
| 340 |
$member_id = null; |
| 341 |
$friend_id = null; |
| 342 |
|
| 343 |
if ( isset( $assoc_args['initiator'] ) ) { |
| 344 |
$user = $this->get_user_id_from_identifier( $assoc_args['initiator'] ); |
| 345 |
$member_id = $user->ID; |
| 346 |
} |
| 347 |
|
| 348 |
if ( isset( $assoc_args['friend'] ) ) { |
| 349 |
$user_2 = $this->get_user_id_from_identifier( $assoc_args['friend'] ); |
| 350 |
$friend_id = $user_2->ID; |
| 351 |
} |
| 352 |
|
| 353 |
$this->generate_callback( |
| 354 |
'Generating friendships', |
| 355 |
$assoc_args, |
| 356 |
function ( $assoc_args, $format ) use ( $member_id, $friend_id ) { |
| 357 |
if ( ! $member_id ) { |
| 358 |
$member_id = $this->get_random_user_id(); |
| 359 |
} |
| 360 |
|
| 361 |
if ( ! $friend_id ) { |
| 362 |
$friend_id = $this->get_random_user_id(); |
| 363 |
} |
| 364 |
|
| 365 |
$params = [ 'force-accept' => true ]; |
| 366 |
|
| 367 |
if ( 'ids' === $format ) { |
| 368 |
$params['porcelain'] = true; |
| 369 |
} else { |
| 370 |
$params['silent'] = true; |
| 371 |
} |
| 372 |
|
| 373 |
return $this->create( [ $member_id, $friend_id ], $params ); |
| 374 |
} |
| 375 |
); |
| 376 |
} |
| 377 |
} |
| 378 |
|