PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.9.1
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.9.1
4.4.9.1 4.4.9 4.4.8 4.4.7 4.4.6 4.4.5 4.4.4 4.4.3 4.4.2 4.4.1 4.4.0 4.3.9.1 4.3.9 4.3.8 4.3.7 4.1.6.9 4.1.6.9.1 4.1.6.9.2 4.1.6.9.3 4.1.6.9.4 4.1.7 4.1.7.1 4.1.7.2 4.1.7.3 4.1.7.3.1 All 141 releases
← All changes | inc/user/lp-user-functions.php +1574 -1728 4.3.9.1 → 4.4.9.1 View file →
@@ -1,1728 +1,1574 @@
1 -<?php
2 -
3 -use LearnPress\Models\UserItems\UserItemModel;
4 -use LearnPress\Models\UserModel;
5 -
6 -/**
7 - * Common functions to process actions about user
8 - *
9 - * @author ThimPress
10 - * @package LearnPress/Functions/User
11 - * @version 1.0
12 - */
13 -function learn_press_get_user_profile_tabs() {
14 - return LP_Profile::instance()->get_tabs();
15 -}
16 -
17 -/**
18 - * Ensure new users receive a public slug.
19 - *
20 - * @param int $user_id
21 - *
22 - * @return void
23 - */
24 -function learn_press_maybe_generate_user_pretty_slug_on_register( int $user_id ) {
25 - $userModel = UserModel::find( $user_id, true );
26 - if ( ! $userModel ) {
27 - return;
28 - }
29 -
30 - $userModel->generate_pretty_slug();
31 -}
32 -
33 -//add_action( 'user_register', 'learn_press_maybe_generate_user_pretty_slug_on_register' );
34 -
35 -/**
36 - * Delete user data by user ID
37 - *
38 - * @param int $user_id
39 - * @param int $course_id
40 - */
41 -function learn_press_delete_user_data( $user_id, $course_id = 0 ) {
42 - global $wpdb;
43 - // TODO: Should be deleted user's order and order data???
44 -
45 - $query_args = array( $user_id );
46 -
47 - if ( $course_id ) {
48 - $query_args[] = $course_id;
49 - }
50 -
51 - $query = $wpdb->prepare(
52 - "
53 - SELECT user_item_id
54 - FROM {$wpdb->prefix}learnpress_user_items
55 - WHERE user_id = %d
56 - " . ( $course_id ? ' AND item_id = %d' : '' ) . '
57 - ',
58 - $query_args
59 - );
60 -
61 - // delete all courses user has enrolled
62 - $query = $wpdb->prepare(
63 - "
64 - DELETE FROM {$wpdb->prefix}learnpress_user_items
65 - WHERE user_id = %d
66 - " . ( $course_id ? ' AND item_id = %d' : '' ) . '
67 - ',
68 - $query_args
69 - );
70 -
71 - @$wpdb->query( $query );
72 -}
73 -
74 -/**
75 - * Get current user ID
76 - *
77 - * @return int|string LP_User is int, LP_User_Guest is string
78 - */
79 -function learn_press_get_current_user_id() {
80 - $user = learn_press_get_current_user();
81 -
82 - if ( ! $user ) {
83 - return 0;
84 - }
85 -
86 - return $user->get_id();
87 -}
88 -
89 -/**
90 - * Get the user by $user_id passed. If $user_id is NULL, get current user.
91 - * If current user is not logged in, return a GUEST user
92 - *
93 - * @param bool $create_temp - Optional. Create temp user if user is not logged in.
94 - *
95 - * @return LP_User|LP_User_Guest
96 - * @editor tungnx
97 - * @modify 4.1.4
98 - * @version 1.0.1
99 - */
100 -function learn_press_get_current_user( $create_temp = true ) {
101 - $user_id = get_current_user_id();
102 -
103 - if ( $user_id ) {
104 - return learn_press_get_user( $user_id );
105 - }
106 -
107 - // Return LP_User_Guest
108 - return learn_press_get_user( 0 );
109 -}
110 -
111 -if ( ! function_exists( 'learn_press_get_user' ) ) {
112 - /**
113 - * Get user by ID. Return false if the user does not exist.
114 - * If user_id = 0, return a guest user.
115 - *
116 - * @param int $user_id
117 - * @param bool $current
118 - *
119 - * @return LP_User|LP_User_Guest|false
120 - * @since 3.0.0
121 - * @version 4.0.1
122 - */
123 - function learn_press_get_user( $user_id = 0, $current = false, $force_new = false ) {
124 - $userClass = $user_id && get_user_by( 'ID', $user_id ) ? 'LP_User' : ( $user_id == 0 ? 'LP_User_Guest' : false );
125 - if ( false === $userClass ) {
126 - return false;
127 - }
128 -
129 - return new $userClass( $user_id );
130 - }
131 -}
132 -
133 -/**
134 - * Add more 2 user roles teacher and student
135 - */
136 -function learn_press_add_user_roles() {
137 - $course_cap = LP_COURSE_CPT . 's';
138 - $lesson_cap = LP_LESSON_CPT . 's';
139 - $order_cap = LP_ORDER_CPT . 's';
140 -
141 - if ( ! get_role( LP_TEACHER_ROLE ) ) {
142 - add_role( LP_TEACHER_ROLE, 'LP Instructor' );
143 - }
144 -
145 - $teacher = get_role( LP_TEACHER_ROLE );
146 - if ( $teacher ) {
147 - $teacher->add_cap( 'read_private_' . $course_cap );
148 - $teacher->add_cap( 'delete_published_' . $course_cap );
149 - $teacher->add_cap( 'edit_published_' . $course_cap );
150 - $teacher->add_cap( 'edit_' . $course_cap );
151 - $teacher->add_cap( 'delete_' . $course_cap );
152 - // $teacher->add_cap( 'unfiltered_html' );
153 - if ( $teacher->has_cap( 'unfiltered_html' ) ) {
154 - $teacher->remove_cap( 'unfiltered_html' );
155 - }
156 -
157 - if ( LP_Settings::get_option( 'required_review', 'yes' ) == 'yes' ) {
158 - $teacher->remove_cap( 'publish_' . $course_cap );
159 - } else {
160 - $teacher->add_cap( 'publish_' . $course_cap );
161 - }
162 -
163 - $teacher->add_cap( 'read_private_' . $lesson_cap );
164 - $teacher->add_cap( 'delete_published_' . $lesson_cap );
165 - $teacher->add_cap( 'edit_published_' . $lesson_cap );
166 - $teacher->add_cap( 'edit_' . $lesson_cap );
167 - $teacher->add_cap( 'delete_' . $lesson_cap );
168 - $teacher->add_cap( 'publish_' . $lesson_cap );
169 - $teacher->add_cap( 'upload_files' );
170 - $teacher->add_cap( 'read' );
171 - $teacher->add_cap( 'edit_posts' );
172 - }
173 -
174 - // administrator
175 - $admin = get_role( 'administrator' );
176 - if ( $admin ) {
177 - $admin->add_cap( 'lp_mcp_access' );
178 -
179 - $admin->add_cap( 'read_private_' . $course_cap );
180 - $admin->add_cap( 'delete_' . $course_cap );
181 - $admin->add_cap( 'delete_published_' . $course_cap );
182 - $admin->add_cap( 'edit_' . $course_cap );
183 - $admin->add_cap( 'edit_published_' . $course_cap );
184 - $admin->add_cap( 'publish_' . $course_cap );
185 - $admin->add_cap( 'delete_private_' . $course_cap );
186 - $admin->add_cap( 'edit_private_' . $course_cap );
187 - $admin->add_cap( 'delete_others_' . $course_cap );
188 - $admin->add_cap( 'edit_others_' . $course_cap );
189 -
190 - $admin->add_cap( 'read_private_' . $lesson_cap );
191 - $admin->add_cap( 'delete_' . $lesson_cap );
192 - $admin->add_cap( 'delete_published_' . $lesson_cap );
193 - $admin->add_cap( 'edit_' . $lesson_cap );
194 - $admin->add_cap( 'edit_published_' . $lesson_cap );
195 - $admin->add_cap( 'publish_' . $lesson_cap );
196 - $admin->add_cap( 'delete_private_' . $lesson_cap );
197 - $admin->add_cap( 'edit_private_' . $lesson_cap );
198 - $admin->add_cap( 'delete_others_' . $lesson_cap );
199 - $admin->add_cap( 'edit_others_' . $lesson_cap );
200 -
201 - $admin->add_cap( 'delete_' . $order_cap );
202 - $admin->add_cap( 'delete_published_' . $order_cap );
203 - $admin->add_cap( 'edit_' . $order_cap );
204 - $admin->add_cap( 'edit_published_' . $order_cap );
205 - $admin->add_cap( 'publish_' . $order_cap );
206 - $admin->add_cap( 'delete_private_' . $order_cap );
207 - $admin->add_cap( 'edit_private_' . $order_cap );
208 - $admin->add_cap( 'delete_others_' . $order_cap );
209 - $admin->add_cap( 'edit_others_' . $order_cap );
210 - }
211 -}
212 -
213 -/**
214 - * Remove capabilities added for MCP integration.
215 - *
216 - * @return void
217 - */
218 -function learn_press_remove_mcp_capabilities() {
219 -
220 - $admin = get_role( 'administrator' );
221 - if ( $admin ) {
222 - $admin->remove_cap( 'lp_mcp_access' );
223 - }
224 -}
225 -/*
226 -function learn_press_current_user_is( $check_type = null ) {
227 - global $current_user;
228 - $user_roles = $current_user->roles;
229 - $user_type = '';
230 -
231 - if ( in_array( 'lpr_teacher', $user_roles ) ) {
232 - $user_type = 'instructor';
233 - } elseif ( in_array( 'lp_teacher', $user_roles ) ) {
234 - $user_type = 'instructor';
235 - } elseif ( in_array( 'administrator', $user_roles ) ) {
236 - $user_type = 'administrator';
237 - }
238 -
239 - return $check_type ? $check_type == $user_type : $user_type;
240 -}*/
241 -
242 -/*
243 -function learn_press_user_has_roles( $roles, $user_id = null ) {
244 - $has_role = false;
245 - if ( ! $user_id ) {
246 - $user = wp_get_current_user();
247 - } else {
248 - $user = get_user_by( 'id', $user_id );
249 - }
250 - $available_roles = (array) $user->roles;
251 - if ( is_array( $roles ) ) {
252 - foreach ( $roles as $role ) {
253 - if ( in_array( $role, $available_roles ) ) {
254 - $has_role = true;
255 - break; // only need one of roles is in available
256 - }
257 - }
258 - } else {
259 - if ( in_array( $roles, $available_roles ) ) {
260 - $has_role = true;
261 - }
262 - }
263 -
264 - return $has_role;
265 -}*/
266 -
267 -/*
268 -function learn_press_current_user_can_view_profile_section( $section, $user ) {
269 - $current_user = wp_get_current_user();
270 - $view = true;
271 - if ( $user->get_data( 'user_login' ) != $current_user->user_login && $section == LP_Settings::instance()->get(
272 - 'profile_endpoints.orders',
273 - 'profile-orders'
274 - ) ) {
275 - $view = false;
276 - }
277 -
278 - return apply_filters( 'learn_press_current_user_can_view_profile_section', $view, $section, $user );
279 -}*/
280 -
281 -/*
282 -function learn_press_profile_tab_quizzes_content( $current, $tab, $user ) {
283 - learn_press_get_template(
284 - 'profile/tabs/quizzes.php',
285 - array(
286 - 'user' => $user,
287 - 'current' => $current,
288 - 'tab' => $tab,
289 - )
290 - );
291 -}*/
292 -
293 -/*
294 -function learn_press_profile_tab_orders_content( $current, $tab, $user ) {
295 - learn_press_get_template(
296 - 'profile/tabs/orders.php',
297 - array(
298 - 'user' => $user,
299 - 'current' => $current,
300 - 'tab' => $tab,
301 - )
302 - );
303 -}*/
304 -
305 -/**
306 - * Get queried user in profile link
307 - *
308 - * @return false|WP_User
309 - * @since 3.0.0
310 - * @deprecated 4.2.9.1
311 - */
312 -function learn_press_get_profile_user() {
313 - return LP_Profile::instance()->get_user_current();
314 -
315 - return LP_Profile::get_queried_user();
316 -}
317 -
318 -
319 -/**
320 - * Add instructor registration button to register page and admin bar
321 - */
322 -function learn_press_user_become_teacher_registration_form() {
323 - if ( LP_Settings::instance()->get( 'instructor_registration' ) != 'yes' ) {
324 - return;
325 - }
326 - ?>
327 - <p>
328 - <label for="become_teacher">
329 - <input type="checkbox" name="become_teacher" id="become_teacher">
330 - <?php esc_html_e( 'Want to become an instructor?', 'learnpress' ); ?>
331 - </label>
332 - </p>
333 - <?php
334 -}
335 -
336 -add_action( 'register_form', 'learn_press_user_become_teacher_registration_form' );
337 -
338 -/**
339 - * Update data into table learnpress_user_items.
340 - *
341 - * @param array $fields - Fields and values to be updated.
342 - * Format: array(
343 - * field_name_1 => value 1,
344 - * field_name_2 => value 2,
345 - * ....
346 - * field_name_n => value n
347 - * )
348 - * @param mixed $where - Optional. Fields with values for conditional update with the same format of $fields.
349 - * @param bool $update_cache - Optional. Should be update to cache or not (since 3.0.0).
350 - * @param bool $update_extra_fields_as_meta - Optional. Update extra fields as item meta (since 3.1.0).
351 - *
352 - * @return mixed
353 - */
354 -function learn_press_update_user_item_field( array $fields = array(), $where = false, $update_cache = true, $update_extra_fields_as_meta = false ) {
355 - global $wpdb;
356 -
357 - // Table fields format.
358 - $table_fields = array(
359 - 'user_item_id' => '%d',
360 - 'user_id' => '%d',
361 - 'item_id' => '%d',
362 - 'ref_id' => '%d',
363 - 'start_time' => '%s',
364 - 'end_time' => '%s',
365 - 'access_level' => '%d',
366 - 'graduation' => '%s',
367 - 'item_type' => '%s',
368 - 'status' => '%s',
369 - 'ref_type' => '%s',
370 - 'parent_id' => '%d',
371 - );
372 -
373 - /**
374 - * Validate item status
375 - */
376 - if ( ! empty( $fields['item_id'] ) && ! empty( $fields['status'] ) ) {
377 - $item_type = learn_press_get_post_type( $fields['item_id'] );
378 -
379 - if ( LP_COURSE_CPT === $item_type ) {
380 - if ( 'completed' === $fields['status'] ) {
381 - $fields['status'] = 'finished';
382 - }
383 - } elseif ( 'finished' === $fields['status'] ) {
384 - $fields['status'] = 'completed';
385 - }
386 - }
387 -
388 - $data = array();
389 - $data_format = array();
390 - foreach ( $fields as $field => $value ) {
391 - if ( ! empty( $table_fields[ $field ] ) ) {
392 - $data[ $field ] = $value;
393 -
394 - // Do not format the date-time field if it's value is NULL
395 - if ( in_array( $field, array( 'start_time', 'end_time' ) ) && empty( $value ) ) {
396 - $data[ $field ] = null;
397 - $data_format[] = '';
398 - } else {
399 - if ( $value instanceof LP_Datetime ) {
400 - $data[ $field ] = $value->toSql();
401 - }
402 - $data_format[] = $table_fields[ $field ];
403 - }
404 - }
405 - }
406 -
407 - if ( ! empty( $fields['user_item_id'] ) ) {
408 - $where = wp_parse_args(
409 - $where,
410 - array( 'user_item_id' => $fields['user_item_id'] )
411 - );
412 - }
413 -
414 - // Set where and where format
415 - $where_format = array();
416 - if ( is_array( $where ) && ! empty( $where ) ) {
417 - if ( empty( $where['user_id'] ) ) {
418 - $where['user_id'] = ! empty( $fields['user_id'] ) ? $fields['user_id'] : learn_press_get_current_user_id();
419 - }
420 -
421 - foreach ( $where as $field => $value ) {
422 - if ( isset( $table_fields[ $field ] ) ) {
423 - $where_format[] = $table_fields[ $field ];
424 - }
425 - }
426 - }
427 -
428 - if ( ! $data ) {
429 - return false;
430 - }
431 -
432 - $inserted = false;
433 - $updated = false;
434 -
435 - // Update to database.
436 - if ( $where ) {
437 - $updated = $wpdb->update(
438 - $wpdb->learnpress_user_items,
439 - $data,
440 - $where,
441 - $data_format,
442 - $where_format
443 - );
444 - } else {
445 - // Insert to database.
446 - if ( $wpdb->insert(
447 - $wpdb->learnpress_user_items,
448 - $data,
449 - $data_format
450 - ) ) {
451 - $inserted = $wpdb->insert_id;
452 - }
453 - }
454 -
455 - if ( $updated && ! empty( $where['user_item_id'] ) ) {
456 - $inserted = $where['user_item_id'];
457 - }
458 -
459 - /**
460 - * @var object|bool $updated_item
461 - */
462 - $updated_item = false;
463 -
464 - // Get the item we just have updated or inserted.
465 - if ( $inserted ) {
466 - $updated_item = learn_press_get_user_item( $inserted );
467 - } elseif ( $updated ) {
468 - $updated_item = learn_press_get_user_item( $where );
469 - }
470 -
471 - // Clear caches.
472 - if ( $updated_item ) {
473 - // Clear cache total students enrolled.
474 - if ( isset( $updated_item->item_type )
475 - && LP_COURSE_CPT === $updated_item->item_type
476 - && isset( $updated_item->item_id ) ) {
477 - $lp_course_cache = new LP_Course_Cache( true );
478 - $lp_course_cache->clean_total_students_enrolled( $updated_item->item_id );
479 - $lp_course_cache->clean_total_students_enrolled_or_purchased( $updated_item->item_id );
480 - // Clear cache count students many courses.
481 - $lp_courses_cache = new LP_Courses_Cache( true );
482 - $lp_courses_cache->clear_cache_on_group( LP_Courses_Cache::KEYS_COUNT_STUDENT_COURSES );
483 - }
484 - // Clear cache user item.
485 - $lp_user_items_cache = new LP_User_Items_Cache();
486 - $lp_user_items_cache->clean_user_item(
487 - array(
488 - $updated_item->user_id,
489 - $updated_item->item_id,
490 - $updated_item->item_type,
491 - )
492 - );
493 - // Clear cache userItemModel
494 - $userItemModel = UserItemModel::find_user_item(
495 - $updated_item->user_id,
496 - $updated_item->item_id,
497 - $updated_item->item_type,
498 - $updated_item->ref_id,
499 - $updated_item->ref_type,
500 - true
501 - );
502 - $userItemModel->clean_caches();
503 - }
504 -
505 - do_action( 'learn-press/updated-user-item-field', $updated_item );
506 -
507 - /**
508 - * If there is some fields does not contain in the main table
509 - * then consider update them as metadata.
510 - *
511 - * @comment by tungnx - 4.1.7.3
512 - */
513 - /*
514 - if ( $updated_item && $update_extra_fields_as_meta ) {
515 - $extra_fields = array_diff_key( $fields, $table_fields );
516 - if ( $extra_fields ) {
517 - foreach ( $extra_fields as $meta_key => $meta_value ) {
518 - if ( $meta_value == 'user_item_id' ) {
519 - continue;
520 - }
521 -
522 - if ( $meta_value === false ) {
523 - learn_press_delete_user_item_meta( $updated_item->user_item_id, $meta_key );
524 - } else {
525 -
526 - if ( empty( $meta_value ) ) {
527 - $meta_value = '';
528 - }
529 - learn_press_update_user_item_meta( $updated_item->user_item_id, $meta_key, $meta_value );
530 - }
531 - }
532 - }
533 - }*/
534 -
535 - // do_action( 'learn-press/updated-user-item-meta', $updated_item );
536 -
537 - return $updated_item;
538 -}
539 -
540 -/**
541 - * Get user item row(s) from user items table by multiple WHERE conditional
542 - *
543 - * @param array|int $where
544 - * @param bool $single
545 - *
546 - * @return array
547 - */
548 -function learn_press_get_user_item( $where, $single = true ) {
549 - global $wpdb;
550 -
551 - // Table fields
552 - $table_fields = array(
553 - 'user_item_id' => '%d',
554 - 'user_id' => '%d',
555 - 'item_id' => '%d',
556 - 'ref_id' => '%d',
557 - 'start_time' => '%s',
558 - 'end_time' => '%s',
559 - 'item_type' => '%s',
560 - 'status' => '%s',
561 - 'ref_type' => '%s',
562 - 'parent_id' => '%d',
563 - );
564 -
565 - // If $where is a number consider we are searching the record with unique user_item_id
566 - if ( is_numeric( $where ) ) {
567 - $where = array( 'user_item_id' => $where );
568 - }
569 -
570 - $where_str = array();
571 - foreach ( $where as $field => $value ) {
572 - if ( ! empty( $table_fields[ $field ] ) ) {
573 - $where_str[] = "{$field} = " . $table_fields[ $field ];
574 - }
575 - }
576 - $item = false;
577 -
578 - if ( $where_str ) {
579 - $query = $wpdb->prepare(
580 - "
581 - SELECT *
582 - FROM {$wpdb->prefix}learnpress_user_items
583 - WHERE " . join( ' AND ', $where_str ) . '
584 - ORDER BY user_item_id DESC
585 - ',
586 - $where
587 - );
588 - if ( $single || ! empty( $where['user_item_id'] ) ) {
589 - $item = $wpdb->get_row( $query );
590 - } else {
591 - $item = $wpdb->get_results( $query );
592 - }
593 - }
594 -
595 - return $item;
596 -}
597 -
598 -/**
599 - * Get user item meta from user_itemmeta table
600 - *
601 - * @param int $user_item_id .
602 - * @param string $meta_key .
603 - * @param bool $single .
604 - *
605 - * @return mixed
606 - */
607 -function learn_press_get_user_item_meta( $user_item_id = 0, $meta_key = '', $single = true ) {
608 - $meta = false;
609 - if ( metadata_exists( 'learnpress_user_item', $user_item_id, $meta_key ) ) {
610 - $meta = get_metadata( 'learnpress_user_item', $user_item_id, $meta_key, $single );
611 - }
612 -
613 - return $meta;
614 -}
615 -
616 -/**
617 - * Add user item meta into table user_itemmeta
618 - *
619 - * @param int $user_item_id
620 - * @param string $meta_key
621 - * @param mixed $meta_value
622 - * @param string $prev_value
623 - *
624 - * @return false|int
625 - */
626 -function learn_press_add_user_item_meta( $user_item_id, $meta_key, $meta_value, $prev_value = '' ) {
627 - return add_metadata( 'learnpress_user_item', $user_item_id, $meta_key, $meta_value, $prev_value );
628 -}
629 -
630 -/**
631 - * Update user item meta to table user_itemmeta
632 - *
633 - * @param int $user_item_id
634 - * @param string $meta_key
635 - * @param mixed $meta_value
636 - * @param string $prev_value
637 - *
638 - * @return bool|int
639 - */
640 -function learn_press_update_user_item_meta( $user_item_id, $meta_key, $meta_value, $prev_value = '' ) {
641 - return update_metadata( 'learnpress_user_item', $user_item_id, $meta_key, $meta_value, $prev_value );
642 -}
643 -
644 -
645 -/**
646 - * Update user item meta to table user_itemmeta
647 - *
648 - * @param int $object_id
649 - * @param string $meta_key
650 - * @param mixed $meta_value
651 - * @param bool $delete_all
652 - *
653 - * @return bool|int
654 - */
655 -function learn_press_delete_user_item_meta( $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
656 - return delete_metadata( 'learnpress_user_item', $object_id, $meta_key, $meta_value, $delete_all );
657 -}
658 -
659 -if ( ! function_exists( 'learn_press_pre_get_avatar_callback' ) ) {
660 - /**
661 - * Filter the avatar
662 - *
663 - * @param string $avatar
664 - * @param string $id_or_email
665 - * @param array $args
666 - *
667 - * @return string
668 - * @since 1.0.0
669 - * @version 1.0.2
670 - */
671 - function learn_press_pre_get_avatar_callback( $avatar, $id_or_email = '', $args = array() ) {
672 - try {
673 - if ( ( isset( $args['gravatar'] ) && $args['gravatar'] ) || ( $args['default'] && $args['force_default'] ) ) {
674 - return $avatar;
675 - }
676 -
677 - $user_id = 0;
678 -
679 - /**
680 - * Get the ID of user from $id_or_email
681 - */
682 - if ( ! is_numeric( $id_or_email ) && is_string( $id_or_email ) ) {
683 - $user = get_user_by( 'email', $id_or_email );
684 - if ( $user ) {
685 - $user_id = $user->ID;
686 - }
687 - } elseif ( is_numeric( $id_or_email ) ) {
688 - $user_id = $id_or_email;
689 - } elseif ( is_object( $id_or_email ) && isset( $id_or_email->user_id ) && $id_or_email->user_id ) {
690 - $user_id = $id_or_email->user_id;
691 - } elseif ( $id_or_email instanceof WP_Comment ) {
692 - $user = get_user_by( 'email', $id_or_email->comment_author_email );
693 - if ( $user ) {
694 - $user_id = $user->ID;
695 - }
696 - }
697 -
698 - if ( ! $user_id ) {
699 - return $avatar;
700 - }
701 -
702 - $user = UserModel::find( $user_id, true );
703 - if ( ! $user ) {
704 - return $avatar;
705 - }
706 -
707 - $class = array( 'avatar', 'avatar-' . (int) $args['size'], 'photo' );
708 - if ( $args['class'] ) {
709 - if ( is_array( $args['class'] ) ) {
710 - $class = array_merge( $class, $args['class'] );
711 - } else {
712 - $class[] = $args['class'];
713 - }
714 - }
715 -
716 - $avatar_url = $user->get_avatar_url();
717 - $html_img = sprintf(
718 - "<img src='%s' srcset='%s' class='%s' height='%d' width='%d' decoding='async'/>",
719 - esc_url( $avatar_url ),
720 - esc_url( $avatar_url ) . ' 2x',
721 - esc_attr( implode( ' ', $class ) ),
722 - (int) $args['height'],
723 - (int) $args['width']
724 - );
725 -
726 - return $html_img;
727 - } catch ( Throwable $e ) {
728 - error_log( $e->getMessage() );
729 -
730 - return $avatar;
731 - }
732 - }
733 -}
734 -add_filter( 'pre_get_avatar', 'learn_press_pre_get_avatar_callback', 999, 3 );
735 -
736 -
737 -function learn_press_user_profile_picture_upload_dir( $width_user = true ) {
738 - $upload_dir = wp_upload_dir();
739 - $subdir = apply_filters( 'learn_press_user_profile_folder', 'learn-press-profile', $width_user );
740 - if ( $width_user ) {
741 - $subdir .= '/' . get_current_user_id();
742 - }
743 - $subdir = '/' . $subdir;
744 -
745 - if ( ! empty( $upload_dir['subdir'] ) ) {
746 - $u_subdir = str_replace( '\\', '/', $upload_dir['subdir'] );
747 - $u_path = str_replace( '\\', '/', $upload_dir['path'] );
748 -
749 - $upload_dir['path'] = str_replace( $u_subdir, $subdir, $u_path );
750 - $upload_dir['url'] = str_replace( $u_subdir, $subdir, $upload_dir['url'] );
751 - } else {
752 - $upload_dir['path'] = $upload_dir['path'] . $subdir;
753 - $upload_dir['url'] = $upload_dir['url'] . $subdir;
754 - }
755 -
756 - $upload_dir['subdir'] = $subdir;
757 -
758 - // Point path/url to main site if we are in multisite
759 - if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) {
760 - foreach ( array( 'path', 'url', 'basedir', 'baseurl' ) as $v ) {
761 - $upload_dir[ $v ] = str_replace( '/sites/' . get_current_blog_id(), '', $upload_dir[ $v ] );
762 - }
763 - }
764 -
765 - return $upload_dir;
766 -}
767 -
768 -/*
769 -add_action( 'learn_press_before_purchase_course_handler', '_learn_press_before_purchase_course_handler', 10, 2 );
770 -function _learn_press_before_purchase_course_handler( $course_id, $cart ) {
771 - // Redirect to login page if user is not logged in
772 - if ( ! is_user_logged_in() ) {
773 - $return_url = esc_url_raw( add_query_arg( $_POST, get_the_permalink( $course_id ) ) );
774 - $return_url = apply_filters( 'learn_press_purchase_course_login_redirect_return_url', $return_url );
775 - $redirect = apply_filters(
776 - 'learn_press_purchase_course_login_redirect',
777 - learn_press_get_login_url( $return_url )
778 - );
779 - if ( $redirect !== false ) {
780 - learn_press_add_message( __( 'Please log in to enroll in this course', 'learnpress' ) );
781 -
782 - if ( learn_press_is_ajax() ) {
783 - learn_press_send_json(
784 - array(
785 - 'redirect' => $redirect,
786 - 'result' => 'success',
787 - )
788 - );
789 - } else {
790 - wp_redirect( $redirect );
791 - exit();
792 - }
793 - }
794 - } else {
795 - $user = learn_press_get_current_user();
796 - $redirect = false;
797 - if ( $user->has_finished_course( $course_id ) ) {
798 - learn_press_add_message( __( 'You have already finished the course', 'learnpress' ) );
799 - $redirect = true;
800 - } elseif ( $user->has_purchased_course( $course_id ) ) {
801 - learn_press_add_message( __( 'You have already enrolled in this course', 'learnpress' ) );
802 - $redirect = true;
803 - }
804 - if ( $redirect ) {
805 - wp_redirect( get_the_permalink( $course_id ) );
806 - exit();
807 - }
808 - }
809 -}*/
810 -
811 -/*
812 -function learn_press_user_is( $role, $user_id = 0 ) {
813 - if ( ! $user_id ) {
814 - $user = learn_press_get_current_user();
815 - } else {
816 - $user = learn_press_get_user( $user_id );
817 - }
818 - if ( $role == 'admin' ) {
819 - return $user->is_admin();
820 - }
821 - if ( $role == 'instructor' ) {
822 - return $user->is_instructor();
823 - }
824 -
825 - return $role;
826 -}*/
827 -
828 -/*
829 -function learn_press_profile_tab_edit_content( $current, $tab, $user ) {
830 - learn_press_get_template(
831 - 'profile/tabs/edit.php',
832 - array(
833 - 'user' => $user,
834 - 'current' => $current,
835 - 'tab' => $tab,
836 - )
837 - );
838 -}*/
839 -
840 -/*
841 -function learn_press_update_user_option( $name, $value, $id = 0 ) {
842 - if ( ! $id ) {
843 - $id = get_current_user_id();
844 - }
845 - $key = 'learnpress_user_options';
846 - $options = get_user_option( $key, $id );
847 - $options[ $name ] = $value;
848 - update_user_option( $id, $key, $options, true );
849 -}*/
850 -
851 -/*
852 -function learn_press_delete_user_option( $name, $id = 0 ) {
853 - if ( ! $id ) {
854 - $id = get_current_user_id();
855 - }
856 - $key = 'learnpress_user_options';
857 - $options = get_user_option( $key, $id );
858 - if ( is_array( $options ) && array_key_exists( $name, $options ) ) {
859 - unset( $options[ $name ] );
860 - update_user_option( $id, $key, $options, true );
861 -
862 - return true;
863 - }
864 -
865 - return false;
866 -}*/
867 -
868 -/*
869 -function learn_press_get_user_option( $name, $id = 0 ) {
870 - if ( ! $id ) {
871 - $id = get_current_user_id();
872 - }
873 - $key = 'learnpress_user_options';
874 - $options = get_user_option( $key, $id );
875 - if ( is_array( $options ) && array_key_exists( $name, $options ) ) {
876 - return $options[ $name ];
877 - }
878 -
879 - return false;
880 -}*/
881 -
882 -/**
883 - * Update user basic information.
884 - *
885 - * @param bool $wp_error - Optional. Return WP_Error object in case updating failed.
886 - *
887 - * @return bool|mixed|WP_Error
888 - */
889 -function learn_press_update_user_profile_basic_information( $wp_error = false ) {
890 - $user_id = get_current_user_id();
891 -
892 - if ( ! $user_id ) {
893 - return new WP_Error( 2, 'The user is invalid' );
894 - }
895 -
896 - $update_data = array(
897 - 'ID' => $user_id,
898 - 'first_name' => LP_Request::get_param( 'first_name' ),
899 - 'last_name' => LP_Request::get_param( 'last_name' ),
900 - 'description' => LP_Request::get_param( 'description', '', 'html' ),
901 - 'display_name' => LP_Request::get_param( 'account_display_name' ),
902 - 'user_email' => LP_Request::get_param( 'account_email' ),
903 - );
904 -
905 - $update_data = apply_filters( 'learn-press/update-profile-basic-information-data', $update_data );
906 - $update_meta = LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register'] ?? '' );
907 -
908 - $return = LP_Forms_Handler::update_user_data( $update_data, $update_meta );
909 -
910 - // Update for social.
911 - $socials = LP_Request::get_param( 'user_profile_social' );
912 - $extra_data = get_user_meta( $user_id, '_lp_extra_info', true );
913 -
914 - if ( ! empty( $extra_data ) ) {
915 - $socials = array_merge( $extra_data, $socials );
916 - }
917 -
918 - update_user_meta( $user_id, '_lp_extra_info', $socials );
919 -
920 - if ( is_wp_error( $return ) ) {
921 - return $wp_error ? $return : false;
922 - }
923 -
924 - return $return;
925 -}
926 -
927 -/**
928 - * Update new password.
929 - *
930 - * @version 1.0.1
931 - */
932 -function learn_press_update_user_profile_change_password() {
933 - try {
934 - $user = wp_get_current_user();
935 - if ( ! $user ) {
936 - throw new Exception( 'error_lp_profile_update_pass', 'The user is invalid' );
937 - }
938 -
939 - $old_pass = LP_Request::get_param( 'pass0', '', 'text', 'post' );
940 - if ( empty( $old_pass ) ) {
941 - throw new Exception( __( 'Enter the old password!', 'learnpress' ) );
942 - }
943 -
944 - $check_old_pass = wp_check_password( $old_pass, $user->data->user_pass, $user->ID );
945 - if ( ! $check_old_pass ) {
946 - throw new Exception( __( 'The old password is incorrect!', 'learnpress' ) );
947 - }
948 -
949 - $new_pass = LP_Request::get_param( 'pass1', '', 'text', 'post' );
950 - $new_pass_confirm = LP_Request::get_param( 'pass2', '', 'text', 'post' );
951 - if ( empty( $new_pass ) || empty( $new_pass_confirm )
952 - || $new_pass != $new_pass_confirm ) {
953 - throw new Exception( __( 'Incorrect confirmation password!', 'learnpress' ) );
954 - }
955 -
956 - // Update user password
957 - // wp_set_password( $new_pass, $user->ID );
958 - $update_data = array(
959 - 'user_pass' => $new_pass,
960 - 'ID' => $user->ID,
961 - );
962 -
963 - $rs = wp_update_user( $update_data );
964 - if ( is_wp_error( $rs ) ) {
965 - throw new Exception( $rs->get_error_message() );
966 - }
967 - } catch ( Exception $ex ) {
968 - return new WP_Error( 'error_lp_profile_update_pass', $ex->getMessage() );
969 - }
970 -
971 - return true;
972 -}
973 -
974 -function learn_press_get_avatar_thumb_size() {
975 - $option = LP_Settings::get_option(
976 - 'avatar_dimensions',
977 - array(
978 - 'width' => 250,
979 - 'height' => 250,
980 - )
981 - );
982 -
983 - if ( ! isset( $option['width'] ) || ! isset( $option['height'] ) ) {
984 - $option = array(
985 - 'width' => 250,
986 - 'height' => 250,
987 - );
988 - }
989 -
990 - // For option get_avatar_url
991 - $option['size'] = $option['width'];
992 -
993 - return $option;
994 -}
995 -
996 -function learn_press_get_course_thumbnail_dimensions() {
997 - $option = LP_Settings::get_option(
998 - 'course_thumbnail_dimensions',
999 - array(
1000 - 'width' => 500,
1001 - 'height' => 300,
1002 - )
1003 - );
1004 -
1005 - if ( ! isset( $option['width'] ) || ! isset( $option['height'] ) ) {
1006 - $option = array(
1007 - 'width' => 500,
1008 - 'height' => 300,
1009 - );
1010 - }
1011 -
1012 - return $option;
1013 -}
1014 -
1015 -/*
1016 -function learn_press_get_user_avatar( $user_id = 0, $size = '' ) {
1017 - $user = learn_press_get_user( $user_id );
1018 -
1019 - return $user->get_profile_picture( '', $size );
1020 -}*/
1021 -
1022 -/**
1023 - * Get profile instance for an user to view.
1024 - *
1025 - * @param int $for_user
1026 - *
1027 - * @return LP_Profile|WP_Error
1028 - */
1029 -function learn_press_get_profile( $for_user = 0 ) {
1030 - return LP_Profile::instance( $for_user );
1031 -}
1032 -
1033 -/*
1034 -function learn_press_remove_user_items( $user_id, $item_id, $course_id, $include_course = false ) {
1035 - global $wpdb;
1036 -
1037 - settype( $item_id, 'array' );
1038 -
1039 - $format = array_fill( 0, sizeof( $item_id ), '%d' );
1040 - $where = '';
1041 -
1042 - $args = array( $user_id );
1043 - $args = array_merge( $args, $item_id );
1044 -
1045 - if ( $course_id ) {
1046 - $args[] = $course_id;
1047 - $where = 'AND ref_id = %d';
1048 - }
1049 -
1050 - if ( $include_course ) {
1051 - $where .= ' OR ( item_id = %d AND item_type = %s )';
1052 - $args[] = $course_id;
1053 - $args[] = LP_COURSE_CPT;
1054 - }
1055 -
1056 - $query = $wpdb->prepare(
1057 - "
1058 - DELETE
1059 - FROM {$wpdb->learnpress_user_items}
1060 - WHERE user_id = %d
1061 - AND ( item_id IN(" . join( ',', $format ) . ")
1062 - $where )
1063 - ",
1064 - $args
1065 - );
1066 -}*/
1067 -
1068 -/**
1069 - * Get user profile link
1070 - *
1071 - * @param int $user_id
1072 - * @param null $tab
1073 - *
1074 - * @return mixed|string
1075 - */
1076 -function learn_press_user_profile_link( $user_id = 0, $tab = '' ) {
1077 - if ( ! $user_id ) {
1078 - $user_id = get_current_user_id();
1079 - }
1080 -
1081 - $user = learn_press_get_user( $user_id );
1082 -
1083 - if ( ! $user ) {
1084 - return '';
1085 - }
1086 -
1087 - global $wp_query;
1088 - $wp_user = get_userdata( $user_id );
1089 - if ( ! $wp_user instanceof WP_User ) {
1090 - return '';
1091 - }
1092 -
1093 - $user_model = new UserModel( $wp_user->data );
1094 - $args = array(
1095 - 'user' => $user_model->get_slug_link(),
1096 - );
1097 -
1098 - if ( $tab ) {
1099 - $args['tab'] = $tab;
1100 - }
1101 -
1102 - /**
1103 - * If no tab is selected in profile and is current user
1104 - * then no need the username in profile link.
1105 - */
1106 - if ( ( $user_id == get_current_user_id() ) && ! isset( $args['tab'] ) ) {
1107 - //unset( $args['user'] );
1108 - }
1109 -
1110 - $profile_link = trailingslashit( learn_press_get_page_link( 'profile' ) );
1111 - if ( $profile_link ) {
1112 - if ( get_option( 'permalink_structure' ) ) {
1113 - $url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) );
1114 - } else {
1115 - $url = esc_url_raw( add_query_arg( $args, $profile_link ) );
1116 - }
1117 - } else {
1118 - $url = get_author_posts_url( $user_id );
1119 - }
1120 -
1121 - return apply_filters( 'learn_press_user_profile_link', $url, $user_id, $tab );
1122 -}
1123 -
1124 -/*
1125 -function learn_press_create_user_item( $args = array(), $wp_error = false ) {
1126 - global $wpdb;
1127 -
1128 - $defaults = array(
1129 - 'user_id' => get_current_user_id(),
1130 - 'item_id' => '',
1131 - 'start_time' => time(),
1132 - 'end_time' => '',
1133 - 'graduation' => '',
1134 - 'item_type' => '',
1135 - 'status' => '',
1136 - 'ref_id' => 0,
1137 - 'ref_type' => '',
1138 - 'parent_id' => 0,
1139 - 'create_meta' => array(),
1140 - );
1141 -
1142 - $item_data = wp_parse_args( $args, $defaults );
1143 -
1144 - // Validate item_id and post type
1145 - if ( empty( $item_data['item_id'] ) ) {
1146 - if ( $wp_error ) {
1147 - return new WP_Error( 'invalid_item_id', __( 'Invalid item id.', 'learnpress' ) );
1148 - }
1149 -
1150 - return 0;
1151 - }
1152 -
1153 - $post_type = learn_press_get_post_type( $item_data['item_id'] );
1154 - if ( empty( $item_data['item_type'] ) && $post_type ) {
1155 - $item_data['item_type'] = $post_type;
1156 - }
1157 -
1158 - // Get id and type of ref if they are null
1159 - if ( ! empty( $item_data['parent_id'] ) && ( empty( $item_data['ref_id'] ) || ( empty( $item_data['ref_type'] ) ) ) ) {
1160 - $parent = $wpdb->get_row(
1161 - $wpdb->prepare(
1162 - "SELECT * FROM {$wpdb->learnpress_user_items} WHERE %d",
1163 - $item_data['parent_id']
1164 - )
1165 - );
1166 -
1167 - if ( $parent ) {
1168 - if ( empty( $item_data['ref_id'] ) ) {
1169 - $item_data['ref_id'] = $parent->item_id;
1170 - }
1171 -
1172 - if ( empty( $item_data['ref_type'] ) ) {
1173 - $item_data['ref_type'] = $parent->item_type;
1174 - }
1175 - }
1176 - }
1177 -
1178 - // Filter
1179 - $item_data = apply_filters( 'learn-press/create-user-item-data', $item_data );
1180 - if ( ! $item_data ) {
1181 - if ( $wp_error ) {
1182 - return new WP_Error( 'invalid_item_data', __( 'Invalid item data.', 'learnpress' ) );
1183 - }
1184 -
1185 - return 0;
1186 - }
1187 -
1188 - do_action( 'learn-press/before-create-user-item', $item_data );
1189 -
1190 - $create_meta = ! empty( $item_data['create_meta'] ) ? $item_data['create_meta'] : false;
1191 -
1192 - if ( $create_meta ) {
1193 - unset( $item_data['create_meta'] );
1194 - }
1195 -
1196 - $user_item = new LP_User_Item( $item_data );
1197 - $result = $user_item->update();
1198 - if ( ! $result || is_wp_error( $result ) ) {
1199 - if ( $wp_error && is_wp_error( $result ) ) {
1200 - return $result;
1201 - }
1202 -
1203 - return 0;
1204 - }
1205 -
1206 - do_action( 'learn-press/created-user-item', $user_item, $item_data );
1207 -
1208 - $create_meta = apply_filters( 'learn-press/create-user-item-meta', $create_meta, $item_data );
1209 - if ( ! $create_meta ) {
1210 - return $user_item;
1211 - }
1212 -
1213 - do_action( 'learn-press/before-create-user-item-meta', $create_meta );
1214 -
1215 - foreach ( $create_meta as $key => $value ) {
1216 - learn_press_update_user_item_meta( $user_item->get_user_item_id(), $key, $value );
1217 - }
1218 -
1219 - do_action( 'learn-press/created-user-item-meta', $user_item, $create_meta );
1220 -
1221 - return $user_item;
1222 -}*/
1223 -
1224 -/*
1225 -function learn_press_isset_user_item_for_quiz( $quiz_id, $course_id ) {
1226 - global $wpdb;
1227 -
1228 - $query = $wpdb->prepare( "SELECT user_item_id FROM $wpdb->learnpress_user_items WHERE ref_id=%d AND item_id=%d", $course_id, $quiz_id );
1229 - $col = $wpdb->get_col( $query );
1230 -
1231 - if ( ! empty( $col ) ) {
1232 - return $col;
1233 - } else {
1234 - return false;
1235 - }
1236 -}*/
1237 -
1238 -/**
1239 - * Prepares list of questions for rest api.
1240 - *
1241 - * @param int[] $question_ids
1242 - * @param array $args
1243 - *
1244 - * @return array
1245 - * @since 3.3.0
1246 - */
1247 -function learn_press_rest_prepare_user_questions( array $question_ids = array(), array $args = array() ): array {
1248 - if ( is_numeric( $args ) ) {
1249 -
1250 - } else {
1251 - $args = wp_parse_args(
1252 - $args,
1253 - array(
1254 - 'instant_hint' => true,
1255 - 'instant_check' => true,
1256 - 'quiz_status' => '',
1257 - 'checked_questions' => array(),
1258 - 'hinted_questions' => array(),
1259 - 'answered' => array(),
1260 - 'show_correct_review' => true,
1261 - )
1262 - );
1263 - }
1264 -
1265 - $checkedQuestions = $args['checked_questions'];
1266 - $hintedQuestions = $args['hinted_questions'];
1267 - $instantHint = $args['instant_hint'];
1268 - $instantCheck = $args['instant_check'];
1269 - $quizStatus = $args['quiz_status'];
1270 - $answered = $args['answered'];
1271 - $status = $args['status'] ?? '';
1272 - $questions = array();
1273 -
1274 - if ( $question_ids ) {
1275 - foreach ( $question_ids as $id ) {
1276 - $question = learn_press_get_question( $id );
1277 - $hasHint = false;
1278 - $hasExplanation = false;
1279 - $canCheck = false;
1280 - $hinted = false;
1281 - $checked = false;
1282 - $theHint = $question->get_hint();
1283 - $theExplanation = '';
1284 -
1285 - if ( $instantCheck || $status == 'completed' ) {
1286 - $theExplanation = $question->get_explanation();
1287 - $checked = in_array( $id, $checkedQuestions );
1288 - $hasExplanation = (bool) $theExplanation;
1289 - }
1290 -
1291 - $mark = $question->get_mark() ? $question->get_mark() : 1;
1292 -
1293 - $questionData = array(
1294 - 'object' => $question,
1295 - 'id' => absint( $id ),
1296 - 'title' => $question->get_title(),
1297 - 'type' => $question->get_type(),
1298 - 'point' => $mark,
1299 - );
1300 -
1301 - $content = $question->get_content();
1302 - if ( $content ) {
1303 - $questionData['content'] = $content;
1304 - }
1305 -
1306 - if ( $theHint ) {
1307 - $questionData['hint'] = $theHint;
1308 - }
1309 -
1310 - if ( $status == 'completed' || ( $checked && $theExplanation ) ) {
1311 - $questionData['explanation'] = $theExplanation;
1312 - }
1313 -
1314 - if ( $hasExplanation ) {
1315 - $questionData['has_explanation'] = $hasExplanation;
1316 -
1317 - if ( $checked ) {
1318 - $questionData['explanation'] = $theExplanation;
1319 - }
1320 - }
1321 -
1322 - $with_true_or_false = ( $checked || ( $quizStatus == 'completed' && $args['show_correct_review'] ) );
1323 -
1324 - // if ( $question->is_support( 'answer-options' ) ) {
1325 - $questionData['options'] = learn_press_get_question_options_for_js(
1326 - $question,
1327 - array(
1328 - 'include_is_true' => $with_true_or_false,
1329 - 'answer' => $answered[ $id ]['answered'] ?? '',
1330 - )
1331 - );
1332 - // }
1333 -
1334 - $questions[] = $questionData;
1335 - }
1336 -
1337 - /**
1338 - * Remove answered
1339 - */
1340 - if ( $quizStatus !== 'completed' ) {
1341 - if ( $checkedQuestions && $quizStatus ) {
1342 -
1343 - $omitIds = array_diff( $question_ids, $checkedQuestions );
1344 -
1345 - if ( $omitIds ) {
1346 - foreach ( $omitIds as $omitId ) {
1347 - if ( ! empty( $answered[ $omitId ] ) ) {
1348 - unset( $answered[ $omitId ] );
1349 - }
1350 - }
1351 - }
1352 - }
1353 - }
1354 - }
1355 -
1356 - return apply_filters( 'learn-press/list-questions-data', $questions );
1357 -}
1358 -
1359 -/**
1360 - * Update extra profile data upon update user.
1361 - *
1362 - * @param int $user_id
1363 - *
1364 - * @since 4.0.0
1365 - */
1366 -function learn_press_update_extra_user_profile_fields( $user_id ) {
1367 - if ( ! current_user_can( 'edit_user', $user_id ) ) {
1368 - return;
1369 - }
1370 -
1371 - if ( array_key_exists( 'lp_user_slug', $_POST ) ) {
1372 - $userModel = UserModel::find( $user_id, true );
1373 - if ( ! $userModel ) {
1374 - return;
1375 - }
1376 -
1377 - $slug_result = $userModel->update_user_nicename( (string) $_POST['lp_user_slug'] );
1378 - if ( is_wp_error( $slug_result ) ) {
1379 - wp_safe_redirect(
1380 - add_query_arg(
1381 - 'lp-message',
1382 - urlencode( $slug_result->get_error_message() ),
1383 - wp_get_referer()
1384 - )
1385 - );
1386 - die();
1387 - }
1388 - }
1389 -
1390 - if ( isset( $_POST['_lp_extra_info'] ) ) {
1391 - $extra_info = LP_Request::get_param( '_lp_extra_info', array(), '', 'post' );
1392 - update_user_meta( $user_id, '_lp_extra_info', $extra_info );
1393 - }
1394 -}
1395 -
1396 -add_action( 'personal_options_update', 'learn_press_update_extra_user_profile_fields' );
1397 -add_action( 'edit_user_profile_update', 'learn_press_update_extra_user_profile_fields' );
1398 -
1399 -/**
1400 - * Get extra profile info data
1401 - *
1402 - * @param int $user_id
1403 - *
1404 - * @return array
1405 - * @since 4.0.1
1406 - */
1407 -function learn_press_get_user_extra_profile_info( $user_id = 0 ) {
1408 - if ( ! $user_id ) {
1409 - $user_id = get_current_user_id();
1410 - }
1411 -
1412 - $extra_profile_info = get_the_author_meta( '_lp_extra_info', $user_id );
1413 - $social_fields = learn_press_social_profiles();
1414 -
1415 - $user_socials = array();
1416 - foreach ( $social_fields as $key => $label ) {
1417 - $key = sanitize_key( $key );
1418 - $user_socials[ $key ] = '';
1419 - if ( is_array( $extra_profile_info ) && isset( $extra_profile_info[ $key ] ) ) {
1420 - $user_socials[ $key ] = esc_url_raw( $extra_profile_info[ $key ] );
1421 - }
1422 - }
1423 -
1424 - return apply_filters( 'learn-press/user-extra-profile-info', $user_socials, $user_id );
1425 -}
1426 -
1427 -/**
1428 - * @return array
1429 - * @since 3.x.x
1430 - * @version 4.0.2
1431 - */
1432 -function learn_press_social_profiles() {
1433 - return apply_filters(
1434 - 'learn-press/social-profiles',
1435 - array(
1436 - 'facebook' => learn_press_social_profile_name( 'facebook' ),
1437 - 'twitter' => learn_press_social_profile_name( 'twitter' ),
1438 - 'youtube' => learn_press_social_profile_name( 'youtube' ),
1439 - 'linkedin' => learn_press_social_profile_name( 'linkedin' ),
1440 - )
1441 - );
1442 -}
1443 -
1444 -function learn_press_social_profile_name( $key ) {
1445 - $name = '';
1446 - switch ( $key ) {
1447 - case 'facebook':
1448 - $name = esc_html__( 'Facebook Profile', 'learnpress' );
1449 - break;
1450 - case 'twitter':
1451 - $name = esc_html__( 'X Profile', 'learnpress' );
1452 - break;
1453 - case 'youtube':
1454 - $name = esc_html__( 'Youtube Channel', 'learnpress' );
1455 - break;
1456 - case 'linkedin':
1457 - $name = esc_html__( 'Linkedin Profile', 'learnpress' );
1458 - break;
1459 - default:
1460 - $name = ucfirst( $key );
1461 - }
1462 -
1463 - return apply_filters( 'learn-press/social-profile-name', $name, $key );
1464 -}
1465 -
1466 -function lp_add_default_fields( $fields ) {
1467 - $first_name = LP_Settings::get_option( 'enable_register_first_name', 'no' );
1468 -
1469 - if ( $first_name === 'yes' ) {
1470 - ?>
1471 - <li class="form-field">
1472 - <label for="reg_first_name"><?php esc_html_e( 'First name', 'learnpress' ); ?></label>
1473 - <input id="reg_first_name" name="reg_first_name" type="text"
1474 - placeholder="<?php esc_attr_e( 'First name', 'learnpress' ); ?>"
1475 - value="<?php echo esc_attr( wp_unslash( $_POST['reg_first_name'] ?? '' ) ); ?>">
1476 - </li>
1477 - <?php
1478 - }
1479 -
1480 - $last_name = LP_Settings::instance()->get( 'enable_register_last_name' );
1481 -
1482 - if ( $last_name === 'yes' ) {
1483 - ?>
1484 - <li class="form-field">
1485 - <label for="reg_last_name"><?php esc_html_e( 'Last name', 'learnpress' ); ?></label>
1486 - <input id="reg_last_name" name="reg_last_name" type="text"
1487 - placeholder="<?php esc_attr_e( 'Last name', 'learnpress' ); ?>"
1488 - value="<?php echo esc_attr( wp_unslash( $_POST['reg_last_name'] ?? '' ) ); ?>">
1489 - </li>
1490 - <?php
1491 - }
1492 -
1493 - $display_name = LP_Settings::instance()->get( 'enable_register_display_name' );
1494 -
1495 - if ( $display_name === 'yes' ) {
1496 - ?>
1497 - <li class="form-field">
1498 - <label for="reg_display_name"><?php esc_html_e( 'Display name', 'learnpress' ); ?></label>
1499 - <input id="reg_display_name" name="reg_display_name" type="text"
1500 - placeholder="<?php esc_attr_e( 'Display name', 'learnpress' ); ?>"
1501 - value="<?php echo esc_attr( wp_unslash( $_POST['reg_display_name'] ?? '' ) ); ?>">
1502 - </li>
1503 - <?php
1504 - }
1505 -}
1506 -
1507 -add_filter( 'learn-press/after-form-register-fields', 'lp_add_default_fields' );
1508 -
1509 -function lp_custom_register_fields_display() {
1510 - ?>
1511 - <?php $custom_fields = LP_Profile::get_register_fields_custom(); ?>
1512 -
1513 - <?php if ( $custom_fields ) : ?>
1514 - <?php foreach ( $custom_fields as $custom_field ) : ?>
1515 - <?php
1516 - $cf_class = '';
1517 - if ( $custom_field['required'] == 'yes' ) {
1518 - $cf_class = ' required';
1519 - ?>
1520 - <style>
1521 - .required label {
1522 - font-weight: bold;
1523 - }
1524 -
1525 - .required label:after {
1526 - content: ' *';
1527 - display: inline;
1528 - }
1529 - </style>
1530 - <?php
1531 - }
1532 -
1533 - if ( isset( $custom_field['id'] ) ) {
1534 - ?>
1535 - <?php $value = $custom_field['id']; ?>
1536 -
1537 - <li class="form-field<?php echo esc_attr( $cf_class ); ?>">
1538 - <?php
1539 - switch ( $custom_field['type'] ) {
1540 - case 'text':
1541 - case 'number':
1542 - case 'email':
1543 - case 'url':
1544 - case 'tel':
1545 - ?>
1546 - <label for="description"><?php echo esc_html( $custom_field['name'] ); ?></label>
1547 - <input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"
1548 - type="<?php echo esc_attr( $custom_field['type'] ); ?>" class="regular-text"
1549 - value=""/>
1550 - <?php
1551 - break;
1552 - case 'textarea':
1553 - ?>
1554 - <label for="description"><?php echo esc_html( $custom_field['name'] ); ?></label>
1555 - <textarea name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"></textarea>
1556 - <?php
1557 - break;
1558 - case 'checkbox':
1559 - ?>
1560 - <label>
1561 - <input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"
1562 - type="<?php echo esc_attr( $custom_field['type'] ); ?>" value="1">
1563 - <?php echo esc_html( $custom_field['name'] ); ?>
1564 - </label>
1565 - <?php
1566 - break;
1567 - }
1568 - ?>
1569 - </li>
1570 - <?php } ?>
1571 - <?php endforeach; ?>
1572 - <?php endif; ?>
1573 - <?php
1574 -}
1575 -
1576 -add_action( 'learn-press/after-form-register-fields', 'lp_custom_register_fields_display' );
1577 -
1578 -/**
1579 - * Custom register fields
1580 - *
1581 - * @param [type] $user_id
1582 - *
1583 - * @return void
1584 - */
1585 -function lp_user_custom_register_fields( $user_id, $fields = array() ) {
1586 - if ( ! empty( $fields ) ) {
1587 - update_user_meta( $user_id, '_lp_custom_register', LP_Helper::sanitize_params_submitted( $fields ) );
1588 - } elseif ( isset( $_POST['_lp_custom_register'] ) ) {
1589 - update_user_meta( $user_id, '_lp_custom_register', LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register'] ) );
1590 - }
1591 -}
1592 -
1593 -add_action( 'personal_options_update', 'lp_user_custom_register_fields' );
1594 -add_action( 'edit_user_profile_update', 'lp_user_custom_register_fields' );
1595 -
1596 -function lp_get_user_custom_register_fields( $user_id = 0 ) {
1597 - if ( ! $user_id ) {
1598 - $user_id = get_current_user_id();
1599 - }
1600 -
1601 - $register_fields = get_the_author_meta( '_lp_custom_register', $user_id );
1602 - $defaults = lp_get_user_custom_fields();
1603 -
1604 - $extra_profile_info = wp_parse_args( $register_fields, $defaults );
1605 -
1606 - return apply_filters( 'lp/user-custom-register-fields', $register_fields, $user_id );
1607 -}
1608 -
1609 -function lp_get_user_custom_fields() {
1610 - $custom_fields = LP_Settings::get_option( 'register_profile_fields', array() );
1611 -
1612 - $output = array();
1613 -
1614 - if ( $custom_fields ) {
1615 - foreach ( $custom_fields as $field ) {
1616 - $output[ $field['id'] ] = '';
1617 - }
1618 - }
1619 -
1620 - return $output;
1621 -}
1622 -
1623 -/**
1624 - * Check extra user data is a social profile.
1625 - *
1626 - * @param $key
1627 - *
1628 - * @return bool
1629 - * @since 4.0.0
1630 - * @deprecated 4.3.2
1631 - */
1632 -function learn_press_is_social_profile( $key ) {
1633 - _deprecated_function( __FUNCTION__, '4.3.2' );
1634 -
1635 - return true;
1636 -
1637 - $is_socials = learn_press_social_profiles();
1638 -
1639 - return in_array( $key, $is_socials );
1640 -}
1641 -
1642 -/**
1643 - * Get extra profile fields will be registered in backend profile.
1644 - *
1645 - * @return array
1646 - * @since 4.0.0
1647 - * @deprecated 4.3.2
1648 - */
1649 -function learn_press_get_user_extra_profile_fields() {
1650 - _deprecated_function( __FUNCTION__, '4.3.2' );
1651 - return array();
1652 -
1653 - $socials = learn_press_social_profiles();
1654 -
1655 - return $socials;
1656 -
1657 - $fields = array();
1658 -
1659 - foreach ( $socials as $social ) {
1660 - $fields[ $social ] = learn_press_social_profile_name( $social );
1661 - }
1662 -
1663 - return apply_filters( 'learn-press/user-extra-profile-fields', $fields );
1664 -}
1665 -
1666 -/**
1667 - * Show courses user enrolled on backend
1668 - *
1669 - * @param $user
1670 - *
1671 - * @return void
1672 - */
1673 -function learn_press_user_profile_data( $user ) {
1674 - if ( ! is_admin() ) {
1675 - return;
1676 - }
1677 -
1678 - learn_press_admin_view( 'backend-user-profile', array( 'user' => $user ) );
1679 - learn_press_admin_view( 'user/courses.php', array( 'user_id' => $user->ID ) );
1680 -}
1681 -
1682 -add_action( 'edit_user_profile', 'learn_press_user_profile_data', 1000 );
1683 -add_action( 'show_user_profile', 'learn_press_user_profile_data', 1000 );
1684 -
1685 -/*
1686 -function learnpress_get_count_by_user( $user_id = '', $post_type = 'lp_course' ) {
1687 - if ( empty( $user_id ) ) {
1688 - return false;
1689 - }
1690 -
1691 - $args = array(
1692 - 'author' => $user_id,
1693 - 'posts_per_page' => - 1,
1694 - 'post_type' => $post_type,
1695 - 'post_status' => 'any',
1696 - );
1697 -
1698 - $posts = get_posts( $args );
1699 -
1700 - $output = array(
1701 - 'all' => count( $posts ),
1702 - 'publish' => array(),
1703 - 'pending' => array(),
1704 - );
1705 -
1706 - $pending = $public = array();
1707 -
1708 - if ( ! empty( $posts ) ) {
1709 - foreach ( $posts as $post ) {
1710 - switch ( $post->post_status ) {
1711 - case 'pending':
1712 - $pending[] = $post;
1713 - break;
1714 - case 'publish':
1715 - $public[] = $post;
1716 - break;
1717 - default:
1718 - break;
1719 - }
1720 - }
1721 - }
1722 -
1723 - return array(
1724 - 'all' => count( $posts ),
1725 - 'publish' => count( $public ),
1726 - 'pending' => count( $pending ),
1727 - );
1728 -}*/
1 +<?php
2 +
3 +use LearnPress\Models\UserItems\UserItemModel;
4 +use LearnPress\Models\UserModel;
5 +use LearnPress\Services\UserService;
6 +
7 +/**
8 + * Common functions to process actions about user
9 + *
10 + * @author ThimPress
11 + * @package LearnPress/Functions/User
12 + * @version 1.0
13 + */
14 +function learn_press_get_user_profile_tabs() {
15 + return LP_Profile::instance()->get_tabs();
16 +}
17 +
18 +/**
19 + * Ensure new users receive a public slug.
20 + *
21 + * @param int $user_id
22 + *
23 + * @return void
24 + */
25 +function learn_press_maybe_generate_user_pretty_slug_on_register( int $user_id ) {
26 + $userModel = UserModel::find( $user_id, true );
27 + if ( ! $userModel ) {
28 + return;
29 + }
30 +
31 + $userModel->generate_pretty_slug();
32 +}
33 +
34 +//add_action( 'user_register', 'learn_press_maybe_generate_user_pretty_slug_on_register' );
35 +
36 +/**
37 + * Delete user data by user ID
38 + *
39 + * @param int $user_id
40 + * @param int $course_id
41 + */
42 +function learn_press_delete_user_data( $user_id, $course_id = 0 ) {
43 + global $wpdb;
44 + // TODO: Should be deleted user's order and order data???
45 +
46 + $query_args = array( $user_id );
47 +
48 + if ( $course_id ) {
49 + $query_args[] = $course_id;
50 + }
51 +
52 + $query = $wpdb->prepare(
53 + "
54 + SELECT user_item_id
55 + FROM {$wpdb->prefix}learnpress_user_items
56 + WHERE user_id = %d
57 + " . ( $course_id ? ' AND item_id = %d' : '' ) . '
58 + ',
59 + $query_args
60 + );
61 +
62 + // delete all courses user has enrolled
63 + $query = $wpdb->prepare(
64 + "
65 + DELETE FROM {$wpdb->prefix}learnpress_user_items
66 + WHERE user_id = %d
67 + " . ( $course_id ? ' AND item_id = %d' : '' ) . '
68 + ',
69 + $query_args
70 + );
71 +
72 + @$wpdb->query( $query );
73 +}
74 +
75 +/**
76 + * Get current user ID
77 + *
78 + * @return int|string LP_User is int, LP_User_Guest is string
79 + */
80 +function learn_press_get_current_user_id() {
81 + $user = learn_press_get_current_user();
82 +
83 + if ( ! $user ) {
84 + return 0;
85 + }
86 +
87 + return $user->get_id();
88 +}
89 +
90 +/**
91 + * Get the user by $user_id passed. If $user_id is NULL, get current user.
92 + * If current user is not logged in, return a GUEST user
93 + *
94 + * @param bool $create_temp - Optional. Create temp user if user is not logged in.
95 + *
96 + * @return LP_User|LP_User_Guest
97 + * @editor tungnx
98 + * @modify 4.1.4
99 + * @version 1.0.1
100 + */
101 +function learn_press_get_current_user( $create_temp = true ) {
102 + $user_id = get_current_user_id();
103 +
104 + if ( $user_id ) {
105 + return learn_press_get_user( $user_id );
106 + }
107 +
108 + // Return LP_User_Guest
109 + return learn_press_get_user( 0 );
110 +}
111 +
112 +if ( ! function_exists( 'learn_press_get_user' ) ) {
113 + /**
114 + * Get user by ID. Return false if the user does not exist.
115 + * If user_id = 0, return a guest user.
116 + *
117 + * @param int $user_id
118 + * @param bool $current
119 + *
120 + * @return LP_User|LP_User_Guest|false
121 + * @since 3.0.0
122 + * @version 4.0.1
123 + */
124 + function learn_press_get_user( $user_id = 0, $current = false, $force_new = false ) {
125 + $userClass = $user_id && get_user_by( 'ID', $user_id ) ? 'LP_User' : ( $user_id == 0 ? 'LP_User_Guest' : false );
126 + if ( false === $userClass ) {
127 + return false;
128 + }
129 +
130 + return new $userClass( $user_id );
131 + }
132 +}
133 +
134 +/**
135 + * Add roles and add capabilities for roles
136 + */
137 +function learn_press_add_user_roles() {
138 + $item_types = [
139 + LP_COURSE_CPT,
140 + LP_LESSON_CPT,
141 + LP_ORDER_CPT,
142 + ];
143 +
144 + UserService::instance()->add_capabilities_for_roles();
145 +
146 + foreach ( $item_types as $item_type ) {
147 + UserService::instance()->add_capabilities_for_roles( $item_type );
148 + }
149 +}
150 +
151 +/**
152 + * Get queried user in profile link
153 + *
154 + * @return false|WP_User
155 + * @since 3.0.0
156 + * @deprecated 4.2.9.1
157 + */
158 +function learn_press_get_profile_user() {
159 + return LP_Profile::instance()->get_user_current();
160 +
161 + return LP_Profile::get_queried_user();
162 +}
163 +
164 +
165 +/**
166 + * Add instructor registration button to register page and admin bar
167 + */
168 +function learn_press_user_become_teacher_registration_form() {
169 + if ( LP_Settings::instance()->get( 'instructor_registration' ) != 'yes' ) {
170 + return;
171 + }
172 + ?>
173 + <p>
174 + <label for="become_teacher">
175 + <input type="checkbox" name="become_teacher" id="become_teacher">
176 + <?php esc_html_e( 'Want to become an instructor?', 'learnpress' ); ?>
177 + </label>
178 + </p>
179 + <?php
180 +}
181 +
182 +add_action( 'register_form', 'learn_press_user_become_teacher_registration_form' );
183 +
184 +/**
185 + * Update data into table learnpress_user_items.
186 + *
187 + * @param array $fields - Fields and values to be updated.
188 + * Format: array(
189 + * field_name_1 => value 1,
190 + * field_name_2 => value 2,
191 + * ....
192 + * field_name_n => value n
193 + * )
194 + * @param mixed $where - Optional. Fields with values for conditional update with the same format of $fields.
195 + * @param bool $update_cache - Optional. Should be update to cache or not (since 3.0.0).
196 + * @param bool $update_extra_fields_as_meta - Optional. Update extra fields as item meta (since 3.1.0).
197 + *
198 + * @return mixed
199 + */
200 +function learn_press_update_user_item_field( array $fields = array(), $where = false, $update_cache = true, $update_extra_fields_as_meta = false ) {
201 + global $wpdb;
202 +
203 + // Table fields format.
204 + $table_fields = array(
205 + 'user_item_id' => '%d',
206 + 'user_id' => '%d',
207 + 'item_id' => '%d',
208 + 'ref_id' => '%d',
209 + 'start_time' => '%s',
210 + 'end_time' => '%s',
211 + 'access_level' => '%d',
212 + 'graduation' => '%s',
213 + 'item_type' => '%s',
214 + 'status' => '%s',
215 + 'ref_type' => '%s',
216 + 'parent_id' => '%d',
217 + );
218 +
219 + /**
220 + * Validate item status
221 + */
222 + if ( ! empty( $fields['item_id'] ) && ! empty( $fields['status'] ) ) {
223 + $item_type = learn_press_get_post_type( $fields['item_id'] );
224 +
225 + if ( LP_COURSE_CPT === $item_type ) {
226 + if ( 'completed' === $fields['status'] ) {
227 + $fields['status'] = 'finished';
228 + }
229 + } elseif ( 'finished' === $fields['status'] ) {
230 + $fields['status'] = 'completed';
231 + }
232 + }
233 +
234 + $data = array();
235 + $data_format = array();
236 + foreach ( $fields as $field => $value ) {
237 + if ( ! empty( $table_fields[ $field ] ) ) {
238 + $data[ $field ] = $value;
239 +
240 + // Do not format the date-time field if it's value is NULL
241 + if ( in_array( $field, array( 'start_time', 'end_time' ) ) && empty( $value ) ) {
242 + $data[ $field ] = null;
243 + $data_format[] = '';
244 + } else {
245 + if ( $value instanceof LP_Datetime ) {
246 + $data[ $field ] = $value->toSql();
247 + }
248 + $data_format[] = $table_fields[ $field ];
249 + }
250 + }
251 + }
252 +
253 + if ( ! empty( $fields['user_item_id'] ) ) {
254 + $where = wp_parse_args(
255 + $where,
256 + array( 'user_item_id' => $fields['user_item_id'] )
257 + );
258 + }
259 +
260 + // Set where and where format
261 + $where_format = array();
262 + if ( is_array( $where ) && ! empty( $where ) ) {
263 + if ( empty( $where['user_id'] ) ) {
264 + $where['user_id'] = ! empty( $fields['user_id'] ) ? $fields['user_id'] : learn_press_get_current_user_id();
265 + }
266 +
267 + foreach ( $where as $field => $value ) {
268 + if ( isset( $table_fields[ $field ] ) ) {
269 + $where_format[] = $table_fields[ $field ];
270 + }
271 + }
272 + }
273 +
274 + if ( ! $data ) {
275 + return false;
276 + }
277 +
278 + $inserted = false;
279 + $updated = false;
280 +
281 + // Update to database.
282 + if ( $where ) {
283 + $updated = $wpdb->update(
284 + $wpdb->learnpress_user_items,
285 + $data,
286 + $where,
287 + $data_format,
288 + $where_format
289 + );
290 + } else {
291 + // Insert to database.
292 + if ( $wpdb->insert(
293 + $wpdb->learnpress_user_items,
294 + $data,
295 + $data_format
296 + ) ) {
297 + $inserted = $wpdb->insert_id;
298 + }
299 + }
300 +
301 + if ( $updated && ! empty( $where['user_item_id'] ) ) {
302 + $inserted = $where['user_item_id'];
303 + }
304 +
305 + /**
306 + * @var object|bool $updated_item
307 + */
308 + $updated_item = false;
309 +
310 + // Get the item we just have updated or inserted.
311 + if ( $inserted ) {
312 + $updated_item = learn_press_get_user_item( $inserted );
313 + } elseif ( $updated ) {
314 + $updated_item = learn_press_get_user_item( $where );
315 + }
316 +
317 + // Clear caches.
318 + if ( $updated_item ) {
319 + // Clear cache total students enrolled.
320 + if ( isset( $updated_item->item_type )
321 + && LP_COURSE_CPT === $updated_item->item_type
322 + && isset( $updated_item->item_id ) ) {
323 + $lp_course_cache = new LP_Course_Cache( true );
324 + $lp_course_cache->clean_total_students_enrolled( $updated_item->item_id );
325 + $lp_course_cache->clean_total_students_enrolled_or_purchased( $updated_item->item_id );
326 + // Clear cache count students many courses.
327 + $lp_courses_cache = new LP_Courses_Cache( true );
328 + $lp_courses_cache->clear_cache_on_group( LP_Courses_Cache::KEYS_COUNT_STUDENT_COURSES );
329 + }
330 + // Clear cache user item.
331 + $lp_user_items_cache = new LP_User_Items_Cache();
332 + $lp_user_items_cache->clean_user_item(
333 + array(
334 + $updated_item->user_id,
335 + $updated_item->item_id,
336 + $updated_item->item_type,
337 + )
338 + );
339 + // Clear cache userItemModel
340 + $userItemModel = UserItemModel::find_user_item(
341 + $updated_item->user_id,
342 + $updated_item->item_id,
343 + $updated_item->item_type,
344 + $updated_item->ref_id,
345 + $updated_item->ref_type,
346 + true
347 + );
348 + $userItemModel->clean_caches();
349 + }
350 +
351 + do_action( 'learn-press/updated-user-item-field', $updated_item );
352 +
353 + /**
354 + * If there is some fields does not contain in the main table
355 + * then consider update them as metadata.
356 + *
357 + * @comment by tungnx - 4.1.7.3
358 + */
359 + /*
360 + if ( $updated_item && $update_extra_fields_as_meta ) {
361 + $extra_fields = array_diff_key( $fields, $table_fields );
362 + if ( $extra_fields ) {
363 + foreach ( $extra_fields as $meta_key => $meta_value ) {
364 + if ( $meta_value == 'user_item_id' ) {
365 + continue;
366 + }
367 +
368 + if ( $meta_value === false ) {
369 + learn_press_delete_user_item_meta( $updated_item->user_item_id, $meta_key );
370 + } else {
371 +
372 + if ( empty( $meta_value ) ) {
373 + $meta_value = '';
374 + }
375 + learn_press_update_user_item_meta( $updated_item->user_item_id, $meta_key, $meta_value );
376 + }
377 + }
378 + }
379 + }*/
380 +
381 + // do_action( 'learn-press/updated-user-item-meta', $updated_item );
382 +
383 + return $updated_item;
384 +}
385 +
386 +/**
387 + * Get user item row(s) from user items table by multiple WHERE conditional
388 + *
389 + * @param array|int $where
390 + * @param bool $single
391 + *
392 + * @return array
393 + */
394 +function learn_press_get_user_item( $where, $single = true ) {
395 + global $wpdb;
396 +
397 + // Table fields
398 + $table_fields = array(
399 + 'user_item_id' => '%d',
400 + 'user_id' => '%d',
401 + 'item_id' => '%d',
402 + 'ref_id' => '%d',
403 + 'start_time' => '%s',
404 + 'end_time' => '%s',
405 + 'item_type' => '%s',
406 + 'status' => '%s',
407 + 'ref_type' => '%s',
408 + 'parent_id' => '%d',
409 + );
410 +
411 + // If $where is a number consider we are searching the record with unique user_item_id
412 + if ( is_numeric( $where ) ) {
413 + $where = array( 'user_item_id' => $where );
414 + }
415 +
416 + $where_str = array();
417 + foreach ( $where as $field => $value ) {
418 + if ( ! empty( $table_fields[ $field ] ) ) {
419 + $where_str[] = "{$field} = " . $table_fields[ $field ];
420 + }
421 + }
422 + $item = false;
423 +
424 + if ( $where_str ) {
425 + $query = $wpdb->prepare(
426 + "
427 + SELECT *
428 + FROM {$wpdb->prefix}learnpress_user_items
429 + WHERE " . join( ' AND ', $where_str ) . '
430 + ORDER BY user_item_id DESC
431 + ',
432 + $where
433 + );
434 + if ( $single || ! empty( $where['user_item_id'] ) ) {
435 + $item = $wpdb->get_row( $query );
436 + } else {
437 + $item = $wpdb->get_results( $query );
438 + }
439 + }
440 +
441 + return $item;
442 +}
443 +
444 +/**
445 + * Get user item meta from user_itemmeta table
446 + *
447 + * @param int $user_item_id .
448 + * @param string $meta_key .
449 + * @param bool $single .
450 + *
451 + * @return mixed
452 + */
453 +function learn_press_get_user_item_meta( $user_item_id = 0, $meta_key = '', $single = true ) {
454 + $meta = false;
455 + if ( metadata_exists( 'learnpress_user_item', $user_item_id, $meta_key ) ) {
456 + $meta = get_metadata( 'learnpress_user_item', $user_item_id, $meta_key, $single );
457 + }
458 +
459 + return $meta;
460 +}
461 +
462 +/**
463 + * Add user item meta into table user_itemmeta
464 + *
465 + * @param int $user_item_id
466 + * @param string $meta_key
467 + * @param mixed $meta_value
468 + * @param string $prev_value
469 + *
470 + * @return false|int
471 + */
472 +function learn_press_add_user_item_meta( $user_item_id, $meta_key, $meta_value, $prev_value = '' ) {
473 + return add_metadata( 'learnpress_user_item', $user_item_id, $meta_key, $meta_value, $prev_value );
474 +}
475 +
476 +/**
477 + * Update user item meta to table user_itemmeta
478 + *
479 + * @param int $user_item_id
480 + * @param string $meta_key
481 + * @param mixed $meta_value
482 + * @param string $prev_value
483 + *
484 + * @return bool|int
485 + */
486 +function learn_press_update_user_item_meta( $user_item_id, $meta_key, $meta_value, $prev_value = '' ) {
487 + return update_metadata( 'learnpress_user_item', $user_item_id, $meta_key, $meta_value, $prev_value );
488 +}
489 +
490 +
491 +/**
492 + * Update user item meta to table user_itemmeta
493 + *
494 + * @param int $object_id
495 + * @param string $meta_key
496 + * @param mixed $meta_value
497 + * @param bool $delete_all
498 + *
499 + * @return bool|int
500 + */
501 +function learn_press_delete_user_item_meta( $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
502 + return delete_metadata( 'learnpress_user_item', $object_id, $meta_key, $meta_value, $delete_all );
503 +}
504 +
505 +if ( ! function_exists( 'learn_press_pre_get_avatar_callback' ) ) {
506 + /**
507 + * Filter the avatar
508 + *
509 + * @param string $avatar
510 + * @param string $id_or_email
511 + * @param array $args
512 + *
513 + * @return string
514 + * @since 1.0.0
515 + * @version 1.0.2
516 + */
517 + function learn_press_pre_get_avatar_callback( $avatar, $id_or_email = '', $args = array() ) {
518 + try {
519 + if ( ( isset( $args['gravatar'] ) && $args['gravatar'] ) || ( $args['default'] && $args['force_default'] ) ) {
520 + return $avatar;
521 + }
522 +
523 + $user_id = 0;
524 +
525 + /**
526 + * Get the ID of user from $id_or_email
527 + */
528 + if ( ! is_numeric( $id_or_email ) && is_string( $id_or_email ) ) {
529 + $user = get_user_by( 'email', $id_or_email );
530 + if ( $user ) {
531 + $user_id = $user->ID;
532 + }
533 + } elseif ( is_numeric( $id_or_email ) ) {
534 + $user_id = $id_or_email;
535 + } elseif ( is_object( $id_or_email ) && isset( $id_or_email->user_id ) && $id_or_email->user_id ) {
536 + $user_id = $id_or_email->user_id;
537 + } elseif ( $id_or_email instanceof WP_Comment ) {
538 + $user = get_user_by( 'email', $id_or_email->comment_author_email );
539 + if ( $user ) {
540 + $user_id = $user->ID;
541 + }
542 + }
543 +
544 + if ( ! $user_id ) {
545 + return $avatar;
546 + }
547 +
548 + $user = UserModel::find( $user_id, true );
549 + if ( ! $user ) {
550 + return $avatar;
551 + }
552 +
553 + $class = array( 'avatar', 'avatar-' . (int) $args['size'], 'photo' );
554 + if ( $args['class'] ) {
555 + if ( is_array( $args['class'] ) ) {
556 + $class = array_merge( $class, $args['class'] );
557 + } else {
558 + $class[] = $args['class'];
559 + }
560 + }
561 +
562 + $avatar_url = $user->get_avatar_url();
563 + $html_img = sprintf(
564 + "<img src='%s' srcset='%s' class='%s' height='%d' width='%d' decoding='async'/>",
565 + esc_url( $avatar_url ),
566 + esc_url( $avatar_url ) . ' 2x',
567 + esc_attr( implode( ' ', $class ) ),
568 + (int) $args['height'],
569 + (int) $args['width']
570 + );
571 +
572 + return $html_img;
573 + } catch ( Throwable $e ) {
574 + error_log( $e->getMessage() );
575 +
576 + return $avatar;
577 + }
578 + }
579 +}
580 +add_filter( 'pre_get_avatar', 'learn_press_pre_get_avatar_callback', 999, 3 );
581 +
582 +
583 +function learn_press_user_profile_picture_upload_dir( $width_user = true ) {
584 + $upload_dir = wp_upload_dir();
585 + $subdir = apply_filters( 'learn_press_user_profile_folder', 'learn-press-profile', $width_user );
586 + if ( $width_user ) {
587 + $subdir .= '/' . get_current_user_id();
588 + }
589 + $subdir = '/' . $subdir;
590 +
591 + if ( ! empty( $upload_dir['subdir'] ) ) {
592 + $u_subdir = str_replace( '\\', '/', $upload_dir['subdir'] );
593 + $u_path = str_replace( '\\', '/', $upload_dir['path'] );
594 +
595 + $upload_dir['path'] = str_replace( $u_subdir, $subdir, $u_path );
596 + $upload_dir['url'] = str_replace( $u_subdir, $subdir, $upload_dir['url'] );
597 + } else {
598 + $upload_dir['path'] = $upload_dir['path'] . $subdir;
599 + $upload_dir['url'] = $upload_dir['url'] . $subdir;
600 + }
601 +
602 + $upload_dir['subdir'] = $subdir;
603 +
604 + // Point path/url to main site if we are in multisite
605 + if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) {
606 + foreach ( array( 'path', 'url', 'basedir', 'baseurl' ) as $v ) {
607 + $upload_dir[ $v ] = str_replace( '/sites/' . get_current_blog_id(), '', $upload_dir[ $v ] );
608 + }
609 + }
610 +
611 + return $upload_dir;
612 +}
613 +
614 +/*
615 +add_action( 'learn_press_before_purchase_course_handler', '_learn_press_before_purchase_course_handler', 10, 2 );
616 +function _learn_press_before_purchase_course_handler( $course_id, $cart ) {
617 + // Redirect to login page if user is not logged in
618 + if ( ! is_user_logged_in() ) {
619 + $return_url = esc_url_raw( add_query_arg( $_POST, get_the_permalink( $course_id ) ) );
620 + $return_url = apply_filters( 'learn_press_purchase_course_login_redirect_return_url', $return_url );
621 + $redirect = apply_filters(
622 + 'learn_press_purchase_course_login_redirect',
623 + learn_press_get_login_url( $return_url )
624 + );
625 + if ( $redirect !== false ) {
626 + learn_press_add_message( __( 'Please log in to enroll in this course', 'learnpress' ) );
627 +
628 + if ( learn_press_is_ajax() ) {
629 + learn_press_send_json(
630 + array(
631 + 'redirect' => $redirect,
632 + 'result' => 'success',
633 + )
634 + );
635 + } else {
636 + wp_redirect( $redirect );
637 + exit();
638 + }
639 + }
640 + } else {
641 + $user = learn_press_get_current_user();
642 + $redirect = false;
643 + if ( $user->has_finished_course( $course_id ) ) {
644 + learn_press_add_message( __( 'You have already finished the course', 'learnpress' ) );
645 + $redirect = true;
646 + } elseif ( $user->has_purchased_course( $course_id ) ) {
647 + learn_press_add_message( __( 'You have already enrolled in this course', 'learnpress' ) );
648 + $redirect = true;
649 + }
650 + if ( $redirect ) {
651 + wp_redirect( get_the_permalink( $course_id ) );
652 + exit();
653 + }
654 + }
655 +}*/
656 +
657 +/*
658 +function learn_press_user_is( $role, $user_id = 0 ) {
659 + if ( ! $user_id ) {
660 + $user = learn_press_get_current_user();
661 + } else {
662 + $user = learn_press_get_user( $user_id );
663 + }
664 + if ( $role == 'admin' ) {
665 + return $user->is_admin();
666 + }
667 + if ( $role == 'instructor' ) {
668 + return $user->is_instructor();
669 + }
670 +
671 + return $role;
672 +}*/
673 +
674 +/*
675 +function learn_press_profile_tab_edit_content( $current, $tab, $user ) {
676 + learn_press_get_template(
677 + 'profile/tabs/edit.php',
678 + array(
679 + 'user' => $user,
680 + 'current' => $current,
681 + 'tab' => $tab,
682 + )
683 + );
684 +}*/
685 +
686 +/*
687 +function learn_press_update_user_option( $name, $value, $id = 0 ) {
688 + if ( ! $id ) {
689 + $id = get_current_user_id();
690 + }
691 + $key = 'learnpress_user_options';
692 + $options = get_user_option( $key, $id );
693 + $options[ $name ] = $value;
694 + update_user_option( $id, $key, $options, true );
695 +}*/
696 +
697 +/*
698 +function learn_press_delete_user_option( $name, $id = 0 ) {
699 + if ( ! $id ) {
700 + $id = get_current_user_id();
701 + }
702 + $key = 'learnpress_user_options';
703 + $options = get_user_option( $key, $id );
704 + if ( is_array( $options ) && array_key_exists( $name, $options ) ) {
705 + unset( $options[ $name ] );
706 + update_user_option( $id, $key, $options, true );
707 +
708 + return true;
709 + }
710 +
711 + return false;
712 +}*/
713 +
714 +/*
715 +function learn_press_get_user_option( $name, $id = 0 ) {
716 + if ( ! $id ) {
717 + $id = get_current_user_id();
718 + }
719 + $key = 'learnpress_user_options';
720 + $options = get_user_option( $key, $id );
721 + if ( is_array( $options ) && array_key_exists( $name, $options ) ) {
722 + return $options[ $name ];
723 + }
724 +
725 + return false;
726 +}*/
727 +
728 +/**
729 + * Update user basic information.
730 + *
731 + * @param bool $wp_error - Optional. Return WP_Error object in case updating failed.
732 + *
733 + * @return bool|mixed|WP_Error
734 + */
735 +function learn_press_update_user_profile_basic_information( $wp_error = false ) {
736 + $user_id = get_current_user_id();
737 +
738 + if ( ! $user_id ) {
739 + return new WP_Error( 2, 'The user is invalid' );
740 + }
741 +
742 + $update_data = array(
743 + 'ID' => $user_id,
744 + 'first_name' => LP_Request::get_param( 'first_name' ),
745 + 'last_name' => LP_Request::get_param( 'last_name' ),
746 + 'description' => LP_Request::get_param( 'description', '', 'html' ),
747 + 'display_name' => LP_Request::get_param( 'account_display_name' ),
748 + 'user_email' => LP_Request::get_param( 'account_email' ),
749 + );
750 +
751 + $update_data = apply_filters( 'learn-press/update-profile-basic-information-data', $update_data );
752 + $update_meta = LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register'] ?? '' );
753 +
754 + $return = LP_Forms_Handler::update_user_data( $update_data, $update_meta );
755 +
756 + // Update for social.
757 + $socials = LP_Request::get_param( 'user_profile_social' );
758 + $extra_data = get_user_meta( $user_id, '_lp_extra_info', true );
759 +
760 + if ( ! empty( $extra_data ) ) {
761 + $socials = array_merge( $extra_data, $socials );
762 + }
763 +
764 + update_user_meta( $user_id, '_lp_extra_info', $socials );
765 +
766 + if ( is_wp_error( $return ) ) {
767 + return $wp_error ? $return : false;
768 + }
769 +
770 + return $return;
771 +}
772 +
773 +/**
774 + * Update new password.
775 + *
776 + * @version 1.0.1
777 + */
778 +function learn_press_update_user_profile_change_password() {
779 + try {
780 + $user = wp_get_current_user();
781 + if ( ! $user ) {
782 + throw new Exception( __( 'The user is invalid', 'learnpress' ) );
783 + }
784 +
785 + $old_pass = LP_Request::get_param( 'pass0', '', 'text', 'post' );
786 + if ( empty( $old_pass ) ) {
787 + throw new Exception( __( 'Enter the old password!', 'learnpress' ) );
788 + }
789 +
790 + $check_old_pass = wp_check_password( $old_pass, $user->data->user_pass, $user->ID );
791 + if ( ! $check_old_pass ) {
792 + throw new Exception( __( 'The old password is incorrect!', 'learnpress' ) );
793 + }
794 +
795 + $new_pass = LP_Request::get_param( 'pass1', '', 'text', 'post' );
796 + $new_pass_confirm = LP_Request::get_param( 'pass2', '', 'text', 'post' );
797 + if ( empty( $new_pass ) || empty( $new_pass_confirm )
798 + || $new_pass != $new_pass_confirm ) {
799 + throw new Exception( __( 'Incorrect confirmation password!', 'learnpress' ) );
800 + }
801 +
802 + // Update user password
803 + // wp_set_password( $new_pass, $user->ID );
804 + $update_data = array(
805 + 'user_pass' => $new_pass,
806 + 'ID' => $user->ID,
807 + );
808 +
809 + $rs = wp_update_user( $update_data );
810 + if ( is_wp_error( $rs ) ) {
811 + throw new Exception( $rs->get_error_message() );
812 + }
813 + } catch ( Exception $ex ) {
814 + return new WP_Error( 'error_lp_profile_update_pass', $ex->getMessage() );
815 + }
816 +
817 + return true;
818 +}
819 +
820 +function learn_press_get_avatar_thumb_size() {
821 + $option = LP_Settings::get_option(
822 + 'avatar_dimensions',
823 + array(
824 + 'width' => 250,
825 + 'height' => 250,
826 + )
827 + );
828 +
829 + if ( ! isset( $option['width'] ) || ! isset( $option['height'] ) ) {
830 + $option = array(
831 + 'width' => 250,
832 + 'height' => 250,
833 + );
834 + }
835 +
836 + // For option get_avatar_url
837 + $option['size'] = $option['width'];
838 +
839 + return $option;
840 +}
841 +
842 +function learn_press_get_course_thumbnail_dimensions() {
843 + $option = LP_Settings::get_option(
844 + 'course_thumbnail_dimensions',
845 + array(
846 + 'width' => 500,
847 + 'height' => 300,
848 + )
849 + );
850 +
851 + if ( ! isset( $option['width'] ) || ! isset( $option['height'] ) ) {
852 + $option = array(
853 + 'width' => 500,
854 + 'height' => 300,
855 + );
856 + }
857 +
858 + return $option;
859 +}
860 +
861 +/*
862 +function learn_press_get_user_avatar( $user_id = 0, $size = '' ) {
863 + $user = learn_press_get_user( $user_id );
864 +
865 + return $user->get_profile_picture( '', $size );
866 +}*/
867 +
868 +/**
869 + * Get profile instance for an user to view.
870 + *
871 + * @param int $for_user
872 + *
873 + * @return LP_Profile|WP_Error
874 + */
875 +function learn_press_get_profile( $for_user = 0 ) {
876 + return LP_Profile::instance( $for_user );
877 +}
878 +
879 +/*
880 +function learn_press_remove_user_items( $user_id, $item_id, $course_id, $include_course = false ) {
881 + global $wpdb;
882 +
883 + settype( $item_id, 'array' );
884 +
885 + $format = array_fill( 0, sizeof( $item_id ), '%d' );
886 + $where = '';
887 +
888 + $args = array( $user_id );
889 + $args = array_merge( $args, $item_id );
890 +
891 + if ( $course_id ) {
892 + $args[] = $course_id;
893 + $where = 'AND ref_id = %d';
894 + }
895 +
896 + if ( $include_course ) {
897 + $where .= ' OR ( item_id = %d AND item_type = %s )';
898 + $args[] = $course_id;
899 + $args[] = LP_COURSE_CPT;
900 + }
901 +
902 + $query = $wpdb->prepare(
903 + "
904 + DELETE
905 + FROM {$wpdb->learnpress_user_items}
906 + WHERE user_id = %d
907 + AND ( item_id IN(" . join( ',', $format ) . ")
908 + $where )
909 + ",
910 + $args
911 + );
912 +}*/
913 +
914 +/**
915 + * Get user profile link
916 + *
917 + * @param int $user_id
918 + * @param null $tab
919 + *
920 + * @return mixed|string
921 + */
922 +function learn_press_user_profile_link( $user_id = 0, $tab = '' ) {
923 + if ( ! $user_id ) {
924 + $user_id = get_current_user_id();
925 + }
926 +
927 + $user = learn_press_get_user( $user_id );
928 +
929 + if ( ! $user ) {
930 + return '';
931 + }
932 +
933 + global $wp_query;
934 + $wp_user = get_userdata( $user_id );
935 + if ( ! $wp_user instanceof WP_User ) {
936 + return '';
937 + }
938 +
939 + $user_model = new UserModel( $wp_user->data );
940 + $args = array(
941 + 'user' => $user_model->get_slug_link(),
942 + );
943 +
944 + if ( $tab ) {
945 + $args['tab'] = $tab;
946 + }
947 +
948 + /**
949 + * If no tab is selected in profile and is current user
950 + * then no need the username in profile link.
951 + */
952 + if ( ( $user_id == get_current_user_id() ) && ! isset( $args['tab'] ) ) {
953 + //unset( $args['user'] );
954 + }
955 +
956 + $profile_link = trailingslashit( learn_press_get_page_link( 'profile' ) );
957 + if ( $profile_link ) {
958 + if ( get_option( 'permalink_structure' ) ) {
959 + $url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) );
960 + } else {
961 + $url = esc_url_raw( add_query_arg( $args, $profile_link ) );
962 + }
963 + } else {
964 + $url = get_author_posts_url( $user_id );
965 + }
966 +
967 + return apply_filters( 'learn_press_user_profile_link', $url, $user_id, $tab );
968 +}
969 +
970 +/*
971 +function learn_press_create_user_item( $args = array(), $wp_error = false ) {
972 + global $wpdb;
973 +
974 + $defaults = array(
975 + 'user_id' => get_current_user_id(),
976 + 'item_id' => '',
977 + 'start_time' => time(),
978 + 'end_time' => '',
979 + 'graduation' => '',
980 + 'item_type' => '',
981 + 'status' => '',
982 + 'ref_id' => 0,
983 + 'ref_type' => '',
984 + 'parent_id' => 0,
985 + 'create_meta' => array(),
986 + );
987 +
988 + $item_data = wp_parse_args( $args, $defaults );
989 +
990 + // Validate item_id and post type
991 + if ( empty( $item_data['item_id'] ) ) {
992 + if ( $wp_error ) {
993 + return new WP_Error( 'invalid_item_id', __( 'Invalid item id.', 'learnpress' ) );
994 + }
995 +
996 + return 0;
997 + }
998 +
999 + $post_type = learn_press_get_post_type( $item_data['item_id'] );
1000 + if ( empty( $item_data['item_type'] ) && $post_type ) {
1001 + $item_data['item_type'] = $post_type;
1002 + }
1003 +
1004 + // Get id and type of ref if they are null
1005 + if ( ! empty( $item_data['parent_id'] ) && ( empty( $item_data['ref_id'] ) || ( empty( $item_data['ref_type'] ) ) ) ) {
1006 + $parent = $wpdb->get_row(
1007 + $wpdb->prepare(
1008 + "SELECT * FROM {$wpdb->learnpress_user_items} WHERE %d",
1009 + $item_data['parent_id']
1010 + )
1011 + );
1012 +
1013 + if ( $parent ) {
1014 + if ( empty( $item_data['ref_id'] ) ) {
1015 + $item_data['ref_id'] = $parent->item_id;
1016 + }
1017 +
1018 + if ( empty( $item_data['ref_type'] ) ) {
1019 + $item_data['ref_type'] = $parent->item_type;
1020 + }
1021 + }
1022 + }
1023 +
1024 + // Filter
1025 + $item_data = apply_filters( 'learn-press/create-user-item-data', $item_data );
1026 + if ( ! $item_data ) {
1027 + if ( $wp_error ) {
1028 + return new WP_Error( 'invalid_item_data', __( 'Invalid item data.', 'learnpress' ) );
1029 + }
1030 +
1031 + return 0;
1032 + }
1033 +
1034 + do_action( 'learn-press/before-create-user-item', $item_data );
1035 +
1036 + $create_meta = ! empty( $item_data['create_meta'] ) ? $item_data['create_meta'] : false;
1037 +
1038 + if ( $create_meta ) {
1039 + unset( $item_data['create_meta'] );
1040 + }
1041 +
1042 + $user_item = new LP_User_Item( $item_data );
1043 + $result = $user_item->update();
1044 + if ( ! $result || is_wp_error( $result ) ) {
1045 + if ( $wp_error && is_wp_error( $result ) ) {
1046 + return $result;
1047 + }
1048 +
1049 + return 0;
1050 + }
1051 +
1052 + do_action( 'learn-press/created-user-item', $user_item, $item_data );
1053 +
1054 + $create_meta = apply_filters( 'learn-press/create-user-item-meta', $create_meta, $item_data );
1055 + if ( ! $create_meta ) {
1056 + return $user_item;
1057 + }
1058 +
1059 + do_action( 'learn-press/before-create-user-item-meta', $create_meta );
1060 +
1061 + foreach ( $create_meta as $key => $value ) {
1062 + learn_press_update_user_item_meta( $user_item->get_user_item_id(), $key, $value );
1063 + }
1064 +
1065 + do_action( 'learn-press/created-user-item-meta', $user_item, $create_meta );
1066 +
1067 + return $user_item;
1068 +}*/
1069 +
1070 +/*
1071 +function learn_press_isset_user_item_for_quiz( $quiz_id, $course_id ) {
1072 + global $wpdb;
1073 +
1074 + $query = $wpdb->prepare( "SELECT user_item_id FROM $wpdb->learnpress_user_items WHERE ref_id=%d AND item_id=%d", $course_id, $quiz_id );
1075 + $col = $wpdb->get_col( $query );
1076 +
1077 + if ( ! empty( $col ) ) {
1078 + return $col;
1079 + } else {
1080 + return false;
1081 + }
1082 +}*/
1083 +
1084 +/**
1085 + * Prepares list of questions for rest api.
1086 + *
1087 + * @param int[] $question_ids
1088 + * @param array $args
1089 + *
1090 + * @return array
1091 + * @since 3.3.0
1092 + */
1093 +function learn_press_rest_prepare_user_questions( array $question_ids = array(), array $args = array() ): array {
1094 + if ( is_numeric( $args ) ) {
1095 +
1096 + } else {
1097 + $args = wp_parse_args(
1098 + $args,
1099 + array(
1100 + 'instant_hint' => true,
1101 + 'instant_check' => true,
1102 + 'quiz_status' => '',
1103 + 'checked_questions' => array(),
1104 + 'hinted_questions' => array(),
1105 + 'answered' => array(),
1106 + 'show_correct_review' => true,
1107 + )
1108 + );
1109 + }
1110 +
1111 + $checkedQuestions = $args['checked_questions'];
1112 + $hintedQuestions = $args['hinted_questions'];
1113 + $instantHint = $args['instant_hint'];
1114 + $instantCheck = $args['instant_check'];
1115 + $quizStatus = $args['quiz_status'];
1116 + $answered = $args['answered'];
1117 + $status = $args['status'] ?? '';
1118 + $questions = array();
1119 +
1120 + if ( $question_ids ) {
1121 + foreach ( $question_ids as $id ) {
1122 + $question = learn_press_get_question( $id );
1123 + $hasHint = false;
1124 + $hasExplanation = false;
1125 + $canCheck = false;
1126 + $hinted = false;
1127 + $checked = false;
1128 + $theHint = $question->get_hint();
1129 + $theExplanation = '';
1130 +
1131 + if ( $instantCheck || $status == 'completed' ) {
1132 + $theExplanation = $question->get_explanation();
1133 + $checked = in_array( $id, $checkedQuestions );
1134 + $hasExplanation = (bool) $theExplanation;
1135 + }
1136 +
1137 + $mark = $question->get_mark() ? $question->get_mark() : 1;
1138 +
1139 + $questionData = array(
1140 + 'object' => $question,
1141 + 'id' => absint( $id ),
1142 + 'title' => $question->get_title(),
1143 + 'type' => $question->get_type(),
1144 + 'point' => $mark,
1145 + );
1146 +
1147 + $content = $question->get_content();
1148 + if ( $content ) {
1149 + $questionData['content'] = $content;
1150 + }
1151 +
1152 + if ( $theHint ) {
1153 + $questionData['hint'] = $theHint;
1154 + }
1155 +
1156 + if ( $status == 'completed' || ( $checked && $theExplanation ) ) {
1157 + $questionData['explanation'] = $theExplanation;
1158 + }
1159 +
1160 + if ( $hasExplanation ) {
1161 + $questionData['has_explanation'] = $hasExplanation;
1162 +
1163 + if ( $checked ) {
1164 + $questionData['explanation'] = $theExplanation;
1165 + }
1166 + }
1167 +
1168 + $with_true_or_false = ( $checked || ( $quizStatus == 'completed' && $args['show_correct_review'] ) );
1169 +
1170 + // if ( $question->is_support( 'answer-options' ) ) {
1171 + $questionData['options'] = learn_press_get_question_options_for_js(
1172 + $question,
1173 + array(
1174 + 'include_is_true' => $with_true_or_false,
1175 + 'answer' => $answered[ $id ]['answered'] ?? '',
1176 + )
1177 + );
1178 + // }
1179 +
1180 + $questions[] = $questionData;
1181 + }
1182 +
1183 + /**
1184 + * Remove answered
1185 + */
1186 + if ( $quizStatus !== 'completed' ) {
1187 + if ( $checkedQuestions && $quizStatus ) {
1188 +
1189 + $omitIds = array_diff( $question_ids, $checkedQuestions );
1190 +
1191 + if ( $omitIds ) {
1192 + foreach ( $omitIds as $omitId ) {
1193 + if ( ! empty( $answered[ $omitId ] ) ) {
1194 + unset( $answered[ $omitId ] );
1195 + }
1196 + }
1197 + }
1198 + }
1199 + }
1200 + }
1201 +
1202 + return apply_filters( 'learn-press/list-questions-data', $questions );
1203 +}
1204 +
1205 +/**
1206 + * Update extra profile data upon update user.
1207 + *
1208 + * @param int $user_id
1209 + *
1210 + * @since 4.0.0
1211 + */
1212 +function learn_press_update_extra_user_profile_fields( $user_id ) {
1213 + if ( ! current_user_can( 'edit_user', $user_id ) ) {
1214 + return;
1215 + }
1216 +
1217 + if ( array_key_exists( 'lp_user_slug', $_POST ) ) {
1218 + $userModel = UserModel::find( $user_id, true );
1219 + if ( ! $userModel ) {
1220 + return;
1221 + }
1222 +
1223 + $slug_result = $userModel->update_user_nicename( (string) $_POST['lp_user_slug'] );
1224 + if ( is_wp_error( $slug_result ) ) {
1225 + wp_safe_redirect(
1226 + add_query_arg(
1227 + 'lp-message',
1228 + urlencode( $slug_result->get_error_message() ),
1229 + wp_get_referer()
1230 + )
1231 + );
1232 + die();
1233 + }
1234 + }
1235 +
1236 + if ( isset( $_POST['_lp_extra_info'] ) ) {
1237 + $extra_info = LP_Request::get_param( '_lp_extra_info', array(), '', 'post' );
1238 + update_user_meta( $user_id, '_lp_extra_info', $extra_info );
1239 + }
1240 +}
1241 +
1242 +add_action( 'personal_options_update', 'learn_press_update_extra_user_profile_fields' );
1243 +add_action( 'edit_user_profile_update', 'learn_press_update_extra_user_profile_fields' );
1244 +
1245 +/**
1246 + * Get extra profile info data
1247 + *
1248 + * @param int $user_id
1249 + *
1250 + * @return array
1251 + * @since 4.0.1
1252 + */
1253 +function learn_press_get_user_extra_profile_info( $user_id = 0 ) {
1254 + if ( ! $user_id ) {
1255 + $user_id = get_current_user_id();
1256 + }
1257 +
1258 + $extra_profile_info = get_the_author_meta( '_lp_extra_info', $user_id );
1259 + $social_fields = learn_press_social_profiles();
1260 +
1261 + $user_socials = array();
1262 + foreach ( $social_fields as $key => $label ) {
1263 + $key = sanitize_key( $key );
1264 + $user_socials[ $key ] = '';
1265 + if ( is_array( $extra_profile_info ) && isset( $extra_profile_info[ $key ] ) ) {
1266 + $user_socials[ $key ] = esc_url_raw( $extra_profile_info[ $key ] );
1267 + }
1268 + }
1269 +
1270 + return apply_filters( 'learn-press/user-extra-profile-info', $user_socials, $user_id );
1271 +}
1272 +
1273 +/**
1274 + * @return array
1275 + * @since 3.x.x
1276 + * @version 4.0.2
1277 + */
1278 +function learn_press_social_profiles() {
1279 + return apply_filters(
1280 + 'learn-press/social-profiles',
1281 + array(
1282 + 'facebook' => learn_press_social_profile_name( 'facebook' ),
1283 + 'twitter' => learn_press_social_profile_name( 'twitter' ),
1284 + 'youtube' => learn_press_social_profile_name( 'youtube' ),
1285 + 'linkedin' => learn_press_social_profile_name( 'linkedin' ),
1286 + )
1287 + );
1288 +}
1289 +
1290 +function learn_press_social_profile_name( $key ) {
1291 + $name = '';
1292 + switch ( $key ) {
1293 + case 'facebook':
1294 + $name = esc_html__( 'Facebook Profile', 'learnpress' );
1295 + break;
1296 + case 'twitter':
1297 + $name = esc_html__( 'X Profile', 'learnpress' );
1298 + break;
1299 + case 'youtube':
1300 + $name = esc_html__( 'Youtube Channel', 'learnpress' );
1301 + break;
1302 + case 'linkedin':
1303 + $name = esc_html__( 'Linkedin Profile', 'learnpress' );
1304 + break;
1305 + default:
1306 + $name = ucfirst( $key );
1307 + }
1308 +
1309 + return apply_filters( 'learn-press/social-profile-name', $name, $key );
1310 +}
1311 +
1312 +function lp_add_default_fields( $fields ) {
1313 + $first_name = LP_Settings::get_option( 'enable_register_first_name', 'no' );
1314 +
1315 + if ( $first_name === 'yes' ) {
1316 + ?>
1317 + <li class="form-field">
1318 + <label for="reg_first_name"><?php esc_html_e( 'First name', 'learnpress' ); ?></label>
1319 + <input id="reg_first_name" name="reg_first_name" type="text"
1320 + placeholder="<?php esc_attr_e( 'First name', 'learnpress' ); ?>"
1321 + value="<?php echo esc_attr( wp_unslash( $_POST['reg_first_name'] ?? '' ) ); ?>">
1322 + </li>
1323 + <?php
1324 + }
1325 +
1326 + $last_name = LP_Settings::instance()->get( 'enable_register_last_name' );
1327 +
1328 + if ( $last_name === 'yes' ) {
1329 + ?>
1330 + <li class="form-field">
1331 + <label for="reg_last_name"><?php esc_html_e( 'Last name', 'learnpress' ); ?></label>
1332 + <input id="reg_last_name" name="reg_last_name" type="text"
1333 + placeholder="<?php esc_attr_e( 'Last name', 'learnpress' ); ?>"
1334 + value="<?php echo esc_attr( wp_unslash( $_POST['reg_last_name'] ?? '' ) ); ?>">
1335 + </li>
1336 + <?php
1337 + }
1338 +
1339 + $display_name = LP_Settings::instance()->get( 'enable_register_display_name' );
1340 +
1341 + if ( $display_name === 'yes' ) {
1342 + ?>
1343 + <li class="form-field">
1344 + <label for="reg_display_name"><?php esc_html_e( 'Display name', 'learnpress' ); ?></label>
1345 + <input id="reg_display_name" name="reg_display_name" type="text"
1346 + placeholder="<?php esc_attr_e( 'Display name', 'learnpress' ); ?>"
1347 + value="<?php echo esc_attr( wp_unslash( $_POST['reg_display_name'] ?? '' ) ); ?>">
1348 + </li>
1349 + <?php
1350 + }
1351 +}
1352 +
1353 +add_filter( 'learn-press/after-form-register-fields', 'lp_add_default_fields' );
1354 +
1355 +function lp_custom_register_fields_display() {
1356 + ?>
1357 + <?php $custom_fields = LP_Profile::get_register_fields_custom(); ?>
1358 +
1359 + <?php if ( $custom_fields ) : ?>
1360 + <?php foreach ( $custom_fields as $custom_field ) : ?>
1361 + <?php
1362 + $cf_class = '';
1363 + if ( $custom_field['required'] == 'yes' ) {
1364 + $cf_class = ' required';
1365 + ?>
1366 + <style>
1367 + .required label {
1368 + font-weight: bold;
1369 + }
1370 +
1371 + .required label:after {
1372 + content: ' *';
1373 + display: inline;
1374 + }
1375 + </style>
1376 + <?php
1377 + }
1378 +
1379 + if ( isset( $custom_field['id'] ) ) {
1380 + ?>
1381 + <?php $value = $custom_field['id']; ?>
1382 +
1383 + <li class="form-field<?php echo esc_attr( $cf_class ); ?>">
1384 + <?php
1385 + switch ( $custom_field['type'] ) {
1386 + case 'text':
1387 + case 'number':
1388 + case 'email':
1389 + case 'url':
1390 + case 'tel':
1391 + ?>
1392 + <label for="description"><?php echo esc_html( $custom_field['name'] ); ?></label>
1393 + <input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"
1394 + type="<?php echo esc_attr( $custom_field['type'] ); ?>" class="regular-text"
1395 + value=""/>
1396 + <?php
1397 + break;
1398 + case 'textarea':
1399 + ?>
1400 + <label for="description"><?php echo esc_html( $custom_field['name'] ); ?></label>
1401 + <textarea name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"></textarea>
1402 + <?php
1403 + break;
1404 + case 'checkbox':
1405 + ?>
1406 + <label>
1407 + <input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"
1408 + type="<?php echo esc_attr( $custom_field['type'] ); ?>" value="1">
1409 + <?php echo esc_html( $custom_field['name'] ); ?>
1410 + </label>
1411 + <?php
1412 + break;
1413 + }
1414 + ?>
1415 + </li>
1416 + <?php } ?>
1417 + <?php endforeach; ?>
1418 + <?php endif; ?>
1419 + <?php
1420 +}
1421 +
1422 +add_action( 'learn-press/after-form-register-fields', 'lp_custom_register_fields_display' );
1423 +
1424 +/**
1425 + * Custom register fields
1426 + *
1427 + * @param [type] $user_id
1428 + *
1429 + * @return void
1430 + */
1431 +function lp_user_custom_register_fields( $user_id, $fields = array() ) {
1432 + if ( ! empty( $fields ) ) {
1433 + update_user_meta( $user_id, '_lp_custom_register', LP_Helper::sanitize_params_submitted( $fields ) );
1434 + } elseif ( isset( $_POST['_lp_custom_register'] ) ) {
1435 + update_user_meta( $user_id, '_lp_custom_register', LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register'] ) );
1436 + }
1437 +}
1438 +
1439 +add_action( 'personal_options_update', 'lp_user_custom_register_fields' );
1440 +add_action( 'edit_user_profile_update', 'lp_user_custom_register_fields' );
1441 +
1442 +function lp_get_user_custom_register_fields( $user_id = 0 ) {
1443 + if ( ! $user_id ) {
1444 + $user_id = get_current_user_id();
1445 + }
1446 +
1447 + $register_fields = get_the_author_meta( '_lp_custom_register', $user_id );
1448 + $defaults = lp_get_user_custom_fields();
1449 +
1450 + $extra_profile_info = wp_parse_args( $register_fields, $defaults );
1451 +
1452 + return apply_filters( 'lp/user-custom-register-fields', $register_fields, $user_id );
1453 +}
1454 +
1455 +function lp_get_user_custom_fields() {
1456 + $custom_fields = LP_Settings::get_option( 'register_profile_fields', array() );
1457 +
1458 + $output = array();
1459 +
1460 + if ( $custom_fields ) {
1461 + foreach ( $custom_fields as $field ) {
1462 + $output[ $field['id'] ] = '';
1463 + }
1464 + }
1465 +
1466 + return $output;
1467 +}
1468 +
1469 +/**
1470 + * Check extra user data is a social profile.
1471 + *
1472 + * @param $key
1473 + *
1474 + * @return bool
1475 + * @since 4.0.0
1476 + * @deprecated 4.3.2
1477 + */
1478 +function learn_press_is_social_profile( $key ) {
1479 + _deprecated_function( __FUNCTION__, '4.3.2' );
1480 +
1481 + return true;
1482 +
1483 + $is_socials = learn_press_social_profiles();
1484 +
1485 + return in_array( $key, $is_socials );
1486 +}
1487 +
1488 +/**
1489 + * Get extra profile fields will be registered in backend profile.
1490 + *
1491 + * @return array
1492 + * @since 4.0.0
1493 + * @deprecated 4.3.2
1494 + */
1495 +function learn_press_get_user_extra_profile_fields() {
1496 + _deprecated_function( __FUNCTION__, '4.3.2' );
1497 + return array();
1498 +
1499 + $socials = learn_press_social_profiles();
1500 +
1501 + return $socials;
1502 +
1503 + $fields = array();
1504 +
1505 + foreach ( $socials as $social ) {
1506 + $fields[ $social ] = learn_press_social_profile_name( $social );
1507 + }
1508 +
1509 + return apply_filters( 'learn-press/user-extra-profile-fields', $fields );
1510 +}
1511 +
1512 +/**
1513 + * Show courses user enrolled on backend
1514 + *
1515 + * @param $user
1516 + *
1517 + * @return void
1518 + */
1519 +function learn_press_user_profile_data( $user ) {
1520 + if ( ! is_admin() ) {
1521 + return;
1522 + }
1523 +
1524 + learn_press_admin_view( 'backend-user-profile', array( 'user' => $user ) );
1525 + learn_press_admin_view( 'user/courses.php', array( 'user_id' => $user->ID ) );
1526 +}
1527 +
1528 +add_action( 'edit_user_profile', 'learn_press_user_profile_data', 1000 );
1529 +add_action( 'show_user_profile', 'learn_press_user_profile_data', 1000 );
1530 +
1531 +/*
1532 +function learnpress_get_count_by_user( $user_id = '', $post_type = 'lp_course' ) {
1533 + if ( empty( $user_id ) ) {
1534 + return false;
1535 + }
1536 +
1537 + $args = array(
1538 + 'author' => $user_id,
1539 + 'posts_per_page' => - 1,
1540 + 'post_type' => $post_type,
1541 + 'post_status' => 'any',
1542 + );
1543 +
1544 + $posts = get_posts( $args );
1545 +
1546 + $output = array(
1547 + 'all' => count( $posts ),
1548 + 'publish' => array(),
1549 + 'pending' => array(),
1550 + );
1551 +
1552 + $pending = $public = array();
1553 +
1554 + if ( ! empty( $posts ) ) {
1555 + foreach ( $posts as $post ) {
1556 + switch ( $post->post_status ) {
1557 + case 'pending':
1558 + $pending[] = $post;
1559 + break;
1560 + case 'publish':
1561 + $public[] = $post;
1562 + break;
1563 + default:
1564 + break;
1565 + }
1566 + }
1567 + }
1568 +
1569 + return array(
1570 + 'all' => count( $posts ),
1571 + 'publish' => count( $public ),
1572 + 'pending' => count( $pending ),
1573 + );
1574 +}*/