| 1 |
<?php |
| 2 |
|
| 3 |
namespace Buddypress\CLI\Command; |
| 4 |
|
| 5 |
use WP_CLI; |
| 6 |
|
| 7 |
/** |
| 8 |
* Manage BuddyPress Signups. |
| 9 |
* |
| 10 |
* ## EXAMPLES |
| 11 |
* |
| 12 |
* # Add a signup. |
| 13 |
* $ wp bp signup create --user-login=test_user --user-email=teste@site.com |
| 14 |
* Success: Successfully added new user signup (ID #345). |
| 15 |
* |
| 16 |
* # Activate a signup. |
| 17 |
* $ wp bp signup activate ee48ec319fef3nn4 |
| 18 |
* Success: Signup activated, new user (ID #545). |
| 19 |
* |
| 20 |
* @since 1.5.0 |
| 21 |
*/ |
| 22 |
class Signup extends BuddyPressCommand { |
| 23 |
|
| 24 |
/** |
| 25 |
* Signup object fields. |
| 26 |
* |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
protected $obj_fields = [ |
| 30 |
'id', |
| 31 |
'user_name', |
| 32 |
'user_login', |
| 33 |
'user_email', |
| 34 |
'registered', |
| 35 |
'meta', |
| 36 |
'activation_key', |
| 37 |
'count_sent', |
| 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_get_signup_allowed() ) { |
| 47 |
WP_CLI::error( 'The BuddyPress signup feature needs to be allowed.' ); |
| 48 |
} |
| 49 |
|
| 50 |
// Fixes a bug in case the signups tables were not properly created. |
| 51 |
require_once buddypress()->plugin_dir . 'bp-core/admin/bp-core-admin-schema.php'; |
| 52 |
require_once buddypress()->plugin_dir . 'bp-core/bp-core-update.php'; |
| 53 |
|
| 54 |
bp_core_maybe_install_signups(); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Add a signup. |
| 59 |
* |
| 60 |
* ## OPTIONS |
| 61 |
* |
| 62 |
* [--user-login=<user-login>] |
| 63 |
* : User login for the signup. |
| 64 |
* |
| 65 |
* [--user-email=<user-email>] |
| 66 |
* : User email for the signup. |
| 67 |
* |
| 68 |
* [--activation-key=<activation-key>] |
| 69 |
* : Activation key for the signup. If none is provided, a random one will be used. |
| 70 |
* |
| 71 |
* [--silent] |
| 72 |
* : Whether to silent the signup creation. |
| 73 |
* |
| 74 |
* [--porcelain] |
| 75 |
* : Output only the new signup id. |
| 76 |
* |
| 77 |
* ## EXAMPLES |
| 78 |
* |
| 79 |
* # Add a signup. |
| 80 |
* $ wp bp signup create --user-login=test_user --user-email=teste@site.com |
| 81 |
* Success: Successfully added new user signup (ID #345). |
| 82 |
* |
| 83 |
* @alias add |
| 84 |
*/ |
| 85 |
public function create( $args, $assoc_args ) { |
| 86 |
$r = wp_parse_args( |
| 87 |
$assoc_args, |
| 88 |
[ |
| 89 |
'user-login' => '', |
| 90 |
'user-email' => '', |
| 91 |
'activation-key' => wp_generate_password( 32, false ), |
| 92 |
] |
| 93 |
); |
| 94 |
|
| 95 |
$signup_args = [ 'meta' => [] ]; |
| 96 |
|
| 97 |
$user_login = $r['user-login']; |
| 98 |
if ( ! empty( $user_login ) ) { |
| 99 |
$user_login = preg_replace( '/\s+/', '', sanitize_user( $user_login, true ) ); |
| 100 |
} |
| 101 |
|
| 102 |
$user_email = $r['user-email']; |
| 103 |
if ( ! empty( $user_email ) ) { |
| 104 |
$user_email = sanitize_email( $user_email ); |
| 105 |
} |
| 106 |
|
| 107 |
$signup_args['user_login'] = $user_login; |
| 108 |
$signup_args['user_email'] = $user_email; |
| 109 |
$signup_args['activation_key'] = $r['activation-key']; |
| 110 |
|
| 111 |
$signup_id = \BP_Signup::add( $signup_args ); |
| 112 |
|
| 113 |
// Silent it. |
| 114 |
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'silent' ) ) { |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
if ( ! $signup_id ) { |
| 119 |
WP_CLI::error( 'Could not add user signup.' ); |
| 120 |
} |
| 121 |
|
| 122 |
if ( WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) { |
| 123 |
WP_CLI::log( $signup_id ); |
| 124 |
} else { |
| 125 |
WP_CLI::success( sprintf( 'Successfully added new user signup (ID #%d).', $signup_id ) ); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Get a signup. |
| 131 |
* |
| 132 |
* ## OPTIONS |
| 133 |
* |
| 134 |
* <signup-id> |
| 135 |
* : Identifier for the signup. Can be a signup ID, an email address, or a user_login. |
| 136 |
* |
| 137 |
* [--match-field=<match-field>] |
| 138 |
* : Field to match the signup-id to. Use if there is ambiguity between, eg, signup ID and user_login. |
| 139 |
* --- |
| 140 |
* options: |
| 141 |
* - signup_id |
| 142 |
* - user_email |
| 143 |
* - user_login |
| 144 |
* --- |
| 145 |
* |
| 146 |
* [--fields=<fields>] |
| 147 |
* : Limit the output to specific signup fields. |
| 148 |
* |
| 149 |
* [--format=<format>] |
| 150 |
* : Render output in a particular format. |
| 151 |
* --- |
| 152 |
* default: table |
| 153 |
* options: |
| 154 |
* - table |
| 155 |
* - json |
| 156 |
* - csv |
| 157 |
* - yaml |
| 158 |
* --- |
| 159 |
* |
| 160 |
* ## EXAMPLES |
| 161 |
* |
| 162 |
* # Get a signup. |
| 163 |
* $ wp bp signup get 35 --fields=id,user_login,user_name,count_sent |
| 164 |
* +------------+------------+ |
| 165 |
* | Field | Value | |
| 166 |
* +------------+------------+ |
| 167 |
* | id | 35 | |
| 168 |
* | user_login | user897616 | |
| 169 |
* | user_name | Test user | |
| 170 |
* | count_sent | 4 | |
| 171 |
* +------------+------------+ |
| 172 |
*/ |
| 173 |
public function get( $args, $assoc_args ) { |
| 174 |
$signup = $this->get_signup_by_identifier( $args[0], $assoc_args ); |
| 175 |
|
| 176 |
$this->get_formatter( $assoc_args )->display_item( $signup ); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Delete a signup. |
| 181 |
* |
| 182 |
* ## OPTIONS |
| 183 |
* |
| 184 |
* <signup-id>... |
| 185 |
* : ID or IDs of signup to delete. |
| 186 |
* |
| 187 |
* [--yes] |
| 188 |
* : Answer yes to the confirmation message. |
| 189 |
* |
| 190 |
* ## EXAMPLES |
| 191 |
* |
| 192 |
* # Delete a signup. |
| 193 |
* $ wp bp signup delete 520 --yes |
| 194 |
* Success: Signup deleted 54565. |
| 195 |
* |
| 196 |
* # Delete multiple signups. |
| 197 |
* $ wp bp signup delete 55654 54565 --yes |
| 198 |
* Success: Signup deleted 55654. |
| 199 |
* Success: Signup deleted 54565. |
| 200 |
* |
| 201 |
* @alias remove |
| 202 |
* @alias trash |
| 203 |
*/ |
| 204 |
public function delete( $args, $assoc_args ) { |
| 205 |
$signup_ids = wp_parse_id_list( $args ); |
| 206 |
|
| 207 |
if ( count( $signup_ids ) > 1 ) { |
| 208 |
WP_CLI::confirm( 'Are you sure you want to delete these signups?', $assoc_args ); |
| 209 |
} else { |
| 210 |
WP_CLI::confirm( 'Are you sure you want to delete this signup?', $assoc_args ); |
| 211 |
} |
| 212 |
|
| 213 |
parent::_delete( |
| 214 |
$signup_ids, |
| 215 |
$assoc_args, |
| 216 |
function ( $signup_id ) { |
| 217 |
if ( \BP_Signup::delete( [ $signup_id ] ) ) { |
| 218 |
return [ 'success', sprintf( 'Signup deleted %d.', $signup_id ) ]; |
| 219 |
} |
| 220 |
|
| 221 |
return [ 'error', sprintf( 'Could not delete signup %d.', $signup_id ) ]; |
| 222 |
} |
| 223 |
); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Activate a signup. |
| 228 |
* |
| 229 |
* ## OPTIONS |
| 230 |
* |
| 231 |
* <signup-id> |
| 232 |
* : Identifier for the signup. Can be a signup ID, an email address, or a user_login. |
| 233 |
* |
| 234 |
* ## EXAMPLES |
| 235 |
* |
| 236 |
* # Activate a signup. |
| 237 |
* $ wp bp signup activate ee48ec319fef3nn4 |
| 238 |
* Success: Signup activated, new user (ID #545). |
| 239 |
*/ |
| 240 |
public function activate( $args, $assoc_args ) { |
| 241 |
$signup = $this->get_signup_by_identifier( $args[0], $assoc_args ); |
| 242 |
$user_id = bp_core_activate_signup( $signup->activation_key ); |
| 243 |
|
| 244 |
if ( $user_id ) { |
| 245 |
WP_CLI::success( sprintf( 'Signup activated, new user (ID #%d).', $user_id ) ); |
| 246 |
} else { |
| 247 |
WP_CLI::error( 'Signup not activated.' ); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Generate random signups. |
| 253 |
* |
| 254 |
* ## OPTIONS |
| 255 |
* |
| 256 |
* [--count=<number>] |
| 257 |
* : How many signups to generate. |
| 258 |
* --- |
| 259 |
* default: 100 |
| 260 |
* --- |
| 261 |
* |
| 262 |
* [--format=<format>] |
| 263 |
* : Render output in a particular format. |
| 264 |
* --- |
| 265 |
* default: progress |
| 266 |
* options: |
| 267 |
* - progress |
| 268 |
* - ids |
| 269 |
* --- |
| 270 |
* |
| 271 |
* ## EXAMPLES |
| 272 |
* |
| 273 |
* # Generate 50 random signups. |
| 274 |
* $ wp bp signup generate --count=50 |
| 275 |
* Generating signups 100% [======================] 0:00 / 0:00 |
| 276 |
* |
| 277 |
* # Generate 5 random signups and return their IDs. |
| 278 |
* $ wp bp signup generate --count=5 --format=ids |
| 279 |
* 70 71 72 73 74 |
| 280 |
*/ |
| 281 |
public function generate( $args, $assoc_args ) { |
| 282 |
// Use the email API to get a valid "from" domain. |
| 283 |
$email_domain = new \BP_Email( '' ); |
| 284 |
$email_domain = $email_domain->get_from()->get_address(); |
| 285 |
$random_login = wp_generate_password( 12, false ); // Generate random user login. |
| 286 |
|
| 287 |
$this->generate_callback( |
| 288 |
'Generating signups', |
| 289 |
$assoc_args, |
| 290 |
function ( $assoc_args, $format ) use ( $random_login, $email_domain ) { |
| 291 |
$params = [ |
| 292 |
'user-login' => $random_login, |
| 293 |
'user-email' => $random_login . substr( $email_domain, strpos( $email_domain, '@' ) ), |
| 294 |
]; |
| 295 |
|
| 296 |
if ( 'ids' === $format ) { |
| 297 |
$params['porcelain'] = true; |
| 298 |
} else { |
| 299 |
$params['silent'] = true; |
| 300 |
} |
| 301 |
|
| 302 |
return $this->create( [], $params ); |
| 303 |
} |
| 304 |
); |
| 305 |
} |
| 306 |
|
| 307 |
/** |
| 308 |
* Resend activation e-mail to a newly registered user. |
| 309 |
* |
| 310 |
* ## OPTIONS |
| 311 |
* |
| 312 |
* <signup-id> |
| 313 |
* : Identifier for the signup. Can be a signup ID, an email address, or a user_login. |
| 314 |
* |
| 315 |
* ## EXAMPLES |
| 316 |
* |
| 317 |
* # Resend activation e-mail to a newly registered user. |
| 318 |
* $ wp bp signup resend test@example.com |
| 319 |
* Success: Email sent successfully. |
| 320 |
* |
| 321 |
* @alias send |
| 322 |
*/ |
| 323 |
public function resend( $args, $assoc_args ) { |
| 324 |
$signup = $this->get_signup_by_identifier( $args[0], $assoc_args ); |
| 325 |
$send = \BP_Signup::resend( [ $signup->signup_id ] ); |
| 326 |
|
| 327 |
// Add feedback message. |
| 328 |
if ( empty( $send['errors'] ) ) { |
| 329 |
WP_CLI::success( 'Email sent successfully.' ); |
| 330 |
} else { |
| 331 |
WP_CLI::error( 'This account is already activated.' ); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Get a list of signups. |
| 337 |
* |
| 338 |
* ## OPTIONS |
| 339 |
* |
| 340 |
* [--<field>=<value>] |
| 341 |
* : One or more parameters to pass. See \BP_Signup::get() |
| 342 |
* |
| 343 |
* [--fields=<fields>] |
| 344 |
* : Fields to display. |
| 345 |
* |
| 346 |
* [--count=<number>] |
| 347 |
* : How many signups to list. |
| 348 |
* --- |
| 349 |
* default: 50 |
| 350 |
* --- |
| 351 |
* |
| 352 |
* [--format=<value>] |
| 353 |
* : Render output in a particular format. |
| 354 |
* --- |
| 355 |
* default: table |
| 356 |
* options: |
| 357 |
* - table |
| 358 |
* - csv |
| 359 |
* - ids |
| 360 |
* - json |
| 361 |
* - count |
| 362 |
* - yaml |
| 363 |
* --- |
| 364 |
* |
| 365 |
* ## EXAMPLES |
| 366 |
* |
| 367 |
* # List signups and get the IDs. |
| 368 |
* $ wp bp signup list --format=ids |
| 369 |
* 70 71 72 73 74 |
| 370 |
* |
| 371 |
* # List 100 signups and return the count. |
| 372 |
* $ wp bp signup list --count=100 --format=count |
| 373 |
* 100 |
| 374 |
* |
| 375 |
* # List active signups. |
| 376 |
* $ wp bp signup list --active=1 --count=10 |
| 377 |
* 50 |
| 378 |
* |
| 379 |
* @subcommand list |
| 380 |
*/ |
| 381 |
public function list_( $args, $assoc_args ) { |
| 382 |
$formatter = $this->get_formatter( $assoc_args ); |
| 383 |
|
| 384 |
$assoc_args['number'] = $assoc_args['count']; |
| 385 |
|
| 386 |
if ( in_array( $formatter->format, [ 'ids', 'count' ], true ) ) { |
| 387 |
$assoc_args['fields'] = 'ids'; |
| 388 |
} |
| 389 |
|
| 390 |
$signups = \BP_Signup::get( $assoc_args ); |
| 391 |
|
| 392 |
if ( empty( $signups['signups'] ) ) { |
| 393 |
WP_CLI::error( 'No signups found.' ); |
| 394 |
} |
| 395 |
|
| 396 |
$formatter->display_items( $signups['signups'] ); |
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
* Look up a signup by the provided identifier. |
| 401 |
* |
| 402 |
* @since 1.5.0 |
| 403 |
* |
| 404 |
* @return mixed |
| 405 |
*/ |
| 406 |
protected function get_signup_by_identifier( $identifier, $assoc_args ) { |
| 407 |
if ( isset( $assoc_args['match-field'] ) ) { |
| 408 |
switch ( $assoc_args['match-field'] ) { |
| 409 |
case 'signup_id': |
| 410 |
$signup_args['include'] = [ $identifier ]; |
| 411 |
break; |
| 412 |
|
| 413 |
case 'user_login': |
| 414 |
$signup_args['user_login'] = $identifier; |
| 415 |
break; |
| 416 |
|
| 417 |
case 'user_email': |
| 418 |
default: |
| 419 |
$signup_args['usersearch'] = $identifier; |
| 420 |
break; |
| 421 |
} |
| 422 |
} elseif ( is_numeric( $identifier ) ) { |
| 423 |
$signup_args['include'] = [ intval( $identifier ) ]; |
| 424 |
} elseif ( is_email( $identifier ) ) { |
| 425 |
$signup_args['usersearch'] = $identifier; |
| 426 |
} else { |
| 427 |
$signup_args['user_login'] = $identifier; |
| 428 |
} |
| 429 |
|
| 430 |
$signups = \BP_Signup::get( $signup_args ); |
| 431 |
$signup = null; |
| 432 |
|
| 433 |
if ( ! empty( $signups['signups'] ) ) { |
| 434 |
$signup = reset( $signups['signups'] ); |
| 435 |
} |
| 436 |
|
| 437 |
if ( ! $signup ) { |
| 438 |
WP_CLI::error( 'No signup found by that identifier.' ); |
| 439 |
} |
| 440 |
|
| 441 |
return $signup; |
| 442 |
} |
| 443 |
} |
| 444 |
|