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