Addons.php
3 years ago
Admin.php
3 years ago
Ajax.php
3 years ago
Announcements.php
3 years ago
Assets.php
3 years ago
Backend_Page_Trait.php
3 years ago
Course.php
3 years ago
Course_Embed.php
3 years ago
Course_Filter.php
3 years ago
Course_List.php
3 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
3 years ago
Frontend.php
3 years ago
Gutenberg.php
3 years ago
Input.php
3 years ago
Instructor.php
3 years ago
Instructors_List.php
3 years ago
Lesson.php
3 years ago
Options_V2.php
3 years ago
Post_types.php
3 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
3 years ago
Quiz_Attempts_List.php
3 years ago
RestAPI.php
3 years ago
Reviews.php
3 years ago
Rewrite_Rules.php
3 years ago
Shortcode.php
3 years ago
Student.php
3 years ago
Students_List.php
3 years ago
Taxonomies.php
3 years ago
Template.php
3 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
3 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
3 years ago
Video_Stream.php
3 years ago
Withdraw.php
3 years ago
Withdraw_Requests_List.php
3 years ago
WooCommerce.php
3 years ago
User.php
279 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 | /** |
| 25 | * Registration notice |
| 26 | * |
| 27 | * @since 1.0.0 |
| 28 | * |
| 29 | * @var boolean |
| 30 | */ |
| 31 | private static $hide_registration_notice = false; |
| 32 | |
| 33 | /** |
| 34 | * Register hooks |
| 35 | * |
| 36 | * @since 1.0.0 |
| 37 | */ |
| 38 | public function __construct() { |
| 39 | add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) ); |
| 40 | add_action( 'show_user_profile', array( $this, 'edit_user_profile' ), 10, 1 ); |
| 41 | |
| 42 | add_action( 'profile_update', array( $this, 'profile_update' ) ); |
| 43 | add_action( 'set_user_role', array( $this, 'set_user_role' ), 10, 3 ); |
| 44 | |
| 45 | add_action( 'wp_ajax_tutor_user_photo_remove', array( $this, 'tutor_user_photo_remove' ) ); |
| 46 | add_action( 'wp_ajax_tutor_user_photo_upload', array( $this, 'update_user_photo' ) ); |
| 47 | |
| 48 | add_action( 'admin_notices', array( $this, 'show_registration_disabled' ) ); |
| 49 | add_action( 'admin_init', array( $this, 'hide_notices' ) ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Profile layouts |
| 54 | * |
| 55 | * @since 1.0.0 |
| 56 | * |
| 57 | * @var array |
| 58 | */ |
| 59 | private $profile_layout = array( |
| 60 | 'pp-circle', |
| 61 | 'pp-rectangle', |
| 62 | 'no-cp', |
| 63 | ); |
| 64 | |
| 65 | /** |
| 66 | * Include edit user template |
| 67 | * |
| 68 | * @since 1.0.0 |
| 69 | * |
| 70 | * @param mixed $user user. |
| 71 | * |
| 72 | * @return void |
| 73 | */ |
| 74 | public function edit_user_profile( $user ) { |
| 75 | include tutor()->path . 'views/metabox/user-profile-fields.php'; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Delete existing user's photo |
| 80 | * |
| 81 | * @since 1.0.0 |
| 82 | * |
| 83 | * @param int $user_id user id. |
| 84 | * @param string $type photo type. |
| 85 | * |
| 86 | * @return void |
| 87 | */ |
| 88 | private function delete_existing_user_photo( $user_id, $type ) { |
| 89 | $meta_key = 'cover_photo' == $type ? '_tutor_cover_photo' : '_tutor_profile_photo'; |
| 90 | $photo_id = get_user_meta( $user_id, $meta_key, true ); |
| 91 | is_numeric( $photo_id ) ? wp_delete_attachment( $photo_id, true ) : 0; |
| 92 | delete_user_meta( $user_id, $meta_key ); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * User photo remove |
| 97 | * |
| 98 | * @since 1.0.0 |
| 99 | * |
| 100 | * @return void |
| 101 | */ |
| 102 | public function tutor_user_photo_remove() { |
| 103 | tutor_utils()->checking_nonce(); |
| 104 | $this->delete_existing_user_photo( |
| 105 | get_current_user_id(), |
| 106 | Input::post( 'photo_type', '' ) |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * User photo update |
| 112 | * |
| 113 | * @since 1.0.0 |
| 114 | * |
| 115 | * @return void |
| 116 | */ |
| 117 | public function update_user_photo() { |
| 118 | tutor_utils()->checking_nonce(); |
| 119 | |
| 120 | $user_id = get_current_user_id(); |
| 121 | $photo_type = Input::post( 'photo_type', '' ); |
| 122 | $meta_key = 'cover_photo' === $photo_type ? '_tutor_cover_photo' : '_tutor_profile_photo'; |
| 123 | |
| 124 | /** |
| 125 | * Photo Update from profile |
| 126 | */ |
| 127 | $photo = tutor_utils()->array_get( 'photo_file', $_FILES ); |
| 128 | $photo_size = tutor_utils()->array_get( 'size', $photo ); |
| 129 | $photo_type = tutor_utils()->array_get( 'type', $photo ); |
| 130 | |
| 131 | if ( $photo_size && strpos( $photo_type, 'image' ) !== false ) { |
| 132 | if ( ! function_exists( 'wp_handle_upload' ) ) { |
| 133 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 134 | } |
| 135 | $upload_overrides = array( 'test_form' => false ); |
| 136 | $movefile = wp_handle_upload( $photo, $upload_overrides ); |
| 137 | |
| 138 | if ( $movefile && ! isset( $movefile['error'] ) ) { |
| 139 | $file_path = tutor_utils()->array_get( 'file', $movefile ); |
| 140 | $file_url = tutor_utils()->array_get( 'url', $movefile ); |
| 141 | $mime_type = ''; |
| 142 | if ( file_exists( $file_path ) ) { |
| 143 | $image_info = getimagesize( $file_path ); |
| 144 | $mime_type = is_array( $image_info ) && count( $image_info ) ? $image_info['mime'] : ''; |
| 145 | } |
| 146 | |
| 147 | $media_id = wp_insert_attachment( |
| 148 | array( |
| 149 | 'guid' => $file_path, |
| 150 | 'post_mime_type' => $mime_type, |
| 151 | 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_url ) ), |
| 152 | 'post_content' => '', |
| 153 | 'post_status' => 'inherit', |
| 154 | ), |
| 155 | $file_path, |
| 156 | 0 |
| 157 | ); |
| 158 | |
| 159 | if ( $media_id ) { |
| 160 | // wp_generate_attachment_metadata() won't work if you do not include this file. |
| 161 | require_once ABSPATH . 'wp-admin/includes/image.php'; |
| 162 | |
| 163 | // Generate and save the attachment metas into the database. |
| 164 | wp_update_attachment_metadata( $media_id, wp_generate_attachment_metadata( $media_id, $file_path ) ); |
| 165 | |
| 166 | // Update it to user profile. |
| 167 | $this->delete_existing_user_photo( $user_id, Input::post( 'photo_type', '' ) ); |
| 168 | update_user_meta( $user_id, $meta_key, $media_id ); |
| 169 | |
| 170 | exit( wp_json_encode( array( 'status' => 'success' ) ) ); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Profile update |
| 178 | * |
| 179 | * @since 1.0.0 |
| 180 | * |
| 181 | * @param int $user_id user id. |
| 182 | * |
| 183 | * @return void |
| 184 | */ |
| 185 | public function profile_update( $user_id ) { |
| 186 | if ( tutor_utils()->array_get( 'tutor_action', $_POST ) !== 'tutor_profile_update_by_wp' ) { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | $_tutor_profile_job_title = Input::post( '_tutor_profile_job_title', '' ); |
| 191 | $_tutor_profile_bio = Input::post( '_tutor_profile_bio', '', Input::TYPE_KSES_POST ); |
| 192 | $_tutor_profile_image = Input::post( '_tutor_profile_photo', '', Input::TYPE_KSES_POST ); |
| 193 | |
| 194 | update_user_meta( $user_id, '_tutor_profile_job_title', $_tutor_profile_job_title ); |
| 195 | update_user_meta( $user_id, '_tutor_profile_bio', $_tutor_profile_bio ); |
| 196 | update_user_meta( $user_id, '_tutor_profile_photo', $_tutor_profile_image ); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Set user role |
| 201 | * |
| 202 | * @since 1.0.0 |
| 203 | * |
| 204 | * @param int $user_id user id. |
| 205 | * @param string $role user role. |
| 206 | * @param string $old_roles old role. |
| 207 | * |
| 208 | * @return void |
| 209 | */ |
| 210 | public function set_user_role( $user_id, $role, $old_roles ) { |
| 211 | $instructor_role = tutor()->instructor_role; |
| 212 | |
| 213 | if ( $role === $instructor_role || in_array( $instructor_role, $old_roles ) ) { |
| 214 | tutor_utils()->add_instructor_role( $user_id ); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Hide notices |
| 220 | * |
| 221 | * @since 1.0.0 |
| 222 | * |
| 223 | * @return void |
| 224 | */ |
| 225 | public function hide_notices() { |
| 226 | $hide_notice = Input::get( 'tutor-hide-notice', '' ); |
| 227 | $is_register_enabled = Input::get( 'tutor-registration', '' ); |
| 228 | if ( is_admin() && 'registration' === $hide_notice ) { |
| 229 | tutor_utils()->checking_nonce( 'get' ); |
| 230 | |
| 231 | if ( 'enable' === $is_register_enabled ) { |
| 232 | update_option( 'users_can_register', 1 ); |
| 233 | } else { |
| 234 | self::$hide_registration_notice = true; |
| 235 | setcookie( 'tutor_notice_hide_registration', 1, time() + ( 86400 * 30 ), tutor()->basepath ); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Show registration disabled |
| 242 | * |
| 243 | * @since 1.0.0 |
| 244 | * |
| 245 | * @return void |
| 246 | */ |
| 247 | public function show_registration_disabled() { |
| 248 | if ( self::$hide_registration_notice || |
| 249 | ! tutor_utils()->is_tutor_dashboard() || |
| 250 | get_option( 'users_can_register' ) || |
| 251 | isset( $_COOKIE['tutor_notice_hide_registration'] ) || |
| 252 | ! current_user_can( 'manage_options' ) |
| 253 | ) { |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | $hide_url = wp_nonce_url( add_query_arg( 'tutor-hide-notice', 'registration' ), tutor()->nonce_action, tutor()->nonce ); |
| 258 | ?> |
| 259 | <div class="wrap tutor-user-registration-notice-wrapper"> |
| 260 | <div class="tutor-user-registration-notice"> |
| 261 | <div> |
| 262 | <img src="<?php echo esc_url( tutor()->url . 'assets/images/icon-info-round.svg' ); ?>"/> |
| 263 | </div> |
| 264 | <div> |
| 265 | <?php esc_html_e( '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".' ); ?> |
| 266 | </div> |
| 267 | <div> |
| 268 | <a href="<?php echo esc_url( add_query_arg( 'tutor-registration', 'enable', $hide_url ) ); ?>"><?php esc_html_e( 'Enable', 'tutor' ); ?></a> |
| 269 | <hr/> |
| 270 | <a href="<?php echo esc_url( $hide_url ); ?>"> |
| 271 | <?php esc_html_e( 'Dismiss', 'tutor' ); ?> |
| 272 | </a> |
| 273 | </div> |
| 274 | </div> |
| 275 | </div> |
| 276 | <?php |
| 277 | } |
| 278 | } |
| 279 |