PluginProbe
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses / 4.4.4
LearnPress – WordPress LMS Plugin for Create and Sell Online Courses v4.4.4
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.4.4, at inc/user/class-lp-profile.php

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