| 1 |
<?php |
| 2 |
/** |
| 3 |
* User controller. |
| 4 |
* |
| 5 |
* @package HivePress\Controllers |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace HivePress\Controllers; |
| 9 |
|
| 10 |
use HivePress\Helpers as hp; |
| 11 |
use HivePress\Models; |
| 12 |
use HivePress\Forms; |
| 13 |
use HivePress\Menus; |
| 14 |
use HivePress\Blocks; |
| 15 |
use HivePress\Emails; |
| 16 |
|
| 17 |
// Exit if accessed directly. |
| 18 |
defined( 'ABSPATH' ) || exit; |
| 19 |
|
| 20 |
/** |
| 21 |
* Manages users. |
| 22 |
*/ |
| 23 |
final class User extends Controller { |
| 24 |
|
| 25 |
/** |
| 26 |
* Class constructor. |
| 27 |
* |
| 28 |
* @param array $args Controller arguments. |
| 29 |
*/ |
| 30 |
public function __construct( $args = [] ) { |
| 31 |
$args = hp\merge_arrays( |
| 32 |
[ |
| 33 |
'routes' => [ |
| 34 |
'users_resource' => [ |
| 35 |
'path' => '/users', |
| 36 |
'method' => 'GET', |
| 37 |
'action' => [ $this, 'get_users' ], |
| 38 |
'rest' => true, |
| 39 |
], |
| 40 |
|
| 41 |
/** |
| 42 |
* @OA\Parameter( |
| 43 |
* name="user_id", |
| 44 |
* description="User ID.", |
| 45 |
* in="path", |
| 46 |
* required=true, |
| 47 |
* @OA\Schema(type="integer"), |
| 48 |
* ), |
| 49 |
*/ |
| 50 |
'user_resource' => [ |
| 51 |
'base' => 'users_resource', |
| 52 |
'path' => '/(?P<user_id>\d+)', |
| 53 |
'rest' => true, |
| 54 |
], |
| 55 |
|
| 56 |
/** |
| 57 |
* @OA\Post( |
| 58 |
* path="/users", |
| 59 |
* summary="Register a user", |
| 60 |
* tags={"Users"}, |
| 61 |
* @OA\RequestBody( |
| 62 |
* @OA\JsonContent( |
| 63 |
* @OA\Property(property="username", type="string", description="Username."), |
| 64 |
* @OA\Property(property="email", type="string", description="Email address."), |
| 65 |
* @OA\Property(property="password", type="string", description="Password.") |
| 66 |
* ), |
| 67 |
* ), |
| 68 |
* @OA\Response(response="201", description="") |
| 69 |
* ) |
| 70 |
*/ |
| 71 |
'user_register_action' => [ |
| 72 |
'base' => 'users_resource', |
| 73 |
'method' => 'POST', |
| 74 |
'action' => [ $this, 'register_user' ], |
| 75 |
'rest' => true, |
| 76 |
], |
| 77 |
|
| 78 |
/** |
| 79 |
* @OA\Post( |
| 80 |
* path="/users/login", |
| 81 |
* summary="Login a user", |
| 82 |
* tags={"Users"}, |
| 83 |
* @OA\RequestBody( |
| 84 |
* @OA\JsonContent( |
| 85 |
* @OA\Property(property="username_or_email", type="string", description="Username or email address."), |
| 86 |
* @OA\Property(property="password", type="string", description="Password.") |
| 87 |
* ), |
| 88 |
* ), |
| 89 |
* @OA\Response(response="200", description="") |
| 90 |
* ) |
| 91 |
*/ |
| 92 |
'user_login_action' => [ |
| 93 |
'base' => 'users_resource', |
| 94 |
'path' => '/login', |
| 95 |
'method' => 'POST', |
| 96 |
'action' => [ $this, 'login_user' ], |
| 97 |
'rest' => true, |
| 98 |
], |
| 99 |
|
| 100 |
/** |
| 101 |
* @OA\Post( |
| 102 |
* path="/users/request-password", |
| 103 |
* summary="Request a password reset", |
| 104 |
* tags={"Users"}, |
| 105 |
* @OA\RequestBody( |
| 106 |
* @OA\JsonContent( |
| 107 |
* @OA\Property(property="username_or_email", type="string", description="Username or email address.") |
| 108 |
* ), |
| 109 |
* ), |
| 110 |
* @OA\Response(response="200", description="") |
| 111 |
* ) |
| 112 |
*/ |
| 113 |
'user_password_request_action' => [ |
| 114 |
'base' => 'users_resource', |
| 115 |
'path' => '/request-password', |
| 116 |
'method' => 'POST', |
| 117 |
'action' => [ $this, 'request_user_password' ], |
| 118 |
'rest' => true, |
| 119 |
], |
| 120 |
|
| 121 |
/** |
| 122 |
* @OA\Post( |
| 123 |
* path="/users/reset-password", |
| 124 |
* summary="Reset a password", |
| 125 |
* tags={"Users"}, |
| 126 |
* @OA\RequestBody( |
| 127 |
* @OA\JsonContent( |
| 128 |
* @OA\Property(property="username", type="string", description="Username."), |
| 129 |
* @OA\Property(property="password", type="string", description="New password."), |
| 130 |
* @OA\Property(property="password_reset_key", type="string", description="Password reset key.") |
| 131 |
* ), |
| 132 |
* ), |
| 133 |
* @OA\Response(response="200", description="") |
| 134 |
* ) |
| 135 |
*/ |
| 136 |
'user_password_reset_action' => [ |
| 137 |
'base' => 'users_resource', |
| 138 |
'path' => '/reset-password', |
| 139 |
'method' => 'POST', |
| 140 |
'action' => [ $this, 'reset_user_password' ], |
| 141 |
'rest' => true, |
| 142 |
], |
| 143 |
|
| 144 |
/** |
| 145 |
* @OA\Post( |
| 146 |
* path="/users/{user_id}", |
| 147 |
* summary="Update a user", |
| 148 |
* description="In addition to the default user fields, you can also update custom fields added via the vendor attributes or HivePress extensions.", |
| 149 |
* tags={"Users"}, |
| 150 |
* @OA\Parameter(ref="#/components/parameters/user_id"), |
| 151 |
* @OA\RequestBody(@OA\JsonContent(ref="#/components/schemas/User")), |
| 152 |
* @OA\Response(response="200", description="") |
| 153 |
* ) |
| 154 |
*/ |
| 155 |
'user_update_action' => [ |
| 156 |
'base' => 'user_resource', |
| 157 |
'method' => 'POST', |
| 158 |
'action' => [ $this, 'update_user' ], |
| 159 |
'rest' => true, |
| 160 |
], |
| 161 |
|
| 162 |
/** |
| 163 |
* @OA\Delete( |
| 164 |
* path="/users/{user_id}", |
| 165 |
* summary="Delete a user", |
| 166 |
* tags={"Users"}, |
| 167 |
* @OA\Parameter(ref="#/components/parameters/user_id"), |
| 168 |
* @OA\Response(response="204", description="") |
| 169 |
* ) |
| 170 |
*/ |
| 171 |
'user_delete_action' => [ |
| 172 |
'base' => 'user_resource', |
| 173 |
'method' => 'DELETE', |
| 174 |
'action' => [ $this, 'delete_user' ], |
| 175 |
'rest' => true, |
| 176 |
], |
| 177 |
|
| 178 |
'user_account_page' => [ |
| 179 |
'path' => '/account', |
| 180 |
'redirect' => [ $this, 'redirect_user_account_page' ], |
| 181 |
], |
| 182 |
|
| 183 |
'user_login_page' => [ |
| 184 |
'title' => esc_html__( 'Sign In', 'hivepress' ), |
| 185 |
'base' => 'user_account_page', |
| 186 |
'path' => '/login', |
| 187 |
'redirect' => [ $this, 'redirect_user_login_page' ], |
| 188 |
'action' => [ $this, 'render_user_login_page' ], |
| 189 |
], |
| 190 |
|
| 191 |
'user_logout_page' => [ |
| 192 |
'base' => 'user_account_page', |
| 193 |
'path' => '/logout', |
| 194 |
'redirect' => [ $this, 'redirect_user_logout_page' ], |
| 195 |
], |
| 196 |
|
| 197 |
'user_password_reset_page' => [ |
| 198 |
'title' => esc_html__( 'Reset Password', 'hivepress' ), |
| 199 |
'base' => 'user_account_page', |
| 200 |
'path' => '/reset-password', |
| 201 |
'redirect' => [ $this, 'redirect_user_password_reset_page' ], |
| 202 |
'action' => [ $this, 'render_user_password_reset_page' ], |
| 203 |
], |
| 204 |
|
| 205 |
'user_email_verify_page' => [ |
| 206 |
'title' => esc_html__( 'Email Verified', 'hivepress' ), |
| 207 |
'base' => 'user_account_page', |
| 208 |
'path' => '/verify-email', |
| 209 |
'redirect' => [ $this, 'redirect_user_email_verify_page' ], |
| 210 |
'action' => [ $this, 'render_user_email_verify_page' ], |
| 211 |
], |
| 212 |
|
| 213 |
'user_edit_settings_page' => [ |
| 214 |
'title' => hivepress()->translator->get_string( 'settings' ), |
| 215 |
'base' => 'user_account_page', |
| 216 |
'path' => '/settings', |
| 217 |
'redirect' => [ $this, 'redirect_user_edit_settings_page' ], |
| 218 |
'action' => [ $this, 'render_user_edit_settings_page' ], |
| 219 |
], |
| 220 |
|
| 221 |
'user_view_page' => [ |
| 222 |
'path' => '/user/(?P<username>[A-Za-z0-9 _.\-@]+)', |
| 223 |
'title' => [ $this, 'get_user_view_title' ], |
| 224 |
'action' => [ $this, 'render_user_view_page' ], |
| 225 |
|
| 226 |
'redirect' => [ |
| 227 |
[ |
| 228 |
'callback' => [ $this, 'redirect_user_view_page' ], |
| 229 |
'_order' => 5, |
| 230 |
], |
| 231 |
], |
| 232 |
], |
| 233 |
], |
| 234 |
], |
| 235 |
$args |
| 236 |
); |
| 237 |
|
| 238 |
parent::__construct( $args ); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* Gets users. |
| 243 |
* |
| 244 |
* @param WP_REST_Request $request API request. |
| 245 |
* @return WP_Rest_Response |
| 246 |
*/ |
| 247 |
public function get_users( $request ) { |
| 248 |
|
| 249 |
// Check authentication. |
| 250 |
if ( ! is_user_logged_in() ) { |
| 251 |
return hp\rest_error( 401 ); |
| 252 |
} |
| 253 |
|
| 254 |
// Check permissions. |
| 255 |
if ( ! current_user_can( 'edit_others_posts' ) ) { |
| 256 |
return hp\rest_error( 403 ); |
| 257 |
} |
| 258 |
|
| 259 |
// Get search query. |
| 260 |
$query = sanitize_text_field( $request->get_param( 'search' ) ); |
| 261 |
|
| 262 |
if ( strlen( $query ) < 3 ) { |
| 263 |
return hp\rest_error( 400 ); |
| 264 |
} |
| 265 |
|
| 266 |
// Get users. |
| 267 |
$users = Models\User::query()->search( $query ) |
| 268 |
->limit( 20 ) |
| 269 |
->get(); |
| 270 |
|
| 271 |
// Get results. |
| 272 |
$results = []; |
| 273 |
|
| 274 |
if ( $request->get_param( 'context' ) === 'list' ) { |
| 275 |
foreach ( $users as $user ) { |
| 276 |
$results[] = [ |
| 277 |
'id' => $user->get_id(), |
| 278 |
'text' => $user->get_username(), |
| 279 |
]; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
return hp\rest_response( 200, $results ); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Registers user. |
| 288 |
* |
| 289 |
* @param WP_REST_Request $request API request. |
| 290 |
* @return WP_Rest_Response |
| 291 |
*/ |
| 292 |
public function register_user( $request ) { |
| 293 |
|
| 294 |
// Check permissions. |
| 295 |
if ( ! get_option( 'hp_user_enable_registration', true ) ) { |
| 296 |
return hp\rest_error( 403 ); |
| 297 |
} |
| 298 |
|
| 299 |
if ( is_user_logged_in() && ! current_user_can( 'create_users' ) ) { |
| 300 |
return hp\rest_error( 403 ); |
| 301 |
} |
| 302 |
|
| 303 |
// Validate form. |
| 304 |
$form = ( new Forms\User_Register() )->set_values( $request->get_params() ); |
| 305 |
|
| 306 |
if ( ! $form->validate() ) { |
| 307 |
return hp\rest_error( 400, $form->get_errors() ); |
| 308 |
} |
| 309 |
|
| 310 |
// Check username. |
| 311 |
if ( $form->get_value( 'username' ) ) { |
| 312 |
if ( sanitize_user( $form->get_value( 'username' ), true ) !== $form->get_value( 'username' ) ) { |
| 313 |
return hp\rest_error( 400, esc_html__( 'Username contains invalid characters.', 'hivepress' ) ); |
| 314 |
} elseif ( username_exists( $form->get_value( 'username' ) ) ) { |
| 315 |
return hp\rest_error( 400, esc_html__( 'This username is already in use.', 'hivepress' ) ); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
// Check email. |
| 320 |
if ( email_exists( $form->get_value( 'email' ) ) ) { |
| 321 |
return hp\rest_error( 400, esc_html__( 'This email is already registered.', 'hivepress' ) ); |
| 322 |
} |
| 323 |
|
| 324 |
// Get username. |
| 325 |
$username = hp\get_first_array_value( explode( '@', $form->get_value( 'email' ) ) ); |
| 326 |
|
| 327 |
if ( $form->get_value( 'username' ) ) { |
| 328 |
$username = $form->get_value( 'username' ); |
| 329 |
} else { |
| 330 |
$username = sanitize_user( $username, true ); |
| 331 |
|
| 332 |
if ( empty( $username ) ) { |
| 333 |
$username = 'user'; |
| 334 |
} |
| 335 |
|
| 336 |
while ( username_exists( $username ) ) { |
| 337 |
$username .= wp_rand( 1, 9 ); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
// Register user. |
| 342 |
$user = new Models\User(); |
| 343 |
|
| 344 |
// @todo remove temporary fix when updated. |
| 345 |
$user->set_id( null ); |
| 346 |
|
| 347 |
$user->fill( |
| 348 |
array_merge( |
| 349 |
$form->get_values(), |
| 350 |
[ |
| 351 |
'username' => $username, |
| 352 |
] |
| 353 |
) |
| 354 |
); |
| 355 |
|
| 356 |
if ( ! $user->save() ) { |
| 357 |
return hp\rest_error( 400, $user->_get_errors() ); |
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Fires when a new user is registered. |
| 362 |
* |
| 363 |
* @hook hivepress/v1/models/user/register |
| 364 |
* @param {int} $user_id User ID. |
| 365 |
* @param {array} $values Form values. |
| 366 |
*/ |
| 367 |
do_action( 'hivepress/v1/models/user/register', $user->get_id(), $form->get_values() ); |
| 368 |
|
| 369 |
if ( get_option( 'hp_user_verify_email' ) ) { |
| 370 |
|
| 371 |
// Set email key. |
| 372 |
$email_key = md5( $user->get_email() . time() . wp_rand() ); |
| 373 |
|
| 374 |
update_user_meta( $user->get_id(), 'hp_email_verify_key', $email_key ); |
| 375 |
|
| 376 |
// Set email redirect. |
| 377 |
$email_redirect = wp_validate_redirect( (string) $form->get_value( '_redirect' ) ); |
| 378 |
|
| 379 |
if ( $email_redirect ) { |
| 380 |
update_user_meta( $user->get_id(), 'hp_email_verify_redirect', $email_redirect ); |
| 381 |
} |
| 382 |
|
| 383 |
// Send email. |
| 384 |
( new Emails\User_Email_Verify( |
| 385 |
[ |
| 386 |
'recipient' => $user->get_email(), |
| 387 |
|
| 388 |
'tokens' => [ |
| 389 |
'user' => $user, |
| 390 |
'user_name' => $user->get_username(), |
| 391 |
'email_verify_url' => hivepress()->router->get_url( |
| 392 |
'user_email_verify_page', |
| 393 |
[ |
| 394 |
'username' => $user->get_username(), |
| 395 |
'email_verify_key' => $email_key, |
| 396 |
] |
| 397 |
), |
| 398 |
], |
| 399 |
] |
| 400 |
) )->send(); |
| 401 |
} elseif ( ! is_user_logged_in() ) { |
| 402 |
|
| 403 |
// Authenticate user. |
| 404 |
do_action( 'hivepress/v1/models/user/login' ); |
| 405 |
|
| 406 |
wp_signon( |
| 407 |
[ |
| 408 |
'user_login' => $user->get_username(), |
| 409 |
'user_password' => $form->get_value( 'password' ), |
| 410 |
'remember' => true, |
| 411 |
] |
| 412 |
); |
| 413 |
} |
| 414 |
|
| 415 |
return hp\rest_response( |
| 416 |
201, |
| 417 |
[ |
| 418 |
'id' => $user->get_id(), |
| 419 |
] |
| 420 |
); |
| 421 |
} |
| 422 |
|
| 423 |
/** |
| 424 |
* Logins user. |
| 425 |
* |
| 426 |
* @param WP_REST_Request $request API request. |
| 427 |
* @return WP_Rest_Response |
| 428 |
*/ |
| 429 |
public function login_user( $request ) { |
| 430 |
|
| 431 |
// Check permissions. |
| 432 |
if ( is_user_logged_in() && ! current_user_can( 'edit_users' ) ) { |
| 433 |
return hp\rest_error( 403 ); |
| 434 |
} |
| 435 |
|
| 436 |
// Validate form. |
| 437 |
$form = ( new Forms\User_Login() )->set_values( $request->get_params() ); |
| 438 |
|
| 439 |
if ( ! $form->validate() ) { |
| 440 |
return hp\rest_error( 400, $form->get_errors() ); |
| 441 |
} |
| 442 |
|
| 443 |
// Get user. |
| 444 |
$user_object = null; |
| 445 |
|
| 446 |
if ( is_email( $form->get_value( 'username_or_email' ) ) ) { |
| 447 |
$user_object = get_user_by( 'email', $form->get_value( 'username_or_email' ) ); |
| 448 |
} else { |
| 449 |
$user_object = get_user_by( 'login', $form->get_value( 'username_or_email' ) ); |
| 450 |
} |
| 451 |
|
| 452 |
if ( empty( $user_object ) ) { |
| 453 |
return hp\rest_error( 401, esc_html__( 'Username or password is incorrect.', 'hivepress' ) ); |
| 454 |
} |
| 455 |
|
| 456 |
$user = Models\User::query()->get_by_id( $user_object ); |
| 457 |
|
| 458 |
// Check password. |
| 459 |
if ( ! wp_check_password( $form->get_value( 'password' ), $user->get_password(), $user->get_id() ) ) { |
| 460 |
return hp\rest_error( 401, esc_html__( 'Username or password is incorrect.', 'hivepress' ) ); |
| 461 |
} |
| 462 |
|
| 463 |
// Check email key. |
| 464 |
if ( get_option( 'hp_user_verify_email' ) && $user_object->hp_email_verify_key ) { |
| 465 |
return hp\rest_error( 401, esc_html__( 'Please check your email to activate your account.', 'hivepress' ) ); |
| 466 |
} |
| 467 |
|
| 468 |
// Authenticate user. |
| 469 |
if ( ! is_user_logged_in() ) { |
| 470 |
|
| 471 |
/** |
| 472 |
* Fires when a user is being authenticated. |
| 473 |
* |
| 474 |
* @hook hivepress/v1/models/user/login |
| 475 |
*/ |
| 476 |
do_action( 'hivepress/v1/models/user/login' ); |
| 477 |
|
| 478 |
wp_signon( |
| 479 |
[ |
| 480 |
'user_login' => $user->get_username(), |
| 481 |
'user_password' => $form->get_value( 'password' ), |
| 482 |
'remember' => true, |
| 483 |
] |
| 484 |
); |
| 485 |
} |
| 486 |
|
| 487 |
return hp\rest_response( |
| 488 |
200, |
| 489 |
[ |
| 490 |
'id' => $user->get_id(), |
| 491 |
] |
| 492 |
); |
| 493 |
} |
| 494 |
|
| 495 |
/** |
| 496 |
* Requests user password. |
| 497 |
* |
| 498 |
* @param WP_REST_Request $request API request. |
| 499 |
* @return WP_Rest_Response |
| 500 |
*/ |
| 501 |
public function request_user_password( $request ) { |
| 502 |
|
| 503 |
// Check permissions. |
| 504 |
if ( is_user_logged_in() && ! current_user_can( 'edit_users' ) ) { |
| 505 |
return hp\rest_error( 403 ); |
| 506 |
} |
| 507 |
|
| 508 |
// Validate form. |
| 509 |
$form = ( new Forms\User_Password_Request() )->set_values( $request->get_params() ); |
| 510 |
|
| 511 |
if ( ! $form->validate() ) { |
| 512 |
return hp\rest_error( 400, $form->get_errors() ); |
| 513 |
} |
| 514 |
|
| 515 |
// Get user. |
| 516 |
$user_object = null; |
| 517 |
|
| 518 |
if ( is_email( $form->get_value( 'username_or_email' ) ) ) { |
| 519 |
$user_object = get_user_by( 'email', $form->get_value( 'username_or_email' ) ); |
| 520 |
} else { |
| 521 |
$user_object = get_user_by( 'login', $form->get_value( 'username_or_email' ) ); |
| 522 |
} |
| 523 |
|
| 524 |
if ( empty( $user_object ) ) { |
| 525 |
if ( is_email( $form->get_value( 'username_or_email' ) ) ) { |
| 526 |
return hp\rest_error( 404, esc_html__( 'User with this email doesn\'t exist.', 'hivepress' ) ); |
| 527 |
} else { |
| 528 |
return hp\rest_error( 404, esc_html__( 'User with this username doesn\'t exist.', 'hivepress' ) ); |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
$user = Models\User::query()->get_by_id( $user_object ); |
| 533 |
|
| 534 |
// Check email key. |
| 535 |
if ( get_option( 'hp_user_verify_email' ) && $user_object->hp_email_verify_key ) { |
| 536 |
return hp\rest_error( 401, esc_html__( 'Please check your email to activate your account.', 'hivepress' ) ); |
| 537 |
} |
| 538 |
|
| 539 |
// Send email. |
| 540 |
( new Emails\User_Password_Request( |
| 541 |
[ |
| 542 |
'recipient' => $user->get_email(), |
| 543 |
|
| 544 |
'tokens' => [ |
| 545 |
'user' => $user, |
| 546 |
'user_name' => $user->get_display_name(), |
| 547 |
'password_reset_url' => hivepress()->router->get_url( |
| 548 |
'user_password_reset_page', |
| 549 |
[ |
| 550 |
'username' => $user->get_username(), |
| 551 |
'password_reset_key' => get_password_reset_key( $user_object ), |
| 552 |
] |
| 553 |
), |
| 554 |
], |
| 555 |
] |
| 556 |
) )->send(); |
| 557 |
|
| 558 |
return hp\rest_response( |
| 559 |
200, |
| 560 |
[ |
| 561 |
'id' => $user->get_id(), |
| 562 |
] |
| 563 |
); |
| 564 |
} |
| 565 |
|
| 566 |
/** |
| 567 |
* Resets user password. |
| 568 |
* |
| 569 |
* @param WP_REST_Request $request API request. |
| 570 |
* @return WP_Rest_Response |
| 571 |
*/ |
| 572 |
public function reset_user_password( $request ) { |
| 573 |
|
| 574 |
// Check permissions. |
| 575 |
if ( is_user_logged_in() && ! current_user_can( 'edit_users' ) ) { |
| 576 |
return hp\rest_error( 403 ); |
| 577 |
} |
| 578 |
|
| 579 |
// Validate form. |
| 580 |
$form = ( new Forms\User_Password_Reset() )->set_values( $request->get_params() ); |
| 581 |
|
| 582 |
if ( ! $form->validate() ) { |
| 583 |
return hp\rest_error( 400, $form->get_errors() ); |
| 584 |
} |
| 585 |
|
| 586 |
// Get user. |
| 587 |
$user_object = check_password_reset_key( $form->get_value( 'password_reset_key' ), $form->get_value( 'username' ) ); |
| 588 |
|
| 589 |
if ( is_wp_error( $user_object ) ) { |
| 590 |
return hp\rest_error( 401, esc_html__( 'Password reset key is expired or invalid.', 'hivepress' ) ); |
| 591 |
} |
| 592 |
|
| 593 |
$user = Models\User::query()->get_by_id( $user_object ); |
| 594 |
|
| 595 |
// Reset password. |
| 596 |
reset_password( $user_object, $form->get_value( 'password' ) ); |
| 597 |
|
| 598 |
// Authenticate user. |
| 599 |
if ( ! is_user_logged_in() ) { |
| 600 |
do_action( 'hivepress/v1/models/user/login' ); |
| 601 |
|
| 602 |
wp_signon( |
| 603 |
[ |
| 604 |
'user_login' => $user->get_username(), |
| 605 |
'user_password' => $form->get_value( 'password' ), |
| 606 |
'remember' => true, |
| 607 |
] |
| 608 |
); |
| 609 |
} |
| 610 |
|
| 611 |
return hp\rest_response( |
| 612 |
200, |
| 613 |
[ |
| 614 |
'id' => $user->get_id(), |
| 615 |
] |
| 616 |
); |
| 617 |
} |
| 618 |
|
| 619 |
/** |
| 620 |
* Updates user. |
| 621 |
* |
| 622 |
* @param WP_REST_Request $request API request. |
| 623 |
* @return WP_Rest_Response |
| 624 |
*/ |
| 625 |
public function update_user( $request ) { |
| 626 |
|
| 627 |
// Check authentication. |
| 628 |
if ( ! is_user_logged_in() ) { |
| 629 |
return hp\rest_error( 401 ); |
| 630 |
} |
| 631 |
|
| 632 |
// Get user. |
| 633 |
$user = Models\User::query()->get_by_id( $request->get_param( 'user_id' ) ); |
| 634 |
|
| 635 |
if ( empty( $user ) ) { |
| 636 |
return hp\rest_error( 404 ); |
| 637 |
} |
| 638 |
|
| 639 |
// Check permissions. |
| 640 |
if ( ! current_user_can( 'edit_users' ) && get_current_user_id() !== $user->get_id() ) { |
| 641 |
return hp\rest_error( 403 ); |
| 642 |
} |
| 643 |
|
| 644 |
// Validate form. |
| 645 |
$form = null; |
| 646 |
|
| 647 |
if ( $request->get_param( '_form' ) === 'user_update_profile' ) { |
| 648 |
$form = new Forms\User_Update_Profile( [ 'model' => $user ] ); |
| 649 |
} else { |
| 650 |
$form = new Forms\User_Update( [ 'model' => $user ] ); |
| 651 |
} |
| 652 |
|
| 653 |
$form->set_values( $request->get_params() ); |
| 654 |
|
| 655 |
if ( ! $form->validate() ) { |
| 656 |
return hp\rest_error( 400, $form->get_errors() ); |
| 657 |
} |
| 658 |
|
| 659 |
// Check password. |
| 660 |
if ( get_current_user_id() === $user->get_id() && ( $form->get_value( 'email' ) !== $user->get_email() || $form->get_value( 'password' ) ) ) { |
| 661 |
if ( ! $form->get_value( 'current_password' ) ) { |
| 662 |
return hp\rest_error( 400, esc_html__( 'Current password is required.', 'hivepress' ) ); |
| 663 |
} |
| 664 |
|
| 665 |
if ( ! wp_check_password( $form->get_value( 'current_password' ), $user->get_password(), $user->get_id() ) ) { |
| 666 |
return hp\rest_error( 401, esc_html__( 'Current password is incorrect.', 'hivepress' ) ); |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
// Check email. |
| 671 |
if ( get_option( 'hp_user_verify_email' ) && $form->get_value( 'email' ) !== $user->get_email() ) { |
| 672 |
|
| 673 |
// Set email key. |
| 674 |
$email_key = md5( $form->get_value( 'email' ) . time() . wp_rand() ); |
| 675 |
|
| 676 |
update_user_meta( $user->get_id(), 'hp_email_verify_key', $email_key ); |
| 677 |
update_user_meta( $user->get_id(), 'hp_email_verify_address', $form->get_value( 'email' ) ); |
| 678 |
|
| 679 |
// Send email. |
| 680 |
( new Emails\User_Email_Verify( |
| 681 |
[ |
| 682 |
'recipient' => $form->get_value( 'email' ), |
| 683 |
|
| 684 |
'tokens' => [ |
| 685 |
'user' => $user, |
| 686 |
'user_name' => $user->get_display_name(), |
| 687 |
'email_verify_url' => hivepress()->router->get_url( |
| 688 |
'user_email_verify_page', |
| 689 |
[ |
| 690 |
'username' => $user->get_username(), |
| 691 |
'email_verify_key' => $email_key, |
| 692 |
] |
| 693 |
), |
| 694 |
], |
| 695 |
] |
| 696 |
) )->send(); |
| 697 |
|
| 698 |
// Set old email. |
| 699 |
$form->set_value( 'email', $user->get_email() ); |
| 700 |
} |
| 701 |
|
| 702 |
// Update user. |
| 703 |
$user->fill( $form->get_values() ); |
| 704 |
|
| 705 |
if ( ! $user->save() ) { |
| 706 |
return hp\rest_error( 400, $user->_get_errors() ); |
| 707 |
} |
| 708 |
|
| 709 |
return hp\rest_response( |
| 710 |
200, |
| 711 |
[ |
| 712 |
'id' => $user->get_id(), |
| 713 |
] |
| 714 |
); |
| 715 |
} |
| 716 |
|
| 717 |
/** |
| 718 |
* Deletes user. |
| 719 |
* |
| 720 |
* @param WP_REST_Request $request API request. |
| 721 |
* @return WP_Rest_Response |
| 722 |
*/ |
| 723 |
public function delete_user( $request ) { |
| 724 |
|
| 725 |
// Check authentication. |
| 726 |
if ( ! is_user_logged_in() ) { |
| 727 |
return hp\rest_error( 401 ); |
| 728 |
} |
| 729 |
|
| 730 |
// Check settings. |
| 731 |
if ( ! get_option( 'hp_user_allow_deletion', true ) ) { |
| 732 |
return hp\rest_error( 403 ); |
| 733 |
} |
| 734 |
|
| 735 |
// Get user. |
| 736 |
$user = Models\User::query()->get_by_id( $request->get_param( 'user_id' ) ); |
| 737 |
|
| 738 |
if ( empty( $user ) ) { |
| 739 |
return hp\rest_error( 404 ); |
| 740 |
} |
| 741 |
|
| 742 |
// Check permissions. |
| 743 |
if ( ! current_user_can( 'delete_users' ) && get_current_user_id() !== $user->get_id() ) { |
| 744 |
return hp\rest_error( 403 ); |
| 745 |
} |
| 746 |
|
| 747 |
// Check password. |
| 748 |
if ( get_current_user_id() === $user->get_id() ) { |
| 749 |
$form = ( new Forms\User_Delete() )->set_values( $request->get_params() ); |
| 750 |
|
| 751 |
if ( ! $form->validate() ) { |
| 752 |
return hp\rest_error( 400, $form->get_errors() ); |
| 753 |
} |
| 754 |
|
| 755 |
if ( ! wp_check_password( $form->get_value( 'password' ), $user->get_password(), $user->get_id() ) ) { |
| 756 |
return hp\rest_error( 401, esc_html__( 'Password is incorrect.', 'hivepress' ) ); |
| 757 |
} |
| 758 |
|
| 759 |
// Clear authentication. |
| 760 |
wp_clear_auth_cookie(); |
| 761 |
} |
| 762 |
|
| 763 |
// Send email. |
| 764 |
( new Emails\User_Delete( |
| 765 |
[ |
| 766 |
'recipient' => $user->get_email(), |
| 767 |
|
| 768 |
'tokens' => [ |
| 769 |
'user' => $user, |
| 770 |
'user_name' => $user->get_display_name(), |
| 771 |
], |
| 772 |
] |
| 773 |
) )->send(); |
| 774 |
|
| 775 |
// Delete user. |
| 776 |
if ( ! $user->delete() ) { |
| 777 |
return hp\rest_error( 400 ); |
| 778 |
} |
| 779 |
|
| 780 |
return hp\rest_response( 204 ); |
| 781 |
} |
| 782 |
|
| 783 |
/** |
| 784 |
* Redirects user account page. |
| 785 |
* |
| 786 |
* @return mixed |
| 787 |
*/ |
| 788 |
public function redirect_user_account_page() { |
| 789 |
|
| 790 |
// Check authentication. |
| 791 |
if ( ! is_user_logged_in() ) { |
| 792 |
return hivepress()->router->get_return_url( 'user_login_page' ); |
| 793 |
} |
| 794 |
|
| 795 |
// Get menu items. |
| 796 |
$menu_items = ( new Menus\User_Account() )->get_items(); |
| 797 |
|
| 798 |
if ( $menu_items ) { |
| 799 |
return hp\get_array_value( hp\get_first_array_value( $menu_items ), 'url' ); |
| 800 |
} |
| 801 |
|
| 802 |
return true; |
| 803 |
} |
| 804 |
|
| 805 |
/** |
| 806 |
* Redirects user login page. |
| 807 |
* |
| 808 |
* @return mixed |
| 809 |
*/ |
| 810 |
public function redirect_user_login_page() { |
| 811 |
if ( is_user_logged_in() ) { |
| 812 |
if ( hivepress()->router->get_redirect_url() ) { |
| 813 |
return hivepress()->router->get_redirect_url(); |
| 814 |
} else { |
| 815 |
return hivepress()->router->get_url( 'user_account_page' ); |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
return false; |
| 820 |
} |
| 821 |
|
| 822 |
/** |
| 823 |
* Renders user login page. |
| 824 |
* |
| 825 |
* @return string |
| 826 |
*/ |
| 827 |
public function render_user_login_page() { |
| 828 |
return ( new Blocks\Template( |
| 829 |
[ |
| 830 |
'template' => 'user_login_page', |
| 831 |
] |
| 832 |
) )->render(); |
| 833 |
} |
| 834 |
|
| 835 |
/** |
| 836 |
* Redirects user logout page. |
| 837 |
* |
| 838 |
* @return mixed |
| 839 |
*/ |
| 840 |
public function redirect_user_logout_page() { |
| 841 |
if ( is_user_logged_in() ) { |
| 842 |
wp_logout(); |
| 843 |
} |
| 844 |
|
| 845 |
return true; |
| 846 |
} |
| 847 |
|
| 848 |
/** |
| 849 |
* Redirects user password reset page. |
| 850 |
* |
| 851 |
* @return mixed |
| 852 |
*/ |
| 853 |
public function redirect_user_password_reset_page() { |
| 854 |
if ( is_user_logged_in() ) { |
| 855 |
if ( hivepress()->router->get_redirect_url() ) { |
| 856 |
return hivepress()->router->get_redirect_url(); |
| 857 |
} else { |
| 858 |
return hivepress()->router->get_url( 'user_account_page' ); |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
return false; |
| 863 |
} |
| 864 |
|
| 865 |
/** |
| 866 |
* Renders user password reset page. |
| 867 |
* |
| 868 |
* @return string |
| 869 |
*/ |
| 870 |
public function render_user_password_reset_page() { |
| 871 |
return ( new Blocks\Template( |
| 872 |
[ |
| 873 |
'template' => 'user_password_reset_page', |
| 874 |
] |
| 875 |
) )->render(); |
| 876 |
} |
| 877 |
|
| 878 |
/** |
| 879 |
* Redirects user email verify page. |
| 880 |
* |
| 881 |
* @return mixed |
| 882 |
*/ |
| 883 |
public function redirect_user_email_verify_page() { |
| 884 |
|
| 885 |
// Check permissions. |
| 886 |
if ( ! get_option( 'hp_user_verify_email' ) ) { |
| 887 |
return true; |
| 888 |
} |
| 889 |
|
| 890 |
// Get username and email key. |
| 891 |
$username = sanitize_user( hp\get_array_value( $_GET, 'username' ) ); |
| 892 |
$email_key = sanitize_key( hp\get_array_value( $_GET, 'email_verify_key' ) ); |
| 893 |
|
| 894 |
if ( ! $username || ! $email_key ) { |
| 895 |
return true; |
| 896 |
} |
| 897 |
|
| 898 |
// Get user. |
| 899 |
$user = get_user_by( 'login', $username ); |
| 900 |
|
| 901 |
// Check email key. |
| 902 |
if ( ! $user || $user->hp_email_verify_key !== $email_key ) { |
| 903 |
return true; |
| 904 |
} |
| 905 |
|
| 906 |
// Delete email key. |
| 907 |
delete_user_meta( $user->ID, 'hp_email_verify_key' ); |
| 908 |
|
| 909 |
if ( is_email( $user->hp_email_verify_address ) ) { |
| 910 |
|
| 911 |
// Set new email. |
| 912 |
wp_update_user( |
| 913 |
[ |
| 914 |
'ID' => $user->ID, |
| 915 |
'user_email' => $user->hp_email_verify_address, |
| 916 |
] |
| 917 |
); |
| 918 |
|
| 919 |
// Delete new email. |
| 920 |
delete_user_meta( $user->ID, 'hp_email_verify_address' ); |
| 921 |
|
| 922 |
// Redirect user. |
| 923 |
return hivepress()->router->get_url( 'user_edit_settings_page' ); |
| 924 |
} |
| 925 |
|
| 926 |
// Send email. |
| 927 |
( new Emails\User_Register( |
| 928 |
[ |
| 929 |
'recipient' => $user->user_email, |
| 930 |
|
| 931 |
'tokens' => [ |
| 932 |
'user' => Models\User::query()->get_by_id( $user->ID ), |
| 933 |
'user_name' => $user->display_name, |
| 934 |
'user_password' => '********', |
| 935 |
], |
| 936 |
] |
| 937 |
) )->send(); |
| 938 |
|
| 939 |
// Check authentication. |
| 940 |
if ( is_user_logged_in() ) { |
| 941 |
return hivepress()->router->get_url( 'user_account_page' ); |
| 942 |
} |
| 943 |
|
| 944 |
// Authenticate user. |
| 945 |
do_action( 'hivepress/v1/models/user/login' ); |
| 946 |
|
| 947 |
wp_set_auth_cookie( $user->ID, true ); |
| 948 |
|
| 949 |
do_action( 'wp_login', $user->user_login, $user ); |
| 950 |
|
| 951 |
// Redirect user. |
| 952 |
$redirect = $user->hp_email_verify_redirect; |
| 953 |
|
| 954 |
if ( $redirect ) { |
| 955 |
delete_user_meta( $user->ID, 'hp_email_verify_redirect' ); |
| 956 |
|
| 957 |
return $redirect; |
| 958 |
} |
| 959 |
|
| 960 |
return false; |
| 961 |
} |
| 962 |
|
| 963 |
/** |
| 964 |
* Renders user email verify page. |
| 965 |
* |
| 966 |
* @return string |
| 967 |
*/ |
| 968 |
public function render_user_email_verify_page() { |
| 969 |
return ( new Blocks\Template( |
| 970 |
[ |
| 971 |
'template' => 'user_email_verify_page', |
| 972 |
] |
| 973 |
) )->render(); |
| 974 |
} |
| 975 |
|
| 976 |
/** |
| 977 |
* Redirects user edit settings page. |
| 978 |
* |
| 979 |
* @return mixed |
| 980 |
*/ |
| 981 |
public function redirect_user_edit_settings_page() { |
| 982 |
if ( ! is_user_logged_in() ) { |
| 983 |
return hivepress()->router->get_return_url( 'user_login_page' ); |
| 984 |
} |
| 985 |
|
| 986 |
return false; |
| 987 |
} |
| 988 |
|
| 989 |
/** |
| 990 |
* Renders user edit settings page. |
| 991 |
* |
| 992 |
* @return string |
| 993 |
*/ |
| 994 |
public function render_user_edit_settings_page() { |
| 995 |
return ( new Blocks\Template( |
| 996 |
[ |
| 997 |
'template' => 'user_edit_settings_page', |
| 998 |
|
| 999 |
'context' => [ |
| 1000 |
'user' => hivepress()->request->get_user(), |
| 1001 |
], |
| 1002 |
] |
| 1003 |
) )->render(); |
| 1004 |
} |
| 1005 |
|
| 1006 |
/** |
| 1007 |
* Gets user view title. |
| 1008 |
* |
| 1009 |
* @return string |
| 1010 |
*/ |
| 1011 |
public function get_user_view_title() { |
| 1012 |
$title = null; |
| 1013 |
|
| 1014 |
// Get user. |
| 1015 |
$user = Models\User::query()->filter( |
| 1016 |
[ |
| 1017 |
'username' => hivepress()->request->get_param( 'username' ), |
| 1018 |
] |
| 1019 |
)->get_first(); |
| 1020 |
|
| 1021 |
if ( $user ) { |
| 1022 |
$title = sprintf( esc_html__( 'Profile of %s', 'hivepress' ), $user->get_display_name() ); |
| 1023 |
} |
| 1024 |
|
| 1025 |
// Set request context. |
| 1026 |
hivepress()->request->set_context( 'viewed_user', $user ); |
| 1027 |
|
| 1028 |
return $title; |
| 1029 |
} |
| 1030 |
|
| 1031 |
/** |
| 1032 |
* Redirects user view page. |
| 1033 |
* |
| 1034 |
* @return mixed |
| 1035 |
*/ |
| 1036 |
public function redirect_user_view_page() { |
| 1037 |
|
| 1038 |
// Check settings. |
| 1039 |
if ( ! get_option( 'hp_user_enable_display' ) ) { |
| 1040 |
return true; |
| 1041 |
} |
| 1042 |
|
| 1043 |
// Get user. |
| 1044 |
$user = hivepress()->request->get_context( 'viewed_user' ); |
| 1045 |
|
| 1046 |
if ( ! $user ) { |
| 1047 |
wp_die( esc_html__( 'No users found.', 'hivepress' ) ); |
| 1048 |
} |
| 1049 |
|
| 1050 |
if ( get_option( 'hp_user_verify_email' ) && get_user_meta( $user->get_id(), 'hp_email_verify_key', true ) ) { |
| 1051 |
return true; |
| 1052 |
} |
| 1053 |
|
| 1054 |
// Get vendor ID. |
| 1055 |
$vendor_id = Models\Vendor::query()->filter( |
| 1056 |
[ |
| 1057 |
'user' => $user->get_id(), |
| 1058 |
'status' => 'publish', |
| 1059 |
] |
| 1060 |
)->get_first_id(); |
| 1061 |
|
| 1062 |
if ( $vendor_id ) { |
| 1063 |
return hivepress()->router->get_url( 'vendor_view_page', [ 'vendor_id' => $vendor_id ] ); |
| 1064 |
} |
| 1065 |
|
| 1066 |
return false; |
| 1067 |
} |
| 1068 |
|
| 1069 |
/** |
| 1070 |
* Renders user view page. |
| 1071 |
* |
| 1072 |
* @return string |
| 1073 |
*/ |
| 1074 |
public function render_user_view_page() { |
| 1075 |
return ( new Blocks\Template( |
| 1076 |
[ |
| 1077 |
'template' => 'user_view_page', |
| 1078 |
|
| 1079 |
'context' => [ |
| 1080 |
'user' => hivepress()->request->get_context( 'viewed_user' ), |
| 1081 |
], |
| 1082 |
] |
| 1083 |
) )->render(); |
| 1084 |
} |
| 1085 |
} |
| 1086 |
|