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

1,217 lines 35.5 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 control 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 : LP_Helper::getUrlCurrent() );
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 * @deprecated 4.2.0
839 */
840 public function get_own_courses_filters( $current_filter = '' ) {
841 _deprecated_function( __METHOD__, '4.2.0' );
842 $url = $this->get_current_url();
843
844 $defaults = array(
845 'all' => sprintf( '<a href="%s">%s</a>', esc_url_raw( $url ), esc_html__( 'All', 'learnpress' ) ),
846 'publish' => sprintf( '<a href="%s">%s</a>', esc_url_raw( add_query_arg( 'filter-status', 'publish', $url ) ), esc_html__( 'Publish', 'learnpress' ) ),
847 'pending' => sprintf( '<a href="%s">%s</a>', esc_url_raw( add_query_arg( 'filter-status', 'pending', $url ) ), esc_html__( 'Pending', 'learnpress' ) ),
848 );
849
850 if ( ! $current_filter ) {
851 $keys = array_keys( $defaults );
852 $current_filter = reset( $keys );
853 }
854
855 foreach ( $defaults as $k => $v ) {
856 if ( $k === $current_filter ) {
857 $defaults[ $k ] = sprintf( '<span>%s</span>', strip_tags( $v ) );
858 }
859 }
860
861 return apply_filters(
862 'learn-press/profile/own-courses-filters',
863 $defaults
864 );
865 }
866
867 /**
868 * Get filters for purchased courses tab.
869 *
870 * @param string $current_filter
871 *
872 * @return array
873 * @deprecated 4.2.0
874 */
875 public function get_purchased_courses_filters( $current_filter = '' ) {
876 _deprecated_function( __METHOD__, '4.2.0' );
877 $url = $this->get_current_url( false );
878 $defaults = array(
879 'all' => sprintf( '<a href="%s">%s</a>', esc_url_raw( $url ), __( 'All', 'learnpress' ) ),
880 'finished' => sprintf( '<a href="%s">%s</a>', esc_url_raw( add_query_arg( 'filter-status', 'finished', $url ) ), __( 'Finished', 'learnpress' ) ),
881 'passed' => sprintf( '<a href="%s">%s</a>', esc_url_raw( add_query_arg( 'filter-status', 'passed', $url ) ), __( 'Passed', 'learnpress' ) ),
882 'failed' => sprintf( '<a href="%s">%s</a>', esc_url_raw( add_query_arg( 'filter-status', 'failed', $url ) ), __( 'Failed', 'learnpress' ) ),
883 'not-enrolled' => sprintf( '<a href="%s">%s</a>', esc_url_raw( add_query_arg( 'filter-status', 'not-enrolled', $url ) ), __( 'Not enrolled', 'learnpress' ) ),
884 );
885
886 if ( ! $current_filter ) {
887 $keys = array_keys( $defaults );
888 $current_filter = reset( $keys );
889 }
890
891 foreach ( $defaults as $k => $v ) {
892 if ( $k === $current_filter ) {
893 $defaults[ $k ] = sprintf( '<span>%s</span>', strip_tags( $v ) );
894 }
895 }
896
897 return apply_filters(
898 'learn-press/profile/purchased-courses-filters',
899 $defaults
900 );
901 }
902
903 /**
904 * Get filters for purchased courses tab.
905 *
906 * @param string $current_filter
907 *
908 * @return array
909 */
910 public function get_quizzes_filters( $current_filter = '' ) {
911 $url = $this->get_tab_link( 'quizzes' );
912 $defaults = array(
913 'all' => sprintf( '<a href="%s">%s</a>', esc_url_raw( $url ), __( 'All', 'learnpress' ) ),
914 'completed' => sprintf( '<a href="%s">%s</a>', esc_url_raw( add_query_arg( 'filter-status', 'completed', $url ) ), __( 'Finished', 'learnpress' ) ),
915 'passed' => sprintf( '<a href="%s">%s</a>', esc_url_raw( add_query_arg( 'filter-graduation', 'passed', $url ) ), __( 'Passed', 'learnpress' ) ),
916 'failed' => sprintf( '<a href="%s">%s</a>', esc_url_raw( add_query_arg( 'filter-graduation', 'failed', $url ) ), __( 'Failed', 'learnpress' ) ),
917 );
918
919 if ( ! $current_filter ) {
920 $keys = array_keys( $defaults );
921 $current_filter = reset( $keys );
922 }
923
924 foreach ( $defaults as $k => $v ) {
925 if ( $k === $current_filter ) {
926 $defaults[ $k ] = sprintf( '<span>%s</span>', strip_tags( $v ) );
927 }
928 }
929
930 return apply_filters(
931 'learn-press/profile/quizzes-filters',
932 $defaults
933 );
934 }
935
936 /**
937 * @param bool $redirect
938 *
939 * @return string
940 * @deprecated 4.1.7.3
941 */
942 public function logout_url( $redirect = false ) {
943 _deprecated_function( __FUNCTION__, '4.1.7.3' );
944 /*if ( $this->enable_login() ) {
945 $profile_url = learn_press_get_page_link( 'profile' );
946 $url = esc_url_raw(
947 add_query_arg(
948 array(
949 'lp-logout' => 'true',
950 'nonce' => wp_create_nonce( 'lp-logout' ),
951 ),
952 untrailingslashit( $profile_url )
953 )
954 );
955
956 if ( $redirect !== false ) {
957 $url = esc_url_raw( add_query_arg( 'redirect', urlencode( $redirect ), $url ) );
958 }
959 } else {
960 $url = wp_logout_url( $redirect !== false ? $redirect : $this->get_current_url() );
961 }
962
963 return apply_filters( 'learn-press/logout-url', $url );*/
964 }
965
966 /**
967 * Echo class for main div.
968 *
969 * @param bool $echo
970 * @param string $more
971 *
972 * @return string
973 */
974 public function main_class( $echo = true, $more = '' ) {
975 $classes = array( 'lp-user-profile' );
976
977 if ( $this->is_current_user() ) {
978 $classes[] = 'current-user';
979 }
980
981 if ( ! is_user_logged_in() ) {
982 $classes[] = 'guest';
983 }
984
985 if ( has_action( 'learn-press/before-user-profile' ) ) {
986 $classes[] = 'has-sidebar';
987 }
988
989 $classes = LP_Helper::merge_class( $classes, $more );
990
991 $class = ' class="' . implode( ' ', apply_filters( 'learn-press/profile/class', $classes ) ) . '"';
992
993 if ( $echo ) {
994 echo wp_kses_post( $class );
995 }
996
997 return $class;
998 }
999
1000 /**
1001 * Return true if the tab is visible for current user.
1002 *
1003 * @param string $tab_key
1004 * @param array $tab_data
1005 *
1006 * @return bool
1007 */
1008 public function tab_is_visible_for_user( $tab_key, $tab_data = null ) {
1009 return $this->is_current_tab( $tab_key ) && $this->current_user_can( "view-tab-{$tab_key}" );
1010 }
1011
1012 /**
1013 * Return true if the section is visible for current user.
1014 *
1015 * @param string $section_key
1016 * @param array $section_data
1017 *
1018 * @return bool
1019 */
1020 public function section_is_visible_for_user( $section_key, $section_data = array() ) {
1021 return $this->current_user_can( "view-section-{$section_key}" ) && ! $this->is_hidden( $section_data );
1022 }
1023
1024 /**
1025 * TRUE if enable show login form in user profile if user is not logged in.
1026 *
1027 * @return bool
1028 */
1029 public function enable_login() {
1030 return 'yes' === LP_Settings::instance()->get( 'enable_login_profile' );
1031 }
1032
1033 /**
1034 * TRUE if enable show register form in user profile if user is not logged in.
1035 *
1036 * @return bool
1037 */
1038 public function enable_register() {
1039 return 'yes' === LP_Settings::instance()->get( 'enable_register_profile' );
1040 }
1041
1042 /**
1043 * Get queried user in profile link.
1044 *
1045 * @param string $return
1046 *
1047 * @return false|WP_User
1048 * @since 3.0.0
1049 */
1050 public static function get_queried_user( $return = '' ) {
1051 global $wp_query;
1052
1053 if ( isset( $wp_query->query['user'] ) ) {
1054 $user = get_user_by( 'login', urldecode( $wp_query->query['user'] ) );
1055 } else {
1056 $user = get_user_by( 'id', get_current_user_id() );
1057 }
1058
1059 return $return === 'id' && $user ? $user->ID : $user;
1060 }
1061
1062 /**
1063 * Return true if there is a name of user in profile link.
1064 *
1065 * @return bool
1066 */
1067 public static function is_queried_user() {
1068 global $wp_query;
1069
1070 return isset( $wp_query->query['user'] ) ? $wp_query->query['user'] : false;
1071 }
1072
1073 public function get_upload_profile_src( $size = '' ) {
1074 $user = $this->get_user();
1075 $uploaded_profile_src = $user->get_data( 'uploaded_profile_src' );
1076
1077 if ( empty( $uploaded_profile_src ) ) {
1078 $profile_picture = get_user_meta( $user->get_id(), '_lp_profile_picture', true );
1079
1080 if ( $profile_picture ) {
1081 $upload = learn_press_user_profile_picture_upload_dir();
1082 $file_path = $upload['basedir'] . DIRECTORY_SEPARATOR . $profile_picture;
1083
1084 if ( file_exists( $file_path ) ) {
1085 $uploaded_profile_src = $upload['baseurl'] . '/' . $profile_picture;
1086
1087 // if ( $user->get_data( 'profile_picture_changed' ) == 'yes' ) {
1088 // $uploaded_profile_src = add_query_arg(
1089 // 'r',
1090 // md5( rand( 0, 10 ) / rand( 1, 1000000 ) ),
1091 // $user->get_data( 'uploaded_profile_src' )
1092 // );
1093 // delete_user_meta( $user->get_id(), '_lp_profile_picture_changed' );
1094 // }
1095 } else {
1096 $uploaded_profile_src = false;
1097 }
1098
1099 $user->set_data( 'uploaded_profile_src', $uploaded_profile_src );
1100 }
1101 }
1102
1103 return $uploaded_profile_src;
1104 }
1105
1106 public function get_profile_picture( $type = '', $size = 96 ) {
1107 $user = $this->get_user();
1108 $args = learn_press_get_avatar_thumb_size();
1109
1110 if ( $type == 'gravatar' ) {
1111 remove_filter( 'pre_get_avatar', 'learn_press_pre_get_avatar_callback', 1 );
1112 }
1113
1114 $profile_picture_src = $this->get_upload_profile_src();
1115
1116 if ( $profile_picture_src ) {
1117 $user->set_data( 'profile_picture_src', $profile_picture_src );
1118 }
1119
1120 $avatar = get_avatar( $user->get_id(), $args['width'], '', esc_attr__( 'User Avatar', 'learnpress' ), $args );
1121
1122 if ( $type == 'gravatar' ) {
1123 add_filter( 'pre_get_avatar', 'learn_press_pre_get_avatar_callback', 1, 5 );
1124 }
1125
1126 return $avatar;
1127 }
1128
1129 /**
1130 * Get option enable "Publish profile"
1131 *
1132 * @return string
1133 */
1134 public static function get_option_publish_profile(): string {
1135 return LP_Settings::get_option( 'publish_profile', 'no' );
1136 }
1137
1138 /**
1139 * Get statistic info of user
1140 *
1141 * @return array
1142 * @since 4.1.6
1143 * @version 1.0.0
1144 */
1145 public function get_statistic_info(): array {
1146 $user = $this->_user;
1147 $statistic = array(
1148 'enrolled_courses' => 0,
1149 'active_courses' => 0,
1150 'completed_courses' => 0,
1151 'total_courses' => 0,
1152 'total_users' => 0,
1153 );
1154
1155 try {
1156 if ( ! $user ) {
1157 throw new Exception( 'The user is invalid' );
1158 }
1159
1160 $user_id = $user->get_id();
1161 $lp_user_items_db = LP_User_Items_DB::getInstance();
1162 $lp_course_db = LP_Course_DB::getInstance();
1163
1164 // Count status
1165 $filter = new LP_User_Items_Filter();
1166 $filter->user_id = $user_id;
1167 $count_status = $lp_user_items_db->count_status_by_items( $filter );
1168
1169 $count_users_attend_courses_of_author = 0;
1170 $courses_of_author = 0;
1171 if ( $user->can_create_course() ) {
1172 // Get total users attend course of author
1173 $filter_count_users = $lp_user_items_db->count_user_attend_courses_of_author( $user_id );
1174 $count_users_attend_courses_of_author = $lp_user_items_db->get_user_courses( $filter_count_users );
1175
1176 // Get total courses publish of author
1177 $filter_count_courses = $lp_course_db->count_courses_publish_of_author( $user_id );
1178 $courses_of_author = $lp_course_db->get_courses( $filter_count_courses );
1179 }
1180
1181 $statistic['enrolled_courses'] = intval( $count_status->{LP_COURSE_PURCHASED} ?? 0 ) + intval( $count_status->{LP_COURSE_ENROLLED} ?? 0 ) + intval( $count_status->{LP_COURSE_FINISHED} ?? 0 );
1182 $statistic['active_courses'] = $count_status->{LP_COURSE_GRADUATION_IN_PROGRESS} ?? 0;
1183 $statistic['completed_courses'] = $count_status->{LP_COURSE_FINISHED} ?? 0;
1184 $statistic['total_courses'] = $courses_of_author;
1185 $statistic['total_users'] = $count_users_attend_courses_of_author;
1186 } catch ( Throwable $e ) {
1187
1188 }
1189
1190 return apply_filters( 'lp/profile/statistic', $statistic, $user );
1191 }
1192
1193 /**
1194 * Get an instance of LP_Profile for a user id
1195 *
1196 * @param $user_id
1197 *
1198 * @return LP_Profile mixed
1199 */
1200 public static function instance( $user_id = 0 ) {
1201 if ( ! $user_id ) {
1202 $user_id = self::get_queried_user( 'id' );
1203
1204 if ( ! $user_id ) {
1205 $user_id = get_current_user_id();
1206 }
1207 }
1208
1209 if ( empty( self::$_instances[ $user_id ] ) ) {
1210 self::$_instances[ $user_id ] = new self( $user_id );
1211 }
1212
1213 return self::$_instances[ $user_id ];
1214 }
1215 }
1216 }
1217