| 1 |
<?php |
| 2 |
class LP_REST_Profile_Controller extends LP_Abstract_REST_Controller { |
| 3 |
public function __construct() { |
| 4 |
$this->namespace = 'lp/v1'; |
| 5 |
$this->rest_base = 'profile'; |
| 6 |
|
| 7 |
parent::__construct(); |
| 8 |
} |
| 9 |
|
| 10 |
public function register_routes() { |
| 11 |
$this->routes = array( |
| 12 |
'statistic' => array( |
| 13 |
array( |
| 14 |
'methods' => WP_REST_Server::READABLE, |
| 15 |
'callback' => array( $this, 'statistic' ), |
| 16 |
'permission_callback' => array( $this, 'check_permission' ), |
| 17 |
), |
| 18 |
), |
| 19 |
'course-tab' => array( |
| 20 |
array( |
| 21 |
'methods' => WP_REST_Server::READABLE, |
| 22 |
'callback' => array( $this, 'course_tab' ), |
| 23 |
'permission_callback' => array( $this, 'check_permission' ), |
| 24 |
), |
| 25 |
), |
| 26 |
'course-attend' => array( |
| 27 |
array( |
| 28 |
'methods' => WP_REST_Server::CREATABLE, |
| 29 |
'callback' => array( $this, 'course_attend' ), |
| 30 |
'permission_callback' => array( $this, 'check_permission' ), |
| 31 |
), |
| 32 |
), |
| 33 |
'get-avatar' => array( |
| 34 |
array( |
| 35 |
'methods' => WP_REST_Server::READABLE, |
| 36 |
'callback' => array( $this, 'get_avatar' ), |
| 37 |
'permission_callback' => function() { |
| 38 |
return get_current_user_id() ? true : false; |
| 39 |
}, |
| 40 |
), |
| 41 |
), |
| 42 |
'upload-avatar' => array( |
| 43 |
array( |
| 44 |
'methods' => WP_REST_Server::CREATABLE, |
| 45 |
'callback' => array( $this, 'upload_avatar' ), |
| 46 |
'permission_callback' => function() { |
| 47 |
return get_current_user_id() ? true : false; |
| 48 |
}, |
| 49 |
), |
| 50 |
), |
| 51 |
'remove-avatar' => array( |
| 52 |
array( |
| 53 |
'methods' => WP_REST_Server::CREATABLE, |
| 54 |
'callback' => array( $this, 'remove_avatar' ), |
| 55 |
'permission_callback' => function() { |
| 56 |
return get_current_user_id() ? true : false; |
| 57 |
}, |
| 58 |
), |
| 59 |
), |
| 60 |
); |
| 61 |
|
| 62 |
parent::register_routes(); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Check permission |
| 67 |
* |
| 68 |
* @param $request |
| 69 |
* |
| 70 |
* @return bool |
| 71 |
*/ |
| 72 |
public function check_permission( $request ): bool { |
| 73 |
$user_id = $request->get_param( 'userID' ); |
| 74 |
|
| 75 |
if ( empty( $user_id ) ) { |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
$profile = learn_press_get_profile( $user_id ); |
| 80 |
|
| 81 |
if ( ! $profile->current_user_can( 'view-tab-courses' ) ) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
return true; |
| 86 |
} |
| 87 |
|
| 88 |
public function get_avatar( WP_REST_Request $request ) { |
| 89 |
$response = new LP_REST_Response(); |
| 90 |
|
| 91 |
$thumb_size = learn_press_get_avatar_thumb_size(); |
| 92 |
|
| 93 |
$profile = learn_press_get_profile( get_current_user_id() ); |
| 94 |
$avatar_url = $profile->get_upload_profile_src(); |
| 95 |
|
| 96 |
$response->data->width = $thumb_size['width']; |
| 97 |
$response->data->height = $thumb_size['height']; |
| 98 |
$response->data->url = $avatar_url ? $avatar_url : ''; |
| 99 |
|
| 100 |
return rest_ensure_response( $response ); |
| 101 |
} |
| 102 |
|
| 103 |
public function upload_avatar( WP_REST_Request $request ) { |
| 104 |
$file_base64 = $request->get_param( 'file' ); |
| 105 |
$response = new LP_REST_Response(); |
| 106 |
|
| 107 |
try { |
| 108 |
$user_id = get_current_user_id(); |
| 109 |
|
| 110 |
if ( ! $user_id ) { |
| 111 |
throw new Exception( __( 'User not found', 'learnpress' ) ); |
| 112 |
} |
| 113 |
|
| 114 |
if ( empty( $file_base64 ) ) { |
| 115 |
throw new Exception( __( 'File not found', 'learnpress' ) ); |
| 116 |
} |
| 117 |
|
| 118 |
$upload_dir = learn_press_user_profile_picture_upload_dir( true ); |
| 119 |
|
| 120 |
$target_dir = LP_WP_Filesystem::instance()->is_dir( $upload_dir['path'] ); |
| 121 |
|
| 122 |
if ( ! $target_dir ) { |
| 123 |
wp_mkdir_p( $upload_dir['path'] ); |
| 124 |
} |
| 125 |
|
| 126 |
if ( ! LP_WP_Filesystem::instance()->is_writable( $upload_dir['path'] ) ) { |
| 127 |
throw new Exception( __( 'The upload directory is not writable', 'learnpress' ) ); |
| 128 |
} |
| 129 |
|
| 130 |
// Delete old image if exists |
| 131 |
$path_img = get_user_meta( $user_id, '_lp_profile_picture', true ); |
| 132 |
|
| 133 |
if ( $path_img ) { |
| 134 |
$path = $upload_dir['basedir'] . '/' . $path_img; |
| 135 |
|
| 136 |
if ( file_exists( $path ) ) { |
| 137 |
LP_WP_Filesystem::instance()->unlink( $path ); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
$file_name = md5( $user_id . microtime( true ) ) . '.jpeg'; |
| 142 |
|
| 143 |
$file_base64 = str_replace( 'data:image/jpeg;base64,', '', $file_base64 ); |
| 144 |
$file_base64 = base64_decode( $file_base64 ); |
| 145 |
|
| 146 |
$put_content = LP_WP_Filesystem::instance()->put_contents( $upload_dir['path'] . '/' . $file_name, $file_base64, FS_CHMOD_FILE ); |
| 147 |
|
| 148 |
if ( ! $put_content ) { |
| 149 |
throw new Exception( __( 'Cannot write the file', 'learnpress' ) ); |
| 150 |
} |
| 151 |
|
| 152 |
update_user_meta( $user_id, '_lp_profile_picture', $upload_dir['subdir'] . '/' . $file_name ); |
| 153 |
|
| 154 |
$response->status = 'success'; |
| 155 |
$response->message = __( 'Avatar updated', 'learnpress' ); |
| 156 |
} catch ( \Throwable $th ) { |
| 157 |
$response->message = $th->getMessage(); |
| 158 |
} |
| 159 |
|
| 160 |
return rest_ensure_response( $response ); |
| 161 |
} |
| 162 |
|
| 163 |
public function remove_avatar( WP_REST_Request $request ) { |
| 164 |
$response = new LP_REST_Response(); |
| 165 |
|
| 166 |
try { |
| 167 |
$user_id = get_current_user_id(); |
| 168 |
|
| 169 |
if ( ! $user_id ) { |
| 170 |
throw new Exception( esc_html__( 'The user is invalid', 'learnpress' ) ); |
| 171 |
} |
| 172 |
|
| 173 |
$upload_dir = learn_press_user_profile_picture_upload_dir( true ); |
| 174 |
|
| 175 |
$target_dir = LP_WP_Filesystem::instance()->is_dir( $upload_dir['path'] ); |
| 176 |
|
| 177 |
if ( ! $target_dir ) { |
| 178 |
wp_mkdir_p( $upload_dir['path'] ); |
| 179 |
} |
| 180 |
|
| 181 |
if ( ! LP_WP_Filesystem::instance()->is_writable( $upload_dir['path'] ) ) { |
| 182 |
throw new Exception( __( 'The upload directory is not writable', 'learnpress' ) ); |
| 183 |
} |
| 184 |
|
| 185 |
$path_img = get_user_meta( $user_id, '_lp_profile_picture', true ); |
| 186 |
|
| 187 |
if ( $path_img ) { |
| 188 |
$path = $upload_dir['basedir'] . '/' . $path_img; |
| 189 |
|
| 190 |
if ( file_exists( $path ) ) { |
| 191 |
LP_WP_Filesystem::instance()->unlink( $path ); |
| 192 |
|
| 193 |
$response->status = 'success'; |
| 194 |
$response->message = esc_html__( 'The profile picture has been removed successfully', 'learnpress' ); |
| 195 |
} |
| 196 |
} |
| 197 |
} catch ( \Throwable $th ) { |
| 198 |
$response->message = $th->getMessage(); |
| 199 |
} |
| 200 |
|
| 201 |
return rest_ensure_response( $response ); |
| 202 |
} |
| 203 |
|
| 204 |
public function statistic( WP_REST_Request $request ) { |
| 205 |
$user_id = $request->get_param( 'userID' ); |
| 206 |
$response = new LP_REST_Response(); |
| 207 |
$response->data = ''; |
| 208 |
|
| 209 |
try { |
| 210 |
if ( empty( $user_id ) ) { |
| 211 |
throw new Exception( esc_html__( 'No user ID found!', 'learnpress' ) ); |
| 212 |
} |
| 213 |
|
| 214 |
$user = learn_press_get_user( $user_id ); |
| 215 |
|
| 216 |
if ( ! $user ) { |
| 217 |
throw new Exception( esc_html__( 'The user does not exist!', 'learnpress' ) ); |
| 218 |
} |
| 219 |
|
| 220 |
$profile = learn_press_get_profile( $user_id ); |
| 221 |
|
| 222 |
if ( $profile instanceof WP_Error ) { |
| 223 |
throw new Exception( $profile->get_error_message() ); |
| 224 |
} |
| 225 |
|
| 226 |
$statistic = $profile->get_statistic_info(); |
| 227 |
|
| 228 |
do_action( 'learnpress/rest/frontend/profile/statistic', $request ); |
| 229 |
|
| 230 |
$response->data = learn_press_get_template_content( 'profile/tabs/courses/general-statistic', compact( 'statistic', 'user' ) ); |
| 231 |
$response->status = 'success'; |
| 232 |
} catch ( Exception $e ) { |
| 233 |
$response->message = $e->getMessage(); |
| 234 |
} |
| 235 |
|
| 236 |
return rest_ensure_response( $response ); |
| 237 |
} |
| 238 |
|
| 239 |
public function course_tab( $request ) { |
| 240 |
$params = $request->get_params(); |
| 241 |
$user_id = $params['userID'] ?? get_current_user_id(); |
| 242 |
$status = $params['status'] ?? ''; |
| 243 |
$paged = $params['paged'] ?? 1; |
| 244 |
$query_type = $params['query'] ?? 'purchased'; |
| 245 |
$layout = $params['layout'] ?? 'grid'; |
| 246 |
$response = new LP_REST_Response(); |
| 247 |
|
| 248 |
try { |
| 249 |
if ( empty( $user_id ) ) { |
| 250 |
throw new Exception( esc_html__( 'No user ID found!', 'learnpress' ) ); |
| 251 |
} |
| 252 |
|
| 253 |
$profile = learn_press_get_profile( $user_id ); |
| 254 |
|
| 255 |
$query = $profile->query_courses( |
| 256 |
$query_type, |
| 257 |
apply_filters( |
| 258 |
'learnpress/rest/frontend/profile/course_tab/query', |
| 259 |
array( |
| 260 |
'status' => $status, |
| 261 |
'limit' => LP_Settings::get_option( 'archive_course_limit', 6 ), |
| 262 |
'paged' => $paged, |
| 263 |
), |
| 264 |
$request |
| 265 |
) |
| 266 |
); |
| 267 |
|
| 268 |
// LP_User_Item_Course. |
| 269 |
$course_item_objects = ! empty( $query['items'] ) ? $query['items'] : false; |
| 270 |
|
| 271 |
if ( empty( $course_item_objects ) ) { |
| 272 |
throw new Exception( esc_html__( 'No Course available!', 'learnpress' ) ); |
| 273 |
} |
| 274 |
|
| 275 |
$course_ids = array_map( |
| 276 |
function( $course_object ) { |
| 277 |
return ! is_object( $course_object ) ? absint( $course_object ) : $course_object->get_id(); |
| 278 |
}, |
| 279 |
$course_item_objects |
| 280 |
); |
| 281 |
|
| 282 |
if ( empty( $course_ids ) ) { |
| 283 |
throw new Exception( esc_html__( 'No Course IDs available!', 'learnpress' ) ); |
| 284 |
} |
| 285 |
|
| 286 |
$user = learn_press_get_user( $user_id ); |
| 287 |
|
| 288 |
if ( empty( $user ) ) { |
| 289 |
throw new Exception( esc_html__( 'No User available!', 'learnpress' ) ); |
| 290 |
} |
| 291 |
|
| 292 |
do_action( 'learnpress/rest/frontend/profile/course_tab', $params ); |
| 293 |
|
| 294 |
$num_pages = $query->get_pages(); |
| 295 |
$current_page = $query->get_paged(); |
| 296 |
|
| 297 |
$template = $layout === 'grid' ? 'profile/tabs/courses/course-grid' : 'profile/tabs/courses/course-list'; |
| 298 |
|
| 299 |
$response->data = learn_press_get_template_content( |
| 300 |
$template, |
| 301 |
array( |
| 302 |
'user' => $user, |
| 303 |
'course_ids' => $course_ids, |
| 304 |
'num_pages' => max( absint( $num_pages ), 1 ), |
| 305 |
'current_page' => absint( $current_page ), |
| 306 |
) |
| 307 |
); |
| 308 |
$response->status = 'success'; |
| 309 |
|
| 310 |
} catch ( Exception $e ) { |
| 311 |
$response->message = $e->getMessage(); |
| 312 |
} |
| 313 |
|
| 314 |
return rest_ensure_response( $response ); |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Get course's user attend |
| 319 |
* |
| 320 |
* @param WP_REST_Request $request |
| 321 |
* |
| 322 |
* @author tungnx |
| 323 |
* @since 4.1.5 |
| 324 |
* @version 1.0.0 |
| 325 |
* @return LP_REST_Response |
| 326 |
*/ |
| 327 |
public function course_attend( WP_REST_Request $request ): LP_REST_Response { |
| 328 |
$params = $request->get_params(); |
| 329 |
$user_id = get_current_user_id(); |
| 330 |
$status = $params['status'] ?? ''; |
| 331 |
$paged = $params['paged'] ?? 1; |
| 332 |
$layout = $params['layout'] ?? 'grid'; |
| 333 |
$response = new LP_REST_Response(); |
| 334 |
|
| 335 |
try { |
| 336 |
if ( ! $user_id ) { |
| 337 |
throw new Exception( __( 'The user is invalid', 'learnpress' ) ); |
| 338 |
} |
| 339 |
|
| 340 |
$filter = new LP_User_Items_Filter(); |
| 341 |
$filter->limit = LP_Settings::get_option( 'archive_course_limit', 6 ); |
| 342 |
$filter->user_id = $user_id; |
| 343 |
$total_rows = 0; |
| 344 |
$courses = LP_User_Item_Course::get_user_courses( $filter, $total_rows ); |
| 345 |
$response->data->courses = $courses; |
| 346 |
$response->data->total_pages = LP_Database::get_total_pages( $filter->limit, $total_rows ); |
| 347 |
} catch ( Throwable $e ) { |
| 348 |
$response->message = $e->getMessage(); |
| 349 |
} |
| 350 |
|
| 351 |
return $response; |
| 352 |
} |
| 353 |
} |
| 354 |
|