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
2 years ago
Course_Embed.php
3 years ago
Course_Filter.php
3 years ago
Course_List.php
2 years 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
3 years ago
Gutenberg.php
3 years ago
Input.php
3 years ago
Instructor.php
2 years ago
Instructors_List.php
3 years ago
Lesson.php
2 years ago
Options_V2.php
2 years ago
Post_types.php
2 years ago
Private_Course_Access.php
3 years ago
Q_and_A.php
3 years ago
Question_Answers_List.php
3 years ago
Quiz.php
2 years ago
Quiz_Attempts_List.php
2 years ago
RestAPI.php
3 years ago
Reviews.php
3 years ago
Rewrite_Rules.php
3 years ago
Shortcode.php
2 years 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
3 years ago
Tutor.php
3 years ago
TutorEDD.php
2 years ago
Tutor_Base.php
3 years ago
Tutor_Setup.php
3 years ago
Upgrader.php
3 years ago
User.php
3 years ago
Utils.php
2 years ago
Video_Stream.php
3 years ago
Withdraw.php
2 years ago
Withdraw_Requests_List.php
3 years ago
WooCommerce.php
2 years ago
User.php
365 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 | /** |
| 29 | * Registration notice |
| 30 | * |
| 31 | * @since 1.0.0 |
| 32 | * |
| 33 | * @var boolean |
| 34 | */ |
| 35 | private static $hide_registration_notice = false; |
| 36 | |
| 37 | /** |
| 38 | * Register hooks |
| 39 | * |
| 40 | * @since 1.0.0 |
| 41 | * @since 2.2.0 $register_hooks param added to resuse the class without hooks register. |
| 42 | * |
| 43 | * @param bool $register_hooks register hooks. |
| 44 | * |
| 45 | * @return void |
| 46 | */ |
| 47 | public function __construct( $register_hooks = true ) { |
| 48 | if ( ! $register_hooks ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) ); |
| 53 | add_action( 'show_user_profile', array( $this, 'edit_user_profile' ), 10, 1 ); |
| 54 | |
| 55 | add_action( 'profile_update', array( $this, 'profile_update' ) ); |
| 56 | add_action( 'set_user_role', array( $this, 'set_user_role' ), 10, 3 ); |
| 57 | |
| 58 | add_action( 'wp_ajax_tutor_user_photo_remove', array( $this, 'tutor_user_photo_remove' ) ); |
| 59 | add_action( 'wp_ajax_tutor_user_photo_upload', array( $this, 'update_user_photo' ) ); |
| 60 | |
| 61 | add_action( 'admin_notices', array( $this, 'show_registration_disabled' ) ); |
| 62 | add_action( 'admin_init', array( $this, 'hide_notices' ) ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Check user has provided role. |
| 67 | * |
| 68 | * @since 2.2.0 |
| 69 | * |
| 70 | * @param string $role role. |
| 71 | * |
| 72 | * @return boolean |
| 73 | */ |
| 74 | public static function is( string $role ) { |
| 75 | return current_user_can( $role ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Check user has any role. |
| 80 | * |
| 81 | * @since 2.2.0 |
| 82 | * |
| 83 | * @param array $roles roles. |
| 84 | * |
| 85 | * @return boolean |
| 86 | */ |
| 87 | public static function has_any_role( array $roles ) { |
| 88 | $user = wp_get_current_user(); |
| 89 | if ( empty( $user->roles ) || empty( $roles ) ) { |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | foreach ( $roles as $role ) { |
| 94 | if ( in_array( $role, $user->roles, true ) ) { |
| 95 | return true; |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Check user is student. |
| 105 | * |
| 106 | * @since 2.2.0 |
| 107 | * |
| 108 | * @return boolean |
| 109 | */ |
| 110 | public static function is_student() { |
| 111 | return current_user_can( self::STUDENT ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Check user is admin. |
| 116 | * |
| 117 | * @since 2.2.0 |
| 118 | * |
| 119 | * @return boolean |
| 120 | */ |
| 121 | public static function is_admin() { |
| 122 | return current_user_can( self::ADMIN ); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Check current user is instructor. |
| 127 | * |
| 128 | * @since 2.2.0 |
| 129 | * |
| 130 | * @param bool $is_approved instructor is approved or not. |
| 131 | * |
| 132 | * @return boolean |
| 133 | */ |
| 134 | public static function is_instructor( $is_approved = true ) { |
| 135 | return tutils()->is_instructor( 0, $is_approved ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Profile layouts |
| 140 | * |
| 141 | * @since 1.0.0 |
| 142 | * |
| 143 | * @var array |
| 144 | */ |
| 145 | private $profile_layout = array( |
| 146 | 'pp-circle', |
| 147 | 'pp-rectangle', |
| 148 | 'no-cp', |
| 149 | ); |
| 150 | |
| 151 | /** |
| 152 | * Include edit user template |
| 153 | * |
| 154 | * @since 1.0.0 |
| 155 | * |
| 156 | * @param mixed $user user. |
| 157 | * |
| 158 | * @return void |
| 159 | */ |
| 160 | public function edit_user_profile( $user ) { |
| 161 | include tutor()->path . 'views/metabox/user-profile-fields.php'; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Delete existing user's photo |
| 166 | * |
| 167 | * @since 1.0.0 |
| 168 | * |
| 169 | * @param int $user_id user id. |
| 170 | * @param string $type photo type. |
| 171 | * |
| 172 | * @return void |
| 173 | */ |
| 174 | private function delete_existing_user_photo( $user_id, $type ) { |
| 175 | $meta_key = 'cover_photo' == $type ? '_tutor_cover_photo' : '_tutor_profile_photo'; |
| 176 | $photo_id = get_user_meta( $user_id, $meta_key, true ); |
| 177 | is_numeric( $photo_id ) ? wp_delete_attachment( $photo_id, true ) : 0; |
| 178 | delete_user_meta( $user_id, $meta_key ); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * User photo remove |
| 183 | * |
| 184 | * @since 1.0.0 |
| 185 | * |
| 186 | * @return void |
| 187 | */ |
| 188 | public function tutor_user_photo_remove() { |
| 189 | tutor_utils()->checking_nonce(); |
| 190 | $this->delete_existing_user_photo( |
| 191 | get_current_user_id(), |
| 192 | Input::post( 'photo_type', '' ) |
| 193 | ); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * User photo update |
| 198 | * |
| 199 | * @since 1.0.0 |
| 200 | * |
| 201 | * @return void |
| 202 | */ |
| 203 | public function update_user_photo() { |
| 204 | tutor_utils()->checking_nonce(); |
| 205 | |
| 206 | $user_id = get_current_user_id(); |
| 207 | $photo_type = Input::post( 'photo_type', '' ); |
| 208 | $meta_key = 'cover_photo' === $photo_type ? '_tutor_cover_photo' : '_tutor_profile_photo'; |
| 209 | |
| 210 | /** |
| 211 | * Photo Update from profile |
| 212 | */ |
| 213 | $photo = tutor_utils()->array_get( 'photo_file', $_FILES ); |
| 214 | $photo_size = tutor_utils()->array_get( 'size', $photo ); |
| 215 | $photo_type = tutor_utils()->array_get( 'type', $photo ); |
| 216 | |
| 217 | if ( $photo_size && strpos( $photo_type, 'image' ) !== false ) { |
| 218 | if ( ! function_exists( 'wp_handle_upload' ) ) { |
| 219 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 220 | } |
| 221 | $upload_overrides = array( 'test_form' => false ); |
| 222 | $movefile = wp_handle_upload( $photo, $upload_overrides ); |
| 223 | |
| 224 | if ( $movefile && ! isset( $movefile['error'] ) ) { |
| 225 | $file_path = tutor_utils()->array_get( 'file', $movefile ); |
| 226 | $file_url = tutor_utils()->array_get( 'url', $movefile ); |
| 227 | $mime_type = ''; |
| 228 | if ( file_exists( $file_path ) ) { |
| 229 | $image_info = getimagesize( $file_path ); |
| 230 | $mime_type = is_array( $image_info ) && count( $image_info ) ? $image_info['mime'] : ''; |
| 231 | } |
| 232 | |
| 233 | $media_id = wp_insert_attachment( |
| 234 | array( |
| 235 | 'guid' => $file_path, |
| 236 | 'post_mime_type' => $mime_type, |
| 237 | 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_url ) ), |
| 238 | 'post_content' => '', |
| 239 | 'post_status' => 'inherit', |
| 240 | ), |
| 241 | $file_path, |
| 242 | 0 |
| 243 | ); |
| 244 | |
| 245 | if ( $media_id ) { |
| 246 | // wp_generate_attachment_metadata() won't work if you do not include this file. |
| 247 | require_once ABSPATH . 'wp-admin/includes/image.php'; |
| 248 | |
| 249 | // Generate and save the attachment metas into the database. |
| 250 | wp_update_attachment_metadata( $media_id, wp_generate_attachment_metadata( $media_id, $file_path ) ); |
| 251 | |
| 252 | // Update it to user profile. |
| 253 | $this->delete_existing_user_photo( $user_id, Input::post( 'photo_type', '' ) ); |
| 254 | update_user_meta( $user_id, $meta_key, $media_id ); |
| 255 | |
| 256 | exit( wp_json_encode( array( 'status' => 'success' ) ) ); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Profile update |
| 264 | * |
| 265 | * @since 1.0.0 |
| 266 | * |
| 267 | * @param int $user_id user id. |
| 268 | * |
| 269 | * @return void |
| 270 | */ |
| 271 | public function profile_update( $user_id ) { |
| 272 | if ( tutor_utils()->array_get( 'tutor_action', $_POST ) !== 'tutor_profile_update_by_wp' ) { |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | $_tutor_profile_job_title = Input::post( '_tutor_profile_job_title', '' ); |
| 277 | $_tutor_profile_bio = Input::post( '_tutor_profile_bio', '', Input::TYPE_KSES_POST ); |
| 278 | $_tutor_profile_image = Input::post( '_tutor_profile_photo', '', Input::TYPE_KSES_POST ); |
| 279 | |
| 280 | update_user_meta( $user_id, '_tutor_profile_job_title', $_tutor_profile_job_title ); |
| 281 | update_user_meta( $user_id, '_tutor_profile_bio', $_tutor_profile_bio ); |
| 282 | update_user_meta( $user_id, '_tutor_profile_photo', $_tutor_profile_image ); |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Set user role |
| 287 | * |
| 288 | * @since 1.0.0 |
| 289 | * |
| 290 | * @param int $user_id user id. |
| 291 | * @param string $role user role. |
| 292 | * @param array $old_roles old role. |
| 293 | * |
| 294 | * @return void |
| 295 | */ |
| 296 | public function set_user_role( $user_id, $role, $old_roles ) { |
| 297 | $instructor_role = tutor()->instructor_role; |
| 298 | |
| 299 | if ( $role === $instructor_role || in_array( $instructor_role, $old_roles ) ) { |
| 300 | tutor_utils()->add_instructor_role( $user_id ); |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Hide notices |
| 306 | * |
| 307 | * @since 1.0.0 |
| 308 | * |
| 309 | * @return void |
| 310 | */ |
| 311 | public function hide_notices() { |
| 312 | $hide_notice = Input::get( 'tutor-hide-notice', '' ); |
| 313 | $is_register_enabled = Input::get( 'tutor-registration', '' ); |
| 314 | if ( is_admin() && 'registration' === $hide_notice ) { |
| 315 | tutor_utils()->checking_nonce( 'get' ); |
| 316 | |
| 317 | if ( 'enable' === $is_register_enabled ) { |
| 318 | update_option( 'users_can_register', 1 ); |
| 319 | } else { |
| 320 | self::$hide_registration_notice = true; |
| 321 | setcookie( 'tutor_notice_hide_registration', 1, time() + ( 86400 * 30 ), tutor()->basepath ); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Show registration disabled |
| 328 | * |
| 329 | * @since 1.0.0 |
| 330 | * |
| 331 | * @return void |
| 332 | */ |
| 333 | public function show_registration_disabled() { |
| 334 | if ( self::$hide_registration_notice || |
| 335 | ! tutor_utils()->is_tutor_dashboard() || |
| 336 | get_option( 'users_can_register' ) || |
| 337 | isset( $_COOKIE['tutor_notice_hide_registration'] ) || |
| 338 | ! current_user_can( 'manage_options' ) |
| 339 | ) { |
| 340 | return; |
| 341 | } |
| 342 | |
| 343 | $hide_url = wp_nonce_url( add_query_arg( 'tutor-hide-notice', 'registration' ), tutor()->nonce_action, tutor()->nonce ); |
| 344 | ?> |
| 345 | <div class="wrap tutor-user-registration-notice-wrapper"> |
| 346 | <div class="tutor-user-registration-notice"> |
| 347 | <div> |
| 348 | <img src="<?php echo esc_url( tutor()->url . 'assets/images/icon-info-round.svg' ); ?>"/> |
| 349 | </div> |
| 350 | <div> |
| 351 | <?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 ) ); ?> |
| 352 | </div> |
| 353 | <div> |
| 354 | <a href="<?php echo esc_url( add_query_arg( 'tutor-registration', 'enable', $hide_url ) ); ?>"><?php esc_html_e( 'Enable', 'tutor' ); ?></a> |
| 355 | <hr/> |
| 356 | <a href="<?php echo esc_url( $hide_url ); ?>"> |
| 357 | <?php esc_html_e( 'Dismiss', 'tutor' ); ?> |
| 358 | </a> |
| 359 | </div> |
| 360 | </div> |
| 361 | </div> |
| 362 | <?php |
| 363 | } |
| 364 | } |
| 365 |