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