AvatarRoute.php
3 years ago
LocationRoute.php
4 years ago
ProfileRoute.php
4 years ago
Tab.php
4 years ago
ProfileRoute.php
120 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonorDashboards\Tabs\EditProfileTab; |
| 4 | |
| 5 | use Give\DonorDashboards\Helpers\SanitizeProfileData as SanitizeHelper; |
| 6 | use Give\DonorDashboards\Profile as Profile; |
| 7 | use Give\DonorDashboards\Tabs\Contracts\Route as RouteAbstract; |
| 8 | use WP_REST_Request; |
| 9 | |
| 10 | /** |
| 11 | * @since 2.10.0 |
| 12 | */ |
| 13 | class ProfileRoute extends RouteAbstract |
| 14 | { |
| 15 | |
| 16 | /** |
| 17 | * @inheritdoc |
| 18 | */ |
| 19 | public function endpoint() |
| 20 | { |
| 21 | return 'profile'; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * @inheritdoc |
| 26 | */ |
| 27 | public function args() |
| 28 | { |
| 29 | return [ |
| 30 | 'data' => [ |
| 31 | 'type' => 'string', |
| 32 | 'required' => true, |
| 33 | 'sanitize_callback' => [$this, 'sanitizeData'], |
| 34 | ], |
| 35 | 'id' => [ |
| 36 | 'type' => 'int', |
| 37 | 'required' => true, |
| 38 | ], |
| 39 | ]; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Handles profile update, and returns updated profile array |
| 44 | * |
| 45 | * @since 2.10.0 |
| 46 | * |
| 47 | * @param WP_REST_Request $request |
| 48 | * |
| 49 | * @return array |
| 50 | * |
| 51 | */ |
| 52 | public function handleRequest(WP_REST_Request $request) |
| 53 | { |
| 54 | return $this->updateProfile($request->get_param('data'), give()->donorDashboard->getId()); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Updates profile model, then gets newly stored profile data to return |
| 59 | * |
| 60 | * @since 2.10.0 |
| 61 | * |
| 62 | * @param int $id Profile id to update |
| 63 | * |
| 64 | * @param object $data Object representing profile data to update |
| 65 | * |
| 66 | * @return array |
| 67 | * |
| 68 | */ |
| 69 | protected function updateProfile($data, $id) |
| 70 | { |
| 71 | $profile = new Profile($id); |
| 72 | $profile->update($data); |
| 73 | |
| 74 | return [ |
| 75 | 'profile' => $profile->getProfileData(), |
| 76 | ]; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Sanitize profile data object |
| 81 | * |
| 82 | * @since 2.10.0 |
| 83 | * |
| 84 | * @param \WP_REST_Request $request |
| 85 | * @param string $param |
| 86 | * |
| 87 | * @param string $value JSON encoded string representing profile data |
| 88 | * |
| 89 | * @return object |
| 90 | * |
| 91 | */ |
| 92 | public function sanitizeData($value, $request, $param) |
| 93 | { |
| 94 | $sanitizeHelper = '\Give\DonorDashboards\Helpers\SanitizeProfileData'; |
| 95 | $values = json_decode($value); |
| 96 | |
| 97 | return [ |
| 98 | 'firstName' => sanitize_text_field($values->firstName), |
| 99 | 'lastName' => sanitize_text_field($values->lastName), |
| 100 | 'company' => sanitize_text_field($values->company), |
| 101 | 'additionalEmails' => SanitizeHelper::sanitizeAdditionalEmails($values->additionalEmails), |
| 102 | 'additionalAddresses' => SanitizeHelper::sanitizeAdditionalAddresses($values->additionalAddresses), |
| 103 | 'primaryEmail' => sanitize_email($values->primaryEmail), |
| 104 | 'primaryAddress' => SanitizeHelper::sanitizeAddress($values->primaryAddress), |
| 105 | 'titlePrefix' => sanitize_text_field($values->titlePrefix), |
| 106 | 'avatarId' => SanitizeHelper::sanitizeInt($values->avatarId), |
| 107 | 'isAnonymous' => intval($values->isAnonymous), |
| 108 | ]; |
| 109 | } |
| 110 | |
| 111 | protected function sanitizeValue($value, $config) |
| 112 | { |
| 113 | if ( ! empty($value) && is_callable($config['sanitizeCallback'])) { |
| 114 | return $config['sanitizeCallback']($value); |
| 115 | } else { |
| 116 | return $config['default']; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 |