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