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

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

1,159 lines 30.6 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 LP_ORDER_REFUNDED_DB,
661 ],
662 //'post__in' => array_keys( $order_ids ),
663 //'orderby' => 'post__in',
664 'fields' => 'ids',
665 'meta_query' => array(
666 'relation' => 'OR',
667 array(
668 'key' => '_user_id',
669 'value' => $this->get_user_data( 'id' ),
670 'compare' => '=',
671 ),
672 array(
673 'key' => '_user_id',
674 'value' => '"' . $this->get_user_data( 'id' ) . '"',
675 'compare' => 'LIKE',
676 ),
677 ),
678 )
679 );
680
681 if ( $query_order->have_posts() ) {
682 $orders = ( isset( $args['fields'] ) && 'ids' === $args['fields'] ) ? $query_order->posts : array_filter( array_map( 'learn_press_get_order', $query_order->posts ) );
683
684 $query['orders'] = $orders;
685 $query['total'] = $query_order->found_posts;
686 $query['num_pages'] = $query_order->max_num_pages;
687 $query['pagination'] = learn_press_paging_nav(
688 array(
689 'num_pages' => $query['num_pages'],
690 'base' => $this->get_tab_link( LP_Settings::instance()->get( 'profile_endpoints.orders' ) ),
691 'format' => $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( '%#%', '' ) : '?paged=%#%',
692 'echo' => false,
693 'paged' => $args['paged'],
694 )
695 );
696
697 $query = new LP_Query_List_Table(
698 array(
699 'total' => $query_order->found_posts,
700 'paged' => $args['paged'],
701 'limit' => $args['limit'],
702 'pages' => $query['num_pages'],
703 'items' => $orders,
704 )
705 );
706 } else {
707 $query = new LP_Query_List_Table( [] );
708 }
709
710 return $query;
711 }
712
713 /**
714 * Query user's courses
715 *
716 * @param string $type - Optional. [own, purchased, enrolled, etc]
717 * @param array $args - Optional.
718 *
719 * @return LP_Query_List_Table
720 * @throws Exception
721 */
722 public function query_courses( string $type = 'own', array $args = array() ): LP_Query_List_Table {
723 $lp_user_items_db = LP_User_Items_DB::getInstance();
724 $courses = array();
725
726 switch ( $type ) {
727 case 'purchased':
728 $filter = new UserItemsFilter();
729 $filter->only_fields = array( 'DISTINCT (item_id) AS item_id', 'ui.user_item_id' );
730 $filter->field_count = 'ui.item_id';
731 $filter->user_id = $this->get_user_data( 'id' );
732 $filter->order_by = 'ui.user_item_id';
733 $filter->order = 'DESC';
734 $filter->item_type = LP_COURSE_CPT;
735 $status = $args['status'] ?? '';
736
737 switch ( $status ) {
738 case '':
739 $filter->where[] = $lp_user_items_db->wpdb->prepare(
740 "AND ui.status != '%s'",
741 UserItemModel::STATUS_CANCEL
742 );
743 break;
744 case UserItemModel::STATUS_FINISHED:
745 $filter->status = UserItemModel::STATUS_FINISHED;
746 break;
747 case UserItemModel::GRADUATION_IN_PROGRESS:
748 case UserItemModel::GRADUATION_PASSED:
749 case UserItemModel::GRADUATION_FAILED:
750 $filter->graduation = $status;
751 break;
752 }
753
754 $filter->page = $args['paged'] ?? 1;
755 $filter->limit = $args['limit'] ?? $filter->limit;
756 $total_rows = 0;
757 $filter = apply_filters( 'lp/api/profile/courses/purchased/filter', $filter, $args );
758 $result_courses = UserItemsDB::getInstance()->get_user_items( $filter, $total_rows );
759
760 $course_ids = LP_Database::get_values_by_key( $result_courses, 'item_id' );
761
762 $courses = array(
763 'total' => $total_rows,
764 'paged' => $filter->page,
765 'limit' => $filter->limit,
766 'items' => $course_ids,
767 );
768 break;
769 case 'own':
770 //$query = $this->_curd->query_own_courses( $this->get_user_data( 'id' ), $args );
771 $filter = new LP_Course_Filter();
772 if ( empty( $args['status'] ) ) {
773 $args['status'] = [ 'publish', 'pending', 'private' ];
774 }
775
776 if ( ! is_array( $args['status'] ) ) {
777 $args['status'] = (array) $args['status'];
778 }
779
780 Courses::handle_params_for_query_courses( $filter, $args );
781 $filter->fields = [ 'ID' ];
782 $filter->post_author = $this->get_user_data( 'id' );
783 $filter->post_status = $args['status'];
784 $filter->page = $args['paged'] ?? 1;
785 $filter->limit = $args['limit'] ?? $filter->limit;
786 $total_rows = 0;
787 $filter = apply_filters( 'lp/api/profile/courses/own/filter', $filter, $args );
788 $result_courses = Courses::get_courses( $filter, $total_rows );
789
790 $course_ids = LP_Database::get_values_by_key( $result_courses );
791
792 $courses = array(
793 'total' => $total_rows,
794 'paged' => $filter->page,
795 'limit' => $filter->limit,
796 'items' => $course_ids,
797 );
798 break;
799 }
800
801 return new LP_Query_List_Table( $courses );
802 }
803
804 /**
805 * Get the order is viewing details.
806 */
807 public function get_view_order() {
808 global $wp_query;
809
810 $order = false;
811 if ( isset( $wp_query->query_vars['view_id'] ) ) {
812 $order = learn_press_get_order( $wp_query->query_vars['view_id'] );
813 }
814
815 return $order;
816 }
817
818 /**
819 * Get filters for purchased courses tab.
820 *
821 * @param string $current_filter
822 *
823 * @return array
824 */
825 public function get_quizzes_filters( $current_filter = '' ) {
826 $url = $this->get_tab_link( 'quizzes' );
827 $defaults = array(
828 'all' => sprintf( '<a href="%s">%s</a>', esc_url_raw( $url ), __( 'All', 'learnpress' ) ),
829 'completed' => sprintf( '<a href="%s">%s</a>', esc_url_raw( add_query_arg( 'filter-status', 'completed', $url ) ), __( 'Finished', 'learnpress' ) ),
830 'passed' => sprintf( '<a href="%s">%s</a>', esc_url_raw( add_query_arg( 'filter-graduation', 'passed', $url ) ), __( 'Passed', 'learnpress' ) ),
831 'failed' => sprintf( '<a href="%s">%s</a>', esc_url_raw( add_query_arg( 'filter-graduation', 'failed', $url ) ), __( 'Failed', 'learnpress' ) ),
832 );
833
834 if ( ! $current_filter ) {
835 $keys = array_keys( $defaults );
836 $current_filter = reset( $keys );
837 }
838
839 foreach ( $defaults as $k => $v ) {
840 if ( $k === $current_filter ) {
841 $defaults[ $k ] = sprintf( '<span>%s</span>', strip_tags( $v ) );
842 }
843 }
844
845 return apply_filters(
846 'learn-press/profile/quizzes-filters',
847 $defaults
848 );
849 }
850
851 /**
852 * Echo class for main div.
853 *
854 * @param bool $echo
855 * @param string $more
856 *
857 * @return string
858 */
859 public function main_class( $echo = true, $more = '' ) {
860 $classes = array( 'lp-user-profile' );
861
862 if ( $this->is_current_user() ) {
863 $classes[] = 'current-user';
864 }
865
866 if ( ! is_user_logged_in() ) {
867 $classes[] = 'guest';
868 }
869
870 if ( has_action( 'learn-press/before-user-profile' ) ) {
871 $classes[] = 'has-sidebar';
872 }
873
874 $classes = LP_Helper::merge_class( $classes, $more );
875
876 $class = ' class="' . implode( ' ', apply_filters( 'learn-press/profile/class', $classes ) ) . '"';
877
878 if ( $echo ) {
879 echo wp_kses_post( $class );
880 }
881
882 return $class;
883 }
884
885 /**
886 * Return true if the tab is visible for current user.
887 *
888 * @param string $tab_key
889 * @param array $tab_data
890 *
891 * @return bool
892 */
893 public function tab_is_visible_for_user( $tab_key, $tab_data = null ) {
894 return $this->is_current_tab( $tab_key ) && $this->current_user_can( "view-tab-{$tab_key}" );
895 }
896
897 /**
898 * Return true if the section is visible for current user.
899 *
900 * @param string $section_key
901 * @param array $section_data
902 *
903 * @return bool
904 */
905 public function section_is_visible_for_user( $section_key, $section_data = array() ) {
906 return $this->current_user_can( "view-section-{$section_key}" ) && ! $this->is_hidden( $section_data );
907 }
908
909 /**
910 * Get queried user in profile link.
911 *
912 * @param string $return
913 *
914 * @return false|WP_User
915 * @since 3.0.0
916 * @deprecated 4.2.9.1
917 */
918 public static function get_queried_user( $return = '' ) {
919 _deprecated_function( __METHOD__, '4.2.9.1', 'LP_Profile::instance()->get_user_current()' );
920 global $wp_query;
921
922 if ( isset( $wp_query->query['user'] ) ) {
923 $user = get_user_by( 'login', urldecode( $wp_query->query['user'] ) );
924 } else {
925 $user = get_user_by( 'id', get_current_user_id() );
926 }
927
928 return $return === 'id' && $user ? $user->ID : $user;
929 }
930
931 public function get_upload_profile_src( $size = '' ) {
932 $user = $this->get_user();
933 if ( ! $user ) {
934 return '';
935 }
936
937 $uploaded_profile_src = $user->get_data( 'uploaded_profile_src' );
938
939 if ( empty( $uploaded_profile_src ) ) {
940 $profile_picture = get_user_meta( $user->get_id(), '_lp_profile_picture', true );
941
942 if ( $profile_picture ) {
943 // Check if hase slug / at the beginning of the path, if not, add it.
944 $slash = substr( $profile_picture, 0, 1 ) === '/' ? '' : '/';
945 $profile_picture = $slash . $profile_picture;
946 // End check.
947 $upload = learn_press_user_profile_picture_upload_dir();
948 $file_path = $upload['basedir'] . $profile_picture;
949
950 if ( file_exists( $file_path ) ) {
951 $uploaded_profile_src = $upload['baseurl'] . $profile_picture;
952 } else {
953 $uploaded_profile_src = false;
954 }
955
956 $user->set_data( 'uploaded_profile_src', $uploaded_profile_src );
957 }
958 }
959
960 return apply_filters( 'learn-press/profile/get-upload-profile-src', $uploaded_profile_src, $user->get_id() );
961 }
962
963 /**
964 * Get profile cover image
965 * @return string image url if exist
966 */
967 public function get_cover_image_src() {
968 $user = $this->get_user();
969 if ( ! $user ) {
970 return '';
971 }
972 $cover_image_src = '';
973 $image_path = get_user_meta( $user->get_id(), '_lp_profile_cover_image', true );
974
975 if ( $image_path ) {
976 // Check if hase slug / at the beginning of the path, if not, add it.
977 $slash = substr( $image_path, 0, 1 ) === '/' ? '' : '/';
978 $image_path = $slash . $image_path;
979 // End check.
980 $upload = learn_press_user_profile_picture_upload_dir();
981 $file_path = $upload['basedir'] . $image_path;
982
983 if ( file_exists( $file_path ) ) {
984 $cover_image_src = $upload['baseurl'] . $image_path;
985 } else {
986 $cover_image_src = '';
987 }
988 }
989
990 return apply_filters( 'learn-press/profile/get-profile-cover-image-src', $cover_image_src, $user->get_id() );
991 }
992
993 /**
994 * Get profile image of user.
995 *
996 * @param $type
997 * @param $size
998 *
999 * @return string
1000 */
1001 public function get_profile_picture( $type = '', $size = 96 ): string {
1002 $avatar = '';
1003
1004 try {
1005 $user = $this->get_user();
1006 $args = [
1007 'width' => $size,
1008 'height' => $size,
1009 ];
1010 if ( 96 === $size ) {
1011 $args = learn_press_get_avatar_thumb_size();
1012 }
1013
1014 $avatar_url = $this->get_upload_profile_src();
1015 if ( ! empty( $avatar_url ) ) {
1016 $user->set_data( 'profile_picture_src', $avatar_url );
1017 } else {
1018 $avatar_url = get_avatar_url( $user->get_id(), $args );
1019 if ( empty( $avatar_url ) ) {
1020 $avatar_url = LP_PLUGIN_URL . 'assets/images/avatar-default.png';
1021 }
1022 }
1023
1024 $avatar = apply_filters(
1025 'learn-press/user-profile/avatar',
1026 sprintf(
1027 '<img alt="%s" class="avatar" src="%s" width="%d" height="%d" />',
1028 esc_attr__( 'User Avatar', 'learnpress' ),
1029 $avatar_url,
1030 $args['width'] ?? 96,
1031 $args['height'] ?? 96
1032 ),
1033 $avatar_url,
1034 $args
1035 );
1036 } catch ( Throwable $e ) {
1037 error_log( $e->getMessage() );
1038 }
1039
1040 return $avatar;
1041 }
1042
1043 /**
1044 * Get option enable "Publish profile"
1045 *
1046 * @return string
1047 */
1048 public static function get_option_publish_profile(): string {
1049 return LP_Settings::get_option( 'publish_profile', 'no' );
1050 }
1051
1052 /**
1053 * Get statistic info of user
1054 *
1055 * @return array
1056 * @since 4.1.6
1057 * @version 1.0.0
1058 */
1059 public function get_statistic_info(): array {
1060 $user = $this->_user;
1061 $statistic = array(
1062 'enrolled_courses' => 0,
1063 'active_courses' => 0,
1064 'completed_courses' => 0,
1065 'total_courses' => 0,
1066 'total_users' => 0,
1067 );
1068
1069 try {
1070 if ( ! $user ) {
1071 throw new Exception( 'The user is invalid' );
1072 }
1073
1074 $user_id = $user->get_id();
1075 $lp_user_items_db = LP_User_Items_DB::getInstance();
1076 $lp_course_db = LP_Course_DB::getInstance();
1077
1078 // Count status
1079 $filter = new LP_User_Items_Filter();
1080 $filter->user_id = $user_id;
1081 $count_status = $lp_user_items_db->count_status_by_items( $filter );
1082
1083 $count_users_attend_courses_of_author = 0;
1084 $courses_of_author = 0;
1085 if ( $user->can_create_course() ) {
1086 // Get total users attend course of author
1087 $filter_count_users = $lp_user_items_db->count_user_attend_courses_of_author( $user_id );
1088 $count_users_attend_courses_of_author = $lp_user_items_db->get_user_courses( $filter_count_users );
1089
1090 // Get total courses publish of author
1091 $filter_count_courses = $lp_course_db->count_courses_of_author( $user_id, [ 'publish' ] );
1092 $courses_of_author = $lp_course_db->get_courses( $filter_count_courses );
1093 }
1094
1095 $statistic['enrolled_courses'] = intval( $count_status->{LP_COURSE_PURCHASED} ?? 0 ) + intval( $count_status->{LP_COURSE_ENROLLED} ?? 0 ) + intval( $count_status->{LP_COURSE_FINISHED} ?? 0 );
1096 $statistic['active_courses'] = $count_status->{LP_COURSE_GRADUATION_IN_PROGRESS} ?? 0;
1097 $statistic['completed_courses'] = $count_status->{LP_COURSE_FINISHED} ?? 0;
1098 $statistic['total_courses'] = $courses_of_author;
1099 $statistic['total_users'] = $count_users_attend_courses_of_author;
1100 } catch ( Throwable $e ) {
1101
1102 }
1103
1104 return apply_filters( 'lp/profile/statistic', $statistic, $user );
1105 }
1106
1107 /**
1108 * Get register fields custom
1109 *
1110 * @return mixed|null
1111 * @since 4.2.6.4
1112 */
1113 public static function get_register_fields_custom() {
1114 return apply_filters(
1115 'learn-press/profile/register-fields-custom',
1116 LP_Settings::get_option( 'register_profile_fields', [] )
1117 );
1118 }
1119
1120 /**
1121 * Get an instance of LP_Profile for a user id
1122 *
1123 * @param $user_id
1124 *
1125 * @return LP_Profile mixed
1126 * @throws Exception
1127 * @version 1.0.5
1128 * @since 3.0.0
1129 */
1130 public static function instance( $user_id = 0 ) {
1131 $is_page_profile = LP_Page_Controller::page_is( 'profile' );
1132
1133 if ( $is_page_profile && empty( $user_id ) ) {
1134 if ( empty( self::$_instance ) ) {
1135 $user_slug = (string) get_query_var( 'user' );
1136 if ( ! empty( $user_slug ) ) {
1137 $userModel = UserService::instance()->get_user_by_slug_link( $user_slug );
1138 if ( $userModel ) {
1139 $user_id = $userModel->get_id();
1140 }
1141 } else {
1142 $user_id = get_current_user_id();
1143 }
1144
1145 self::$_instance = new self( $user_id );
1146 }
1147
1148 return self::$_instance;
1149 } else {
1150 if ( empty( self::$_instances[ $user_id ] ) ) {
1151 self::$_instances[ $user_id ] = new self( $user_id );
1152 }
1153
1154 return self::$_instances[ $user_id ];
1155 }
1156 }
1157 }
1158 }
1159