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

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