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