← All changes
|
inc/rest-api/v1/frontend/class-lp-rest-profile-controller.php
+21
-9
4.4.5
→
4.4.9
View file →
| @@ -1,6 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +use LearnPress\Helpers\Response; | |
| 3 | 4 | use LearnPress\Helpers\Template; |
| 4 | 5 | use LearnPress\Models\UserModel; |
| 5 | 6 | |
| 6 | 7 | class LP_REST_Profile_Controller extends LP_Abstract_REST_Controller { |
| @@ -159,9 +160,13 @@ | ||
| 159 | 160 | |
| 160 | 161 | // Delete old image if exists |
| 161 | 162 | $path_img = $userModel->get_meta_value_by_key( UserModel::META_KEY_IMAGE ); |
| 162 | 163 | if ( $path_img ) { |
| 163 | - $path = $upload_dir['basedir'] . '/' . $path_img; | |
| 164 | + if ( 0 === strpos( $path_img, '/' ) ) { | |
| 165 | + $path = $upload_dir['basedir'] . $path_img; | |
| 166 | + } else { | |
| 167 | + $path = trailingslashit( $upload_dir['path'] ) . basename( $path_img ); | |
| 168 | + } | |
| 164 | 169 | |
| 165 | 170 | if ( file_exists( $path ) ) { |
| 166 | 171 | LP_WP_Filesystem::instance()->unlink( $path ); |
| 167 | 172 | } |
| @@ -215,9 +220,10 @@ | ||
| 215 | 220 | |
| 216 | 221 | $editor->set_quality( 100 ); |
| 217 | 222 | $editor->save( $upload_dir['path'] . '/' . $file_name ); |
| 218 | 223 | |
| 219 | - $userModel->set_meta_value_by_key( UserModel::META_KEY_IMAGE, $file_name ); | |
| 224 | + $path_save = trailingslashit( $upload_dir['subdir'] ) . $file_name; | |
| 225 | + $userModel->set_meta_value_by_key( UserModel::META_KEY_IMAGE, $path_save ); | |
| 220 | 226 | do_action( 'learnpress/rest/frontend/profile/upload_avatar', $user_id ); |
| 221 | 227 | |
| 222 | 228 | $response->status = 'success'; |
| 223 | 229 | $response->message = __( 'Avatar updated', 'learnpress' ); |
| @@ -228,9 +234,9 @@ | ||
| 228 | 234 | return rest_ensure_response( $response ); |
| 229 | 235 | } |
| 230 | 236 | |
| 231 | 237 | public function remove_avatar( WP_REST_Request $request ) { |
| 232 | - $response = new LP_REST_Response(); | |
| 238 | + $response = new Response(); | |
| 233 | 239 | |
| 234 | 240 | try { |
| 235 | 241 | $user_id = get_current_user_id(); |
| 236 | 242 | |
| @@ -249,21 +255,27 @@ | ||
| 249 | 255 | if ( ! LP_WP_Filesystem::instance()->is_writable( $upload_dir['path'] ) ) { |
| 250 | 256 | throw new Exception( __( 'The upload directory is not writable', 'learnpress' ) ); |
| 251 | 257 | } |
| 252 | 258 | |
| 253 | - $path_img = get_user_meta( $user_id, '_lp_profile_picture', true ); | |
| 259 | + $path_img = get_user_meta( $user_id, UserModel::META_KEY_IMAGE, true ); | |
| 254 | 260 | |
| 255 | 261 | if ( $path_img ) { |
| 256 | - $path = $upload_dir['basedir'] . '/' . $path_img; | |
| 262 | + if ( 0 === strpos( $path_img, '/' ) ) { | |
| 263 | + $path = $upload_dir['basedir'] . $path_img; | |
| 264 | + } else { | |
| 265 | + $path = trailingslashit( $upload_dir['path'] ) . basename( $path_img ); | |
| 266 | + } | |
| 257 | 267 | |
| 258 | 268 | if ( file_exists( $path ) ) { |
| 259 | 269 | LP_WP_Filesystem::instance()->unlink( $path ); |
| 270 | + } | |
| 260 | 271 | |
| 261 | - $response->status = 'success'; | |
| 262 | - $response->message = esc_html__( 'The profile picture has been removed successfully', 'learnpress' ); | |
| 263 | - } | |
| 272 | + delete_user_meta( $user_id, UserModel::META_KEY_IMAGE ); | |
| 273 | + | |
| 274 | + $response->status = Response::STATUS_SUCCESS; | |
| 275 | + $response->message = esc_html__( 'The profile picture has been removed successfully', 'learnpress' ); | |
| 264 | 276 | } |
| 265 | - } catch ( \Throwable $th ) { | |
| 277 | + } catch ( Throwable $th ) { | |
| 266 | 278 | $response->message = $th->getMessage(); |
| 267 | 279 | } |
| 268 | 280 | |
| 269 | 281 | return rest_ensure_response( $response ); |