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