PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.3.8
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.3.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 4.1.7.3.2 4.2.0 4.2.1 All 138 releases
learnpress / inc / user / class-lp-profile.php

class-lp-profile.php in LearnPress – WordPress LMS Plugin for Create and Sell Online Courses 4.3.8, at inc/user/class-lp-profile.php

1,158 lines 31.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use LearnPress\Databases\UserItemsDB;
4 use LearnPress\Filters\UserItemsFilter;
5 use LearnPress\Helpers\Config;
6 use LearnPress\Models\Courses;
7 use LearnPress\Models\UserModel;
8 use LearnPress\Models\UserItems\UserCourseModel;
9 use LearnPress\Models\UserItems\UserItemModel;
10 use LearnPress\Services\UserService;
11 use LearnPress\TemplateHooks\Profile\ProfileOrdersTemplate;
12
13 defined( 'ABSPATH' ) || exit;
14
15 require_once 'class-lp-profile-tabs.php';
16
17 if ( ! class_exists( 'LP_Profile' ) ) {
18 /**
19 * Class LP_Profile
20 *
21 * Main class to control the profile of a user
22 */
23 class LP_Profile {
24 /**
25 * The instances of all users has initialed a profile
26 *
27 * @var array
28 */
29 protected static $_instances = array();
30
31 protected static $_instance = null;
32
33 /**
34 * @var LP_User Current user viewing profile
35 */
36 protected $user_current = false;
37
38 /**
39 * @var LP_User User of Profile
40 */
41 protected $_user = false;
42
43 /**
44 * @var string
45 */
46 protected $_role = '';
47
48 /**
49 * @var bool
50 */
51 protected static $_hook_added = false;
52
53 /**
54 * @var array
55 * @deprecated 4.2.6.2
56 */
57 protected $_privacy = array();
58
59 /**
60 * @var array
61 */
62 protected $_default_actions = array();
63
64 /**
65 * @var LP_User_CURD
66 */
67 protected $_curd = null;
68
69 /**
70 * @var null
71 */
72 protected $_tabs = null;
73
74 /**
75 * @var array
76 */
77 protected $_default_settings = array();
78
79 /**
80 * Constructor
81 *
82 * @param $user
83 * @param string $role
84 */
85 protected function __construct( $user, $role = '' ) {
86 $this->_curd = new LP_User_CURD();
87 $this->user_current = learn_press_get_current_user();
88 $this->_user = learn_press_get_user( $user );
89 //$this->get_user();
90
91 /*if ( ! $role ) {
92 $this->_role = $this->get_role();
93 }*/
94
95 $this->_default_actions = apply_filters(
96 'learn-press/profile-default-actions',
97 array(
98 'basic-information' => esc_html__( 'Account information updated successfully.', 'learnpress' ),
99 'avatar' => esc_html__( 'Account avatar updated successfully.', 'learnpress' ),
100 'password' => esc_html__( 'Password updated successfully.', 'learnpress' ),
101 'privacy' => esc_html__( 'Account privacy updated successfully.', 'learnpress' ),
102 )
103 );
104
105 if ( ! self::$_hook_added ) {
106 self::$_hook_added = true;
107
108 add_action( 'learn-press/profile-content', array( $this, 'output' ), 10, 3 );
109 add_action( 'learn-press/before-profile-content', array( $this, 'output_section' ), 10, 3 );
110 add_action( 'learn-press/profile-section-content', array( $this, 'output_section_content' ), 10, 3 );
111
112 /*foreach ( $this->_default_actions as $action => $message ) {
113 LP_Request::register( 'save-profile-' . $action, array( $this, 'save' ) );
114 }*/
115
116 foreach ( $this->_default_actions as $action => $message ) {
117 if ( isset( $_REQUEST[ 'save-profile-' . $action ] ) ) {
118 $this->save( $_REQUEST[ 'save-profile-' . $action ] );
119 }
120 }
121 }
122 }
123
124 public function is_guest() {
125 return ! $this->_user || $this->_user && $this->_user->is_guest();
126 }
127
128 public function output( $tab, $args, $user ) {
129 $location = learn_press_locate_template( 'profile/tabs/' . $tab . '.php' );
130
131 if ( $location && file_exists( $location ) ) {
132 include $location;
133 }
134 }
135
136 public function output_section( $tab_key, $tab_data, $user ) {
137 learn_press_get_template( 'profile/tabs/sections.php', compact( 'tab_key', 'tab_data', 'user' ) );
138 }
139
140 public function output_section_content( $section, $args, $user ) {
141 global $wp;
142
143 $current = $this->get_current_section();
144
145 if ( $current === $section ) {
146 $location = learn_press_locate_template( 'profile/tabs/' . $this->get_current_tab() . '/' . $section . '.php' );
147 if ( $location && file_exists( $location ) ) {
148 include $location;
149 }
150 }
151 }
152
153 /**
154 * Get role of current user.
155 *
156 * @return string
157 * @deprecated 4.2.6.2
158 */
159 protected function get_role() {
160 _deprecated_function( __METHOD__, '4.2.6.2' );
161
162 return '';
163 $user = $this->get_user();
164
165 if ( $user ) {
166 if ( ! $user->is_guest() ) {
167 if ( $this->_user->is_admin() ) {
168 return 'admin';
169 }
170
171 if ( $this->_user->is_instructor() ) {
172 return 'instructor';
173 }
174
175 return 'user';
176 }
177 }
178
179 return '';
180 }
181
182 /**
183 * Get the user of a profile instance.
184 *
185 * @return bool|LP_User
186 * @since 3.0.0
187 * @version 1.0.2
188 */
189 public function get_user() {
190 return $this->_user;
191 }
192
193 /**
194 * Get current user viewing profile.
195 *
196 * @return bool|LP_User
197 * @since 3.0.0
198 * @version 1.0.2
199 */
200 public function get_user_current() {
201 return $this->user_current;
202 }
203
204 /**
205 * Check current user view self profile.
206 *
207 * @return bool
208 */
209 public function is_current_user(): bool {
210 return $this->get_user() instanceof LP_User && $this->get_user()->get_id() === $this->get_user_current()->get_id();
211 }
212
213 /**
214 * Wrap function for $user->get_data()
215 *
216 * @param string $field
217 *
218 * @return mixed
219 */
220 public function get_user_data( $field ) {
221 $user_id = 0;
222 $user_data = [];
223 if ( $this->_user instanceof LP_User ) {
224 $user_id = $this->_user->get_id();
225 $user_data = $this->_user->get_data( $field );
226 }
227
228 return 'id' === strtolower( $field ) ? $user_id : $user_data;
229 }
230
231 /**
232 * @deprecated 4.2.6.2
233 */
234 public function tab_dashboard() {
235 _deprecated_function( __METHOD__, '4.2.6.2' );
236
237 return;
238 learn_press_get_template( 'profile/dashboard.php', array( 'user' => $this->_user ) );
239 }
240
241 public function get_login_url( $redirect = false ) {
242 return learn_press_get_login_url( $redirect !== false ? $redirect : LP_Helper::getUrlCurrent() );
243 }
244
245 /**
246 * Get tabs.
247 *
248 * @return array
249 * @since 4.2.6.4
250 * @version 1.0.0
251 */
252 public static function get_tabs_arr(): array {
253 return Config::instance()->get( 'profile-tabs' );
254 }
255
256 /**
257 * Get default tabs for profile.
258 * Hide tabs if user is not Administrator/Instructor.
259 *
260 * @return LP_Profile_Tabs
261 */
262 public function get_tabs() {
263 $user_of_profile = $this->get_user();
264 $tabs = self::get_tabs_arr();
265
266 $userModelProfile = UserModel::find( $user_of_profile->get_id(), true );
267 if ( $userModelProfile ) {
268 $userModelProfileRoles = $userModelProfile->get_roles();
269
270 /*
271 * Check if user not Admin/Instructor, will be hide tab Courses.
272 */
273 if ( ! array_intersect( $userModelProfileRoles, [ UserModel::ROLE_ADMINISTRATOR, UserModel::ROLE_INSTRUCTOR ] ) ) {
274 unset( $tabs['courses'] );
275 }
276 }
277
278 // Filter tabs by role - only show tabs with role restriction to users with matching roles
279 $user_role = $user_of_profile instanceof LP_User ? $user_of_profile->get_data( 'role' ) : '';
280 foreach ( $tabs as $tab_key => $tab_data ) {
281 if ( ! empty( $tab_data['role'] ) && is_array( $tab_data['role'] ) ) {
282 if ( ! in_array( $user_role, $tab_data['role'] ) ) {
283 unset( $tabs[ $tab_key ] );
284 }
285 }
286 }
287
288 $tabs = apply_filters( 'learn-press/get-profile-tabs', $tabs, $user_of_profile, $this->user_current );
289 $this->_tabs = new LP_Profile_Tabs( $tabs, $this );
290
291 return $this->_tabs;
292 }
293
294 public function get_slug( $data, $default = '' ) {
295 return $this->get_tabs()->get_slug( $data, $default );
296 }
297
298 /**
299 * Get current tab slug in query string.
300 *
301 * @param string $default Optional.
302 * @param bool $key Optional. True if return the key instead of value.
303 *
304 * @return string
305 */
306 public function get_current_tab( $default = '', $key = true ) {
307 return $this->get_tabs()->get_current_tab( $default, $key );
308 }
309
310 /**
311 * Get current section in query string.
312 *
313 * @param string $default
314 * @param bool $key
315 * @param string $tab
316 *
317 * @return bool|int|mixed|string
318 */
319 public function get_current_section( $default = '', $key = true, $tab = '' ) {
320 return $this->get_tabs()->get_current_section( $default, $key, $tab );
321 }
322
323 /**
324 * Get tab data at a position.
325 *
326 * @param int $position Optional. Indexed number or slug.
327 *
328 * @return mixed
329 */
330 public function get_tab_at( $position = 0 ) {
331 return $this->get_tabs()->get_tab_at( $position );
332 }
333
334 /**
335 * Get permalink of a tab and section if passed.
336 *
337 * @param bool $tab
338 * @param bool $with_section
339 *
340 * @return mixed|string
341 */
342 public function get_tab_link( $tab = false, $with_section = false ) {
343 $user = $this->get_user();
344 if ( ! $user ) {
345 return '';
346 }
347
348 $userModel = UserModel::find( $user->get_id(), true );
349 if ( ! $userModel ) {
350 return '';
351 }
352
353 $user_pretty_slug = $userModel->get_slug_link();
354 $url = $this->get_tabs()->get_tab_link( $tab, $with_section, $user_pretty_slug );
355
356 /**
357 * @deprecated
358 */
359 $url = apply_filters( 'learn_press_user_profile_link', $url, $user->get_id(), $tab );
360
361 return apply_filters( 'learn-press/user-profile-url', $url, $user->get_id(), $tab );
362 }
363
364 /**
365 * Get current link of profile
366 *
367 * @param string $args - Optional. Add more query args to url.
368 * @param bool $with_permalink - Optional. TRUE to build url as friendly url.
369 *
370 * @return mixed|string
371 */
372 public function get_current_url( $args = '', $with_permalink = false ) {
373 return $this->get_tabs()->get_current_url( $args, $with_permalink );
374 }
375
376 /**
377 * Check if the $key is current tab.
378 *
379 * @param string $key
380 *
381 * @return bool
382 */
383 public function is_current_tab( $key ) {
384 return $this->get_current_tab() === $key;
385 }
386
387 /**
388 * Check if the $key is current section.
389 *
390 * @param string $key
391 * @param string $tab
392 *
393 * @return bool
394 */
395 public function is_current_section( $key, $tab = '' ) {
396 return $this->get_current_section() === $key;
397 }
398
399 /**
400 * Check if a tab or section is hidden.
401 *
402 * @param array $tab_or_section
403 *
404 * @return bool
405 */
406 public function is_hidden( $tab_or_section ) {
407 return is_array( $tab_or_section ) && array_key_exists( 'hidden', $tab_or_section ) && $tab_or_section['hidden'];
408 }
409
410 /**
411 * @deprecated 4.2.6.2
412 */
413 public function is_public() {
414 _deprecated_function( __METHOD__, '4.2.6.2' );
415
416 return false;
417
418 return $this->current_user_can( 'view-tab-dashboard' ) || is_super_admin();
419 }
420
421 public function get_default_public_tabs() {
422 return apply_filters( 'learn-press/profile/privacy-tabs', [] );
423 }
424
425 public function get_public_tabs() {
426 $privacy = get_user_meta( $this->get_user_data( 'id' ), '_lp_profile_privacy', true );
427 $public_tabs = $this->get_default_public_tabs();
428
429 if ( $privacy ) {
430 foreach ( $privacy as $k => $is_yes ) {
431 if ( $is_yes === 'yes' || is_super_admin() ) {
432 $public_tabs[] = $k;
433 }
434 }
435 }
436
437 return $public_tabs;
438 }
439
440 /**
441 * Check if user can with a capability.
442 *
443 * @since 3.0.0
444 * @version 1.0.2
445 */
446 public function current_user_can( $capability ) {
447 $privacy = array(
448 'view-tab-courses' => self::get_option_publish_profile() === 'yes',
449 'view-tab-my-courses' => $this->get_privacy( 'courses' ) == 'yes',
450 'view-tab-quizzes' => $this->get_privacy( 'quizzes' ) == 'yes',
451 );
452
453 if ( current_user_can( ADMIN_ROLE ) ) {
454 $can = true;
455 } elseif ( $this->is_current_user() ) {
456 $can = true;
457 } else {
458 $can = ! empty( $privacy[ $capability ] ) && $privacy[ $capability ] === true;
459 }
460
461 return apply_filters( 'learn-press/profile-current-user-can', $can, $capability, $this );
462 }
463
464 /**
465 * Save profile.
466 *
467 * @param string $nonce . Value of nonce depending on the action requested from profile tab.
468 *
469 * @return mixed
470 */
471 public function save( $nonce ) {
472 $user_id = get_current_user_id();
473 if ( ! $user_id ) {
474 return new WP_Error( 2, 'The user is invalid' );
475 }
476
477 $message = [
478 'status' => 'error',
479 'content' => '',
480 ];
481 $message_action = '';
482
483 foreach ( $this->_default_actions as $_action => $message_action ) {
484 if ( wp_verify_nonce( $nonce, 'learn-press-save-profile-' . $_action ) ) {
485 $action = $_action;
486 break;
487 }
488 $action = '';
489 }
490
491 if ( ! isset( $action ) ) {
492 return false;
493 }
494
495 $return = false;
496 switch ( $action ) {
497 case 'basic-information':
498 $return = learn_press_update_user_profile_basic_information( true );
499 break;
500 case 'password':
501 $return = learn_press_update_user_profile_change_password();
502 break;
503 case 'privacy':
504 $privacy = LP_Request::get_array( 'privacy' );
505
506 if ( ! $privacy ) {
507 update_user_meta( get_current_user_id(), '_lp_profile_privacy', array() );
508 } else {
509 update_user_meta( get_current_user_id(), '_lp_profile_privacy', $privacy );
510 }
511 }
512
513 if ( is_wp_error( $return ) ) {
514 $message['content'] = $return->get_error_message();
515 } else {
516 $message['status'] = 'success';
517 $message['content'] = $message_action;
518 }
519
520 learn_press_set_message( $message );
521
522 if ( 'basic-information' === $action && ! is_wp_error( $return ) ) {
523 $redirect = LP_Profile::instance( $user_id )->get_current_url();
524 } elseif ( ! empty( $_REQUEST['redirect'] ) ) {
525 $redirect = esc_url_raw( $_REQUEST['redirect'] );
526 } else {
527 $redirect = LP_Helper::getUrlCurrent();
528 }
529
530 $redirect = apply_filters( 'learn-press/profile-updated-redirect', $redirect, $action );
531
532 if ( $redirect ) {
533 wp_safe_redirect( $redirect );
534 exit;
535 }
536
537 return true;
538 }
539
540 /**
541 * Get settings for profile privacy tab.
542 *
543 * @return array
544 * @since 4.0.0
545 */
546 public function get_privacy_settings() {
547 $privacy = array(
548 array(
549 'name' => esc_html__( 'Courses', 'learnpress' ),
550 'id' => 'courses',
551 'default' => 'yes',
552 'type' => 'yes-no',
553 'description' => esc_html__( 'Public your profile courses attended.', 'learnpress' ),
554 ),
555 array(
556 'name' => esc_html__( 'Quizzes', 'learnpress' ),
557 'id' => 'quizzes',
558 'default' => 'yes',
559 'type' => 'yes-no',
560 'description' => esc_html__( 'Public your profile quizzes.', 'learnpress' ),
561 ),
562 );
563
564 return apply_filters( 'learn-press/profile-privacy-settings', $privacy );
565 }
566
567 /**
568 * Get privacy profile settings.
569 *
570 * @param string $tab
571 *
572 * @return array|mixed
573 * @since 3.0.0
574 * @version 1.0.1
575 */
576 public function get_privacy( string $tab = '' ) {
577 $user_id = $this->get_user() ? $this->get_user()->get_id() : 0;
578 $privacy = get_user_meta( $user_id, '_lp_profile_privacy', true );
579
580 return $privacy[ $tab ] ?? '';
581 }
582
583 /**
584 * Get all orders of profile's user.
585 *
586 * @param mixed $args
587 *
588 * @return array
589 * @since 3.0.0
590 * @deprecated 4.2.3.x
591 */
592 public function get_user_orders( $args = '' ) {
593 _deprecated_function( __METHOD__, '4.2.3.x' );
594 return [];
595
596 $args = wp_parse_args(
597 $args,
598 array(
599 'group_by_order' => true,
600 'status' => '',
601 )
602 );
603
604 return $this->_curd->get_orders( $this->get_user_data( 'id' ), $args );
605 }
606
607 /**
608 * Query order of user is viewing profile.
609 *
610 * @param string $args
611 *
612 * @return LP_Query_List_Table
613 */
614 public function query_orders( $args = '' ) {
615 global $wp_query;
616
617 $query = array(
618 'items' => array(),
619 'total' => 0,
620 'num_pages' => 0,
621 'pagination' => '',
622 );
623
624 /*$query_args = array();
625
626 if ( is_array( $args ) ) {
627 foreach ( array( 'status', 'group_by_order' ) as $k ) {
628 if ( isset( $args[ $k ] ) ) {
629 $query_args[ $k ] = $args[ $k ];
630 }
631 }
632 }*/
633
634 /*if ( empty( $query_args['status'] ) ) {
635 $query_args['status'] = 'completed processing cancelled pending';
636 }*/
637
638 $default_args = array(
639 'paged' => 1,
640 'limit' => 10,
641 );
642
643 if ( $this->get_current_tab() === 'orders' && isset( $wp_query->query_vars['view_id'] ) ) {
644 $default_args['paged'] = $wp_query->query_vars['view_id'];
645 }
646
647 $args = wp_parse_args( $args, $default_args );
648 $offset = isset( $args['limit'] ) && $args['limit'] > 0 && $args['paged'] ? ( $args['paged'] - 1 ) * $args['limit'] : 0;
649
650 $query_order = new WP_Query(
651 array(
652 'post_type' => LP_ORDER_CPT,
653 'posts_per_page' => $args['limit'],
654 'offset' => $offset,
655 'post_status' => [
656 LP_ORDER_COMPLETED_DB,
657 LP_ORDER_PENDING_DB,
658 LP_ORDER_PROCESSING_DB,
659 LP_ORDER_CANCELLED_DB,
660 ],
661 //'post__in' => array_keys( $order_ids ),
662 //'orderby' => 'post__in',
663 'fields' => 'ids',
664 'meta_query' => array(
665 'relation' => 'OR',
666 array(
667 'key' => '_user_id',
668 'value' => $this->get_user_data( 'id' ),
669 'compare' => '=',
670 ),
671 array(
672 'key' => '_user_id',
673 'value' => '"' . $this->get_user_data( 'id' ) . '"',
674 'compare' => 'LIKE',
675 ),
676 ),
677 )
678 );
679
680 if ( $query_order->have_posts() ) {
681 $orders = ( isset( $args['fields'] ) && 'ids' === $args['fields'] ) ? $query_order->posts : array_filter( array_map( 'learn_press_get_order', $query_order->posts ) );
682
683 $query['orders'] = $orders;
684 $query['total'] = $query_order->found_posts;
685 $query['num_pages'] = $query_order->max_num_pages;
686 $query['pagination'] = learn_press_paging_nav(
687 array(
688 'num_pages' => $query['num_pages'],
689 'base' => $this->get_tab_link( LP_Settings::instance()->get( 'profile_endpoints.orders' ) ),
690 'format' => $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( '%#%', '' ) : '?paged=%#%',
691 'echo' => false,
692 'paged' => $args['paged'],
693 )
694 );
695
696 $query = new LP_Query_List_Table(
697 array(
698 'total' => $query_order->found_posts,
699 'paged' => $args['paged'],
700 'limit' => $args['limit'],
701 'pages' => $query['num_pages'],
702 'items' => $orders,
703 )
704 );
705 } else {
706 $query = new LP_Query_List_Table( [] );
707 }
708
709 return $query;
710 }
711
712 /**
713 * Query user's courses
714 *
715 * @param string $type - Optional. [own, purchased, enrolled, etc]
716 * @param array $args - Optional.
717 *
718 * @return LP_Query_List_Table
719 * @throws Exception
720 */
721 public function query_courses( string $type = 'own', array $args = array() ): LP_Query_List_Table {
722 $lp_user_items_db = LP_User_Items_DB::getInstance();
723 $courses = array();
724
725 switch ( $type ) {
726 case 'purchased':
727 $filter = new UserItemsFilter();
728 $filter->only_fields = array( 'DISTINCT (item_id) AS item_id', 'ui.user_item_id' );
729 $filter->field_count = 'ui.item_id';
730 $filter->user_id = $this->get_user_data( 'id' );
731 $filter->order_by = 'ui.user_item_id';
732 $filter->order = 'DESC';
733 $filter->item_type = LP_COURSE_CPT;
734 $status = $args['status'] ?? '';
735
736 switch ( $status ) {
737 case '':
738 $filter->where[] = $lp_user_items_db->wpdb->prepare(
739 "AND ui.status != '%s'",
740 UserItemModel::STATUS_CANCEL
741 );
742 break;
743 case UserItemModel::STATUS_FINISHED:
744 $filter->status = UserItemModel::STATUS_FINISHED;
745 break;
746 case UserItemModel::GRADUATION_IN_PROGRESS:
747 case UserItemModel::GRADUATION_PASSED:
748 case UserItemModel::GRADUATION_FAILED:
749 $filter->graduation = $status;
750 break;
751 }
752
753 $filter->page = $args['paged'] ?? 1;
754 $filter->limit = $args['limit'] ?? $filter->limit;
755 $total_rows = 0;
756 $filter = apply_filters( 'lp/api/profile/courses/purchased/filter', $filter, $args );
757 $result_courses = UserItemsDB::getInstance()->get_user_items( $filter, $total_rows );
758
759 $course_ids = LP_Database::get_values_by_key( $result_courses, 'item_id' );
760
761 $courses = array(
762 'total' => $total_rows,
763 'paged' => $filter->page,
764 'limit' => $filter->limit,
765 'items' => $course_ids,
766 );
767 break;
768 case 'own':
769 //$query = $this->_curd->query_own_courses( $this->get_user_data( 'id' ), $args );
770 $filter = new LP_Course_Filter();
771 if ( empty( $args['status'] ) ) {
772 $args['status'] = [ 'publish', 'pending', 'private' ];
773 }
774
775 if ( ! is_array( $args['status'] ) ) {
776 $args['status'] = (array) $args['status'];
777 }
778
779 Courses::handle_params_for_query_courses( $filter, $args );
780 $filter->fields = [ 'ID' ];
781 $filter->post_author = $this->get_user_data( 'id' );
782 $filter->post_status = $args['status'];
783 $filter->page = $args['paged'] ?? 1;
784 $filter->limit = $args['limit'] ?? $filter->limit;
785 $total_rows = 0;
786 $filter = apply_filters( 'lp/api/profile/courses/own/filter', $filter, $args );
787 $result_courses = Courses::get_courses( $filter, $total_rows );
788
789 $course_ids = LP_Database::get_values_by_key( $result_courses );
790
791 $courses = array(
792 'total' => $total_rows,
793 'paged' => $filter->page,
794 'limit' => $filter->limit,
795 'items' => $course_ids,
796 );
797 break;
798 }
799
800 return new LP_Query_List_Table( $courses );
801 }
802
803 /**
804 * Get the order is viewing details.
805 */
806 public function get_view_order() {
807 global $wp_query;
808
809 $order = false;
810 if ( isset( $wp_query->query_vars['view_id'] ) ) {
811 $order = learn_press_get_order( $wp_query->query_vars['view_id'] );
812 }
813
814 return $order;
815 }
816
817 /**
818 * Get filters for purchased courses tab.
819 *
820 * @param string $current_filter
821 *
822 * @return array
823 */
824 public function get_quizzes_filters( $current_filter = '' ) {
825 $url = $this->get_tab_link( 'quizzes' );
826 $defaults = array(
827 'all' => sprintf( '<a href="%s">%s</a>', esc_url_raw( $url ), __( 'All', 'learnpress' ) ),
828 'completed' => sprintf( '<a href="%s">%s</a>', esc_url_raw( add_query_arg( 'filter-status', 'completed', $url ) ), __( 'Finished', 'learnpress' ) ),
829 'passed' => sprintf( '<a href="%s">%s</a>', esc_url_raw( add_query_arg( 'filter-graduation', 'passed', $url ) ), __( 'Passed', 'learnpress' ) ),
830 'failed' => sprintf( '<a href="%s">%s</a>', esc_url_raw( add_query_arg( 'filter-graduation', 'failed', $url ) ), __( 'Failed', 'learnpress' ) ),
831 );
832
833 if ( ! $current_filter ) {
834 $keys = array_keys( $defaults );
835 $current_filter = reset( $keys );
836 }
837
838 foreach ( $defaults as $k => $v ) {
839 if ( $k === $current_filter ) {
840 $defaults[ $k ] = sprintf( '<span>%s</span>', strip_tags( $v ) );
841 }
842 }
843
844 return apply_filters(
845 'learn-press/profile/quizzes-filters',
846 $defaults
847 );
848 }
849
850 /**
851 * Echo class for main div.
852 *
853 * @param bool $echo
854 * @param string $more
855 *
856 * @return string
857 */
858 public function main_class( $echo = true, $more = '' ) {
859 $classes = array( 'lp-user-profile' );
860
861 if ( $this->is_current_user() ) {
862 $classes[] = 'current-user';
863 }
864
865 if ( ! is_user_logged_in() ) {
866 $classes[] = 'guest';
867 }
868
869 if ( has_action( 'learn-press/before-user-profile' ) ) {
870 $classes[] = 'has-sidebar';
871 }
872
873 $classes = LP_Helper::merge_class( $classes, $more );
874
875 $class = ' class="' . implode( ' ', apply_filters( 'learn-press/profile/class', $classes ) ) . '"';
876
877 if ( $echo ) {
878 echo wp_kses_post( $class );
879 }
880
881 return $class;
882 }
883
884 /**
885 * Return true if the tab is visible for current user.
886 *
887 * @param string $tab_key
888 * @param array $tab_data
889 *
890 * @return bool
891 */
892 public function tab_is_visible_for_user( $tab_key, $tab_data = null ) {
893 return $this->is_current_tab( $tab_key ) && $this->current_user_can( "view-tab-{$tab_key}" );
894 }
895
896 /**
897 * Return true if the section is visible for current user.
898 *
899 * @param string $section_key
900 * @param array $section_data
901 *
902 * @return bool
903 */
904 public function section_is_visible_for_user( $section_key, $section_data = array() ) {
905 return $this->current_user_can( "view-section-{$section_key}" ) && ! $this->is_hidden( $section_data );
906 }
907
908 /**
909 * Get queried user in profile link.
910 *
911 * @param string $return
912 *
913 * @return false|WP_User
914 * @since 3.0.0
915 * @deprecated 4.2.9.1
916 */
917 public static function get_queried_user( $return = '' ) {
918 _deprecated_function( __METHOD__, '4.2.9.1', 'LP_Profile::instance()->get_user_current()' );
919 global $wp_query;
920
921 if ( isset( $wp_query->query['user'] ) ) {
922 $user = get_user_by( 'login', urldecode( $wp_query->query['user'] ) );
923 } else {
924 $user = get_user_by( 'id', get_current_user_id() );
925 }
926
927 return $return === 'id' && $user ? $user->ID : $user;
928 }
929
930 public function get_upload_profile_src( $size = '' ) {
931 $user = $this->get_user();
932 if ( ! $user ) {
933 return '';
934 }
935
936 $uploaded_profile_src = $user->get_data( 'uploaded_profile_src' );
937
938 if ( empty( $uploaded_profile_src ) ) {
939 $profile_picture = get_user_meta( $user->get_id(), '_lp_profile_picture', true );
940
941 if ( $profile_picture ) {
942 // Check if hase slug / at the beginning of the path, if not, add it.
943 $slash = substr( $profile_picture, 0, 1 ) === '/' ? '' : '/';
944 $profile_picture = $slash . $profile_picture;
945 // End check.
946 $upload = learn_press_user_profile_picture_upload_dir();
947 $file_path = $upload['basedir'] . $profile_picture;
948
949 if ( file_exists( $file_path ) ) {
950 $uploaded_profile_src = $upload['baseurl'] . $profile_picture;
951 } else {
952 $uploaded_profile_src = false;
953 }
954
955 $user->set_data( 'uploaded_profile_src', $uploaded_profile_src );
956 }
957 }
958
959 return apply_filters( 'learn-press/profile/get-upload-profile-src', $uploaded_profile_src, $user->get_id() );
960 }
961
962 /**
963 * Get profile cover image
964 * @return string image url if exist
965 */
966 public function get_cover_image_src() {
967 $user = $this->get_user();
968 if ( ! $user ) {
969 return '';
970 }
971 $cover_image_src = '';
972 $image_path = get_user_meta( $user->get_id(), '_lp_profile_cover_image', true );
973
974 if ( $image_path ) {
975 // Check if hase slug / at the beginning of the path, if not, add it.
976 $slash = substr( $image_path, 0, 1 ) === '/' ? '' : '/';
977 $image_path = $slash . $image_path;
978 // End check.
979 $upload = learn_press_user_profile_picture_upload_dir();
980 $file_path = $upload['basedir'] . $image_path;
981
982 if ( file_exists( $file_path ) ) {
983 $cover_image_src = $upload['baseurl'] . $image_path;
984 } else {
985 $cover_image_src = '';
986 }
987 }
988
989 return apply_filters( 'learn-press/profile/get-profile-cover-image-src', $cover_image_src, $user->get_id() );
990 }
991
992 /**
993 * Get profile image of user.
994 *
995 * @param $type
996 * @param $size
997 *
998 * @return string
999 */
1000 public function get_profile_picture( $type = '', $size = 96 ): string {
1001 $avatar = '';
1002
1003 try {
1004 $user = $this->get_user();
1005 $args = [
1006 'width' => $size,
1007 'height' => $size,
1008 ];
1009 if ( 96 === $size ) {
1010 $args = learn_press_get_avatar_thumb_size();
1011 }
1012
1013 $avatar_url = $this->get_upload_profile_src();
1014 if ( ! empty( $avatar_url ) ) {
1015 $user->set_data( 'profile_picture_src', $avatar_url );
1016 } else {
1017 $avatar_url = get_avatar_url( $user->get_id(), $args );
1018 if ( empty( $avatar_url ) ) {
1019 $avatar_url = LP_PLUGIN_URL . 'assets/images/avatar-default.png';
1020 }
1021 }
1022
1023 $avatar = apply_filters(
1024 'learn-press/user-profile/avatar',
1025 sprintf(
1026 '<img alt="%s" class="avatar" src="%s" width="%d" height="%d" />',
1027 esc_attr__( 'User Avatar', 'learnpress' ),
1028 $avatar_url,
1029 $args['width'] ?? 96,
1030 $args['height'] ?? 96
1031 ),
1032 $avatar_url,
1033 $args
1034 );
1035 } catch ( Throwable $e ) {
1036 error_log( $e->getMessage() );
1037 }
1038
1039 return $avatar;
1040 }
1041
1042 /**
1043 * Get option enable "Publish profile"
1044 *
1045 * @return string
1046 */
1047 public static function get_option_publish_profile(): string {
1048 return LP_Settings::get_option( 'publish_profile', 'no' );
1049 }
1050
1051 /**
1052 * Get statistic info of user
1053 *
1054 * @return array
1055 * @since 4.1.6
1056 * @version 1.0.0
1057 */
1058 public function get_statistic_info(): array {
1059 $user = $this->_user;
1060 $statistic = array(
1061 'enrolled_courses' => 0,
1062 'active_courses' => 0,
1063 'completed_courses' => 0,
1064 'total_courses' => 0,
1065 'total_users' => 0,
1066 );
1067
1068 try {
1069 if ( ! $user ) {
1070 throw new Exception( 'The user is invalid' );
1071 }
1072
1073 $user_id = $user->get_id();
1074 $lp_user_items_db = LP_User_Items_DB::getInstance();
1075 $lp_course_db = LP_Course_DB::getInstance();
1076
1077 // Count status
1078 $filter = new LP_User_Items_Filter();
1079 $filter->user_id = $user_id;
1080 $count_status = $lp_user_items_db->count_status_by_items( $filter );
1081
1082 $count_users_attend_courses_of_author = 0;
1083 $courses_of_author = 0;
1084 if ( $user->can_create_course() ) {
1085 // Get total users attend course of author
1086 $filter_count_users = $lp_user_items_db->count_user_attend_courses_of_author( $user_id );
1087 $count_users_attend_courses_of_author = $lp_user_items_db->get_user_courses( $filter_count_users );
1088
1089 // Get total courses publish of author
1090 $filter_count_courses = $lp_course_db->count_courses_of_author( $user_id, [ 'publish' ] );
1091 $courses_of_author = $lp_course_db->get_courses( $filter_count_courses );
1092 }
1093
1094 $statistic['enrolled_courses'] = intval( $count_status->{LP_COURSE_PURCHASED} ?? 0 ) + intval( $count_status->{LP_COURSE_ENROLLED} ?? 0 ) + intval( $count_status->{LP_COURSE_FINISHED} ?? 0 );
1095 $statistic['active_courses'] = $count_status->{LP_COURSE_GRADUATION_IN_PROGRESS} ?? 0;
1096 $statistic['completed_courses'] = $count_status->{LP_COURSE_FINISHED} ?? 0;
1097 $statistic['total_courses'] = $courses_of_author;
1098 $statistic['total_users'] = $count_users_attend_courses_of_author;
1099 } catch ( Throwable $e ) {
1100
1101 }
1102
1103 return apply_filters( 'lp/profile/statistic', $statistic, $user );
1104 }
1105
1106 /**
1107 * Get register fields custom
1108 *
1109 * @return mixed|null
1110 * @since 4.2.6.4
1111 */
1112 public static function get_register_fields_custom() {
1113 return apply_filters(
1114 'learn-press/profile/register-fields-custom',
1115 LP_Settings::get_option( 'register_profile_fields', [] )
1116 );
1117 }
1118
1119 /**
1120 * Get an instance of LP_Profile for a user id
1121 *
1122 * @param $user_id
1123 *
1124 * @return LP_Profile mixed
1125 * @throws Exception
1126 * @version 1.0.5
1127 * @since 3.0.0
1128 */
1129 public static function instance( $user_id = 0 ) {
1130 $is_page_profile = LP_Page_Controller::page_is( 'profile' );
1131
1132 if ( $is_page_profile && empty( $user_id ) ) {
1133 if ( empty( self::$_instance ) ) {
1134 $user_slug = (string) get_query_var( 'user' );
1135 if ( ! empty( $user_slug ) ) {
1136 $userModel = UserService::instance()->get_user_by_slug_link( $user_slug );
1137 if ( $userModel ) {
1138 $user_id = $userModel->get_id();
1139 }
1140 } else {
1141 $user_id = get_current_user_id();
1142 }
1143
1144 self::$_instance = new self( $user_id );
1145 }
1146
1147 return self::$_instance;
1148 } else {
1149 if ( empty( self::$_instances[ $user_id ] ) ) {
1150 self::$_instances[ $user_id ] = new self( $user_id );
1151 }
1152
1153 return self::$_instances[ $user_id ];
1154 }
1155 }
1156 }
1157 }
1158