AvatarRoute.php
2 years ago
LocationRoute.php
4 years ago
PasswordRoute.php
2 weeks ago
ProfileRoute.php
4 years ago
Tab.php
2 years ago
PasswordRoute.php
87 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonorDashboards\Tabs\EditProfileTab; |
| 4 | |
| 5 | use Give\Donors\Models\Donor; |
| 6 | use Give\DonorDashboards\Tabs\Contracts\Route as RouteAbstract; |
| 7 | use WP_REST_Request; |
| 8 | use WP_REST_Response; |
| 9 | |
| 10 | /** |
| 11 | * @since 2.10.0 |
| 12 | */ |
| 13 | class PasswordRoute extends RouteAbstract |
| 14 | { |
| 15 | |
| 16 | /** |
| 17 | * @inheritdoc |
| 18 | */ |
| 19 | public function endpoint() |
| 20 | { |
| 21 | return 'password'; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @inheritdoc |
| 26 | */ |
| 27 | public function args() |
| 28 | { |
| 29 | return [ |
| 30 | 'newPassword' => [ |
| 31 | 'type' => 'string', |
| 32 | 'required' => true, |
| 33 | 'sanitize_callback' => 'sanitize_text_field', |
| 34 | ], |
| 35 | ]; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Handles password update. |
| 40 | * |
| 41 | * @since 4.16.6 added password validation |
| 42 | * @since 3.3.0 |
| 43 | * |
| 44 | * @param WP_REST_Request $request |
| 45 | * |
| 46 | * @return array|WP_REST_Response |
| 47 | */ |
| 48 | public function handleRequest(WP_REST_Request $request) |
| 49 | { |
| 50 | $newPassword = trim($request->get_param('newPassword')); |
| 51 | |
| 52 | if (empty($newPassword)) { |
| 53 | return new WP_REST_Response( |
| 54 | [ |
| 55 | 'status' => 400, |
| 56 | 'response' => 'invalid_password', |
| 57 | 'body_response' => [ |
| 58 | 'message' => esc_html__('Please enter a valid password.', 'give'), |
| 59 | ], |
| 60 | ] |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | $donorId = give()->donorDashboard->getId(); |
| 65 | |
| 66 | $donor = Donor::find($donorId); |
| 67 | |
| 68 | if ( ! $donor || empty($donor->userId)) { |
| 69 | return new WP_REST_Response( |
| 70 | [ |
| 71 | 'status' => 400, |
| 72 | 'response' => 'no_user_account', |
| 73 | 'body_response' => [ |
| 74 | 'message' => esc_html__('Unable to update password. Contact a site administrator.', 'give'), |
| 75 | ], |
| 76 | ] |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | wp_set_password($newPassword, $donor->userId); |
| 81 | |
| 82 | return [ |
| 83 | 'success' => true, |
| 84 | ]; |
| 85 | } |
| 86 | } |
| 87 |