Addons.php
2 years ago
Admin.php
2 years ago
Ajax.php
2 years ago
Announcements.php
3 years ago
Assets.php
2 years ago
Backend_Page_Trait.php
3 years ago
Course.php
1 year ago
Course_Embed.php
3 years ago
Course_Filter.php
1 year ago
Course_List.php
1 year ago
Course_Settings_Tabs.php
3 years ago
Course_Widget.php
3 years ago
Custom_Validation.php
3 years ago
Dashboard.php
3 years ago
FormHandler.php
2 years ago
Frontend.php
2 years ago
Gutenberg.php
3 years ago
Input.php
3 years ago
Instructor.php
2 years ago
Instructors_List.php
1 year ago
Lesson.php
2 years ago
Options_V2.php
1 year ago
Permalink.php
2 years ago
Post_types.php
2 years ago
Private_Course_Access.php
3 years ago
Q_And_A.php
1 year ago
Question_Answers_List.php
3 years ago
Quiz.php
2 years ago
Quiz_Attempts_List.php
2 years ago
RestAPI.php
2 years ago
Reviews.php
3 years ago
Rewrite_Rules.php
2 years ago
Shortcode.php
1 year ago
Student.php
2 years ago
Students_List.php
3 years ago
Taxonomies.php
3 years ago
Template.php
2 years ago
Theme_Compatibility.php
3 years ago
Tools.php
3 years ago
Tools_V2.php
2 years ago
Tutor.php
2 years ago
TutorEDD.php
2 years ago
Tutor_Base.php
2 years ago
Tutor_Setup.php
2 years ago
Upgrader.php
2 years ago
User.php
2 years ago
Utils.php
1 year ago
Video_Stream.php
3 years ago
WhatsNew.php
2 years ago
Withdraw.php
2 years ago
Withdraw_Requests_List.php
3 years ago
WooCommerce.php
2 years ago
User.php
402 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Manage user |
| 4 | * |
| 5 | * @package Tutor\User |
| 6 | * @author Themeum <support@themeum.com> |
| 7 | * @link https://themeum.com |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | |
| 11 | namespace TUTOR; |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * User class |
| 19 | * |
| 20 | * @since 1.0.0 |
| 21 | */ |
| 22 | class User { |
| 23 | |
| 24 | const STUDENT = 'subscriber'; |
| 25 | const INSTRUCTOR = 'tutor_instructor'; |
| 26 | const ADMIN = 'administrator'; |
| 27 | |
| 28 | const REVIEW_POPUP_META = 'tutor_review_course_popup'; |
| 29 | const LAST_LOGIN_META = 'tutor_last_login'; |
| 30 | |
| 31 | /** |
| 32 | * Registration notice |
| 33 | * |
| 34 | * @since 1.0.0 |
| 35 | * |
| 36 | * @var boolean |
| 37 | */ |
| 38 | private static $hide_registration_notice = false; |
| 39 | |
| 40 | /** |
| 41 | * Register hooks |
| 42 | * |
| 43 | * @since 1.0.0 |
| 44 | * @since 2.2.0 $register_hooks param added to resuse the class without hooks register. |
| 45 | * |
| 46 | * @param bool $register_hooks register hooks. |
| 47 | * |
| 48 | * @return void |
| 49 | */ |
| 50 | public function __construct( $register_hooks = true ) { |
| 51 | if ( ! $register_hooks ) { |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) ); |
| 56 | add_action( 'show_user_profile', array( $this, 'edit_user_profile' ), 10, 1 ); |
| 57 | |
| 58 | add_action( 'profile_update', array( $this, 'profile_update' ) ); |
| 59 | add_action( 'set_user_role', array( $this, 'set_user_role' ), 10, 3 ); |
| 60 | |
| 61 | add_action( 'wp_ajax_tutor_user_photo_remove', array( $this, 'tutor_user_photo_remove' ) ); |
| 62 | add_action( 'wp_ajax_tutor_user_photo_upload', array( $this, 'update_user_photo' ) ); |
| 63 | |
| 64 | add_action( 'admin_notices', array( $this, 'show_registration_disabled' ) ); |
| 65 | add_action( 'admin_init', array( $this, 'hide_notices' ) ); |
| 66 | add_action( 'wp_login', array( $this, 'update_user_last_login' ), 10, 2 ); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get meta key name for review popup. |
| 71 | * |
| 72 | * @since 2.4.0 |
| 73 | * |
| 74 | * @param int $course_id course id. |
| 75 | * |
| 76 | * @return string user meta key name. |
| 77 | */ |
| 78 | public static function get_review_popup_meta( $course_id ) { |
| 79 | return self::REVIEW_POPUP_META . '_' . $course_id; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Check user has provided role. |
| 84 | * |
| 85 | * @since 2.2.0 |
| 86 | * |
| 87 | * @param string $role role. |
| 88 | * |
| 89 | * @return boolean |
| 90 | */ |
| 91 | public static function is( string $role ) { |
| 92 | return current_user_can( $role ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Check user has any role. |
| 97 | * |
| 98 | * @since 2.2.0 |
| 99 | * @since 2.6.2 $user_id param added. |
| 100 | * |
| 101 | * @param array $roles roles. |
| 102 | * @param int $user_id user id. |
| 103 | * |
| 104 | * @return boolean |
| 105 | */ |
| 106 | public static function has_any_role( array $roles, $user_id = 0 ) { |
| 107 | $user = get_userdata( tutor_utils()->get_user_id( $user_id ) ); |
| 108 | if ( empty( $user->roles ) || empty( $roles ) ) { |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | foreach ( $roles as $role ) { |
| 113 | if ( in_array( $role, $user->roles, true ) ) { |
| 114 | return true; |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Check user is student. |
| 124 | * |
| 125 | * @since 2.2.0 |
| 126 | * @since 2.6.2 $user_id param added. |
| 127 | * |
| 128 | * @param int $user_id user id. |
| 129 | * |
| 130 | * @return boolean |
| 131 | */ |
| 132 | public static function is_student( $user_id = 0 ) { |
| 133 | return self::has_any_role( array( self::STUDENT ), $user_id ); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Check user is admin. |
| 138 | * |
| 139 | * @since 2.2.0 |
| 140 | * |
| 141 | * @return boolean |
| 142 | */ |
| 143 | public static function is_admin() { |
| 144 | return current_user_can( self::ADMIN ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Check current user is instructor. |
| 149 | * |
| 150 | * @since 2.2.0 |
| 151 | * |
| 152 | * @param bool $is_approved instructor is approved or not. |
| 153 | * |
| 154 | * @return boolean |
| 155 | */ |
| 156 | public static function is_instructor( $is_approved = true ) { |
| 157 | return tutils()->is_instructor( 0, $is_approved ); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Profile layouts |
| 162 | * |
| 163 | * @since 1.0.0 |
| 164 | * |
| 165 | * @var array |
| 166 | */ |
| 167 | private $profile_layout = array( |
| 168 | 'pp-circle', |
| 169 | 'pp-rectangle', |
| 170 | 'no-cp', |
| 171 | ); |
| 172 | |
| 173 | /** |
| 174 | * Include edit user template |
| 175 | * |
| 176 | * @since 1.0.0 |
| 177 | * |
| 178 | * @param mixed $user user. |
| 179 | * |
| 180 | * @return void |
| 181 | */ |
| 182 | public function edit_user_profile( $user ) { |
| 183 | include tutor()->path . 'views/metabox/user-profile-fields.php'; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Delete existing user's photo |
| 188 | * |
| 189 | * @since 1.0.0 |
| 190 | * |
| 191 | * @param int $user_id user id. |
| 192 | * @param string $type photo type. |
| 193 | * |
| 194 | * @return void |
| 195 | */ |
| 196 | private function delete_existing_user_photo( $user_id, $type ) { |
| 197 | $meta_key = 'cover_photo' == $type ? '_tutor_cover_photo' : '_tutor_profile_photo'; |
| 198 | $photo_id = get_user_meta( $user_id, $meta_key, true ); |
| 199 | is_numeric( $photo_id ) ? wp_delete_attachment( $photo_id, true ) : 0; |
| 200 | delete_user_meta( $user_id, $meta_key ); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * User photo remove |
| 205 | * |
| 206 | * @since 1.0.0 |
| 207 | * |
| 208 | * @return void |
| 209 | */ |
| 210 | public function tutor_user_photo_remove() { |
| 211 | tutor_utils()->checking_nonce(); |
| 212 | $this->delete_existing_user_photo( |
| 213 | get_current_user_id(), |
| 214 | Input::post( 'photo_type', '' ) |
| 215 | ); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * User photo update |
| 220 | * |
| 221 | * @since 1.0.0 |
| 222 | * |
| 223 | * @return void |
| 224 | */ |
| 225 | public function update_user_photo() { |
| 226 | tutor_utils()->checking_nonce(); |
| 227 | |
| 228 | $user_id = get_current_user_id(); |
| 229 | $photo_type = Input::post( 'photo_type', '' ); |
| 230 | $meta_key = 'cover_photo' === $photo_type ? '_tutor_cover_photo' : '_tutor_profile_photo'; |
| 231 | |
| 232 | /** |
| 233 | * Photo Update from profile |
| 234 | */ |
| 235 | $photo = tutor_utils()->array_get( 'photo_file', $_FILES ); |
| 236 | $photo_size = tutor_utils()->array_get( 'size', $photo ); |
| 237 | $photo_type = tutor_utils()->array_get( 'type', $photo ); |
| 238 | |
| 239 | if ( $photo_size && strpos( $photo_type, 'image' ) !== false ) { |
| 240 | if ( ! function_exists( 'wp_handle_upload' ) ) { |
| 241 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 242 | } |
| 243 | $upload_overrides = array( 'test_form' => false ); |
| 244 | $movefile = wp_handle_upload( $photo, $upload_overrides ); |
| 245 | |
| 246 | if ( $movefile && ! isset( $movefile['error'] ) ) { |
| 247 | $file_path = tutor_utils()->array_get( 'file', $movefile ); |
| 248 | $file_url = tutor_utils()->array_get( 'url', $movefile ); |
| 249 | $mime_type = ''; |
| 250 | if ( file_exists( $file_path ) ) { |
| 251 | $image_info = getimagesize( $file_path ); |
| 252 | $mime_type = is_array( $image_info ) && count( $image_info ) ? $image_info['mime'] : ''; |
| 253 | } |
| 254 | |
| 255 | $media_id = wp_insert_attachment( |
| 256 | array( |
| 257 | 'guid' => $file_path, |
| 258 | 'post_mime_type' => $mime_type, |
| 259 | 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_url ) ), |
| 260 | 'post_content' => '', |
| 261 | 'post_status' => 'inherit', |
| 262 | ), |
| 263 | $file_path, |
| 264 | 0 |
| 265 | ); |
| 266 | |
| 267 | if ( $media_id ) { |
| 268 | // wp_generate_attachment_metadata() won't work if you do not include this file. |
| 269 | require_once ABSPATH . 'wp-admin/includes/image.php'; |
| 270 | |
| 271 | // Generate and save the attachment metas into the database. |
| 272 | wp_update_attachment_metadata( $media_id, wp_generate_attachment_metadata( $media_id, $file_path ) ); |
| 273 | |
| 274 | // Update it to user profile. |
| 275 | $this->delete_existing_user_photo( $user_id, Input::post( 'photo_type', '' ) ); |
| 276 | update_user_meta( $user_id, $meta_key, $media_id ); |
| 277 | |
| 278 | exit( wp_json_encode( array( 'status' => 'success' ) ) ); |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Profile update |
| 286 | * |
| 287 | * @since 1.0.0 |
| 288 | * |
| 289 | * @param int $user_id user id. |
| 290 | * |
| 291 | * @return void |
| 292 | */ |
| 293 | public function profile_update( $user_id ) { |
| 294 | if ( 'tutor_profile_update_by_wp' !== Input::post( 'tutor_action' ) ) { |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | $_tutor_profile_job_title = Input::post( '_tutor_profile_job_title', '' ); |
| 299 | $_tutor_profile_bio = Input::post( '_tutor_profile_bio', '', Input::TYPE_KSES_POST ); |
| 300 | $_tutor_profile_image = Input::post( '_tutor_profile_photo', '', Input::TYPE_KSES_POST ); |
| 301 | |
| 302 | update_user_meta( $user_id, '_tutor_profile_job_title', $_tutor_profile_job_title ); |
| 303 | update_user_meta( $user_id, '_tutor_profile_bio', $_tutor_profile_bio ); |
| 304 | update_user_meta( $user_id, '_tutor_profile_photo', $_tutor_profile_image ); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Set user role |
| 309 | * |
| 310 | * @since 1.0.0 |
| 311 | * |
| 312 | * @param int $user_id user id. |
| 313 | * @param string $role user role. |
| 314 | * @param array $old_roles old role. |
| 315 | * |
| 316 | * @return void |
| 317 | */ |
| 318 | public function set_user_role( $user_id, $role, $old_roles ) { |
| 319 | $instructor_role = tutor()->instructor_role; |
| 320 | |
| 321 | if ( $role === $instructor_role || in_array( $instructor_role, $old_roles ) ) { |
| 322 | tutor_utils()->add_instructor_role( $user_id ); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Hide notices |
| 328 | * |
| 329 | * @since 1.0.0 |
| 330 | * |
| 331 | * @return void |
| 332 | */ |
| 333 | public function hide_notices() { |
| 334 | $hide_notice = Input::get( 'tutor-hide-notice', '' ); |
| 335 | $is_register_enabled = Input::get( 'tutor-registration', '' ); |
| 336 | $has_manage_cap = current_user_can( 'manage_options' ); |
| 337 | |
| 338 | if ( $has_manage_cap && is_admin() && 'registration' === $hide_notice ) { |
| 339 | tutor_utils()->checking_nonce( 'get' ); |
| 340 | |
| 341 | if ( 'enable' === $is_register_enabled ) { |
| 342 | update_option( 'users_can_register', 1 ); |
| 343 | } else { |
| 344 | self::$hide_registration_notice = true; |
| 345 | setcookie( 'tutor_notice_hide_registration', 1, time() + MONTH_IN_SECONDS, tutor()->basepath ); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * Show registration disabled |
| 352 | * |
| 353 | * @since 1.0.0 |
| 354 | * |
| 355 | * @return void |
| 356 | */ |
| 357 | public function show_registration_disabled() { |
| 358 | if ( self::$hide_registration_notice || |
| 359 | ( '0' !== get_option( 'users_can_register' ) ) || |
| 360 | isset( $_COOKIE['tutor_notice_hide_registration'] ) || |
| 361 | ! current_user_can( 'manage_options' ) |
| 362 | ) { |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | $hide_url = wp_nonce_url( add_query_arg( 'tutor-hide-notice', 'registration' ), tutor()->nonce_action, tutor()->nonce ); |
| 367 | ?> |
| 368 | <div class="wrap tutor-user-registration-notice-wrapper"> |
| 369 | <div class="tutor-user-registration-notice"> |
| 370 | <div> |
| 371 | <img src="<?php echo esc_url( tutor()->url . 'assets/images/icon-info-round.svg' ); ?>"/> |
| 372 | </div> |
| 373 | <div> |
| 374 | <?php echo wp_kses( 'As membership is turned off, students and instructors will not be able to sign up. <strong>Press Enable</strong> or go to <strong>Settings > General > Membership</strong> and enable "Anyone can register".', array( 'strong' => true ) ); ?> |
| 375 | </div> |
| 376 | <div> |
| 377 | <a href="<?php echo esc_url( add_query_arg( 'tutor-registration', 'enable', $hide_url ) ); ?>"><?php esc_html_e( 'Enable', 'tutor' ); ?></a> |
| 378 | <hr/> |
| 379 | <a href="<?php echo esc_url( $hide_url ); ?>"> |
| 380 | <?php esc_html_e( 'Dismiss', 'tutor' ); ?> |
| 381 | </a> |
| 382 | </div> |
| 383 | </div> |
| 384 | </div> |
| 385 | <?php |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Set the user last active timestamp to now. |
| 390 | * |
| 391 | * @since 2.5.0 |
| 392 | * |
| 393 | * @param string $user_login active user name. |
| 394 | * @param \WP_User $user User object data. |
| 395 | * |
| 396 | * @return void |
| 397 | */ |
| 398 | public function update_user_last_login( $user_login, $user ) { |
| 399 | update_user_meta( $user->ID, self::LAST_LOGIN_META, time() ); |
| 400 | } |
| 401 | } |
| 402 |