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

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