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

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