| @@ -9,15 +9,16 @@ | ||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | namespace TUTOR; |
| 12 | 12 | |
| 13 | -defined( 'ABSPATH' ) || exit; | |
| 14 | - | |
| 15 | 13 | use Tutor\Helpers\HttpHelper; |
| 16 | 14 | use Tutor\Models\UserModel; |
| 17 | 15 | use Tutor\Traits\JsonResponse; |
| 18 | -use TUTOR\InstructorList; | |
| 19 | 16 | |
| 17 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 18 | + exit; | |
| 19 | +} | |
| 20 | + | |
| 20 | 21 | /** |
| 21 | 22 | * User class |
| 22 | 23 | * |
| 23 | 24 | * @since 1.0.0 |
| @@ -31,42 +32,19 @@ | ||
| 31 | 32 | |
| 32 | 33 | /** |
| 33 | 34 | * User meta keys. |
| 34 | 35 | */ |
| 35 | - const REVIEW_POPUP_META = 'tutor_review_course_popup'; | |
| 36 | - const LAST_LOGIN_META = 'tutor_last_login'; | |
| 37 | - const TIMEZONE_META = '_tutor_timezone'; | |
| 38 | - const PROFILE_PHOTO_META = '_tutor_profile_photo'; | |
| 39 | - const PHONE_NUMBER_META = 'phone_number'; | |
| 40 | - const COVER_PHOTO_META = '_tutor_cover_photo'; | |
| 41 | - const PROFILE_BIO_META = '_tutor_profile_bio'; | |
| 42 | - const PROFILE_JOB_TITLE_META = '_tutor_profile_job_title'; | |
| 43 | - const TUTOR_STUDENT_META = '_is_tutor_student'; | |
| 44 | - const TOUR_COMPLETED_META = '_tutor_tour_completed'; | |
| 45 | - const APPLICATION_SOURCE_META = '_tutor_application_source'; | |
| 46 | - const INSTRUCTOR_APPROVAL_NOTICE_META = 'tutor_instructor_show_approval_message'; | |
| 36 | + const REVIEW_POPUP_META = 'tutor_review_course_popup'; | |
| 37 | + const LAST_LOGIN_META = 'tutor_last_login'; | |
| 38 | + const TIMEZONE_META = '_tutor_timezone'; | |
| 39 | + const PROFILE_PHOTO_META = '_tutor_profile_photo'; | |
| 40 | + const PHONE_NUMBER_META = 'phone_number'; | |
| 41 | + const COVER_PHOTO_META = '_tutor_cover_photo'; | |
| 42 | + const PROFILE_BIO_META = '_tutor_profile_bio'; | |
| 43 | + const PROFILE_JOB_TITLE_META = '_tutor_profile_job_title'; | |
| 44 | + const TUTOR_STUDENT_META = '_is_tutor_student'; | |
| 47 | 45 | |
| 48 | - const SOURCE_INSTRUCTOR_REGISTRATION = 'instructor_registration'; | |
| 49 | - const SOURCE_STUDENT_DASHBOARD = 'student_dashboard'; | |
| 50 | - | |
| 51 | 46 | /** |
| 52 | - * View as constants | |
| 53 | - * | |
| 54 | - * @since 4.0.0 | |
| 55 | - */ | |
| 56 | - const VIEW_AS_INSTRUCTOR = 'instructor'; | |
| 57 | - const VIEW_AS_STUDENT = 'student'; | |
| 58 | - | |
| 59 | - /** | |
| 60 | - * User meta key for storing view as mode | |
| 61 | - * | |
| 62 | - * @since 4.0.0 | |
| 63 | - * | |
| 64 | - * @var string | |
| 65 | - */ | |
| 66 | - const VIEW_MODE_USER_META = 'tutor_profile_view_mode'; | |
| 67 | - | |
| 68 | - /** | |
| 69 | 47 | * User model |
| 70 | 48 | * |
| 71 | 49 | * @since 3.0.0 |
| 72 | 50 | * |
| @@ -114,12 +92,8 @@ | ||
| 114 | 92 | add_action( 'login_form', array( $this, 'add_timezone_input' ) ); |
| 115 | 93 | add_action( 'wp_login', array( $this, 'set_timezone' ), 10, 2 ); |
| 116 | 94 | |
| 117 | 95 | add_action( 'wp_ajax_tutor_user_list', array( $this, 'ajax_user_list' ) ); |
| 118 | - add_action( 'wp_ajax_tutor_switch_profile', array( $this, 'ajax_switch_profile' ) ); | |
| 119 | - add_action( 'wp_ajax_tutor_complete_tour', array( $this, 'ajax_complete_tour' ) ); | |
| 120 | - | |
| 121 | - add_filter( 'retrieve_password_message', array( $this, 'maybe_update_password_reset_link' ), 10, 3 ); | |
| 122 | 96 | } |
| 123 | 97 | |
| 124 | 98 | /** |
| 125 | 99 | * Get meta key name for review popup. |
| @@ -147,28 +121,8 @@ | ||
| 147 | 121 | return current_user_can( $role ); |
| 148 | 122 | } |
| 149 | 123 | |
| 150 | 124 | /** |
| 151 | - * Check current user has capability. | |
| 152 | - * | |
| 153 | - * Example usage: | |
| 154 | - * | |
| 155 | - * User::can( 'edit_posts' ); | |
| 156 | - * User::can( 'edit_post', $post->ID ); | |
| 157 | - * User::can( 'edit_post_meta', $post->ID, $meta_key ); | |
| 158 | - * | |
| 159 | - * @since 4.0.0 | |
| 160 | - * | |
| 161 | - * @param string $capability capability. | |
| 162 | - * @param mixed ...$args args. | |
| 163 | - * | |
| 164 | - * @return boolean | |
| 165 | - */ | |
| 166 | - public static function can( string $capability = 'manage_options', ...$args ) { | |
| 167 | - return current_user_can( $capability, ...$args ); | |
| 168 | - } | |
| 169 | - | |
| 170 | - /** | |
| 171 | 125 | * Check user has any role. |
| 172 | 126 | * |
| 173 | 127 | * @since 2.2.0 |
| 174 | 128 | * @since 2.6.2 $user_id param added. |
| @@ -186,8 +140,9 @@ | ||
| 186 | 140 | |
| 187 | 141 | foreach ( $roles as $role ) { |
| 188 | 142 | if ( in_array( $role, $user->roles, true ) ) { |
| 189 | 143 | return true; |
| 144 | + break; | |
| 190 | 145 | } |
| 191 | 146 | } |
| 192 | 147 | |
| 193 | 148 | return false; |
| @@ -237,78 +192,8 @@ | ||
| 237 | 192 | return tutils()->is_instructor( $user_id, $is_approved ); |
| 238 | 193 | } |
| 239 | 194 | |
| 240 | 195 | /** |
| 241 | - * Get Tutor application source for a user. | |
| 242 | - * | |
| 243 | - * @since 4.0.0 | |
| 244 | - * | |
| 245 | - * @param int $user_id user id. | |
| 246 | - * | |
| 247 | - * @return string | |
| 248 | - */ | |
| 249 | - public static function get_application_source( $user_id = 0 ): string { | |
| 250 | - return (string) get_user_meta( | |
| 251 | - tutor_utils()->get_user_id( $user_id ), | |
| 252 | - self::APPLICATION_SOURCE_META, | |
| 253 | - true | |
| 254 | - ); | |
| 255 | - } | |
| 256 | - | |
| 257 | - /** | |
| 258 | - * Check if the user came through instructor registration. | |
| 259 | - * | |
| 260 | - * @since 4.0.0 | |
| 261 | - * | |
| 262 | - * @param int $user_id user id. | |
| 263 | - * | |
| 264 | - * @return boolean | |
| 265 | - */ | |
| 266 | - public static function used_instructor_registration( $user_id = 0 ): bool { | |
| 267 | - return self::SOURCE_INSTRUCTOR_REGISTRATION === self::get_application_source( $user_id ); | |
| 268 | - } | |
| 269 | - | |
| 270 | - /** | |
| 271 | - * Check if a user can view instructor dashboard screens. | |
| 272 | - * | |
| 273 | - * @since 4.0.0 | |
| 274 | - * | |
| 275 | - * @param int $user_id user id. | |
| 276 | - * | |
| 277 | - * @return boolean | |
| 278 | - */ | |
| 279 | - public static function can_view_instructor_dashboard( $user_id = 0 ): bool { | |
| 280 | - $user_id = tutor_utils()->get_user_id( $user_id ); | |
| 281 | - | |
| 282 | - if ( self::is_admin( $user_id ) || self::is_instructor( $user_id ) ) { | |
| 283 | - return true; | |
| 284 | - } | |
| 285 | - | |
| 286 | - if ( ! self::used_instructor_registration( $user_id ) ) { | |
| 287 | - return false; | |
| 288 | - } | |
| 289 | - | |
| 290 | - return in_array( | |
| 291 | - tutor_utils()->instructor_status( $user_id, false ), | |
| 292 | - array( Instructors_List::STATUS_PENDING, Instructors_List::STATUS_APPROVED ), | |
| 293 | - true | |
| 294 | - ); | |
| 295 | - } | |
| 296 | - | |
| 297 | - /** | |
| 298 | - * Check if user has a pending instructor application. | |
| 299 | - * | |
| 300 | - * @since 4.0.0 | |
| 301 | - * | |
| 302 | - * @param int $user_id user id. | |
| 303 | - * | |
| 304 | - * @return boolean | |
| 305 | - */ | |
| 306 | - public static function has_pending_instructor_application( $user_id = 0 ): bool { | |
| 307 | - return Instructors_List::STATUS_PENDING === tutor_utils()->instructor_status( $user_id, false ); | |
| 308 | - } | |
| 309 | - | |
| 310 | - /** | |
| 311 | 196 | * Check current user is only instructor as admin also has instructor role. |
| 312 | 197 | * |
| 313 | 198 | * @since 3.2.0 |
| 314 | 199 | * |
| @@ -641,94 +526,8 @@ | ||
| 641 | 526 | } |
| 642 | 527 | } |
| 643 | 528 | |
| 644 | 529 | /** |
| 645 | - * Get profile settings data | |
| 646 | - * | |
| 647 | - * @since 4.0.0 | |
| 648 | - * | |
| 649 | - * @param int $user_id user id. | |
| 650 | - * | |
| 651 | - * @return array | |
| 652 | - */ | |
| 653 | - public static function get_profile_settings_data( $user_id = 0 ) { | |
| 654 | - $user_id = tutor_utils()->get_user_id( $user_id ); | |
| 655 | - $user = get_userdata( $user_id ); | |
| 656 | - | |
| 657 | - // Prepare profile pic. | |
| 658 | - $profile_placeholder = apply_filters( 'tutor_login_default_avatar', tutor()->url . 'assets/images/profile-photo.png' ); | |
| 659 | - $profile_photo_src = $profile_placeholder; | |
| 660 | - $profile_photo_id = get_user_meta( $user->ID, self::PROFILE_PHOTO_META, true ); | |
| 661 | - | |
| 662 | - if ( $profile_photo_id ) { | |
| 663 | - $url = wp_get_attachment_image_url( $profile_photo_id, 'full' ); | |
| 664 | - if ( ! empty( $url ) ) { | |
| 665 | - $profile_photo_src = $url; | |
| 666 | - } | |
| 667 | - } | |
| 668 | - | |
| 669 | - $timezone = self::get_user_timezone_string( $user ); | |
| 670 | - | |
| 671 | - // Prepare cover photo. | |
| 672 | - $cover_placeholder = tutor()->url . 'assets/images/cover-photo.webp'; | |
| 673 | - $cover_photo_src = $cover_placeholder; | |
| 674 | - $cover_photo_id = get_user_meta( $user->ID, self::COVER_PHOTO_META, true ); | |
| 675 | - | |
| 676 | - if ( $cover_photo_id ) { | |
| 677 | - $url = wp_get_attachment_image_url( $cover_photo_id, 'full' ); | |
| 678 | - if ( ! empty( $url ) ) { | |
| 679 | - $cover_photo_src = $url; | |
| 680 | - } | |
| 681 | - } | |
| 682 | - | |
| 683 | - // Prepare display name. | |
| 684 | - $public_display = array(); | |
| 685 | - $public_display['display_nickname'] = $user->nickname; | |
| 686 | - $public_display['display_username'] = $user->user_login; | |
| 687 | - | |
| 688 | - if ( ! empty( $user->first_name ) ) { | |
| 689 | - $public_display['display_firstname'] = $user->first_name; | |
| 690 | - } | |
| 691 | - | |
| 692 | - if ( ! empty( $user->last_name ) ) { | |
| 693 | - $public_display['display_lastname'] = $user->last_name; | |
| 694 | - } | |
| 695 | - | |
| 696 | - if ( ! empty( $user->first_name ) && ! empty( $user->last_name ) ) { | |
| 697 | - $public_display['display_firstlast'] = $user->first_name . ' ' . $user->last_name; | |
| 698 | - $public_display['display_lastfirst'] = $user->last_name . ' ' . $user->first_name; | |
| 699 | - } | |
| 700 | - | |
| 701 | - if ( ! in_array( $user->display_name, $public_display, true ) ) { // Only add this if it isn't duplicated elsewhere. | |
| 702 | - $public_display = array( 'display_displayname' => $user->display_name ) + $public_display; | |
| 703 | - } | |
| 704 | - | |
| 705 | - $public_display = array_map( 'trim', $public_display ); | |
| 706 | - $public_display = array_unique( $public_display ); | |
| 707 | - $max_filesize = floatval( ini_get( 'upload_max_filesize' ) ) * ( 1024 * 1024 ); | |
| 708 | - | |
| 709 | - $profile_bio = wp_kses_post( get_user_meta( $user->ID, self::PROFILE_BIO_META, true ) ); | |
| 710 | - $job_title = get_user_meta( $user->ID, self::PROFILE_JOB_TITLE_META, true ); | |
| 711 | - $phone = get_user_meta( $user->ID, self::PHONE_NUMBER_META, true ); | |
| 712 | - | |
| 713 | - return array( | |
| 714 | - 'user' => $user, | |
| 715 | - 'profile_placeholder' => $profile_placeholder, | |
| 716 | - 'profile_photo_src' => $profile_photo_src, | |
| 717 | - 'profile_photo_id' => $profile_photo_id, | |
| 718 | - 'cover_placeholder' => $cover_placeholder, | |
| 719 | - 'cover_photo_src' => $cover_photo_src, | |
| 720 | - 'cover_photo_id' => $cover_photo_id, | |
| 721 | - 'timezone' => $timezone, | |
| 722 | - 'public_display' => $public_display, | |
| 723 | - 'max_filesize' => $max_filesize, | |
| 724 | - 'profile_bio' => $profile_bio, | |
| 725 | - 'job_title' => $job_title, | |
| 726 | - 'phone_number' => $phone, | |
| 727 | - ); | |
| 728 | - } | |
| 729 | - | |
| 730 | - /** | |
| 731 | 530 | * Get user list with pagination & support |
| 732 | 531 | * search term |
| 733 | 532 | * |
| 734 | 533 | * @since 3.0.0 |
| @@ -777,173 +576,6 @@ | ||
| 777 | 576 | $this->json_response( |
| 778 | 577 | __( 'User list fetched successfully!', 'tutor' ), |
| 779 | 578 | $response |
| 780 | 579 | ); |
| 781 | - } | |
| 782 | - | |
| 783 | - /** | |
| 784 | - * Switch user profile ajax-handler | |
| 785 | - * | |
| 786 | - * @since 4.0.0 | |
| 787 | - * | |
| 788 | - * @return void send wp_json response | |
| 789 | - */ | |
| 790 | - public function ajax_switch_profile() { | |
| 791 | - tutor_utils()->checking_nonce(); | |
| 792 | - | |
| 793 | - $user_id = get_current_user_id(); | |
| 794 | - if ( ! self::can_switch_mode( $user_id ) ) { | |
| 795 | - $this->json_response( | |
| 796 | - tutor_utils()->error_message(), | |
| 797 | - null, | |
| 798 | - HttpHelper::STATUS_UNAUTHORIZED | |
| 799 | - ); | |
| 800 | - } | |
| 801 | - | |
| 802 | - $switch_mode = ''; | |
| 803 | - $switch_label = ''; | |
| 804 | - $current_mode = Input::post( 'current_mode' ); | |
| 805 | - | |
| 806 | - if ( ! in_array( $current_mode, array( self::VIEW_AS_INSTRUCTOR, self::VIEW_AS_STUDENT ), true ) ) { | |
| 807 | - $this->response_bad_request( tutor_utils()->error_message( 'invalid_req' ) ); | |
| 808 | - } | |
| 809 | - | |
| 810 | - if ( self::VIEW_AS_INSTRUCTOR === $current_mode ) { | |
| 811 | - $switch_mode = self::VIEW_AS_STUDENT; | |
| 812 | - $switch_label = __( 'Student', 'tutor' ); | |
| 813 | - } elseif ( self::VIEW_AS_STUDENT === $current_mode ) { | |
| 814 | - $switch_mode = self::VIEW_AS_INSTRUCTOR; | |
| 815 | - $switch_label = __( 'Instructor', 'tutor' ); | |
| 816 | - } | |
| 817 | - | |
| 818 | - update_user_meta( $user_id, self::VIEW_MODE_USER_META, $switch_mode ); | |
| 819 | - | |
| 820 | - // translators:%s for switching mode. | |
| 821 | - $this->response_success( sprintf( __( 'Profile switched to %s!', 'tutor' ), $switch_label ) ); | |
| 822 | - } | |
| 823 | - | |
| 824 | - /** | |
| 825 | - * Get current view mode STUDENT/INSTRUCTOR | |
| 826 | - * | |
| 827 | - * @since 4.0.0 | |
| 828 | - * | |
| 829 | - * @return string | |
| 830 | - */ | |
| 831 | - public static function get_current_view_mode(): string { | |
| 832 | - $user_id = get_current_user_id(); | |
| 833 | - $can_switch = self::can_switch_mode( $user_id ); | |
| 834 | - $default_mode = $can_switch ? self::VIEW_AS_INSTRUCTOR : self::VIEW_AS_STUDENT; | |
| 835 | - $current_mode = get_user_meta( $user_id, self::VIEW_MODE_USER_META, true ); | |
| 836 | - | |
| 837 | - if ( $can_switch && in_array( $current_mode, array( self::VIEW_AS_INSTRUCTOR, self::VIEW_AS_STUDENT ), true ) ) { | |
| 838 | - return $current_mode; | |
| 839 | - } | |
| 840 | - | |
| 841 | - if ( self::used_instructor_registration( $user_id ) ) { | |
| 842 | - $instructor_status = tutor_utils()->instructor_status( $user_id, false ); | |
| 843 | - if ( in_array( $instructor_status, array( Instructors_List::STATUS_PENDING, Instructors_List::STATUS_APPROVED ), true ) ) { | |
| 844 | - return self::VIEW_AS_INSTRUCTOR; | |
| 845 | - } | |
| 846 | - } | |
| 847 | - | |
| 848 | - return $default_mode; | |
| 849 | - } | |
| 850 | - | |
| 851 | - /** | |
| 852 | - * Check if the user is in instructor view | |
| 853 | - * | |
| 854 | - * @since 4.0.0 | |
| 855 | - * | |
| 856 | - * @return bool | |
| 857 | - */ | |
| 858 | - public static function is_instructor_view(): bool { | |
| 859 | - return self::VIEW_AS_INSTRUCTOR === self::get_current_view_mode(); | |
| 860 | - } | |
| 861 | - | |
| 862 | - /** | |
| 863 | - * Check if the user is in student view | |
| 864 | - * | |
| 865 | - * @since 4.0.0 | |
| 866 | - * | |
| 867 | - * @return bool | |
| 868 | - */ | |
| 869 | - public static function is_student_view(): bool { | |
| 870 | - return self::VIEW_AS_STUDENT === self::get_current_view_mode(); | |
| 871 | - } | |
| 872 | - | |
| 873 | - /** | |
| 874 | - * Check if the user can switch between learner and instructor dashboard modes. | |
| 875 | - * | |
| 876 | - * @since 4.0.0 | |
| 877 | - * | |
| 878 | - * @param int $user_id User ID. | |
| 879 | - * | |
| 880 | - * @return bool | |
| 881 | - */ | |
| 882 | - public static function can_switch_mode( int $user_id = 0 ): bool { | |
| 883 | - return self::is_admin( $user_id ) || self::is_instructor( $user_id ); | |
| 884 | - } | |
| 885 | - | |
| 886 | - /** | |
| 887 | - * Mark dashboard tour as completed for the current user. | |
| 888 | - * | |
| 889 | - * @since 4.0.0 | |
| 890 | - * | |
| 891 | - * @return void JSON response. | |
| 892 | - */ | |
| 893 | - public function ajax_complete_tour() { | |
| 894 | - tutor_utils()->check_nonce(); | |
| 895 | - | |
| 896 | - update_user_meta( get_current_user_id(), self::TOUR_COMPLETED_META, true ); | |
| 897 | - | |
| 898 | - $this->json_response( __( 'Tour completed', 'tutor' ) ); | |
| 899 | - } | |
| 900 | - | |
| 901 | - /** | |
| 902 | - * If user don't have pro and using tutor login then change the password | |
| 903 | - * reset email link | |
| 904 | - * | |
| 905 | - * @since 4.0.2 | |
| 906 | - * | |
| 907 | - * @param string $message Email message. | |
| 908 | - * @param string $key Reset key. | |
| 909 | - * @param string $user_login User login name. | |
| 910 | - * | |
| 911 | - * @return string | |
| 912 | - */ | |
| 913 | - public function maybe_update_password_reset_link( $message, $key, $user_login ) { | |
| 914 | - if ( tutor()->has_pro && tutor_utils()->is_addon_enabled( 'tutor-email' ) ) { | |
| 915 | - return $message; | |
| 916 | - } | |
| 917 | - | |
| 918 | - $is_tutor_login_enabled = tutor_utils()->get_option( 'enable_tutor_native_login', false ); | |
| 919 | - if ( ! $is_tutor_login_enabled ) { | |
| 920 | - return $message; | |
| 921 | - } | |
| 922 | - | |
| 923 | - $default_url = add_query_arg( | |
| 924 | - array( | |
| 925 | - 'login' => $user_login, | |
| 926 | - 'key' => $key, | |
| 927 | - 'action' => 'rp', | |
| 928 | - ), | |
| 929 | - network_site_url( 'wp-login.php' ) | |
| 930 | - ); | |
| 931 | - | |
| 932 | - $user = get_user_by( 'login', $user_login ); | |
| 933 | - if ( ! $user ) { | |
| 934 | - return $message; | |
| 935 | - } | |
| 936 | - | |
| 937 | - $tutor_reset_url = add_query_arg( | |
| 938 | - array( | |
| 939 | - 'reset_key' => $key, | |
| 940 | - 'user_id' => $user->ID, | |
| 941 | - ), | |
| 942 | - tutor_utils()->tutor_dashboard_url( 'retrieve-password' ) | |
| 943 | - ); | |
| 944 | - | |
| 945 | - $message = str_replace( $default_url, $tutor_reset_url, $message ); | |
| 946 | - | |
| 947 | - return $message; | |
| 948 | 580 | } |
| 949 | 581 | } |